diff --git a/amt_test.go b/amt_test.go index 0dcc6ca..5861bfe 100644 --- a/amt_test.go +++ b/amt_test.go @@ -73,6 +73,23 @@ func TestBasicSetGet(t *testing.T) { } +func TestRoundTrip(t *testing.T) { + bs := cbor.NewCborStore(newMockBlocks()) + ctx := context.Background() + a := NewAMT(bs) + emptyCid, err := a.Flush(ctx) + require.NoError(t, err) + + k := uint64(100000) + assertSet(t, a, k, "foo") + assertDelete(t, a, k) + + c, err := a.Flush(ctx) + require.NoError(t, err) + + require.Equal(t, emptyCid, c) +} + func TestOutOfRange(t *testing.T) { ctx := context.Background() bs := cbor.NewCborStore(newMockBlocks()) diff --git a/go.sum b/go.sum index adbfe23..e739be5 100644 --- a/go.sum +++ b/go.sum @@ -4,7 +4,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/filecoin-project/go-amt-ipld v1.0.0 h1:jJteqdKcwFYoN9Wgs7MzDWVWvRWUfhhFo4bltJ7wrus= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= diff --git a/node.go b/node.go index a62e1fc..1e0feb8 100644 --- a/node.go +++ b/node.go @@ -83,15 +83,19 @@ func newNode(nd internal.Node, allowEmpty, expectLeaf bool) (*node, error) { } func (nd *node) collapse(ctx context.Context, bs cbor.IpldStore, height int) (int, error) { - if nd.links[0] == nil { - return height, nil - } + // If we have any links going "to the right", we can't collapse any + // more. for _, l := range nd.links[1:] { if l != nil { return height, nil } } + // If we have _no_ links, we've collapsed everything. + if nd.links[0] == nil { + return 0, nil + } + // only one child, collapse it. subn, err := nd.links[0].load(ctx, bs, height-1)