Skip to content

Commit

Permalink
fix: save and load transition state for block processing (#324)
Browse files Browse the repository at this point in the history
* fix: save and load transition state for block processing

* log whether the tree is verkle in LoadTransitionState

* fix: ensure the transition is marked as started in insertChain

* dump saved address

* fix nil pointer panic

* remove stacktrace that is no longer useful

* fix a panic

* fix build

* check: copy current account address BEFORE it's saved

* mandatory panic fix

* Remove debug fmt.Println

* more cleanup + comments
  • Loading branch information
gballet authored Dec 8, 2023
1 parent 2c5d36c commit 1e2d2ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root {
return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error())
}
statedb.Database().SaveTransitionState(header.Root)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,15 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}

if bc.Config().IsPrague(block.Number(), block.Time()) {
bc.stateCache.LoadTransitionState(parent.Root)

// pragueTime has been reached. If the transition isn't active, it means this
// is the fork block and that the conversion needs to be marked at started.
if !bc.stateCache.InTransition() && !bc.stateCache.Transitioned() {
bc.stateCache.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), bc.Config().PragueTime, parent.Root)
}
}
if parent.Number.Uint64() == conversionBlock {
bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time, parent.Root)
bc.stateCache.SetLastMerkleRoot(parent.Root)
Expand Down
14 changes: 8 additions & 6 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {

// TODO separate both cases when I can be certain that it won't
// find a Verkle trie where is expects a Transitoion trie.
if db.CurrentTransitionState != nil && (db.CurrentTransitionState.started || db.CurrentTransitionState.ended) {
if db.InTransition() || db.Transitioned() {
// NOTE this is a kaustinen-only change, it will break replay
vkt, err := db.openVKTrie(root)
if err != nil {
Expand Down Expand Up @@ -542,7 +542,11 @@ func (db *cachingDB) SaveTransitionState(root common.Hash) {
db.TransitionStatePerRoot = make(map[common.Hash]*TransitionState)
}

db.TransitionStatePerRoot[root] = db.CurrentTransitionState
if db.CurrentTransitionState != nil {
// Copy so that the address pointer isn't updated after
// it has been saved.
db.TransitionStatePerRoot[root] = db.CurrentTransitionState.Copy()
}
}

func (db *cachingDB) LoadTransitionState(root common.Hash) {
Expand All @@ -556,9 +560,7 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) {
ts = &TransitionState{ended: db.triedb.IsVerkle()}
}

// Copy so that the CurrentAddress pointer in the map
// doesn't get overwritten.
db.CurrentTransitionState = ts.Copy()

if db.CurrentTransitionState != nil {
fmt.Println("address", db.CurrentTransitionState.CurrentAccountAddress)
}
}

0 comments on commit 1e2d2ad

Please sign in to comment.