Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

GOBIN = ./build/bin
GO ?= latest
GORUN = env GO111MODULE=on go run
GORUN = env GO111MODULE=on go1.22.1 run

geth:
$(GORUN) build/ci.go install ./cmd/geth
Expand Down
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
14 changes: 12 additions & 2 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Flags: flags.Merge([]cli.Flag{utils.CachePreimagesFlag, utils.OverrideVerkle}, utils.DatabasePathFlags),
Flags: flags.Merge([]cli.Flag{utils.CachePreimagesFlag, utils.OverrideVerkle, utils.OverrideStateExpiry, utils.OverrideStateExpiryPeriod}, utils.DatabasePathFlags),
Description: `
The init command initializes a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
Expand Down Expand Up @@ -207,6 +207,16 @@ func initGenesis(ctx *cli.Context) error {
overrides.OverrideVerkle = &v
}

if ctx.IsSet(utils.OverrideStateExpiry.Name) {
v := ctx.Uint64(utils.OverrideStateExpiry.Name)
overrides.OverrideStateExpiry = &v
}

if ctx.IsSet(utils.OverrideStateExpiryPeriod.Name) {
v := ctx.Uint64(utils.OverrideStateExpiryPeriod.Name)
overrides.OverrideStateExpiryPeriod = &v
}

for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabaseWithFreezer(name, 0, 0, ctx.String(utils.AncientFlag.Name), "", false)
if err != nil {
Expand Down Expand Up @@ -513,7 +523,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
8 changes: 8 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
v := ctx.Uint64(utils.OverrideVerkle.Name)
cfg.Eth.OverrideVerkle = &v
}
if ctx.IsSet(utils.OverrideStateExpiry.Name) {
v := ctx.Uint64(utils.OverrideStateExpiry.Name)
cfg.Eth.OverrideStateExpiry = &v
}
if ctx.IsSet(utils.OverrideStateExpiryPeriod.Name) {
v := ctx.Uint64(utils.OverrideStateExpiryPeriod.Name)
cfg.Eth.OverrideStateExpiryPeriod = &v
}
if ctx.IsSet(utils.OverrideProofInBlock.Name) {
v := ctx.Bool(utils.OverrideProofInBlock.Name)
cfg.Eth.OverrideProofInBlock = &v
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ var (
utils.OverrideOverlayStride,
utils.OverrideCancun,
utils.OverrideVerkle,
utils.OverrideStateExpiry,
utils.OverrideStateExpiryPeriod,
utils.OverrideProofInBlock,
utils.ClearVerkleCosts,
utils.EnablePersonal,
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
10 changes: 10 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ var (
Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideStateExpiry = &cli.Uint64Flag{
Name: "override.expiry",
Usage: "Manually specify the state expiry timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideStateExpiryPeriod = &cli.Uint64Flag{
Name: "override.expiryperiod",
Usage: "Manually specify the state expiry period, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideProofInBlock = &cli.BoolFlag{
Name: "override.blockproof",
Usage: "Manually specify the proof-in-block setting",
Expand Down
44 changes: 40 additions & 4 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -414,7 +415,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 +430,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, parentPeriod 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, parentPeriod)
if err != nil {
return nil, nil, fmt.Errorf("error opening pre-state tree root: %w", err)
}
Expand Down Expand Up @@ -469,7 +470,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 All @@ -483,9 +484,44 @@ func BuildVerkleProof(header *types.Header, state *state.StateDB, parentRoot com
}
}
}

fmt.Println(getStateDiffString(stateDiff))

return stateDiff, proof, nil
}

func getStateDiffString(sd verkle.StateDiff) string {
var b strings.Builder
b.WriteString("StateDiff[\n")
for i, stem := range sd {
if i > 0 {
b.WriteString(",\n")
}
b.WriteString(fmt.Sprintf(" Stem: %x\n", stem.Stem))
b.WriteString(fmt.Sprintf(" Period: %d\n", stem.PrePeriod))
b.WriteString(" SuffixDiffs[\n")
for j, suffix := range stem.SuffixDiffs {
if j > 0 {
b.WriteString(",\n")
}
b.WriteString(fmt.Sprintf(" Suffix: %x\n", suffix.Suffix))
if suffix.CurrentValue != nil {
b.WriteString(fmt.Sprintf(" CurrentValue: %x\n", *suffix.CurrentValue))
} else {
b.WriteString(" CurrentValue: nil\n")
}
if suffix.NewValue != nil {
b.WriteString(fmt.Sprintf(" NewValue: %x\n", *suffix.NewValue))
} else {
b.WriteString(" NewValue: nil\n")
}
}
b.WriteString(" ]")
}
b.WriteString("\n]")
return b.String()
}

// Seal generates a new sealing request for the given input block and pushes
// the result into the given channel.
//
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
Loading
Loading