Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement eth_getBlobSidecars && eth_getBlobSidecarByTxHash #2286

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/types/tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func (s BlobTxSidecars) EncodeIndex(i int, w *bytes.Buffer) {

// BlobTxSidecar contains the blobs of a blob transaction.
type BlobTxSidecar struct {
Blobs []kzg4844.Blob // Blobs needed by the blob pool
Commitments []kzg4844.Commitment // Commitments needed by the blob pool
Proofs []kzg4844.Proof // Proofs needed by the blob pool
Blobs []kzg4844.Blob `json:"blobs"` // Blobs needed by the blob pool
buddh0 marked this conversation as resolved.
Show resolved Hide resolved
Commitments []kzg4844.Commitment `json:"commitments"` // Commitments needed by the blob pool
Proofs []kzg4844.Proof `json:"proofs"` // Proofs needed by the blob pool
}

// BlobHashes computes the blob hashes of the given blobs.
Expand Down
3 changes: 3 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (b *EthAPIBackend) GetReceipts(ctx context.Context, hash common.Hash) (type
return b.eth.blockchain.GetReceiptsByHash(hash), nil
}

func (b *EthAPIBackend) GetBlobSidecars(ctx context.Context, hash common.Hash) (types.BlobTxSidecars, error) {
return b.eth.blockchain.GetBlobsByHash(hash), nil
}
func (b *EthAPIBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) {
return rawdb.ReadLogs(b.eth.chainDb, hash, number), nil
}
Expand Down
20 changes: 20 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ func (ec *Client) BlockReceipts(ctx context.Context, blockNrOrHash rpc.BlockNumb
return r, err
}

// 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
err := ec.c.CallContext(ctx, &r, "eth_getBlobSidecars", blockNrOrHash.String())
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) {
var r *types.BlobTxSidecar
err := ec.c.CallContext(ctx, &r, "eth_getBlockSidecarByTxHash", hash)
if err == nil && r == nil {
return nil, ethereum.NotFound
}
return r, err
}

type rpcBlock struct {
Hash common.Hash `json:"hash"`
Transactions []rpcTransaction `json:"transactions"`
Expand Down
65 changes: 65 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,60 @@ 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) {
block, err := s.b.BlockByNumberOrHash(ctx, blockNrOrHash)
if block == nil || err != nil {
// When the block doesn't exist, the RPC method should return JSON null
// as per specification.
return nil, nil
buddh0 marked this conversation as resolved.
Show resolved Hide resolved
}
blobSidecars, err := s.b.GetBlobSidecars(ctx, block.Hash())
if err != nil || len(blobSidecars) == 0 {
return nil, nil
}
result := make([]map[string]interface{}, len(blobSidecars))
blobIndex := -1
for txIndex, tx := range block.Transactions() {
if tx.Type() == types.BlobTxType {
blobIndex++
if blobIndex >= len(blobSidecars) {
return nil, fmt.Errorf("blobSidecars length mismatch %d", len(blobSidecars))
}
result[blobIndex] = marshalBlobSidecar(blobSidecars[blobIndex], block.Hash(), block.NumberU64(), tx, txIndex)
}
}
return result, nil
}

func (s *BlockChainAPI) GetBlobSidecarByTxHash(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
txTarget, blockHash, blockNumber, Index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
if txTarget == nil {
return nil, nil
}
block, err := s.b.BlockByHash(ctx, blockHash)
if block == nil || err != nil {
// When the block doesn't exist, the RPC method should return JSON null
// as per specification.
return nil, nil
buddh0 marked this conversation as resolved.
Show resolved Hide resolved
}
blobSidecars, err := s.b.GetBlobSidecars(ctx, blockHash)
zlacfzy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil || len(blobSidecars) == 0 {
return nil, nil
}
blobIndex := -1
var result map[string]interface{}
for txIndex, tx := range block.Transactions() {
if tx.Type() == types.BlobTxType {
blobIndex++
buddh0 marked this conversation as resolved.
Show resolved Hide resolved
if txIndex == int(Index) {
result = marshalBlobSidecar(blobSidecars[blobIndex], blockHash, blockNumber, txTarget, int(Index))
return result, nil
}
}
}
return result, nil
}

// OverrideAccount indicates the overriding fields of account during the execution
// of a message call.
// Note, state and stateDiff can't be specified at the same time. If state is
Expand Down Expand Up @@ -2105,6 +2159,17 @@ func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber u
return fields
}

func marshalBlobSidecar(blobSidecar *types.BlobTxSidecar, blockHash common.Hash, blockNumber uint64, tx *types.Transaction, txIndex int) map[string]interface{} {
fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockNumber),
"txHash": tx.Hash(),
"txIndex": hexutil.Uint64(txIndex),
"blobSidecar": blobSidecar,
}
return fields
}

// sign is a helper function that signs a transaction with the private key of the given address.
func (s *TransactionAPI) sign(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
// Look up the wallet containing the requested signer
Expand Down
9 changes: 9 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,15 @@ func (b testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.R
receipts := rawdb.ReadReceipts(b.db, hash, header.Number.Uint64(), header.Time, b.chain.Config())
return receipts, nil
}

func (b testBackend) GetBlobSidecars(ctx context.Context, hash common.Hash) (types.BlobTxSidecars, error) {
header, err := b.HeaderByHash(ctx, hash)
if header == nil || err != nil {
return nil, err
}
blobSidecars := rawdb.ReadRawBlobs(b.db, hash, header.Number.Uint64())
return blobSidecars, nil
}
func (b testBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some unit tests for these two apis?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will added later

if b.pending != nil && hash == b.pending.Hash() {
return nil
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Backend interface {
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
GetBlobSidecars(ctx context.Context, hash common.Hash) (types.BlobTxSidecars, error)

// Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error
Expand Down
3 changes: 3 additions & 0 deletions internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ func (b *backendMock) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
func (b *backendMock) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
return nil, nil
}
func (b *backendMock) GetBlobSidecars(ctx context.Context, hash common.Hash) (types.BlobTxSidecars, error) {
return nil, nil
}
func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) {
return nil, nil
}
Expand Down
10 changes: 10 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,16 @@ web3._extend({
call: 'eth_getBlockReceipts',
params: 1,
}),
new web3._extend.Method({
name: 'getBlobSidecars',
call: 'eth_getBlobSidecars',
params: 1,
}),
new web3._extend.Method({
name: 'getBlobSidecarByTxHash',
call: 'eth_getBlobSidecarByTxHash',
params: 1,
}),
],
properties: [
new web3._extend.Property({
Expand Down
Loading