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 counter cache to opt process #882

Merged
merged 7 commits into from
Jul 31, 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
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
Loading