mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/storage: GetAllReferences test and fixes
This commit is contained in:
parent
9104193585
commit
eb2d20ff17
2 changed files with 39 additions and 3 deletions
|
|
@ -99,20 +99,20 @@ func (f *FileStore) HashSize() int {
|
||||||
|
|
||||||
// Public API. This endpoint returns all chunk hashes (only) for a given file
|
// Public API. This endpoint returns all chunk hashes (only) for a given file
|
||||||
func (f *FileStore) GetAllReferences(ctx context.Context, data io.Reader) (addrs []Address, err error) {
|
func (f *FileStore) GetAllReferences(ctx context.Context, data io.Reader) (addrs []Address, err error) {
|
||||||
var addrs = make([]Address, 0)
|
addrs = make([]Address, 0)
|
||||||
// create a special kind of putter, which only will store the references
|
// create a special kind of putter, which only will store the references
|
||||||
putter := &HashExplorer{
|
putter := &HashExplorer{
|
||||||
hasherStore: NewHasherStore(f.ChunkStore, f.hashFunc, false),
|
hasherStore: NewHasherStore(f.ChunkStore, f.hashFunc, false),
|
||||||
References: make([]Reference, 0),
|
References: make([]Reference, 0),
|
||||||
}
|
}
|
||||||
// do the actual splitting anyway, no way around it
|
// do the actual splitting anyway, no way around it
|
||||||
_, _, err := PyramidSplit(ctx, data, putter, putter)
|
_, _, err = PyramidSplit(ctx, data, putter, putter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// collect all references
|
// collect all references
|
||||||
for _, ref := range putter.References {
|
for _, ref := range putter.References {
|
||||||
addrs = append(addrs, ref)
|
addrs = append(addrs, Address(ref))
|
||||||
}
|
}
|
||||||
return addrs, nil
|
return addrs, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -173,3 +173,39 @@ func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
|
||||||
t.Fatalf("Comparison error after clearing memStore.")
|
t.Fatalf("Comparison error after clearing memStore.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestGetAllReferences only tests that GetAllReferences returns an expected
|
||||||
|
// number of references for a given file
|
||||||
|
func TestGetAllReferences(t *testing.T) {
|
||||||
|
tdb, cleanup, err := newTestDbStore(false, false)
|
||||||
|
defer cleanup()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("init dbStore failed: %v", err)
|
||||||
|
}
|
||||||
|
db := tdb.LDBStore
|
||||||
|
memStore := NewMemStore(NewDefaultStoreParams(), db)
|
||||||
|
localStore := &LocalStore{
|
||||||
|
memStore: memStore,
|
||||||
|
DbStore: db,
|
||||||
|
}
|
||||||
|
fileStore := NewFileStore(localStore, NewFileStoreParams())
|
||||||
|
|
||||||
|
checkRefs := func(dataSize int, expectedLen int) {
|
||||||
|
slice := testutil.RandomBytes(1, dataSize)
|
||||||
|
|
||||||
|
addrs, err := fileStore.GetAllReferences(context.Background(), bytes.NewReader(slice))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(addrs) != expectedLen {
|
||||||
|
t.Fatalf("Expected reference array length to be %d, but is %d", expectedLen, len(addrs))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// testRuns[i] and expectedLen[i] are dataSize and expected length respectively
|
||||||
|
testRuns := []int{1024, 8192, 16000, 30000, 1000000}
|
||||||
|
expectedLens := []int{1, 3, 5, 9, 248}
|
||||||
|
for i, r := range testRuns {
|
||||||
|
checkRefs(r, expectedLens[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue