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

Commit 05100b3

Browse files
authored
feat(pkg): move sender from internal to pkg (#626)
1 parent 0aaf06b commit 05100b3

19 files changed

+149
-70
lines changed

driver/txlist_fetcher/blob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (d *BlobFetcher) Fetch(
3131
return nil, errBlobUnused
3232
}
3333

34-
sidecars, err := d.rpc.GetBlobs(ctx, new(big.Int).SetUint64(meta.L1Height+1))
34+
sidecars, err := d.rpc.L1Beacon.GetBlobs(ctx, new(big.Int).SetUint64(meta.L1Height+1))
3535
if err != nil {
3636
return nil, err
3737
}

internal/testutils/interfaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/ethereum/go-ethereum/core/types"
77

88
"github.com/taikoxyz/taiko-client/cmd/utils"
9-
"github.com/taikoxyz/taiko-client/internal/sender"
9+
"github.com/taikoxyz/taiko-client/pkg/sender"
1010
)
1111

1212
type CalldataSyncer interface {

pkg/rpc/beaconclient.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package rpc
2+
3+
import (
4+
"context"
5+
"crypto/sha256"
6+
"encoding/json"
7+
"errors"
8+
"fmt"
9+
"math/big"
10+
"time"
11+
12+
"github.com/ethereum/go-ethereum/common"
13+
"github.com/ethereum/go-ethereum/crypto/kzg4844"
14+
"github.com/prysmaticlabs/prysm/v4/api/client"
15+
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"
16+
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob"
17+
)
18+
19+
var (
20+
// Request urls.
21+
sidecarsRequestURL = "eth/v1/beacon/blob_sidecars/%d"
22+
)
23+
24+
type BeaconClient struct {
25+
*beacon.Client
26+
27+
timeout time.Duration
28+
}
29+
30+
// NewBeaconClient returns a new beacon client.
31+
func NewBeaconClient(endpoint string, timeout time.Duration) (*BeaconClient, error) {
32+
cli, err := beacon.NewClient(endpoint, client.WithTimeout(timeout))
33+
if err != nil {
34+
return nil, err
35+
}
36+
return &BeaconClient{cli, timeout}, nil
37+
}
38+
39+
// GetBlobs returns the sidecars for a given slot.
40+
func (c *BeaconClient) GetBlobs(ctx context.Context, slot *big.Int) ([]*blob.Sidecar, error) {
41+
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
42+
defer cancel()
43+
44+
var sidecars *blob.SidecarsResponse
45+
resBytes, err := c.Get(ctxWithTimeout, fmt.Sprintf(sidecarsRequestURL, slot))
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
51+
}
52+
53+
// GetBlobByHash returns the sidecars for a given slot.
54+
func (c *BeaconClient) GetBlobByHash(ctx context.Context, slot *big.Int, blobHash common.Hash) ([]byte, error) {
55+
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
56+
defer cancel()
57+
58+
sidecars, err := c.GetBlobs(ctxWithTimeout, slot)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
for _, sidecar := range sidecars {
64+
commitment := kzg4844.Commitment(common.FromHex(sidecar.KzgCommitment))
65+
if kzg4844.CalcBlobHashV1(
66+
sha256.New(),
67+
&commitment,
68+
) == blobHash {
69+
return DecodeBlob(common.FromHex(sidecar.Blob))
70+
}
71+
}
72+
73+
return nil, errors.New("sidecar not found")
74+
}

pkg/rpc/client.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77

88
"github.com/ethereum/go-ethereum/accounts/abi/bind"
99
"github.com/ethereum/go-ethereum/common"
10-
"github.com/prysmaticlabs/prysm/v4/api/client"
11-
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"
12-
1310
"github.com/taikoxyz/taiko-client/bindings"
1411
)
1512

@@ -26,7 +23,7 @@ type Client struct {
2623
// Geth Engine API clients
2724
L2Engine *EngineClient
2825
// Beacon clients
29-
L1Beacon *beacon.Client
26+
L1Beacon *BeaconClient
3027
// Protocol contracts clients
3128
TaikoL1 *bindings.TaikoL1Client
3229
TaikoL2 *bindings.TaikoL2Client
@@ -113,9 +110,9 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
113110
}
114111
}
115112

