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

Lock the state transition during conversion #384

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/conversion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Overlay conversion

on:
push:
branches: [ master, transition-post-genesis, store-transition-state-in-db ]
pull_request:
branches: [ master, kaustinen-with-shapella, transition-post-genesis, store-transition-state-in-db, lock-overlay-transition ]
workflow_dispatch:

jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21.1

- name: Cleanup from previous runs
run: |
rm -f log.txt
rm -rf .shadowfork
rm -f genesis.json

- name: Download genesis file
run: wget https://gist.githubusercontent.com/gballet/0b02a025428aa0e7b67941864d54716c/raw/bfb4e158bca5217b356a19b2ec55c4a45a7b2bad/genesis.json

- name: Init data
run: go run ./cmd/geth --dev --cache.preimages init genesis.json

- name: Run geth in devmode
run: go run ./cmd/geth --dev --dev.period=5 --cache.preimages --http --datadir=.shadowfork --override.overlay-stride=10 --override.prague=$(($(date +%s) + 45)) > log.txt &

- name: Wait for the transition to start
run: |
start_time=$(date +%s)
while true; do
sleep 5
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))

# 2 minute timeout
if [ $elapsed_time -ge 120 ]; then
kill -9 $(pgrep -f geth)
exit 1
fi

# Check for signs that the conversion has started
if grep -q "Processing verkle conversion starting at" log.txt; then
break
fi
done

- name: Wait for the transition to end
run: |
start_time=$(date +%s)
while true; do
sleep 5
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))

# 10 minute timeout
if [ $elapsed_time -ge 300 ]; then
cat log.txt
kill -9 $(pgrep -f geth)
exit 1
fi

# Check for signs that the conversion has started
if egrep -q "at block.*performing transition\? false" log.txt; then
kill -9 $(pgrep -f geth)
break
fi
done
6 changes: 5 additions & 1 deletion core/overlay/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ func (kvm *keyValueMigrator) migrateCollectedKeyValues(tree *trie.VerkleTrie) er
// OverlayVerkleTransition contains the overlay conversion logic
func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedCount uint64) error {
migrdb := statedb.Database()
migrdb.LockCurrentTransitionState()
defer migrdb.UnLockCurrentTransitionState()

// verkle transition: if the conversion process is in progress, move
// N values from the MPT into the verkle tree.
Expand Down Expand Up @@ -402,7 +404,9 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC
if crypto.Keccak256Hash(addr[:]) != accIt.Hash() {
return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash())
}
fmt.Printf("Converting account address hash=%x addr=%x", accIt.Hash(), addr)
if count%100 == 0 {
fmt.Printf("Converting account address hash=%x addr=%x\n", accIt.Hash(), addr)
}
preimageSeek += int64(len(addr))
migrdb.SetCurrentAccountAddress(addr)
} else {
Expand Down
19 changes: 18 additions & 1 deletion core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"runtime/debug"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
Expand Down Expand Up @@ -105,6 +106,10 @@ type Database interface {
SaveTransitionState(common.Hash)

LoadTransitionState(common.Hash)

LockCurrentTransitionState()

UnLockCurrentTransitionState()
}

// Trie is a Ethereum Merkle Patricia trie.
Expand Down Expand Up @@ -311,11 +316,11 @@ type cachingDB struct {
LastMerkleRoot common.Hash // root hash of the read-only base tree
CurrentTransitionState *TransitionState
TransitionStatePerRoot lru.BasicLRU[common.Hash, *TransitionState]
transitionStateLock sync.Mutex

addrToPoint *utils.PointCache

baseRoot common.Hash // hash of the read-only base tree

}

func (db *cachingDB) openMPTTrie(root common.Hash) (Trie, error) {
Expand Down Expand Up @@ -548,6 +553,8 @@ func (db *cachingDB) SetLastMerkleRoot(merkleRoot common.Hash) {
}

func (db *cachingDB) SaveTransitionState(root common.Hash) {
db.transitionStateLock.Lock()
defer db.transitionStateLock.Unlock()
if db.CurrentTransitionState != nil {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
Expand All @@ -570,6 +577,8 @@ func (db *cachingDB) SaveTransitionState(root common.Hash) {
}

func (db *cachingDB) LoadTransitionState(root common.Hash) {
db.transitionStateLock.Lock()
defer db.transitionStateLock.Unlock()
// Try to get the transition state from the cache and
// the DB if it's not there.
ts, ok := db.TransitionStatePerRoot.Get(root)
Expand Down Expand Up @@ -614,3 +623,11 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) {
fmt.Println("loaded transition state", "storage processed", db.CurrentTransitionState.StorageProcessed, "addr", db.CurrentTransitionState.CurrentAccountAddress, "slot hash", db.CurrentTransitionState.CurrentSlotHash, "root", root, "ended", db.CurrentTransitionState.ended, "started", db.CurrentTransitionState.started)
debug.PrintStack()
}

func (db *cachingDB) LockCurrentTransitionState() {
db.transitionStateLock.Lock()
}

func (db *cachingDB) UnLockCurrentTransitionState() {
db.transitionStateLock.Unlock()
}
7 changes: 7 additions & 0 deletions light/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func (db *odrDatabase) LoadTransitionState(common.Hash) {
panic("not implemented") // TODO: Implement
}

func (db *odrDatabase) LockCurrentTransitionState() {
panic("not implemented") // TODO: Implement
}
func (db *odrDatabase) UnLockCurrentTransitionState() {
panic("not implemented") // TODO: Implement
}

type odrTrie struct {
db *odrDatabase
id *TrieID
Expand Down
Loading