Skip to content

Commit

Permalink
feat(core): introduce BasefeeSharingPctg in BlockMetadata (#287)
Browse files Browse the repository at this point in the history
* feat(core): introduce `BasefeeSharingPctg` in `BlockMetadata`

* feat: update message

* feat: check BasefeeSharingPctg
  • Loading branch information
davidtaikocha authored Jul 26, 2024
1 parent b137b2a commit e6487f0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 28 deletions.
38 changes: 18 additions & 20 deletions beacon/engine/gen_blockmetadata.go

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

7 changes: 3 additions & 4 deletions beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ type payloadAttributesMarshaling struct {
// CHANGE(taiko): BlockMetadata represents a `BlockMetadata` struct defined in
// protocol.
type BlockMetadata struct {
// Fields defined in `LibData.blockMetadata`.
Beneficiary common.Address `json:"beneficiary" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
Timestamp uint64 `json:"timestamp" gencodec:"required"`
MixHash common.Hash `json:"mixHash" gencodec:"required"`

// Extra fields required in taiko-geth.
TxList []byte `json:"txList" gencodec:"required"`
HighestBlockID *big.Int `json:"highestBlockID" gencodec:"required"`
ExtraData []byte `json:"extraData" gencodec:"required"`
TxList []byte `json:"txList" gencodec:"required"`
ExtraData []byte `json:"extraData" gencodec:"required"`
BasefeeSharingPctg uint8 `json:"basefeeSharingPctg"`
}

// CHANGE(taiko): JSON type overrides for BlockMetadata.
Expand Down
4 changes: 4 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ type StateDB struct {

// Testing hooks
onCommit func(states *triestate.Set) // Hook invoked when commit is performed

// CHANGE(taiko): basefeeSharingPctg of the basefee will be sent to the block.coinbase,
// the remaining will be sent to the treasury address.
BasefeeSharingPctg uint8
}

// New creates a new state from a given trie.
Expand Down
2 changes: 2 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
if err != nil {
return nil, err
}
// CHANGE(taiko): set `BasefeeSharingPctg` for the current message.
msg.BasefeeSharingPctg = statedb.BasefeeSharingPctg
// Create a new context to be used in the EVM environment
blockContext := NewEVMBlockContext(header, bc, author)
txContext := NewEVMTxContext(msg)
Expand Down
15 changes: 11 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type Message struct {

// CHANGE(taiko): whether the current transaction is the first TaikoL2.anchor transaction in a block.
IsAnchor bool
// CHANGE(taiko): basefeeSharingPctg of the basefee will be sent to the block.coinbase,
// the remaining will be sent to the treasury address.
BasefeeSharingPctg uint8
}

// TransactionToMessage converts a transaction into a Message.
Expand Down Expand Up @@ -467,12 +470,16 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
fee := new(uint256.Int).SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTipU256)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
// CHANGE(taiko): basefee is not burnt, but sent to a treasury instead.
// CHANGE(taiko): basefee is not burnt, but sent to a treasury and block.coinbase instead.
if st.evm.ChainConfig().Taiko && st.evm.Context.BaseFee != nil && !st.msg.IsAnchor {
st.state.AddBalance(
st.getTreasuryAddress(),
uint256.MustFromBig(new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed()))),
totalFee := new(big.Int).Mul(st.evm.Context.BaseFee, new(big.Int).SetUint64(st.gasUsed()))
feeCoinbase := new(big.Int).Div(
new(big.Int).Mul(totalFee, new(big.Int).SetUint64(uint64(st.msg.BasefeeSharingPctg))),
new(big.Int).SetUint64(100),
)
feeTreasury := new(big.Int).Sub(totalFee, feeCoinbase)
st.state.AddBalance(st.getTreasuryAddress(), uint256.MustFromBig(feeTreasury))
st.state.AddBalance(st.evm.Context.Coinbase, uint256.MustFromBig(feeCoinbase))
}
}

Expand Down
5 changes: 5 additions & 0 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
if payloadAttributes != nil {
// CHANGE(taiko): create a L2 block by Taiko protocol.
if isTaiko {
if payloadAttributes.BlockMetadata.BasefeeSharingPctg > 100 {
return valid(nil), engine.InvalidPayloadAttributes.With(
fmt.Errorf("invalid basefeeSharingPctg %d", payloadAttributes.BlockMetadata.BasefeeSharingPctg),
)
}
// No need to check payloadAttribute here, because all its fields are
// marked as required.
block, err := api.eth.Miner().SealBlockWith(
Expand Down
1 change: 1 addition & 0 deletions miner/taiko_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (w *worker) sealBlockWith(
defer env.discard()

env.header.GasLimit = blkMeta.GasLimit
env.state.BasefeeSharingPctg = blkMeta.BasefeeSharingPctg

// Commit transactions.
gasLimit := env.header.GasLimit
Expand Down

0 comments on commit e6487f0

Please sign in to comment.