Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aggregator wait L1block confirmations to generate proof of a virtual batch #3302

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,23 @@ func (a *Aggregator) getAndLockBatchToProve(ctx context.Context, prover proverIn
return nil, nil, err
}

// Get header of the last L1 block
lastL1BlockHeader, err := a.Ethman.GetLatestBlockHeader(ctx)
if err != nil {
log.Errorf("Failed to get last L1 block header, err: %v", err)
return nil, nil, err
}
lastL1BlockNumber := lastL1BlockHeader.Number.Uint64()

// Calculate max L1 block number for getting next virtual batch to prove
maxL1BlockNumber := uint64(0)
if a.cfg.BatchProofL1BlockConfirmations <= lastL1BlockNumber {
maxL1BlockNumber = lastL1BlockNumber - a.cfg.BatchProofL1BlockConfirmations
}
log.Debugf("Max L1 block number for getting next virtual batch to prove: %d", maxL1BlockNumber)

// Get virtual batch pending to generate proof
batchToVerify, err := a.State.GetVirtualBatchToProve(ctx, lastVerifiedBatch.BatchNumber, nil)
batchToVerify, err := a.State.GetVirtualBatchToProve(ctx, lastVerifiedBatch.BatchNumber, maxL1BlockNumber, nil)
if err != nil {
return nil, nil, err
}
Expand Down
16 changes: 11 additions & 5 deletions aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -775,7 +776,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
m.proverMock.On("ID").Return(proverID).Twice()
m.proverMock.On("Addr").Return("addr")
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(&lastVerifiedBatch, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, mock.Anything, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("AddGeneratedProof", mock.MatchedBy(matchProverCtxFn), mock.Anything, nil).Run(
func(args mock.Arguments) {
proof := args[1].(*state.Proof)
Expand All @@ -798,6 +799,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.etherman.On("GetLatestBlockHeader", mock.Anything).Return(&types.Header{Number: new(big.Int).SetUint64(1)}, nil).Once()
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
m.stateMock.On("GetLeafsByL1InfoRoot", mock.Anything, *vb.L1InfoRoot, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
Expand All @@ -817,7 +819,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
m.proverMock.On("ID").Return(proverID).Twice()
m.proverMock.On("Addr").Return("addr")
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(&lastVerifiedBatch, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, mock.Anything, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("AddGeneratedProof", mock.MatchedBy(matchProverCtxFn), mock.Anything, nil).Run(
func(args mock.Arguments) {
proof := args[1].(*state.Proof)
Expand All @@ -840,6 +842,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.etherman.On("GetLatestBlockHeader", mock.Anything).Return(&types.Header{Number: new(big.Int).SetUint64(1)}, nil).Once()
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
m.stateMock.On("GetLeafsByL1InfoRoot", mock.Anything, *vb.L1InfoRoot, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
Expand All @@ -860,7 +863,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
m.proverMock.On("ID").Return(proverID).Twice()
m.proverMock.On("Addr").Return(proverID)
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(&lastVerifiedBatch, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, mock.Anything, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("AddGeneratedProof", mock.MatchedBy(matchProverCtxFn), mock.Anything, nil).Run(
func(args mock.Arguments) {
proof := args[1].(*state.Proof)
Expand All @@ -883,6 +886,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.etherman.On("GetLatestBlockHeader", mock.Anything).Return(&types.Header{Number: new(big.Int).SetUint64(1)}, nil).Once()
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
m.stateMock.On("GetLeafsByL1InfoRoot", mock.Anything, *vb.L1InfoRoot, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
Expand All @@ -903,7 +907,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
m.proverMock.On("ID").Return(proverID).Times(3)
m.proverMock.On("Addr").Return("addr")
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(&lastVerifiedBatch, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, mock.Anything, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("AddGeneratedProof", mock.MatchedBy(matchProverCtxFn), mock.Anything, nil).Run(
func(args mock.Arguments) {
proof := args[1].(*state.Proof)
Expand All @@ -926,6 +930,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
L1InfoRoot: &l1InfoRoot,
TimestampBatchEtrog: &t,
}
m.etherman.On("GetLatestBlockHeader", mock.Anything).Return(&types.Header{Number: new(big.Int).SetUint64(1)}, nil).Once()
m.stateMock.On("GetVirtualBatch", mock.Anything, lastVerifiedBatchNum+1, nil).Return(&vb, nil).Twice()
m.stateMock.On("GetLeafsByL1InfoRoot", mock.Anything, *vb.L1InfoRoot, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil).Twice()
expectedInputProver, err := a.buildInputProver(context.Background(), &batchToProve)
Expand Down Expand Up @@ -960,7 +965,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
m.proverMock.On("ID").Return(proverID).Times(3)
m.proverMock.On("Addr").Return("addr")
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(&lastVerifiedBatch, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("GetVirtualBatchToProve", mock.MatchedBy(matchProverCtxFn), lastVerifiedBatchNum, mock.Anything, nil).Return(&batchToProve, nil).Once()
m.stateMock.On("AddGeneratedProof", mock.MatchedBy(matchProverCtxFn), mock.Anything, nil).Run(
func(args mock.Arguments) {
proof := args[1].(*state.Proof)
Expand Down Expand Up @@ -995,6 +1000,7 @@ func TestTryGenerateBatchProof(t *testing.T) {
On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).
Return(&state.VerifiedBatch{BatchNumber: uint64(42)}, nil).Once()
m.etherman.On("GetLatestVerifiedBatchNum").Return(uint64(42), nil).Once()
m.etherman.On("GetLatestBlockHeader", mock.Anything).Return(&types.Header{Number: new(big.Int).SetUint64(1)}, nil).Once()
// make tryBuildFinalProof fail ASAP
m.stateMock.On("GetLastVerifiedBatch", mock.MatchedBy(matchProverCtxFn), nil).Return(nil, errBanana).Once().NotBefore(isSyncedCall)
m.stateMock.On("UpdateGeneratedProof", mock.MatchedBy(matchAggregatorCtxFn), mock.Anything, nil).Run(
Expand Down
3 changes: 3 additions & 0 deletions aggregator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ type Config struct {

// UpgradeEtrogBatchNumber is the number of the first batch after upgrading to etrog
UpgradeEtrogBatchNumber uint64 `mapstructure:"UpgradeEtrogBatchNumber"`

// BatchProofL1BlockConfirmations is number of L1 blocks to consider we can generate the proof for a virtual batch
BatchProofL1BlockConfirmations uint64 `mapstructure:"BatchProofL1BlockConfirmations"`
}
4 changes: 3 additions & 1 deletion aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/ethtxmanager"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/jackc/pgx/v4"
)

Expand Down Expand Up @@ -39,6 +40,7 @@ type ethTxManager interface {
type etherman interface {
GetLatestVerifiedBatchNum() (uint64, error)
BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVerifiedBatch uint64, inputs *ethmanTypes.FinalProofInputs, beneficiary common.Address) (to *common.Address, data []byte, err error)
GetLatestBlockHeader(ctx context.Context) (*types.Header, error)
}

// aggregatorTxProfitabilityChecker interface for different profitability
Expand All @@ -53,7 +55,7 @@ type stateInterface interface {
CheckProofContainsCompleteSequences(ctx context.Context, proof *state.Proof, dbTx pgx.Tx) (bool, error)
GetLastVerifiedBatch(ctx context.Context, dbTx pgx.Tx) (*state.VerifiedBatch, error)
GetProofReadyToVerify(ctx context.Context, lastVerfiedBatchNumber uint64, dbTx pgx.Tx) (*state.Proof, error)
GetVirtualBatchToProve(ctx context.Context, lastVerfiedBatchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
GetVirtualBatchToProve(ctx context.Context, lastVerfiedBatchNumber uint64, maxL1Block uint64, dbTx pgx.Tx) (*state.Batch, error)
GetProofsToAggregate(ctx context.Context, dbTx pgx.Tx) (*state.Proof, *state.Proof, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
AddGeneratedProof(ctx context.Context, proof *state.Proof, dbTx pgx.Tx) error
Expand Down
35 changes: 35 additions & 0 deletions aggregator/mocks/mock_etherman.go

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

18 changes: 9 additions & 9 deletions aggregator/mocks/mock_state.go

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

4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ func Test_Defaults(t *testing.T) {
path: "Aggregator.UpgradeEtrogBatchNumber",
expectedValue: uint64(0),
},
{
path: "Aggregator.BatchProofL1BlockConfirmations",
expectedValue: uint64(2),
},
{
path: "State.Batch.Constraints.MaxTxsPerBatch",
expectedValue: uint64(300),
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ CleanupLockedProofsInterval = "2m"
GeneratingProofCleanupThreshold = "10m"
GasOffset = 0
UpgradeEtrogBatchNumber = 0
BatchProofL1BlockConfirmations = 2
[L2GasPriceSuggester]
Type = "follower"
Expand Down
1 change: 1 addition & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ SenderAddress = "0x70997970c51812dc3a010c7d01b50e0d17dc79c8"
CleanupLockedProofsInterval = "2m"
GeneratingProofCleanupThreshold = "10m"
UpgradeEtrogBatchNumber = 0
BatchProofL1BlockConfirmations = 2

[EthTxManager]
ForcedGas = 0
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,7 @@ GasOffset=80000
| - [GeneratingProofCleanupThreshold](#Aggregator_GeneratingProofCleanupThreshold ) | No | string | No | - | GeneratingProofCleanupThreshold represents the time interval after<br />which a proof in generating state is considered to be stuck and<br />allowed to be cleared. |
| - [GasOffset](#Aggregator_GasOffset ) | No | integer | No | - | GasOffset is the amount of gas to be added to the gas estimation in order<br />to provide an amount that is higher than the estimated one. This is used<br />to avoid the TX getting reverted in case something has changed in the network<br />state after the estimation which can cause the TX to require more gas to be<br />executed.<br /><br />ex:<br />gas estimation: 1000<br />gas offset: 100<br />final gas: 1100 |
| - [UpgradeEtrogBatchNumber](#Aggregator_UpgradeEtrogBatchNumber ) | No | integer | No | - | UpgradeEtrogBatchNumber is the number of the first batch after upgrading to etrog |
| - [BatchProofL1BlockConfirmations](#Aggregator_BatchProofL1BlockConfirmations ) | No | integer | No | - | BatchProofL1BlockConfirmations is number of L1 blocks to consider we can generate the proof for a virtual batch |

### <a name="Aggregator_Host"></a>12.1. `Aggregator.Host`

Expand Down Expand Up @@ -2647,6 +2648,20 @@ GasOffset=0
UpgradeEtrogBatchNumber=0
```

### <a name="Aggregator_BatchProofL1BlockConfirmations"></a>12.16. `Aggregator.BatchProofL1BlockConfirmations`

**Type:** : `integer`

**Default:** `2`

**Description:** BatchProofL1BlockConfirmations is number of L1 blocks to consider we can generate the proof for a virtual batch

**Example setting the default value** (2):
```
[Aggregator]
BatchProofL1BlockConfirmations=2
```

## <a name="NetworkConfig"></a>13. `[NetworkConfig]`

**Type:** : `object`
Expand Down
5 changes: 5 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,11 @@
"type": "integer",
"description": "UpgradeEtrogBatchNumber is the number of the first batch after upgrading to etrog",
"default": 0
},
"BatchProofL1BlockConfirmations": {
"type": "integer",
"description": "BatchProofL1BlockConfirmations is number of L1 blocks to consider we can generate the proof for a virtual batch",
"default": 2
}
},
"additionalProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type storage interface {
GetExitRootByGlobalExitRoot(ctx context.Context, ger common.Hash, dbTx pgx.Tx) (*GlobalExitRoot, error)
AddSequence(ctx context.Context, sequence Sequence, dbTx pgx.Tx) error
GetSequences(ctx context.Context, lastVerifiedBatchNumber uint64, dbTx pgx.Tx) ([]Sequence, error)
GetVirtualBatchToProve(ctx context.Context, lastVerfiedBatchNumber uint64, dbTx pgx.Tx) (*Batch, error)
GetVirtualBatchToProve(ctx context.Context, lastVerfiedBatchNumber uint64, maxL1Block uint64, dbTx pgx.Tx) (*Batch, error)
CheckProofContainsCompleteSequences(ctx context.Context, proof *Proof, dbTx pgx.Tx) (bool, error)
GetProofReadyToVerify(ctx context.Context, lastVerfiedBatchNumber uint64, dbTx pgx.Tx) (*Proof, error)
GetProofsToAggregate(ctx context.Context, dbTx pgx.Tx) (*Proof, *Proof, error)
Expand Down
Loading
Loading