Skip to content

Commit

Permalink
check codesize
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Aug 15, 2024
1 parent 57b67d7 commit 7fc6397
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions miner/scroll_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ func (w *worker) commit(force bool) (common.Hash, error) {
} else {
commitReasonCCCCounter.Inc(1)
}
log.Info("Go CCC", "hash", block.Hash(), "rc", w.current.cccLogger.RowConsumption(), "bytecodes", w.current.cccLogger.Codes())
w.current = nil
return block.Hash(), nil
}
Expand Down
39 changes: 33 additions & 6 deletions rollup/ccc/async_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ func (c *AsyncChecker) checkerTask(block *types.Block, ccc *Checker, forkCtx con
}

var err error
var failingRc *types.RowConsumption
bytecodesMap := make(map[common.Hash]uint64)
failingCallback := func() {
failCounter.Inc(1)
log.Info("Rust CCC", "hash", block.Hash(), "rc", failingRc, "err", err, "bytecodes", bytecodesMap)
if isForkStillActive(forkCtx) {
// we failed the CCC check, cancel the context to signal all tasks preceding this one to terminate early
forkCtxCancelFunc()
Expand Down Expand Up @@ -178,7 +181,29 @@ func (c *AsyncChecker) checkerTask(block *types.Block, ccc *Checker, forkCtx con
}

var curRc *types.RowConsumption
curRc, err = c.checkTxAndApply(parent, header, statedb, gasPool, tx, ccc)
var bytecodes []*types.BytecodeTrace
curRc, bytecodes, err = c.checkTxAndApply(parent, header, statedb, gasPool, tx, ccc)

duplicate := false
internalDuplicate := false
thisRoundSize := uint64(0)
thisRound := uint64(0)
interalMap := make(map[common.Hash]bool)

for _, bytecode := range bytecodes {
_, internalFound := interalMap[bytecode.PoseidonCodeHash]
_, found := bytecodesMap[bytecode.PoseidonCodeHash]
duplicate = duplicate || found
internalDuplicate = internalDuplicate || internalFound
if !found {
thisRound += 1
thisRoundSize += uint64(len(([]byte)(bytecode.Code))) + 1
}
bytecodesMap[bytecode.PoseidonCodeHash] = uint64(len(([]byte)(bytecode.Code)))
}

log.Info("step", "blockhash", block.Hash(), "indx", txIdx, "duplicate", duplicate, "internalduplicate", internalDuplicate, "thisRound", thisRound, "thisRoundSize", thisRoundSize, "rc", curRc)

if err != nil {
err = &ErrorWithTxnIdx{
TxIdx: uint(txIdx),
Expand All @@ -187,6 +212,7 @@ func (c *AsyncChecker) checkerTask(block *types.Block, ccc *Checker, forkCtx con
// by this txn alone is enough to overflow the circuit, skip
ShouldSkip: txIdx == 0 || curRc.Difference(*accRc).IsOverflown(),
}
failingRc = curRc
return failingCallback
}
accRc = curRc
Expand All @@ -196,12 +222,13 @@ func (c *AsyncChecker) checkerTask(block *types.Block, ccc *Checker, forkCtx con
if isForkStillActive(forkCtx) {
// all good, write the row consumption
log.Debug("CCC passed", "blockhash", block.Hash(), "height", block.NumberU64())
log.Info("Rust CCC", "hash", block.Hash(), "rc", accRc, "err", err, "bytecodes", bytecodesMap)
rawdb.WriteBlockRowConsumption(c.bc.Database(), block.Hash(), accRc)
}
}
}

func (c *AsyncChecker) checkTxAndApply(parent *types.Block, header *types.Header, state *state.StateDB, gasPool *core.GasPool, tx *types.Transaction, ccc *Checker) (*types.RowConsumption, error) {
func (c *AsyncChecker) checkTxAndApply(parent *types.Block, header *types.Header, state *state.StateDB, gasPool *core.GasPool, tx *types.Transaction, ccc *Checker) (*types.RowConsumption, []*types.BytecodeTrace, error) {
// don't commit the state during tracing for circuit capacity checker, otherwise we cannot revert.
// and even if we don't commit the state, the `refund` value will still be correct, as explained in `CommitTransaction`
commitStateAfterApply := false
Expand All @@ -220,20 +247,20 @@ func (c *AsyncChecker) checkTxAndApply(parent *types.Block, header *types.Header
// revert to snapshot for calling `core.ApplyMessage` again, (both `traceEnv.GetBlockTrace` & `core.ApplyTransaction` will call `core.ApplyMessage`)
state.RevertToSnapshot(snap)
if err != nil {
return nil, err
return nil, nil, err
}

rc, err := ccc.ApplyTransaction(trace)
if err != nil {
return rc, err
return rc, trace.Bytecodes, err
}

_, err = core.ApplyTransaction(c.bc.Config(), c.bc, nil /* coinbase will default to chainConfig.Scroll.FeeVaultAddress */, gasPool,
state, header, tx, &header.GasUsed, *c.bc.GetVMConfig())
if err != nil {
return nil, err
return nil, nil, err
}
return rc, nil
return rc, trace.Bytecodes, nil
}

// ScheduleError forces a block to error on a given transaction index
Expand Down
4 changes: 4 additions & 0 deletions rollup/ccc/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ pub mod checker {
bail!("traces.tx_storage_trace.len() != 1");
}

for code in &traces.codes {
println!("hash: {} size: {}", code.hash, code.code.len());
}

let r = panic::catch_unwind(|| {
CHECKERS
.get_mut()
Expand Down
8 changes: 8 additions & 0 deletions rollup/ccc/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Logger struct {

currentEnv *vm.EVM
codesAccessed map[common.Hash]bool
codeSize map[common.Hash]uint64

evmUsage uint64
stateUsage uint64
Expand All @@ -27,20 +28,27 @@ func NewLogger(limitPerCircuit uint64) *Logger {
return &Logger{
limitPerCircuit: limitPerCircuit,
codesAccessed: make(map[common.Hash]bool),
codeSize: make(map[common.Hash]uint64),
}
}

// Snapshot creates an independent copy of the logger
func (l *Logger) Snapshot() *Logger {
newL := *l
newL.codesAccessed = maps.Clone(newL.codesAccessed)
newL.codeSize = maps.Clone(newL.codeSize)
return &newL
}

func (l *Logger) Codes() map[common.Hash]uint64 {
return maps.Clone(l.codeSize)
}

func (l *Logger) logBytecodeAccessAt(addr common.Address) {
codeHash := l.currentEnv.StateDB.GetKeccakCodeHash(addr)
if codeHash != (common.Hash{}) && !l.codesAccessed[codeHash] {
l.bytecodeUsage += l.currentEnv.StateDB.GetCodeSize(addr)
l.codeSize[codeHash] = l.currentEnv.StateDB.GetCodeSize(addr)
l.codesAccessed[codeHash] = true
}
}
Expand Down

0 comments on commit 7fc6397

Please # to comment.