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. // 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, // Create an ephemeral in-memory database for computing hash,
// all the derived states will be discarded to not pollute disk. // all the derived states will be discarded to not pollute disk.
db := state.NewDatabase(rawdb.NewMemoryDatabase()) db := state.NewDatabase(rawdb.NewMemoryDatabase())
// XXX check this is the case // XXX check this is the case
// TODO remove the nil config check once we have rebased, it should never be nil // 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() db.EndVerkleTransition()
} }
statedb, err := state.New(types.EmptyRootHash, db, nil) 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 // 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() 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. // ToBlock returns the genesis block according to genesis specification.
func (g *Genesis) ToBlock() *types.Block { 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 { if err != nil {
panic(err) 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 // Note the state changes will be committed in hash-based scheme, use Commit
// if path-scheme is preferred. // if path-scheme is preferred.
func (g *Genesis) MustCommit(db ethdb.Database) *types.Block { 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 { if err != nil {
panic(err) 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}}}, {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}}}, {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) blob, _ := json.Marshal(alloc)
rawdb.WriteGenesisStateSpec(db, hash, blob) 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), codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
triedb: triedb, triedb: triedb,
addrToPoint: utils.NewPointCache(), addrToPoint: utils.NewPointCache(),
ended: triedb.IsVerkle(),
} }
} }
@ -313,8 +314,16 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
err error err error
) )
if db.started { // TODO separate both cases when I can be certain that it won't
vkt, err := db.openVKTrie(db.getTranslation(root)) // 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -175,19 +175,19 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
} }
if tr.IsVerkle() { if tr.IsVerkle() {
sdb.witness = NewAccessWitness(sdb) sdb.witness = NewAccessWitness(sdb)
if sdb.snaps == nil { // if sdb.snaps == nil {
snapconfig := snapshot.Config{ // snapconfig := snapshot.Config{
CacheSize: 256, // CacheSize: 256,
Recovery: false, // Recovery: false,
NoBuild: false, // NoBuild: false,
AsyncBuild: false, // AsyncBuild: false,
Verkle: true, // Verkle: true,
} // }
sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root) // sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
} // }
} }
if sdb.snaps != nil { if sdb.snaps != nil {
if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap == 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 Cache int // Memory allowance (MB) to use for caching trie nodes in memory
Preimages bool // Flag whether the preimage of trie key is recorded Preimages bool // Flag whether the preimage of trie key is recorded
PathDB *pathdb.Config // Configs for experimental path-based scheme, not used yet. PathDB *pathdb.Config // Configs for experimental path-based scheme, not used yet.
Verkle bool
// Testing hooks // Testing hooks
OnCommit func(states *triestate.Set) // Hook invoked when commit is performed 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() defer db.addrToRootLock.Unlock()
delete(db.addrToRoot, addr) delete(db.addrToRoot, addr)
} }
func (db *Database) IsVerkle() bool {
return db.config != nil && db.config.Verkle
}