Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(pkg): move sender from internal to pkg (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Mar 11, 2024
1 parent 0aaf06b commit 05100b3
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 70 deletions.
2 changes: 1 addition & 1 deletion driver/txlist_fetcher/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (d *BlobFetcher) Fetch(
return nil, errBlobUnused
}

sidecars, err := d.rpc.GetBlobs(ctx, new(big.Int).SetUint64(meta.L1Height+1))
sidecars, err := d.rpc.L1Beacon.GetBlobs(ctx, new(big.Int).SetUint64(meta.L1Height+1))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/testutils/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"

"github.com/taikoxyz/taiko-client/cmd/utils"
"github.com/taikoxyz/taiko-client/internal/sender"
"github.com/taikoxyz/taiko-client/pkg/sender"
)

type CalldataSyncer interface {
Expand Down
74 changes: 74 additions & 0 deletions pkg/rpc/beaconclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package rpc

import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/prysmaticlabs/prysm/v4/api/client"
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob"
)

var (
// Request urls.
sidecarsRequestURL = "eth/v1/beacon/blob_sidecars/%d"
)

type BeaconClient struct {
*beacon.Client

timeout time.Duration
}

// NewBeaconClient returns a new beacon client.
func NewBeaconClient(endpoint string, timeout time.Duration) (*BeaconClient, error) {
cli, err := beacon.NewClient(endpoint, client.WithTimeout(timeout))
if err != nil {
return nil, err
}
return &BeaconClient{cli, timeout}, nil
}

// GetBlobs returns the sidecars for a given slot.
func (c *BeaconClient) GetBlobs(ctx context.Context, slot *big.Int) ([]*blob.Sidecar, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
defer cancel()

var sidecars *blob.SidecarsResponse
resBytes, err := c.Get(ctxWithTimeout, fmt.Sprintf(sidecarsRequestURL, slot))
if err != nil {
return nil, err
}

return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
}

// GetBlobByHash returns the sidecars for a given slot.
func (c *BeaconClient) GetBlobByHash(ctx context.Context, slot *big.Int, blobHash common.Hash) ([]byte, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
defer cancel()

sidecars, err := c.GetBlobs(ctxWithTimeout, slot)
if err != nil {
return nil, err
}

for _, sidecar := range sidecars {
commitment := kzg4844.Commitment(common.FromHex(sidecar.KzgCommitment))
if kzg4844.CalcBlobHashV1(
sha256.New(),
&commitment,
) == blobHash {
return DecodeBlob(common.FromHex(sidecar.Blob))
}
}

return nil, errors.New("sidecar not found")
}
9 changes: 3 additions & 6 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/v4/api/client"
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"

"github.com/taikoxyz/taiko-client/bindings"
)

Expand All @@ -26,7 +23,7 @@ type Client struct {
// Geth Engine API clients
L2Engine *EngineClient
// Beacon clients
L1Beacon *beacon.Client
L1Beacon *BeaconClient
// Protocol contracts clients
TaikoL1 *bindings.TaikoL1Client
TaikoL2 *bindings.TaikoL2Client
Expand Down Expand Up @@ -113,9 +110,9 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
}
}

