Skip to content
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

fix: save and load transition state for block processing #324

Merged
merged 12 commits into from
Dec 8, 2023
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)
}
}