diff --git a/claimtrie/node/manager.go b/claimtrie/node/manager.go index e3f2dae8..391b1270 100644 --- a/claimtrie/node/manager.go +++ b/claimtrie/node/manager.go @@ -226,7 +226,7 @@ func (nm *BaseManager) IncrementHeightTo(height int32) ([][]byte, error) { names = append(names, nm.changes[i].Name) } - if err := nm.repo.AppendChanges(nm.changes); err != nil { + if err := nm.repo.AppendChanges(nm.changes); err != nil { // destroys names return nil, fmt.Errorf("save changes to node repo: %w", err) } diff --git a/claimtrie/node/noderepo/noderepo_test.go b/claimtrie/node/noderepo/noderepo_test.go index 1cf2e64e..385fa44e 100644 --- a/claimtrie/node/noderepo/noderepo_test.go +++ b/claimtrie/node/noderepo/noderepo_test.go @@ -179,10 +179,10 @@ func TestIterator(t *testing.T) { err = repo.AppendChanges(creation) r.NoError(err) - var received []change.Change + i := 0 repo.IterateChildren([]byte{}, func(changes []change.Change) bool { - received = append(received, changes...) + r.Equal(creation[i], changes[0]) + i++ return true }) - r.Equal(creation, received) } diff --git a/claimtrie/node/noderepo/pebble.go b/claimtrie/node/noderepo/pebble.go index 5d7b5440..8af5ef99 100644 --- a/claimtrie/node/noderepo/pebble.go +++ b/claimtrie/node/noderepo/pebble.go @@ -96,12 +96,14 @@ func (repo *Pebble) AppendChanges(changes []change.Change) error { // TODO: switch to buffer pool and reuse encoder for _, chg := range changes { + name := chg.Name + chg.Name = nil // don't waste the storage space on this (annotation a better approach?) value, err := msgpack.Marshal(chg) if err != nil { return fmt.Errorf("msgpack marshal value: %w", err) } - err = batch.Merge(chg.Name, value, pebble.NoSync) + err = batch.Merge(name, value, pebble.NoSync) if err != nil { return fmt.Errorf("pebble set: %w", err) } @@ -124,10 +126,10 @@ func (repo *Pebble) LoadChanges(name []byte) ([]change.Change, error) { defer closer.Close() } - return unmarshalChanges(data) + return unmarshalChanges(name, data) } -func unmarshalChanges(data []byte) ([]change.Change, error) { +func unmarshalChanges(name, data []byte) ([]change.Change, error) { var changes []change.Change dec := msgpack.GetDecoder() defer msgpack.PutDecoder(dec) @@ -140,6 +142,7 @@ func unmarshalChanges(data []byte) ([]change.Change, error) { if err != nil { return nil, fmt.Errorf("msgpack unmarshal: %w", err) } + chg.Name = name changes = append(changes, chg) } @@ -189,7 +192,8 @@ func (repo *Pebble) IterateChildren(name []byte, f func(changes []change.Change) defer iter.Close() for iter.First(); iter.Valid(); iter.Next() { - changes, err := unmarshalChanges(iter.Value()) + // NOTE! iter.Key() is ephemeral! + changes, err := unmarshalChanges(iter.Key(), iter.Value()) if err != nil { panic(err) }