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

Reuse block preparation logic in streaming API #617

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 6 additions & 17 deletions api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package api
import (
"context"
"fmt"
"math/big"

"github.com/onflow/go-ethereum/common"
"github.com/onflow/go-ethereum/common/hexutil"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/eth/filters"
"github.com/onflow/go-ethereum/rpc"
Expand All @@ -20,6 +17,7 @@ import (

type StreamAPI struct {
logger zerolog.Logger
api *BlockChainAPI
config *config.Config
blocks storage.BlockIndexer
transactions storage.TransactionIndexer
Expand All @@ -32,6 +30,7 @@ type StreamAPI struct {
func NewStreamAPI(
logger zerolog.Logger,
config *config.Config,
api *BlockChainAPI,
blocks storage.BlockIndexer,
transactions storage.TransactionIndexer,
receipts storage.ReceiptIndexer,
Expand All @@ -42,6 +41,7 @@ func NewStreamAPI(
return &StreamAPI{
logger: logger,
config: config,
api: api,
blocks: blocks,
transactions: transactions,
receipts: receipts,
Expand All @@ -59,23 +59,12 @@ func (s *StreamAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
s.blocksPublisher,
func(notifier *rpc.Notifier, sub *rpc.Subscription) func(block *models.Block) error {
return func(block *models.Block) error {
h, err := block.Hash()
response, err := s.api.prepareBlockResponse(block, false)
if err != nil {
return err
return fmt.Errorf("failed to get block response: %w", err)
}

return notifier.Notify(sub.ID, &Block{
Hash: h,
Number: hexutil.Uint64(block.Height),
ParentHash: block.ParentBlockHash,
ReceiptsRoot: block.ReceiptRoot,
Transactions: block.TransactionHashes,
Uncles: []common.Hash{},
GasLimit: hexutil.Uint64(blockGasLimit),
Nonce: gethTypes.BlockNonce{0x1},
Timestamp: hexutil.Uint64(block.Timestamp),
BaseFeePerGas: hexutil.Big(*big.NewInt(0)),
})
return notifier.Notify(sub.ID, response)
Comment on lines +62 to +67
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 18, 2024

Choose a reason for hiding this comment

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

💡 Codebase verification

Bloom filter is not included in prepareBlockResponse

The prepareBlockResponse method does not include the Bloom field in the returned Block struct, which is contrary to the PR objective.

  • File: api/stream.go
  • Lines: 62-67
🔗 Analysis chain

Verify that the Bloom filter is included in block responses as intended

To fulfill the PR objective of including the Bloom filter in the streaming API's block responses, ensure that the prepareBlockResponse method correctly incorporates the Bloom filter.

Run the following script to confirm that the Bloom field is included in the block responses:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the Bloom filter is included in the block responses from the streaming API.

# Test: Search for the inclusion of the Bloom field in the block response preparation. Expect: The Bloom field should be part of the Block struct in the prepareBlockResponse method.

ast-grep --lang go --pattern $'func ($_ *BlockChainAPI) prepareBlockResponse($_, $_) {
    $$$
    return Block{
        $$$
        Bloom: $_,
        $$$
    }
}' --json

Length of output: 175

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its called LogsBloom

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterargue, thank you for the correction. I see that the Bloom filter is included as LogsBloom in the prepareBlockResponse method.


✏️ Learnings added
Learnt from: peterargue
PR: onflow/flow-evm-gateway#617
File: api/stream.go:62-67
Timestamp: 2024-10-18T19:26:37.250Z
Learning: In the `flow-evm-gateway` project, within the Go file `api/stream.go`, the `prepareBlockResponse` method includes the Bloom filter as the field `LogsBloom` in the returned `Block` struct.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

}
},
)
Expand Down
1 change: 1 addition & 0 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
streamAPI := api.NewStreamAPI(
b.logger,
b.config,
blockchainAPI,
b.storages.Blocks,
b.storages.Transactions,
b.storages.Receipts,
Expand Down
Loading