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

Commit

Permalink
feat: recover rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshliu committed Sep 22, 2023
1 parent fbf22fb commit 3cfa551
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 73 deletions.
14 changes: 6 additions & 8 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package driver

import (
"context"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -48,20 +47,19 @@ func New(ctx context.Context, cfg *Config) (d *Driver, err error) {
d.syncNotify = make(chan struct{}, 1)
d.ctx = ctx
d.backOffRetryInterval = cfg.BackOffRetryInterval
ep, err := EndpointFromConfig(ctx, cfg)
d.rpc, err = EndpointFromConfig(ctx, cfg)
if err != nil {
return nil, err
}
peers, err := ep.L2.PeerCount(ctx)
if d.state, err = state.New(d.ctx, d.rpc); err != nil {
return nil, err
}
peers, err := d.rpc.L2.PeerCount(ctx)
if err != nil {
return nil, err
}
if cfg.P2PSyncVerifiedBlocks && peers == 0 {
fmt.Printf("P2P syncing verified blocks enabled, but no connected peer found in L2 execution engine")
}

if d.state, err = state.New(d.ctx, d.rpc); err != nil {
return nil, err
log.Warn("P2P syncing verified blocks enabled, but no connected peer found in L2 execution engine")
}

signalServiceAddress, err := d.rpc.TaikoL1.Resolve0(
Expand Down
37 changes: 18 additions & 19 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var (

// Proposer keep proposing new transactions from L2 execution engine's tx pool at a fixed interval.
type Proposer struct {
// RPC clients
RPC *rpc.Client
// rpc clients
rpc *rpc.Client

// Private keys and account addresses
l1ProposerPrivKey *ecdsa.PrivateKey
Expand Down Expand Up @@ -76,13 +76,12 @@ type Proposer struct {

// New initializes the proposer instance based on the given configurations.
func New(ctx context.Context, cfg *Config) (p *Proposer, err error) {
ep, err := EndpointFromConfig(ctx, cfg)
p = &Proposer{}
p.rpc, err = EndpointFromConfig(ctx, cfg)
if err != nil {
return nil, err
}
p = &Proposer{
RPC: ep,
}

p.l1ProposerPrivKey = cfg.L1ProposerPrivKey
p.l1ProposerAddress = crypto.PubkeyToAddress(p.l1ProposerPrivKey.PublicKey)
p.l2SuggestedFeeRecipient = cfg.L2SuggestedFeeRecipient
Expand All @@ -100,7 +99,7 @@ func New(ctx context.Context, cfg *Config) (p *Proposer, err error) {
p.cfg = cfg

// Protocol configs
protocolConfigs, err := p.RPC.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
protocolConfigs, err := p.rpc.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, fmt.Errorf("failed to get protocol configs: %w", err)
}
Expand All @@ -110,7 +109,7 @@ func New(ctx context.Context, cfg *Config) (p *Proposer, err error) {

if p.proverSelector, err = selector.NewETHFeeEOASelector(
&protocolConfigs,
p.RPC,
p.rpc,
cfg.TaikoL1Address,
cfg.BlockProposalFee,
cfg.BlockProposalFeeIncreasePercentage,
Expand Down Expand Up @@ -190,18 +189,18 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
}

// Wait until L2 execution engine is synced at first.
if err := p.RPC.WaitTillL2ExecutionEngineSynced(ctx); err != nil {
if err := p.rpc.WaitTillL2ExecutionEngineSynced(ctx); err != nil {
return fmt.Errorf("failed to wait until L2 execution engine synced: %w", err)
}

log.Info("Start fetching L2 execution engine's transaction pool content")

l2Head, err := p.RPC.L2.HeaderByNumber(ctx, nil)
l2Head, err := p.rpc.L2.HeaderByNumber(ctx, nil)
if err != nil {
return err
}

baseFee, err := p.RPC.TaikoL2.GetBasefee(
baseFee, err := p.rpc.TaikoL2.GetBasefee(
&bind.CallOpts{Context: ctx},
uint64(time.Now().Unix())-l2Head.Time,
uint32(l2Head.GasUsed),
Expand All @@ -212,7 +211,7 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {

log.Info("Current base fee", "fee", baseFee)

txLists, err := p.RPC.GetPoolContent(
txLists, err := p.rpc.GetPoolContent(
ctx,
p.L2SuggestedFeeRecipient(),
baseFee,
Expand All @@ -228,7 +227,7 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
if p.localsOnly {
var (
localTxsLists []types.Transactions
signer = types.LatestSignerForChainID(p.RPC.L2ChainID)
signer = types.LatestSignerForChainID(p.rpc.L2ChainID)
)
for _, txs := range txLists {
var filtered types.Transactions
Expand Down Expand Up @@ -258,11 +257,11 @@ func (p *Proposer) ProposeOp(ctx context.Context) error {
return errNoNewTxs
}

head, err := p.RPC.L1.BlockNumber(ctx)
head, err := p.rpc.L1.BlockNumber(ctx)
if err != nil {
return err
}
nonce, err := p.RPC.L1.NonceAt(
nonce, err := p.rpc.L1.NonceAt(
ctx,
crypto.PubkeyToAddress(p.l1ProposerPrivKey.PublicKey),
new(big.Int).SetUint64(head),
Expand Down Expand Up @@ -330,7 +329,7 @@ func (p *Proposer) sendProposeBlockTx(
if err != nil {
return nil, err
}
opts, err := getTxOpts(ctx, p.RPC.L1, p.l1ProposerPrivKey, p.RPC.L1ChainID, fee)
opts, err := getTxOpts(ctx, p.rpc.L1, p.l1ProposerPrivKey, p.rpc.L1ChainID, fee)
if err != nil {
return nil, err
}
Expand All @@ -342,7 +341,7 @@ func (p *Proposer) sendProposeBlockTx(
}
if isReplacement {
log.Info("Try replacing a transaction with same nonce", "sender", p.l1ProposerAddress, "nonce", nonce)
originalTx, err := rpc.GetPendingTxByNonce(ctx, p.RPC, p.l1ProposerAddress, *nonce)
originalTx, err := rpc.GetPendingTxByNonce(ctx, p.rpc, p.l1ProposerAddress, *nonce)
if err != nil || originalTx == nil {
log.Warn(
"Original transaction not found",
Expand Down Expand Up @@ -378,7 +377,7 @@ func (p *Proposer) sendProposeBlockTx(
}
}

proposeTx, err := p.RPC.TaikoL1.ProposeBlock(opts, inputs, assignment, txListBytes)
proposeTx, err := p.rpc.TaikoL1.ProposeBlock(opts, inputs, assignment, txListBytes)
if err != nil {
return nil, encoding.TryParsingCustomError(err)
}
Expand Down Expand Up @@ -437,7 +436,7 @@ func (p *Proposer) ProposeTxList(
ctxWithTimeout, cancel := context.WithTimeout(ctx, p.waitReceiptTimeout)
defer cancel()

if _, err := rpc.WaitReceipt(ctxWithTimeout, p.RPC.L1, tx); err != nil {
if _, err := rpc.WaitReceipt(ctxWithTimeout, p.rpc.L1, tx); err != nil {
return err
}

Expand Down
18 changes: 9 additions & 9 deletions proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ func (s *ProposerTestSuite) TestProposeOp() {
// Propose txs in L2 execution engine's mempool
sink := make(chan *bindings.TaikoL1ClientBlockProposed)

sub, err := s.p.RPC.TaikoL1.WatchBlockProposed(nil, sink, nil, nil)
sub, err := s.p.rpc.TaikoL1.WatchBlockProposed(nil, sink, nil, nil)
s.Nil(err)
defer func() {
sub.Unsubscribe()
close(sink)
}()

nonce, err := s.p.RPC.L2.PendingNonceAt(context.Background(), s.TestAddr)
nonce, err := s.p.rpc.L2.PendingNonceAt(context.Background(), s.TestAddr)
s.Nil(err)

gaslimit := 21000

parent, err := s.p.RPC.L2.BlockByNumber(context.Background(), nil)
parent, err := s.p.rpc.L2.BlockByNumber(context.Background(), nil)
s.Nil(err)

baseFee, err := s.p.RPC.TaikoL2.GetBasefee(nil, 1, uint32(parent.GasUsed()))
baseFee, err := s.p.rpc.TaikoL2.GetBasefee(nil, 1, uint32(parent.GasUsed()))
s.Nil(err)

to := common.BytesToAddress(testutils.RandomBytes(32))
Expand All @@ -91,20 +91,20 @@ func (s *ProposerTestSuite) TestProposeOp() {
Value: common.Big1,
})

signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.p.RPC.L2ChainID), s.TestAddrPrivKey)
signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(s.p.rpc.L2ChainID), s.TestAddrPrivKey)
s.Nil(err)
s.Nil(s.p.RPC.L2.SendTransaction(context.Background(), signedTx))
s.Nil(s.p.rpc.L2.SendTransaction(context.Background(), signedTx))

s.Nil(s.p.ProposeOp(context.Background()))

event := <-sink

_, isPending, err := s.p.RPC.L1.TransactionByHash(context.Background(), event.Raw.TxHash)
_, isPending, err := s.p.rpc.L1.TransactionByHash(context.Background(), event.Raw.TxHash)
s.Nil(err)
s.False(isPending)
s.Equal(s.p.l2SuggestedFeeRecipient, event.Meta.Proposer)

receipt, err := s.p.RPC.L1.TransactionReceipt(context.Background(), event.Raw.TxHash)
receipt, err := s.p.rpc.L1.TransactionReceipt(context.Background(), event.Raw.TxHash)
s.Nil(err)
s.Equal(types.ReceiptStatusSuccessful, receipt.Status)
}
Expand All @@ -129,7 +129,7 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
fee := big.NewInt(10000)
opts, err := getTxOpts(
context.Background(),
s.p.RPC.L1,
s.p.rpc.L1,
s.p.l1ProposerPrivKey,
s.RpcClient.L1ChainID,
fee,
Expand Down
Loading

0 comments on commit 3cfa551

Please sign in to comment.