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

Add EIP7736 #533

Draft
wants to merge 5 commits into
base: kaustinen-with-shapella
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .cursorignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv)
*.json
13 changes: 9 additions & 4 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (b *SimulatedBackend) rollback(parent *types.Block) {
blocks, _ := core.GenerateChain(b.config, parent, ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.blockchain.StateCache(), nil)
pendingPeriod := types.GetStatePeriod(b.config, b.pendingBlock.Time())
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.blockchain.StateCache(), nil, pendingPeriod)
}

// Fork creates a side-chain that can be used to simulate reorgs.
Expand Down Expand Up @@ -187,7 +188,8 @@ func (b *SimulatedBackend) stateByBlockNumber(ctx context.Context, blockNumber *
if err != nil {
return nil, err
}
return b.blockchain.StateAt(block.Root())
blockPeriod := types.GetStatePeriod(b.config, block.Time())
return b.blockchain.StateAt(block.Root(), blockPeriod)
}

// CodeAt returns the code associated with a certain account in the blockchain.
Expand Down Expand Up @@ -660,6 +662,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
GasTipCap: call.GasTipCap,
Data: call.Data,
AccessList: call.AccessList,
// TODO(wh): handle revive
SkipAccountChecks: true,
}

Expand Down Expand Up @@ -703,7 +706,8 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
stateDB, _ := b.blockchain.State()

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
pendingPeriod := types.GetStatePeriod(b.config, b.pendingBlock.Time())
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil, pendingPeriod)
b.pendingReceipts = receipts[0]
return nil
}
Expand Down Expand Up @@ -824,7 +828,8 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
stateDB, _ := b.blockchain.State()

b.pendingBlock = blocks[0]
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil)
pendingPeriod := types.GetStatePeriod(b.config, b.pendingBlock.Time())
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil, pendingPeriod)

return nil
}
Expand Down
1 change: 1 addition & 0 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error)
return c.transact(opts, &c.address, nil)
}

// TODO(wh): handle this, create resurrect tx
func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Address, input []byte, head *types.Header) (*types.Transaction, error) {
// Normalize value
value := opts.Value
Expand Down
10 changes: 5 additions & 5 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}
// Re-create statedb instance with new root upon the updated database
// for accessing latest states.
statedb, err = state.New(root, statedb.Database(), nil)
statedb, err = state.New(root, statedb.Database(), nil, types.Period0)
if err != nil {
return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
}
Expand All @@ -434,7 +434,7 @@ func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prest
sdb.InitTransitionStatus(true, true, common.Hash{})
}

statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
statedb, _ := state.New(types.EmptyRootHash, sdb, nil, types.Period0)

