Skip to content
Closed
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
2 changes: 1 addition & 1 deletion x/merkledb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) (uint16, error) {
return 0, err
}
n.children[byte(index)] = child{
compressedPath: compressedPath.deserialize(),
compressedPath: compressedPath.Deserialize(),
id: childID,
}
}
Expand Down
6 changes: 3 additions & 3 deletions x/merkledb/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newRandomProofNode(r *rand.Rand) ProofNode {
}

return ProofNode{
KeyPath: newPath(key).Serialize(),
KeyPath: NewPath(key).Serialize(),
ValueOrHash: Some(val),
Children: children,
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func FuzzCodecSerializedPath(f *testing.F) {
require.Len(bufBytes, numRead)
require.Equal(b[:numRead], bufBytes)

clonedGot := got.deserialize().Serialize()
clonedGot := got.Deserialize().Serialize()
require.Equal(got, clonedGot)
},
)
Expand Down Expand Up @@ -527,7 +527,7 @@ func FuzzCodecDBNodeDeterministic(f *testing.F) {
_, _ = r.Read(childPathBytes) // #nosec G404

children[byte(i)] = child{
compressedPath: newPath(childPathBytes),
compressedPath: NewPath(childPathBytes),
id: childID,
}
}
Expand Down
38 changes: 19 additions & 19 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type Database struct {
metadataDB database.Database

// If a value is nil, the corresponding key isn't in the trie.
nodeCache onEvictCache[path, *node]
nodeCache onEvictCache[Path, *node]
onEvictionErr utils.Atomic[error]

// Stores change lists. Used to serve change proofs and construct
Expand Down Expand Up @@ -122,7 +122,7 @@ func newDatabase(

// Note: trieDB.OnEviction is responsible for writing intermediary nodes to
// disk as they are evicted from the cache.
trieDB.nodeCache = newOnEvictCache[path](config.NodeCacheSize, trieDB.onEviction)
trieDB.nodeCache = newOnEvictCache[Path](config.NodeCacheSize, trieDB.onEviction)

root, err := trieDB.initializeRootIfNeeded()
if err != nil {
Expand All @@ -132,8 +132,8 @@ func newDatabase(
// add current root to history (has no changes)
trieDB.history.record(&changeSummary{
rootID: root,
values: map[path]*change[Maybe[[]byte]]{},
nodes: map[path]*change[*node]{},
values: map[Path]*change[Maybe[[]byte]]{},
nodes: map[Path]*change[*node]{},
})

shutdownType, err := trieDB.metadataDB.Get(cleanShutdownKey)
Expand Down Expand Up @@ -190,7 +190,7 @@ func (db *Database) rebuild(ctx context.Context) error {
}

key := it.Key()
path := path(key)
path := Path(key)
value := it.Value()
n, err := parseNode(path, value)
if err != nil {
Expand Down Expand Up @@ -327,7 +327,7 @@ func (db *Database) GetValues(ctx context.Context, keys [][]byte) ([][]byte, []e
values := make([][]byte, len(keys))
errors := make([]error, len(keys))
for i, key := range keys {
values[i], errors[i] = db.getValueCopy(newPath(key), false /*lock*/)
values[i], errors[i] = db.getValueCopy(NewPath(key), false /*lock*/)
}
return values, errors
}
Expand All @@ -338,12 +338,12 @@ func (db *Database) GetValue(ctx context.Context, key []byte) ([]byte, error) {
_, span := db.tracer.Start(ctx, "MerkleDB.GetValue")
defer span.End()

return db.getValueCopy(newPath(key), true /*lock*/)
return db.getValueCopy(NewPath(key), true /*lock*/)
}

// getValueCopy returns a copy of the value for the given [key].
// Returns database.ErrNotFound if it doesn't exist.
func (db *Database) getValueCopy(key path, lock bool) ([]byte, error) {
func (db *Database) getValueCopy(key Path, lock bool) ([]byte, error) {
val, err := db.getValue(key, lock)
if err != nil {
return nil, err
Expand All @@ -355,7 +355,7 @@ func (db *Database) getValueCopy(key path, lock bool) ([]byte, error) {
// Returns database.ErrNotFound if it doesn't exist.
// If [lock], [db.lock]'s read lock is acquired.
// Otherwise assumes [db.lock] is already held.
func (db *Database) getValue(key path, lock bool) ([]byte, error) {
func (db *Database) getValue(key Path, lock bool) ([]byte, error) {
if lock {
db.lock.RLock()
defer db.lock.RUnlock()
Expand Down Expand Up @@ -511,7 +511,7 @@ func (db *Database) GetChangeProof(
// values modified between [startRootID] to [endRootID] sorted in increasing
// order.
changedKeys := maps.Keys(changes.values)
slices.SortFunc(changedKeys, func(i, j path) bool {
slices.SortFunc(changedKeys, func(i, j Path) bool {
return i.Compare(j) < 0
})

Expand Down Expand Up @@ -620,7 +620,7 @@ func (db *Database) Has(k []byte) (bool, error) {
return false, database.ErrClosed
}

_, err := db.getValue(newPath(k), false /*lock*/)
_, err := db.getValue(NewPath(k), false /*lock*/)
if err == database.ErrNotFound {
return false, nil
}
Expand Down Expand Up @@ -671,21 +671,21 @@ func (db *Database) NewIterator() database.Iterator {

func (db *Database) NewIteratorWithStart(start []byte) database.Iterator {
return &iterator{
nodeIter: db.nodeDB.NewIteratorWithStart(newPath(start).Bytes()),
nodeIter: db.nodeDB.NewIteratorWithStart(NewPath(start).Bytes()),
db: db,
}
}

func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator {
return &iterator{
nodeIter: db.nodeDB.NewIteratorWithPrefix(newPath(prefix).Bytes()),
nodeIter: db.nodeDB.NewIteratorWithPrefix(NewPath(prefix).Bytes()),
db: db,
}
}

func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
startBytes := newPath(start).Bytes()
prefixBytes := newPath(prefix).Bytes()
startBytes := NewPath(start).Bytes()
prefixBytes := NewPath(prefix).Bytes()
return &iterator{
nodeIter: db.nodeDB.NewIteratorWithStartAndPrefix(startBytes, prefixBytes),
db: db,
Expand Down Expand Up @@ -992,7 +992,7 @@ func (db *Database) getKeysNotInSet(start, end []byte, keySet set.Set[string]) (
// This copy may be edited by the caller without affecting the database state.
// Returns database.ErrNotFound if the node doesn't exist.
// Assumes [db.lock] isn't held.
func (db *Database) getEditableNode(key path) (*node, error) {
func (db *Database) getEditableNode(key Path) (*node, error) {
db.lock.RLock()
defer db.lock.RUnlock()

Expand All @@ -1007,7 +1007,7 @@ func (db *Database) getEditableNode(key path) (*node, error) {
// Editing the returned node affects the database state.
// Returns database.ErrNotFound if the node doesn't exist.
// Assumes [db.lock] is read locked.
func (db *Database) getNode(key path) (*node, error) {
func (db *Database) getNode(key Path) (*node, error) {
if db.closed {
return nil, database.ErrClosed
}
Expand Down Expand Up @@ -1175,14 +1175,14 @@ func (db *Database) prepareRangeProofView(start []byte, proof *RangeProof) (*tri
}

// Non-nil error is fatal -- [db] will close.
func (db *Database) putNodeInCache(key path, n *node) error {
func (db *Database) putNodeInCache(key Path, n *node) error {
// TODO Cache metrics
// Note that this may cause a node to be evicted from the cache,
// which will call [OnEviction].
return db.nodeCache.Put(key, n)
}

func (db *Database) getNodeInCache(key path) (*node, bool) {
func (db *Database) getNodeInCache(key Path) (*node, bool) {
// TODO Cache metrics
if node, ok := db.nodeCache.Get(key); ok {
return node, true
Expand Down
22 changes: 11 additions & 11 deletions x/merkledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_MerkleDB_Get_Safety(t *testing.T) {

val, err := db.Get([]byte{0})
require.NoError(t, err)
n, err := db.getNode(newPath([]byte{0}))
n, err := db.getNode(NewPath([]byte{0}))
require.NoError(t, err)
val[0] = 1

Expand Down Expand Up @@ -762,10 +762,10 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest) {
startRoot, err := db.GetMerkleRoot(context.Background())
require.NoError(err)

values := make(map[path][]byte) // tracks content of the trie
values := make(map[Path][]byte) // tracks content of the trie
currentBatch := db.NewBatch()
currentValues := make(map[path][]byte)
deleteValues := make(map[path]struct{})
currentValues := make(map[Path][]byte)
deleteValues := make(map[Path]struct{})
pastRoots := []ids.ID{}

for i, step := range rt {
Expand All @@ -774,13 +774,13 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest) {
case opUpdate:
err := currentBatch.Put(step.key, step.value)
require.NoError(err)
currentValues[newPath(step.key)] = step.value
delete(deleteValues, newPath(step.key))
currentValues[NewPath(step.key)] = step.value
delete(deleteValues, NewPath(step.key))
case opDelete:
err := currentBatch.Delete(step.key)
require.NoError(err)
deleteValues[newPath(step.key)] = struct{}{}
delete(currentValues, newPath(step.key))
deleteValues[NewPath(step.key)] = struct{}{}
delete(currentValues, NewPath(step.key))
case opGenerateRangeProof:
root, err := db.GetMerkleRoot(context.Background())
require.NoError(err)
Expand Down Expand Up @@ -843,15 +843,15 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest) {
pastRoots = pastRoots[len(pastRoots)-300:]
}
}
currentValues = map[path][]byte{}
deleteValues = map[path]struct{}{}
currentValues = map[Path][]byte{}
deleteValues = map[Path]struct{}{}
currentBatch = db.NewBatch()
case opGet:
v, err := db.Get(step.key)
if err != nil {
require.ErrorIs(err, database.ErrNotFound)
}
want := values[newPath(step.key)]
want := values[NewPath(step.key)]
require.True(bytes.Equal(want, v)) // Use bytes.Equal so nil treated equal to []byte{}
trieValue, err := getNodeValue(db, string(step.key))
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions x/merkledb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ type changeSummaryAndIndex struct {
type changeSummary struct {
rootID ids.ID
// key is path prefix
nodes map[path]*change[*node]
nodes map[Path]*change[*node]
// key is full path
values map[path]*change[Maybe[[]byte]]
values map[Path]*change[Maybe[[]byte]]
}

func newChangeSummary(estimatedSize int) *changeSummary {
return &changeSummary{
nodes: make(map[path]*change[*node], estimatedSize),
values: make(map[path]*change[Maybe[[]byte]], estimatedSize),
nodes: make(map[Path]*change[*node], estimatedSize),
values: make(map[Path]*change[Maybe[[]byte]], estimatedSize),
}
}

Expand Down Expand Up @@ -128,13 +128,13 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
// Keep changes sorted so the largest can be removed in order to stay within the maxLength limit.
sortedKeys := btree.NewG(
2,
func(a, b path) bool {
func(a, b Path) bool {
return a.Compare(b) < 0
},
)

startPath := newPath(start)
endPath := newPath(end)
startPath := NewPath(start)
endPath := NewPath(end)

// For each element in the history in the range between [startRoot]'s
// last appearance (exclusive) and [endRoot]'s last appearance (inclusive),
Expand Down Expand Up @@ -206,8 +206,8 @@ func (th *trieHistory) getChangesToGetToRoot(rootID ids.ID, start, end []byte) (
}

var (
startPath = newPath(start)
endPath = newPath(end)
startPath = NewPath(start)
endPath = NewPath(end)
combinedChanges = newChangeSummary(defaultPreallocationSize)
)

Expand Down
30 changes: 15 additions & 15 deletions x/merkledb/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ func Test_History_Values_Lookup_Over_Queue_Break(t *testing.T) {
// changes should still be collectable even though the history has had to loop due to hitting max size
changes, err := db.history.getValueChanges(startRoot, endRoot, nil, nil, 10)
require.NoError(err)
require.Contains(changes.values, newPath([]byte("key1")))
require.Equal([]byte("value1"), changes.values[newPath([]byte("key1"))].after.value)
require.Contains(changes.values, newPath([]byte("key2")))
require.Equal([]byte("value3"), changes.values[newPath([]byte("key2"))].after.value)
require.Contains(changes.values, NewPath([]byte("key1")))
require.Equal([]byte("value1"), changes.values[NewPath([]byte("key1"))].after.value)
require.Contains(changes.values, NewPath([]byte("key2")))
require.Equal([]byte("value3"), changes.values[NewPath([]byte("key2"))].after.value)
}

func Test_History_RepeatedRoot(t *testing.T) {
Expand Down Expand Up @@ -770,14 +770,14 @@ func TestHistoryGetChangesToRoot(t *testing.T) {
for i := 0; i < maxHistoryLen; i++ { // Fill the history
changes = append(changes, &changeSummary{
rootID: ids.GenerateTestID(),
nodes: map[path]*change[*node]{
newPath([]byte{byte(i)}): {
nodes: map[Path]*change[*node]{
NewPath([]byte{byte(i)}): {
before: &node{id: ids.GenerateTestID()},
after: &node{id: ids.GenerateTestID()},
},
},
values: map[path]*change[Maybe[[]byte]]{
newPath([]byte{byte(i)}): {
values: map[Path]*change[Maybe[[]byte]]{
NewPath([]byte{byte(i)}): {
before: Some([]byte{byte(i)}),
after: Some([]byte{byte(i + 1)}),
},
Expand Down Expand Up @@ -816,7 +816,7 @@ func TestHistoryGetChangesToRoot(t *testing.T) {
require.Len(got.nodes, 1)
require.Len(got.values, 1)
reversedChanges := changes[maxHistoryLen-1]
removedKey := newPath([]byte{byte(maxHistoryLen - 1)})
removedKey := NewPath([]byte{byte(maxHistoryLen - 1)})
require.Equal(reversedChanges.nodes[removedKey].before, got.nodes[removedKey].after)
require.Equal(reversedChanges.values[removedKey].before, got.values[removedKey].after)
require.Equal(reversedChanges.values[removedKey].after, got.values[removedKey].before)
Expand All @@ -829,12 +829,12 @@ func TestHistoryGetChangesToRoot(t *testing.T) {
require.Len(got.nodes, 2)
require.Len(got.values, 2)
reversedChanges1 := changes[maxHistoryLen-1]
removedKey1 := newPath([]byte{byte(maxHistoryLen - 1)})
removedKey1 := NewPath([]byte{byte(maxHistoryLen - 1)})
require.Equal(reversedChanges1.nodes[removedKey1].before, got.nodes[removedKey1].after)
require.Equal(reversedChanges1.values[removedKey1].before, got.values[removedKey1].after)
require.Equal(reversedChanges1.values[removedKey1].after, got.values[removedKey1].before)
reversedChanges2 := changes[maxHistoryLen-2]
removedKey2 := newPath([]byte{byte(maxHistoryLen - 2)})
removedKey2 := NewPath([]byte{byte(maxHistoryLen - 2)})
require.Equal(reversedChanges2.nodes[removedKey2].before, got.nodes[removedKey2].after)
require.Equal(reversedChanges2.values[removedKey2].before, got.values[removedKey2].after)
require.Equal(reversedChanges2.values[removedKey2].after, got.values[removedKey2].before)
Expand All @@ -848,12 +848,12 @@ func TestHistoryGetChangesToRoot(t *testing.T) {
require.Len(got.nodes, 2)
require.Len(got.values, 1)
reversedChanges1 := changes[maxHistoryLen-1]
removedKey1 := newPath([]byte{byte(maxHistoryLen - 1)})
removedKey1 := NewPath([]byte{byte(maxHistoryLen - 1)})
require.Equal(reversedChanges1.nodes[removedKey1].before, got.nodes[removedKey1].after)
require.Equal(reversedChanges1.values[removedKey1].before, got.values[removedKey1].after)
require.Equal(reversedChanges1.values[removedKey1].after, got.values[removedKey1].before)
reversedChanges2 := changes[maxHistoryLen-2]
removedKey2 := newPath([]byte{byte(maxHistoryLen - 2)})
removedKey2 := NewPath([]byte{byte(maxHistoryLen - 2)})
require.Equal(reversedChanges2.nodes[removedKey2].before, got.nodes[removedKey2].after)
},
},
Expand All @@ -865,10 +865,10 @@ func TestHistoryGetChangesToRoot(t *testing.T) {
require.Len(got.nodes, 2)
require.Len(got.values, 1)
reversedChanges1 := changes[maxHistoryLen-1]
removedKey1 := newPath([]byte{byte(maxHistoryLen - 1)})
removedKey1 := NewPath([]byte{byte(maxHistoryLen - 1)})
require.Equal(reversedChanges1.nodes[removedKey1].before, got.nodes[removedKey1].after)
reversedChanges2 := changes[maxHistoryLen-2]
removedKey2 := newPath([]byte{byte(maxHistoryLen - 2)})
removedKey2 := NewPath([]byte{byte(maxHistoryLen - 2)})
require.Equal(reversedChanges2.nodes[removedKey2].before, got.nodes[removedKey2].after)
require.Equal(reversedChanges2.values[removedKey2].before, got.values[removedKey2].after)
require.Equal(reversedChanges2.values[removedKey2].after, got.values[removedKey2].before)
Expand Down
2 changes: 1 addition & 1 deletion x/merkledb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (i *iterator) Next() bool {
}
for i.nodeIter.Next() {
i.db.metrics.IOKeyRead()
n, err := parseNode(path(i.nodeIter.Key()), i.nodeIter.Value())
n, err := parseNode(Path(i.nodeIter.Key()), i.nodeIter.Value())
if err != nil {
i.err = err
return false
Expand Down
4 changes: 3 additions & 1 deletion x/merkledb/maybe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package merkledb

import "golang.org/x/exp/slices"
import (
"golang.org/x/exp/slices"
)

// Maybe T = Some T | Nothing.
// A data wrapper that allows values to be something [Some T] or nothing [Nothing].
Expand Down
Loading