Skip to content

Commit

Permalink
Merge pull request #185 from m-Peter/add-missing-block-fields
Browse files Browse the repository at this point in the history
Add missing block fields to the type used for `eth_getBlockByNumber` and `eth_getBlockByHash`
  • Loading branch information
sideninja authored Apr 4, 2024
2 parents 6e398ab + 929969f commit fd9c284
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 55 deletions.
93 changes: 45 additions & 48 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
26 changes: 20 additions & 6 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down

0 comments on commit fd9c284

Please sign in to comment.