From 259e17efd9379b97da3cffe33c59e6fc317289b2 Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:29:22 +0300 Subject: [PATCH] chore: move --- pkg/storer/sample.go | 122 +++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/pkg/storer/sample.go b/pkg/storer/sample.go index 85e2e477b1e..717d9254721 100644 --- a/pkg/storer/sample.go +++ b/pkg/storer/sample.go @@ -42,67 +42,6 @@ type Sample struct { Items []SampleItem } -// RandSample returns Sample with random values. -func RandSample(t *testing.T, anchor []byte) Sample { - t.Helper() - - chunks := make([]swarm.Chunk, SampleSize) - for i := 0; i < SampleSize; i++ { - ch := chunk.GenerateTestRandomChunk() - if i%3 == 0 { - ch = chunk.GenerateTestRandomSoChunk(t, ch) - } - chunks[i] = ch - } - - sample, err := MakeSampleUsingChunks(chunks, anchor) - if err != nil { - t.Fatal(err) - } - - return sample -} - -// MakeSampleUsingChunks returns Sample constructed using supplied chunks. -func MakeSampleUsingChunks(chunks []swarm.Chunk, anchor []byte) (Sample, error) { - prefixHasherFactory := func() hash.Hash { - return swarm.NewPrefixHasher(anchor) - } - items := make([]SampleItem, len(chunks)) - for i, ch := range chunks { - tr, err := transformedAddress(bmt.NewHasher(prefixHasherFactory), ch, getChunkType(ch)) - if err != nil { - return Sample{}, err - } - - items[i] = SampleItem{ - TransformedAddress: tr, - ChunkAddress: ch.Address(), - ChunkData: ch.Data(), - Stamp: newStamp(ch.Stamp()), - } - } - - sort.Slice(items, func(i, j int) bool { - return items[i].TransformedAddress.Compare(items[j].TransformedAddress) == -1 - }) - - return Sample{Items: items}, nil -} - -func newStamp(s swarm.Stamp) *postage.Stamp { - return postage.NewStamp(s.BatchID(), s.Index(), s.Timestamp(), s.Sig()) -} - -func getChunkType(chunk swarm.Chunk) swarm.ChunkType { - if cac.Valid(chunk) { - return swarm.ChunkTypeContentAddressed - } else if soc.Valid(chunk) { - return swarm.ChunkTypeSingleOwner - } - return swarm.ChunkTypeUnspecified -} - // ReserveSample generates the sample of reserve storage of a node required for the // storage incentives agent to participate in the lottery round. In order to generate // this sample we need to iterate through all the chunks in the node's reserve and @@ -435,3 +374,64 @@ func (s *SampleStats) add(other SampleStats) { s.ChunkLoadFailed += other.ChunkLoadFailed s.StampLoadFailed += other.StampLoadFailed } + +// RandSample returns Sample with random values. +func RandSample(t *testing.T, anchor []byte) Sample { + t.Helper() + + chunks := make([]swarm.Chunk, SampleSize) + for i := 0; i < SampleSize; i++ { + ch := chunk.GenerateTestRandomChunk() + if i%3 == 0 { + ch = chunk.GenerateTestRandomSoChunk(t, ch) + } + chunks[i] = ch + } + + sample, err := MakeSampleUsingChunks(chunks, anchor) + if err != nil { + t.Fatal(err) + } + + return sample +} + +// MakeSampleUsingChunks returns Sample constructed using supplied chunks. +func MakeSampleUsingChunks(chunks []swarm.Chunk, anchor []byte) (Sample, error) { + prefixHasherFactory := func() hash.Hash { + return swarm.NewPrefixHasher(anchor) + } + items := make([]SampleItem, len(chunks)) + for i, ch := range chunks { + tr, err := transformedAddress(bmt.NewHasher(prefixHasherFactory), ch, getChunkType(ch)) + if err != nil { + return Sample{}, err + } + + items[i] = SampleItem{ + TransformedAddress: tr, + ChunkAddress: ch.Address(), + ChunkData: ch.Data(), + Stamp: newStamp(ch.Stamp()), + } + } + + sort.Slice(items, func(i, j int) bool { + return items[i].TransformedAddress.Compare(items[j].TransformedAddress) == -1 + }) + + return Sample{Items: items}, nil +} + +func newStamp(s swarm.Stamp) *postage.Stamp { + return postage.NewStamp(s.BatchID(), s.Index(), s.Timestamp(), s.Sig()) +} + +func getChunkType(chunk swarm.Chunk) swarm.ChunkType { + if cac.Valid(chunk) { + return swarm.ChunkTypeContentAddressed + } else if soc.Valid(chunk) { + return swarm.ChunkTypeSingleOwner + } + return swarm.ChunkTypeUnspecified +}