feat: update

This commit is contained in:
georgehao
2024-02-01 11:40:47 +08:00
parent 6e1ca34ada
commit 60d26492ec
3 changed files with 45 additions and 30 deletions

View File

@@ -5,14 +5,15 @@ use crate::{
OUTPUT_DIR,
},
};
use glob::glob;
use libc::c_char;
use prover::{
consts::CHUNK_VK_FILENAME,
utils::init_env_and_log,
utils::{get_block_trace_from_file, init_env_and_log},
zkevm::{Prover, Verifier},
BlockTrace, ChunkProof,
};
use std::{cell::OnceCell, env, ptr::null};
use std::{cell::OnceCell, env, ffi::CString, ptr::null};
static mut PROVER: OnceCell<Prover> = OnceCell::new();
static mut VERIFIER: OnceCell<Verifier> = OnceCell::new();
@@ -66,9 +67,14 @@ pub unsafe extern "C" fn get_chunk_vk() -> *const c_char {
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn gen_chunk_proof(block_traces: *const c_char) -> *const c_char {
pub unsafe extern "C" fn gen_chunk_proof(block_traces1: *const c_char) -> *const c_char {
let chunk_trace = load_batch_traces().1;
let json_str = serde_json::to_string(&chunk_trace).expect("Serialization failed");
let c_string = CString::new(json_str).expect("CString conversion failed");
let c_str_ptr = c_string.as_ptr();
let proof_result: Result<Vec<u8>, String> = panic_catch(|| {
let block_traces = c_char_to_vec(block_traces);
let block_traces = c_char_to_vec(c_str_ptr);
let block_traces = serde_json::from_slice::<Vec<BlockTrace>>(&block_traces)
.map_err(|e| format!("failed to deserialize block traces: {e:?}"))?;
@@ -105,3 +111,31 @@ pub unsafe extern "C" fn verify_chunk_proof(proof: *const c_char) -> c_char {
let verified = panic_catch(|| VERIFIER.get().unwrap().verify_chunk_proof(proof));
verified.unwrap_or(false) as c_char
}
fn load_batch_traces() -> (Vec<String>, Vec<BlockTrace>) {
let file_names: Vec<String> = glob(&"/assets/traces/1_transfer.json".to_string())
.unwrap()
.map(|p| p.unwrap().to_str().unwrap().to_string())
.collect();
log::info!("test batch with {:?}", file_names);
let mut names_and_traces = file_names
.into_iter()
.map(|trace_path| {
let trace: BlockTrace = get_block_trace_from_file(trace_path.clone());
(
trace_path,
trace.clone(),
trace.header.number.unwrap().as_u64(),
)
})
.collect::<Vec<_>>();
names_and_traces.sort_by(|a, b| a.2.cmp(&b.2));
log::info!(
"sorted: {:?}",
names_and_traces
.iter()
.map(|(f, _, _)| f.clone())
.collect::<Vec<String>>()
);
names_and_traces.into_iter().map(|(f, t, _)| (f, t)).unzip()
}

View File

@@ -206,25 +206,11 @@ func (p *ProverCore) proveBatch(chunkInfosByt []byte, chunkProofsByt []byte) ([]
}
func (p *ProverCore) proveChunk(tracesByt []byte) ([]byte, error) {
tracesStr := C.CString(string(tracesByt))
defer C.free(unsafe.Pointer(tracesStr))
log.Info("Start to create chunk proof ...")
cProof := C.gen_chunk_proof(tracesStr)
cProof := C.gen_chunk_proof("")
defer C.free_c_chars(cProof)
log.Info("Finish creating chunk proof!")
var result ProofResult
err := json.Unmarshal([]byte(C.GoString(cProof)), &result)
if err != nil {
return nil, fmt.Errorf("failed to parse chunk proof result: %v", err)
}
if result.Error != "" {
return nil, fmt.Errorf("failed to generate chunk proof: %s", result.Error)
}
return result.Message, nil
return nil, nil
}
func (p *ProverCore) mayDumpProof(id string, proofByt []byte) error {

View File

@@ -1,24 +1,21 @@
//go:build ffi
// go test -v -race -gcflags="-l" -ldflags="-s=false" -tags ffi ./...
package core_test
package core
import (
"encoding/json"
"flag"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
_ "net/http/pprof"
"testing"
"github.com/scroll-tech/go-ethereum/core/types"
"scroll-tech/common/types/message"
"scroll-tech/prover/config"
"scroll-tech/prover/core"
)
var (
@@ -44,12 +41,10 @@ func TestFFI(t *testing.T) {
ProofType: message.ProofTypeChunk,
}
chunkProverCore, _ := core.NewProverCore(chunkProverConfig)
chunkTrace1 := readChunkTrace(t, *tracePath1)
chunkProverCore, _ := NewProverCore(chunkProverConfig)
for {
chunkProverCore.ProveChunk("chunk_proof1", chunkTrace1)
time.Sleep(time.Millisecond * 10)
chunkProverCore.proveChunk()
}
}