refactor: rename CCC package for less repetition (#967)

This commit is contained in:
Ömer Faruk Irmak 2024-08-09 08:21:14 +03:00 committed by GitHub
parent 3f5fb275ef
commit 2411ae063e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 63 additions and 1717 deletions

View file

@ -45,7 +45,7 @@ jobs:
- name: Build
run: |
make libzkp
sudo cp ./rollup/circuitcapacitychecker/libzkp/libzkp.so /usr/local/lib/
sudo cp ./rollup/ccc/libzkp/libzkp.so /usr/local/lib/
make geth
check:
if: github.event.pull_request.draft == false

View file

@ -9,15 +9,15 @@ FROM scrolltech/go-rust-builder:go-1.21-rust-nightly-2023-12-03 as chef
WORKDIR app
FROM chef as planner
COPY ./rollup/circuitcapacitychecker/libzkp/ .
COPY ./rollup/ccc/libzkp/ .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef as zkp-builder
COPY ./rollup/circuitcapacitychecker/libzkp/rust-toolchain ./
COPY ./rollup/ccc/libzkp/rust-toolchain ./
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY ./rollup/circuitcapacitychecker/libzkp .
COPY ./rollup/ccc/libzkp .
RUN cargo clean
RUN cargo build --release

View file

@ -9,7 +9,7 @@ GO ?= latest
GORUN = env GO111MODULE=on go run
libzkp:
cd $(PWD)/rollup/circuitcapacitychecker/libzkp && make libzkp
cd $(PWD)/rollup/ccc/libzkp && make libzkp
nccc_geth: ## geth without circuit capacity checker
$(GORUN) build/ci.go install ./cmd/geth

View file

@ -30,7 +30,7 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
"github.com/scroll-tech/go-ethereum/rollup/ccc"
"github.com/scroll-tech/go-ethereum/trie"
)
@ -55,7 +55,7 @@ type BlockValidator struct {
checkCircuitCapacity bool // whether enable circuit capacity check
cMu sync.Mutex // mutex for circuit capacity checker
tracer tracerWrapper // scroll tracer wrapper
circuitCapacityChecker *circuitcapacitychecker.CircuitCapacityChecker // circuit capacity checker instance
circuitCapacityChecker *ccc.Checker // circuit capacity checker instance
}
// NewBlockValidator returns a new block validator which is safe for re-use
@ -75,7 +75,7 @@ type tracerWrapper interface {
func (v *BlockValidator) SetupTracerAndCircuitCapacityChecker(tracer tracerWrapper) {
v.checkCircuitCapacity = true
v.tracer = tracer
v.circuitCapacityChecker = circuitcapacitychecker.NewCircuitCapacityChecker(true)
v.circuitCapacityChecker = ccc.NewChecker(true)
log.Info("new CircuitCapacityChecker in BlockValidator", "ID", v.circuitCapacityChecker.ID)
}

View file

@ -36,7 +36,7 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
"github.com/scroll-tech/go-ethereum/rollup/ccc"
"github.com/scroll-tech/go-ethereum/rollup/fees"
"github.com/scroll-tech/go-ethereum/rollup/pipeline"
"github.com/scroll-tech/go-ethereum/trie"
@ -128,7 +128,7 @@ type worker struct {
// External functions
isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner.
circuitCapacityChecker *circuitcapacitychecker.CircuitCapacityChecker
circuitCapacityChecker *ccc.Checker
prioritizedTx *prioritizedTransaction
// Test hooks
@ -148,7 +148,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
exitCh: make(chan struct{}),
startCh: make(chan struct{}, 1),
circuitCapacityChecker: circuitcapacitychecker.NewCircuitCapacityChecker(true),
circuitCapacityChecker: ccc.NewChecker(true),
}
log.Info("created new worker", "CircuitCapacityChecker ID", worker.circuitCapacityChecker.ID)
@ -176,7 +176,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
// getCCC returns a pointer to this worker's CCC instance.
// Only used in tests.
func (w *worker) getCCC() *circuitcapacitychecker.CircuitCapacityChecker {
func (w *worker) getCCC() *ccc.Checker {
return w.circuitCapacityChecker
}
@ -586,16 +586,16 @@ func (w *worker) handlePipelineResult(res *pipeline.Result) error {
switch {
case res.OverflowingTx.IsL1MessageTx() &&
errors.Is(res.CCCErr, circuitcapacitychecker.ErrBlockRowConsumptionOverflow):
errors.Is(res.CCCErr, ccc.ErrBlockRowConsumptionOverflow):
l1TxRowConsumptionOverflowCounter.Inc(1)
case !res.OverflowingTx.IsL1MessageTx() &&
errors.Is(res.CCCErr, circuitcapacitychecker.ErrBlockRowConsumptionOverflow):
errors.Is(res.CCCErr, ccc.ErrBlockRowConsumptionOverflow):
l2TxRowConsumptionOverflowCounter.Inc(1)
case res.OverflowingTx.IsL1MessageTx() &&
errors.Is(res.CCCErr, circuitcapacitychecker.ErrUnknown):
errors.Is(res.CCCErr, ccc.ErrUnknown):
l1TxCccUnknownErrCounter.Inc(1)
case !res.OverflowingTx.IsL1MessageTx() &&
errors.Is(res.CCCErr, circuitcapacitychecker.ErrUnknown):
errors.Is(res.CCCErr, ccc.ErrUnknown):
l2TxCccUnknownErrCounter.Inc(1)
}
}

View file

@ -38,7 +38,7 @@ import (
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/event"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
"github.com/scroll-tech/go-ethereum/rollup/ccc"
"github.com/scroll-tech/go-ethereum/rollup/sync_service"
)
@ -788,7 +788,7 @@ func TestOversizedTxThenNormal(t *testing.T) {
switch blockNum {
case 0:
// schedule to skip 2nd call to ccc
w.getCCC().ScheduleError(2, circuitcapacitychecker.ErrBlockRowConsumptionOverflow)
w.getCCC().ScheduleError(2, ccc.ErrBlockRowConsumptionOverflow)
return false
case 1:
// include #0, fail on #1, then seal the block
@ -803,7 +803,7 @@ func TestOversizedTxThenNormal(t *testing.T) {
assert.Equal(uint64(1), *queueIndex)
// schedule to skip next call to ccc
w.getCCC().ScheduleError(1, circuitcapacitychecker.ErrBlockRowConsumptionOverflow)
w.getCCC().ScheduleError(1, ccc.ErrBlockRowConsumptionOverflow)
return false
case 2:
@ -869,7 +869,7 @@ func TestPrioritizeOverflowTx(t *testing.T) {
// Process 2 transactions with gas order: tx0 > tx1, tx1 will overflow.
b.txPool.AddRemotesSync([]*types.Transaction{tx0, tx1})
w.getCCC().ScheduleError(2, circuitcapacitychecker.ErrBlockRowConsumptionOverflow)
w.getCCC().ScheduleError(2, ccc.ErrBlockRowConsumptionOverflow)
w.start()
select {
@ -903,7 +903,7 @@ func TestPrioritizeOverflowTx(t *testing.T) {
t.Fatalf("timeout")
}
w.getCCC().Skip(tx4.Hash(), circuitcapacitychecker.ErrBlockRowConsumptionOverflow)
w.getCCC().Skip(tx4.Hash(), ccc.ErrBlockRowConsumptionOverflow)
assert.Equal([]error{nil, nil, nil}, b.txPool.AddRemotesSync([]*types.Transaction{tx3, tx4, tx5}))
w.start()

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
//go:build circuit_capacity_checker
package circuitcapacitychecker
package ccc
/*
#cgo LDFLAGS: -lm -ldl -lzkp
@ -22,7 +22,7 @@ import (
"github.com/scroll-tech/go-ethereum/metrics"
)
// mutex for concurrent CircuitCapacityChecker creations
// mutex for concurrent Checker creations
var (
creationMu sync.Mutex
encodeTimer = metrics.NewRegisteredTimer("ccc/encode", nil)
@ -32,26 +32,26 @@ func init() {
C.init()
}
type CircuitCapacityChecker struct {
// mutex for each CircuitCapacityChecker itself
type Checker struct {
// mutex for each Checker itself
sync.Mutex
ID uint64
jsonBuffer bytes.Buffer
}
// NewCircuitCapacityChecker creates a new CircuitCapacityChecker
func NewCircuitCapacityChecker(lightMode bool) *CircuitCapacityChecker {
// NewChecker creates a new Checker
func NewChecker(lightMode bool) *Checker {
creationMu.Lock()
defer creationMu.Unlock()
id := C.new_circuit_capacity_checker()
ccc := &CircuitCapacityChecker{ID: uint64(id)}
ccc := &Checker{ID: uint64(id)}
ccc.SetLightMode(lightMode)
return ccc
}
// Reset resets a CircuitCapacityChecker
func (ccc *CircuitCapacityChecker) Reset() {
func (ccc *Checker) Reset() {
ccc.Lock()
defer ccc.Unlock()
@ -59,7 +59,7 @@ func (ccc *CircuitCapacityChecker) Reset() {
}
// ApplyTransaction appends a tx's wrapped BlockTrace into the ccc, and return the accumulated RowConsumption
func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyTransaction(traces *types.BlockTrace) (*types.RowConsumption, error) {
ccc.Lock()
defer ccc.Unlock()
@ -84,13 +84,13 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
return ccc.applyTransactionRustTrace(rustTrace)
}
func (ccc *CircuitCapacityChecker) ApplyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
ccc.Lock()
defer ccc.Unlock()
return ccc.applyTransactionRustTrace(rustTrace)
}
func (ccc *CircuitCapacityChecker) applyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
func (ccc *Checker) applyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
rawResult := C.apply_tx(C.uint64_t(ccc.ID), rustTrace)
defer func() {
C.free_c_chars(rawResult)
@ -103,11 +103,11 @@ func (ccc *CircuitCapacityChecker) applyTransactionRustTrace(rustTrace unsafe.Po
}
if result.Error != "" {
log.Error("fail to apply_tx in CircuitCapacityChecker", "id", ccc.ID, "err", result.Error)
log.Error("fail to apply_tx in Checker", "id", ccc.ID, "err", result.Error)
return nil, ErrUnknown
}
if result.AccRowUsage == nil {
log.Error("fail to apply_tx in CircuitCapacityChecker",
log.Error("fail to apply_tx in Checker",
"id", ccc.ID, "result.AccRowUsage == nil", result.AccRowUsage == nil,
"err", "AccRowUsage is empty unexpectedly")
return nil, ErrUnknown
@ -119,7 +119,7 @@ func (ccc *CircuitCapacityChecker) applyTransactionRustTrace(rustTrace unsafe.Po
}
// ApplyBlock gets a block's RowConsumption
func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyBlock(traces *types.BlockTrace) (*types.RowConsumption, error) {
ccc.Lock()
defer ccc.Unlock()
@ -145,11 +145,11 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
}
if result.Error != "" {
log.Error("fail to apply_block in CircuitCapacityChecker", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash(), "err", result.Error)
log.Error("fail to apply_block in Checker", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash(), "err", result.Error)
return nil, ErrUnknown
}
if result.AccRowUsage == nil {
log.Error("fail to apply_block in CircuitCapacityChecker", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash(), "err", "AccRowUsage is empty unexpectedly")
log.Error("fail to apply_block in Checker", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash(), "err", "AccRowUsage is empty unexpectedly")
return nil, ErrUnknown
}
if !result.AccRowUsage.IsOk {
@ -159,7 +159,7 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
}
// CheckTxNum compares whether the tx_count in ccc match the expected
func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error) {
func (ccc *Checker) CheckTxNum(expected int) (bool, uint64, error) {
ccc.Lock()
defer ccc.Unlock()
@ -182,7 +182,7 @@ func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error
}
// SetLightMode sets to ccc light mode
func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error {
func (ccc *Checker) SetLightMode(lightMode bool) error {
ccc.Lock()
defer ccc.Unlock()
@ -198,7 +198,7 @@ func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error {
return fmt.Errorf("fail to json unmarshal set_light_mode result, id: %d, err: %w", ccc.ID, err)
}
if result.Error != "" {
return fmt.Errorf("fail to set_light_mode in CircuitCapacityChecker, id: %d, err: %w", ccc.ID, result.Error)
return fmt.Errorf("fail to set_light_mode in Checker, id: %d, err: %w", ccc.ID, result.Error)
}
return nil

View file

@ -1,6 +1,6 @@
//go:build !circuit_capacity_checker
package circuitcapacitychecker
package ccc
import (
"bytes"
@ -11,7 +11,7 @@ import (
"github.com/scroll-tech/go-ethereum/core/types"
)
type CircuitCapacityChecker struct {
type Checker struct {
ID uint64
countdown int
nextError *error
@ -20,20 +20,20 @@ type CircuitCapacityChecker struct {
skipError error
}
// NewCircuitCapacityChecker creates a new CircuitCapacityChecker
func NewCircuitCapacityChecker(lightMode bool) *CircuitCapacityChecker {
ccc := &CircuitCapacityChecker{ID: rand.Uint64()}
// NewChecker creates a new Checker
func NewChecker(lightMode bool) *Checker {
ccc := &Checker{ID: rand.Uint64()}
ccc.SetLightMode(lightMode)
return ccc
}
// Reset resets a ccc, but need to do nothing in mock_ccc.
func (ccc *CircuitCapacityChecker) Reset() {
func (ccc *Checker) Reset() {
}
// ApplyTransaction appends a tx's wrapped BlockTrace into the ccc, and return the accumulated RowConsumption.
// Will only return a dummy value in mock_ccc.
func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyTransaction(traces *types.BlockTrace) (*types.RowConsumption, error) {
if ccc.nextError != nil {
ccc.countdown--
if ccc.countdown == 0 {
@ -53,13 +53,13 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
}}, nil
}
func (ccc *CircuitCapacityChecker) ApplyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyTransactionRustTrace(rustTrace unsafe.Pointer) (*types.RowConsumption, error) {
return ccc.ApplyTransaction(goTraces[rustTrace])
}
// ApplyBlock gets a block's RowConsumption.
// Will only return a dummy value in mock_ccc.
func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.RowConsumption, error) {
func (ccc *Checker) ApplyBlock(traces *types.BlockTrace) (*types.RowConsumption, error) {
return &types.RowConsumption{types.SubCircuitRowUsage{
Name: "mock",
RowNumber: 2,
@ -68,23 +68,23 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
// CheckTxNum compares whether the tx_count in ccc match the expected.
// Will alway return true in mock_ccc.
func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error) {
func (ccc *Checker) CheckTxNum(expected int) (bool, uint64, error) {
return true, uint64(expected), nil
}
// SetLightMode sets to ccc light mode
func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error {
func (ccc *Checker) SetLightMode(lightMode bool) error {
return nil
}
// ScheduleError schedules an error for a tx (see `ApplyTransaction`), only used in tests.
func (ccc *CircuitCapacityChecker) ScheduleError(cnt int, err error) {
func (ccc *Checker) ScheduleError(cnt int, err error) {
ccc.countdown = cnt
ccc.nextError = &err
}
// Skip forced CCC to return always an error for a given txn
func (ccc *CircuitCapacityChecker) Skip(txnHash common.Hash, err error) {
func (ccc *Checker) Skip(txnHash common.Hash, err error) {
ccc.skipHash = txnHash.String()
ccc.skipError = err
}

View file

@ -1,4 +1,4 @@
package circuitcapacitychecker
package ccc
import (
"errors"

View file

@ -16,7 +16,7 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rollup/circuitcapacitychecker"
"github.com/scroll-tech/go-ethereum/rollup/ccc"
"github.com/scroll-tech/go-ethereum/rollup/tracing"
)
@ -62,7 +62,7 @@ type Pipeline struct {
cancelCtx context.CancelFunc
// accumulators
ccc *circuitcapacitychecker.CircuitCapacityChecker
ccc *ccc.Checker
Header types.Header
state *state.StateDB
nextL1MsgIndex uint64
@ -88,7 +88,7 @@ func NewPipeline(
header *types.Header,
nextL1MsgIndex uint64,
ccc *circuitcapacitychecker.CircuitCapacityChecker,
ccc *ccc.Checker,
) *Pipeline {
// make sure we are not sharing a tracer with the caller and not in debug mode
vmConfig.Tracer = nil
@ -359,7 +359,7 @@ func (p *Pipeline) encodeStage(traces <-chan *BlockCandidate) <-chan *BlockCandi
encodeStart := time.Now()
if p.ccc != nil {
trace.RustTrace = circuitcapacitychecker.MakeRustTrace(trace.LastTrace, buffer)
trace.RustTrace = ccc.MakeRustTrace(trace.LastTrace, buffer)
if trace.RustTrace == nil {
log.Error("making rust trace", "txHash", trace.LastTrace.Transactions[0].TxHash)
// ignore the error here, CCC stage will catch it and treat it as a CCC error
@ -370,7 +370,7 @@ func (p *Pipeline) encodeStage(traces <-chan *BlockCandidate) <-chan *BlockCandi
stallStart := time.Now()
if sendCancellable(downstreamCh, trace, p.ctx.Done()) && trace.RustTrace != nil {
// failed to send the trace downstream, free it here.
circuitcapacitychecker.FreeRustTrace(trace.RustTrace)
ccc.FreeRustTrace(trace.RustTrace)
}
encodeStallTimer.UpdateSince(stallStart)
case <-p.ctx.Done():
@ -404,7 +404,7 @@ func (p *Pipeline) cccStage(candidates <-chan *BlockCandidate, deadline time.Tim
break
}
if candidate.RustTrace != nil {
circuitcapacitychecker.FreeRustTrace(candidate.RustTrace)
ccc.FreeRustTrace(candidate.RustTrace)
}
}
p.wg.Done()