if pre.Env.Ended != nil && *pre.Env.Ended {
vtr := statedb.GetTrie().(*trie.VerkleTrie)
Expand Down Expand Up @@ -528,7 +528,7 @@ func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prest
// to dump the tree that was passed as parameters, but is not suited for block
// execution since it's missing the snapshot and therefore it can't go through
// the conversion.
statedb, err = state.New(types.EmptyRootHash, sdb, nil)
statedb, err = state.New(types.EmptyRootHash, sdb, nil, types.Period0)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -562,12 +562,12 @@ func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prest

// recreate the verkle db with the tree root, but this time with the mpt snapshot,
// so that the conversion can proceed.
statedb, err = state.New(root, sdb, snaps)
statedb, err = state.New(root, sdb, snaps, types.Period0)
if err != nil {
panic(err)
}
} else {
statedb, err = state.New(mptRoot, sdb, nil)
statedb, err = state.New(mptRoot, sdb, nil, types.Period0)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func Transaction(ctx *cli.Context) error {
r.Address = sender
}
// Check intrinsic gas
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil,
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.ReviveList(), tx.To() == nil,
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int)), chainConfig.IsShanghai(new(big.Int), 0)); err != nil {
r.Error = err
results = append(results, r)
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func VerkleRoot(ctx *cli.Context) error {
}

func genVktFromAlloc(alloc core.GenesisAlloc) (*trie.VerkleTrie, error) {
vkt := trie.NewVerkleTrie(verkle.New(), trie.NewDatabase(rawdb.NewMemoryDatabase()), utils.NewPointCache(), true)
vkt := trie.NewVerkleTrie(verkle.New(), trie.NewDatabase(rawdb.NewMemoryDatabase()), utils.NewPointCache(), true, 0)

for addr, acc := range alloc {
for slot, value := range acc.Storage {
Expand Down
4 changes: 2 additions & 2 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func runCmd(ctx *cli.Context) error {
db := rawdb.NewMemoryDatabase()
genesis := gen.MustCommit(db)
sdb := state.NewDatabaseWithConfig(db, &trie.Config{Preimages: preimages})
statedb, _ = state.New(genesis.Root(), sdb, nil)
statedb, _ = state.New(genesis.Root(), sdb, nil, types.Period0)
chainConfig = gen.Config
} else {
sdb := state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: preimages})
statedb, _ = state.New(types.EmptyRootHash, sdb, nil)
statedb, _ = state.New(types.EmptyRootHash, sdb, nil, types.Period0)
genesisConfig = new(core.Genesis)
}
if ctx.String(SenderFlag.Name) != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func runStateTest(fname string, cfg vm.Config, jsonOut, dump bool) error {
// Test failed, mark as so and dump any state to aid debugging
result.Pass, result.Error = false, err.Error()
if dump && s != nil {
s, _ = state.New(*result.Root, s.Database(), nil)
s, _ = state.New(*result.Root, s.Database(), nil, 0)
dump := s.RawDump(nil)
result.State = &dump
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func dump(ctx *cli.Context) error {
config := &trie.Config{
Preimages: true, // always enable preimage lookup
}
state, err := state.New(root, state.NewDatabaseWithConfig(db, config), nil)
state, err := state.New(root, state.NewDatabaseWithConfig(db, config), nil, types.Period0) // TODO(wh): load head block and get the period
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/geth/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func convertToVerkle(ctx *cli.Context) error {
lastReport time.Time
start = time.Now()
vRoot = verkle.New().(*verkle.InternalNode)
curPeriod = verkle.StatePeriod(0) // we assume conversion always starts from period 0
)

saveverkle := func(path []byte, node verkle.VerkleNode) {
Expand Down Expand Up @@ -202,7 +203,7 @@ func convertToVerkle(ctx *cli.Context) error {

// Otherwise, store the previous group in the tree with a
// stem insertion.
vRoot.InsertValuesAtStem(chunkkey[:31], values, chaindb.Get)
vRoot.InsertValuesAtStem(chunkkey[:31], values, curPeriod, false, chaindb.Get)
}

// Write the code size in the account header group
Expand Down Expand Up @@ -267,7 +268,7 @@ func convertToVerkle(ctx *cli.Context) error {
copy(k[:], []byte(s))
// reminder that InsertStem will merge leaves
// if they exist.
vRoot.InsertValuesAtStem(k[:31], vs, chaindb.Get)
vRoot.InsertValuesAtStem(k[:31], vs, curPeriod, false, chaindb.Get)
}
translatedStorage = make(map[string][][]byte)
vRoot.FlushAtDepth(2, saveverkle)
Expand All @@ -276,7 +277,7 @@ func convertToVerkle(ctx *cli.Context) error {
for s, vs := range translatedStorage {
var k [31]byte
copy(k[:], []byte(s))
vRoot.InsertValuesAtStem(k[:31], vs, chaindb.Get)
vRoot.InsertValuesAtStem(k[:31], vs, curPeriod, false, chaindb.Get)
}
storageIt.Release()
if storageIt.Error() != nil {
Expand All @@ -285,7 +286,7 @@ func convertToVerkle(ctx *cli.Context) error {
}
}
// Finish with storing the complete account header group inside the tree.
vRoot.InsertValuesAtStem(stem[:31], newValues, chaindb.Get)
vRoot.InsertValuesAtStem(stem[:31], newValues, curPeriod, false, chaindb.Get)

if time.Since(lastReport) > time.Second*8 {
log.Info("Traversing state", "accounts", accounts, "elapsed", common.PrettyDuration(time.Since(start)))
Expand Down Expand Up @@ -445,7 +446,7 @@ func expandVerkle(ctx *cli.Context) error {

for i, key := range keylist {
log.Info("Reading key", "index", i, "key", keylist[0])
root.Get(key, chaindb.Get)
root.Get(key, 0, chaindb.Get)
}

if err := os.WriteFile("dump.dot", []byte(verkle.ToDot(root)), 0o600); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
state.Database().LoadTransitionState(parent.Root)

var err error
stateDiff, proof, err = BuildVerkleProof(header, state, parent.Root)
stateDiff, proof, err = BuildVerkleProof(header, state, parent.Root, types.GetStatePeriod(chain.Config(), parent.Time))
if err != nil {
return nil, fmt.Errorf("error building verkle proof: %w", err)
}
Expand All @@ -429,13 +429,13 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
return block, nil
}

func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot common.Hash) (verkle.StateDiff, *verkle.VerkleProof, error) {
func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot common.Hash, curPeriod verkle.StatePeriod) (verkle.StateDiff, *verkle.VerkleProof, error) {
var (
proof *verkle.VerkleProof
stateDiff verkle.StateDiff
)

preTrie, err := state.Database().OpenTrie(parentRoot)
preTrie, err := state.Database().OpenTrie(parentRoot, curPeriod)
if err != nil {
return nil, nil, fmt.Errorf("error opening pre-state tree root: %w", err)
}
Expand Down Expand Up @@ -469,7 +469,7 @@ func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot com
// conversion, when the previous tree is a merkle tree.
// Logically, the "previous" verkle tree is an empty tree.
okpre = true
vtrpre = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false)
vtrpre = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false, types.Period0)
post := state.GetTrie().(*trie.TransitionTrie)
vtrpost = post.Overlay()
okpost = true
Expand Down
2 changes: 1 addition & 1 deletion core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
return func(i int, gen *BlockGen) {
toaddr := common.Address{}
data := make([]byte, nbytes)
gas, _ := IntrinsicGas(data, nil, false, false, false, false)
gas, _ := IntrinsicGas(data, nil, nil, false, false, false, false)
signer := types.MakeSigner(gen.config, big.NewInt(int64(i)), gen.header.Time)
gasPrice := big.NewInt(0)
if gen.header.BaseFee != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD
if parent == nil {
return fmt.Errorf("nil parent header for block %d", header.Number)
}
stateDiff, proof, err := beacon.BuildVerkleProof(header, statedb, parent.Root())
stateDiff, proof, err := beacon.BuildVerkleProof(header, statedb, parent.Root(), types.GetStatePeriod(v.bc.Config(), parent.Time()))
if err != nil {
return fmt.Errorf("error building verkle proof: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}

curPeriod := types.GetStatePeriod(bc.Config(), block.Time())
if bc.Config().IsVerkle(block.Number(), block.Time()) {
bc.stateCache.LoadTransitionState(parent.Root)

Expand All @@ -1778,7 +1779,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time, parent.Root)
bc.stateCache.SetLastMerkleRoot(parent.Root)
}
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps, curPeriod)
if err != nil {
return it.index, err
}
Expand All @@ -1792,7 +1793,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
var followupInterrupt atomic.Bool
if !bc.cacheConfig.TrieCleanNoPrefetch {
if followup, err := it.peek(); followup != nil && err == nil {
throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps)
blockPeriod := types.GetStatePeriod(bc.Config(), followup.Time())
throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps, blockPeriod)

go func(start time.Time, followup *types.Block, throwaway *state.StateDB) {
bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt)
Expand Down
11 changes: 7 additions & 4 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-verkle"
)

// CurrentHeader retrieves the current head header of the canonical chain. The
Expand Down Expand Up @@ -278,7 +279,7 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {

// HasState checks if state trie is fully present in the database or not.
func (bc *BlockChain) HasState(hash common.Hash) bool {
_, err := bc.stateCache.OpenTrie(hash)
_, err := bc.stateCache.OpenTrie(hash, types.Period0) // we can ignore the actual period since we're only checking for presence
return err == nil
}

Expand Down Expand Up @@ -315,12 +316,14 @@ func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) ([]byte, error) {

// State returns a new mutable state based on the current HEAD block.
func (bc *BlockChain) State() (*state.StateDB, error) {
return bc.StateAt(bc.CurrentBlock().Root)
curBlock := bc.CurrentBlock()
curPeriod := types.GetStatePeriod(bc.chainConfig, curBlock.Time)
return bc.StateAt(curBlock.Root, curPeriod)
}

// StateAt returns a new mutable state based on a particular point in time.
func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
return state.New(root, bc.stateCache, bc.snaps)
func (bc *BlockChain) StateAt(root common.Hash, curPeriod verkle.StatePeriod) (*state.StateDB, error) {
return state.New(root, bc.stateCache, bc.snaps, curPeriod)
}

// Config retrieves the chain's fork configuration.
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
}
return err
}
statedb, err := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateCache, nil)
statedb, err := state.New(blockchain.GetBlockByHash(block.ParentHash()).Root(), blockchain.stateCache, nil, types.Period0)
if err != nil {
return err
}
Expand Down Expand Up @@ -4222,7 +4222,7 @@ func TestTransientStorageReset(t *testing.T) {
t.Fatalf("failed to insert into chain: %v", err)
}
// Check the storage
state, err := chain.StateAt(chain.CurrentHeader().Root)
state, err := chain.StateAt(chain.CurrentHeader().Root, types.Period0)
if err != nil {
t.Fatalf("Failed to load state %v", err)
}
Expand Down
10 changes: 7 additions & 3 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
return nil, nil
}
for i := 0; i < n; i++ {
statedb, err := state.New(parent.Root(), state.NewDatabase(db), nil)
parentPeriod := types.GetStatePeriod(config, parent.Time())
statedb, err := state.New(parent.Root(), state.NewDatabase(db), nil, parentPeriod)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -385,6 +386,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
b.header = makeHeader(chainreader, parent, statedb, b.engine)
preState := statedb.Copy()
fmt.Println("prestate", preState.GetTrie().(*trie.VerkleTrie).ToDot())
statedb.SetCurPeriod(types.GetStatePeriod(config, b.header.Time)) // update period as we are going to process new block

if config.IsVerkle(b.header.Number, b.header.Time) {
if !config.IsVerkle(b.parent.Number(), b.parent.Time()) {
Expand Down Expand Up @@ -432,7 +434,8 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
proots = append(proots, parent.Root())

// quick check that we are self-consistent
err = verkle.Verify(block.ExecutionWitness().VerkleProof, block.ExecutionWitness().ParentStateRoot[:], block.Root().Bytes(), block.ExecutionWitness().StateDiff)
blockPeriod := types.GetStatePeriod(config, b.header.Time)
err = verkle.Verify(block.ExecutionWitness().VerkleProof, block.ExecutionWitness().ParentStateRoot[:], block.Root().Bytes(), block.ExecutionWitness().StateDiff, blockPeriod)
if err != nil {
panic(err)
}
Expand All @@ -447,7 +450,8 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
db.EndVerkleTransition()
db.SaveTransitionState(parent.Root())
for i := 0; i < n; i++ {
statedb, err := state.New(parent.Root(), db, snaps)
curPeriod := types.GetStatePeriod(config, parent.Time())
statedb, err := state.New(parent.Root(), db, snaps, curPeriod)
if err != nil {
panic(fmt.Sprintf("could not find state for block %d: err=%v, parent root=%x", i, err, parent.Root()))
}
Expand Down
Loading
Loading