var l1BeaconClient *beacon.Client
var l1BeaconClient *BeaconClient
if cfg.L1BeaconEndpoint != "" {
if l1BeaconClient, err = beacon.NewClient(cfg.L1BeaconEndpoint, client.WithTimeout(defaultTimeout)); err != nil {
if l1BeaconClient, err = NewBeaconClient(cfg.L1BeaconEndpoint, defaultTimeout); err != nil {
return nil, err
}
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rpc

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob"
"golang.org/x/sync/errgroup"

"github.com/taikoxyz/taiko-client/bindings"
Expand All @@ -29,9 +27,6 @@ var (
errEmptyTiersList = errors.New("empty proof tiers list in protocol")
waitL1OriginPollingInterval = 3 * time.Second
defaultWaitL1OriginTimeout = 3 * time.Minute

// Request urls.
sidecarsRequestURL = "eth/v1/beacon/blob_sidecars/%d"
)

// ensureGenesisMatched fetches the L2 genesis block from TaikoL1 contract,
Expand Down Expand Up @@ -696,14 +691,3 @@ func (c *Client) GetTiers(ctx context.Context) ([]*TierProviderTierWithID, error

return tiers, nil
}

// GetBlobs fetches blobs by the given slot from a L1 consensus client.
func (c *Client) GetBlobs(ctx context.Context, slot *big.Int) ([]*blob.Sidecar, error) {
var sidecars *blob.SidecarsResponse
resBytes, err := c.L1Beacon.Get(ctx, fmt.Sprintf(sidecarsRequestURL, slot))
if err != nil {
return nil, err
}

return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
}
17 changes: 8 additions & 9 deletions pkg/rpc/tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func (c *EthClient) TransactBlobTx(
return nil, errors.New("no signer to authorize the transaction with")
}
// Create blob tx
rawTx, err := c.createBlobTx(opts, contract, input, sidecar)
blobTx, err := c.CreateBlobTx(opts, contract, input, sidecar)
if err != nil {
return nil, err
}
signedTx, err := opts.Signer(opts.From, rawTx)
signedTx, err := opts.Signer(opts.From, types.NewTx(blobTx))
if err != nil {
return nil, err
}
Expand All @@ -44,13 +44,14 @@ func (c *EthClient) TransactBlobTx(
return signedTx, nil
}

// createBlobTx creates a blob transaction by given parameters.
func (c *EthClient) createBlobTx(
// CreateBlobTx creates a blob transaction by given parameters.
func (c *EthClient) CreateBlobTx(
opts *bind.TransactOpts,
contract common.Address,
input []byte,
sidecar *types.BlobTxSidecar,
) (*types.Transaction, error) {
) (*types.BlobTx, error) {
// Fetch the nonce for the account
var (
nonce *hexutil.Uint64
gas *hexutil.Uint64
Expand Down Expand Up @@ -88,7 +89,7 @@ func (c *EthClient) createBlobTx(
return nil, err
}

blobTx := &types.BlobTx{
return &types.BlobTx{
ChainID: uint256.MustFromBig(rawTx.ChainId()),
Nonce: rawTx.Nonce(),
GasTipCap: uint256.MustFromBig(rawTx.GasTipCap()),
Expand All @@ -101,9 +102,7 @@ func (c *EthClient) createBlobTx(
BlobFeeCap: uint256.MustFromBig(rawTx.BlobGasFeeCap()),
BlobHashes: sidecar.BlobHashes(),
Sidecar: sidecar,
}

return types.NewTx(blobTx), nil
}, nil
}

// MakeSidecar makes a sidecar which only includes one blob with the given data.
Expand Down
File renamed without changes.
64 changes: 44 additions & 20 deletions internal/sender/sender.go → pkg/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,38 +198,62 @@ func (s *Sender) GetUnconfirmedTx(txID string) *types.Transaction {
}

// SendRawTransaction sends a transaction to the given Ethereum node.
func (s *Sender) SendRawTransaction(nonce uint64, target *common.Address, value *big.Int, data []byte) (string, error) {
func (s *Sender) SendRawTransaction(
nonce uint64,
target *common.Address,
value *big.Int,
data []byte,
sidecar *types.BlobTxSidecar,
) (string, error) {
if s.unconfirmedTxs.Count() >= unconfirmedTxsCap {
return "", fmt.Errorf("too many pending transactions")
}

gasLimit := s.GasLimit
if gasLimit == 0 {
var err error
gasLimit, err = s.client.EstimateGas(s.ctx, ethereum.CallMsg{
From: s.opts.From,
To: target,
Value: value,
Data: data,
GasTipCap: s.opts.GasTipCap,
GasFeeCap: s.opts.GasFeeCap,
})
var (
originalTx types.TxData
opts = s.GetOpts()
)
if sidecar != nil {
opts.Value = value
if target == nil {
target = &common.Address{}
}
blobTx, err := s.client.CreateBlobTx(opts, *target, data, sidecar)
if err != nil {
return "", err
}
}

txToConfirm := &TxToConfirm{
originalTx: &types.DynamicFeeTx{
blobTx.Nonce = setDefault(nonce, s.nonce)
originalTx = blobTx
} else {
gasLimit := s.GasLimit
if gasLimit == 0 {
var err error
gasLimit, err = s.client.EstimateGas(s.ctx, ethereum.CallMsg{
From: opts.From,
To: target,
Value: value,
Data: data,
GasTipCap: opts.GasTipCap,
GasFeeCap: opts.GasFeeCap,
})
if err != nil {
return "", err
}
}
originalTx = &types.DynamicFeeTx{
ChainID: s.client.ChainID,
To: target,
Nonce: nonce,
GasFeeCap: s.opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
Nonce: setDefault(nonce, s.nonce),
GasFeeCap: opts.GasFeeCap,
GasTipCap: opts.GasTipCap,
Gas: gasLimit,
Value: value,
Data: data,
},
}
}

txToConfirm := &TxToConfirm{
originalTx: originalTx,
}

if err := s.send(txToConfirm, false); err != nil && !strings.Contains(err.Error(), "replacement transaction") {
Expand Down
10 changes: 5 additions & 5 deletions internal/sender/sender_test.go → pkg/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/stretchr/testify/suite"
"golang.org/x/sync/errgroup"

"github.com/taikoxyz/taiko-client/internal/sender"
"github.com/taikoxyz/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-client/pkg/sender"
)

type SenderTestSuite struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (s *SenderTestSuite) TestSendRawTransaction() {
i := i
eg.Go(func() error {
addr := common.BigToAddress(big.NewInt(int64(i)))
_, err := s.sender.SendRawTransaction(nonce+uint64(i), &addr, big.NewInt(1), nil)
_, err := s.sender.SendRawTransaction(nonce+uint64(i), &addr, big.NewInt(1), nil, nil)
return err
})
}
Expand Down Expand Up @@ -114,12 +114,12 @@ func (s *SenderTestSuite) TestReplacement() {
s.Nil(err)

// Replace the transaction with a higher nonce.
_, err = send.SendRawTransaction(nonce, &common.Address{}, big.NewInt(1), nil)
_, err = send.SendRawTransaction(nonce, &common.Address{}, big.NewInt(1), nil, nil)
s.Nil(err)

time.Sleep(time.Second * 6)
// Send a transaction with a next nonce and let all the transactions be confirmed.
_, err = send.SendRawTransaction(nonce-1, &common.Address{}, big.NewInt(1), nil)
_, err = send.SendRawTransaction(nonce-1, &common.Address{}, big.NewInt(1), nil, nil)
s.Nil(err)

for _, confirmCh := range send.TxToConfirmChannels() {
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *SenderTestSuite) TestNonceTooLow() {
return
}

txID, err := send.SendRawTransaction(nonce-3, &common.Address{}, big.NewInt(1), nil)
txID, err := send.SendRawTransaction(nonce-3, &common.Address{}, big.NewInt(1), nil, nil)
s.Nil(err)
confirm := <-send.TxToConfirmChannel(txID)
s.Nil(confirm.Err)
Expand Down
3 changes: 2 additions & 1 deletion proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"

"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/internal/metrics"
"github.com/taikoxyz/taiko-client/internal/sender"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/pkg/sender"
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
builder "github.com/taikoxyz/taiko-client/proposer/transaction_builder"
"github.com/urfave/cli/v2"
Expand Down
4 changes: 2 additions & 2 deletions proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
nonce, err := s.RPCClient.L1.PendingNonceAt(context.Background(), s.p.proposerAddress)
s.Nil(err)

txID, err := sender.SendRawTransaction(nonce, &common.Address{}, common.Big1, nil)
txID, err := sender.SendRawTransaction(nonce, &common.Address{}, common.Big1, nil, nil)
s.Nil(err)
tx := sender.GetUnconfirmedTx(txID)

Expand All @@ -176,7 +176,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {

s.Nil(err)

txID, err = sender.SendRawTransaction(nonce, newTx.To(), newTx.Value(), newTx.Data())
txID, err = sender.SendRawTransaction(nonce, newTx.To(), newTx.Value(), newTx.Data(), nil)
s.Nil(err)
newTx = sender.GetUnconfirmedTx(txID)

Expand Down
2 changes: 1 addition & 1 deletion proposer/transaction_builder/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/stretchr/testify/suite"

"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/internal/sender"
"github.com/taikoxyz/taiko-client/internal/testutils"
"github.com/taikoxyz/taiko-client/pkg/sender"
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
)

Expand Down
2 changes: 1 addition & 1 deletion prover/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethereum/go-ethereum/log"

"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/internal/sender"
"github.com/taikoxyz/taiko-client/pkg/rpc"
"github.com/taikoxyz/taiko-client/pkg/sender"
handler "github.com/taikoxyz/taiko-client/prover/event_handler"
proofProducer "github.com/taikoxyz/taiko-client/prover/proof_producer"
proofSubmitter "github.com/taikoxyz/taiko-client/prover/proof_submitter"
Expand Down
Loading

0 comments on commit 05100b3

Please sign in to comment.