Skip to content

Commit

Permalink
fix: free rust traces that never make it to CCC (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored Jun 21, 2024
1 parent c003520 commit aafca83
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
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 = 5 // Major version component of the current release
VersionMinor = 4 // Minor version component of the current release
VersionPatch = 6 // Patch version component of the current release
VersionPatch = 7 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
4 changes: 4 additions & 0 deletions rollup/circuitcapacitychecker/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,7 @@ func MakeRustTrace(trace *types.BlockTrace, buffer *bytes.Buffer) unsafe.Pointer

return C.parse_json_to_rust_trace(tracesStr)
}

func FreeRustTrace(ptr unsafe.Pointer) {
C.free_rust_trace(ptr)
}
3 changes: 2 additions & 1 deletion rollup/circuitcapacitychecker/libzkp/libzkp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ char* apply_block(uint64_t id, void* block_trace);
char* get_tx_num(uint64_t id);
char* set_light_mode(uint64_t id, bool light_mode);
void free_c_chars(char* ptr);
void* parse_json_to_rust_trace(char* trace_json_ptr);
void* parse_json_to_rust_trace(char* trace_json_ptr);
void free_rust_trace(void* trace_ptr);
8 changes: 8 additions & 0 deletions rollup/circuitcapacitychecker/libzkp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub mod utils {
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::str::Utf8Error;
use prover::BlockTrace;

/// # Safety
#[no_mangle]
Expand All @@ -283,6 +284,13 @@ pub mod utils {
let _ = CString::from_raw(ptr);
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn free_rust_trace(trace_ptr: *mut BlockTrace) {
let _ = Box::from_raw(trace_ptr);
}


#[allow(dead_code)]
pub(crate) fn c_char_to_str(c: *const c_char) -> Result<&'static str, Utf8Error> {
let cstr = unsafe { CStr::from_ptr(c) };
Expand Down
3 changes: 3 additions & 0 deletions rollup/circuitcapacitychecker/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ func MakeRustTrace(trace *types.BlockTrace, buffer *bytes.Buffer) unsafe.Pointer
goTraces[unsafe.Pointer(rustTrace)] = trace
return unsafe.Pointer(rustTrace)
}

func FreeRustTrace(ptr unsafe.Pointer) {
}
14 changes: 13 additions & 1 deletion rollup/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ func (p *Pipeline) encodeStage(traces <-chan *BlockCandidate) <-chan *BlockCandi
encodeTimer.UpdateSince(encodeStart)

stallStart := time.Now()
sendCancellable(downstreamCh, trace, p.ctx.Done())
if sendCancellable(downstreamCh, trace, p.ctx.Done()) {
// failed to send the trace downstream, free it here.
circuitcapacitychecker.FreeRustTrace(trace.RustTrace)
}
encodeStallTimer.UpdateSince(stallStart)
case <-p.ctx.Done():
return
Expand All @@ -395,6 +398,15 @@ func (p *Pipeline) cccStage(candidates <-chan *BlockCandidate, deadline time.Tim
close(resultCh)
deadlineTimer.Stop()
lifetimeTimer.UpdateSince(p.start)
// consume candidates and free all rust traces
for candidate := range candidates {
if candidate == nil {
break
}
if candidate.RustTrace != nil {
circuitcapacitychecker.FreeRustTrace(candidate.RustTrace)
}
}
p.wg.Done()
}()
for {
Expand Down

0 comments on commit aafca83

Please sign in to comment.