Skip to content

optimize historical range #3658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/exp/maps"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -347,8 +346,9 @@ func newDatabase(
rootChange: change[maybe.Maybe[*node]]{
after: trieDB.root,
},
values: map[Key]*change[maybe.Maybe[[]byte]]{},
nodes: map[Key]*change[*node]{},
sortedKeys: []Key{},
nodes: map[Key]*change[*node]{},
keyChanges: map[Key]*change[maybe.Maybe[[]byte]]{},
})

// mark that the db has not yet been cleanly closed
Expand Down Expand Up @@ -747,29 +747,23 @@ func (db *merkleDB) GetChangeProof(
return nil, database.ErrClosed
}

changes, err := db.history.getValueChanges(startRootID, endRootID, start, end, maxLength)
// [valueChanges] contains a subset of the keys that were added or had their
// values modified between [startRootID] to [endRootID].
valueChanges, err := db.history.getValueChanges(startRootID, endRootID, start, end, maxLength)
if err != nil {
return nil, err
}

// [changedKeys] are a subset of the keys that were added or had their
// values modified between [startRootID] to [endRootID] sorted in increasing
// order.
changedKeys := maps.Keys(changes.values)
utils.Sort(changedKeys)

result := &ChangeProof{
KeyChanges: make([]KeyChange, 0, len(changedKeys)),
KeyChanges: make([]KeyChange, len(valueChanges)),
}

for _, key := range changedKeys {
change := changes.values[key]

result.KeyChanges = append(result.KeyChanges, KeyChange{
Key: key.Bytes(),
for i, valueChange := range valueChanges {
result.KeyChanges[i] = KeyChange{
Key: valueChange.key.Bytes(),
// create a copy so edits of the []byte don't affect the db
Value: maybe.Bind(change.after, slices.Clone[[]byte]),
})
Value: maybe.Bind(valueChange.change.after, slices.Clone[[]byte]),
}
}

largestKey := end
Expand Down Expand Up @@ -985,7 +979,7 @@ func (db *merkleDB) commitView(ctx context.Context, trieToCommit *view) error {
changes := trieToCommit.changes
_, span := db.infoTracer.Start(ctx, "MerkleDB.commitView", oteltrace.WithAttributes(
attribute.Int("nodesChanged", len(changes.nodes)),
attribute.Int("valuesChanged", len(changes.values)),
attribute.Int("valuesChanged", len(changes.keyChanges)),
))
defer span.End()

Expand Down Expand Up @@ -1365,9 +1359,10 @@ func (db *merkleDB) Clear() error {
// Clear history
db.history = newTrieHistory(db.history.maxHistoryLen)
db.history.record(&changeSummary{
rootID: db.rootID,
values: map[Key]*change[maybe.Maybe[[]byte]]{},
nodes: map[Key]*change[*node]{},
rootID: db.rootID,
sortedKeys: []Key{},
nodes: map[Key]*change[*node]{},
keyChanges: map[Key]*change[maybe.Maybe[[]byte]]{},
})
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions x/merkledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,11 @@ func TestMerkleDBClear(t *testing.T) {
require.Zero(db.intermediateNodeDB.writeBuffer.currentSize)

// Assert history has only the clearing change.
require.Len(db.history.lastChanges, 1)
change, ok := db.history.lastChanges[emptyRootID]
require.Len(db.history.lastChangesInsertNumber, 1)
change, ok := db.history.getRootChanges(emptyRootID)
require.True(ok)
require.Empty(change.nodes)
require.Empty(change.values)
require.Empty(change.keyChanges)
}

func FuzzMerkleDBEmptyRandomizedActions(f *testing.F) {
Expand Down
Loading