Skip to content

Commit

Permalink
refactor astria configs into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-123 committed Jun 10, 2024
1 parent 2c84666 commit cdcc437
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 52 deletions.
5 changes: 3 additions & 2 deletions consensus/misc/eip1559/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
32 changes: 18 additions & 14 deletions grpc/execution/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions grpc/execution/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions grpc/execution/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions grpc/execution/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit cdcc437

Please sign in to comment.