From f2451261c948f2d57665b11528dee9f63b6787cd Mon Sep 17 00:00:00 2001 From: Bharath Date: Mon, 10 Jun 2024 22:03:57 +0530 Subject: [PATCH] refactor astria configs to a struct --- consensus/misc/eip1559/eip1559.go | 5 +- core/blockchain.go | 2 +- core/genesis.go | 2 +- grpc/execution/server.go | 32 +++++---- grpc/execution/server_test.go | 16 ++--- grpc/execution/test_utils.go | 14 ++-- grpc/execution/validation_test.go | 4 +- params/config.go | 114 +++++++++++++++++++++++++----- 8 files changed, 137 insertions(+), 52 deletions(-) diff --git a/consensus/misc/eip1559/eip1559.go b/consensus/misc/eip1559/eip1559.go index 8c50c52b0..540d5fbb8 100644 --- a/consensus/misc/eip1559/eip1559.go +++ b/consensus/misc/eip1559/eip1559.go @@ -91,8 +91,9 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { baseFee := num.Sub(parent.BaseFee, num) lowerBound := common.Big0 - if config.AstriaEIP1559Params != nil { - lowerBound = config.AstriaEIP1559Params.MinBaseFeeAt(parent.Number.Uint64() + 1) + astriaEip1559Params := config.AstriaEIP1559Params() + if astriaEip1559Params != nil { + lowerBound = astriaEip1559Params.MinBaseFeeAt(parent.Number.Uint64() + 1) } return math.BigMax(baseFee, lowerBound) diff --git a/core/blockchain.go b/core/blockchain.go index ca79738d6..780e24fb1 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -325,7 +325,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis bc.currentBlock.Store(bc.genesisBlock.Header()) bc.currentFinalBlock.Store(bc.genesisBlock.Header()) bc.currentSafeBlock.Store(bc.genesisBlock.Header()) - bc.currentBaseCelestiaHeight.Store(bc.Config().AstriaCelestiaInitialHeight) + bc.currentBaseCelestiaHeight.Store(bc.Config().AstriaCelestiaInitialHeight()) // Update chain info data metrics chainInfoGauge.Update(metrics.GaugeInfoValue{"chain_id": bc.chainConfig.ChainID.String()}) diff --git a/core/genesis.go b/core/genesis.go index 1ac4ce1fc..e95bc1086 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -428,7 +428,7 @@ func (g *Genesis) ToBlock() *types.Block { } extraData := g.ExtraData - if g.Config.AstriaOverrideGenesisExtraData { + if g.Config.AstriaOverrideGenesisExtraData() { extraData = g.Config.AstriaExtraData() } diff --git a/grpc/execution/server.go b/grpc/execution/server.go index 74ab0e88d..e08d53f55 100644 --- a/grpc/execution/server.go +++ b/grpc/execution/server.go @@ -76,29 +76,30 @@ var ( func NewExecutionServiceServerV1Alpha2(eth *eth.Ethereum) (*ExecutionServiceServerV1Alpha2, error) { bc := eth.BlockChain() - if bc.Config().AstriaRollupName == "" { + if bc.Config().AstriaRollupName() == "" { return nil, errors.New("rollup name not set") } - if bc.Config().AstriaSequencerInitialHeight == 0 { + if bc.Config().AstriaSequencerInitialHeight() == 0 { return nil, errors.New("sequencer initial height not set") } - if bc.Config().AstriaCelestiaInitialHeight == 0 { + if bc.Config().AstriaCelestiaInitialHeight() == 0 { return nil, errors.New("celestia initial height not set") } - if bc.Config().AstriaCelestiaHeightVariance == 0 { + if bc.Config().AstriaCelestiaHeightVariance() == 0 { return nil, errors.New("celestia height variance not set") } bridgeAddresses := make(map[string]*params.AstriaBridgeAddressConfig) bridgeAllowedAssetIDs := make(map[[32]byte]struct{}) - if bc.Config().AstriaBridgeAddressConfigs == nil { + astriaBridgeAddressConfig := bc.Config().AstriaBridgeAddressConfigs() + if astriaBridgeAddressConfig == nil { log.Warn("bridge addresses not set") } else { nativeBridgeSeen := false - for _, cfg := range bc.Config().AstriaBridgeAddressConfigs { + for _, cfg := range bc.Config().AstriaBridgeAddressConfigs() { err := cfg.Validate() if err != nil { return nil, fmt.Errorf("invalid bridge address config: %w", err) @@ -120,12 +121,13 @@ func NewExecutionServiceServerV1Alpha2(eth *eth.Ethereum) (*ExecutionServiceServ // To decrease compute cost, we identify the next fee recipient at the start // and update it as we execute blocks. nextFeeRecipient := common.Address{} - if bc.Config().AstriaFeeCollectors == nil { + astriaFeeCollectors := bc.Config().AstriaFeeCollectors() + if astriaFeeCollectors == nil { log.Warn("fee asset collectors not set, assets will be burned") } else { maxHeightCollectorMatch := uint32(0) nextBlock := uint32(bc.CurrentBlock().Number.Int64()) + 1 - for height, collector := range bc.Config().AstriaFeeCollectors { + for height, collector := range astriaFeeCollectors { if height <= nextBlock && height > maxHeightCollectorMatch { maxHeightCollectorMatch = height nextFeeRecipient = collector @@ -146,12 +148,12 @@ func (s *ExecutionServiceServerV1Alpha2) GetGenesisInfo(ctx context.Context, req log.Debug("GetGenesisInfo called") getGenesisInfoRequestCount.Inc(1) - rollupId := sha256.Sum256([]byte(s.bc.Config().AstriaRollupName)) + rollupId := sha256.Sum256([]byte(s.bc.Config().AstriaRollupName())) res := &astriaPb.GenesisInfo{ RollupId: rollupId[:], - SequencerGenesisBlockHeight: s.bc.Config().AstriaSequencerInitialHeight, - CelestiaBlockVariance: s.bc.Config().AstriaCelestiaHeightVariance, + SequencerGenesisBlockHeight: s.bc.Config().AstriaSequencerInitialHeight(), + CelestiaBlockVariance: s.bc.Config().AstriaCelestiaHeightVariance(), } log.Info("GetGenesisInfo completed", "response", res) @@ -285,10 +287,12 @@ func (s *ExecutionServiceServerV1Alpha2) ExecuteBlock(ctx context.Context, req * }, } - if next, ok := s.bc.Config().AstriaFeeCollectors[res.Number+1]; ok { - s.nextFeeRecipient = next + astriaFeeCollectors := s.bc.Config().AstriaFeeCollectors() + if astriaFeeCollectors != nil { + if next, ok := astriaFeeCollectors[res.Number+1]; ok { + s.nextFeeRecipient = next + } } - log.Info("ExecuteBlock completed", "block_num", res.Number, "timestamp", res.Timestamp) totalExecutedTxCount.Inc(int64(len(block.Transactions()))) executeBlockSuccessCount.Inc(1) diff --git a/grpc/execution/server_test.go b/grpc/execution/server_test.go index cdfc120c8..ec7ad78b5 100644 --- a/grpc/execution/server_test.go +++ b/grpc/execution/server_test.go @@ -27,11 +27,11 @@ func TestExecutionService_GetGenesisInfo(t *testing.T) { genesisInfo, err := serviceV1Alpha1.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{}) require.Nil(t, err, "GetGenesisInfo failed") - hashedRollupId := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaRollupName)) + hashedRollupId := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaRollupName())) require.True(t, bytes.Equal(genesisInfo.RollupId, hashedRollupId[:]), "RollupId is not correct") - require.Equal(t, genesisInfo.GetSequencerGenesisBlockHeight(), ethservice.BlockChain().Config().AstriaSequencerInitialHeight, "SequencerInitialHeight is not correct") - require.Equal(t, genesisInfo.GetCelestiaBlockVariance(), ethservice.BlockChain().Config().AstriaCelestiaHeightVariance, "CelestiaHeightVariance is not correct") + require.Equal(t, genesisInfo.GetSequencerGenesisBlockHeight(), ethservice.BlockChain().Config().AstriaSequencerInitialHeight(), "SequencerInitialHeight is not correct") + require.Equal(t, genesisInfo.GetCelestiaBlockVariance(), ethservice.BlockChain().Config().AstriaCelestiaHeightVariance(), "CelestiaHeightVariance is not correct") require.True(t, serviceV1Alpha1.genesisInfoCalled, "GetGenesisInfo should be called") } @@ -56,7 +56,7 @@ func TestExecutionServiceServerV1Alpha2_GetCommitmentState(t *testing.T) { require.True(t, bytes.Equal(commitmentState.Firm.Hash, firmBlock.Hash().Bytes()), "Firm Block Hashes do not match") require.True(t, bytes.Equal(commitmentState.Firm.ParentBlockHash, firmBlock.ParentHash.Bytes()), "Firm Block Parent Hash do not match") require.Equal(t, uint64(commitmentState.Firm.Number), firmBlock.Number.Uint64(), "Firm Block Number do not match") - require.Equal(t, commitmentState.BaseCelestiaHeight, ethservice.BlockChain().Config().AstriaCelestiaInitialHeight, "BaseCelestiaHeight is not correct") + require.Equal(t, commitmentState.BaseCelestiaHeight, ethservice.BlockChain().Config().AstriaCelestiaInitialHeight(), "BaseCelestiaHeight is not correct") require.True(t, serviceV1Alpha1.getCommitmentStateCalled, "GetCommitmentState should be called") } @@ -286,8 +286,8 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) { // create deposit tx if depositTxAmount is non zero if tt.depositTxAmount.Cmp(big.NewInt(0)) != 0 { depositAmount := bigIntToProtoU128(tt.depositTxAmount) - bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].BridgeAddress - bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].AssetDenom)) + bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].BridgeAddress + bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].AssetDenom)) // create new chain destination address for better testing chainDestinationAddressPrivKey, err := crypto.GenerateKey() @@ -373,8 +373,8 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testi amountToDeposit := big.NewInt(1000000000000000000) depositAmount := bigIntToProtoU128(amountToDeposit) - bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].BridgeAddress - bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].AssetDenom)) + bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].BridgeAddress + bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].AssetDenom)) // create new chain destination address for better testing chainDestinationAddressPrivKey, err := crypto.GenerateKey() diff --git a/grpc/execution/test_utils.go b/grpc/execution/test_utils.go index e5a9d7d0c..ea439a795 100644 --- a/grpc/execution/test_utils.go +++ b/grpc/execution/test_utils.go @@ -49,11 +49,11 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecd } bridgeAddress := crypto.PubkeyToAddress(bridgeAddressKey.PublicKey) - config.AstriaRollupName = "astria" - config.AstriaSequencerInitialHeight = 10 - config.AstriaCelestiaInitialHeight = 10 - config.AstriaCelestiaHeightVariance = 10 - config.AstriaBridgeAddressConfigs = []params.AstriaBridgeAddressConfig{ + config.AstriaChainConfig.RollupName = "astria" + config.AstriaChainConfig.SequencerInitialHeight = 10 + config.AstriaChainConfig.CelestiaInitialHeight = 10 + config.AstriaChainConfig.CelestiaHeightVariance = 10 + config.AstriaChainConfig.BridgeAddressConfigs = []params.AstriaBridgeAddressConfig{ { BridgeAddress: bridgeAddress.Bytes(), StartHeight: 2, @@ -71,7 +71,7 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecd astriaFeeCollectors := make(map[uint32]common.Address) astriaFeeCollectors[1] = feeCollector - config.AstriaFeeCollectors = astriaFeeCollectors + config.AstriaChainConfig.FeeCollectors = astriaFeeCollectors genesis := &core.Genesis{ Config: &config, @@ -129,7 +129,7 @@ func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*eth.Ethereu feeCollector := crypto.PubkeyToAddress(feeCollectorKey.PublicKey) require.Equal(t, feeCollector, serviceV1Alpha1.nextFeeRecipient, "nextFeeRecipient not set correctly") - bridgeAsset := sha256.Sum256([]byte(genesis.Config.AstriaBridgeAddressConfigs[0].AssetDenom)) + bridgeAsset := sha256.Sum256([]byte(genesis.Config.AstriaBridgeAddressConfigs()[0].AssetDenom)) _, ok := serviceV1Alpha1.bridgeAllowedAssetIDs[bridgeAsset] require.True(t, ok, "bridgeAllowedAssetIDs does not contain bridge asset id") diff --git a/grpc/execution/validation_test.go b/grpc/execution/validation_test.go index c9e247d8b..5939a523e 100644 --- a/grpc/execution/validation_test.go +++ b/grpc/execution/validation_test.go @@ -52,10 +52,10 @@ func TestSequenceTxValidation(t *testing.T) { require.Nil(t, err, "failed to generate chain destination key: %v", err) chainDestinationAddress := crypto.PubkeyToAddress(chainDestinationKey.PublicKey) - bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].AssetDenom)) + bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].AssetDenom)) invalidBridgeAssetDenom := sha256.Sum256([]byte("invalid-asset-denom")) - bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].BridgeAddress + bridgeAddress := ethservice.BlockChain().Config().AstriaBridgeAddressConfigs()[0].BridgeAddress tests := []struct { description string diff --git a/params/config.go b/params/config.go index ba163bcda..72feb2131 100644 --- a/params/config.go +++ b/params/config.go @@ -65,6 +65,7 @@ var ( ShanghaiTime: newUint64(1681338455), CancunTime: newUint64(1710338135), Ethash: new(EthashConfig), + AstriaChainConfig: &AstriaConfig{}, } // HoleskyChainConfig contains the chain parameters to run a node on the Holesky test network. HoleskyChainConfig = &ChainConfig{ @@ -90,6 +91,7 @@ var ( ShanghaiTime: newUint64(1696000704), CancunTime: newUint64(1707305664), Ethash: new(EthashConfig), + AstriaChainConfig: &AstriaConfig{}, } // SepoliaChainConfig contains the chain parameters to run a node on the Sepolia test network. SepoliaChainConfig = &ChainConfig{ @@ -115,6 +117,7 @@ var ( ShanghaiTime: newUint64(1677557088), CancunTime: newUint64(1706655072), Ethash: new(EthashConfig), + AstriaChainConfig: &AstriaConfig{}, } // GoerliChainConfig contains the chain parameters to run a node on the Görli test network. GoerliChainConfig = &ChainConfig{ @@ -141,6 +144,7 @@ var ( Period: 15, Epoch: 30000, }, + AstriaChainConfig: &AstriaConfig{}, } // AllEthashProtocolChanges contains every protocol change (EIPs) introduced // and accepted by the Ethereum core developers into the Ethash consensus. @@ -170,6 +174,7 @@ var ( TerminalTotalDifficultyPassed: true, Ethash: new(EthashConfig), Clique: nil, + AstriaChainConfig: &AstriaConfig{}, } AllDevChainProtocolChanges = &ChainConfig{ @@ -191,6 +196,7 @@ var ( CancunTime: newUint64(0), TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, + AstriaChainConfig: &AstriaConfig{}, } // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced @@ -221,6 +227,7 @@ var ( TerminalTotalDifficultyPassed: false, Ethash: nil, Clique: &CliqueConfig{Period: 0, Epoch: 30000}, + AstriaChainConfig: &AstriaConfig{}, } // TestChainConfig contains every protocol change (EIPs) introduced @@ -251,6 +258,7 @@ var ( TerminalTotalDifficultyPassed: false, Ethash: new(EthashConfig), Clique: nil, + AstriaChainConfig: &AstriaConfig{}, } // MergedTestChainConfig contains every protocol change (EIPs) introduced @@ -281,6 +289,7 @@ var ( TerminalTotalDifficultyPassed: true, Ethash: new(EthashConfig), Clique: nil, + AstriaChainConfig: &AstriaConfig{}, } // NonActivatedConfig defines the chain configuration without activating @@ -311,6 +320,7 @@ var ( TerminalTotalDifficultyPassed: false, Ethash: new(EthashConfig), Clique: nil, + AstriaChainConfig: &AstriaConfig{}, } TestRules = TestChainConfig.Rules(new(big.Int), false, 0) ) @@ -376,26 +386,94 @@ type ChainConfig struct { IsDevMode bool `json:"isDev,omitempty"` // Astria Specific Configuration - AstriaOverrideGenesisExtraData bool `json:"astriaOverrideGenesisExtraData,omitempty"` - AstriaExtraDataOverride hexutil.Bytes `json:"astriaExtraDataOverride,omitempty"` - AstriaRollupName string `json:"astriaRollupName"` - AstriaSequencerInitialHeight uint32 `json:"astriaSequencerInitialHeight"` - AstriaCelestiaInitialHeight uint64 `json:"astriaCelestiaInitialHeight"` - AstriaCelestiaHeightVariance uint64 `json:"astriaCelestiaHeightVariance,omitempty"` - AstriaBridgeAddressConfigs []AstriaBridgeAddressConfig `json:"astriaBridgeAddresses,omitempty"` - AstriaFeeCollectors map[uint32]common.Address `json:"astriaFeeCollectors"` - AstriaEIP1559Params *AstriaEIP1559Params `json:"astriaEIP1559Params,omitempty"` + AstriaChainConfig *AstriaConfig `json:"astriaChainConfig,omitempty"` +} + +type AstriaConfig struct { + OverrideGenesisExtraData bool `json:"overrideGenesisExtraData,omitempty"` + ExtraDataOverride hexutil.Bytes `json:"extraDataOverride,omitempty"` + RollupName string `json:"rollupName"` + SequencerInitialHeight uint32 `json:"sequencerInitialHeight"` + CelestiaInitialHeight uint64 `json:"celestiaInitialHeight"` + CelestiaHeightVariance uint64 `json:"celestiaHeightVariance,omitempty"` + BridgeAddressConfigs []AstriaBridgeAddressConfig `json:"bridgeAddresses,omitempty"` + FeeCollectors map[uint32]common.Address `json:"feeCollectors"` + EIP1559Params *AstriaEIP1559Params `json:"eip1559Params,omitempty"` +} + +func (c *ChainConfig) AstriaOverrideGenesisExtraData() bool { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.OverrideGenesisExtraData + } + return false +} + +func (c *ChainConfig) AstriaRollupName() string { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.RollupName + } + return "" +} + +func (c *ChainConfig) AstriaSequencerInitialHeight() uint32 { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.SequencerInitialHeight + } + return 0 +} + +func (c *ChainConfig) AstriaCelestiaInitialHeight() uint64 { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.CelestiaInitialHeight + } + return 0 +} + +func (c *ChainConfig) AstriaCelestiaHeightVariance() uint64 { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.CelestiaHeightVariance + } + return 0 +} + +func (c *ChainConfig) AstriaBridgeAddressConfigs() []AstriaBridgeAddressConfig { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.BridgeAddressConfigs + } + return nil +} + +func (c *ChainConfig) AstriaFeeCollectors() map[uint32]common.Address { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.FeeCollectors + } + return nil +} + +func (c *ChainConfig) AstriaEIP1559Params() *AstriaEIP1559Params { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.EIP1559Params + } + return nil +} + +func (c *ChainConfig) AstriaExtraDataOverride() hexutil.Bytes { + if c.AstriaChainConfig != nil { + return c.AstriaChainConfig.ExtraDataOverride + } + return nil } func (c *ChainConfig) AstriaExtraData() []byte { - if c.AstriaExtraDataOverride != nil { - return c.AstriaExtraDataOverride + astriaExtraDataOverride := c.AstriaExtraDataOverride() + if astriaExtraDataOverride != nil { + return astriaExtraDataOverride } // create default extradata extra, _ := rlp.EncodeToBytes([]interface{}{ - c.AstriaRollupName, - c.AstriaSequencerInitialHeight, + c.AstriaRollupName(), + c.AstriaSequencerInitialHeight(), }) if uint64(len(extra)) > MaximumExtraDataSize { log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", MaximumExtraDataSize) @@ -845,16 +923,18 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, headNumber *big.Int, // BaseFeeChangeDenominator bounds the amount the base fee can change between blocks. func (c *ChainConfig) BaseFeeChangeDenominator(height uint64) uint64 { - if c.AstriaEIP1559Params != nil { - return c.AstriaEIP1559Params.BaseFeeChangeDenominatorAt(height) + astriaEip1559Params := c.AstriaEIP1559Params() + if astriaEip1559Params != nil { + return astriaEip1559Params.BaseFeeChangeDenominatorAt(height) } return DefaultBaseFeeChangeDenominator } // ElasticityMultiplier bounds the maximum gas limit an EIP-1559 block may have. func (c *ChainConfig) ElasticityMultiplier(height uint64) uint64 { - if c.AstriaEIP1559Params != nil { - return c.AstriaEIP1559Params.ElasticityMultiplierAt(height) + astriaEip1559Params := c.AstriaEIP1559Params() + if astriaEip1559Params != nil { + return astriaEip1559Params.ElasticityMultiplierAt(height) } return DefaultElasticityMultiplier }