From 7cebee85a8a539d01e03691878a069804bbbe364 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 3 Oct 2023 21:42:54 +0800 Subject: [PATCH] fix(libzkp): upgrade libzkp to `v0.9.4` and add ccc `SetLightMode` (#522) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Upgrade to prover `v0.9.4`, and add `SetLightMode` function. * set ccc light_mode = false for validator * fix * bump version --------- Co-authored-by: Zhang Zhuo Co-authored-by: Péter Garamvölgyi --- core/block_validator.go | 2 +- miner/worker.go | 2 +- params/version.go | 2 +- rollup/circuitcapacitychecker/impl.go | 29 +++++++++++++- .../circuitcapacitychecker/libzkp/Cargo.lock | 24 ++++++------ .../circuitcapacitychecker/libzkp/Cargo.toml | 2 +- rollup/circuitcapacitychecker/libzkp/libzkp.h | 3 ++ .../circuitcapacitychecker/libzkp/src/lib.rs | 39 +++++++++++++++++++ rollup/circuitcapacitychecker/mock.go | 11 +++++- rollup/circuitcapacitychecker/types.go | 4 ++ 10 files changed, 98 insertions(+), 20 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index 4cf511ee20..6ed3f96ae1 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -56,7 +56,7 @@ func NewBlockValidator(config *params.ChainConfig, blockchain *BlockChain, engin bc: blockchain, checkCircuitCapacity: checkCircuitCapacity, db: db, - circuitCapacityChecker: circuitcapacitychecker.NewCircuitCapacityChecker(), + circuitCapacityChecker: circuitcapacitychecker.NewCircuitCapacityChecker(false), } log.Info("created new BlockValidator", "CircuitCapacityChecker ID", validator.circuitCapacityChecker.ID) return validator diff --git a/miner/worker.go b/miner/worker.go index 1c27899638..0a3e2d651c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -243,7 +243,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus startCh: make(chan struct{}, 1), resubmitIntervalCh: make(chan time.Duration), resubmitAdjustCh: make(chan *intervalAdjust, resubmitAdjustChanSize), - circuitCapacityChecker: circuitcapacitychecker.NewCircuitCapacityChecker(), + circuitCapacityChecker: circuitcapacitychecker.NewCircuitCapacityChecker(true), } log.Info("created new worker", "CircuitCapacityChecker ID", worker.circuitCapacityChecker.ID) diff --git a/params/version.go b/params/version.go index cc6547c085..cbf2097013 100644 --- a/params/version.go +++ b/params/version.go @@ -24,7 +24,7 @@ import ( const ( VersionMajor = 4 // Major version component of the current release VersionMinor = 4 // Minor version component of the current release - VersionPatch = 15 // Patch version component of the current release + VersionPatch = 16 // Patch version component of the current release VersionMeta = "sepolia" // Version metadata to append to the version string ) diff --git a/rollup/circuitcapacitychecker/impl.go b/rollup/circuitcapacitychecker/impl.go index 212396bccb..766a9e4130 100644 --- a/rollup/circuitcapacitychecker/impl.go +++ b/rollup/circuitcapacitychecker/impl.go @@ -33,12 +33,14 @@ type CircuitCapacityChecker struct { } // NewCircuitCapacityChecker creates a new CircuitCapacityChecker -func NewCircuitCapacityChecker() *CircuitCapacityChecker { +func NewCircuitCapacityChecker(lightMode bool) *CircuitCapacityChecker { creationMu.Lock() defer creationMu.Unlock() id := C.new_circuit_capacity_checker() - return &CircuitCapacityChecker{ID: uint64(id)} + ccc := &CircuitCapacityChecker{ID: uint64(id)} + ccc.SetLightMode(lightMode) + return ccc } // Reset resets a CircuitCapacityChecker @@ -169,3 +171,26 @@ func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error return result.TxNum == uint64(expected), result.TxNum, nil } + +// SetLightMode sets to ccc light mode +func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error { + ccc.Lock() + defer ccc.Unlock() + + log.Debug("ccc set_light_mode start", "id", ccc.ID) + rawResult := C.set_light_mode(C.uint64_t(ccc.ID), C.bool(lightMode)) + defer func() { + C.free(unsafe.Pointer(rawResult)) + }() + log.Debug("ccc set_light_mode end", "id", ccc.ID) + + result := &WrappedCommonResult{} + if err := json.Unmarshal([]byte(C.GoString(rawResult)), result); err != nil { + 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 nil +} diff --git a/rollup/circuitcapacitychecker/libzkp/Cargo.lock b/rollup/circuitcapacitychecker/libzkp/Cargo.lock index 3afe2227b5..ea53f9256e 100644 --- a/rollup/circuitcapacitychecker/libzkp/Cargo.lock +++ b/rollup/circuitcapacitychecker/libzkp/Cargo.lock @@ -16,7 +16,7 @@ dependencies = [ [[package]] name = "aggregator" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "ark-std", "env_logger 0.10.0", @@ -297,7 +297,7 @@ checksum = "3c6ed94e98ecff0c12dd1b04c15ec0d7d9458ca8fe806cea6f12954efe74c63b" [[package]] name = "bus-mapping" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "eth-types", "ethers-core", @@ -971,7 +971,7 @@ dependencies = [ [[package]] name = "eth-types" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "ethers-core", "ethers-signers", @@ -1128,7 +1128,7 @@ dependencies = [ [[package]] name = "external-tracer" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "eth-types", "geth-utils", @@ -1308,7 +1308,7 @@ dependencies = [ [[package]] name = "gadgets" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "digest 0.7.6", "eth-types", @@ -1340,7 +1340,7 @@ dependencies = [ [[package]] name = "geth-utils" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "env_logger 0.9.3", "gobuild 0.1.0-alpha.2 (git+https://github.com/scroll-tech/gobuild.git)", @@ -1503,7 +1503,7 @@ dependencies = [ [[package]] name = "halo2-mpt-circuits" version = "0.1.0" -source = "git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.6.5#0bae9eeb813583c11f6db1f961a7e92f8c9bda82" +source = "git+https://github.com/scroll-tech/mpt-circuit.git?tag=v0.7.0#578c210ceb88d3c143ee2a013ad836d19285d9c1" dependencies = [ "ethers-core", "halo2_proofs", @@ -1949,7 +1949,7 @@ dependencies = [ [[package]] name = "keccak256" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "env_logger 0.9.3", "eth-types", @@ -2157,7 +2157,7 @@ dependencies = [ [[package]] name = "mock" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "eth-types", "ethers-core", @@ -2173,7 +2173,7 @@ dependencies = [ [[package]] name = "mpt-zktrie" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "eth-types", "halo2-mpt-circuits", @@ -2595,7 +2595,7 @@ dependencies = [ [[package]] name = "prover" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "aggregator", "anyhow", @@ -4212,7 +4212,7 @@ checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" [[package]] name = "zkevm-circuits" version = "0.1.0" -source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.2#2723b82fb5d538d6fcc7b2dd0d84d3df8818499f" +source = "git+https://github.com/scroll-tech/zkevm-circuits.git?tag=v0.9.4#40f4758f5b4b5a5c82fb312ee58492487f181185" dependencies = [ "array-init", "bus-mapping", diff --git a/rollup/circuitcapacitychecker/libzkp/Cargo.toml b/rollup/circuitcapacitychecker/libzkp/Cargo.toml index b462e452bf..9390bbad09 100644 --- a/rollup/circuitcapacitychecker/libzkp/Cargo.toml +++ b/rollup/circuitcapacitychecker/libzkp/Cargo.toml @@ -20,7 +20,7 @@ maingate = { git = "https://github.com/scroll-tech/halo2wrong", branch = "halo2- halo2curves = { git = "https://github.com/scroll-tech/halo2curves.git", branch = "0.3.1-derive-serde" } [dependencies] -prover = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.9.2", default-features = false, features = ["parallel_syn", "scroll", "shanghai"] } +prover = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.9.4", default-features = false, features = ["parallel_syn", "scroll", "shanghai"] } anyhow = "1.0" log = "0.4" diff --git a/rollup/circuitcapacitychecker/libzkp/libzkp.h b/rollup/circuitcapacitychecker/libzkp/libzkp.h index 59213a0cee..184f33cd7f 100644 --- a/rollup/circuitcapacitychecker/libzkp/libzkp.h +++ b/rollup/circuitcapacitychecker/libzkp/libzkp.h @@ -1,7 +1,10 @@ +#include #include + void init(); uint64_t new_circuit_capacity_checker(); void reset_circuit_capacity_checker(uint64_t id); char* apply_tx(uint64_t id, char *tx_traces); char* apply_block(uint64_t id, char *block_trace); char* get_tx_num(uint64_t id); +char* set_light_mode(uint64_t id, bool light_mode); diff --git a/rollup/circuitcapacitychecker/libzkp/src/lib.rs b/rollup/circuitcapacitychecker/libzkp/src/lib.rs index c2a25c8c1d..6583de48c2 100644 --- a/rollup/circuitcapacitychecker/libzkp/src/lib.rs +++ b/rollup/circuitcapacitychecker/libzkp/src/lib.rs @@ -14,6 +14,11 @@ pub mod checker { use std::panic; use std::ptr::null; + #[derive(Debug, Clone, Deserialize, Serialize)] + pub struct CommonResult { + pub error: Option, + } + #[derive(Debug, Clone, Deserialize, Serialize)] pub struct RowUsageResult { pub acc_row_usage: Option, @@ -218,6 +223,40 @@ pub mod checker { |result| result, ) } + + /// # Safety + #[no_mangle] + pub unsafe extern "C" fn set_light_mode(id: u64, light_mode: bool) -> *const c_char { + let result = set_light_mode_inner(id, light_mode); + let r = match result { + Ok(()) => CommonResult { error: None }, + Err(e) => CommonResult { + error: Some(format!("{e:?}")), + }, + }; + serde_json::to_vec(&r).map_or(null(), vec_to_c_char) + } + + unsafe fn set_light_mode_inner(id: u64, light_mode: bool) -> Result<(), Error> { + log::debug!("ccc set_light_mode raw input, id: {id}"); + panic::catch_unwind(|| { + CHECKERS + .get_mut() + .ok_or(anyhow!( + "fail to get circuit capacity checkers map in set_light_mode" + ))? + .get_mut(&id) + .ok_or(anyhow!( + "fail to get circuit capacity checker (id: {id}) in set_light_mode" + ))? + .set_light_mode(light_mode); + Ok(()) + }) + .map_or_else( + |e| bail!("circuit capacity checker (id: {id}) error in set_light_mode: {e:?}"), + |result| result, + ) + } } pub(crate) mod utils { diff --git a/rollup/circuitcapacitychecker/mock.go b/rollup/circuitcapacitychecker/mock.go index 6ec15dece2..a50bc6d5d7 100644 --- a/rollup/circuitcapacitychecker/mock.go +++ b/rollup/circuitcapacitychecker/mock.go @@ -15,8 +15,10 @@ type CircuitCapacityChecker struct { } // NewCircuitCapacityChecker creates a new CircuitCapacityChecker -func NewCircuitCapacityChecker() *CircuitCapacityChecker { - return &CircuitCapacityChecker{ID: rand.Uint64()} +func NewCircuitCapacityChecker(lightMode bool) *CircuitCapacityChecker { + ccc := &CircuitCapacityChecker{ID: rand.Uint64()} + ccc.SetLightMode(lightMode) + return ccc } // Reset resets a ccc, but need to do nothing in mock_ccc. @@ -55,6 +57,11 @@ func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error return true, uint64(expected), nil } +// SetLightMode sets to ccc light mode +func (ccc *CircuitCapacityChecker) 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) { ccc.countdown = cnt diff --git a/rollup/circuitcapacitychecker/types.go b/rollup/circuitcapacitychecker/types.go index 0cf75cf702..13a9385d56 100644 --- a/rollup/circuitcapacitychecker/types.go +++ b/rollup/circuitcapacitychecker/types.go @@ -11,6 +11,10 @@ var ( ErrBlockRowConsumptionOverflow = errors.New("block row consumption overflow") ) +type WrappedCommonResult struct { + Error string `json:"error,omitempty"` +} + type WrappedRowUsage struct { AccRowUsage *types.RowUsage `json:"acc_row_usage,omitempty"` Error string `json:"error,omitempty"`