Skip to content

Commit

Permalink
internal/api.go: add choice about not show full blob (#2458)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlacfzy authored May 14, 2024
1 parent 0560685 commit 97c3b9b
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 44 deletions.
8 changes: 4 additions & 4 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ 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) {
func (ec *Client) BlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, fullBlob bool) ([]*types.BlobTxSidecar, error) {
var r []*types.BlobTxSidecar
err := ec.c.CallContext(ctx, &r, "eth_getBlobSidecars", blockNrOrHash.String())
err := ec.c.CallContext(ctx, &r, "eth_getBlobSidecars", blockNrOrHash.String(), fullBlob)
if err == nil && r == nil {
return nil, ethereum.NotFound
}
return r, err
}

// BlobSidecarByTxHash return a sidecar of a given blob transaction
func (ec *Client) BlobSidecarByTxHash(ctx context.Context, hash common.Hash) (*types.BlobTxSidecar, error) {
func (ec *Client) BlobSidecarByTxHash(ctx context.Context, hash common.Hash, fullBlob bool) (*types.BlobTxSidecar, error) {
var r *types.BlobTxSidecar
err := ec.c.CallContext(ctx, &r, "eth_getBlockSidecarByTxHash", hash)
err := ec.c.CallContext(ctx, &r, "eth_getBlockSidecarByTxHash", hash, fullBlob)
if err == nil && r == nil {
return nil, ethereum.NotFound
}
Expand Down
30 changes: 24 additions & 6 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ func (s *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.
return result, nil
}

func (s *BlockChainAPI) GetBlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) {
func (s *BlockChainAPI) GetBlobSidecars(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, fullBlob bool) ([]map[string]interface{}, error) {
header, err := s.b.HeaderByNumberOrHash(ctx, blockNrOrHash)
if header == nil || err != nil {
// When the block doesn't exist, the RPC method should return JSON null
Expand All @@ -1023,12 +1023,12 @@ func (s *BlockChainAPI) GetBlobSidecars(ctx context.Context, blockNrOrHash rpc.B
}
result := make([]map[string]interface{}, len(blobSidecars))
for i, sidecar := range blobSidecars {
result[i] = marshalBlobSidecar(sidecar)
result[i] = marshalBlobSidecar(sidecar, fullBlob)
}
return result, nil
}

func (s *BlockChainAPI) GetBlobSidecarByTxHash(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
func (s *BlockChainAPI) GetBlobSidecarByTxHash(ctx context.Context, hash common.Hash, fullBlob bool) (map[string]interface{}, error) {
txTarget, blockHash, _, Index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
if txTarget == nil {
return nil, nil
Expand All @@ -1045,7 +1045,7 @@ func (s *BlockChainAPI) GetBlobSidecarByTxHash(ctx context.Context, hash common.
}
for _, sidecar := range blobSidecars {
if sidecar.TxIndex == Index {
return marshalBlobSidecar(sidecar), nil
return marshalBlobSidecar(sidecar, fullBlob), nil
}
}

Expand Down Expand Up @@ -2142,13 +2142,31 @@ func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber u
return fields
}

func marshalBlobSidecar(sidecar *types.BlobSidecar) map[string]interface{} {
func marshalBlobSidecar(sidecar *types.BlobSidecar, fullBlob bool) map[string]interface{} {
fields := map[string]interface{}{
"blockHash": sidecar.BlockHash,
"blockNumber": hexutil.EncodeUint64(sidecar.BlockNumber.Uint64()),
"txHash": sidecar.TxHash,
"txIndex": hexutil.EncodeUint64(sidecar.TxIndex),
"blobSidecar": sidecar.BlobTxSidecar,
}
fields["blobSidecar"] = marshalBlob(sidecar.BlobTxSidecar, fullBlob)
return fields
}

func marshalBlob(blobTxSidecar types.BlobTxSidecar, fullBlob bool) map[string]interface{} {
fields := map[string]interface{}{
"blobs": blobTxSidecar.Blobs,
"commitments": blobTxSidecar.Commitments,
"proofs": blobTxSidecar.Proofs,
}
if !fullBlob {
var blobs []common.Hash
for _, blob := range blobTxSidecar.Blobs {
var value common.Hash
copy(value[:], blob[:32])
blobs = append(blobs, value)
}
fields["blobs"] = blobs
}
return fields
}
Expand Down
91 changes: 59 additions & 32 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2139,48 +2139,63 @@ func TestRPCGetBlobSidecars(t *testing.T) {
}

var testSuite = []struct {
test rpc.BlockNumberOrHash
file string
test rpc.BlockNumberOrHash
fullBlob bool
file string
}{
// 1. block without any txs(number)
{
test: rpc.BlockNumberOrHashWithNumber(0),
file: "number-1",
test: rpc.BlockNumberOrHashWithNumber(0),
fullBlob: true,
file: "number-1",
},
// 2. earliest tag
{
test: rpc.BlockNumberOrHashWithNumber(rpc.EarliestBlockNumber),
file: "tag-earliest",
test: rpc.BlockNumberOrHashWithNumber(rpc.EarliestBlockNumber),
fullBlob: true,
file: "tag-earliest",
},
// 3. latest tag
{
test: rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber),
file: "tag-latest",
test: rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber),
fullBlob: true,
file: "tag-latest",
},
// 4. block is empty
{
test: rpc.BlockNumberOrHashWithHash(common.Hash{}, false),
file: "hash-empty",
test: rpc.BlockNumberOrHashWithHash(common.Hash{}, false),
fullBlob: true,
file: "hash-empty",
},
// 5. block is not found
{
test: rpc.BlockNumberOrHashWithHash(common.HexToHash("deadbeef"), false),
file: "hash-notfound",
test: rpc.BlockNumberOrHashWithHash(common.HexToHash("deadbeef"), false),
fullBlob: true,
file: "hash-notfound",
},
// 6. block is not found
{
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(genBlocks + 1)),
file: "block-notfound",
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(genBlocks + 1)),
fullBlob: true,
file: "block-notfound",
},
// 7. block with blob tx
{
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(6)),
file: "block-with-blob-tx",
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(6)),
fullBlob: true,
file: "block-with-blob-tx",
},
// 8. block with sidecar
{
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(7)),
file: "block-with-blobSidecars",
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(7)),
fullBlob: true,
file: "block-with-blobSidecars",
},
// 9. block with sidecar but show little
{
test: rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(7)),
fullBlob: false,
file: "block-with-blobSidecars-show-little",
},
}

