Skip to content

Commit 7bf4955

Browse files
author
Dan Laine
authored
merkledb -- nits (#1916)
1 parent 9851214 commit 7bf4955

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

x/merkledb/batch.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import "github.com/ava-labs/avalanchego/database"
77

88
var _ database.Batch = (*batch)(nil)
99

10-
// batch is a write-only database that commits changes to its host database
11-
// when Write is called.
1210
type batch struct {
1311
database.BatchOps
1412

1513
db *merkleDB
1614
}
1715

18-
// apply all operations in order to the database and write the result to disk
1916
func (b *batch) Write() error {
2017
return b.db.commitBatch(b.Ops)
2118
}

x/merkledb/cache.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212

1313
// A cache that calls [onEviction] on the evicted element.
1414
type onEvictCache[K comparable, V any] struct {
15-
lock sync.RWMutex
16-
maxSize int
17-
fifo linkedhashmap.LinkedHashmap[K, V]
15+
lock sync.RWMutex
16+
maxSize int
17+
fifo linkedhashmap.LinkedHashmap[K, V]
18+
// Must not call any method that grabs [c.lock]
19+
// because this would cause a deadlock.
1820
onEviction func(V) error
1921
}
2022

@@ -27,6 +29,7 @@ func newOnEvictCache[K comparable, V any](maxSize int, onEviction func(V) error)
2729
}
2830

2931
// removeOldest returns and removes the oldest element from this cache.
32+
// Assumes [c.lock] is held.
3033
func (c *onEvictCache[K, V]) removeOldest() (K, V, bool) {
3134
k, v, exists := c.fifo.Oldest()
3235
if exists {

x/merkledb/db.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ type ChangeProofer interface {
7676
// If [end] is nothing, all keys are considered < [end].
7777
// - [proof.KeyValues] and [proof.DeletedKeys] are sorted in order of increasing key.
7878
// - [proof.StartProof] and [proof.EndProof] are well-formed.
79-
// - When the keys in [proof.KeyValues] are added to [db] and the keys in [proof.DeletedKeys]
80-
// are removed from [db], the root ID of [db] is [expectedEndRootID].
81-
//
82-
// This is defined on Database instead of ChangeProof because it accesses
83-
// database internals.
79+
// - When the changes in [proof.KeyChanes] are applied,
80+
// the root ID of the database is [expectedEndRootID].
8481
VerifyChangeProof(
8582
ctx context.Context,
8683
proof *ChangeProof,
@@ -146,6 +143,7 @@ type merkleDB struct {
146143
// Should be held before taking [db.lock]
147144
commitLock sync.RWMutex
148145

146+
// Stores this trie's nodes.
149147
nodeDB database.Database
150148

151149
// Stores data about the database's current state.
@@ -155,7 +153,8 @@ type merkleDB struct {
155153
// Note that a call to Put may cause a node to be evicted
156154
// from the cache, which will call [OnEviction].
157155
// A non-nil error returned from Put is considered fatal.
158-
nodeCache onEvictCache[path, *node]
156+
nodeCache onEvictCache[path, *node]
157+
// Stores any error returned by [onEviction].
159158
onEvictionErr utils.Atomic[error]
160159
evictionBatchSize int
161160

@@ -979,6 +978,8 @@ func (*merkleDB) CommitToDB(context.Context) error {
979978
return nil
980979
}
981980

981+
// This is defined on merkleDB instead of ChangeProof
982+
// because it accesses database internals.
982983
// Assumes [db.lock] isn't held.
983984
func (db *merkleDB) VerifyChangeProof(
984985
ctx context.Context,

0 commit comments

Comments
 (0)