-
Notifications
You must be signed in to change notification settings - Fork 762
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 all 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,7 +49,8 @@ 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 ChangeProofer interface { | ||
|
@@ -255,7 +255,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, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -282,7 +282,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, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -308,7 +308,7 @@ func (db *merkleDB) CommitChangeProof(ctx context.Context, proof *ChangeProof) e | |
} | ||
} | ||
|
||
view, err := db.newUntrackedView(ops) | ||
view, err := db.newUntrackedView(ops, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -349,7 +349,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, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -504,7 +504,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, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -655,15 +655,26 @@ func (db *merkleDB) GetChangeProof( | |
return result, nil | ||
} | ||
|
||
// NewView 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. | ||
// NewView returns a new view on top of this Trie where the passed changes | ||
// have been applied. | ||
// | ||
// 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(_ context.Context, batchOps []database.BatchOp) (TrieView, error) { | ||
func (db *merkleDB) NewView( | ||
_ context.Context, | ||
changes ViewChanges, | ||
) (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 := newTrieView(db, db, db.root.clone(), changes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -679,16 +690,16 @@ func (db *merkleDB) NewView(_ context.Context, batchOps []database.BatchOp) (Tri | |
// 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) newUntrackedView(batchOps []database.BatchOp, consumeBytes bool) (*trieView, error) { | ||
return newTrieView( | ||
db, | ||
db, | ||
db.root.clone(), | ||
ViewChanges{ | ||
BatchOps: batchOps, | ||
ConsumeBytes: consumeBytes, | ||
}, | ||
) | ||
} | ||
|
||
func (db *merkleDB) Has(k []byte) (bool, error) { | ||
|
@@ -820,12 +831,13 @@ func (db *merkleDB) PutContext(ctx context.Context, k, v []byte) error { | |
return database.ErrClosed | ||
} | ||
|
||
view, err := db.newUntrackedView([]database.BatchOp{ | ||
{ | ||
view, err := db.newUntrackedView( | ||
[]database.BatchOp{{ | ||
Key: k, | ||
Value: v, | ||
}, | ||
}) | ||
}}, | ||
false, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -844,18 +856,21 @@ func (db *merkleDB) DeleteContext(ctx context.Context, key []byte) error { | |
return database.ErrClosed | ||
} | ||
|
||
view, err := db.newUntrackedView([]database.BatchOp{ | ||
{ | ||
view, err := db.newUntrackedView( | ||
[]database.BatchOp{{ | ||
Key: key, | ||
Delete: true, | ||
}, | ||
}) | ||
}}, | ||
true, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return view.commitToDB(ctx) | ||
} | ||
|
||
// Assumes values inside of [ops] are safe to reference after the function | ||
// returns. | ||
func (db *merkleDB) commitBatch(ops []database.BatchOp) error { | ||
db.commitLock.Lock() | ||
defer db.commitLock.Unlock() | ||
|
@@ -864,7 +879,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 | ||
} | ||
|
@@ -1042,6 +1057,10 @@ func (db *merkleDB) VerifyChangeProof( | |
db.commitLock.RLock() | ||
defer db.commitLock.RUnlock() | ||
|
||
if db.closed { | ||
return database.ErrClosed | ||
} | ||
|
||
if err := verifyAllChangeProofKeyValuesPresent( | ||
ctx, | ||
db, | ||
|
@@ -1075,7 +1094,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 +1203,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 newTrieView(db, db, db.root.clone(), ViewChanges{}) | ||
} | ||
|
||
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.