116-
var l1BeaconClient *beacon.Client
113+
var l1BeaconClient *BeaconClient
117114
if cfg.L1BeaconEndpoint != "" {
118-
if l1BeaconClient, err = beacon.NewClient(cfg.L1BeaconEndpoint, client.WithTimeout(defaultTimeout)); err != nil {
115+
if l1BeaconClient, err = NewBeaconClient(cfg.L1BeaconEndpoint, defaultTimeout); err != nil {
119116
return nil, err
120117
}
121118
}

pkg/rpc/methods.go

-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rpc
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"math/big"
@@ -16,7 +15,6 @@ import (
1615
"github.com/ethereum/go-ethereum/core/rawdb"
1716
"github.com/ethereum/go-ethereum/core/types"
1817
"github.com/ethereum/go-ethereum/log"
19-
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob"
2018
"golang.org/x/sync/errgroup"
2119

2220
"github.com/taikoxyz/taiko-client/bindings"
@@ -29,9 +27,6 @@ var (
2927
errEmptyTiersList = errors.New("empty proof tiers list in protocol")
3028
waitL1OriginPollingInterval = 3 * time.Second
3129
defaultWaitL1OriginTimeout = 3 * time.Minute
32-
33-
// Request urls.
34-
sidecarsRequestURL = "eth/v1/beacon/blob_sidecars/%d"
3530
)
3631

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

697692
return tiers, nil
698693
}
699-
700-
// GetBlobs fetches blobs by the given slot from a L1 consensus client.
701-
func (c *Client) GetBlobs(ctx context.Context, slot *big.Int) ([]*blob.Sidecar, error) {
702-
var sidecars *blob.SidecarsResponse
703-
resBytes, err := c.L1Beacon.Get(ctx, fmt.Sprintf(sidecarsRequestURL, slot))
704-
if err != nil {
705-
return nil, err
706-
}
707-
708-
return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
709-
}

pkg/rpc/tx_blob.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func (c *EthClient) TransactBlobTx(
2727
return nil, errors.New("no signer to authorize the transaction with")
2828
}
2929
// Create blob tx
30-
rawTx, err := c.createBlobTx(opts, contract, input, sidecar)
30+
blobTx, err := c.CreateBlobTx(opts, contract, input, sidecar)
3131
if err != nil {
3232
return nil, err
3333
}
34-
signedTx, err := opts.Signer(opts.From, rawTx)
34+
signedTx, err := opts.Signer(opts.From, types.NewTx(blobTx))
3535
if err != nil {
3636
return nil, err
3737
}
@@ -44,13 +44,14 @@ func (c *EthClient) TransactBlobTx(
4444
return signedTx, nil
4545
}
4646

47-
// createBlobTx creates a blob transaction by given parameters.
48-
func (c *EthClient) createBlobTx(
47+
// CreateBlobTx creates a blob transaction by given parameters.
48+
func (c *EthClient) CreateBlobTx(
4949
opts *bind.TransactOpts,
5050
contract common.Address,
5151
input []byte,
5252
sidecar *types.BlobTxSidecar,
53-
) (*types.Transaction, error) {
53+
) (*types.BlobTx, error) {
54+
// Fetch the nonce for the account
5455
var (
5556
nonce *hexutil.Uint64
5657
gas *hexutil.Uint64
@@ -88,7 +89,7 @@ func (c *EthClient) createBlobTx(
8889
return nil, err
8990
}
9091

91-
blobTx := &types.BlobTx{
92+
return &types.BlobTx{
9293
ChainID: uint256.MustFromBig(rawTx.ChainId()),
9394
Nonce: rawTx.Nonce(),
9495
GasTipCap: uint256.MustFromBig(rawTx.GasTipCap()),
@@ -101,9 +102,7 @@ func (c *EthClient) createBlobTx(
101102
BlobFeeCap: uint256.MustFromBig(rawTx.BlobGasFeeCap()),
102103
BlobHashes: sidecar.BlobHashes(),
103104
Sidecar: sidecar,
104-
}
105-
106-
return types.NewTx(blobTx), nil
105+
}, nil
107106
}
108107

109108
// MakeSidecar makes a sidecar which only includes one blob with the given data.
File renamed without changes.

internal/sender/sender.go pkg/sender/sender.go

+44-20
Original file line numberDiff line numberDiff line change
@@ -198,38 +198,62 @@ func (s *Sender) GetUnconfirmedTx(txID string) *types.Transaction {
198198
}
199199

200200
// SendRawTransaction sends a transaction to the given Ethereum node.
201-
func (s *Sender) SendRawTransaction(nonce uint64, target *common.Address, value *big.Int, data []byte) (string, error) {
201+
func (s *Sender) SendRawTransaction(
202+
nonce uint64,
203+
target *common.Address,
204+
value *big.Int,
205+
data []byte,
206+
sidecar *types.BlobTxSidecar,
207+
) (string, error) {
202208
if s.unconfirmedTxs.Count() >= unconfirmedTxsCap {
203209
return "", fmt.Errorf("too many pending transactions")
204210
}
205211

206-
gasLimit := s.GasLimit
207-
if gasLimit == 0 {
208-
var err error
209-
gasLimit, err = s.client.EstimateGas(s.ctx, ethereum.CallMsg{
210-
From: s.opts.From,
211-
To: target,
212-
Value: value,
213-
Data: data,
214-
GasTipCap: s.opts.GasTipCap,
215-
GasFeeCap: s.opts.GasFeeCap,
216-
})
212+
var (
213+
originalTx types.TxData
214+
opts = s.GetOpts()
215+
)
216+
if sidecar != nil {
217+
opts.Value = value
218+
if target == nil {
219+
target = &common.Address{}
220+
}
221+
blobTx, err := s.client.CreateBlobTx(opts, *target, data, sidecar)
217222
if err != nil {
218223
return "", err
219224
}
220-
}
221-
222-
txToConfirm := &TxToConfirm{
223-
originalTx: &types.DynamicFeeTx{
225+
blobTx.Nonce = setDefault(nonce, s.nonce)
226+
originalTx = blobTx
227+
} else {
228+
gasLimit := s.GasLimit
229+
if gasLimit == 0 {
230+
var err error
231+
gasLimit, err = s.client.EstimateGas(s.ctx, ethereum.CallMsg{
232+
From: opts.From,
233+
To: target,
234+
Value: value,
235+
Data: data,
236+
GasTipCap: opts.GasTipCap,
237+
GasFeeCap: opts.GasFeeCap,
238+
})
239+
if err != nil {
240+
return "", err
241+
}
242+
}
243+
originalTx = &types.DynamicFeeTx{
224244
ChainID: s.client.ChainID,
225245
To: target,
226-
Nonce: nonce,
227-
GasFeeCap: s.opts.GasFeeCap,
228-
GasTipCap: s.opts.GasTipCap,
246+
Nonce: setDefault(nonce, s.nonce),
247+
GasFeeCap: opts.GasFeeCap,
248+
GasTipCap: opts.GasTipCap,
229249
Gas: gasLimit,
230250
Value: value,
231251
Data: data,
232-
},
252+
}
253+
}
254+
255+
txToConfirm := &TxToConfirm{
256+
originalTx: originalTx,
233257
}
234258

235259
if err := s.send(txToConfirm, false); err != nil && !strings.Contains(err.Error(), "replacement transaction") {

internal/sender/sender_test.go pkg/sender/sender_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"github.com/stretchr/testify/suite"
1515
"golang.org/x/sync/errgroup"
1616

17-
"github.com/taikoxyz/taiko-client/internal/sender"
1817
"github.com/taikoxyz/taiko-client/internal/testutils"
18+
"github.com/taikoxyz/taiko-client/pkg/sender"
1919
)
2020

2121
type SenderTestSuite struct {
@@ -66,7 +66,7 @@ func (s *SenderTestSuite) TestSendRawTransaction() {
6666
i := i
6767
eg.Go(func() error {
6868
addr := common.BigToAddress(big.NewInt(int64(i)))
69-
_, err := s.sender.SendRawTransaction(nonce+uint64(i), &addr, big.NewInt(1), nil)
69+
_, err := s.sender.SendRawTransaction(nonce+uint64(i), &addr, big.NewInt(1), nil, nil)
7070
return err
7171
})
7272
}
@@ -114,12 +114,12 @@ func (s *SenderTestSuite) TestReplacement() {
114114
s.Nil(err)
115115

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

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

125125
for _, confirmCh := range send.TxToConfirmChannels() {
@@ -150,7 +150,7 @@ func (s *SenderTestSuite) TestNonceTooLow() {
150150
return
151151
}
152152

153-
txID, err := send.SendRawTransaction(nonce-3, &common.Address{}, big.NewInt(1), nil)
153+
txID, err := send.SendRawTransaction(nonce-3, &common.Address{}, big.NewInt(1), nil, nil)
154154
s.Nil(err)
155155
confirm := <-send.TxToConfirmChannel(txID)
156156
s.Nil(confirm.Err)

proposer/proposer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
"github.com/ethereum/go-ethereum/crypto"
1616
"github.com/ethereum/go-ethereum/log"
1717
"github.com/ethereum/go-ethereum/rlp"
18+
1819
"github.com/taikoxyz/taiko-client/bindings"
1920
"github.com/taikoxyz/taiko-client/bindings/encoding"
2021
"github.com/taikoxyz/taiko-client/internal/metrics"
21-
"github.com/taikoxyz/taiko-client/internal/sender"
2222
"github.com/taikoxyz/taiko-client/pkg/rpc"
23+
"github.com/taikoxyz/taiko-client/pkg/sender"
2324
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
2425
builder "github.com/taikoxyz/taiko-client/proposer/transaction_builder"
2526
"github.com/urfave/cli/v2"

proposer/proposer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
159159
nonce, err := s.RPCClient.L1.PendingNonceAt(context.Background(), s.p.proposerAddress)
160160
s.Nil(err)
161161

162-
txID, err := sender.SendRawTransaction(nonce, &common.Address{}, common.Big1, nil)
162+
txID, err := sender.SendRawTransaction(nonce, &common.Address{}, common.Big1, nil, nil)
163163
s.Nil(err)
164164
tx := sender.GetUnconfirmedTx(txID)
165165

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

177177
s.Nil(err)
178178

179-
txID, err = sender.SendRawTransaction(nonce, newTx.To(), newTx.Value(), newTx.Data())
179+
txID, err = sender.SendRawTransaction(nonce, newTx.To(), newTx.Value(), newTx.Data(), nil)
180180
s.Nil(err)
181181
newTx = sender.GetUnconfirmedTx(txID)
182182

proposer/transaction_builder/common_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/stretchr/testify/suite"
1313

1414
"github.com/taikoxyz/taiko-client/bindings/encoding"
15-
"github.com/taikoxyz/taiko-client/internal/sender"
1615
"github.com/taikoxyz/taiko-client/internal/testutils"
16+
"github.com/taikoxyz/taiko-client/pkg/sender"
1717
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
1818
)
1919

prover/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/ethereum/go-ethereum/log"
1111

1212
"github.com/taikoxyz/taiko-client/bindings/encoding"
13-
"github.com/taikoxyz/taiko-client/internal/sender"
1413
"github.com/taikoxyz/taiko-client/pkg/rpc"
14+
"github.com/taikoxyz/taiko-client/pkg/sender"
1515
handler "github.com/taikoxyz/taiko-client/prover/event_handler"
1616
proofProducer "github.com/taikoxyz/taiko-client/prover/proof_producer"
1717
proofSubmitter "github.com/taikoxyz/taiko-client/prover/proof_submitter"

0 commit comments

Comments
 (0)