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

Commit

Permalink
feat(prover): improve prover implementation (#635)
Browse files Browse the repository at this point in the history
Co-authored-by: David <[email protected]>
  • Loading branch information
mask-pp and davidtaikocha authored Mar 14, 2024
1 parent 7abd6d0 commit 5983828
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 177 deletions.
7 changes: 4 additions & 3 deletions pkg/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (s *Sender) Close() {
}

// GetOpts returns the transaction options of the sender.
func (s *Sender) GetOpts() *bind.TransactOpts {
func (s *Sender) GetOpts(ctx context.Context) *bind.TransactOpts {
return &bind.TransactOpts{
From: s.opts.From,
Nonce: s.opts.Nonce,
Expand All @@ -161,7 +161,7 @@ func (s *Sender) GetOpts() *bind.TransactOpts {
GasFeeCap: s.opts.GasFeeCap,
GasTipCap: s.opts.GasTipCap,
GasLimit: s.opts.GasLimit,
Context: s.opts.Context,
Context: ctx,
NoSend: s.opts.NoSend,
}
}
Expand Down Expand Up @@ -200,6 +200,7 @@ func (s *Sender) GetUnconfirmedTx(txID string) *types.Transaction {

// SendRawTransaction sends a transaction to the given Ethereum node.
func (s *Sender) SendRawTransaction(
ctx context.Context,
nonce uint64,
target *common.Address,
value *big.Int,
Expand All @@ -216,7 +217,7 @@ func (s *Sender) SendRawTransaction(

var (
originalTx types.TxData
opts = s.GetOpts()
opts = s.GetOpts(ctx)
gasLimit = s.GasLimit
err error
)
Expand Down
33 changes: 24 additions & 9 deletions pkg/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.GetOpts()
opts = s.sender.GetOpts(context.Background())
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.GetOpts().From, nil)
nonce, err := s.RPCClient.L1.NonceAt(context.Background(), s.sender.Address(), nil)
s.Nil(err)

var eg errgroup.Group
Expand All @@ -66,7 +66,14 @@ 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, nil)
_, err := s.sender.SendRawTransaction(
context.Background(),
nonce+uint64(i),
&addr,
big.NewInt(1),
nil,
nil,
)
return err
})
}
Expand All @@ -82,7 +89,7 @@ func (s *SenderTestSuite) TestSendRawTransaction() {
func (s *SenderTestSuite) TestReplacement() {
send := s.sender
client := s.RPCClient.L1
opts := send.GetOpts()
opts := send.GetOpts(context.Background())

// Let max gas price be 2 times of the gas fee cap.
send.MaxGasFee = opts.GasFeeCap.Uint64() * 2
Expand All @@ -108,18 +115,19 @@ func (s *SenderTestSuite) TestReplacement() {
Value: big.NewInt(1),
Data: nil,
}
rawTx, err := send.GetOpts().Signer(send.GetOpts().From, types.NewTx(baseTx))
rawTx, err := opts.Signer(opts.From, types.NewTx(baseTx))
s.Nil(err)
err = client.SendTransaction(context.Background(), rawTx)
s.Nil(err)

ctx := context.Background()
// Replace the transaction with a higher nonce.
_, err = send.SendRawTransaction(nonce, &common.Address{}, big.NewInt(1), nil, nil)
_, err = send.SendRawTransaction(ctx, 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, nil)
_, err = send.SendRawTransaction(ctx, nonce-1, &common.Address{}, big.NewInt(1), nil, nil)
s.Nil(err)

for _, confirmCh := range send.TxToConfirmChannels() {
Expand All @@ -139,7 +147,7 @@ func (s *SenderTestSuite) TestReplacement() {
func (s *SenderTestSuite) TestNonceTooLow() {
client := s.RPCClient.L1
send := s.sender
opts := s.sender.GetOpts()
opts := s.sender.GetOpts(context.Background())

nonce, err := client.NonceAt(context.Background(), opts.From, nil)
s.Nil(err)
Expand All @@ -150,7 +158,14 @@ func (s *SenderTestSuite) TestNonceTooLow() {
return
}

txID, err := send.SendRawTransaction(nonce-3, &common.Address{}, big.NewInt(1), nil, nil)
txID, err := send.SendRawTransaction(
context.Background(),
nonce-3,
&common.Address{},
big.NewInt(1),
nil,
nil,
)
s.Nil(err)
confirm := <-send.TxToConfirmChannel(txID)
s.Nil(confirm.Err)
Expand Down
2 changes: 1 addition & 1 deletion proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (p *Proposer) ProposeTxList(
tx, err := p.txBuilder.Build(
ctx,
p.tierFees,
p.sender.GetOpts(),
p.sender.GetOpts(p.ctx),
p.IncludeParentMetaHash,
txListBytes,
)
Expand Down
20 changes: 17 additions & 3 deletions proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ 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, nil)
txID, err := sender.SendRawTransaction(
context.Background(),
nonce,
&common.Address{},
common.Big1,
nil,
nil,
)
s.Nil(err)
tx := sender.GetUnconfirmedTx(txID)

Expand All @@ -169,14 +176,21 @@ func (s *ProposerTestSuite) TestSendProposeBlockTx() {
newTx, err := s.p.txBuilder.Build(
context.Background(),
s.p.tierFees,
sender.GetOpts(),
sender.GetOpts(context.Background()),
false,
encoded,
)

s.Nil(err)

txID, err = sender.SendRawTransaction(nonce, newTx.To(), newTx.Value(), newTx.Data(), nil)
txID, err = sender.SendRawTransaction(
context.Background(),
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/calldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (s *TransactionBuilderTestSuite) TestBuildCalldata() {
{Tier: encoding.TierOptimisticID, Fee: common.Big256},
{Tier: encoding.TierSgxID, Fee: common.Big256},
{Tier: encoding.TierSgxAndZkVMID, Fee: common.Big257},
}, s.sender.GetOpts(), false, []byte{1})
}, s.sender.GetOpts(context.Background()), false, []byte{1})
s.Nil(err)
s.Equal(types.DynamicFeeTxType, int(tx.Type()))
}
35 changes: 16 additions & 19 deletions prover/event_handler/assignment_expired.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ func (h *AssignmentExpiredEventHandler) Handle(
if err != nil {
return err
}
if proofStatus.IsSubmitted {
// If there is already a proof submitted and there is no need to contest
// it, we skip proving this block here.
if !proofStatus.Invalid || !h.contesterMode {
return nil
}
if !proofStatus.IsSubmitted {
go func() {
h.proofSubmissionCh <- &proofProducer.ProofRequestBody{Tier: e.Meta.MinTier, Event: e}
}()
return nil
}
// If there is already a proof submitted and there is no need to contest
// it, we skip proving this block here.
if !proofStatus.Invalid || !h.contesterMode {
return nil
}

// If there is no contester, we submit a contest to protocol.
// If there is no contester, we submit a contest to protocol.
go func() {
if proofStatus.CurrentTransitionState.Contester == rpc.ZeroAddress {
h.proofContestCh <- &proofProducer.ContestRequestBody{
BlockID: e.BlockId,
Expand All @@ -67,22 +73,13 @@ func (h *AssignmentExpiredEventHandler) Handle(
Meta: &e.Meta,
Tier: proofStatus.CurrentTransitionState.Tier,
}

return nil
}

go func() {
} else {
h.proofSubmissionCh <- &proofProducer.ProofRequestBody{
Tier: proofStatus.CurrentTransitionState.Tier + 1,
Event: e,
}
}()

return nil
}

go func() {
h.proofSubmissionCh <- &proofProducer.ProofRequestBody{Tier: e.Meta.MinTier, Event: e}
}
}()

return nil
}
7 changes: 5 additions & 2 deletions prover/event_handler/block_proposed.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ func (h *BlockProposedEventHandler) Handle(
}
return nil
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(h.backOffRetryInterval), h.backOffMaxRetrys),
backoff.WithContext(
backoff.WithMaxRetries(backoff.NewConstantBackOff(h.backOffRetryInterval), h.backOffMaxRetrys),
ctx,
),
); err != nil {
log.Error("Handle new BlockProposed event error", "error", err)
}
Expand Down Expand Up @@ -357,7 +360,7 @@ func (h *BlockProposedEventHandler) checkExpirationAndSubmitProof(

// ========================= Guardian Prover =========================

// NewBlockProposedEventHandlerOps is the options for creating a new BlockProposedEventHandler.
// NewBlockProposedGuardianEventHandlerOps is the options for creating a new BlockProposedEventHandler.
type NewBlockProposedGuardianEventHandlerOps struct {
*NewBlockProposedEventHandlerOps
GuardianProverHeartbeater guardianProverHeartbeater.BlockSenderHeartbeater
Expand Down
4 changes: 2 additions & 2 deletions prover/event_handler/transition_contested.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *TransitionContestedEventHandler) Handle(
}

// Compare the contested transition to the block in local L2 canonical chain.
isValidProof, err := isValidProof(
isValid, err := isValidProof(
ctx,
h.rpc,
e.BlockId,
Expand All @@ -69,7 +69,7 @@ func (h *TransitionContestedEventHandler) Handle(
if err != nil {
return err
}
if isValidProof {
if isValid {
log.Info(
"Contested transition is valid to local canonical chain, ignore the contest",
"blockID", e.BlockId,
Expand Down
25 changes: 9 additions & 16 deletions prover/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func (p *Prover) setApprovalAmount(ctx context.Context, contract common.Address)
return nil
}

opts := p.txSender.GetOpts()
opts.Context = ctx
opts := p.txSender.GetOpts(ctx)

log.Info("Approving the contract for taiko token", "allowance", p.cfg.Allowance.String(), "contract", contract)

Expand Down Expand Up @@ -107,28 +106,22 @@ func (p *Prover) initProofSubmitters(
)
switch tier.ID {
case encoding.TierOptimisticID:
producer = &proofProducer.OptimisticProofProducer{DummyProofProducer: new(proofProducer.DummyProofProducer)}
producer = &proofProducer.OptimisticProofProducer{}
case encoding.TierSgxID:
sgxProducer, err := proofProducer.NewSGXProducer(
p.cfg.RaikoHostEndpoint,
p.cfg.L1HttpEndpoint,
p.cfg.L1BeaconEndpoint,
p.cfg.L2HttpEndpoint,
)
if err != nil {
return err
}
if p.cfg.Dummy {
sgxProducer.DummyProofProducer = new(proofProducer.DummyProofProducer)
producer = &proofProducer.SGXProofProducer{
RaikoHostEndpoint: p.cfg.RaikoHostEndpoint,
L1Endpoint: p.cfg.L1HttpEndpoint,
L1BeaconEndpoint: p.cfg.L1BeaconEndpoint,
L2Endpoint: p.cfg.L2HttpEndpoint,
Dummy: p.cfg.Dummy,
}
producer = sgxProducer
case encoding.TierGuardianID:
producer = proofProducer.NewGuardianProofProducer(p.cfg.EnableLivenessBondProof)
default:
return fmt.Errorf("unsupported tier: %d", tier.ID)
}

if submitter, err = proofSubmitter.New(
if submitter, err = proofSubmitter.NewProofSubmitter(
p.rpc,
producer,
p.proofGenerationCh,
Expand Down
2 changes: 1 addition & 1 deletion prover/proof_producer/dummy_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDummyProducerRequestProof(t *testing.T) {
}

var (
producer = &DummyProofProducer{}
producer = DummyProofProducer{}
tier uint16 = 1024
blockID = common.Big32
)
Expand Down
15 changes: 2 additions & 13 deletions prover/proof_producer/guardian_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ import (
// GuardianProofProducer always returns an optimistic (dummy) proof.
type GuardianProofProducer struct {
returnLivenessBond bool
*DummyProofProducer
DummyProofProducer
}

func NewGuardianProofProducer(returnLivenessBond bool) *GuardianProofProducer {
gp := &GuardianProofProducer{
return &GuardianProofProducer{
returnLivenessBond: returnLivenessBond,
}

if !returnLivenessBond {
gp.DummyProofProducer = new(DummyProofProducer)
}

return gp
}

// RequestProof implements the ProofProducer interface.
Expand Down Expand Up @@ -64,8 +58,3 @@ func (g *GuardianProofProducer) RequestProof(
func (g *GuardianProofProducer) Tier() uint16 {
return encoding.TierGuardianID
}

// Cancellable implements the ProofProducer interface.
func (g *GuardianProofProducer) Cancellable() bool {
return false
}
7 changes: 1 addition & 6 deletions prover/proof_producer/optimistic_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// OptimisticProofProducer always returns an optimistic (dummy) proof.
type OptimisticProofProducer struct{ *DummyProofProducer }
type OptimisticProofProducer struct{ DummyProofProducer }

// RequestProof implements the ProofProducer interface.
func (o *OptimisticProofProducer) RequestProof(
Expand All @@ -37,8 +37,3 @@ func (o *OptimisticProofProducer) RequestProof(
func (o *OptimisticProofProducer) Tier() uint16 {
return encoding.TierOptimisticID
}

// Cancellable implements the ProofProducer interface.
func (o *OptimisticProofProducer) Cancellable() bool {
return false
}
1 change: 0 additions & 1 deletion prover/proof_producer/proof_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ type ProofProducer interface {
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
) (*ProofWithHeader, error)
Cancellable() bool
Tier() uint16
}
Loading

0 comments on commit 5983828

Please sign in to comment.