Skip to content

Commit

Permalink
adapt BSC RPC and add blob extra field
Browse files Browse the repository at this point in the history
grant allowance
  • Loading branch information
alexgao001 committed Jun 24, 2024
1 parent 6cac84e commit 4e1845e
Show file tree
Hide file tree
Showing 26 changed files with 1,400 additions and 43 deletions.
1 change: 1 addition & 0 deletions db/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Blob struct {
VersionedHash string `gorm:"NOT NULL"`
Slot uint64 `gorm:"NOT NULL;index:idx_blob_slot_index"`
Idx int `gorm:"NOT NULL;index:idx_blob_slot_idx"`
TxIndex int
KzgCommitment string `gorm:"NOT NULL"`
KzgProof string `gorm:"NOT NULL"`
CommitmentInclusionProof string `gorm:"NOT NULL"`
Expand Down
69 changes: 47 additions & 22 deletions external/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/big"
"strconv"

types2 "github.com/bnb-chain/blob-hub/types"
"github.com/bnb-chain/blob-hub/util"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -18,7 +20,7 @@ import (
const BSCBlockConfirmNum = 3

type IClient interface {
GetBlob(ctx context.Context, blockID uint64) ([]*structs.Sidecar, error)
GetBlob(ctx context.Context, blockID uint64) ([]*types2.GeneralSideCar, error)
GetBlockHeader(ctx context.Context, height uint64) (*types.Header, error)
GetFinalizedBlockNum(ctx context.Context) (uint64, error)
BlockByNumber(ctx context.Context, int2 *big.Int) (*types.Block, error)
Expand Down Expand Up @@ -61,32 +63,54 @@ func NewClient(cfg *config.SyncerConfig) IClient {
return cli
}

func (c *Client) GetBlob(ctx context.Context, blockID uint64) ([]*structs.Sidecar, error) {
func (c *Client) GetBlob(ctx context.Context, blockID uint64) ([]*types2.GeneralSideCar, error) {
sidecars := make([]*types2.GeneralSideCar, 0)
if c.cfg.Chain == config.BSC {
var r []*BlobTxSidecar
var txSidecars []*BSCBlobTxSidecar
number := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockID))
err := c.rpcClient.CallContext(ctx, &r, "eth_getBlobSidecars", number.String())
if err == nil && r == nil {
err := c.rpcClient.CallContext(ctx, &txSidecars, "eth_getBlobSidecars", number.String())
if err != nil {
return nil, err
}
if txSidecars == nil {
return nil, ethereum.NotFound
}
sidecars := make([]*structs.Sidecar, 0)
idx := 0
for _, b := range r {
for j := range b.BlobSidecar.Blobs {
for _, txSidecar := range txSidecars {
txIndex, err := util.HexToUint64(txSidecar.TxIndex)
if err != nil {
return nil, err
}
for j := range txSidecar.BlobSidecar.Blobs {
sidecars = append(sidecars,
&structs.Sidecar{
Index: strconv.Itoa(idx),
Blob: b.BlobSidecar.Blobs[j],
KzgCommitment: b.BlobSidecar.Commitments[j],
KzgProof: b.BlobSidecar.Proofs[j],
&types2.GeneralSideCar{
Sidecar: structs.Sidecar{
Index: strconv.Itoa(idx),
Blob: txSidecar.BlobSidecar.Blobs[j],
KzgCommitment: txSidecar.BlobSidecar.Commitments[j],
KzgProof: txSidecar.BlobSidecar.Proofs[j],
},
TxIndex: int64(txIndex),
TxHash: txSidecar.TxHash,
},
)
idx++
}
}
return sidecars, err
}
return c.beaconClient.GetBlob(ctx, blockID)
ethSidecars, err := c.beaconClient.GetBlob(ctx, blockID)
if err != nil {
return nil, err
}
for _, sidecar := range ethSidecars {
sidecars = append(sidecars,
&types2.GeneralSideCar{
Sidecar: *sidecar,
},
)
}
return sidecars, nil
}

func (c *Client) GetBlockHeader(ctx context.Context, height uint64) (*types.Header, error) {
Expand Down Expand Up @@ -124,17 +148,18 @@ func (c *Client) GetBeaconBlock(ctx context.Context, slotNumber uint64) (*struct
return c.beaconClient.GetBeaconBlock(ctx, slotNumber)
}

// Define the Go structs to match the JSON structure
type BlobSidecar struct {
// BSCBlobSidecar is a sidecar struct for BSC
type BSCBlobSidecar struct {
Blobs []string `json:"blobs"`
Commitments []string `json:"commitments"`
Proofs []string `json:"proofs"`
}

type BlobTxSidecar struct {
BlobSidecar BlobSidecar `json:"blobSidecar"`
BlockNumber string `json:"blockNumber"`
BlockHash string `json:"blockHash"`
TxIndex string `json:"txIndex"`
TxHash string `json:"txHash"`
// BSCBlobTxSidecar is a sidecar struct for BSC blob tx
type BSCBlobTxSidecar struct {
BlobSidecar BSCBlobSidecar `json:"blobSidecar"`
BlockNumber string `json:"blockNumber"`
BlockHash string `json:"blockHash"`
TxIndex string `json:"txIndex"`
TxHash string `json:"txHash"`
}
56 changes: 56 additions & 0 deletions models/b_s_c_blob_sidecar.go

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

116 changes: 116 additions & 0 deletions models/b_s_c_blob_tx_sidecar.go

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

55 changes: 55 additions & 0 deletions models/rpc_error.go

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

Loading

0 comments on commit 4e1845e

Please sign in to comment.