Skip to content

Commit

Permalink
[api] eth_getBlobSidecar (#4371)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Sep 23, 2024
1 parent 6eb8de4 commit 0dec7d5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
52 changes: 52 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"strconv"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"

Expand Down Expand Up @@ -180,6 +182,8 @@ type (

// Track tracks the api call
Track(ctx context.Context, start time.Time, method string, size int64, success bool)
// BlobSidecarsByHeight returns blob sidecars by height
BlobSidecarsByHeight(height uint64) ([]*apitypes.BlobSidecarResult, error)
}

// coreService implements the CoreService interface
Expand Down Expand Up @@ -1213,6 +1217,54 @@ func (core *coreService) getBlockByHeight(height uint64) (*apitypes.BlockWithRec
}, nil
}

func (core *coreService) BlobSidecarsByHeight(height uint64) ([]*apitypes.BlobSidecarResult, error) {
res := make([]*apitypes.BlobSidecarResult, 0)
blobs, txHashes, err := core.getBlobSidecars(height)
if err != nil {
return nil, err
}
header, err := core.bc.BlockHeaderByHeight(height)
if err != nil {
return nil, err
}
blkHash := header.HashBlock()
blokHashComm := common.BytesToHash(blkHash[:])
for i, blob := range blobs {
_, _, index, err := core.ActionByActionHash(txHashes[i])
if err != nil {
return nil, err
}
res = append(res, &apitypes.BlobSidecarResult{
BlobSidecar: blob,
BlockNumber: height,
BlockHash: blokHashComm,
TxIndex: uint64(index),
TxHash: common.BytesToHash(txHashes[i][:]),
})
}
return res, nil
}

func (core *coreService) getBlobSidecars(height uint64) ([]*types.BlobTxSidecar, []hash.Hash256, error) {
blobs, txHashStr, err := core.dao.GetBlobsByHeight(height)
switch errors.Cause(err) {
case nil:
case db.ErrNotExist:
return nil, nil, errors.Wrapf(ErrNotFound, "failed to find blobs by height %d", height)
default:
return nil, nil, err
}
txHashes := make([]hash.Hash256, 0)
for _, hashStr := range txHashStr {
txHash, err := hash.HexStringToHash256(hashStr)
if err != nil {
return nil, nil, err
}
txHashes = append(txHashes, txHash)
}
return blobs, txHashes, nil
}

func (core *coreService) getGravityChainStartHeight(epochHeight uint64) (uint64, error) {
gravityChainStartHeight := epochHeight
if pp := poll.FindProtocol(core.registry); pp != nil {
Expand Down
11 changes: 11 additions & 0 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
)
Expand Down Expand Up @@ -37,6 +40,14 @@ type (
Block *block.Block
Receipts []*action.Receipt
}
// BlobSidecarResult is the result of get blob sidecar
BlobSidecarResult struct {
BlobSidecar *types.BlobTxSidecar `json:"blobSidecar"`
BlockNumber uint64 `json:"blockHeight"`
BlockHash common.Hash `json:"blockHash"`
TxIndex uint64 `json:"txIndex"`
TxHash common.Hash `json:"txHash"`
}
)

// responseWriter for server
Expand Down
22 changes: 22 additions & 0 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func (svr *web3Handler) handleWeb3Req(ctx context.Context, web3Req *gjson.Result
res, err = svr.subscribe(sc, web3Req, writer)
case "eth_unsubscribe":
res, err = svr.unsubscribe(web3Req)
case "eth_getBlobSidecars":
res, err = svr.getBlobSidecars(web3Req)
//TODO: enable debug api after archive mode is supported
// case "debug_traceTransaction":
// res, err = svr.traceTransaction(ctx, web3Req)
Expand Down Expand Up @@ -982,6 +984,26 @@ func (svr *web3Handler) unsubscribe(in *gjson.Result) (interface{}, error) {
return chainListener.RemoveResponder(id.String())
}

func (svr *web3Handler) getBlobSidecars(in *gjson.Result) (interface{}, error) {
blkNum := in.Get("params.0")
if !blkNum.Exists() {
return nil, errInvalidFormat
}
num, err := svr.parseBlockNumber(blkNum.String())
if err != nil {
return nil, err
}
res, err := svr.coreService.BlobSidecarsByHeight(num)
switch errors.Cause(err) {
case nil:
return res, nil
case ErrNotFound:
return nil, nil
default:
return nil, err
}
}

func (svr *web3Handler) traceTransaction(ctx context.Context, in *gjson.Result) (interface{}, error) {
actHash, options := in.Get("params.0"), in.Get("params.1")
if !actHash.Exists() {
Expand Down
43 changes: 43 additions & 0 deletions api/web3server_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/action"
apitypes "github.com/iotexproject/iotex-core/api/types"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/test/identityset"
Expand Down Expand Up @@ -542,3 +545,43 @@ func TestStreamResponseMarshal(t *testing.T) {
}
`, string(res))
}

func TestBlobSiderCar(t *testing.T) {
require := require.New(t)

t.Run("Marshal", func(t *testing.T) {
res, err := json.Marshal(&apitypes.BlobSidecarResult{
BlobSidecar: &types.BlobTxSidecar{
Blobs: []kzg4844.Blob{},
Commitments: []kzg4844.Commitment{
kzg4844.Commitment{},
},
Proofs: []kzg4844.Proof{
kzg4844.Proof{},
},
},
BlockNumber: 1,
BlockHash: common.BigToHash(big.NewInt(2)),
TxIndex: 2,
TxHash: common.BigToHash(big.NewInt(3)),
})
require.NoError(err)
require.JSONEq(`
{
"blobSidecar":{
"Blobs":[],
"Commitments":[
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
],
"Proofs":[
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
]
},
"blockHeight":1,
"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000002",
"txIndex":2,
"txHash":"0x0000000000000000000000000000000000000000000000000000000000000003"
}
`, string(res))
})
}
15 changes: 15 additions & 0 deletions test/mock/mock_apicoreservice/mock_apicoreservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dec7d5

Please sign in to comment.