Skip to content

Commit fab6283

Browse files
authored
Test cid stability (filecoin-project#26)
* Test cid stability Add 500 items, then remove them in reverse order asserting that the CIDs are the same. * rebase fixup
1 parent db5f2dd commit fab6283

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

amt_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,3 +1187,45 @@ func assertEquals(ctx context.Context, t testing.TB, a *Root, expected []cbg.CBO
11871187
}))
11881188
assertCount(t, a, uint64(len(expected)))
11891189
}
1190+
1191+
func TestCidStability(t *testing.T) {
1192+
const count = 500
1193+
1194+
bs := cbor.NewCborStore(newMockBlocks())
1195+
ctx := context.Background()
1196+
a, err := NewAMT(bs)
1197+
require.NoError(t, err)
1198+
1199+
cids := make([]cid.Cid, count)
1200+
1201+
// Add, flushing each time.
1202+
for i := 0; i < count; i++ {
1203+
c, err := a.Flush(ctx)
1204+
require.NoError(t, err)
1205+
cids[i] = c
1206+
assert.NoError(t, a.Set(ctx, uint64(i), cborstr(fmt.Sprintf("%d", i))))
1207+
}
1208+
1209+
// now go backwards
1210+
for i := count - 1; i >= 0; i-- {
1211+
found, err := a.Delete(ctx, uint64(i))
1212+
assert.NoError(t, err)
1213+
assert.True(t, found)
1214+
c, err := a.Flush(ctx)
1215+
require.NoError(t, err)
1216+
assert.Equal(t, cids[i], c)
1217+
}
1218+
1219+
// now go backwards and start over.
1220+
a2, err := LoadAMT(ctx, bs, cids[len(cids)-1])
1221+
require.NoError(t, err)
1222+
1223+
for i := count - 2; i >= 0; i-- {
1224+
found, err := a2.Delete(ctx, uint64(i))
1225+
assert.NoError(t, err)
1226+
assert.True(t, found)
1227+
c, err := a2.Flush(ctx)
1228+
require.NoError(t, err)
1229+
assert.Equal(t, cids[i], c)
1230+
}
1231+
}

0 commit comments

Comments
 (0)