Skip to content

Commit

Permalink
add counter cache to opt process (#882)
Browse files Browse the repository at this point in the history
* add counter cache to opt process

* fix method to calculate Combined Counters

---------

Co-authored-by: Valentin Staykov <79150443+V-Staykov@users.noreply.github.com>
  • Loading branch information
louisliu2048 and V-Staykov authored Jul 31, 2024
1 parent 3d34df5 commit d7c13bd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
54 changes: 40 additions & 14 deletions core/vm/zk_batch_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ type BatchCounterCollector struct {
forkId uint16
unlimitedCounters bool
addonCounters *Counters

rlpCombinedCounters Counters
executionCombinedCounters Counters
processingCombinedCounters Counters

rlpCombinedCountersCache Counters
executionCombinedCountersCache Counters
processingCombinedCountersCache Counters
}

func NewBatchCounterCollector(smtMaxLevel int, forkId uint16, mcpReduction float64, unlimitedCounters bool, addonCounters *Counters) *BatchCounterCollector {
smtLevels := calculateSmtLevels(smtMaxLevel, 0, mcpReduction)
smtLevelsForTransaction := calculateSmtLevels(smtMaxLevel, 32, mcpReduction)
return &BatchCounterCollector{
bcc := BatchCounterCollector{
transactions: []*TransactionCounter{},
smtLevels: smtLevels,
smtLevelsForTransaction: smtLevelsForTransaction,
Expand All @@ -32,6 +40,12 @@ func NewBatchCounterCollector(smtMaxLevel int, forkId uint16, mcpReduction float
unlimitedCounters: unlimitedCounters,
addonCounters: addonCounters,
}

bcc.rlpCombinedCounters = bcc.NewCounters()
bcc.executionCombinedCounters = bcc.NewCounters()
bcc.processingCombinedCounters = bcc.NewCounters()

return &bcc
}

func (bcc *BatchCounterCollector) Clone() *BatchCounterCollector {
Expand All @@ -55,6 +69,10 @@ func (bcc *BatchCounterCollector) Clone() *BatchCounterCollector {
blockCount: bcc.blockCount,
forkId: bcc.forkId,
unlimitedCounters: bcc.unlimitedCounters,

rlpCombinedCounters: bcc.rlpCombinedCounters.Clone(),
executionCombinedCounters: bcc.executionCombinedCounters.Clone(),
processingCombinedCounters: bcc.processingCombinedCounters.Clone(),
}
}

Expand All @@ -69,6 +87,7 @@ func (bcc *BatchCounterCollector) AddNewTransactionCounters(txCounters *Transact
}

bcc.transactions = append(bcc.transactions, txCounters)
bcc.UpdateRlpCountersCache(txCounters)

return bcc.CheckForOverflow(false) //no need to calculate the merkle proof here
}
Expand Down Expand Up @@ -202,19 +221,10 @@ func (bcc *BatchCounterCollector) CombineCollectors(verifyMerkleProof bool) (Cou
}
}

for _, tx := range bcc.transactions {
for k, v := range tx.rlpCounters.counters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
for k, v := range tx.executionCounters.counters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
for k, v := range tx.processingCounters.counters {
combined[k].used += v.used
combined[k].remaining -= v.used
}
for k, _ := range combined {
val := bcc.rlpCombinedCounters[k].used + bcc.executionCombinedCounters[k].used + bcc.processingCombinedCounters[k].used
combined[k].used += val
combined[k].remaining -= val
}

return combined, nil
Expand Down Expand Up @@ -260,3 +270,19 @@ func (bcc *BatchCounterCollector) CombineCollectorsNoChanges(verifyMerkleProof b

return combined
}

func (bcc *BatchCounterCollector) UpdateRlpCountersCache(txCounters *TransactionCounter) {
for k, v := range txCounters.rlpCounters.counters {
bcc.rlpCombinedCounters[k].used += v.used
}
}

func (bcc *BatchCounterCollector) UpdateExecutionAndProcessingCountersCache(txCounters *TransactionCounter) {
for k, v := range txCounters.executionCounters.counters {
bcc.executionCombinedCounters[k].used += v.used
}

for k, v := range txCounters.processingCounters.counters {
bcc.processingCombinedCounters[k].used += v.used
}
}
10 changes: 10 additions & 0 deletions core/vm/zk_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func (c *Counters) GetPoseidonPaddings() *Counter {
return (*c)[D]
}

func (cc Counters) Clone() Counters {
var clonedCounters Counters = Counters{}

for k, v := range cc {
clonedCounters[k] = v.Clone()
}

return clonedCounters
}

type CounterKey string

var (
Expand Down
1 change: 1 addition & 0 deletions zk/stages/stage_sequence_execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func attemptAddTransaction(
return nil, nil, false, err
}

batchCounters.UpdateExecutionAndProcessingCountersCache(txCounters)
// now that we have executed we can check again for an overflow
if overflow, err = batchCounters.CheckForOverflow(l1InfoIndex != 0); err != nil {
return nil, nil, false, err
Expand Down
2 changes: 2 additions & 0 deletions zk/tests/zk_counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ func runTest(t *testing.T, test vector, err error, fileName string, idx int) {
if err = txCounters.ProcessTx(ibs, result.ReturnData); err != nil {
t.Fatal(err)
}

batchCollector.UpdateExecutionAndProcessingCountersCache(txCounters)
}
}

Expand Down

0 comments on commit d7c13bd

Please sign in to comment.