Skip to content

Commit 1627805

Browse files
committed
all: implement EIP-compliant verkle trees
verkle: Implement Trie, NodeIterator and Database ifs Fix crash in TestDump Fix TestDump Fix TrieCopy remove unnecessary traces fix: Error() returned errIteratorEnd in verkle node iterator rewrite the iterator and change the signature of OpenStorageTrie add the adapter to reuse the account trie for storage don't try to deserialize a storage leaf into an account Fix statedb unit tests (#14) * debug code * Fix more unit tests * remove traces * Go back to the full range One tree to rule them all remove updateRoot, there is no root to update store code inside the account leaf fix build save current state for Sina Update go-verkle to latest Charge WITNESS_*_COST gas on storage loads Add witness costs for SSTORE as well Charge witness gas in the case of code execution corresponding code deletion add a --verkle flag to separate verkle experiments from regular geth operations use the snapshot to get data stateless execution from block witness AccessWitness functions Add block generation test + genesis snapshot generation test stateless block execution (#18) * test stateless block execution * Force tree resolution before generating the proof increased coverage in stateless test execution (#19) * test stateless block execution * Force tree resolution before generating the proof * increase coverage in stateless test execution ensure geth compiles fix issues in tests with verkle trees deactivated Ensure stateless data is available when executing statelessly (#20) * Ensure stateless data is available when executing statelessly * Actual execution of a statless block * bugfixes in stateless block execution * code cleanup - Reduce PR footprint by reverting NewEVM to its original signature - Move the access witness to the block context - prepare for a change in AW semantics Need to store the initial values. - Use the touch helper function, DRY * revert the signature of MustCommit to its original form (#21) fix leaf proofs in stateless execution (#22) * Fixes in witness pre-state * Add the recipient's nonce to the witness * reduce PR footprint and investigate issue in root state calculation * quick build fix cleanup: Remove extra parameter in ToBlock revert ToBlock to its older signature fix import cycle in vm tests fix linter issue fix appveyor build fix nil pointers in tests Add indices, yis and Cis to the block's Verkle proof upgrade geth dependency to drop geth's common dep fix cmd/devp2p tests fix rebase issues quell an appveyor warning fix address touching in SLOAD and SSTORE fix access witness for code size touch target account data before calling make sure the proper locations get touched in (ext)codecopy touch all code pages in execution add pushdata to witness remove useless code in genesis snapshot generation testnet: fix some of the rebase/drift issues Fix verkle proof generation in block fix an issue occuring when chunking past the code size fix: ensure the code copy doesn't extend past the code size
1 parent c10a0a6 commit 1627805

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1731
-66
lines changed

