Skip to content

Commit

Permalink
wip: evm
Browse files Browse the repository at this point in the history
  • Loading branch information
dndll committed Jan 31, 2024
1 parent 90aaab5 commit bdedd24
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 311 deletions.
2 changes: 1 addition & 1 deletion circuits/plonky2x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ version = "0.1.0"
async-trait.workspace = true
borsh.workspace = true
ethers = "2.0.11"
hex.workspace = true
log.workspace = true
pretty_assertions = "1.4.0"
serde.workspace = true
Expand All @@ -21,7 +22,6 @@ near-light-client-rpc.workspace = true

[dev-dependencies]
borsh.workspace = true
hex.workspace = true
near-primitives.workspace = true
pretty_env_logger.workspace = true
serde_json.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion circuits/plonky2x/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,12 @@ impl<L: PlonkParameters<D>, const D: usize> Sync<L, D> for CircuitBuilder<L, D>
let d = self.ensure_stake_is_sufficient(&stake);
self.assertx(d);

// TODO: might not need this now, also the logic is wrong because nbps is always >0
let (next_bps_epoch, next_bps) = if next_block.next_bps.len() > 0 {
// TODO: hashing bps in circut
let e = self.ensure_next_bps_is_valid(
&next_block.header.inner_lite.next_bp_hash,
Some(&next_block.next_bp_hash),
Some(&next_block.next_bps_hash),
);
self.assertx(e);
assert!(next_block.next_bps.len() == NUM_BLOCK_PRODUCER_SEATS);
Expand Down
39 changes: 39 additions & 0 deletions circuits/plonky2x/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,45 @@ impl FetchNextHeaderInputs {
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FetchBpsInputs(pub Network);

#[async_trait]
impl<L: PlonkParameters<D>, const D: usize> AsyncHint<L, D> for FetchBpsInputs {
async fn hint(
&self,
input_stream: &mut ValueStream<L, D>,
output_stream: &mut ValueStream<L, D>,
) {
let client = NearRpcClient::new(self.0.clone());

let epoch = input_stream.read_value::<CryptoHashVariable>().0;

let bps = client
.fetch_epoch_bps(&CryptoHash(epoch))
.await
.expect("Failed to fetch bps");
log::debug!("fetched bps: {}", bps.len());
let bps = bps_to_variable(Some(bps));

output_stream.write_value::<BpsArr<ValidatorStakeVariable>>(bps.into());
}
}

impl FetchBpsInputs {
pub fn fetch<L: PlonkParameters<D>, const D: usize>(
self,
b: &mut CircuitBuilder<L, D>,
epoch: &CryptoHashVariable,
) -> BpsArr<ValidatorStakeVariable> {
let mut input_stream = VariableStream::new();
input_stream.write::<CryptoHashVariable>(epoch);

let output_stream = b.async_hint(input_stream, self);
output_stream.read::<BpsArr<ValidatorStakeVariable>>(b)
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FetchBatchInputs<const AMT: usize = 1>(pub Network);

Expand Down
32 changes: 19 additions & 13 deletions circuits/plonky2x/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![feature(generic_const_exprs)]

use builder::Sync;
use hint::FetchNextHeaderInputs;
use hint::{FetchBpsInputs, FetchNextHeaderInputs};
pub use plonky2x::{self, backend::circuit::Circuit, prelude::*};
use variables::{BpsArr, HeaderVariable, ValidatorStakeVariable};
use variables::{
BpsArr, CryptoHashVariable, HashBpsInputs, HeaderVariable, ValidatorStakeVariable,
};
use variables::{BuildEndorsement, EncodeInner, SyncedVariable};

/// Building blocks injected into the CircuitBuilder
Expand Down Expand Up @@ -39,15 +41,19 @@ impl<const AMT: usize> Circuit for SyncCircuit<AMT> {
<<L as PlonkParameters<D>>::Config as plonky2::plonk::config::GenericConfig<D>>::Hasher:
plonky2::plonk::config::AlgebraicHasher<<L as PlonkParameters<D>>::Field>,
{
// TODO: evm
let head = b.read::<HeaderVariable>();
let bps = b.read::<BpsArr<ValidatorStakeVariable>>();
let trusted_head = b.evm_read::<HeaderVariable>();

let head_hash = head.hash(b);
let bps = FetchBpsInputs(near_light_client_rpc::Network::Testnet)
.fetch(b, &trusted_head.inner_lite.next_epoch_id);
let bps_hash = HashBpsInputs.hash(b, &bps);
b.watch(&bps_hash, "calculate_bps_hash");
b.assert_is_equal(trusted_head.inner_lite.next_bp_hash, bps_hash);

let head_hash = trusted_head.hash(b);
let next_block =
FetchNextHeaderInputs(near_light_client_rpc::Network::Testnet).fetch(b, &head_hash);
let synced = b.sync(&head, &bps, &next_block.unwrap());
b.write::<SyncedVariable>(synced);
// let synced = b.sync(&trusted_head, &bps, &next_block.unwrap());
// b.evm_write::<HeaderVariable>(synced.new_head);
}

fn register_generators<L: PlonkParameters<D>, const D: usize>(registry: &mut HintRegistry<L, D>)
Expand All @@ -66,27 +72,27 @@ impl<const AMT: usize> Circuit for SyncCircuit<AMT> {
mod beefy_tests {
use super::*;
use crate::{
test_utils::{builder_suite, test_state, B, PI, PO},
test_utils::{builder_suite, testnet_state, B, PI, PO},
variables::bps_to_variable,
};
use ::test_utils::CryptoHash;
use serial_test::serial;

#[test]
#[serial]
fn beefy_test_sync_e2e() {
const SYNC_AMT: usize = 1;
let (header, bps, _nb) = test_state();
let (header, _bps, _) = testnet_state();

let define = |b: &mut B| {
b.set_debug();
SyncCircuit::<SYNC_AMT>::define(b);
};
let writer = |input: &mut PI| {
input.write::<HeaderVariable>(header.into());
input.write::<BpsArr<ValidatorStakeVariable>>(bps_to_variable(Some(bps)));
input.evm_write::<HeaderVariable>(header.into());
};
let assertions = |mut output: PO| {
println!("{:#?}", output.read::<SyncedVariable>().new_head);
println!("{:#?}", output.evm_read::<HeaderVariable>());
};
builder_suite(define, writer, assertions);
}
Expand Down
6 changes: 6 additions & 0 deletions circuits/plonky2x/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub fn builder_suite<F, WriteInputs, Assertions>(
writer(&mut inputs);

let (proof, output) = circuit.prove(&inputs);
//proof.public_inputs
std::fs::write(
"input3.bin",
serde_json::to_string(&proof.public_inputs).unwrap(),
)
.unwrap();

assertions(output.clone());

Expand Down
Loading

0 comments on commit bdedd24

Please sign in to comment.