fix the first half of verkle test

This commit is contained in:
Guillaume Ballet 2023-08-09 07:54:32 +02:00
parent faf7fa052c
commit ff71485e31
5 changed files with 36 additions and 21 deletions

View file

@ -121,13 +121,13 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error {
}
// deriveHash computes the state root according to the genesis specification.
func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig) (common.Hash, error) {
func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (common.Hash, error) {
// Create an ephemeral in-memory database for computing hash,
// all the derived states will be discarded to not pollute disk.
db := state.NewDatabase(rawdb.NewMemoryDatabase())
// XXX check this is the case
// TODO remove the nil config check once we have rebased, it should never be nil
if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), 0 /* XXX */) {
if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), timestamp) {
db.EndVerkleTransition()
}
statedb, err := state.New(types.EmptyRootHash, db, nil)
@ -155,7 +155,7 @@ func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhas
}
// End the verkle conversion at genesis if the fork block is 0
if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), 0 /* XXX */) {
if triedb.IsVerkle() {
statedb.Database().EndVerkleTransition()
}
@ -456,7 +456,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
// ToBlock returns the genesis block according to genesis specification.
func (g *Genesis) ToBlock() *types.Block {
root, err := g.Alloc.deriveHash(g.Config)
root, err := g.Alloc.deriveHash(g.Config, g.Timestamp)
if err != nil {
panic(err)
}
@ -547,7 +547,8 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *trie.Database) (*types.Block
// Note the state changes will be committed in hash-based scheme, use Commit
// if path-scheme is preferred.
func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
block, err := g.Commit(db, trie.NewDatabase(db))
triedb := trie.NewDatabaseWithConfig(db, &trie.Config{Verkle: g.Config != nil && g.Config.IsCancun(big.NewInt(int64(g.Number)), g.Timestamp)})
block, err := g.Commit(db, triedb)
if err != nil {
panic(err)
}

View file

@ -219,7 +219,7 @@ func TestReadWriteGenesisAlloc(t *testing.T) {
{1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}},
{2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}},
}
hash, _ = alloc.deriveHash(&params.ChainConfig{})
hash, _ = alloc.deriveHash(&params.ChainConfig{}, 0)
)
blob, _ := json.Marshal(alloc)
rawdb.WriteGenesisStateSpec(db, hash, blob)

View file

@ -195,6 +195,7 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb,
addrToPoint: utils.NewPointCache(),
ended: triedb.IsVerkle(),
}
}
@ -313,8 +314,16 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
err error
)
if db.started {
vkt, err := db.openVKTrie(db.getTranslation(root))
// 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.started || db.ended {
var r common.Hash
if db.ended {
r = root
} else {
r = db.getTranslation(root)
}
vkt, err := db.openVKTrie(r)
if err != nil {
return nil, err
}

View file

@ -175,19 +175,19 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
}
if tr.IsVerkle() {
sdb.witness = NewAccessWitness(sdb)
if sdb.snaps == nil {
snapconfig := snapshot.Config{
CacheSize: 256,
Recovery: false,
NoBuild: false,
AsyncBuild: false,
Verkle: true,
}
sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root)
if err != nil {
return nil, err
}
}
// if sdb.snaps == nil {
// snapconfig := snapshot.Config{
// CacheSize: 256,
// Recovery: false,
// NoBuild: false,
// AsyncBuild: false,
// Verkle: true,
// }
// sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root)
// if err != nil {
// return nil, err
// }
// }
}
if sdb.snaps != nil {
if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap == nil {

View file

@ -33,6 +33,7 @@ type Config struct {
Cache int // Memory allowance (MB) to use for caching trie nodes in memory
Preimages bool // Flag whether the preimage of trie key is recorded
PathDB *pathdb.Config // Configs for experimental path-based scheme, not used yet.
Verkle bool
// Testing hooks
OnCommit func(states *triestate.Set) // Hook invoked when commit is performed
@ -279,3 +280,7 @@ func (db *Database) ClearStorageRootConversion(addr common.Address) {
defer db.addrToRootLock.Unlock()
delete(db.addrToRoot, addr)
}
func (db *Database) IsVerkle() bool {
return db.config != nil && db.config.Verkle
}