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

all: implement withdrawals (EIP-4895) #26484

Merged
merged 43 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
be4ad19
core/types: add withdrawal type
lightclient Nov 5, 2022
72c91a4
core/beacon: add withdrawals and make types version agnostic
lightclient Nov 5, 2022
1779e45
eth/catalyst,les/catalyst: add withdrawals and make types version agn…
lightclient Nov 5, 2022
79ed6d4
miner: add withdrawals and update catalyst types
lightclient Nov 5, 2022
ed15060
consensus: process withdrawals
lightclient Nov 5, 2022
43ff685
core/state_processor: finalize with withdrawals
lightclient Nov 5, 2022
5e37321
core/chain_makers: add withdrawal support
lightclient Nov 5, 2022
cf34465
core/block_validator: verify withdrawals root
lightclient Nov 5, 2022
e7170c2
core/rawdb: update block readers with new block format
lightclient Nov 5, 2022
18a3ced
eth: add p2p support for withdrawals
lightclient Nov 5, 2022
8c55bd6
cmd/evm: support withdrawals in t8n and b11r
lightclient Nov 5, 2022
a6dd779
eth/downloader: always use WithBody2 when constructing blocks
lightclient Nov 30, 2022
f00089f
eth/fetcher: always use WithBody2 when constructing blocks
lightclient Nov 30, 2022
c66dc48
params: core: enable shanghai based on timestamps
MariusVanDerWijden Sep 27, 2022
7c1651b
core,consensus: use timestamp based is shanghai check
lightclient Nov 6, 2022
218b71a
consensus: fail if running shanghai on clique or ethash
lightclient Nov 6, 2022
5c5cd11
core: set withdrawals hash in genesis, fix tests, add withdrawalRoot
MariusVanDerWijden Nov 8, 2022
2e8233a
all: implement forkid changes for shanghai
MariusVanDerWijden Nov 10, 2022
9c905c2
core/types: make fetchers fetch withdrawals without transactions
MariusVanDerWijden Dec 1, 2022
5daaafe
internal/ethapi: add withdrawals to full block
MariusVanDerWijden Nov 22, 2022
f701ea4
internal/ethapi: add comment
MariusVanDerWijden Dec 7, 2022
91b284e
miner: catalyst: return transaction fees for block
MariusVanDerWijden Oct 16, 2022
47b582d
core/forkid: fix forkid filter
MariusVanDerWijden Dec 13, 2022
a686276
core/forkid: reduce diff by anonymous function
MariusVanDerWijden Dec 15, 2022
91b47e3
core/beacon: correctly marshal block value
MariusVanDerWijden Dec 17, 2022
101041a
core: fix rebasing issues
MariusVanDerWijden Jan 11, 2023
bac9e89
eth/protocols/eth: revert accidental modification
MariusVanDerWijden Jan 13, 2023
300f5f2
core, eth: use gwei for withdrawals
MariusVanDerWijden Jan 13, 2023
f959d38
eth/catalyst: use gwei in test instead of hard coded value
lightclient Jan 13, 2023
cf0ed02
core: fix nil panic
MariusVanDerWijden Jan 18, 2023
8c74d60
all: fix review comments, add beacon.Faker, fix tests
MariusVanDerWijden Jan 19, 2023
3263936
consensus/beacon: add faker
MariusVanDerWijden Jan 19, 2023
1931e38
eth/catalyst: go generate, fix nitpicks
MariusVanDerWijden Jan 19, 2023
493741d
Update consensus/beacon/consensus.go
holiman Jan 25, 2023
dd3c41a
Apply suggestions from code review
holiman Jan 25, 2023
7b073d1
Update internal/ethapi/api.go
holiman Jan 25, 2023
3481ebc
Update core/types/block.go
holiman Jan 25, 2023
b43da37
all: rebase on uint64 timestamps
MariusVanDerWijden Jan 25, 2023
f665111
consensus/beacon: add check for Shanghai activation in FinalizeAndAss…
fjl Jan 25, 2023
31a979c
core: remove Shanghai fork check in GenerateChain
fjl Jan 25, 2023
8bdbf39
consensus/beacon: remove Shanghai fork check in Finalize
fjl Jan 25, 2023
8783b19
core: simplify withdrawals existence check in StateProcessor
fjl Jan 25, 2023
428b0e6
consensus/beacon: ensure correct empty withdrawals root in Shanghai b…
fjl Jan 25, 2023
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
108 changes: 59 additions & 49 deletions cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ import (

//go:generate go run github.com/fjl/gencodec -type header -field-override headerMarshaling -out gen_header.go
type header struct {
ParentHash common.Hash `json:"parentHash"`
OmmerHash *common.Hash `json:"sha3Uncles"`
Coinbase *common.Address `json:"miner"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *big.Int `json:"difficulty"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed"`
Time uint64 `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
ParentHash common.Hash `json:"parentHash"`
OmmerHash *common.Hash `json:"sha3Uncles"`
Coinbase *common.Address `json:"miner"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *big.Int `json:"difficulty"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed"`
Time uint64 `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
WithdrawalsHash *common.Hash `json:"withdrawalsRoot" rlp:"optional"`
}

type headerMarshaling struct {
Expand All @@ -67,10 +68,11 @@ type headerMarshaling struct {
}

type bbInput struct {
Header *header `json:"header,omitempty"`
OmmersRlp []string `json:"ommers,omitempty"`
TxRlp string `json:"txs,omitempty"`
Clique *cliqueInput `json:"clique,omitempty"`
Header *header `json:"header,omitempty"`
OmmersRlp []string `json:"ommers,omitempty"`
TxRlp string `json:"txs,omitempty"`
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
Clique *cliqueInput `json:"clique,omitempty"`

Ethash bool `json:"-"`
EthashDir string `json:"-"`
Expand Down Expand Up @@ -114,21 +116,22 @@ func (c *cliqueInput) UnmarshalJSON(input []byte) error {
// ToBlock converts i into a *types.Block
func (i *bbInput) ToBlock() *types.Block {
header := &types.Header{
ParentHash: i.Header.ParentHash,
UncleHash: types.EmptyUncleHash,
Coinbase: common.Address{},
Root: i.Header.Root,
TxHash: types.EmptyRootHash,
ReceiptHash: types.EmptyRootHash,
Bloom: i.Header.Bloom,
Difficulty: common.Big0,
Number: i.Header.Number,
GasLimit: i.Header.GasLimit,
GasUsed: i.Header.GasUsed,
Time: i.Header.Time,
Extra: i.Header.Extra,
MixDigest: i.Header.MixDigest,
BaseFee: i.Header.BaseFee,
ParentHash: i.Header.ParentHash,
UncleHash: types.EmptyUncleHash,
Coinbase: common.Address{},
Root: i.Header.Root,
TxHash: types.EmptyRootHash,
ReceiptHash: types.EmptyRootHash,
Bloom: i.Header.Bloom,
Difficulty: common.Big0,
Number: i.Header.Number,
GasLimit: i.Header.GasLimit,
GasUsed: i.Header.GasUsed,
Time: i.Header.Time,
Extra: i.Header.Extra,
MixDigest: i.Header.MixDigest,
BaseFee: i.Header.BaseFee,
WithdrawalsHash: i.Header.WithdrawalsHash,
}

// Fill optional values.
Expand All @@ -153,7 +156,7 @@ func (i *bbInput) ToBlock() *types.Block {
if header.Difficulty != nil {
header.Difficulty = i.Header.Difficulty
}
return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers)
return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers).WithWithdrawals(i.Withdrawals)
}

// SealBlock seals the given block using the configured engine.
Expand Down Expand Up @@ -259,14 +262,15 @@ func BuildBlock(ctx *cli.Context) error {

func readInput(ctx *cli.Context) (*bbInput, error) {
var (
headerStr = ctx.String(InputHeaderFlag.Name)
ommersStr = ctx.String(InputOmmersFlag.Name)
txsStr = ctx.String(InputTxsRlpFlag.Name)
cliqueStr = ctx.String(SealCliqueFlag.Name)
ethashOn = ctx.Bool(SealEthashFlag.Name)
ethashDir = ctx.String(SealEthashDirFlag.Name)
ethashMode = ctx.String(SealEthashModeFlag.Name)
inputData = &bbInput{}
headerStr = ctx.String(InputHeaderFlag.Name)
ommersStr = ctx.String(InputOmmersFlag.Name)
withdrawalsStr = ctx.String(InputWithdrawalsFlag.Name)
txsStr = ctx.String(InputTxsRlpFlag.Name)
cliqueStr = ctx.String(SealCliqueFlag.Name)
ethashOn = ctx.Bool(SealEthashFlag.Name)
ethashDir = ctx.String(SealEthashDirFlag.Name)
ethashMode = ctx.String(SealEthashModeFlag.Name)
inputData = &bbInput{}
)
if ethashOn && cliqueStr != "" {
return nil, NewError(ErrorConfig, fmt.Errorf("both ethash and clique sealing specified, only one may be chosen"))
Expand Down Expand Up @@ -312,6 +316,13 @@ func readInput(ctx *cli.Context) (*bbInput, error) {
}
inputData.OmmersRlp = ommers
}
if withdrawalsStr != stdinSelector && withdrawalsStr != "" {
var withdrawals []*types.Withdrawal
if err := readFile(withdrawalsStr, "withdrawals", &withdrawals); err != nil {
return nil, err
}
inputData.Withdrawals = withdrawals
}
if txsStr != stdinSelector {
var txs string
if err := readFile(txsStr, "txs", &txs); err != nil {
Expand Down Expand Up @@ -351,15 +362,14 @@ func readInput(ctx *cli.Context) (*bbInput, error) {
// files
func dispatchBlock(ctx *cli.Context, baseDir string, block *types.Block) error {
raw, _ := rlp.EncodeToBytes(block)

type blockInfo struct {
Rlp hexutil.Bytes `json:"rlp"`
Hash common.Hash `json:"hash"`
}
var enc blockInfo
enc.Rlp = raw
enc.Hash = block.Hash()

enc := blockInfo{
Rlp: raw,
Hash: block.Hash(),
}
b, err := json.MarshalIndent(enc, "", " ")
if err != nil {
return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err))
Expand Down
32 changes: 22 additions & 10 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ type Prestate struct {
// ExecutionResult contains the execution status after running a state test, any
// error that might have occurred and a dump of the final state if requested.
type ExecutionResult struct {
StateRoot common.Hash `json:"stateRoot"`
TxRoot common.Hash `json:"txRoot"`
ReceiptRoot common.Hash `json:"receiptsRoot"`
LogsHash common.Hash `json:"logsHash"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Receipts types.Receipts `json:"receipts"`
Rejected []*rejectedTx `json:"rejected,omitempty"`
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
StateRoot common.Hash `json:"stateRoot"`
TxRoot common.Hash `json:"txRoot"`
ReceiptRoot common.Hash `json:"receiptsRoot"`
LogsHash common.Hash `json:"logsHash"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Receipts types.Receipts `json:"receipts"`
Rejected []*rejectedTx `json:"rejected,omitempty"`
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
WithdrawalsRoot *common.Hash `json:"withdrawalsRoot,omitempty"`
}

type ommer struct {
Expand All @@ -79,6 +80,7 @@ type stEnv struct {
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
Ommers []ommer `json:"ommers,omitempty"`
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
ParentUncleHash common.Hash `json:"parentUncleHash"`
}
Expand Down Expand Up @@ -254,6 +256,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}
statedb.AddBalance(pre.Env.Coinbase, minerReward)
}
// Apply withdrawals
for _, w := range pre.Env.Withdrawals {
// Amount is in gwei, turn into wei
amount := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), big.NewInt(params.GWei))
statedb.AddBalance(w.Address, amount)
}
// Commit block
root, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber))
if err != nil {
Expand All @@ -272,6 +280,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
GasUsed: (math.HexOrDecimal64)(gasUsed),
BaseFee: (*math.HexOrDecimal256)(vmContext.BaseFee),
}
if pre.Env.Withdrawals != nil {
h := types.DeriveSha(types.Withdrawals(pre.Env.Withdrawals), trie.NewStackTrie(nil))
execRs.WithdrawalsRoot = &h
}
return statedb, execRs, nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/evm/internal/t8ntool/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ var (
Name: "input.ommers",
Usage: "`stdin` or file name of where to find the list of ommer header RLPs to use.",
}
InputWithdrawalsFlag = &cli.StringFlag{
Name: "input.withdrawals",
Usage: "`stdin` or file name of where to find the list of withdrawals to use.",
}
InputTxsRlpFlag = &cli.StringFlag{
Name: "input.txs",
Usage: "`stdin` or file name of where to find the transactions list in RLP form.",
Expand Down
70 changes: 38 additions & 32 deletions cmd/evm/internal/t8ntool/gen_header.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions cmd/evm/internal/t8ntool/gen_stenv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ func Transition(ctx *cli.Context) error {
return NewError(ErrorConfig, errors.New("EIP-1559 config but missing 'currentBaseFee' in env section"))
}
}
if chainConfig.IsShanghai(prestate.Env.Number) && prestate.Env.Withdrawals == nil {
return NewError(ErrorConfig, errors.New("Shanghai config but missing 'withdrawals' in env section"))
}
isMerged := chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.BitLen() == 0
env := prestate.Env
if isMerged {
Expand Down
1 change: 1 addition & 0 deletions cmd/evm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ var blockBuilderCommand = &cli.Command{
t8ntool.OutputBlockFlag,
t8ntool.InputHeaderFlag,
t8ntool.InputOmmersFlag,
t8ntool.InputWithdrawalsFlag,
t8ntool.InputTxsRlpFlag,
t8ntool.SealCliqueFlag,
t8ntool.SealEthashFlag,
Expand Down
Loading