Skip to content

Commit

Permalink
Backport v0.0.1-RC4 & RC5 to develop (#1483)
Browse files Browse the repository at this point in the history
* update poe sc to security mechanisms version (#1467)

* update poe sc to security mechanisms version

* updated names, fix tests and remove ger timestamp and other fixes

* fix rlp tool

* fix linter

* new genesis + fix

* new smc

* Update executor errors

* Update executor errors

* Update executor errors

* Update executor errors

* new l2 contract names

* sequencewr unit test commented

* fix test

Co-authored-by: Alonso <ARR551@protonmail.com>
Co-authored-by: ToniRamirezM <toni@iden3.com>

* Fix/1472 verify batches timeout (#1473)

Closes #1472

### What does this PR do?

During testing we faced an issue of two consecutive verified batches on L1 without respecting the time spacing.
The problem was that we were relying on the timer to check if we can start generating a new final proof. That timer is only reset after the final proof is sent to L1. This means that there's a time window between the generation of a final proof and its verification in which a new proof eligible to be final can be generated and immediately trigger another final proof generation.

The fix introduced here is to have a `verifyingProof` flag that gets checked when the final proof generation process begins and released only when the verification phase ends (or in case of errors).

Co-authored-by: Thiago Coimbra Lemos <tclemos@users.noreply.github.com>
Co-authored-by: Alonso <ARR551@protonmail.com>
Co-authored-by: ToniRamirezM <toni@iden3.com>
  • Loading branch information
4 people committed Dec 23, 2022
1 parent ba9a046 commit 1570ef0
Show file tree
Hide file tree
Showing 49 changed files with 2,895 additions and 1,129 deletions.
43 changes: 34 additions & 9 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type Aggregator struct {
StateDBMutex *sync.Mutex
TimeSendFinalProofMutex *sync.RWMutex

finalProof chan finalProofMsg
finalProof chan finalProofMsg
verifyingProof bool

srv *grpc.Server
ctx context.Context
Expand Down Expand Up @@ -207,6 +208,7 @@ func (a *Aggregator) sendFinalProof() {
finalBatch, err := a.State.GetBatchByNumber(ctx, proof.BatchNumberFinal, nil)
if err != nil {
log.Errorf("Failed to retrieve batch with number [%d]", proof.BatchNumberFinal)
a.enableProofVerification()
continue
}

Expand All @@ -228,6 +230,7 @@ func (a *Aggregator) sendFinalProof() {
if err != nil {
log.Errorf("Rollback failed updating proof state (false) for proof ID [%v], err: %v", proof.ProofID, err)
}
a.enableProofVerification()
continue
}

Expand Down Expand Up @@ -302,12 +305,19 @@ func (a *Aggregator) buildFinalProof(ctx context.Context, prover proverInterface
func (a *Aggregator) tryBuildFinalProof(ctx context.Context, prover proverInterface, proof *state.Proof) (bool, error) {
log.Debugf("tryBuildFinalProof start prover { ID [%s], addr [%s] }", prover.ID(), prover.Addr())

if !a.verifyProofTimeReached() {
var err error
if !a.canVerifyProof() {
log.Debug("Time to verify proof not reached")
return false, nil
}
log.Debug("Send final proof time reached")

defer func() {
if err != nil {
a.enableProofVerification()
}
}()

for !a.isSynced(ctx) {
log.Info("Waiting for synchronizer to sync...")
time.Sleep(a.cfg.RetryTime.Duration)
Expand Down Expand Up @@ -351,7 +361,8 @@ func (a *Aggregator) tryBuildFinalProof(ctx context.Context, prover proverInterf
// we do have a proof generating at the moment, check if it is
// eligible to be verified

eligible, err := a.validateEligibleFinalProof(ctx, proof, lastVerifiedBatchNum)
var eligible bool // we need this to keep using err from the outer scope and trigger the defer func
eligible, err = a.validateEligibleFinalProof(ctx, proof, lastVerifiedBatchNum)
if err != nil {
return false, fmt.Errorf("Failed to validate eligible final proof, %w", err)
}
Expand Down Expand Up @@ -732,18 +743,32 @@ func (a *Aggregator) tryGenerateBatchProof(ctx context.Context, prover *prover.P
return true, nil
}

// verifyProofTimeReached returns if we have reached the timeout to verify a
// proof.
func (a *Aggregator) verifyProofTimeReached() bool {
a.TimeSendFinalProofMutex.RLock()
defer a.TimeSendFinalProofMutex.RUnlock()
return a.TimeSendFinalProof.Before(time.Now())
// canVerifyProof returns true if we have reached the timeout to verify a proof
// and no other prover is verifying a proof.
func (a *Aggregator) canVerifyProof() bool {
a.TimeSendFinalProofMutex.Lock()
defer a.TimeSendFinalProofMutex.Unlock()
if a.TimeSendFinalProof.Before(time.Now()) {
if a.verifyingProof {
return false
}
a.verifyingProof = true
return true
}
return false
}

func (a *Aggregator) enableProofVerification() {
a.TimeSendFinalProofMutex.Lock()
defer a.TimeSendFinalProofMutex.Unlock()
a.verifyingProof = false
}

// resetVerifyProofTime updates the timeout to verify a proof.
func (a *Aggregator) resetVerifyProofTime() {
a.TimeSendFinalProofMutex.Lock()
defer a.TimeSendFinalProofMutex.Unlock()
a.verifyingProof = false
a.TimeSendFinalProof = time.Now().Add(a.cfg.VerifyProofInterval.Duration)
}

Expand Down
62 changes: 30 additions & 32 deletions aggregator/pb/aggregator.pb.go

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

4 changes: 4 additions & 0 deletions aggregator/pb/aggregator_grpc.pb.go

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

6 changes: 3 additions & 3 deletions config/environments/local/local.genesis.config.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func loadGenesisFileConfig(ctx *cli.Context) (NetworkConfig, error) {
Actions: []*state.GenesisAction{},
}

const l2GlobalExitRootManagerSCName = "GlobalExitRootManagerL2"
const l2BridgeSCName = "Bridge"
const l2GlobalExitRootManagerSCName = "PolygonZkEVMGlobalExitRootL2"
const l2BridgeSCName = "PolygonZkEVMBridge"

for _, account := range cfgJSON.Genesis {
if account.ContractName == l2GlobalExitRootManagerSCName {
Expand Down
4 changes: 2 additions & 2 deletions config/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestLoadCustomNetworkConfig(t *testing.T) {
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x9d98deabc42dd696deb9e40b4f1cab7ddbf55988"
},
"contractName": "GlobalExitRootManagerL2"
"contractName": "PolygonZkEVMGlobalExitRootL2"
},
{
"balance": "100000000000000000000000",
Expand All @@ -50,7 +50,7 @@ func TestLoadCustomNetworkConfig(t *testing.T) {
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0xc949254d682d8c9ad5682521675b8f43b102aec4"
},
"contractName": "Bridge"
"contractName": "PolygonZkEVMBridge"
},
{
"balance": "0",
Expand Down
2 changes: 1 addition & 1 deletion db/migrations/state/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ CREATE TABLE state.transaction

CREATE TABLE state.exit_root
(
id SERIAL PRIMARY KEY,
block_num BIGINT NOT NULL REFERENCES state.block (block_num) ON DELETE CASCADE,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
mainnet_exit_root BYTEA,
rollup_exit_root BYTEA,
global_exit_root BYTEA
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ services:

zkevm-prover:
container_name: zkevm-prover
image: hermeznetwork/zkevm-prover:98676e8
image: hermeznetwork/zkevm-prover@sha256:ca123e5d23b38ac27c4b32c3a6c92ee7c988beb2f2405369e7065b0d63601245
ports:
- 50061:50061 # MT
- 50071:50071 # Executor
Expand Down
Loading

0 comments on commit 1570ef0

Please sign in to comment.