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

feat(op-node): update l1 gas price with fixed value #65

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
119 changes: 117 additions & 2 deletions op-node/chaincfg/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,115 @@ var Goerli = rollup.Config{
RegolithTime: u64Ptr(1679079600),
}

var OPBNBMainnet = rollup.Config{
Genesis: rollup.Genesis{
L1: eth.BlockID{
Hash: common.HexToHash("0x29443b21507894febe7700f7c5cd3569cc8bf1ba535df0489276d8004af81044"),
Number: 30758357,
},
L2: eth.BlockID{
Hash: common.HexToHash("0x4dd61178c8b0f01670c231597e7bcb368e84545acd46d940a896d6a791dd6df4"),
Number: 0,
},
L2Time: 1691753723,
SystemConfig: eth.SystemConfig{
BatcherAddr: common.HexToAddress("0xef8783382ef80ec23b66c43575a6103deca909c3"),
Overhead: eth.Bytes32(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000834")),
Scalar: eth.Bytes32(common.HexToHash("0x00000000000000000000000000000000000000000000000000000000000f4240")),
GasLimit: 100000000,
},
},
BlockTime: 1,
MaxSequencerDrift: 600,
SeqWindowSize: 14400,
ChannelTimeout: 1200,
L1ChainID: big.NewInt(56),
L2ChainID: big.NewInt(204),
BatchInboxAddress: common.HexToAddress("0xff00000000000000000000000000000000000204"),
DepositContractAddress: common.HexToAddress("0x1876ea7702c0ad0c6a2ae6036de7733edfbca519"),
L1SystemConfigAddress: common.HexToAddress("0x7ac836148c14c74086d57f7828f2d065672db3b8"),
RegolithTime: u64Ptr(0),
// TODO update block number
Fermat: nil,
}

var OPBNBTestnet = rollup.Config{
Genesis: rollup.Genesis{
L1: eth.BlockID{
Hash: common.HexToHash("0xc01a09840419cd993cf4666309f36e6d38de39771af8dbffecfa0386321c19f7"),
Number: 30727847,
},
L2: eth.BlockID{
Hash: common.HexToHash("0x51fa57729dfb1c27542c21b06cb72a0459c57440ceb43a465dae1307cd04fe80"),
Number: 0,
},
L2Time: 1686878506,
SystemConfig: eth.SystemConfig{
BatcherAddr: common.HexToAddress("0x1fd6a75cc72f39147756a663f3ef1fc95ef89495"),
Overhead: eth.Bytes32(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000834")),
Scalar: eth.Bytes32(common.HexToHash("0x00000000000000000000000000000000000000000000000000000000000f4240")),
GasLimit: 100000000,
},
},
BlockTime: 1,
MaxSequencerDrift: 600,
SeqWindowSize: 14400,
ChannelTimeout: 1200,
L1ChainID: big.NewInt(97),
L2ChainID: big.NewInt(5611),
BatchInboxAddress: common.HexToAddress("0xff00000000000000000000000000000000005611"),
DepositContractAddress: common.HexToAddress("0x4386c8abf2009ac0c263462da568dd9d46e52a31"),
L1SystemConfigAddress: common.HexToAddress("0x406ac857817708eaf4ca3a82317ef4ae3d1ea23b"),
RegolithTime: u64Ptr(0),
// TODO update block number
Fermat: nil,
}

var OPBNBDevnet = rollup.Config{
Genesis: rollup.Genesis{
L1: eth.BlockID{
Hash: common.HexToHash("0x29aee50ab3edefa64219e5c9b9c07f7d1953a98f2f4003d2c6fd93abeee4b706"),
Number: 2890195,
},
L2: eth.BlockID{
Hash: common.HexToHash("0x49d448b8dc98cc95e3968615ff3dbd904d9eec8252c5f52271f029896e6147ee"),
Number: 0,
},
L2Time: 1694166483,
SystemConfig: eth.SystemConfig{
BatcherAddr: common.HexToAddress("0x425a3598cb5e2d37213936e187914ea2059957ba"),
Overhead: eth.Bytes32(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000834")),
Scalar: eth.Bytes32(common.HexToHash("0x00000000000000000000000000000000000000000000000000000000000f4240")),
GasLimit: 100000000,
},
},
BlockTime: 1,
MaxSequencerDrift: 600,
SeqWindowSize: 14400,
ChannelTimeout: 1200,
L1ChainID: big.NewInt(797),
L2ChainID: big.NewInt(1320),
BatchInboxAddress: common.HexToAddress("0xff00000000000000000000000000000000000204"),
DepositContractAddress: common.HexToAddress("0xd93160096c5b65bb036b3269eb02328ddadb9856"),
L1SystemConfigAddress: common.HexToAddress("0xf053067cec8d8990de2ba9e17ec2f16c63c7bec4"),
RegolithTime: u64Ptr(0),
Fermat: big.NewInt(3615117),
}

var NetworksByName = map[string]rollup.Config{
"goerli": Goerli,
"mainnet": Mainnet,
"goerli": Goerli,
"mainnet": Mainnet,
"opBNBMainnet": OPBNBMainnet,
"opBNBTestnet": OPBNBTestnet,
"opBNBDevnet": OPBNBDevnet,
}

var NetworksByChainId = map[string]rollup.Config{
"420": Goerli,
"10": Mainnet,
"204": OPBNBMainnet,
"5611": OPBNBTestnet,
"1320": OPBNBDevnet,
}

var L2ChainIDToNetworkName = func() map[string]string {
Expand Down Expand Up @@ -100,6 +206,15 @@ func GetRollupConfig(name string) (rollup.Config, error) {
return network, nil
}

func GetRollupConfigByChainId(chainId string) (rollup.Config, error) {
network, ok := NetworksByChainId[chainId]
if !ok {
return rollup.Config{}, fmt.Errorf("no match pre-setting network chainId %s, use file config", chainId)
}

return network, nil
}

func u64Ptr(v uint64) *uint64 {
return &v
}
14 changes: 10 additions & 4 deletions op-node/rollup/derive/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package derive
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -58,11 +59,16 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
}

// Calculate bsc block base fee
_, transactions, err := ba.l1.InfoAndTxsByHash(ctx, epoch.Hash)
if err != nil {
return nil, NewTemporaryError(fmt.Errorf("failed to fetch L1 block info and txs: %w", err))
var l1BaseFee *big.Int
if ba.cfg.IsFermat(big.NewInt(int64(l2Parent.Number + 1))) {
l1BaseFee = bsc.BaseFeeByNetworks(ba.cfg.L2ChainID)
} else {
_, transactions, err := ba.l1.InfoAndTxsByHash(ctx, epoch.Hash)
if err != nil {
return nil, NewTemporaryError(fmt.Errorf("failed to fetch L1 block info and txs: %w", err))
}
l1BaseFee = bsc.BaseFeeByTransactions(transactions)
}
l1BaseFee := bsc.BaseFeeByTransactions(transactions)

// If the L1 origin changed this block, then we are in the first block of the epoch. In this
// case we need to fetch all transaction receipts from the L1 origin block so we can scan for
Expand Down
23 changes: 22 additions & 1 deletion op-node/rollup/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type Config struct {
DepositContractAddress common.Address `json:"deposit_contract_address"`
// L1 System Config Address
L1SystemConfigAddress common.Address `json:"l1_system_config_address"`

// OPBNB hard fork
// Fermat switch block (nil = no fork, 0 = already on Fermat)
Fermat *big.Int `json:"fermat,omitempty"`
}

// ValidateL1Config checks L1 config variables for errors.
Expand Down Expand Up @@ -256,6 +260,21 @@ func (c *Config) IsRegolith(timestamp uint64) bool {
return c.RegolithTime != nil && timestamp >= *c.RegolithTime
}

// IsFermat returns true if the Fermat hardfork is active at or past the given block.
func (c *Config) IsFermat(num *big.Int) bool {
return isBlockForked(c.Fermat, num)
}

// isBlockForked returns whether a fork scheduled at block s is active at the
// given head block. Whilst this method is the same as isTimestampForked, they
// are explicitly separate for clearer reading.
func isBlockForked(s, head *big.Int) bool {
if s == nil || head == nil {
return false
}
return s.Cmp(head) <= 0
}

// Description outputs a banner describing the important parts of rollup configuration in a human-readable form.
// Optionally provide a mapping of L2 chain IDs to network names to label the L2 chain with if not unknown.
// The config should be config.Check()-ed before creating a description.
Expand Down Expand Up @@ -283,6 +302,8 @@ func (c *Config) Description(l2Chains map[string]string) string {
// Report the upgrade configuration
banner += "Post-Bedrock Network Upgrades (timestamp based):\n"
banner += fmt.Sprintf(" - Regolith: %s\n", fmtForkTimeOrUnset(c.RegolithTime))
banner += "OPBNB hard forks (block based):\n"
banner += fmt.Sprintf(" - Fermat: #%-8v\n", c.Fermat)
return banner
}

Expand All @@ -305,7 +326,7 @@ func (c *Config) LogDescription(log log.Logger, l2Chains map[string]string) {
log.Info("Rollup Config", "l2_chain_id", c.L2ChainID, "l2_network", networkL2, "l1_chain_id", c.L1ChainID,
"l1_network", networkL1, "l2_start_time", c.Genesis.L2Time, "l2_block_hash", c.Genesis.L2.Hash.String(),
"l2_block_number", c.Genesis.L2.Number, "l1_block_hash", c.Genesis.L1.Hash.String(),
"l1_block_number", c.Genesis.L1.Number, "regolith_time", fmtForkTimeOrUnset(c.RegolithTime))
"l1_block_number", c.Genesis.L1.Number, "regolith_time", fmtForkTimeOrUnset(c.RegolithTime), "Fermat", c.Fermat)
}

func fmtForkTimeOrUnset(v *uint64) string {
Expand Down
9 changes: 8 additions & 1 deletion op-node/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ func NewRollupConfig(ctx *cli.Context) (*rollup.Config, error) {
if err := json.NewDecoder(file).Decode(&rollupConfig); err != nil {
return nil, fmt.Errorf("failed to decode rollup config: %w", err)
}
return &rollupConfig, nil
if rollupConfig.L2ChainID == nil {
return nil, fmt.Errorf("l2 chain ID must not be nil")
}
newRollupConfig, err := chaincfg.GetRollupConfigByChainId(rollupConfig.L2ChainID.String())
if err != nil {
return &rollupConfig, nil
}
return &newRollupConfig, nil
}

func NewSnapshotLogger(ctx *cli.Context) (log.Logger, error) {
Expand Down
17 changes: 14 additions & 3 deletions op-service/bsc/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/ethereum-optimism/optimism/op-node/eth"
)

var DefaultBaseFeeByTransactions = big.NewInt(3000000000)
var DefaultBaseFee = big.NewInt(3000000000)
var DefaultOPBNBTestnetBaseFee = big.NewInt(5000000000)
var OPBNBTestnet = big.NewInt(5611)

type BlockInfoBSCWrapper struct {
eth.BlockInfo
Expand All @@ -31,7 +33,7 @@ func (b *BlockInfoBSCWrapper) BaseFee() *big.Int {
}

// BaseFeeByTransactions calculates the average gas price of the non-zero-gas-price transactions in the block.
// If there is no non-zero-gas-price transaction in the block, it returns DefaultBaseFeeByTransactions.
// If there is no non-zero-gas-price transaction in the block, it returns DefaultBaseFee.
func BaseFeeByTransactions(transactions types.Transactions) *big.Int {
nonZeroTxsCnt := big.NewInt(0)
nonZeroTxsSum := big.NewInt(0)
Expand All @@ -43,11 +45,20 @@ func BaseFeeByTransactions(transactions types.Transactions) *big.Int {
}

if nonZeroTxsCnt.Cmp(big.NewInt(0)) == 0 {
return DefaultBaseFeeByTransactions
return DefaultBaseFee
}
return nonZeroTxsSum.Div(nonZeroTxsSum, nonZeroTxsCnt)
}

// BaseFeeByNetworks set l1 gas price by network.
func BaseFeeByNetworks(chainId *big.Int) *big.Int {
if chainId.Cmp(OPBNBTestnet) == 0 {
return DefaultOPBNBTestnetBaseFee
} else {
return DefaultBaseFee
}
}

func ToLegacyTx(dynTx *types.DynamicFeeTx) types.TxData {
return &types.LegacyTx{
Nonce: dynTx.Nonce,
Expand Down