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 3 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
56 changes: 56 additions & 0 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"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 @@

// 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,58 @@
}, nil
}

func (core *coreService) BlobSidecarsByHeight(height uint64) ([]*apitypes.BlobSidecarResult, error) {
header, err := core.bc.BlockHeaderByHeight(height)
Copy link
Member

Choose a reason for hiding this comment

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

move to L1238

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

if err != nil {
return nil, err
}
res := make([]*apitypes.BlobSidecarResult, 0)
blobs, txHashes, err := core.getBlobSidecars(height)
if err != nil {
return nil, err
}
txIndices := make([]uint64, 0)
for _, txHash := range txHashes {
_, _, index, err := core.ActionByActionHash(txHash)
if err != nil {
return nil, err
}
txIndices = append(txIndices, uint64(index))
}
blkHash := header.HashBlock()
for i, blob := range blobs {
Copy link
Member

Choose a reason for hiding this comment

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

move L1232 to here, get the index and use it at L1244, then we don't need to define txIndices?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

res = append(res, &apitypes.BlobSidecarResult{
BlobSidecar: blob,
BlockNumber: height,
BlockHash: common.BytesToHash(blkHash[:]),
Copy link
Member

@dustinxie dustinxie Sep 23, 2024

Choose a reason for hiding this comment

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

BlockNumber, BlockHash is common for all tx, only need 1 copy, i mean like below?

apitypes.BlobSidecarResult {
    BlockNumber
    BlockHash
    [] struct {
        blobSidecar
        txIndex
        txHash
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can just keep same as bsc. IMO, this design is to use same structure for getSidercarByBlock and getSidercarByTx

TxIndex: txIndices[i],
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)
txIndexes := make([]uint64, 0)

Check failure on line 1261 in api/coreservice.go

View workflow job for this annotation

GitHub Actions / ci flow

txIndexes declared and not used
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