accounts/abi/bind/bind_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ func TestGolangBindings(t *testing.T) {
19831983
t.Fatalf("failed to tidy Go module file: %v\n%s", err, out)
19841984
}
19851985
// Test the entire package and report any failures
1986-
cmd := exec.Command(gocmd, "test", "-v", "-count", "1")
1986+
cmd := exec.Command(gocmd, "test", "-tags=bignum_kilic", "-v", "-count", "1")
19871987
cmd.Dir = pkg
19881988
if out, err := cmd.CombinedOutput(); err != nil {
19891989
t.Fatalf("failed to run binding test: %v\n%s", err, out)

cmd/geth/chaincmd.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
ArgsUsage: "<genesisPath>",
5050
Flags: []cli.Flag{
5151
utils.DataDirFlag,
52+
utils.VerkleFlag,
5253
},
5354
Category: "BLOCKCHAIN COMMANDS",
5455
Description: `
@@ -205,6 +206,10 @@ func initGenesis(ctx *cli.Context) error {
205206
stack, _ := makeConfigNode(ctx)
206207
defer stack.Close()
207208

209+
if ctx.GlobalBool(utils.VerkleFlag.Name) {
210+
genesis.Config.UseVerkle = true
211+
}
212+
208213
for _, name := range []string{"chaindata", "lightchaindata"} {
209214
chaindb, err := stack.OpenDatabase(name, 0, 0, "", false)
210215
if err != nil {

cmd/geth/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ var (
158158
utils.MinerNotifyFullFlag,
159159
configFileFlag,
160160
utils.CatalystFlag,
161+
utils.VerkleFlag,
161162
}
162163

163164
rpcFlags = []cli.Flag{

cmd/geth/snapshot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func verifyState(ctx *cli.Context) error {
220220
log.Error("Failed to load head block")
221221
return errors.New("no head block")
222222
}
223-
snaptree, err := snapshot.New(chaindb, trie.NewDatabase(chaindb), 256, headBlock.Root(), false, false, false)
223+
snaptree, err := snapshot.New(chaindb, trie.NewDatabase(chaindb), 256, headBlock.Root(), false, false, false, false)
224224
if err != nil {
225225
log.Error("Failed to open snapshot tree", "err", err)
226226
return err
@@ -472,7 +472,7 @@ func dumpState(ctx *cli.Context) error {
472472
if err != nil {
473473
return err
474474
}
475-
snaptree, err := snapshot.New(db, trie.NewDatabase(db), 256, root, false, false, false)
475+
snaptree, err := snapshot.New(db, trie.NewDatabase(db), 256, root, false, false, false, false)
476476
if err != nil {
477477
return err
478478
}

cmd/geth/usage.go

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ var AppHelpFlagGroups = []flags.FlagGroup{
230230
utils.BloomFilterSizeFlag,
231231
cli.HelpFlag,
232232
utils.CatalystFlag,
233+
utils.VerkleFlag,
233234
},
234235
},
235236
}

cmd/utils/flags.go

+16
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,11 @@ var (
794794
Name: "catalyst",
795795
Usage: "Catalyst mode (eth2 integration testing)",
796796
}
797+
798+
VerkleFlag = cli.BoolFlag{
799+
Name: "verkle",
800+
Usage: "Enable geth with verkle trees (EXPERIMENTAL)",
801+
}
797802
)
798803

799804
// MakeDataDir retrieves the currently requested data directory, terminating
@@ -1475,7 +1480,11 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
14751480
// SetEthConfig applies eth-related command line flags to the config.
14761481
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
14771482
// Avoid conflicting network flags
1483+
<<<<<<< HEAD
14781484
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, RopstenFlag, RinkebyFlag, GoerliFlag, SepoliaFlag)
1485+
=======
1486+
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, RopstenFlag, RinkebyFlag, GoerliFlag, VerkleFlag)
1487+
>>>>>>> 00ed1c501 (all: implement EIP-compliant verkle trees)
14791488
CheckExclusive(ctx, LightServeFlag, SyncModeFlag, "light")
14801489
CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
14811490
if ctx.GlobalString(GCModeFlag.Name) == "archive" && ctx.GlobalUint64(TxLookupLimitFlag.Name) != 0 {
@@ -1613,6 +1622,13 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16131622
}
16141623
cfg.Genesis = core.DefaultGenesisBlock()
16151624
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
1625+
case ctx.GlobalBool(VerkleFlag.Name):
1626+
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
1627+
cfg.NetworkId = 86 // 'V'
1628+
}
1629+
cfg.Genesis = core.DefaultVerkleGenesisBlock()
1630+
cfg.Genesis.Config.UseVerkle = true
1631+
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
16161632
case ctx.GlobalBool(RopstenFlag.Name):
16171633
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
16181634
cfg.NetworkId = 3

core/blockchain.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
234234
Cache: cacheConfig.TrieCleanLimit,
235235
Journal: cacheConfig.TrieCleanJournal,
236236
Preimages: cacheConfig.Preimages,
237+
UseVerkle: chainConfig.UseVerkle,
237238
}),
238239
quit: make(chan struct{}),
239240
chainmu: syncx.NewClosableMutex(),
@@ -375,7 +376,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
375376
log.Warn("Enabling snapshot recovery", "chainhead", head.NumberU64(), "diskbase", *layer)
376377
recover = true
377378
}
378-
bc.snaps, _ = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, head.Root(), !bc.cacheConfig.SnapshotWait, true, recover)
379+
bc.snaps, _ = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, head.Root(), !bc.cacheConfig.SnapshotWait, true, recover, bc.Config().UseVerkle)
379380
}
380381

381382
// Start future block processor.
@@ -1592,7 +1593,22 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
15921593

15931594
// Process block using the parent state as reference point
15941595
substart := time.Now()
1595-
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
1596+
var (
1597+
usedGas uint64
1598+
receipts types.Receipts
1599+
logs []*types.Log
1600+
)
1601+
if len(block.Header().VerkleProof) == 0 {
1602+
receipts, logs, _, usedGas, err = bc.processor.Process(block, statedb, bc.vmConfig)
1603+
} else {
1604+
var leaves map[common.Hash]common.Hash
1605+
_, _, _, leaves, err = trie.DeserializeAndVerifyVerkleProof(block.Header().VerkleProof)
1606+
if err != nil {
1607+
return it.index, err
1608+
}
1609+
statedb.SetStateless(leaves)
1610+
receipts, logs, usedGas, err = bc.processor.ProcessStateless(block, statedb, bc.vmConfig, leaves)
1611+
}
15961612
if err != nil {
15971613
bc.reportBlock(block, receipts, err)
15981614
atomic.StoreUint32(&followupInterrupt, 1)

core/blockchain_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
160160
if err != nil {
161161
return err
162162
}
163-
receipts, _, usedGas, err := blockchain.processor.Process(block, statedb, vm.Config{})
163+
receipts, _, _, usedGas, err := blockchain.processor.Process(block, statedb, vm.Config{})
164164
if err != nil {
165165
blockchain.reportBlock(block, receipts, err)
166166
return err

core/chain_makers.go

+94-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/core/vm"
2929
"github.com/ethereum/go-ethereum/ethdb"
3030
"github.com/ethereum/go-ethereum/params"
31+
"github.com/ethereum/go-ethereum/trie"
3132
)
3233

3334
// BlockGen creates blocks for testing.
@@ -43,6 +44,7 @@ type BlockGen struct {
4344
txs []*types.Transaction
4445
receipts []*types.Receipt
4546
uncles []*types.Header
47+
witness *types.AccessWitness
4648

4749
config *params.ChainConfig
4850
engine consensus.Engine
@@ -103,10 +105,17 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
103105
b.SetCoinbase(common.Address{})
104106
}
105107
b.statedb.Prepare(tx.Hash(), len(b.txs))
106-
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{})
108+
receipt, accesses, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{})
107109
if err != nil {
108110
panic(err)
109111
}
112+
if accesses != nil {
113+
if b.witness != nil {
114+
b.witness.Merge(accesses)
115+
} else {
116+
b.witness = accesses
117+
}
118+
}
110119
b.txs = append(b.txs, tx)
111120
b.receipts = append(b.receipts, receipt)
112121
}
@@ -284,6 +293,90 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
284293
return blocks, receipts
285294
}
286295

296+
func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine consensus.Engine, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
297+
if config == nil {
298+
config = params.TestChainConfig
299+
}
300+
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
301+
chainreader := &fakeChainReader{config: config}
302+
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
303+
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine, witness: types.NewAccessWitness()}
304+
b.header = makeHeader(chainreader, parent, statedb, b.engine)
305+
306+
// Mutate the state and block according to any hard-fork specs
307+
if daoBlock := config.DAOForkBlock; daoBlock != nil {
308+
limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
309+
if b.header.Number.Cmp(daoBlock) >= 0 && b.header.Number.Cmp(limit) < 0 {
310+
if config.DAOForkSupport {
311+
b.header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
312+
}
313+
}
314+
}
315+
if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(b.header.Number) == 0 {
316+
misc.ApplyDAOHardFork(statedb)
317+
}
318+
// Execute any user modifications to the block
319+
if gen != nil {
320+
gen(i, b)
321+
}
322+
if b.engine != nil {
323+
// Finalize and seal the block
324+
block, err := b.engine.FinalizeAndAssemble(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
325+
if err != nil {
326+
panic(err)
327+
}
328+
329+
// Write state changes to db
330+
root, err := statedb.Commit(config.IsEIP158(b.header.Number))
331+
if err != nil {
332+
panic(fmt.Sprintf("state write error: %v", err))
333+
}
334+
if err := statedb.Database().TrieDB().Commit(root, false, nil); err != nil {
335+
panic(fmt.Sprintf("trie write error: %v", err))
336+
}
337+
338+
// Generate an associated verkle proof
339+
if tr := statedb.GetTrie(); tr.IsVerkle() {
340+
vtr := tr.(*trie.VerkleTrie)
341+
// Generate the proof if we are using a verkle tree
342+
// WORKAROUND: make sure all keys are resolved
343+
// before building the proof. Ultimately, node
344+
// resolution can be done with a prefetcher or
345+
// from GetCommitmentsAlongPath.
346+
keys := b.witness.Keys()
347+
for _, key := range keys {
348+
out, err := vtr.TryGet(key)
349+
if err != nil {
350+
panic(err)
351+
}
352+
if len(out) == 0 {
353+
panic(fmt.Sprintf("%x should be present in the tree", key))
354+
}
355+
}
356+
vtr.Hash()
357+
_, err := vtr.ProveAndSerialize(keys, b.witness.KeyVals())
358+
//block.SetVerkleProof(p)
359+
if err != nil {
360+
panic(err)
361+
}
362+
}
363+
return block, b.receipts
364+
}
365+
return nil, nil
366+
}
367+
for i := 0; i < n; i++ {
368+
statedb, err := state.New(parent.Root(), state.NewDatabaseWithConfig(db, &trie.Config{UseVerkle: true}), nil)
369+
if err != nil {
370+
panic(err)
371+
}
372+
block, receipt := genblock(i, parent, statedb)
373+
blocks[i] = block
374+
receipts[i] = receipt
375+
parent = block
376+
}
377+
return blocks, receipts
378+
}
379+
287380
func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.StateDB, engine consensus.Engine) *types.Header {
288381
var time uint64
289382
if parent.Time() == 0 {

core/evm.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func NewEVMTxContext(msg Message) vm.TxContext {
6969
return vm.TxContext{
7070
Origin: msg.From(),
7171
GasPrice: new(big.Int).Set(msg.GasPrice()),
72+
Accesses: types.NewAccessWitness(),
7273
}
7374
}
7475

core/genesis.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
180180
// We have the genesis block in database(perhaps in ancient database)
181181
// but the corresponding state is missing.
182182
header := rawdb.ReadHeader(db, stored, 0)
183-
if _, err := state.New(header.Root, state.NewDatabaseWithConfig(db, nil), nil); err != nil {
183+
var cfg *trie.Config = nil
184+
if genesis.Config.UseVerkle {
185+
cfg = &trie.Config{UseVerkle: true}
186+
}
187+
if _, err := state.New(header.Root, state.NewDatabaseWithConfig(db, cfg), nil); err != nil {
184188
if genesis == nil {
185189
genesis = DefaultGenesisBlock()
186190
}
@@ -264,7 +268,11 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
264268
if db == nil {
265269
db = rawdb.NewMemoryDatabase()
266270
}
267-
statedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil)
271+
var trieCfg *trie.Config
272+
if g.Config != nil {
273+
trieCfg = &trie.Config{UseVerkle: g.Config.UseVerkle}
274+
}
275+
statedb, err := state.New(common.Hash{}, state.NewDatabaseWithConfig(db, trieCfg), nil)
268276
if err != nil {
269277
panic(err)
270278
}
@@ -306,6 +314,9 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
306314
}
307315
statedb.Commit(false)
308316
statedb.Database().TrieDB().Commit(root, true, nil)
317+
if err := statedb.Cap(root); err != nil {
318+
panic(err)
319+
}
309320

310321
return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil))
311322
}
@@ -357,6 +368,20 @@ func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big
357368
return g.MustCommit(db)
358369
}
359370

371+
func DefaultVerkleGenesisBlock() *Genesis {
372+
return &Genesis{
373+
Config: params.VerkleChainConfig,
374+
Nonce: 86,
375+
GasLimit: 0x2fefd8,
376+
Difficulty: big.NewInt(1),
377+
Alloc: map[common.Address]GenesisAccount{
378+
common.BytesToAddress([]byte{97, 118, 97, 209, 72, 165, 43, 239, 81, 162, 104, 199, 40, 179, 162, 27, 88, 249, 67, 6}): {
379+
Balance: big.NewInt(0).Lsh(big.NewInt(1), 27),
380+
},
381+
},
382+
}
383+
}
384+
360385
// DefaultGenesisBlock returns the Ethereum main net genesis block.
361386
func DefaultGenesisBlock() *Genesis {
362387
return &Genesis{

0 commit comments

Comments
 (0)