Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
reserved zk counters (#3343)
Browse files Browse the repository at this point in the history
* reserved zk counters

* new prover image

* udpate counters names

* udpate counters names

* add gas used to reserved zkCounters

* add ReservedZKCounters to pool trasactions
  • Loading branch information
ToniRamirezM committed Feb 22, 2024
1 parent 0204aff commit f567269
Show file tree
Hide file tree
Showing 20 changed files with 1,030 additions and 896 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:v5.0.0-RC4
image: hermeznetwork/zkevm-prover:v5.0.0-RC5
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
36 changes: 18 additions & 18 deletions pool/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ func TestIsWithinConstraints(t *testing.T) {
{
desc: "All constraints within limits",
counters: state.ZKCounters{
GasUsed: 300,
UsedKeccakHashes: 50,
UsedPoseidonHashes: 100,
UsedPoseidonPaddings: 75,
UsedMemAligns: 500,
UsedArithmetics: 1000,
UsedBinaries: 2000,
UsedSteps: 2000,
UsedSha256Hashes_V2: 4000,
GasUsed: 300,
KeccakHashes: 50,
PoseidonHashes: 100,
PoseidonPaddings: 75,
MemAligns: 500,
Arithmetics: 1000,
Binaries: 2000,
Steps: 2000,
Sha256Hashes_V2: 4000,
},
expected: true,
},
{
desc: "All constraints exceed limits",
counters: state.ZKCounters{
GasUsed: 600,
UsedKeccakHashes: 150,
UsedPoseidonHashes: 300,
UsedPoseidonPaddings: 200,
UsedMemAligns: 2000,
UsedArithmetics: 3000,
UsedBinaries: 4000,
UsedSteps: 5000,
UsedSha256Hashes_V2: 6000,
GasUsed: 600,
KeccakHashes: 150,
PoseidonHashes: 300,
PoseidonPaddings: 200,
MemAligns: 2000,
Arithmetics: 3000,
Binaries: 4000,
Steps: 5000,
Sha256Hashes_V2: 6000,
},
expected: false,
},
Expand Down
56 changes: 28 additions & 28 deletions pool/pgpoolstorage/pgpoolstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
gasPrice,
nonce,
tx.GasUsed,
tx.UsedKeccakHashes,
tx.UsedPoseidonHashes,
tx.UsedPoseidonPaddings,
tx.UsedMemAligns,
tx.UsedArithmetics,
tx.UsedBinaries,
tx.UsedSteps,
tx.UsedSha256Hashes_V2,
tx.KeccakHashes,
tx.PoseidonHashes,
tx.PoseidonPaddings,
tx.MemAligns,
tx.Arithmetics,
tx.Binaries,
tx.Steps,
tx.Sha256Hashes_V2,
tx.ReceivedAt,
fromAddress,
tx.IsWIP,
Expand Down Expand Up @@ -333,15 +333,15 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt
tx.Status = pool.TxStatus(status)
tx.ReceivedAt = receivedAt
tx.ZKCounters = state.ZKCounters{
GasUsed: cumulativeGasUsed,
UsedKeccakHashes: usedKeccakHashes,
UsedPoseidonHashes: usedPoseidonHashes,
UsedPoseidonPaddings: usedPoseidonPaddings,
UsedMemAligns: usedMemAligns,
UsedArithmetics: usedArithmetics,
UsedBinaries: usedBinaries,
UsedSteps: usedSteps,
UsedSha256Hashes_V2: usedSHA256Hashes,
GasUsed: cumulativeGasUsed,
KeccakHashes: usedKeccakHashes,
PoseidonHashes: usedPoseidonHashes,
PoseidonPaddings: usedPoseidonPaddings,
MemAligns: usedMemAligns,
Arithmetics: usedArithmetics,
Binaries: usedBinaries,
Steps: usedSteps,
Sha256Hashes_V2: usedSHA256Hashes,
}
tx.IsWIP = isWIP
tx.IP = ip
Expand Down Expand Up @@ -705,14 +705,14 @@ func scanTx(rows pgx.Rows) (*pool.Transaction, error) {
tx.IsWIP = isWIP
tx.IP = ip
tx.ZKCounters.GasUsed = cumulativeGasUsed
tx.ZKCounters.UsedKeccakHashes = usedKeccakHashes
tx.ZKCounters.UsedPoseidonHashes = usedPoseidonHashes
tx.ZKCounters.UsedPoseidonPaddings = usedPoseidonPaddings
tx.ZKCounters.UsedMemAligns = usedMemAligns
tx.ZKCounters.UsedArithmetics = usedArithmetics
tx.ZKCounters.UsedBinaries = usedBinaries
tx.ZKCounters.UsedSteps = usedSteps
tx.ZKCounters.UsedSha256Hashes_V2 = usedSHA256Hashes
tx.ZKCounters.KeccakHashes = usedKeccakHashes
tx.ZKCounters.PoseidonHashes = usedPoseidonHashes
tx.ZKCounters.PoseidonPaddings = usedPoseidonPaddings
tx.ZKCounters.MemAligns = usedMemAligns
tx.ZKCounters.Arithmetics = usedArithmetics
tx.ZKCounters.Binaries = usedBinaries
tx.ZKCounters.Steps = usedSteps
tx.ZKCounters.Sha256Hashes_V2 = usedSHA256Hashes
tx.FailedReason = failedReason

return tx, nil
Expand All @@ -733,9 +733,9 @@ func (p *PostgresPoolStorage) GetTxZkCountersByHash(ctx context.Context, hash co

sql := `SELECT cumulative_gas_used, used_keccak_hashes, used_poseidon_hashes, used_poseidon_paddings, used_mem_aligns,
used_arithmetics, used_binaries, used_steps, used_sha256_hashes FROM pool.transaction WHERE hash = $1`
err := p.db.QueryRow(ctx, sql, hash.String()).Scan(&zkCounters.GasUsed, &zkCounters.UsedKeccakHashes,
&zkCounters.UsedPoseidonHashes, &zkCounters.UsedPoseidonPaddings,
&zkCounters.UsedMemAligns, &zkCounters.UsedArithmetics, &zkCounters.UsedBinaries, &zkCounters.UsedSteps, &zkCounters.UsedSha256Hashes_V2)
err := p.db.QueryRow(ctx, sql, hash.String()).Scan(&zkCounters.GasUsed, &zkCounters.KeccakHashes,
&zkCounters.PoseidonHashes, &zkCounters.PoseidonPaddings,
&zkCounters.MemAligns, &zkCounters.Arithmetics, &zkCounters.Binaries, &zkCounters.Steps, &zkCounters.Sha256Hashes_V2)
if errors.Is(err, pgx.ErrNoRows) {
return nil, pool.ErrNotFound
} else if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Transaction struct {
types.Transaction
Status TxStatus
state.ZKCounters
ReservedZKCounters state.ZKCounters
ReceivedAt time.Time
PreprocessedStateRoot common.Hash
IsWIP bool
Expand Down
10 changes: 10 additions & 0 deletions proto/src/proto/executor/v1/executor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ message ProcessBatchResponseV2 {
uint64 fork_id = 22;
uint32 invalid_batch = 23;
RomError error_rom = 24;
uint32 cnt_reserve_keccak_hashes = 25;
uint32 cnt_reserve_poseidon_hashes = 26;
uint32 cnt_reserve_poseidon_paddings = 27;
uint32 cnt_reserve_mem_aligns = 28;
uint32 cnt_reserve_arithmetics = 29;
uint32 cnt_reserve_binaries = 30;
uint32 cnt_reserve_steps = 31;
uint32 cnt_reserve_sha256_hashes = 32;
}

// Trace configuration request params
Expand Down Expand Up @@ -527,6 +535,8 @@ message ProcessTransactionResponseV2 {
uint32 has_gasprice_opcode = 19;
// Flag to indicate if opcode 'BALANCE' has been called
uint32 has_balance_opcode = 20;
// Receipt status of the transaction, 1 = success, 0 = failure
uint32 status = 21;
}

message LogV2 {
Expand Down
56 changes: 28 additions & 28 deletions sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,34 +464,34 @@ func (f *finalizer) maxTxsPerBatchReached(batch *Batch) bool {

// isBatchResourcesMarginExhausted checks if one of resources of the batch has reached the exhausted margin and returns the name of the exhausted resource
func (f *finalizer) isBatchResourcesMarginExhausted(resources state.BatchResources) (bool, string) {
zkCounters := resources.ZKCounters
zkCounters := resources.UsedZKCounters
result := false
resourceName := ""
if resources.Bytes <= f.getConstraintThresholdUint64(f.batchConstraints.MaxBatchBytesSize) {
resourceName = "Bytes"
result = true
} else if zkCounters.UsedSteps <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSteps) {
} else if zkCounters.Steps <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSteps) {
resourceName = "Steps"
result = true
} else if zkCounters.UsedPoseidonPaddings <= f.getConstraintThresholdUint32(f.batchConstraints.MaxPoseidonPaddings) {
} else if zkCounters.PoseidonPaddings <= f.getConstraintThresholdUint32(f.batchConstraints.MaxPoseidonPaddings) {
resourceName = "PoseidonPaddings"
result = true
} else if zkCounters.UsedBinaries <= f.getConstraintThresholdUint32(f.batchConstraints.MaxBinaries) {
} else if zkCounters.Binaries <= f.getConstraintThresholdUint32(f.batchConstraints.MaxBinaries) {
resourceName = "Binaries"
result = true
} else if zkCounters.UsedKeccakHashes <= f.getConstraintThresholdUint32(f.batchConstraints.MaxKeccakHashes) {
} else if zkCounters.KeccakHashes <= f.getConstraintThresholdUint32(f.batchConstraints.MaxKeccakHashes) {
resourceName = "KeccakHashes"
result = true
} else if zkCounters.UsedArithmetics <= f.getConstraintThresholdUint32(f.batchConstraints.MaxArithmetics) {
} else if zkCounters.Arithmetics <= f.getConstraintThresholdUint32(f.batchConstraints.MaxArithmetics) {
resourceName = "Arithmetics"
result = true
} else if zkCounters.UsedMemAligns <= f.getConstraintThresholdUint32(f.batchConstraints.MaxMemAligns) {
} else if zkCounters.MemAligns <= f.getConstraintThresholdUint32(f.batchConstraints.MaxMemAligns) {
resourceName = "MemAligns"
result = true
} else if zkCounters.GasUsed <= f.getConstraintThresholdUint64(f.batchConstraints.MaxCumulativeGasUsed) {
resourceName = "CumulativeGas"
result = true
} else if zkCounters.UsedSha256Hashes_V2 <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSHA256Hashes) {
} else if zkCounters.Sha256Hashes_V2 <= f.getConstraintThresholdUint32(f.batchConstraints.MaxSHA256Hashes) {
resourceName = "SHA256Hashes"
result = true
}
Expand All @@ -512,16 +512,16 @@ func (f *finalizer) getConstraintThresholdUint32(input uint32) uint32 {
// getUsedBatchResources calculates and returns the used resources of a batch from remaining resources
func getUsedBatchResources(constraints state.BatchConstraintsCfg, remainingResources state.BatchResources) state.BatchResources {
return state.BatchResources{
ZKCounters: state.ZKCounters{
GasUsed: constraints.MaxCumulativeGasUsed - remainingResources.ZKCounters.GasUsed,
UsedKeccakHashes: constraints.MaxKeccakHashes - remainingResources.ZKCounters.UsedKeccakHashes,
UsedPoseidonHashes: constraints.MaxPoseidonHashes - remainingResources.ZKCounters.UsedPoseidonHashes,
UsedPoseidonPaddings: constraints.MaxPoseidonPaddings - remainingResources.ZKCounters.UsedPoseidonPaddings,
UsedMemAligns: constraints.MaxMemAligns - remainingResources.ZKCounters.UsedMemAligns,
UsedArithmetics: constraints.MaxArithmetics - remainingResources.ZKCounters.UsedArithmetics,
UsedBinaries: constraints.MaxBinaries - remainingResources.ZKCounters.UsedBinaries,
UsedSteps: constraints.MaxSteps - remainingResources.ZKCounters.UsedSteps,
UsedSha256Hashes_V2: constraints.MaxSHA256Hashes - remainingResources.ZKCounters.UsedSha256Hashes_V2,
UsedZKCounters: state.ZKCounters{
GasUsed: constraints.MaxCumulativeGasUsed - remainingResources.UsedZKCounters.GasUsed,
KeccakHashes: constraints.MaxKeccakHashes - remainingResources.UsedZKCounters.KeccakHashes,
PoseidonHashes: constraints.MaxPoseidonHashes - remainingResources.UsedZKCounters.PoseidonHashes,
PoseidonPaddings: constraints.MaxPoseidonPaddings - remainingResources.UsedZKCounters.PoseidonPaddings,
MemAligns: constraints.MaxMemAligns - remainingResources.UsedZKCounters.MemAligns,
Arithmetics: constraints.MaxArithmetics - remainingResources.UsedZKCounters.Arithmetics,
Binaries: constraints.MaxBinaries - remainingResources.UsedZKCounters.Binaries,
Steps: constraints.MaxSteps - remainingResources.UsedZKCounters.Steps,
Sha256Hashes_V2: constraints.MaxSHA256Hashes - remainingResources.UsedZKCounters.Sha256Hashes_V2,
},
Bytes: constraints.MaxBatchBytesSize - remainingResources.Bytes,
}
Expand All @@ -530,16 +530,16 @@ func getUsedBatchResources(constraints state.BatchConstraintsCfg, remainingResou
// getMaxRemainingResources returns the max resources that can be used in a batch
func getMaxRemainingResources(constraints state.BatchConstraintsCfg) state.BatchResources {
return state.BatchResources{
ZKCounters: state.ZKCounters{
GasUsed: constraints.MaxCumulativeGasUsed,
UsedKeccakHashes: constraints.MaxKeccakHashes,
UsedPoseidonHashes: constraints.MaxPoseidonHashes,
UsedPoseidonPaddings: constraints.MaxPoseidonPaddings,
UsedMemAligns: constraints.MaxMemAligns,
UsedArithmetics: constraints.MaxArithmetics,
UsedBinaries: constraints.MaxBinaries,
UsedSteps: constraints.MaxSteps,
UsedSha256Hashes_V2: constraints.MaxSHA256Hashes,
UsedZKCounters: state.ZKCounters{
GasUsed: constraints.MaxCumulativeGasUsed,
KeccakHashes: constraints.MaxKeccakHashes,
PoseidonHashes: constraints.MaxPoseidonHashes,
PoseidonPaddings: constraints.MaxPoseidonPaddings,
MemAligns: constraints.MaxMemAligns,
Arithmetics: constraints.MaxArithmetics,
Binaries: constraints.MaxBinaries,
Steps: constraints.MaxSteps,
Sha256Hashes_V2: constraints.MaxSHA256Hashes,
},
Bytes: constraints.MaxBatchBytesSize,
}
Expand Down
10 changes: 5 additions & 5 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ func (f *finalizer) processTransaction(ctx context.Context, tx *TxTracker, first
// Save values for later logging
tx.EGPLog.L1GasPrice = tx.L1GasPrice
tx.EGPLog.L2GasPrice = txL2GasPrice
tx.EGPLog.GasUsedFirst = tx.BatchResources.ZKCounters.GasUsed
tx.EGPLog.GasUsedFirst = tx.BatchResources.UsedZKCounters.GasUsed
tx.EGPLog.GasPrice.Set(txGasPrice)

// Calculate EffectiveGasPrice
egp, err := f.effectiveGasPrice.CalculateEffectiveGasPrice(tx.RawTx, txGasPrice, tx.BatchResources.ZKCounters.GasUsed, tx.L1GasPrice, txL2GasPrice)
egp, err := f.effectiveGasPrice.CalculateEffectiveGasPrice(tx.RawTx, txGasPrice, tx.BatchResources.UsedZKCounters.GasUsed, tx.L1GasPrice, txL2GasPrice)
if err != nil {
if f.effectiveGasPrice.IsEnabled() {
return nil, err
Expand Down Expand Up @@ -529,7 +529,7 @@ func (f *finalizer) handleProcessTransactionResponse(ctx context.Context, tx *Tx

// Check remaining resources

overflow, overflowResource := f.wipBatch.imRemainingResources.Sub(state.BatchResources{ZKCounters: result.UsedZkCounters, Bytes: uint64(len(tx.RawTx))})
overflow, overflowResource := f.wipBatch.imRemainingResources.Sub(state.BatchResources{UsedZKCounters: result.UsedZkCounters, Bytes: uint64(len(tx.RawTx))})
if overflow {
log.Infof("current tx %s exceeds the remaining batch resources, overflow resource: %s, updating metadata for tx in worker and continuing", tx.HashStr, overflowResource)
if !f.batchConstraints.IsWithinConstraints(result.UsedZkCounters) {
Expand Down Expand Up @@ -747,8 +747,8 @@ func (f *finalizer) checkIfProverRestarted(proverID string) {
// logZKCounters returns a string with all the zkCounters values
func (f *finalizer) logZKCounters(counters state.ZKCounters) string {
return fmt.Sprintf("{gasUsed: %d, keccakHashes: %d, poseidonHashes: %d, poseidonPaddings: %d, memAligns: %d, arithmetics: %d, binaries: %d, sha256Hashes: %d, steps: %d}",
counters.GasUsed, counters.UsedKeccakHashes, counters.UsedPoseidonHashes, counters.UsedPoseidonPaddings, counters.UsedMemAligns, counters.UsedArithmetics,
counters.UsedBinaries, counters.UsedSha256Hashes_V2, counters.UsedSteps)
counters.GasUsed, counters.KeccakHashes, counters.PoseidonHashes, counters.PoseidonPaddings, counters.MemAligns, counters.Arithmetics,
counters.Binaries, counters.Sha256Hashes_V2, counters.Steps)
}

// Halt halts the finalizer
Expand Down
Loading

0 comments on commit f567269

Please sign in to comment.