Skip to content

Commit

Permalink
feat(core): skip baseFee check for anchor transaction (ethereum#54)
Browse files Browse the repository at this point in the history
* test: fix testing errors

* feat: update baseFee management

* feat: update genesis jsons

* chore: update workflow
  • Loading branch information
davidtaikocha authored Apr 25, 2023
1 parent 0a3a6bb commit c36ae0f
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}

for i, tx := range txs {
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee)
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee, i == 0)
if err != nil {
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
Expand Down
2 changes: 1 addition & 1 deletion core/state_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
return
}
// Convert the transaction into an executable message and pre-cache its sender
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
msg, err := TransactionToMessage(tx, signer, header.BaseFee, i == 0)
if err != nil {
return // Also invalid block, bail out
}
Expand Down
6 changes: 2 additions & 4 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
msg, err := TransactionToMessage(tx, types.MakeSigner(p.config, header.Number), header.BaseFee)
msg, err := TransactionToMessage(tx, types.MakeSigner(p.config, header.Number), header.BaseFee, i == 0)
if err != nil {
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
Expand Down Expand Up @@ -147,12 +147,10 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config, isAnchor bool) (*types.Receipt, error) {
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee, isAnchor)
if err != nil {
return nil, err
}
// CHANGE(taiko): mark if this transaction is TaikoL2.anchor
msg.IsAnchor = isAnchor
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
Expand Down
19 changes: 13 additions & 6 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ type Message struct {
// This field will be set to true for operations like RPC eth_call.
SkipAccountChecks bool

// CHANGE(taiko): Whteher the current transaction is TaikoL2.anchor.
IsAnchor bool
// CHANGE(taiko): Whteher the current transaction is the first transaction in a block.
IsFirstTx bool
}

