Skip to content

Commit

Permalink
feat(all): changes based on protocol's major upgrade (ethereum#53)
Browse files Browse the repository at this point in the history
* feat: update Genesis JSONs

* feat: update Genesis JSONs

* chore: update workflow

* feat(consensus): activate withdrawals

* chore: update genesis configs

* feat: update taiko_worker

* feat: send baseFee to treasure address

* feat: skip anchor baseFee checks
  • Loading branch information
davidtaikocha authored Apr 24, 2023
1 parent e3a19b9 commit 0a3a6bb
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 19 deletions.
18 changes: 15 additions & 3 deletions consensus/taiko/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ func (t *Taiko) Finalize(chain consensus.ChainHeaderReader, header *types.Header
header.Root = state.IntermediateRoot(true)
header.UncleHash = types.CalcUncleHash(nil)
header.Difficulty = common.Big0
header.WithdrawalsHash = nil
// Withdrawals processing.
for _, w := range withdrawals {
// Convert amount from gwei to wei.
amount := new(big.Int).SetUint64(w.Amount)
amount = amount.Mul(amount, big.NewInt(params.GWei))
state.AddBalance(w.Address, amount)
}
}

// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
Expand All @@ -215,9 +221,15 @@ func (t *Taiko) Finalize(chain consensus.ChainHeaderReader, header *types.Header
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
func (t *Taiko) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
if withdrawals == nil {
withdrawals = make([]*types.Withdrawal, 0)
}

// Finalize block
t.Finalize(chain, header, state, txs, uncles, nil)
return types.NewBlock(header, txs, nil /* ignore uncles */, receipts, trie.NewStackTrie(nil)), nil
t.Finalize(chain, header, state, txs, uncles, withdrawals)
return types.NewBlockWithWithdrawals(
header, txs, nil /* ignore uncles */, receipts, withdrawals, trie.NewStackTrie(nil),
), nil
}

// Seal generates a new sealing request for the given input block and pushes
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti
b.SetCoinbase(common.Address{})
}
b.statedb.SetTxContext(tx.Hash(), len(b.txs))
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vmConfig)
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vmConfig, false)
if err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, sta
// and uses the input parameters for its environment. It returns the receipt
// 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) (*types.Receipt, error) {
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)
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
17 changes: 16 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ type Message struct {
// account nonce in state. It also disables checking that the sender is an EOA.
// 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
}

// TransactionToMessage converts a transaction into a Message.
Expand Down Expand Up @@ -234,6 +237,11 @@ func (st *StateTransition) buyGas() error {
balanceCheck = balanceCheck.Mul(balanceCheck, st.msg.GasFeeCap)
balanceCheck.Add(balanceCheck, st.msg.Value)
}
// CHANGE(taiko): skip balance check for TaikoL2.anchor transaction.
if st.msg.IsAnchor {
balanceCheck = common.Big0
mgval = common.Big0
}
if have, want := st.state.GetBalance(st.msg.From), balanceCheck; have.Cmp(want) < 0 {
return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want)
}
Expand Down Expand Up @@ -274,7 +282,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 {
if (!st.evm.Config.NoBaseFee || msg.GasFeeCap.BitLen() > 0 || msg.GasTipCap.BitLen() > 0) && !msg.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 @@ -395,6 +403,13 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
fee := new(big.Int).SetUint64(st.gasUsed())
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 {
st.state.AddBalance(
st.evm.ChainConfig().Treasure,
new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed())),
)
}
}

return &ExecutionResult{
Expand Down
4 changes: 3 additions & 1 deletion core/taiko_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TaikoGenesisBlock(networkID uint64) *Genesis {
switch networkID {
case params.TaikoInternal1NetworkID.Uint64():
chainConfig.ChainID = params.TaikoInternal1NetworkID
chainConfig.Treasure = common.HexToAddress("0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f")
allocJSON = taikoGenesis.Internal1GenesisAllocJSON
case params.TaikoInternal2NetworkID.Uint64():
chainConfig.ChainID = params.TaikoInternal2NetworkID
chainConfig.Treasure = common.HexToAddress("0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f")
allocJSON = taikoGenesis.Internal2GenesisAllocJSON
case params.SnæfellsjökullNetworkID.Uint64():
chainConfig.ChainID = params.SnæfellsjökullNetworkID
Expand All @@ -43,7 +45,7 @@ func TaikoGenesisBlock(networkID uint64) *Genesis {
GasLimit: uint64(6000000),
Difficulty: common.Big0,
Alloc: alloc,
GasUsed: uint64(6000000),
GasUsed: 0,
BaseFee: new(big.Int).SetUint64(10000000),
}
}
Loading

0 comments on commit 0a3a6bb

Please sign in to comment.