Skip to content

Commit afc364b

Browse files
committed
eth: add debug_conversionStatus RPC call (#392)
* eth: add debug_conversionStatus RPC call * add debug trace4s * Apply suggestions from code review * export started/ended fields
1 parent 7a3bb67 commit afc364b

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

core/state/database.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
219219
}
220220

221221
func (db *cachingDB) InTransition() bool {
222-
return db.CurrentTransitionState != nil && db.CurrentTransitionState.started && !db.CurrentTransitionState.ended
222+
return db.CurrentTransitionState != nil && db.CurrentTransitionState.Started && !db.CurrentTransitionState.Ended
223223
}
224224

225225
func (db *cachingDB) Transitioned() bool {
226-
return db.CurrentTransitionState != nil && db.CurrentTransitionState.ended
226+
return db.CurrentTransitionState != nil && db.CurrentTransitionState.Ended
227227
}
228228

229229
// Fork implements the fork
@@ -236,7 +236,7 @@ func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.H
236236
|____| |___| /\___ \___ |____/\___ | __/|___| (____ |___| |__| |___| (____ /_____/ \/\_/ |__|___| /_____//_____/
237237
|__|`)
238238
db.CurrentTransitionState = &TransitionState{
239-
started: true,
239+
Started: true,
240240
// initialize so that the first storage-less accounts are processed
241241
StorageProcessed: true,
242242
}
@@ -255,15 +255,15 @@ func (db *cachingDB) ReorgThroughVerkleTransition() {
255255

256256
func (db *cachingDB) InitTransitionStatus(started, ended bool) {
257257
db.CurrentTransitionState = &TransitionState{
258-
ended: ended,
259-
started: started,
258+
Ended: ended,
259+
Started: started,
260260
// TODO add other fields when we handle mid-transition interrupts
261261
}
262262
}
263263

264264
func (db *cachingDB) EndVerkleTransition() {
265-
if !db.CurrentTransitionState.started {
266-
db.CurrentTransitionState.started = true
265+
if !db.CurrentTransitionState.Started {
266+
db.CurrentTransitionState.Started = true
267267
}
268268

269269
fmt.Println(`
@@ -273,14 +273,14 @@ func (db *cachingDB) EndVerkleTransition() {
273273
| | | Y \ ___/ \ ___/| |_\ ___/| |_> | Y \/ __ \| | | | | Y \/ __ \_\___ \ | |__/ __ \| | / /_/ \ ___// /_/ |
274274
|____| |___| /\___ \___ |____/\___ | __/|___| (____ |___| |__| |___| (____ /_____/ |____(____ |___| \____ |\___ \____ |
275275
|__|`)
276-
db.CurrentTransitionState.ended = true
276+
db.CurrentTransitionState.Ended = true
277277
}
278278

279279
type TransitionState struct {
280280
CurrentAccountAddress *common.Address // addresss of the last translated account
281281
CurrentSlotHash common.Hash // hash of the last translated storage slot
282282
CurrentPreimageOffset int64 // next byte to read from the preimage file
283-
started, ended bool
283+
Started, Ended bool
284284

285285
// Mark whether the storage for an account has been processed. This is useful if the
286286
// maximum number of leaves of the conversion is reached before the whole storage is
@@ -290,8 +290,8 @@ type TransitionState struct {
290290

291291
func (ts *TransitionState) Copy() *TransitionState {
292292
ret := &TransitionState{
293-
started: ts.started,
294-
ended: ts.ended,
293+
Started: ts.Started,
294+
Ended: ts.Ended,
295295
CurrentSlotHash: ts.CurrentSlotHash,
296296
CurrentPreimageOffset: ts.CurrentPreimageOffset,
297297
StorageProcessed: ts.StorageProcessed,
@@ -334,14 +334,14 @@ func (db *cachingDB) openMPTTrie(root common.Hash) (Trie, error) {
334334
func (db *cachingDB) openVKTrie(root common.Hash) (Trie, error) {
335335
payload, err := db.DiskDB().Get(trie.FlatDBVerkleNodeKeyPrefix)
336336
if err != nil {
337-
return trie.NewVerkleTrie(verkle.New(), db.triedb, db.addrToPoint, db.CurrentTransitionState.ended), nil
337+
return trie.NewVerkleTrie(verkle.New(), db.triedb, db.addrToPoint, db.CurrentTransitionState.Ended), nil
338338
}
339339

340340
r, err := verkle.ParseNode(payload, 0)
341341
if err != nil {
342342
panic(err)
343343
}
344-
return trie.NewVerkleTrie(r, db.triedb, db.addrToPoint, db.CurrentTransitionState.ended), err
344+
return trie.NewVerkleTrie(r, db.triedb, db.addrToPoint, db.CurrentTransitionState.Ended), err
345345
}
346346

347347
// OpenTrie opens the main account trie at a specific root hash.
@@ -364,7 +364,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
364364

365365
// If the verkle conversion has ended, return a single
366366
// verkle trie.
367-
if db.CurrentTransitionState.ended {
367+
if db.CurrentTransitionState.Ended {
368368
log.Debug("transition ended, returning a simple verkle tree")
369369
return vkt, nil
370370
}
@@ -572,7 +572,7 @@ func (db *cachingDB) SaveTransitionState(root common.Hash) {
572572
rawdb.WriteVerkleTransitionState(db.DiskDB(), root, buf.Bytes())
573573
}
574574

575-
log.Debug("saving 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)
575+
log.Debug("saving 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)
576576
}
577577
}
578578

@@ -590,7 +590,7 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) {
590590
return
591591
}
592592

593-
// if a state could be read from the db, attempt to decode it
593+
// if a state could be read from the db, attempt to decode it
594594
if len(data) > 0 {
595595
var (
596596
newts TransitionState
@@ -613,15 +613,15 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) {
613613
// as a verkle database.
614614
log.Debug("no transition state found, starting fresh", "is verkle", db.triedb.IsVerkle())
615615
// Start with a fresh state
616-
ts = &TransitionState{ended: db.triedb.IsVerkle()}
616+
ts = &TransitionState{Ended: db.triedb.IsVerkle()}
617617
}
618618
}
619619

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

624-
log.Debug("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)
624+
log.Debug("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)
625625
debug.PrintStack()
626626
}
627627

eth/api_debug.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package eth
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"encoding/gob"
2123
"errors"
2224
"fmt"
2325
"time"
@@ -432,3 +434,41 @@ func (api *DebugAPI) SetTrieFlushInterval(interval string) error {
432434
func (api *DebugAPI) GetTrieFlushInterval() string {
433435
return api.eth.blockchain.GetTrieFlushInterval().String()
434436
}
437+
438+
type ConversionStatusResult struct {
439+
Started bool `json:"started"`
440+
Ended bool `json:"ended"`
441+
}
442+
443+
func (api *DebugAPI) ConversionStatus(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*ConversionStatusResult, error) {
444+
block, err := api.eth.APIBackend.BlockByNumberOrHash(ctx, blockNrOrHash)
445+
if err != nil {
446+
return nil, err
447+
}
448+
data, err := rawdb.ReadVerkleTransitionState(api.eth.ChainDb(), block.Root())
449+
if err != nil {
450+
if err.Error() == "pebble: not found" {
451+
return &ConversionStatusResult{}, nil
452+
}
453+
return nil, err
454+
}
455+
log.Info("found entry", "data", data)
456+
if len(data) == 0 {
457+
log.Info("found no data")
458+
// started and ended will be false as no conversion has started
459+
return &ConversionStatusResult{}, nil
460+
}
461+
462+
var (
463+
ts state.TransitionState
464+
buf = bytes.NewBuffer(data[:])
465+
dec = gob.NewDecoder(buf)
466+
)
467+
// Decode transition state
468+
err = dec.Decode(&ts)
469+
if err != nil {
470+
return nil, fmt.Errorf("failed to decode transition state, err=%v", err)
471+
}
472+
473+
return &ConversionStatusResult{Started: ts.Started, Ended: ts.Ended}, nil
474+
}

0 commit comments

Comments
 (0)