Skip to content

Commit

Permalink
Merge release/v0.0.1 into develop (0xPolygonHermez#1561)
Browse files Browse the repository at this point in the history
Merge release/v0.0.1 changes into develop. The original release/v0.0.1 branch has been left intact to be able to perform hotfixes in case.
  • Loading branch information
kind84 authored Jan 23, 2023
1 parent c8091e1 commit 6c02d15
Show file tree
Hide file tree
Showing 27 changed files with 1,314 additions and 817 deletions.
104 changes: 59 additions & 45 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (a *Aggregator) Start(ctx context.Context) error {

metrics.Register()

// process monitored batch verifications before starting
a.EthTxManager.ProcessPendingMonitoredTxs(ctx, ethTxManagerOwner, func(result ethtxmanager.MonitoredTxResult, dbTx pgx.Tx) {
a.handleMonitoredTxResult(result)
}, nil)

// Delete ungenerated recursive proofs
err := a.State.DeleteUngeneratedProofs(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -134,11 +139,6 @@ func (a *Aggregator) Start(ctx context.Context) error {

a.resetVerifyProofTime()

// process monitored batch verifications before starting a next cycle
a.EthTxManager.ProcessPendingMonitoredTxs(ctx, ethTxManagerOwner, func(result ethtxmanager.MonitoredTxResult, dbTx pgx.Tx) {
a.handleMonitoredTxResult(result)
}, nil)

go a.sendFinalProof()

<-ctx.Done()
Expand Down Expand Up @@ -223,12 +223,15 @@ func (a *Aggregator) sendFinalProof() {
ctx := a.ctx
proof := msg.recursiveProof

log.Infof("Verifying final proof with ethereum smart contract, batches %d-%d", proof.BatchNumber, proof.BatchNumberFinal)
log.WithFields("proofId", proof.ProofID, "batches", fmt.Sprintf("%d-%d", proof.BatchNumber, proof.BatchNumberFinal))
log.Info("Verifying final proof with ethereum smart contract")

a.startProofVerification()

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()
a.endProofVerification()
continue
}

Expand All @@ -244,14 +247,15 @@ func (a *Aggregator) sendFinalProof() {
sender := common.HexToAddress(a.cfg.SenderAddress)
to, data, err := a.Ethman.BuildTrustedVerifyBatchesTxData(proof.BatchNumber-1, proof.BatchNumberFinal, &inputs)
if err != nil {
log.Error("error estimating batch verification to add to eth tx manager: ", err)
log.Errorf("error estimating batch verification to add to eth tx manager: %v", err)
a.handleFailureToAddVerifyBatchToBeMonitored(ctx, proof)
continue
}
monitoredTxID := fmt.Sprintf(monitoredIDFormat, proof.BatchNumber, proof.BatchNumberFinal)
err = a.EthTxManager.Add(ctx, ethTxManagerOwner, monitoredTxID, sender, to, nil, data, nil)
if err != nil {
log.Error("error to add batch verification tx to eth tx manager: ", err)
log := log.WithFields("tx", monitoredTxID)
log.Errorf("error to add batch verification tx to eth tx manager: %v", err)
a.handleFailureToAddVerifyBatchToBeMonitored(ctx, proof)
continue
}
Expand All @@ -262,17 +266,19 @@ func (a *Aggregator) sendFinalProof() {
}, nil)

a.resetVerifyProofTime()
a.endProofVerification()
}
}
}

func (a *Aggregator) handleFailureToAddVerifyBatchToBeMonitored(ctx context.Context, proof *state.Proof) {
log := log.WithFields("proofId", proof.ProofID, "batches", fmt.Sprintf("%d-%d", proof.BatchNumber, proof.BatchNumberFinal))
proof.Generating = false
err := a.State.UpdateGeneratedProof(ctx, proof, nil)
if err != nil {
log.Errorf("failed updating proof state (false) for proof ID [%v], err: %v", proof.ProofID, err)
log.Errorf("failed updating proof state (false), err: %v", err)
}
a.enableProofVerification()
a.endProofVerification()
}

// buildFinalProof builds and return the final proof for an aggregated/batch proof.
Expand Down Expand Up @@ -322,17 +328,11 @@ func (a *Aggregator) tryBuildFinalProof(ctx context.Context, prover proverInterf

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

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

for !a.isSynced(ctx, nil) {
log.Info("Waiting for synchronizer to sync...")
time.Sleep(a.cfg.RetryTime.Duration)
Expand Down Expand Up @@ -376,13 +376,11 @@ 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

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)
eligible, err := a.validateEligibleFinalProof(ctx, proof, lastVerifiedBatchNum)
if err != nil {
return false, fmt.Errorf("failed to validate eligible final proof, %w", err)
}
if !eligible {
a.enableProofVerification()
return false, nil
}
}
Expand Down Expand Up @@ -419,13 +417,26 @@ func (a *Aggregator) validateEligibleFinalProof(ctx context.Context, proof *stat
batchNumberToVerify := lastVerifiedBatchNum + 1

if proof.BatchNumber != batchNumberToVerify {
log.Infof("Proof batch number %d is not the following to last verfied batch number %d", proof.BatchNumber, lastVerifiedBatchNum)
return false, nil
if proof.BatchNumber < batchNumberToVerify && proof.BatchNumberFinal >= batchNumberToVerify {
// We have a proof that contains some batches below the last batch verified, anyway can be eligible as final proof
log.Warnf("Proof %d-%d contains some batches lower than last batch verified %d. Check anyway if it is eligible", proof.BatchNumber, lastVerifiedBatchNum, batchNumberToVerify)
} else if proof.BatchNumberFinal < batchNumberToVerify {
// We have a proof that contains batches below that the last batch verified, we need to delete this proof
log.Warnf("Proof %d-%d lower than last batch verified %d. Delete it", proof.BatchNumber, lastVerifiedBatchNum, batchNumberToVerify)
err := a.State.DeleteGeneratedProofs(ctx, proof.BatchNumber, proof.BatchNumberFinal, nil)
if err != nil {
return false, fmt.Errorf("Failed to delete discarded proof, err: %v", err)
}
return false, nil
} else {
log.Debugf("Proof batch number %d is not the following to last verfied batch number %d", proof.BatchNumber, lastVerifiedBatchNum)
return false, nil
}
}

bComplete, err := a.State.CheckProofContainsCompleteSequences(ctx, proof, nil)
if err != nil {
return false, fmt.Errorf("failed to check if proof contains compete sequences, %w", err)
return false, fmt.Errorf("failed to check if proof contains complete sequences, %w", err)
}
if !bComplete {
log.Infof("Recursive proof %d-%d not eligible to be verified: not containing complete sequences", proof.BatchNumber, proof.BatchNumberFinal)
Expand Down Expand Up @@ -764,21 +775,22 @@ func (a *Aggregator) tryGenerateBatchProof(ctx context.Context, prover *prover.P
}

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

// startProofVerification sets to true the verifyingProof variable to indicate that there is a proof verification in progress
func (a *Aggregator) startProofVerification() {
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
a.verifyingProof = true
}

func (a *Aggregator) enableProofVerification() {
// endProofVerification set verifyingProof to false to indicate that there is not proof verification in progress
func (a *Aggregator) endProofVerification() {
a.TimeSendFinalProofMutex.Lock()
defer a.TimeSendFinalProofMutex.Unlock()
a.verifyingProof = false
Expand All @@ -788,7 +800,6 @@ func (a *Aggregator) enableProofVerification() {
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 Expand Up @@ -879,35 +890,38 @@ func (hc *healthChecker) Watch(req *grpchealth.HealthCheckRequest, server grpche
}

func (a *Aggregator) handleMonitoredTxResult(result ethtxmanager.MonitoredTxResult) {
resLog := log.WithFields("owner", ethTxManagerOwner, "txId", result.ID)
if result.Status == ethtxmanager.MonitoredTxStatusFailed {
resultLog := log.WithFields("owner", ethTxManagerOwner, "id", result.ID)
resultLog.Fatal("failed to send batch verification, TODO: review this fatal and define what to do in this case")
resLog.Fatal("failed to send batch verification, TODO: review this fatal and define what to do in this case")
}

//monitoredIDFormat = "proof-from-%v-to-%v"
// monitoredIDFormat: "proof-from-%v-to-%v"
idSlice := strings.Split(result.ID, "-")
proofBatchNumberStr := idSlice[2]
proofBatchNumber, err := strconv.ParseUint(proofBatchNumberStr, encoding.Base10, 0)
if err != nil {
log.Errorf("failed to read final proof batch number from monitored tx ID %v: %v", result.ID, err)
resLog.Errorf("failed to read final proof batch number from monitored tx: %v", err)
}

proofBatchNumberFinalStr := idSlice[4]
proofBatchNumberFinal, err := strconv.ParseUint(proofBatchNumberFinalStr, encoding.Base10, 0)
if err != nil {
log.Errorf("failed to read final proof batch number final from monitored tx ID %v: %v", result.ID, err)
resLog.Errorf("failed to read final proof batch number final from monitored tx: %v", err)
}

// network is synced with the final proof, we can safely delete the recursive proofs
err = a.State.DeleteGeneratedProofs(a.ctx, proofBatchNumber, proofBatchNumberFinal, nil)
if err != nil {
log.Errorf("Failed to store proof aggregation result, err: %v", err)
}
log := log.WithFields("txId", result.ID, "batches", fmt.Sprintf("%d-%d", proofBatchNumber, proofBatchNumberFinal))
log.Info("Final proof verified")

// wait for the synchronizer to catch up the verified batches
log.Debug("A final proof has been sent, waiting for the network to be synced")
for !a.isSynced(a.ctx, &proofBatchNumberFinal) {
log.Info("Waiting for synchronizer to sync...")
time.Sleep(a.cfg.RetryTime.Duration)
}

// network is synced with the final proof, we can safely delete the recursive proofs
err = a.State.DeleteGeneratedProofs(a.ctx, proofBatchNumber, proofBatchNumberFinal, nil)
if err != nil {
log.Errorf("failed to store proof aggregation result: %v", err)
}
}
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "300s"
WaitBlocksToUpdateGER = 10
WaitBlocksToConsiderGerFinal = 10
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s"
MinTimeToCloseBatch = "60s"
MaxTimeForBatchToBeOpen = "15s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
Expand Down
122 changes: 122 additions & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
IsTrustedSequencer = false

[Log]
Environment = "development" # "production" or "development"
Level = "debug"
Outputs = ["stderr"]

[StateDB]
User = "state_user"
Password = "state_password"
Name = "state_db"
Host = "zkevm-state-db"
Port = "5432"
EnableLog = false
MaxConns = 200

[PoolDB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "zkevm-pool-db"
Port = "5432"
EnableLog = false
MaxConns = 200

[Etherman]
URL = "http://your.L1node.url"
L1ChainID = 5
PoEAddr = "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
MaticAddr = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
GlobalExitRootManagerAddr = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
MultiGasProvider = false
[Etherman.Etherscan]
ApiKey = ""

[RPC]
Host = "0.0.0.0"
Port = 8545
ReadTimeoutInSec = 60
WriteTimeoutInSec = 60
MaxRequestsPerIPAndSecond = 5000
SequencerNodeURI = "https://internal.zkevm-test.net:2083/"
BroadcastURI = "internal.zkevm-test.net:61090"
DefaultSenderAddress = "0x1111111111111111111111111111111111111111"
[RPC.WebSockets]
Enabled = true
Port = 8546

[Synchronizer]
SyncInterval = "1s"
SyncChunkSize = 100
TrustedSequencerURI = ""
GenBlockNumber = 1

[Sequencer]
MaxSequenceSize = "2000000"
WaitPeriodPoolIsEmpty = "1s"
WaitPeriodSendSequence = "15s"
LastBatchVirtualizationTimeMaxWaitPeriod = "300s"
WaitBlocksToUpdateGER = 10
WaitBlocksToConsiderGerFinal = 10
ElapsedTimeToCloseBatchWithoutTxsDueToNewGER = "60s"
MinTimeToCloseBatch = "60s"
MaxTimeForBatchToBeOpen = "15s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
MaxTxsPerBatch = 150
MaxBatchBytesSize = 150000
MaxCumulativeGasUsed = 30000000
MaxKeccakHashes = 468
MaxPoseidonHashes = 279620
MaxPoseidonPaddings = 149796
MaxMemAligns = 262144
MaxArithmetics = 262144
MaxBinaries = 262144
MaxSteps = 8388608
MaxAllowedFailedCounter = 50
SenderAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
PrivateKeys = [{Path = "/pk/sequencer.keystore", Password = "testonly"}]
[Sequencer.ProfitabilityChecker]
SendBatchesEvenWhenNotProfitable = "true"

[Aggregator]
Host = "0.0.0.0"
Port = 50081
RetryTime = "5s"
VerifyProofInterval = "30s"
TxProfitabilityCheckerType = "acceptall"
TxProfitabilityMinReward = "1.1"
ProofStatePollingInterval = "5s"
SenderAddress = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

[EthTxManager]
PrivateKeys = [
{Path = "/pk/sequencer.keystore", Password = "testonly"},
{Path = "/pk/aggregator.keystore", Password = "testonly"}
]

[GasPriceEstimator]
Type = "default"
DefaultGasPriceWei = 1000000000

[MTServer]
Host = "0.0.0.0"
Port = 50060
StoreBackend = "PostgreSQL"

[MTClient]
URI = "zkevm-prover:50061"

[Executor]
URI = "zkevm-prover:50071"

[BroadcastServer]
Host = "0.0.0.0"
Port = 61090

[Metrics]
Host = "0.0.0.0"
Port = 9091
Enabled = false

17 changes: 8 additions & 9 deletions config/environments/public/public.genesis.config.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions config/environments/public/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ MaxConns = 200
[Etherman]
URL = "http://your.L1node.url"
L1ChainID = 5
PoEAddr = "0x14cB06e8dE2222912138F9a062E5a4d9F4821409"
MaticAddr = "0x4701Aa9471d7bfAc765D87dcb1Ea6BB23AD32733"
GlobalExitRootManagerAddr = "0x762d09Ed110ace4EaC94acbb67b1C35D16C3297b"
PoEAddr = "0x5e2e663A39205348cE985Bccc9673Ca25AeE727E"
MaticAddr = "0x957601b24ce513E64C7D77c9EB169501B7CC4D71"
GlobalExitRootManagerAddr = "0x715fC3A1422de7Ae2EA483d1DA8415E8dD45bAd3"
MultiGasProvider = false
[Etherman.Etherscan]
ApiKey = ""
Expand All @@ -48,7 +48,7 @@ DefaultSenderAddress = "0x1111111111111111111111111111111111111111"
SyncInterval = "2s"
SyncChunkSize = 10000
TrustedSequencerURI = ""
GenBlockNumber = 7710669
GenBlockNumber = 8168980

[GasPriceEstimator]
Type = "default"
Expand Down
1 change: 0 additions & 1 deletion config/environments/public/public.prover.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
"keccakScriptFile": "keccak_script.json",
"keccakPolsFile_DISABLED": "keccak_pols.json",
"keccakConnectionsFile": "keccak_connections.json",
"storageRomFile": "storage_sm_rom.json",
"starkInfoFile": "zkevm.starkinfo.json",
"starkInfoC12aFile": "zkevm.c12a.starkinfo.json",
"starkInfoRecursive1File": "zkevm.recursive1.starkinfo.json",
Expand Down
Loading

0 comments on commit 6c02d15

Please sign in to comment.