Skip to content

Commit

Permalink
chore: move
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Oct 30, 2024
1 parent e43c29d commit 259e17e
Showing 1 changed file with 61 additions and 61 deletions.
122 changes: 61 additions & 61 deletions pkg/storer/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 259e17e

Please sign in to comment.