mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 05:36:46 +00:00
refactor(ccc): allow go side to construct a rust trace from json (#807)
This commit is contained in:
parent
dc57ae2468
commit
8bd2a16eb6
4 changed files with 52 additions and 20 deletions
|
|
@ -24,7 +24,7 @@ import (
|
||||||
const (
|
const (
|
||||||
VersionMajor = 5 // Major version component of the current release
|
VersionMajor = 5 // Major version component of the current release
|
||||||
VersionMinor = 3 // Minor version component of the current release
|
VersionMinor = 3 // Minor version component of the current release
|
||||||
VersionPatch = 39 // Patch version component of the current release
|
VersionPatch = 40 // Patch version component of the current release
|
||||||
VersionMeta = "mainnet" // Version metadata to append to the version string
|
VersionMeta = "mainnet" // Version metadata to append to the version string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,19 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/scroll-tech/go-ethereum/core/types"
|
"github.com/scroll-tech/go-ethereum/core/types"
|
||||||
"github.com/scroll-tech/go-ethereum/log"
|
"github.com/scroll-tech/go-ethereum/log"
|
||||||
|
"github.com/scroll-tech/go-ethereum/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// mutex for concurrent CircuitCapacityChecker creations
|
// mutex for concurrent CircuitCapacityChecker creations
|
||||||
var creationMu sync.Mutex
|
var (
|
||||||
|
creationMu sync.Mutex
|
||||||
|
encodeTimer = metrics.NewRegisteredTimer("ccc/encode", nil)
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
C.init()
|
C.init()
|
||||||
|
|
@ -67,6 +72,7 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
|
||||||
return nil, ErrUnknown
|
return nil, ErrUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
encodeStart := time.Now()
|
||||||
ccc.jsonBuffer.Reset()
|
ccc.jsonBuffer.Reset()
|
||||||
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -79,8 +85,15 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
|
||||||
C.free(unsafe.Pointer(tracesStr))
|
C.free(unsafe.Pointer(tracesStr))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
rustTrace := C.parse_json_to_rust_trace(tracesStr)
|
||||||
|
if rustTrace == nil {
|
||||||
|
log.Error("fail to parse json in to rust trace", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
|
||||||
|
return nil, ErrUnknown
|
||||||
|
}
|
||||||
|
encodeTimer.UpdateSince(encodeStart)
|
||||||
|
|
||||||
log.Debug("start to check circuit capacity for tx", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
|
log.Debug("start to check circuit capacity for tx", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
|
||||||
rawResult := C.apply_tx(C.uint64_t(ccc.ID), tracesStr)
|
rawResult := C.apply_tx(C.uint64_t(ccc.ID), rustTrace)
|
||||||
defer func() {
|
defer func() {
|
||||||
C.free_c_chars(rawResult)
|
C.free_c_chars(rawResult)
|
||||||
}()
|
}()
|
||||||
|
|
@ -114,6 +127,7 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
|
||||||
ccc.Lock()
|
ccc.Lock()
|
||||||
defer ccc.Unlock()
|
defer ccc.Unlock()
|
||||||
|
|
||||||
|
encodeStart := time.Now()
|
||||||
ccc.jsonBuffer.Reset()
|
ccc.jsonBuffer.Reset()
|
||||||
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
err := json.NewEncoder(&ccc.jsonBuffer).Encode(traces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -126,8 +140,15 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
|
||||||
C.free(unsafe.Pointer(tracesStr))
|
C.free(unsafe.Pointer(tracesStr))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
rustTrace := C.parse_json_to_rust_trace(tracesStr)
|
||||||
|
if rustTrace == nil {
|
||||||
|
log.Error("fail to parse json in to rust trace", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
|
||||||
|
return nil, ErrUnknown
|
||||||
|
}
|
||||||
|
encodeTimer.UpdateSince(encodeStart)
|
||||||
|
|
||||||
log.Debug("start to check circuit capacity for block", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())
|
log.Debug("start to check circuit capacity for block", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())
|
||||||
rawResult := C.apply_block(C.uint64_t(ccc.ID), tracesStr)
|
rawResult := C.apply_block(C.uint64_t(ccc.ID), rustTrace)
|
||||||
defer func() {
|
defer func() {
|
||||||
C.free_c_chars(rawResult)
|
C.free_c_chars(rawResult)
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@
|
||||||
void init();
|
void init();
|
||||||
uint64_t new_circuit_capacity_checker();
|
uint64_t new_circuit_capacity_checker();
|
||||||
void reset_circuit_capacity_checker(uint64_t id);
|
void reset_circuit_capacity_checker(uint64_t id);
|
||||||
char* apply_tx(uint64_t id, char *tx_traces);
|
char* apply_tx(uint64_t id, void* tx_traces);
|
||||||
char* apply_block(uint64_t id, char *block_trace);
|
char* apply_block(uint64_t id, void* block_trace);
|
||||||
char* get_tx_num(uint64_t id);
|
char* get_tx_num(uint64_t id);
|
||||||
char* set_light_mode(uint64_t id, bool light_mode);
|
char* set_light_mode(uint64_t id, bool light_mode);
|
||||||
void free_c_chars(char* ptr);
|
void free_c_chars(char* ptr);
|
||||||
|
void* parse_json_to_rust_trace(char* trace_json_ptr);
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
pub mod checker {
|
pub mod checker {
|
||||||
use crate::utils::{c_char_to_str, c_char_to_vec, vec_to_c_char};
|
use crate::utils::vec_to_c_char;
|
||||||
use anyhow::{anyhow, bail, Error};
|
use anyhow::{anyhow, bail, Error};
|
||||||
use libc::c_char;
|
use libc::c_char;
|
||||||
use prover::{
|
use prover::{
|
||||||
|
|
@ -7,10 +7,11 @@ pub mod checker {
|
||||||
BlockTrace,
|
BlockTrace,
|
||||||
};
|
};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use std::cell::OnceCell;
|
use std::{cell::OnceCell, ptr::null_mut};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::panic;
|
use std::panic;
|
||||||
use std::ptr::null;
|
use std::ptr::null;
|
||||||
|
use std::ffi::CStr;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct CommonResult {
|
pub struct CommonResult {
|
||||||
|
|
@ -43,6 +44,17 @@ pub mod checker {
|
||||||
.expect("circuit capacity checker initialized twice");
|
.expect("circuit capacity checker initialized twice");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Safety
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn parse_json_to_rust_trace(trace_json_ptr: *const c_char) -> *mut BlockTrace {
|
||||||
|
let trace_json_cstr = unsafe { CStr::from_ptr(trace_json_ptr) };
|
||||||
|
let trace = serde_json::from_slice::<BlockTrace>(trace_json_cstr.to_bytes());
|
||||||
|
match trace {
|
||||||
|
Err(_) => return null_mut(),
|
||||||
|
Ok(t) => return Box::into_raw(Box::new(t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn new_circuit_capacity_checker() -> u64 {
|
pub unsafe extern "C" fn new_circuit_capacity_checker() -> u64 {
|
||||||
|
|
@ -68,8 +80,9 @@ pub mod checker {
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn apply_tx(id: u64, tx_traces: *const c_char) -> *const c_char {
|
pub unsafe extern "C" fn apply_tx(id: u64, trace_ptr: *mut BlockTrace) -> *const c_char {
|
||||||
let result = apply_tx_inner(id, tx_traces);
|
let trace = Box::from_raw(trace_ptr);
|
||||||
|
let result = apply_tx_inner(id, *trace);
|
||||||
let r = match result {
|
let r = match result {
|
||||||
Ok(acc_row_usage) => {
|
Ok(acc_row_usage) => {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
|
|
@ -90,14 +103,12 @@ pub mod checker {
|
||||||
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
|
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn apply_tx_inner(id: u64, tx_traces: *const c_char) -> Result<RowUsage, Error> {
|
unsafe fn apply_tx_inner(id: u64, traces: BlockTrace) -> Result<RowUsage, Error> {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"ccc apply_tx raw input, id: {:?}, tx_traces: {:?}",
|
"ccc apply_tx raw input, id: {:?}, tx_traces: {:?}",
|
||||||
id,
|
id,
|
||||||
c_char_to_str(tx_traces)?
|
traces
|
||||||
);
|
);
|
||||||
let tx_traces_vec = c_char_to_vec(tx_traces);
|
|
||||||
let traces = serde_json::from_slice::<BlockTrace>(&tx_traces_vec)?;
|
|
||||||
|
|
||||||
if traces.transactions.len() != 1 {
|
if traces.transactions.len() != 1 {
|
||||||
bail!("traces.transactions.len() != 1");
|
bail!("traces.transactions.len() != 1");
|
||||||
|
|
@ -131,8 +142,9 @@ pub mod checker {
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn apply_block(id: u64, block_trace: *const c_char) -> *const c_char {
|
pub unsafe extern "C" fn apply_block(id: u64, trace_ptr: *mut BlockTrace) -> *const c_char {
|
||||||
let result = apply_block_inner(id, block_trace);
|
let trace = Box::from_raw(trace_ptr);
|
||||||
|
let result = apply_block_inner(id, *trace);
|
||||||
let r = match result {
|
let r = match result {
|
||||||
Ok(acc_row_usage) => {
|
Ok(acc_row_usage) => {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
|
|
@ -153,14 +165,12 @@ pub mod checker {
|
||||||
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
|
serde_json::to_vec(&r).map_or(null(), vec_to_c_char)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn apply_block_inner(id: u64, block_trace: *const c_char) -> Result<RowUsage, Error> {
|
unsafe fn apply_block_inner(id: u64, traces: BlockTrace) -> Result<RowUsage, Error> {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"ccc apply_block raw input, id: {:?}, block_trace: {:?}",
|
"ccc apply_block raw input, id: {:?}, block_trace: {:?}",
|
||||||
id,
|
id,
|
||||||
c_char_to_str(block_trace)?
|
traces
|
||||||
);
|
);
|
||||||
let block_trace = c_char_to_vec(block_trace);
|
|
||||||
let traces = serde_json::from_slice::<BlockTrace>(&block_trace)?;
|
|
||||||
|
|
||||||
let r = panic::catch_unwind(|| {
|
let r = panic::catch_unwind(|| {
|
||||||
CHECKERS
|
CHECKERS
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue