Skip to content

Commit

Permalink
speedup log fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
Code0x2 committed Jul 4, 2023
1 parent e749d8f commit b54de9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
16 changes: 8 additions & 8 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,16 @@ func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
// mevexec
// GetHeaderAndReceiptsByHash retrieves the header and receipts of a block by hash
// header must not be nil
func (bc *BlockChain) GetReceiptsWithHeader(header *types.Header) types.Receipts {
func (bc *BlockChain) GetLogsWithHeader(header *types.Header) [][]*types.Log {
if receipts, ok := bc.receiptsCache.Get(header.Hash()); ok {
return receipts
}
receipts := rawdb.ReadReceipts(bc.db, header.Hash(), header.Number.Uint64(), header.Time, bc.chainConfig)
if receipts == nil {
return nil
logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
logs[i] = receipt.Logs
}
return logs
}
bc.receiptsCache.Add(header.Hash(), receipts)
return receipts
logs := rawdb.ReadLogs(bc.db, header.Hash(), header.Number.Uint64(), bc.chainConfig)
return logs
}

// mevexec
Expand Down
26 changes: 10 additions & 16 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,18 +1399,18 @@ func RPCMarshalCompactHeader(header *types.Header) map[string]interface{} {
}
}

func RPCMarshalCompactLogs(receipts types.Receipts) []map[string]interface{} {
logs := []map[string]interface{}{}
for _, receipt := range receipts {
for _, log := range receipt.Logs {
logs = append(logs, map[string]interface{}{
func RPCMarshalCompactLogs(logs [][]*types.Log) []map[string]interface{} {
logMap := []map[string]interface{}{}
for _, txLog := range logs {
for _, log := range txLog {
logMap = append(logMap, map[string]interface{}{
"address": log.Address,
"data": hexutil.Bytes(log.Data),
"topics": log.Topics,
})
}
}
return logs
return logMap
}

// rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field, which requires
Expand Down Expand Up @@ -2719,25 +2719,19 @@ func (s *SearcherAPI) rpcMarshalCompactHeader(ctx context.Context, h *types.Head
return RPCMarshalCompactHeader(h)
}

// rpcMarshalCompact uses the generalized output filler, then adds the total difficulty field, which requires
// a `PublicBlockchainAPI`.
func (s *SearcherAPI) rpcMarshalCompactLogs(ctx context.Context, r types.Receipts) []map[string]interface{} {
return RPCMarshalCompactLogs(r)
}

// GetCompactBlocks gets the compact block data for the given block's hash or number
// the logs in the block can also be requested
func (s *SearcherAPI) GetCompactBlocks(ctx context.Context, blockNrOrHashes []rpc.BlockNumberOrHash, logs bool) ([]map[string]interface{}, error) {
func (s *SearcherAPI) GetCompactBlocks(ctx context.Context, blockNrOrHashes []rpc.BlockNumberOrHash, returnLogs bool) ([]map[string]interface{}, error) {
resultArray := make([]map[string]interface{}, 0, len(blockNrOrHashes))
for _, blockNrOrHash := range blockNrOrHashes {
header, err := s.b.HeaderByNumberOrHash(ctx, blockNrOrHash)
if err != nil {
return nil, err
}
result := s.rpcMarshalCompactHeader(ctx, header)
if logs { // add logs if requested
receipts := s.chain.GetReceiptsWithHeader(header)
result["logs"] = s.rpcMarshalCompactLogs(ctx, receipts)
if returnLogs { // add logs if requested
logs := s.chain.GetLogsWithHeader(header)
result["logs"] = RPCMarshalCompactLogs(logs)
}
resultArray = append(resultArray, result)
}
Expand Down

0 comments on commit b54de9b

Please sign in to comment.