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

Commit

Permalink
feat(sender): add sender.GetOpts method (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Mar 7, 2024
1 parent 1b21e4c commit 2644e60
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
18 changes: 9 additions & 9 deletions internal/sender/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ func (s *Sender) adjustGas(txData types.TxData) {
blobFeeCap = utils.Min(blobFeeCap, s.MaxBlobFee)
baseTx.BlobFeeCap = uint256.NewInt(blobFeeCap)
default:
log.Warn("Unsupported transaction type when adjust gas fee", "from", s.Opts.From)
log.Warn("Unsupported transaction type when adjust gas fee", "from", s.opts.From)
}
}

// SetNonce adjusts the nonce of the given transaction with the current nonce of the sender.
func (s *Sender) SetNonce(txData types.TxData, adjust bool) (err error) {
var nonce uint64
if adjust {
s.nonce, err = s.client.NonceAt(s.ctx, s.Opts.From, nil)
s.nonce, err = s.client.NonceAt(s.ctx, s.opts.From, nil)
if err != nil {
log.Warn("Failed to get the nonce", "from", s.Opts.From, "err", err)
log.Warn("Failed to get the nonce", "from", s.opts.From, "err", err)
return err
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ func (s *Sender) updateGasTipGasFee(head *types.Header) error {
gasTipCap = new(big.Int).Set(maxGasFee)
}

s.Opts.GasTipCap = gasTipCap
s.Opts.GasFeeCap = gasFeeCap
s.opts.GasTipCap = gasTipCap
s.opts.GasFeeCap = gasFeeCap

return nil
}
Expand All @@ -111,8 +111,8 @@ func (s *Sender) buildTxData(tx *types.Transaction) (types.TxData, error) {
ChainID: s.client.ChainID,
To: tx.To(),
Nonce: tx.Nonce(),
GasFeeCap: s.Opts.GasFeeCap,
GasTipCap: s.Opts.GasTipCap,
GasFeeCap: s.opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
Gas: tx.Gas(),
Value: tx.Value(),
Data: tx.Data(),
Expand All @@ -127,8 +127,8 @@ func (s *Sender) buildTxData(tx *types.Transaction) (types.TxData, error) {
ChainID: uint256.MustFromBig(s.client.ChainID),
To: to,
Nonce: tx.Nonce(),
GasFeeCap: uint256.MustFromBig(s.Opts.GasFeeCap),
GasTipCap: uint256.MustFromBig(s.Opts.GasTipCap),
GasFeeCap: uint256.MustFromBig(s.opts.GasFeeCap),
GasTipCap: uint256.MustFromBig(s.opts.GasTipCap),
Gas: tx.Gas(),
Value: uint256.MustFromBig(tx.Value()),
Data: tx.Data(),
Expand Down
32 changes: 24 additions & 8 deletions internal/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type Sender struct {
client *rpc.EthClient

nonce uint64
Opts *bind.TransactOpts
opts *bind.TransactOpts

unconfirmedTxs cmap.ConcurrentMap[string, *TxToConfirm]
txToConfirmCh cmap.ConcurrentMap[string, chan *TxToConfirm]
Expand Down Expand Up @@ -129,7 +129,7 @@ func NewSender(ctx context.Context, cfg *Config, client *rpc.EthClient, priv *ec
head: head,
client: client,
nonce: nonce,
Opts: opts,
opts: opts,
unconfirmedTxs: cmap.New[*TxToConfirm](),
txToConfirmCh: cmap.New[chan *TxToConfirm](),
stopCh: make(chan struct{}),
Expand All @@ -154,6 +154,22 @@ func (s *Sender) Close() {
s.wg.Wait()
}

// GetOpts returns the transaction options of the sender.
func (s *Sender) GetOpts() *bind.TransactOpts {
return &bind.TransactOpts{
From: s.opts.From,
Nonce: s.opts.Nonce,
Signer: s.opts.Signer,
Value: s.opts.Value,
GasPrice: s.opts.GasPrice,
GasFeeCap: s.opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
GasLimit: s.opts.GasLimit,
Context: s.opts.Context,
NoSend: s.opts.NoSend,
}
}

// TxToConfirmChannel returns a channel to wait the given transaction's confirmation.
func (s *Sender) TxToConfirmChannel(txID string) <-chan *TxToConfirm {
ch, ok := s.txToConfirmCh.Get(txID)
Expand Down Expand Up @@ -191,12 +207,12 @@ func (s *Sender) SendRawTransaction(nonce uint64, target *common.Address, value
if gasLimit == 0 {
var err error
gasLimit, err = s.client.EstimateGas(s.ctx, ethereum.CallMsg{
From: s.Opts.From,
From: s.opts.From,
To: target,
Value: value,
Data: data,
GasTipCap: s.Opts.GasTipCap,
GasFeeCap: s.Opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
GasFeeCap: s.opts.GasFeeCap,
})
if err != nil {
return "", err
Expand All @@ -210,8 +226,8 @@ func (s *Sender) SendRawTransaction(nonce uint64, target *common.Address, value
ChainID: s.client.ChainID,
To: target,
Nonce: nonce,
GasFeeCap: s.Opts.GasFeeCap,
GasTipCap: s.Opts.GasTipCap,
GasFeeCap: s.opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
Gas: gasLimit,
Value: value,
Data: data,
Expand Down Expand Up @@ -285,7 +301,7 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {

for i := 0; i < nonceIncorrectRetrys; i++ {
// Retry when nonce is incorrect
rawTx, err := s.Opts.Signer(s.Opts.From, types.NewTx(originalTx))
rawTx, err := s.opts.Signer(s.opts.From, types.NewTx(originalTx))
if err != nil {
return err
}
Expand Down
18 changes: 10 additions & 8 deletions internal/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type SenderTestSuite struct {

func (s *SenderTestSuite) TestSendTransaction() {
var (
opts = s.sender.Opts
opts = s.sender.GetOpts()
client = s.RPCClient.L1
eg errgroup.Group
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func (s *SenderTestSuite) TestSendTransaction() {
}

func (s *SenderTestSuite) TestSendRawTransaction() {
nonce, err := s.RPCClient.L1.NonceAt(context.Background(), s.sender.Opts.From, nil)
nonce, err := s.RPCClient.L1.NonceAt(context.Background(), s.sender.GetOpts().From, nil)
s.Nil(err)

var eg errgroup.Group
Expand All @@ -82,14 +82,15 @@ func (s *SenderTestSuite) TestSendRawTransaction() {
func (s *SenderTestSuite) TestReplacement() {
send := s.sender
client := s.RPCClient.L1
opts := send.GetOpts()

// Let max gas price be 2 times of the gas fee cap.
send.MaxGasFee = send.Opts.GasFeeCap.Uint64() * 2
send.MaxGasFee = opts.GasFeeCap.Uint64() * 2

nonce, err := client.NonceAt(context.Background(), send.Opts.From, nil)
nonce, err := client.NonceAt(context.Background(), opts.From, nil)
s.Nil(err)

pendingNonce, err := client.PendingNonceAt(context.Background(), send.Opts.From)
pendingNonce, err := client.PendingNonceAt(context.Background(), opts.From)
s.Nil(err)
// Run test only if mempool has no pending transactions.
if pendingNonce > nonce {
Expand All @@ -107,7 +108,7 @@ func (s *SenderTestSuite) TestReplacement() {
Value: big.NewInt(1),
Data: nil,
}
rawTx, err := send.Opts.Signer(send.Opts.From, types.NewTx(baseTx))
rawTx, err := send.GetOpts().Signer(send.GetOpts().From, types.NewTx(baseTx))
s.Nil(err)
err = client.SendTransaction(context.Background(), rawTx)
s.Nil(err)
Expand Down Expand Up @@ -138,10 +139,11 @@ func (s *SenderTestSuite) TestReplacement() {
func (s *SenderTestSuite) TestNonceTooLow() {
client := s.RPCClient.L1
send := s.sender
opts := s.sender.GetOpts()

nonce, err := client.NonceAt(context.Background(), send.Opts.From, nil)
nonce, err := client.NonceAt(context.Background(), opts.From, nil)
s.Nil(err)
pendingNonce, err := client.PendingNonceAt(context.Background(), send.Opts.From)
pendingNonce, err := client.PendingNonceAt(context.Background(), opts.From)
s.Nil(err)
// Run test only if mempool has no pending transactions.
if pendingNonce > nonce {
Expand Down
4 changes: 2 additions & 2 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (p *Proposer) makeBlobProposeBlockTx(
return nil, err
}

opts := p.sender.Opts
opts := p.sender.GetOpts()
opts.Value = maxFee
rawTx, err := p.rpc.TaikoL1.ProposeBlock(
opts,
Expand Down Expand Up @@ -375,7 +375,7 @@ func (p *Proposer) makeCalldataProposeBlockTx(
return nil, err
}

opts := p.sender.Opts
opts := p.sender.GetOpts()
opts.Value = maxFee

var parentMetaHash = [32]byte{}
Expand Down
2 changes: 1 addition & 1 deletion proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
s.Nil(sender.SetNonce(nil, true))

fee := big.NewInt(10000)
opts := sender.Opts
opts := sender.GetOpts()
opts.Value = fee
s.Greater(opts.GasTipCap.Uint64(), uint64(0))

Expand Down

0 comments on commit 2644e60

Please sign in to comment.