Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing block fields to the type used for eth_getBlockByNumber and eth_getBlockByHash #185

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Comment on lines +137 to +156
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing the Transactions field type from interface{} to []interface{} to explicitly support both transaction hashes and full transaction objects.

-	Transactions     interface{}      `json:"transactions"`
+	Transactions     []interface{}    `json:"transactions"`

This modification ensures that the Transactions field can hold a slice of either transaction hashes or full transaction objects, enhancing flexibility and clarity.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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"`
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
Loading