-
Notifications
You must be signed in to change notification settings - Fork 8
Feat: pass coinbase from consensus node #179
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
base: main
Are you sure you want to change the base?
Changes from all commits
35e253f
6fe0ee6
6039a12
d28602a
09af684
6f1d6b6
2fbc3c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,27 @@ package l2 | |
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/morph-l2/go-ethereum/contracts/morphtoken" | ||
"math/big" | ||
|
||
"github.com/morph-l2/go-ethereum/common" | ||
"github.com/morph-l2/go-ethereum/consensus" | ||
"github.com/morph-l2/go-ethereum/consensus/misc" | ||
"github.com/morph-l2/go-ethereum/contracts/l2staking" | ||
"github.com/morph-l2/go-ethereum/core" | ||
"github.com/morph-l2/go-ethereum/core/state" | ||
"github.com/morph-l2/go-ethereum/core/types" | ||
"github.com/morph-l2/go-ethereum/core/vm" | ||
"github.com/morph-l2/go-ethereum/params" | ||
"github.com/morph-l2/go-ethereum/rollup/rcfg" | ||
"github.com/morph-l2/go-ethereum/rpc" | ||
"github.com/morph-l2/go-ethereum/trie" | ||
) | ||
|
||
var ( | ||
l2Difficulty = common.Big0 // The default block difficulty in the l2 consensus | ||
l2Nonce = types.EncodeNonce(0) // The default block nonce in the l2 consensus | ||
l2Difficulty = common.Big0 // The default block difficulty in the l2 consensus | ||
l2Nonce = types.EncodeNonce(0) // The default block nonce in the l2 consensus | ||
rewardEpoch uint64 = 86400 | ||
) | ||
|
||
// Various error messages to mark blocks invalid. These should be private to | ||
|
@@ -29,9 +35,28 @@ var ( | |
errInvalidNonce = errors.New("invalid nonce") | ||
errInvalidUncleHash = errors.New("invalid uncle hash") | ||
errInvalidTimestamp = errors.New("invalid timestamp") | ||
errInvalidCoinbase = errors.New("invalid coinbase") | ||
) | ||
|
||
// chain context | ||
type chainContext struct { | ||
Chain consensus.ChainHeaderReader | ||
engine consensus.Engine | ||
} | ||
|
||
func (c chainContext) Engine() consensus.Engine { | ||
return c.engine | ||
} | ||
|
||
func (c chainContext) GetHeader(hash common.Hash, number uint64) *types.Header { | ||
return c.Chain.GetHeader(hash, number) | ||
} | ||
|
||
func (c chainContext) Config() *params.ChainConfig { | ||
return c.Chain.Config() | ||
} | ||
|
||
var _ = consensus.Engine(&Consensus{}) | ||
|
||
type Consensus struct { | ||
ethone consensus.Engine // Original consensus engine used in eth1, e.g. ethash or clique | ||
config *params.ChainConfig | ||
|
@@ -142,9 +167,6 @@ func (l2 *Consensus) verifyHeader(chain consensus.ChainHeaderReader, header, par | |
return errInvalidUncleHash | ||
} | ||
|
||
if l2.config.Morph.FeeVaultEnabled() && header.Coinbase != types.EmptyAddress { | ||
return errInvalidCoinbase | ||
} | ||
// Verify the timestamp | ||
if header.Time <= parent.Time { | ||
return errInvalidTimestamp | ||
|
@@ -187,9 +209,43 @@ func (l2 *Consensus) Prepare(chain consensus.ChainHeaderReader, header *types.He | |
header.Nonce = l2Nonce | ||
header.UncleHash = types.EmptyUncleHash | ||
header.Extra = []byte{} // disable extra field filling with bytes | ||
// set coinbase to empty address, if feeVault is enabled | ||
if l2.config.Morph.FeeVaultEnabled() { | ||
header.Coinbase = types.EmptyAddress | ||
return nil | ||
} | ||
|
||
// StartHook implements calling before start apply transactions of block | ||
func (l2 *Consensus) StartHook(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) error { | ||
rewardStarted := state.GetState(rcfg.L2StakingAddress, rcfg.RewardStartedSlot).Big() | ||
if rewardStarted.Cmp(common.Big1) != 0 { | ||
return nil | ||
} | ||
inflationMintedEpochs := state.GetState(rcfg.MorphTokenAddress, rcfg.InflationMintedEpochsSolt).Big().Uint64() | ||
rewardStartTime := state.GetState(rcfg.L2StakingAddress, rcfg.RewardStartTimeSlot).Big().Uint64() | ||
parentHeader := chain.GetHeaderByHash(header.ParentHash) | ||
if parentHeader == nil { | ||
return consensus.ErrUnknownAncestor | ||
} | ||
cx := chainContext{Chain: chain, engine: l2.ethone} | ||
blockContext := core.NewEVMBlockContext(header, cx, l2.config, nil) | ||
Comment on lines
+223
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
-parentHeader := chain.GetHeaderByHash(header.ParentHash)
+parentHeader := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) If the richer interface is really required, assert the type first: type headerByHash interface {
GetHeaderByHash(hash common.Hash) *types.Header
}
var parentHeader *types.Header
if r, ok := chain.(headerByHash); ok {
parentHeader = r.GetHeaderByHash(header.ParentHash)
} else {
parentHeader = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
} |
||
// TODO tracer | ||
evm := vm.NewEVM(blockContext, vm.TxContext{}, state, l2.config, vm.Config{Tracer: nil}) | ||
stakingCallData, err := l2staking.PacketData(parentHeader.Coinbase) | ||
if err != nil { | ||
return err | ||
} | ||
systemAddress := vm.AccountRef(rcfg.SystemAddress) | ||
_, _, err = evm.Call(systemAddress, rcfg.L2StakingAddress, stakingCallData, params.MaxGasLimit, common.Big0) | ||
if err != nil { | ||
return err | ||
} | ||
if header.Time > rewardStartTime && (header.Time-rewardStartTime)/rewardEpoch > inflationMintedEpochs { | ||
callData, err := morphtoken.PacketData() | ||
if err != nil { | ||
return err | ||
} | ||
_, _, err = evm.Call(systemAddress, rcfg.MorphTokenAddress, callData, params.MaxGasLimit, common.Big0) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package l2staking | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/morph-l2/go-ethereum/accounts/abi" | ||
"github.com/morph-l2/go-ethereum/common" | ||
) | ||
|
||
const jsonData = `[{"inputs":[{"internalType":"address","name":"sequencerAddr","type":"address"}],"name":"recordBlocks","outputs":[],"stateMutability":"nonpayable","type":"function"}]` | ||
|
||
var ( | ||
l2StakingABI *abi.ABI | ||
loadOnce sync.Once | ||
loadErr error | ||
) | ||
|
||
func Abi() (*abi.ABI, error) { | ||
loadOnce.Do(func() { | ||
stakingABI, err := abi.JSON(strings.NewReader(jsonData)) | ||
if err != nil { | ||
loadErr = fmt.Errorf("failed to parse ABI: %w", err) | ||
return | ||
} | ||
l2StakingABI = &stakingABI | ||
}) | ||
return l2StakingABI, loadErr | ||
} | ||
|
||
func PacketData(addr common.Address) ([]byte, error) { | ||
a, err := Abi() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get ABI: %w", err) | ||
} | ||
data, err := a.Pack("recordBlocks", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to pack data: %w", err) | ||
} | ||
return data, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package l2staking | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/morph-l2/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPackData(t *testing.T) { | ||
_, err := PacketData(common.HexToAddress("0x01")) | ||
require.NoError(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package morphtoken | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/morph-l2/go-ethereum/accounts/abi" | ||
) | ||
|
||
const jsonData = `[{"inputs":[],"name":"mintInflations","outputs":[],"stateMutability":"nonpayable","type":"function"}]` | ||
|
||
var ( | ||
morphTokenABI *abi.ABI | ||
loadOnce sync.Once | ||
loadErr error | ||
) | ||
|
||
func Abi() (*abi.ABI, error) { | ||
loadOnce.Do(func() { | ||
tokenABI, err := abi.JSON(strings.NewReader(jsonData)) | ||
if err != nil { | ||
loadErr = fmt.Errorf("failed to parse ABI: %w", err) | ||
return | ||
} | ||
morphTokenABI = &tokenABI | ||
}) | ||
return morphTokenABI, loadErr | ||
} | ||
|
||
func PacketData() ([]byte, error) { | ||
a, err := Abi() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get ABI: %w", err) | ||
} | ||
data, err := a.Pack("mintInflations") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to pack data: %w", err) | ||
} | ||
return data, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package morphtoken | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPackData(t *testing.T) { | ||
_, err := PacketData() | ||
require.NoError(t, err) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
GetHeaderByNumber
breaks thecore.ChainContext
contractcore.NewEVMBlockContext
expects the argument to satisfycore.ChainContext
, which in Geth also requiresGetHeaderByNumber(uint64) *types.Header
.The current
chainContext
implementsEngine
andGetHeader
but omits this method, so the code will not compile.📝 Committable suggestion