Skip to content

Commit

Permalink
fix: size was not fulfilled for block signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
dndll committed Feb 16, 2024
1 parent afdff1a commit d784a8a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crates/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::prelude::*;

pub mod prelude;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub enum Network {
Mainnet,
#[default]
Expand Down
12 changes: 6 additions & 6 deletions nearx/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ impl<L: PlonkParameters<D>, const D: usize> AsyncHint<L, D> for FetchNextHeaderI

impl FetchNextHeaderInputs {
pub fn fetch<L: PlonkParameters<D>, const D: usize>(
self,
&self,
b: &mut CircuitBuilder<L, D>,
hash: &CryptoHashVariable,
) -> Option<BlockVariable> {
let mut input_stream = VariableStream::new();
input_stream.write::<CryptoHashVariable>(hash);

let output_stream = b.async_hint(input_stream, self);
let output_stream = b.async_hint(input_stream, self.clone());
Some(output_stream.read::<BlockVariable>(b))
}
}
Expand Down Expand Up @@ -75,14 +75,14 @@ impl<L: PlonkParameters<D>, const D: usize> AsyncHint<L, D> for FetchHeaderInput
impl FetchHeaderInputs {
/// Fetches a header based on its known hash and witnesses the result.
pub fn fetch<L: PlonkParameters<D>, const D: usize>(
self,
&self,
b: &mut CircuitBuilder<L, D>,
trusted_hash: &CryptoHashVariable,
) -> HeaderVariable {
let mut input_stream = VariableStream::new();
input_stream.write::<CryptoHashVariable>(trusted_hash);

let output_stream = b.async_hint(input_stream, self);
let output_stream = b.async_hint(input_stream, self.clone());
let untrusted = output_stream.read::<HeaderVariable>(b);
let untrusted_hash = untrusted.hash(b);
b.assert_is_equal(*trusted_hash, untrusted_hash);
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<L: PlonkParameters<D>, const D: usize, const B: usize> AsyncHint<L, D>

impl<const N: usize> FetchProofInputs<N> {
pub fn fetch<L: PlonkParameters<D>, const D: usize>(
self,
&self,
b: &mut CircuitBuilder<L, D>,
head: &HeaderVariable,
reqs: &[TransactionOrReceiptIdVariable],
Expand All @@ -162,7 +162,7 @@ impl<const N: usize> FetchProofInputs<N> {
input_stream.write::<CryptoHashVariable>(&head.hash(b));
input_stream.write_slice::<TransactionOrReceiptIdVariable>(reqs);

let output_stream = b.async_hint(input_stream, self);
let output_stream = b.async_hint(input_stream, self.clone());
let mut inputs = vec![];
for _ in 0..N {
inputs.push(ProofInputVariable {
Expand Down
23 changes: 13 additions & 10 deletions nearx/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,30 @@ impl<const NETWORK: usize> Circuit for SyncCircuit<NETWORK> {
<<L as PlonkParameters<D>>::Config as plonky2::plonk::config::GenericConfig<D>>::Hasher:
plonky2::plonk::config::AlgebraicHasher<<L as PlonkParameters<D>>::Field>,
{
let network = NETWORK.into();
let fetch_header = FetchHeaderInputs(network);
let fetch_next_header = FetchNextHeaderInputs(network);

let trusted_header_hash = b.evm_read::<CryptoHashVariable>();
let head = FetchHeaderInputs(NETWORK.into()).fetch(b, &trusted_header_hash);

// This is a very interesting trick to be able to get the BPS for the next epoch
// without the need to store the BPS, we verify the hash of the BPS in the
// circuit
let bps = FetchNextHeaderInputs(NETWORK.into())
.fetch(b, &head.inner_lite.next_epoch_id)
let header = fetch_header.fetch(b, &trusted_header_hash);
let bps = fetch_next_header
.fetch(b, &header.inner_lite.next_epoch_id)
.unwrap()
.next_bps;

let bps_hash = HashBpsInputs.hash(b, &bps);
b.assert_is_equal(head.inner_lite.next_bp_hash, bps_hash);
b.assert_is_equal(header.inner_lite.next_bp_hash, bps_hash);
b.watch(&bps_hash, "calculate_bps_hash");

let head_hash = head.hash(b);
let next_block = FetchNextHeaderInputs(NETWORK.into())
.fetch(b, &head_hash)
let next_block = fetch_next_header
.fetch(b, &trusted_header_hash)
.expect("Failed to fetch next block");
b.watch(&bps_hash, "calculate_bps_hash");

let synced = b.sync(&head, &bps, &next_block);
let synced = b.sync(&header, &bps, &next_block);
let synced_hash = synced.new_head.hash(b);
b.evm_write::<CryptoHashVariable>(synced_hash);
}
Expand All @@ -50,8 +53,8 @@ impl<const NETWORK: usize> Circuit for SyncCircuit<NETWORK> {
<<L as PlonkParameters<D>>::Config as plonky2::plonk::config::GenericConfig<D>>::Hasher:
plonky2::plonk::config::AlgebraicHasher<L::Field>,
{
registry.register_async_hint::<FetchNextHeaderInputs>();
registry.register_async_hint::<FetchHeaderInputs>();
registry.register_async_hint::<FetchNextHeaderInputs>();
registry.register_hint::<EncodeInner>();
registry.register_hint::<BuildEndorsement>();
registry.register_hint::<HashBpsInputs>();
Expand Down
17 changes: 14 additions & 3 deletions nearx/src/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,23 @@ impl<F: RichField> From<LightClientBlockView> for BlockVariableValue<F> {
.0
.into();

Self {
let variable = Self {
next_block_inner_hash: block.next_block_inner_hash.0.into(),
header: block.clone().into(),
next_bps: bps_to_variable(block.next_bps),
approvals_after_next: block.approvals_after_next.into(),
next_bps_hash,
}
};
assert_eq!(variable.next_bps.len(), NUM_BLOCK_PRODUCER_SEATS);
assert_eq!(
variable.approvals_after_next.is_active.len(),
NUM_BLOCK_PRODUCER_SEATS
);
assert_eq!(
variable.approvals_after_next.signatures.len(),
NUM_BLOCK_PRODUCER_SEATS
);
variable
}
}

Expand All @@ -345,7 +355,8 @@ pub struct BpsApprovals<const AMT: usize> {
impl<F: RichField, const AMT: usize> From<Vec<Option<Box<Signature>>>>
for BpsApprovalsValue<AMT, F>
{
fn from(approvals: Vec<Option<Box<Signature>>>) -> Self {
fn from(mut approvals: Vec<Option<Box<Signature>>>) -> Self {
approvals.resize(AMT, None);
let (signatures, is_active) = approvals
.into_iter()
.take(AMT)
Expand Down

0 comments on commit d784a8a

Please sign in to comment.