// TransactionToMessage converts a transaction into a Message.
func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.Int) (*Message, error) {
func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.Int, isFirstTx bool) (*Message, error) {
msg := &Message{
Nonce: tx.Nonce(),
GasLimit: tx.Gas(),
Expand All @@ -158,6 +158,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
Data: tx.Data(),
AccessList: tx.AccessList(),
SkipAccountChecks: false,
IsFirstTx: isFirstTx,
}
// If baseFee provided, set gasPrice to effectiveGasPrice.
if baseFee != nil {
Expand Down Expand Up @@ -238,7 +239,7 @@ func (st *StateTransition) buyGas() error {
balanceCheck.Add(balanceCheck, st.msg.Value)
}
// CHANGE(taiko): skip balance check for TaikoL2.anchor transaction.
if st.msg.IsAnchor {
if st.isAnchor() {
balanceCheck = common.Big0
mgval = common.Big0
}
Expand Down Expand Up @@ -282,7 +283,7 @@ func (st *StateTransition) preCheck() error {
// Make sure that transaction gasFeeCap is greater than the baseFee (post london)
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call)
if (!st.evm.Config.NoBaseFee || msg.GasFeeCap.BitLen() > 0 || msg.GasTipCap.BitLen() > 0) && !msg.IsAnchor {
if (!st.evm.Config.NoBaseFee || msg.GasFeeCap.BitLen() > 0 || msg.GasTipCap.BitLen() > 0) && !st.isAnchor() {
if l := msg.GasFeeCap.BitLen(); l > 256 {
return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh,
msg.From.Hex(), l)
Expand Down Expand Up @@ -404,7 +405,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
fee.Mul(fee, effectiveTip)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
// CHANGE(taiko): basefee is not burnt, but sent to a treasure instead.
if st.evm.ChainConfig().Taiko {
if st.evm.ChainConfig().Taiko && st.evm.Context.BaseFee != nil {
st.state.AddBalance(
st.evm.ChainConfig().Treasure,
new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed())),
Expand Down Expand Up @@ -440,3 +441,9 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gasRemaining
}

func (st *StateTransition) isAnchor() bool {
return st.evm.ChainConfig().Taiko &&
st.msg.IsFirstTx &&
st.msg.From == common.HexToAddress("0x0000777735367b36bC9B61C50022d9D0700dB4Ec")
}
11 changes: 4 additions & 7 deletions core/taiko_genesis/internal-1.json

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions core/taiko_genesis/internal-2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
signer := types.MakeSigner(eth.blockchain.Config(), block.Number())
for idx, tx := range block.Transactions() {
// Assemble the transaction call message and return if the requested offset
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), idx == 0)
txContext := core.NewEVMTxContext(msg)
context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil)
if idx == txIndex {
Expand Down
12 changes: 6 additions & 6 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed
)
// Trace all the transactions contained within
for i, tx := range task.block.Transactions() {
msg, _ := core.TransactionToMessage(tx, signer, task.block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, task.block.BaseFee(), i == 0)
txctx := &Context{
BlockHash: task.block.Hash(),
BlockNumber: task.block.Number(),
Expand Down Expand Up @@ -554,7 +554,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
return nil, err
}
var (
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee(), i == 0)
txContext = core.NewEVMTxContext(msg)
vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{})
)
Expand Down Expand Up @@ -628,7 +628,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
)
for i, tx := range txs {
// Generate the next state snapshot fast without tracing
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), i == 0)
txctx := &Context{
BlockHash: blockHash,
BlockNumber: block.Number(),
Expand Down Expand Up @@ -671,7 +671,7 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat
defer pend.Done()
// Fetch and execute the next transaction trace tasks
for task := range jobs {
msg, _ := core.TransactionToMessage(txs[task.index], signer, block.BaseFee())
msg, _ := core.TransactionToMessage(txs[task.index], signer, block.BaseFee(), task.index == 0)
txctx := &Context{
BlockHash: blockHash,
BlockNumber: block.Number(),
Expand Down Expand Up @@ -702,7 +702,7 @@ txloop:
}

// Generate the next state snapshot fast without tracing
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), i == 0)
statedb.SetTxContext(tx.Hash(), i)
vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{})
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil {
Expand Down Expand Up @@ -782,7 +782,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
for i, tx := range block.Transactions() {
// Prepare the transaction for un-traced execution
var (
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee(), i == 0)
txContext = core.NewEVMTxContext(msg)
vmConf vm.Config
dump *os.File
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b *testBackend) StateAtTransaction(ctx context.Context, block *types.Block
// Recompute transactions up to the target index.
signer := types.MakeSigner(b.chainConfig, block.Number())
for idx, tx := range block.Transactions() {
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), idx == 0)
txContext := core.NewEVMTxContext(msg)
context := core.NewEVMBlockContext(block.Header(), b.chain, nil)
if idx == txIndex {
Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/internal/tracetest/calltrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
t.Fatalf("failed to create call tracer: %v", err)
}
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})
msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
b.Fatalf("failed to parse testcase input: %v", err)
}
signer := types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)))
msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
b.Fatalf("failed to prepare transaction for tracing: %v", err)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestZeroValueToNotExitCall(t *testing.T) {
t.Fatalf("failed to create call tracer: %v", err)
}
evm := vm.NewEVM(context, txContext, statedb, params.MainnetChainConfig, vm.Config{Debug: true, Tracer: tracer})
msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/internal/tracetest/flat_calltrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func flatCallTracerTestRunner(tracerName string, filename string, dirPath string
}
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})

msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
return fmt.Errorf("failed to prepare transaction for tracing: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/internal/tracetest/prestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
t.Fatalf("failed to create call tracer: %v", err)
}
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})
msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
t.Fatalf("failed to prepare transaction for tracing: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func BenchmarkTransactionTrace(b *testing.B) {
//EnableReturnData: false,
})
evm := vm.NewEVM(context, txContext, statedb, params.AllEthashProtocolChanges, vm.Config{Debug: true, Tracer: tracer})
msg, err := core.TransactionToMessage(tx, signer, nil)
msg, err := core.TransactionToMessage(tx, signer, nil, false)
if err != nil {
b.Fatalf("failed to prepare transaction for tracing: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion les/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.
signer := types.MakeSigner(leth.blockchain.Config(), block.Number())
for idx, tx := range block.Transactions() {
// Assemble the transaction call message and return if the requested offset
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), idx == 0)
txContext := core.NewEVMTxContext(msg)
context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
statedb.SetTxContext(tx.Hash(), idx)
Expand Down

0 comments on commit c36ae0f

Please sign in to comment.