diff --git a/go/common/subscription/new_heads_manager.go b/go/common/subscription/new_heads_manager.go index b39ebc21ef..4ebbb8ce02 100644 --- a/go/common/subscription/new_heads_manager.go +++ b/go/common/subscription/new_heads_manager.go @@ -88,7 +88,7 @@ func (nhs *NewHeadsService) onNewBatch(head *common.BatchHeader) error { var msg any = head if nhs.convertToEthHeader { - msg = convertBatchHeader(head) + msg = ConvertBatchHeader(head) } nhs.notifiersMutex.Lock() @@ -130,10 +130,11 @@ func (nhs *NewHeadsService) HealthStatus(context.Context) host.HealthStatus { return &host.BasicErrHealthStatus{} } -func convertBatchHeader(head *common.BatchHeader) *types.Header { +func ConvertBatchHeader(head *common.BatchHeader) *types.Header { return &types.Header{ ParentHash: head.ParentHash, UncleHash: gethcommon.Hash{}, + Coinbase: head.Coinbase, Root: head.Root, TxHash: head.TxHash, ReceiptHash: head.ReceiptHash, diff --git a/tools/walletextension/rpcapi/blockchain_api.go b/tools/walletextension/rpcapi/blockchain_api.go index aeb6a01ed9..5760e760f2 100644 --- a/tools/walletextension/rpcapi/blockchain_api.go +++ b/tools/walletextension/rpcapi/blockchain_api.go @@ -12,6 +12,7 @@ import ( "github.com/ten-protocol/go-ten/go/common" "github.com/ten-protocol/go-ten/go/common/gethapi" "github.com/ten-protocol/go-ten/go/common/privacy" + "github.com/ten-protocol/go-ten/go/common/subscription" "github.com/ten-protocol/go-ten/lib/gethfork/rpc" ) @@ -100,7 +101,7 @@ func (api *BlockChainAPI) GetHeaderByHash(ctx context.Context, hash gethcommon.H } func (api *BlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { - resp, err := UnauthenticatedTenRPCCall[map[string]interface{}]( + resp, err := UnauthenticatedTenRPCCall[common.BatchHeader]( ctx, api.we, &CacheCfg{ @@ -111,15 +112,23 @@ func (api *BlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.Block if resp == nil { return nil, err } - return *resp, err + + // convert to geth header and marshall + header := subscription.ConvertBatchHeader(resp) + fields := RPCMarshalHeader(header) + return fields, err } func (api *BlockChainAPI) GetBlockByHash(ctx context.Context, hash gethcommon.Hash, fullTx bool) (map[string]interface{}, error) { - resp, err := UnauthenticatedTenRPCCall[map[string]interface{}](ctx, api.we, &CacheCfg{CacheType: LongLiving}, "eth_getBlockByHash", hash, fullTx) + resp, err := UnauthenticatedTenRPCCall[common.BatchHeader](ctx, api.we, &CacheCfg{CacheType: LongLiving}, "eth_getBlockByHash", hash, fullTx) if resp == nil { return nil, err } - return *resp, err + + // convert to geth header and marshall + header := subscription.ConvertBatchHeader(resp) + fields := RPCMarshalHeader(header) + return fields, err } func (api *BlockChainAPI) GetCode(ctx context.Context, address gethcommon.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) { @@ -338,3 +347,42 @@ func extractCustomQueryAddress(params any) (*gethcommon.Address, error) { address := gethcommon.HexToAddress(addressStr) return &address, nil } + +// RPCMarshalHeader converts the given header to the RPC output . +// duplicated from go-ethereum +func RPCMarshalHeader(head *types.Header) map[string]interface{} { + result := map[string]interface{}{ + "number": (*hexutil.Big)(head.Number), + "hash": head.Hash(), + "parentHash": head.ParentHash, + "nonce": head.Nonce, + "mixHash": head.MixDigest, + "sha3Uncles": head.UncleHash, + "logsBloom": head.Bloom, + "stateRoot": head.Root, + "miner": head.Coinbase, + "difficulty": (*hexutil.Big)(head.Difficulty), + "extraData": hexutil.Bytes(head.Extra), + "gasLimit": hexutil.Uint64(head.GasLimit), + "gasUsed": hexutil.Uint64(head.GasUsed), + "timestamp": hexutil.Uint64(head.Time), + "transactionsRoot": head.TxHash, + "receiptsRoot": head.ReceiptHash, + } + if head.BaseFee != nil { + result["baseFeePerGas"] = (*hexutil.Big)(head.BaseFee) + } + if head.WithdrawalsHash != nil { + result["withdrawalsRoot"] = head.WithdrawalsHash + } + if head.BlobGasUsed != nil { + result["blobGasUsed"] = hexutil.Uint64(*head.BlobGasUsed) + } + if head.ExcessBlobGas != nil { + result["excessBlobGas"] = hexutil.Uint64(*head.ExcessBlobGas) + } + if head.ParentBeaconRoot != nil { + result["parentBeaconBlockRoot"] = head.ParentBeaconRoot + } + return result +}