From 22afd7cdb7302aa50f5b97ebfe82ba15e6d06df8 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 4 Apr 2024 13:17:29 +0300 Subject: [PATCH 1/3] Add missing block fields to the type used for eth_getBlockByNumber and eth_getBlockByHash --- api/api.go | 89 ++++++++++++++++++++++++--------------------------- api/models.go | 26 +++++++++++---- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/api/api.go b/api/api.go index f3b3b9c7..b94da3c7 100644 --- a/api/api.go +++ b/api/api.go @@ -277,34 +277,12 @@ func (b *BlockChainAPI) GetBlockByHash( hash common.Hash, fullTx bool, ) (*Block, error) { - bl, err := b.blocks.GetByID(hash) + block, err := b.blocks.GetByID(hash) if err != nil { return handleError[*Block](b.logger, err) } - h, err := bl.Hash() - if err != nil { - b.logger.Error().Err(err).Msg("failed to calculate hash for block by hash") - return nil, errs.ErrInternal - } - - block := &Block{ - Hash: h, - Number: hexutil.Uint64(bl.Height), - ParentHash: bl.ParentBlockHash, - ReceiptsRoot: bl.ReceiptRoot, - Transactions: bl.TransactionHashes, - } - - if fullTx { - transactions, err := b.fetchBlockTransactions(ctx, bl) - if err != nil { - return nil, err - } - block.Transactions = transactions - } - - return block, nil + return b.prepareBlockResponse(ctx, block, fullTx) } // GetBlockByNumber returns the requested canonical block. @@ -329,34 +307,12 @@ func (b *BlockChainAPI) GetBlockByNumber( } } - bl, err := b.blocks.GetByHeight(height) + block, err := b.blocks.GetByHeight(height) if err != nil { return handleError[*Block](b.logger, err) } - h, err := bl.Hash() - if err != nil { - b.logger.Error().Err(err).Msg("failed to calculate hash for block by number") - return nil, errs.ErrInternal - } - - block := &Block{ - Hash: h, - Number: hexutil.Uint64(bl.Height), - ParentHash: bl.ParentBlockHash, - ReceiptsRoot: bl.ReceiptRoot, - Transactions: bl.TransactionHashes, - } - - if fullTx { - transactions, err := b.fetchBlockTransactions(ctx, bl) - if err != nil { - return nil, err - } - block.Transactions = transactions - } - - return block, nil + return b.prepareBlockResponse(ctx, block, fullTx) } // GetBlockReceipts returns the block receipts for the given block hash or number or tag. @@ -791,3 +747,40 @@ func (b *BlockChainAPI) fetchBlockTransactions( return transactions, nil } + +func (b *BlockChainAPI) prepareBlockResponse( + ctx context.Context, + block *evmTypes.Block, + fullTx bool, +) (*Block, error) { + h, err := block.Hash() + if err != nil { + b.logger.Error().Err(err).Msg("failed to calculate hash for block by number") + return nil, errs.ErrInternal + } + + blockResponse := &Block{ + Hash: h, + Number: hexutil.Uint64(block.Height), + ParentHash: block.ParentBlockHash, + ReceiptsRoot: block.ReceiptRoot, + Transactions: block.TransactionHashes, + Uncles: []common.Hash{}, + GasLimit: hexutil.Uint64(15_000_000), + Nonce: types.BlockNonce{0x1}, + } + + transactions, err := b.fetchBlockTransactions(ctx, block) + if err != nil { + return nil, err + } + for _, tx := range transactions { + blockResponse.GasUsed += tx.Gas + } + + if fullTx { + blockResponse.Transactions = transactions + } + + return blockResponse, nil +} diff --git a/api/models.go b/api/models.go index 01de91f1..446214b6 100644 --- a/api/models.go +++ b/api/models.go @@ -134,10 +134,24 @@ type BlockOverrides struct { } type Block struct { - Hash common.Hash `json:"hash"` - Number hexutil.Uint64 `json:"number"` - ParentHash common.Hash `json:"parentHash"` - ReceiptsRoot common.Hash `json:"receiptsRoot"` - Transactions interface{} `json:"transactions"` - // todo add more fields needed + Number hexutil.Uint64 `json:"number"` + Hash common.Hash `json:"hash"` + ParentHash common.Hash `json:"parentHash"` + Nonce types.BlockNonce `json:"nonce"` + Sha3Uncles common.Hash `json:"sha3Uncles"` + LogsBloom hexutil.Bytes `json:"logsBloom"` + TransactionsRoot common.Hash `json:"transactionsRoot"` + StateRoot common.Hash `json:"stateRoot"` + ReceiptsRoot common.Hash `json:"receiptsRoot"` + Miner common.Address `json:"miner"` + Difficulty hexutil.Uint64 `json:"difficulty"` + TotalDifficulty hexutil.Uint64 `json:"totalDifficulty"` + ExtraData hexutil.Bytes `json:"extraData"` + Size hexutil.Uint64 `json:"size"` + GasLimit hexutil.Uint64 `json:"gasLimit"` + GasUsed hexutil.Uint64 `json:"gasUsed"` + Timestamp hexutil.Uint64 `json:"timestamp"` + Transactions interface{} `json:"transactions"` + Uncles []common.Hash `json:"uncles"` + MixHash common.Hash `json:"mixHash"` } From b1f8e8e00a1086830f450e41b3b0806288397ce0 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 4 Apr 2024 13:48:38 +0300 Subject: [PATCH 2/3] Refactor calculation for totalGasUsed --- api/api.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/api.go b/api/api.go index b94da3c7..3a63d118 100644 --- a/api/api.go +++ b/api/api.go @@ -774,8 +774,12 @@ func (b *BlockChainAPI) prepareBlockResponse( if err != nil { return nil, err } - for _, tx := range transactions { - blockResponse.GasUsed += tx.Gas + if len(transactions) > 0 { + totalGasUsed := hexutil.Uint64(0) + for _, tx := range transactions { + totalGasUsed += tx.Gas + } + blockResponse.GasUsed = totalGasUsed } if fullTx { From 4ae5ad6ebcbc2eaad65c86c428f447c28e31988b Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Thu, 4 Apr 2024 13:49:06 +0300 Subject: [PATCH 3/3] Fix unit in default value for filterExpiry config flag --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index c175d92e..7802b399 100644 --- a/config/config.go +++ b/config/config.go @@ -81,7 +81,7 @@ func FromFlags() (*Config, error) { flag.StringVar(&keysPath, "coa-key-file", "", "File path that contains JSON array of COA keys used in key-rotation mechanism, this is exclusive with coa-key flag.") flag.BoolVar(&cfg.CreateCOAResource, "coa-resource-create", false, "Auto-create the COA resource in the Flow COA account provided if one doesn't exist") flag.StringVar(&logLevel, "log-level", "debug", "Define verbosity of the log output ('debug', 'info', 'error')") - flag.StringVar(&filterExpiry, "filter-expiry", "5min", "Filter defines the time it takes for an idle filter to expire") + flag.StringVar(&filterExpiry, "filter-expiry", "5m", "Filter defines the time it takes for an idle filter to expire") flag.Parse() if coinbase == "" {