Skip to content

Commit

Permalink
fix(libzkp): upgrade libzkp to v0.9.4 and add ccc SetLightMode (#522
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>
Co-authored-by: Péter Garamvölgyi <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2023
1 parent 736cf7b commit 7cebee8
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
29 changes: 27 additions & 2 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
24 changes: 12 additions & 12 deletions rollup/circuitcapacitychecker/libzkp/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup/circuitcapacitychecker/libzkp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions rollup/circuitcapacitychecker/libzkp/libzkp.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <stdbool.h>
#include<stdint.h>

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);
39 changes: 39 additions & 0 deletions rollup/circuitcapacitychecker/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RowUsageResult {
pub acc_row_usage: Option<RowUsage>,
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions rollup/circuitcapacitychecker/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions rollup/circuitcapacitychecker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 7cebee8

Please sign in to comment.