Skip to content

Commit

Permalink
Merge pull request #617 from onflow/petera/align-streaming-headers
Browse files Browse the repository at this point in the history
Reuse block preparation logic in streaming API
  • Loading branch information
m-Peter authored Oct 21, 2024
2 parents 418a150 + 42c08a0 commit d21f299
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
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)
}
},
)
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

0 comments on commit d21f299

Please sign in to comment.