diff --git a/api/api.go b/api/api.go index f3b3b9c71..3a63d118f 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,44 @@ 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 + } + if len(transactions) > 0 { + totalGasUsed := hexutil.Uint64(0) + for _, tx := range transactions { + totalGasUsed += tx.Gas + } + blockResponse.GasUsed = totalGasUsed + } + + if fullTx { + blockResponse.Transactions = transactions + } + + return blockResponse, nil +} diff --git a/api/models.go b/api/models.go index 01de91f17..446214b60 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"` } diff --git a/config/config.go b/config/config.go index c175d92e1..7802b399b 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 == "" {