Skip to content

Commit

Permalink
Revamp sequencer and aggregator integration with ethtxmanager (0xPoly…
Browse files Browse the repository at this point in the history
…gonHermez#1330)

* move sequence to state and create persistence methods

* WIP

* conclude sequence persistence

* ethtxmanager refactor to work with sequence groups

* fix sql

* implemented sequence groups

* WIP

* WIP

* WIP

* refactoring how to send proofs to l1

* aggregator l1 tx management

* remove unused method from etherman

* readd l1 network to deploy sc script

* remove duplicated sequence group logic

* db schema modified

* sequence table removed

* tryToSendVerifiedBatch removed

* fixes

* migration fixed

* fix txHash update in db

Co-authored-by: Alonso <[email protected]>
  • Loading branch information
tclemos and ARR552 authored Nov 14, 2022
1 parent 4d02f02 commit 7223b79
Show file tree
Hide file tree
Showing 36 changed files with 1,067 additions and 504 deletions.
53 changes: 5 additions & 48 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewAggregator(
// Check if prover is already working in a proof generation
proof, err := stateInterface.GetWIPProofByProver(ctx, proverURI, nil)
if err != nil && err != state.ErrNotFound {
log.Errorf("Error while getting WIP proof for prover %v", proverURI)
log.Errorf("Error while getting WIP proof for prover %v, error: %w", proverURI, err)
continue
}

Expand Down Expand Up @@ -101,6 +101,7 @@ func (a *Aggregator) Start(ctx context.Context) {
defer tickerSendVerifiedBatch.Stop()

for i := 0; i < len(a.ProverClients); i++ {
// persist proofs into the DB
go func() {
for {
a.tryVerifyBatch(ctx, tickerVerifyBatch)
Expand All @@ -109,61 +110,17 @@ func (a *Aggregator) Start(ctx context.Context) {
time.Sleep(time.Second)
}

// send and monitor persisted proofs to L1
go func() {
for {
a.tryToSendVerifiedBatch(ctx, tickerSendVerifiedBatch)
a.EthTxManager.SyncPendingProofs()
waitTick(ctx, tickerSendVerifiedBatch)
}
}()
// Wait until context is done
<-ctx.Done()
}

func (a *Aggregator) tryToSendVerifiedBatch(ctx context.Context, ticker *time.Ticker) {
log.Debug("checking if network is synced")
for !a.isSynced(ctx) {
log.Infof("waiting for synchronizer to sync...")
waitTick(ctx, ticker)
continue
}
log.Debug("checking if there is any consolidated batch to be verified")
lastVerifiedBatch, err := a.State.GetLastVerifiedBatch(ctx, nil)
if err != nil && err != state.ErrNotFound {
log.Warnf("failed to get last consolidated batch, err: %v", err)
waitTick(ctx, ticker)
return
} else if err == state.ErrNotFound {
log.Debug("no consolidated batch found")
waitTick(ctx, ticker)
return
}

batchNumberToVerify := lastVerifiedBatch.BatchNumber + 1

proof, err := a.State.GetGeneratedProofByBatchNumber(ctx, batchNumberToVerify, nil)
if err != nil && err != state.ErrNotFound {
log.Warnf("failed to get last proof for batch %v, err: %v", batchNumberToVerify, err)
waitTick(ctx, ticker)
return
}

if proof != nil && proof.Proof != nil {
log.Infof("sending verified proof with id [%s] to the ethereum smart contract, batchNumber [%d]", *proof.ProofID, batchNumberToVerify)
err := a.EthTxManager.VerifyBatch(ctx, batchNumberToVerify, proof.Proof)
if err != nil {
log.Errorf("error verifying batch %d. Error: %w", batchNumberToVerify, err)
} else {
log.Infof("proof with id [%s] for the batch was sent, batchNumber: [%v]", *proof.ProofID, batchNumberToVerify)
err := a.State.DeleteGeneratedProof(ctx, batchNumberToVerify, nil)
if err != nil {
log.Warnf("failed to delete generated proof for batchNumber %v, err: %v", batchNumberToVerify, err)
}
}
} else {
log.Debugf("no generated proof for batchNumber %v has been found", batchNumberToVerify)
waitTick(ctx, ticker)
}
}

func (a *Aggregator) tryVerifyBatch(ctx context.Context, ticker *time.Ticker) {
log.Info("checking if network is synced")
for !a.isSynced(ctx) {
Expand Down
2 changes: 1 addition & 1 deletion aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// ethTxManager contains the methods required to send txs to
// ethereum.
type ethTxManager interface {
VerifyBatch(ctx context.Context, batchNum uint64, proof *pb.GetProofResponse) error
SyncPendingProofs()
}

// etherman contains the methods required to interact with ethereum
Expand Down
22 changes: 4 additions & 18 deletions aggregator/mocks/mock_ethtxmanager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func start(cliCtx *cli.Context) error {
ctx := context.Background()
st := newState(ctx, c, l2ChainID, stateSqlDB)

ethTxManager := ethtxmanager.New(c.EthTxManager, etherman)
ethTxManager := ethtxmanager.New(c.EthTxManager, st, etherman)

for _, item := range cliCtx.StringSlice(config.FlagComponents) {
switch item {
Expand Down
32 changes: 4 additions & 28 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ func Test_Defaults(t *testing.T) {
path: "Sequencer.WaitPeriodPoolIsEmpty",
expectedValue: types.NewDuration(1 * time.Second),
},
{
path: "Sequencer.LastBatchVirtualizationTimeMaxWaitPeriod",
expectedValue: types.NewDuration(300 * time.Second),
},
{
path: "Sequencer.WaitBlocksToUpdateGER",
expectedValue: uint64(10),
Expand Down Expand Up @@ -152,32 +148,12 @@ func Test_Defaults(t *testing.T) {
expectedValue: true,
},
{
path: "EthTxManager.MaxSendBatchTxRetries",
expectedValue: uint32(10),
},
{
path: "EthTxManager.MaxVerifyBatchTxRetries",
expectedValue: uint32(10),
},
{
path: "EthTxManager.FrequencyForResendingFailedSendBatches",
expectedValue: types.NewDuration(1 * time.Second),
},
{
path: "EthTxManager.FrequencyForResendingFailedVerifyBatch",
expectedValue: types.NewDuration(1 * time.Second),
},
{
path: "EthTxManager.WaitTxToBeMined",
expectedValue: types.NewDuration(2 * time.Minute),
path: "EthTxManager.IntervalToReviewSendBatchTx",
expectedValue: types.NewDuration(1 * time.Minute),
},
{
path: "EthTxManager.PercentageToIncreaseGasPrice",
expectedValue: uint64(10),
},
{
path: "EthTxManager.PercentageToIncreaseGasLimit",
expectedValue: uint64(10),
path: "EthTxManager.IntervalToReviewVerifyBatchTx",
expectedValue: types.NewDuration(1 * time.Minute),
},
{
path: "PriceGetter.Type",
Expand Down
10 changes: 2 additions & 8 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ MultiGasProvider = true
ApiKey = ""
[EthTxManager]
MaxSendBatchTxRetries = 10
MaxVerifyBatchTxRetries = 10
FrequencyForResendingFailedSendBatches = "1s"
FrequencyForResendingFailedVerifyBatch = "1s"
WaitTxToBeMined = "2m"
PercentageToIncreaseGasPrice = 10
PercentageToIncreaseGasLimit = 10
IntervalToReviewSendBatchTx = "1m"
IntervalToReviewVerifyBatchTx = "1m"
[RPC]
Host = "0.0.0.0"
Expand Down Expand Up @@ -73,7 +68,6 @@ GenBlockNumber = 1
MaxSequenceSize = "2000000"
WaitPeriodPoolIsEmpty = "1s"
WaitPeriodSendSequence = "15s"
LastBatchVirtualizationTimeMaxWaitPeriod = "300s"
WaitBlocksToUpdateGER = 10
WaitBlocksToConsiderGerFinal = 10
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s"
Expand Down
12 changes: 3 additions & 9 deletions config/environments/local/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ MultiGasProvider = false
ApiKey = ""

[EthTxManager]
MaxSendBatchTxRetries = 10
MaxVerifyBatchTxRetries = 10
FrequencyForResendingFailedSendBatches = "1s"
FrequencyForResendingFailedVerifyBatch = "1s"
WaitTxToBeMined = "2m"
PercentageToIncreaseGasPrice = 10
PercentageToIncreaseGasLimit = 10
IntervalToReviewSendBatchTx = "1m"
IntervalToReviewVerifyBatchTx = "1m"

[RPC]
Host = "0.0.0.0"
Expand Down Expand Up @@ -70,8 +65,7 @@ GenBlockNumber = 1
[Sequencer]
MaxSequenceSize = "2000000"
WaitPeriodPoolIsEmpty = "1s"
WaitPeriodSendSequence = "15s"
LastBatchVirtualizationTimeMaxWaitPeriod = "300s"
WaitPeriodSendSequence = "10s"
WaitBlocksToUpdateGER = 10
WaitBlocksToConsiderGerFinal = 10
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s"
Expand Down
12 changes: 3 additions & 9 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ MultiGasProvider = false
ApiKey = ""

[EthTxManager]
MaxSendBatchTxRetries = 10
MaxVerifyBatchTxRetries = 10
FrequencyForResendingFailedSendBatches = "1s"
FrequencyForResendingFailedVerifyBatch = "1s"
WaitTxToBeMined = "2m"
PercentageToIncreaseGasPrice = 10
PercentageToIncreaseGasLimit = 10
IntervalToReviewSendBatchTx = "1m"
IntervalToReviewVerifyBatchTx = "1m"

[RPC]
Host = "0.0.0.0"
Expand Down Expand Up @@ -70,8 +65,7 @@ GenBlockNumber = 1
[Sequencer]
MaxSequenceSize = "2000000"
WaitPeriodPoolIsEmpty = "1s"
WaitPeriodSendSequence = "15s"
LastBatchVirtualizationTimeMaxWaitPeriod = "300s"
WaitPeriodSendSequence = "10s"
WaitBlocksToUpdateGER = 10
WaitBlocksToConsiderGerFinal = 10
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s"
Expand Down
10 changes: 4 additions & 6 deletions db/migrations/state/0002.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
-- +migrate Up
ALTER TABLE state.proof ADD COLUMN proof_id VARCHAR;
ALTER TABLE state.proof ADD COLUMN input_prover jsonb;
ALTER TABLE state.proof ADD COLUMN prover VARCHAR;

-- +migrate Down
ALTER TABLE state.proof DROP COLUMN prover;
ALTER TABLE state.proof DROP COLUMN input_prover;
ALTER TABLE state.proof DROP COLUMN proof_id;


-- +migrate Up
ALTER TABLE state.proof ADD COLUMN proof_id VARCHAR;
ALTER TABLE state.proof ADD COLUMN input_prover jsonb;
ALTER TABLE state.proof ADD COLUMN prover VARCHAR;
34 changes: 34 additions & 0 deletions db/migrations/state/0003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- +migrate Down
DROP TABLE state.sequence_group;

ALTER TABLE state.proof DROP COLUMN tx_hash;
ALTER TABLE state.proof DROP COLUMN tx_nonce;
ALTER TABLE state.proof DROP COLUMN status;
ALTER TABLE state.proof DROP COLUMN created_at;
ALTER TABLE state.proof DROP COLUMN updated_at;

-- +migrate Up
CREATE TABLE state.sequence_group
(
tx_hash VARCHAR,
tx_nonce DECIMAL(78, 0),
from_batch_num BIGINT NOT NULL REFERENCES state.batch (batch_num) ON DELETE CASCADE,
to_batch_num BIGINT NOT NULL REFERENCES state.batch (batch_num) ON DELETE CASCADE,
status VARCHAR(15) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE,
PRIMARY KEY (tx_hash)
);

ALTER TABLE state.proof ADD COLUMN tx_hash VARCHAR;
ALTER TABLE state.proof ADD COLUMN tx_nonce DECIMAL(78, 0);
ALTER TABLE state.proof ADD COLUMN status VARCHAR(15);
ALTER TABLE state.proof ADD COLUMN created_at TIMESTAMP WITH TIME ZONE;
ALTER TABLE state.proof ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE;

UPDATE state.proof SET created_at = NOW();
UPDATE state.proof SET status = 'pending' WHERE batch_num > (SELECT batch_num from state.verified_batch ORDER BY batch_num DESC LIMIT 1);
UPDATE state.proof SET status = 'confirmed', updated_at = NOW() WHERE batch_num <= (SELECT batch_num from state.verified_batch ORDER BY batch_num DESC LIMIT 1);

ALTER TABLE state.proof ALTER COLUMN status SET NOT NULL;
ALTER TABLE state.proof ALTER COLUMN created_at SET NOT NULL;
9 changes: 4 additions & 5 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/globalexitrootmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/matic"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/proofofefficiency"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/proverclient/pb"
"github.com/0xPolygonHermez/zkevm-node/state"
Expand Down Expand Up @@ -272,7 +271,7 @@ func (etherMan *Client) WaitTxToBeMined(ctx context.Context, tx *types.Transacti
}

// EstimateGasSequenceBatches estimates gas for sending batches
func (etherMan *Client) EstimateGasSequenceBatches(sequences []ethmanTypes.Sequence) (*types.Transaction, error) {
func (etherMan *Client) EstimateGasSequenceBatches(sequences []state.Sequence) (*types.Transaction, error) {
if etherMan.IsReadOnly() {
return nil, ErrIsReadOnlyMode
}
Expand All @@ -287,7 +286,7 @@ func (etherMan *Client) EstimateGasSequenceBatches(sequences []ethmanTypes.Seque
}

// SequenceBatches send sequences of batches to the ethereum
func (etherMan *Client) SequenceBatches(ctx context.Context, sequences []ethmanTypes.Sequence, gasLimit uint64, gasPrice, nonce *big.Int) (*types.Transaction, error) {
func (etherMan *Client) SequenceBatches(ctx context.Context, sequences []state.Sequence, gasLimit uint64, gasPrice, nonce *big.Int) (*types.Transaction, error) {
if etherMan.IsReadOnly() {
return nil, ErrIsReadOnlyMode
}
Expand All @@ -304,7 +303,7 @@ func (etherMan *Client) SequenceBatches(ctx context.Context, sequences []ethmanT
return etherMan.sequenceBatches(&sendSequencesOpts, sequences)
}

func (etherMan *Client) sequenceBatches(opts *bind.TransactOpts, sequences []ethmanTypes.Sequence) (*types.Transaction, error) {
func (etherMan *Client) sequenceBatches(opts *bind.TransactOpts, sequences []state.Sequence) (*types.Transaction, error) {
var batches []proofofefficiency.ProofOfEfficiencyBatchData
for _, seq := range sequences {
batchL2Data, err := state.EncodeTransactions(seq.Txs)
Expand All @@ -314,7 +313,7 @@ func (etherMan *Client) sequenceBatches(opts *bind.TransactOpts, sequences []eth
batch := proofofefficiency.ProofOfEfficiencyBatchData{
Transactions: batchL2Data,
GlobalExitRoot: seq.GlobalExitRoot,
Timestamp: uint64(seq.Timestamp),
Timestamp: uint64(seq.Timestamp.Unix()),
ForceBatchesTimestamp: nil,
}

Expand Down
8 changes: 4 additions & 4 deletions etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/bridge"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/proofofefficiency"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
Expand Down Expand Up @@ -295,12 +295,12 @@ func TestSendSequences(t *testing.T) {
require.NoError(t, err)

tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(1), big.NewInt(10), []byte{})
sequence := ethmanTypes.Sequence{
sequence := state.Sequence{
GlobalExitRoot: ger,
Timestamp: int64(currentBlock.Time() - 1),
Timestamp: time.Unix(int64(currentBlock.Time()-1), 0),
Txs: []types.Transaction{*tx1},
}
tx, err := etherman.sequenceBatches(etherman.auth, []ethmanTypes.Sequence{sequence})
tx, err := etherman.sequenceBatches(etherman.auth, []state.Sequence{sequence})
require.NoError(t, err)
log.Debug("TX: ", tx.Hash())
ethBackend.Commit()
Expand Down
Loading

0 comments on commit 7223b79

Please sign in to comment.