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

fix (libzkp): check tx num when CCC ErrUnknown error occurred #505

Merged
merged 14 commits into from
Sep 8, 2023
22 changes: 22 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,17 @@ loop:
// However, after `ErrUnknown`, ccc might remain in an
// inconsistent state, so we cannot pack more transactions.
circuitCapacityReached = true
txNumInCcc, err := w.circuitCapacityChecker.GetTxNum()
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
txNumInMiner := uint64(w.current.tcount)
if txNumInMiner == txNumInCcc {
log.Info("tx count in miner is same with CCC", "txNum", txNumInMiner)
silathdiir marked this conversation as resolved.
Show resolved Hide resolved
} else {
log.Error("tx count in miner is different with CCC", "txNumInMiner", txNumInMiner, "txNumInCcc", txNumInCcc)
}
} else {
log.Error("failed to get_tx_num", "err", err)
}
break loop

case (errors.Is(err, circuitcapacitychecker.ErrUnknown) && !tx.IsL1MessageTx()):
Expand All @@ -1164,6 +1175,17 @@ loop:
// inconsistent state, so we cannot pack more transactions.
w.eth.TxPool().RemoveTx(tx.Hash(), true)
circuitCapacityReached = true
txNumInCcc, err := w.circuitCapacityChecker.GetTxNum()
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
txNumInMiner := uint64(w.current.tcount)
if txNumInMiner == txNumInCcc {
log.Info("tx count in miner is same with CCC", "txNum", txNumInMiner)
} else {
log.Error("tx count in miner is different with CCC", "txNumInMiner", txNumInMiner, "txNumInCcc", txNumInCcc)
}
} else {
log.Error("failed to get_tx_num", "err", err)
}
break loop

default:
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 = 2 // Patch version component of the current release
VersionPatch = 3 // Patch version component of the current release
VersionMeta = "sepolia" // Version metadata to append to the version string
)

Expand Down
28 changes: 28 additions & 0 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,31 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
}
return (*types.RowConsumption)(&result.AccRowUsage.RowUsageDetails), nil
}

func (ccc *CircuitCapacityChecker) GetTxNum() (uint64, error) {
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
ccc.Lock()
defer ccc.Unlock()

log.Debug("ccc get_tx_num start", "id", ccc.ID)
rawResult := C.get_tx_num(C.uint64_t(ccc.ID))
defer func() {
C.free(unsafe.Pointer(rawResult))
}()
log.Debug("ccc get_tx_num end", "id", ccc.ID)

result := &WrappedTxNum{}
if err = json.Unmarshal([]byte(C.GoString(rawResult)), result); err != nil {
log.Error("fail to json unmarshal get_tx_num result", "id", ccc.ID, "err", err)
return nil, ErrUnknown
}
if result.Error != "" {
log.Error("fail to get_tx_num in CircuitCapacityChecker", "id", ccc.ID, "err", result.Error)
return nil, ErrUnknown
}
if result.TxNum == nil {
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
log.Error("fail to get_tx_num in CircuitCapacityChecker", "id", ccc.ID, "err", "TxNum is empty unexpectedly")
return nil, ErrUnknown
}

return result.TxNum, nil
}
1 change: 1 addition & 0 deletions rollup/circuitcapacitychecker/libzkp/libzkp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ 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);
50 changes: 50 additions & 0 deletions rollup/circuitcapacitychecker/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub mod checker {
pub error: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TxNumResult {
pub tx_num: Option<u64>,
pub error: Option<String>,
}

static mut CHECKERS: OnceCell<HashMap<u64, CircuitCapacityChecker>> = OnceCell::new();

/// # Safety
Expand Down Expand Up @@ -176,6 +182,50 @@ pub mod checker {
}
}
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn get_tx_num(id: u64) -> *const c_char {
let result = get_tx_num_inner(id);
let r = match result {
Ok(tx_num) => {
log::debug!("id: {id}, tx_num: {tx_num}");
TxNumResult {
tx_num: Some(tx_num),
error: None,
}
}
Err(e) => TxNumResult {
tx_num: None,
error: Some(format!("{e:?}")),
},
};
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
}

unsafe fn get_tx_num_inner(id: u64) -> Result<u64, Error> {
log::debug!("ccc get_tx_num raw input, id: {id}");
panic::catch_unwind(|| {
Ok(CHECKERS
.get_mut()
.ok_or(anyhow!(
"fail to get circuit capacity checkers map in get_tx_num"
))?
.get_mut(&id)
.ok_or(anyhow!(
"fail to get circuit capacity checker (id: {id}) in get_tx_num"
))?
.get_tx_num() as u64)
})
.map_or_else(
|e| {
Err(anyhow!(
"circuit capacity checker (id: {id}) error in get_tx_num: {e:?}"
))
},
|result| result,
)
}
}

pub(crate) mod utils {
Expand Down
4 changes: 4 additions & 0 deletions rollup/circuitcapacitychecker/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
}}, nil
}

func (ccc *CircuitCapacityChecker) GetTxNum() (uint64, error) {
return 0, nil
Thegaram marked this conversation as resolved.
Show resolved Hide resolved
}

func (ccc *CircuitCapacityChecker) ScheduleError(cnt int, err error) {
ccc.countdown = cnt
ccc.nextError = &err
Expand Down
5 changes: 5 additions & 0 deletions rollup/circuitcapacitychecker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ type WrappedRowUsage struct {
AccRowUsage *types.RowUsage `json:"acc_row_usage,omitempty"`
Error string `json:"error,omitempty"`
}

type WrappedTxNum struct {
TxNum uint64 `json:"tx_num,omitempty"`
Error string `json:"error,omitempty"`
}
Loading