Skip to content

Commit

Permalink
fix getBlock RPC (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored Jul 24, 2024
1 parent ec5315b commit 9bd08a2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
5 changes: 3 additions & 2 deletions go/common/subscription/new_heads_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 52 additions & 4 deletions tools/walletextension/rpcapi/blockchain_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 9bd08a2

Please sign in to comment.