Expand All @@ -2189,7 +2204,7 @@ func TestRPCGetBlobSidecars(t *testing.T) {
result interface{}
err error
)
result, err = api.GetBlobSidecars(context.Background(), tt.test)
result, err = api.GetBlobSidecars(context.Background(), tt.test, tt.fullBlob)
if err != nil {
t.Errorf("test %d: want no error, have %v", i, err)
continue
Expand All @@ -2205,33 +2220,45 @@ func TestGetBlobSidecarByTxHash(t *testing.T) {
api = NewBlockChainAPI(backend)
)
var testSuite = []struct {
test common.Hash
file string
test common.Hash
fullBlob bool
file string
}{
// 0. txHash is empty
{
test: common.Hash{},
file: "hash-empty",
test: common.Hash{},
fullBlob: true,
file: "hash-empty",
},
// 1. txHash is not found
{
test: common.HexToHash("deadbeef"),
file: "hash-notfound",
test: common.HexToHash("deadbeef"),
fullBlob: true,
file: "hash-notfound",
},
// 2. txHash is not blob tx
{
test: common.HexToHash("deadbeef"),
file: "not-blob-tx",
test: common.HexToHash("deadbeef"),
fullBlob: true,
file: "not-blob-tx",
},
// 3. block with blob tx without sidecar
{
test: txHashs[5],
file: "block-with-blob-tx",
test: txHashs[5],
fullBlob: true,
file: "block-with-blob-tx",
},
// 4. block with sidecar
{
test: txHashs[6],
file: "block-with-blobSidecars",
test: txHashs[6],
fullBlob: true,
file: "block-with-blobSidecars",
},
// 4. block show part blobs
{
test: txHashs[6],
fullBlob: false,
file: "block-with-blobSidecars-show-little",
},
}

Expand All @@ -2240,7 +2267,7 @@ func TestGetBlobSidecarByTxHash(t *testing.T) {
result interface{}
err error
)
result, err = api.GetBlobSidecarByTxHash(context.Background(), tt.test)
result, err = api.GetBlobSidecarByTxHash(context.Background(), tt.test, tt.fullBlob)
if err != nil {
t.Errorf("test %d: want no error, have %v", i, err)
continue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"blobSidecar": {
"blobs": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"commitments": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"proofs": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
]
},
"blockHash": "0x6fe5e7eea22c44f700c95da066d6e0740894b6c1e993825cc63f634ad4f74250",
"blockNumber": "0x7",
"txHash": "0xc520427e696154779f6b21ab03a0735769e1c029035a484f5876f60383a0a7ce",
"txIndex": "0x0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"blobSidecar": {
"blobs": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"commitments": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"proofs": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
]
},
"blockHash": "0x6fe5e7eea22c44f700c95da066d6e0740894b6c1e993825cc63f634ad4f74250",
"blockNumber": "0x7",
"txHash": "0xc520427e696154779f6b21ab03a0735769e1c029035a484f5876f60383a0a7ce",
"txIndex": "0x0"
}
]
4 changes: 2 additions & 2 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,12 @@ web3._extend({
new web3._extend.Method({
name: 'getBlobSidecars',
call: 'eth_getBlobSidecars',
params: 1,
params: 2,
}),
new web3._extend.Method({
name: 'getBlobSidecarByTxHash',
call: 'eth_getBlobSidecarByTxHash',
params: 1,
params: 2,
}),
],
properties: [
Expand Down

0 comments on commit 97c3b9b

Please sign in to comment.