-
Notifications
You must be signed in to change notification settings - Fork 741
MerkleDB Adjust New View function(s) #1927
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
Changes from 4 commits
e631961
8ac2767
fd08c33
c9d6ad9
fbafd8a
9bdf8ab
db4f71d
3c3eeb8
82a9825
083e057
8b1e270
dd9684c
20bb50e
e714357
cf10e71
5594d2c
45c9e32
16133c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ const ( | |
) | ||
|
||
var ( | ||
_ TrieView = (*merkleDB)(nil) | ||
_ MerkleDB = (*merkleDB)(nil) | ||
|
||
codec = newCodec() | ||
|
@@ -50,9 +49,15 @@ var ( | |
hadCleanShutdown = []byte{1} | ||
didNotHaveCleanShutdown = []byte{0} | ||
|
||
errSameRoot = errors.New("start and end root are the same") | ||
errSameRoot = errors.New("start and end root are the same") | ||
errNoNewRoot = errors.New("there was no updated root in change list") | ||
) | ||
|
||
type ChangeOp struct { | ||
dboehm-avalabs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Value []byte | ||
Delete bool | ||
} | ||
|
||
type ChangeProofer interface { | ||
// GetChangeProof returns a proof for a subset of the key/value changes in key range | ||
// [start, end] that occurred between [startRootID] and [endRootID]. | ||
|
@@ -255,7 +260,7 @@ func (db *merkleDB) rebuild(ctx context.Context) error { | |
|
||
for it.Next() { | ||
if len(currentOps) >= viewSizeLimit { | ||
view, err := db.newUntrackedView(currentOps) | ||
view, err := db.newUntrackedView(currentOps, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -282,7 +287,7 @@ func (db *merkleDB) rebuild(ctx context.Context) error { | |
if err := it.Error(); err != nil { | ||
return err | ||
} | ||
view, err := db.newUntrackedView(currentOps) | ||
view, err := db.newUntrackedView(currentOps, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -308,7 +313,7 @@ func (db *merkleDB) CommitChangeProof(ctx context.Context, proof *ChangeProof) e | |
} | ||
} | ||
|
||
view, err := db.newUntrackedView(ops) | ||
view, err := db.newUntrackedView(ops, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -349,7 +354,7 @@ func (db *merkleDB) CommitRangeProof(ctx context.Context, start maybe.Maybe[[]by | |
} | ||
|
||
// Don't need to lock [view] because nobody else has a reference to it. | ||
view, err := db.newUntrackedView(ops) | ||
view, err := db.newUntrackedView(ops, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -504,7 +509,7 @@ func (db *merkleDB) getProof(ctx context.Context, key []byte) (*Proof, error) { | |
return nil, database.ErrClosed | ||
} | ||
|
||
view, err := db.newUntrackedView(nil) | ||
view, err := db.newUntrackedView(nil, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -655,15 +660,47 @@ func (db *merkleDB) GetChangeProof( | |
return result, nil | ||
} | ||
|
||
// NewView returns a new view on top of this trie. | ||
// NewViewFromBatchOps returns a new view on top of this trie. | ||
// Changes made to the view will only be reflected in the original trie if Commit is called. | ||
// if copyBytes is true, code will duplicate any passed []byte so that editing in calling code is safe | ||
// Assumes [db.commitLock] and [db.lock] aren't held. | ||
func (db *merkleDB) NewViewFromBatchOps(_ context.Context, batchOps []database.BatchOp, copyBytes bool) (TrieView, error) { | ||
return db.newView(func() (*trieView, error) { | ||
return newTrieViewFromBatchOps(db, db, db.root.clone(), batchOps, copyBytes) | ||
}) | ||
} | ||
|
||
// Returns a new view that isn't tracked in [db.childViews]. | ||
// For internal use only, namely in methods that create short-lived views. | ||
// Assumes [db.lock] isn't held and [db.commitLock] is read locked. | ||
func (db *merkleDB) newUntrackedView(batchOps []database.BatchOp, copyBytes bool) (*trieView, error) { | ||
return newTrieViewFromBatchOps(db, db, db.root.clone(), batchOps, copyBytes) | ||
} | ||
|
||
// NewViewFromMap returns a new view on top of this trie. | ||
// Changes made to the view will only be reflected in the original trie if Commit is called. | ||
// if copyBytes is true, code will duplicate any passed []byte so that editing in calling code is safe | ||
// Assumes [db.commitLock] and [db.lock] aren't held. | ||
func (db *merkleDB) NewView(_ context.Context, batchOps []database.BatchOp) (TrieView, error) { | ||
func (db *merkleDB) NewViewFromMap(_ context.Context, changes map[string]ChangeOp, copyBytes bool) (TrieView, error) { | ||
dboehm-avalabs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return db.newView(func() (*trieView, error) { | ||
return newTrieViewFromMap(db, db, db.root.clone(), changes, copyBytes) | ||
}) | ||
} | ||
|
||
// newView returns a new view on top of this trie that has been added to childViews | ||
// Wrapper function for NewViewFromMap and NewViewFromBatchOps | ||
// Changes made to the view will only be reflected in the original trie if Commit is called. | ||
// Assumes [db.commitLock] and [db.lock] aren't held. | ||
func (db *merkleDB) newView(createView func() (*trieView, error)) (TrieView, error) { | ||
// ensure the db doesn't change while creating the new view | ||
db.commitLock.RLock() | ||
defer db.commitLock.RUnlock() | ||
|
||
newView, err := db.newUntrackedView(batchOps) | ||
if db.closed { | ||
return nil, database.ErrClosed | ||
} | ||
|
||
newView, err := createView() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -676,21 +713,6 @@ func (db *merkleDB) NewView(_ context.Context, batchOps []database.BatchOp) (Tri | |
return newView, nil | ||
} | ||
|
||
// Returns a new view that isn't tracked in [db.childViews]. | ||
// For internal use only, namely in methods that create short-lived views. | ||
// Assumes [db.lock] isn't held and [db.commitLock] is read locked. | ||
func (db *merkleDB) newUntrackedView(batchOps []database.BatchOp) (*trieView, error) { | ||
if db.closed { | ||
return nil, database.ErrClosed | ||
} | ||
Comment on lines
-683
to
-685
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of weird that this was removed - but because this is an internal function it makes sense that this should never really happen anyways There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we always check it before we call this function, so it couldn't really happen as you said. |
||
|
||
newView, err := newTrieView(db, db, db.root.clone(), batchOps) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newView, nil | ||
} | ||
|
||
func (db *merkleDB) Has(k []byte) (bool, error) { | ||
db.lock.RLock() | ||
defer db.lock.RUnlock() | ||
|
@@ -825,7 +847,8 @@ func (db *merkleDB) PutContext(ctx context.Context, k, v []byte) error { | |
Key: k, | ||
Value: v, | ||
}, | ||
}) | ||
}, | ||
true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -849,7 +872,7 @@ func (db *merkleDB) DeleteContext(ctx context.Context, key []byte) error { | |
Key: key, | ||
Delete: true, | ||
}, | ||
}) | ||
}, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -864,7 +887,7 @@ func (db *merkleDB) commitBatch(ops []database.BatchOp) error { | |
return database.ErrClosed | ||
} | ||
|
||
view, err := db.newUntrackedView(ops) | ||
view, err := db.newUntrackedView(ops, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -1075,7 +1098,7 @@ func (db *merkleDB) VerifyChangeProof( | |
} | ||
|
||
// Don't need to lock [view] because nobody else has a reference to it. | ||
view, err := db.newUntrackedView(ops) | ||
view, err := db.newUntrackedView(ops, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -1184,7 +1207,8 @@ func (db *merkleDB) getHistoricalViewForRange( | |
|
||
// looking for the trie's current root id, so return the trie unmodified | ||
if currentRootID == rootID { | ||
return newTrieView(db, db, db.root.clone(), nil) | ||
// create an empty trie | ||
return newTrieViewFromBatchOps(db, db, db.root.clone(), nil, false) | ||
} | ||
|
||
changeHistory, err := db.history.getChangesToGetToRoot(rootID, start, end) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed.