Skip to content

Commit

Permalink
sserialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dndll committed Mar 21, 2024
1 parent cf201f0 commit f32adbe
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 40 deletions.
22 changes: 10 additions & 12 deletions bin/operator/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ use anyhow::{anyhow, Result};
use futures::FutureExt;
use hashbrown::{hash_map::DefaultHashBuilder, HashMap};
use near_light_client_rpc::{prelude::Itertools, TransactionOrReceiptId};
use near_light_clientx::VERIFY_AMT;
use priority_queue::PriorityQueue;
use serde::{Deserialize, Serialize};
pub use types::RegistryInfo;

use self::types::{PriorityWeight, TransactionOrReceiptIdNewtype};
use crate::{
succinct::{
self,
types::{ProofResponse, ProofStatus},
ProofId,
},
VERIFY_ID_AMT,
use crate::succinct::{
self,
types::{ProofResponse, ProofStatus},
ProofId,
};

mod types;
Expand Down Expand Up @@ -78,10 +76,10 @@ impl Engine {
}

fn make_batch(&mut self) -> Option<(u32, Vec<TransactionOrReceiptId>)> {
if self.proving_queue.len() >= VERIFY_ID_AMT {
if self.proving_queue.len() >= VERIFY_AMT {
let id = self.batches.len() as u32;
let mut txs = vec![];
for _ in 0..VERIFY_ID_AMT {
for _ in 0..VERIFY_AMT {
let (req, _) = self.proving_queue.pop().unwrap();
txs.push(req.0);
}
Expand Down Expand Up @@ -325,7 +323,7 @@ mod tests {
// let mut m = manager().await;
//
// let fixture = fixture::<Vec<TransactionOrReceiptId>>("ids.json");
// let fixtures = fixture.iter().take(VERIFY_ID_AMT);
// let fixtures = fixture.iter().take(VERIFY_AMT);
// for r in fixtures {
// m.prove(message::ProveTransaction {
// tx: r.clone(),
Expand All @@ -352,9 +350,9 @@ mod tests {
let mut m = manager().await;

let fixture = fixture::<Vec<TransactionOrReceiptId>>("ids.json");
let to_take = VERIFY_ID_AMT - 1;
let to_take = VERIFY_AMT - 1;
let fixtures = fixture.clone().into_iter().take(to_take);
let priority_from = VERIFY_ID_AMT - 7;
let priority_from = VERIFY_AMT - 7;

let mut priority: Vec<TransactionOrReceiptIdNewtype> = vec![];

Expand Down
2 changes: 0 additions & 2 deletions bin/operator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pub mod engine;
pub mod rpc;
pub mod succinct;

pub const VERIFY_ID_AMT: usize = 128;

pub mod prelude {
pub use actix::{self, prelude::*};
pub use near_light_client_primitives::prelude::*;
Expand Down
26 changes: 13 additions & 13 deletions bin/operator/src/succinct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use anyhow::{ensure, Context};
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions};
use near_light_client_rpc::prelude::{CryptoHash, Itertools};
pub use near_light_clientx::plonky2x::backend::prover::ProofId;
use near_light_clientx::plonky2x::{
backend::{
circuit::DefaultParameters,
function::{BytesRequestData, ProofRequest, ProofRequestBase},
use near_light_clientx::{
plonky2x::{
backend::{
circuit::DefaultParameters,
function::{BytesRequestData, ProofRequest, ProofRequestBase},
},
utils::hex,
},
utils::hex,
VERIFY_AMT,
};
use reqwest::{
header::{self, HeaderMap, HeaderValue},
Expand All @@ -28,10 +31,7 @@ use succinct_client::{request::SuccinctClient as SuccinctClientExt, utils::get_g
use types::TransactionOrReceiptIdPrimitive;

use self::types::{Circuit, Deployment, NearX::NearXInstance, NearXClient, ProofResponse};
use crate::{
config, succinct::types::ProofRequestResponse, types::NearX::TransactionOrReceiptId,
VERIFY_ID_AMT,
};
use crate::{config, succinct::types::ProofRequestResponse, types::NearX::TransactionOrReceiptId};

pub mod types;

Expand Down Expand Up @@ -390,7 +390,7 @@ impl Client {
) -> anyhow::Result<ProofId> {
log::trace!("verifying {} ids", ids.len());
ensure!(
ids.len() == VERIFY_ID_AMT,
ids.len() == VERIFY_AMT,
"wrong number of transactions for verify"
);
let circuit = Circuit::Verify;
Expand Down Expand Up @@ -631,7 +631,7 @@ pub mod tests {

let txs = fixture::<Vec<TransactionOrReceiptIdPrimitive>>("ids.json")
.into_iter()
.take(VERIFY_ID_AMT)
.take(VERIFY_AMT)
.collect_vec();

let req = client.build_verify_request(hash, txs);
Expand All @@ -658,7 +658,7 @@ pub mod tests {
let client = mocks().await;
let txs = fixture::<Vec<TransactionOrReceiptIdPrimitive>>("ids.json")
.into_iter()
.take(VERIFY_ID_AMT)
.take(VERIFY_AMT)
.collect_vec();

let s = client.verify(txs, false).await.unwrap();
Expand All @@ -670,7 +670,7 @@ pub mod tests {
let client = mocks().await;
let txs = fixture::<Vec<TransactionOrReceiptIdPrimitive>>("ids.json")
.into_iter()
.take(VERIFY_ID_AMT)
.take(VERIFY_AMT)
.collect_vec();

let s = client.verify(txs, true).await.unwrap();
Expand Down
7 changes: 4 additions & 3 deletions bin/operator/tests/succinct.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::str::FromStr;

use near_light_client_rpc::prelude::Itertools;
use near_light_clientx::VERIFY_AMT;
use nearx_operator::{
config::Config, succinct::*, types::TransactionOrReceiptIdPrimitive, BaseConfig, VERIFY_ID_AMT,
config::Config, succinct::*, types::TransactionOrReceiptIdPrimitive, BaseConfig,
};
use test_utils::fixture;
use uuid::Uuid;
Expand Down Expand Up @@ -30,7 +31,7 @@ async fn test_verify() {

let txs = fixture::<Vec<TransactionOrReceiptIdPrimitive>>("ids.json")
.into_iter()
.take(VERIFY_ID_AMT)
.take(VERIFY_AMT)
.collect_vec();

let s = client.verify(txs, false).await.unwrap();
Expand All @@ -43,7 +44,7 @@ async fn test_verify_relay() {

let txs = fixture::<Vec<TransactionOrReceiptIdPrimitive>>("ids.json")
.into_iter()
.take(VERIFY_ID_AMT)
.take(VERIFY_AMT)
.collect_vec();

let s = client.verify(txs, true).await.unwrap();
Expand Down
54 changes: 54 additions & 0 deletions nearx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,57 @@ pub mod verify;

#[cfg(test)]
mod test_utils;

pub const VERIFY_AMT: usize = 64;
pub const VERIFY_BATCH: usize = 4;

#[cfg(test)]
mod beefy_tests {
use log::logger;
use serial_test::serial;

use super::*;
use crate::test_utils::NETWORK;

#[test]
#[serial]
#[ignore]
fn sync_serialization() {
logger();

let mut builder = DefaultBuilder::new();

log::debug!("Defining circuit");
SyncCircuit::<NETWORK>::define(&mut builder);
let circuit = builder.build();
log::debug!("Done building circuit");

let mut hint_registry = HintRegistry::new();
let mut gate_registry = GateRegistry::new();
SyncCircuit::<NETWORK>::register_generators(&mut hint_registry);
SyncCircuit::<NETWORK>::register_gates(&mut gate_registry);

circuit.test_serializers(&gate_registry, &hint_registry);
}

#[test]
#[serial]
#[ignore]
fn verify_serialization() {
logger();

let mut builder = DefaultBuilder::new();

log::debug!("Defining circuit");
VerifyCircuit::<VERIFY_AMT, VERIFY_BATCH, NETWORK>::define(&mut builder);
let circuit = builder.build();
log::debug!("Done building circuit");

let mut hint_registry = HintRegistry::new();
let mut gate_registry = GateRegistry::new();
VerifyCircuit::<VERIFY_AMT, VERIFY_BATCH, NETWORK>::register_generators(&mut hint_registry);
VerifyCircuit::<VERIFY_AMT, VERIFY_BATCH, NETWORK>::register_gates(&mut gate_registry);

circuit.test_serializers(&gate_registry, &hint_registry);
}
}
12 changes: 5 additions & 7 deletions nearx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(any(feature = "sync", feature = "verify"))]
use near_light_clientx::plonky2x::backend::function::Plonky2xFunction;
#[allow(unused_imports)]
use near_light_clientx::{plonky2x::backend::function::Plonky2xFunction, VERIFY_AMT, VERIFY_BATCH};

// Testnet, FIXME: this is error prone, use something else
#[allow(dead_code)]
Expand All @@ -13,14 +13,12 @@ fn main() {
use near_light_clientx::SyncCircuit;
SyncCircuit::<NETWORK>::entrypoint();
} else if #[cfg(feature = "verify")] {
const PROOF_AMT: usize = 64;
const PROOF_BATCH_SIZE: usize = 4;

assert!(PROOF_AMT % PROOF_BATCH_SIZE == 0);
assert!((PROOF_AMT / PROOF_BATCH_SIZE).is_power_of_two());
assert!(VERIFY_AMT % VERIFY_BATCH == 0);
assert!((VERIFY_AMT / VERIFY_BATCH).is_power_of_two());

use near_light_clientx::VerifyCircuit;
VerifyCircuit::<PROOF_AMT, PROOF_BATCH_SIZE, NETWORK>::entrypoint();
VerifyCircuit::<VERIFY_AMT, VERIFY_BATCH, NETWORK>::entrypoint();
} else {
panic!("No circuit feature enabled");
}
Expand Down
10 changes: 7 additions & 3 deletions nearx/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use plonky2x::register_watch_generator;
pub use plonky2x::{self, backend::circuit::Circuit, prelude::*};

use crate::{
builder::Sync,
hint::{FetchHeaderInputs, FetchNextHeaderInputs},
variables::{BuildEndorsement, CryptoHashVariable, EncodeInner, HashBpsInputs},
variables::{
ApprovalMessage, BuildEndorsement, CryptoHashVariable, EncodeInner, HashBpsInputs,
StakeInfoVariable,
},
};

// TODO: lazy sync
Expand Down Expand Up @@ -60,6 +64,8 @@ impl<const NETWORK: usize> Circuit for SyncCircuit<NETWORK> {
registry.register_hint::<EncodeInner>();
registry.register_hint::<BuildEndorsement>();
registry.register_hint::<HashBpsInputs>();

register_watch_generator!(registry, L, D, ApprovalMessage, StakeInfoVariable);
}
}

Expand All @@ -76,8 +82,6 @@ mod beefy_tests {
fn sync_e2e() {
let (header, _, _) = testnet_state();
let header = header.hash().0;
let header =
bytes32!("0xeff7dccf304315aa520ad7e704062a8b8deadc5c0906e7e16d7305067a72a57e").0;

let define = |b: &mut B| {
SyncCircuit::<NETWORK>::define(b);
Expand Down
1 change: 1 addition & 0 deletions nearx/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ impl<const N: usize> MergeProofHint<N> {
mod beefy_tests {
use std::str::FromStr;

use log::logger;
use near_light_client_protocol::prelude::Itertools;
use near_primitives::types::TransactionOrReceiptId;
use serial_test::serial;
Expand Down

0 comments on commit f32adbe

Please sign in to comment.