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

[api] eth_getBlobSidecar #4371

Merged
merged 5 commits into from
Sep 23, 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
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][:]),
Copy link
Collaborator

Choose a reason for hiding this comment

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

do the conversion in line 1230 and save them in local variables.

})
}
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
Copy link
Member

Choose a reason for hiding this comment

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

ErrNotFound should return nil to caller?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, just like getBlock and getTransaction

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.

Loading