internal/api.go: add choice about not show full blob (#2458)

This commit is contained in:
Eric 2024-05-14 17:17:58 +08:00 committed by GitHub
parent 0560685460
commit 97c3b9b267
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 125 additions and 44 deletions

@ -131,9 +131,9 @@ 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
}
@ -141,9 +141,9 @@ 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) {
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
}

@ -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
@ -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
@ -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
}
}
@ -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
}

@ -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",
},
}
@ -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
@ -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",
},
}
@ -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

@ -0,0 +1,17 @@
{
"blobSidecar": {
"blobs": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"commitments": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"proofs": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
]
},
"blockHash": "0x6fe5e7eea22c44f700c95da066d6e0740894b6c1e993825cc63f634ad4f74250",
"blockNumber": "0x7",
"txHash": "0xc520427e696154779f6b21ab03a0735769e1c029035a484f5876f60383a0a7ce",
"txIndex": "0x0"
}

@ -0,0 +1,19 @@
[
{
"blobSidecar": {
"blobs": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"commitments": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"proofs": [
"0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
]
},
"blockHash": "0x6fe5e7eea22c44f700c95da066d6e0740894b6c1e993825cc63f634ad4f74250",
"blockNumber": "0x7",
"txHash": "0xc520427e696154779f6b21ab03a0735769e1c029035a484f5876f60383a0a7ce",
"txIndex": "0x0"
}
]

@ -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: [