internal/ethapi: refactor func rpcMarshalBlock #19570 (#1602)

This commit is contained in:
Daniel Liu 2025-10-08 13:01:56 +08:00 committed by GitHub
parent 72bfc81606
commit 915186fea2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -411,7 +411,7 @@ func (api *BlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.Hash)
func (s *BlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) {
block, err := s.b.BlockByNumber(ctx, number)
if block != nil {
response, err := s.rpcMarshalBlock(block, true, fullTx)
response, err := s.rpcMarshalBlock(ctx, block, true, fullTx)
if err == nil && number == rpc.PendingBlockNumber {
// Pending blocks need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner", "number"} {
@ -428,7 +428,7 @@ func (s *BlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNu
func (s *BlockChainAPI) GetBlockByHash(ctx context.Context, hash common.Hash, fullTx bool) (map[string]interface{}, error) {
block, err := s.b.GetBlock(ctx, hash)
if block != nil {
return s.rpcMarshalBlock(block, true, fullTx)
return s.rpcMarshalBlock(ctx, block, true, fullTx)
}
return nil, err
}
@ -444,7 +444,7 @@ func (s *BlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, block
return nil, nil
}
block = types.NewBlockWithHeader(uncles[index])
return s.rpcMarshalBlock(block, false, false)
return s.rpcMarshalBlock(ctx, block, false, false)
}
return nil, err
}
@ -461,7 +461,7 @@ func (s *BlockChainAPI) GetUncleByBlockHashAndIndex(ctx context.Context, blockHa
return nil, nil
}
block = types.NewBlockWithHeader(uncles[index])
return s.rpcMarshalBlock(block, false, false)
return s.rpcMarshalBlock(ctx, block, false, false)
}
return nil, err
}
@ -1374,13 +1374,13 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
// rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
// a `BlockChainAPI`.
func (s *BlockChainAPI) rpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
func (s *BlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Block, inclTx bool, fullTx bool) (map[string]interface{}, error) {
fields, err := RPCMarshalBlock(b, inclTx, fullTx, s.b.ChainConfig())
if err != nil {
return nil, err
}
if inclTx {
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(context.Background(), b.Hash()))
fields["totalDifficulty"] = (*hexutil.Big)(s.b.GetTd(ctx, b.Hash()))
}
return fields, err
}