From c46d7e8bd85ab6503fec62351648dac0a1753895 Mon Sep 17 00:00:00 2001 From: "Dike.w" Date: Thu, 22 Aug 2024 11:07:05 +0800 Subject: [PATCH] ethclient: fix BlobSidecars api (#2656) --- core/types/blob_sidecar.go | 39 ++++++++++++++++++++++++++++++++++++++ ethclient/ethclient.go | 8 ++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/core/types/blob_sidecar.go b/core/types/blob_sidecar.go index d4f63602d..a97d1ed40 100644 --- a/core/types/blob_sidecar.go +++ b/core/types/blob_sidecar.go @@ -2,10 +2,12 @@ package types import ( "bytes" + "encoding/json" "errors" "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/rlp" ) @@ -53,3 +55,40 @@ func (s *BlobSidecar) SanityCheck(blockNumber *big.Int, blockHash common.Hash) e } return nil } + +func (s *BlobSidecar) MarshalJSON() ([]byte, error) { + fields := map[string]interface{}{ + "blockHash": s.BlockHash, + "blockNumber": hexutil.EncodeUint64(s.BlockNumber.Uint64()), + "txHash": s.TxHash, + "txIndex": hexutil.EncodeUint64(s.TxIndex), + } + fields["blobSidecar"] = s.BlobTxSidecar + return json.Marshal(fields) +} + +func (s *BlobSidecar) UnmarshalJSON(input []byte) error { + type blobSidecar struct { + BlobSidecar BlobTxSidecar `json:"blobSidecar"` + BlockNumber *hexutil.Big `json:"blockNumber"` + BlockHash common.Hash `json:"blockHash"` + TxIndex *hexutil.Big `json:"txIndex"` + TxHash common.Hash `json:"txHash"` + } + var blob blobSidecar + if err := json.Unmarshal(input, &blob); err != nil { + return err + } + s.BlobTxSidecar = blob.BlobSidecar + if blob.BlockNumber == nil { + return errors.New("missing required field 'blockNumber' for BlobSidecar") + } + s.BlockNumber = blob.BlockNumber.ToInt() + s.BlockHash = blob.BlockHash + if blob.TxIndex == nil { + return errors.New("missing required field 'txIndex' for BlobSidecar") + } + s.TxIndex = blob.TxIndex.ToInt().Uint64() + s.TxHash = blob.TxHash + return nil +} diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index bf26c79aa..8cf50eb52 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -131,8 +131,8 @@ func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumb } // BlobSidecars return the Sidecars of a given block number or hash. -func (ec *Client) BlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.BlobTxSidecar, error) { - var r []*types.BlobTxSidecar +func (ec *Client) BlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.BlobSidecar, error) { + var r []*types.BlobSidecar err := ec.c.CallContext(ctx, &r, "eth_getBlobSidecars", blockNrOrHash.String()) if err == nil && r == nil { return nil, ethereum.NotFound @@ -141,8 +141,8 @@ func (ec *Client) BlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumbe } // BlobSidecarByTxHash return a sidecar of a given blob transaction -func (ec *Client) BlobSidecarByTxHash(ctx context.Context, hash common.Hash) (*types.BlobTxSidecar, error) { - var r *types.BlobTxSidecar +func (ec *Client) BlobSidecarByTxHash(ctx context.Context, hash common.Hash) (*types.BlobSidecar, error) { + var r *types.BlobSidecar err := ec.c.CallContext(ctx, &r, "eth_getBlobSidecarByTxHash", hash) if err == nil && r == nil { return nil, ethereum.NotFound