Skip to content

Commit

Permalink
wip: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dndll committed Mar 21, 2024
1 parent 576944e commit cf201f0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 154 deletions.
12 changes: 1 addition & 11 deletions crates/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,7 @@ impl Protocol {
let approved_stake = match Self::validate_signature(approval_message, sig, pk) {
Ok(_) => approved_stake + stake,
Err(Error::SignatureInvalid) => {
log::debug!(
"invalid signature: pk: {} sig: {:?}",
hex::encode(pk.unwrap_as_ed25519().0),
sig.clone().map(|s| {
if let Signature::ED25519(s) = *s {
hex::encode(s.r_bytes())
} else {
unreachable!()
}
})
);
log::debug!("invalid signature: pk: {} sig: {:?}", pk, sig);
approved_stake
}
Err(Error::ValidatorNotSigned) => approved_stake,
Expand Down
92 changes: 49 additions & 43 deletions nearx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use pretty_assertions::assert_eq;
use crate::{
merkle::{MerklePathVariable, NearMerkleTree},
variables::{
ApprovalMessage, BlockHeightVariable, BlockVariable, BpsApprovals, BpsArr,
BuildEndorsement, CryptoHashVariable, HeaderVariable, ProofVariable, PublicKeyVariable,
StakeInfoVariable, ValidatorStakeVariable,
ApprovalMessage, BlockHeightVariable, BlockVariable, BpsApprovals, BuildEndorsement,
CryptoHashVariable, HeaderVariable, ProofVariable, PublicKeyVariable, StakeInfoVariable,
Validators,
},
};

Expand All @@ -30,17 +30,17 @@ pub trait Ensure<L: PlonkParameters<D>, const D: usize> {
epoch_id: &CryptoHashVariable,
) -> BoolVariable;

fn ensure_if_next_epoch_contains_next_bps(
fn ensure_if_next_epoch_contains_next_bps<const LEN: usize>(
&mut self,
head: &HeaderVariable,
epoch_id: &CryptoHashVariable,
next_bps: &BpsArr<ValidatorStakeVariable>,
next_bps: &Validators<LEN>,
) -> BoolVariable;

fn validate_signatures<const LEN: usize>(
&mut self,
approvals: &BpsApprovals<LEN>,
bps: &BpsArr<ValidatorStakeVariable, LEN>,
bps: &Validators<LEN>,
approval_message: ApprovalMessage,
) -> StakeInfoVariable;

Expand Down Expand Up @@ -102,27 +102,27 @@ impl<L: PlonkParameters<D>, const D: usize> Ensure<L, D> for CircuitBuilder<L, D
self.or(this, next)
}

fn ensure_if_next_epoch_contains_next_bps(
fn ensure_if_next_epoch_contains_next_bps<const LEN: usize>(
&mut self,
head: &HeaderVariable,
epoch_id: &CryptoHashVariable,
next_bps: &BpsArr<ValidatorStakeVariable>,
next_bps: &Validators<LEN>,
) -> BoolVariable {
let is_next_epoch = self.is_equal(head.inner_lite.next_epoch_id, *epoch_id);
let is_not_empty = self.constant(next_bps.len() > 0);
let is_not_empty = self.constant(next_bps.inner.len() > 0);
let ok_anyway = self._true();
self.select(is_next_epoch, is_not_empty, ok_anyway)
}

fn validate_signatures<const LEN: usize>(
&mut self,
approvals_after_next: &BpsApprovals<LEN>,
epoch_bps: &BpsArr<ValidatorStakeVariable, LEN>,
epoch_bps: &Validators<LEN>,
approval_message: ApprovalMessage,
) -> StakeInfoVariable {
assert_eq!(approvals_after_next.is_active.len(), LEN);
assert_eq!(approvals_after_next.signatures.len(), LEN);
assert_eq!(epoch_bps.data.len(), LEN);
assert_eq!(epoch_bps.inner.len(), LEN);

let messages = [approval_message; LEN];

Expand All @@ -135,7 +135,7 @@ impl<L: PlonkParameters<D>, const D: usize> Ensure<L, D> for CircuitBuilder<L, D
let mut approved_stake = self.zero();

for i in 0..LEN {
let vs = &epoch_bps.data[i];
let vs = &epoch_bps.inner.data[i];

let is_dummy = self.is_equal(dummy_pk.clone(), vs.public_key.clone());
let sig_active = approvals_after_next.is_active[i];
Expand Down Expand Up @@ -249,22 +249,25 @@ impl<L: PlonkParameters<D>, const D: usize> Ensure<L, D> for CircuitBuilder<L, D
}

pub trait Sync<L: PlonkParameters<D>, const D: usize> {
fn sync(
fn sync<const LEN: usize>(
&mut self,
head: &HeaderVariable,
epoch_bps: &BpsArr<ValidatorStakeVariable>,
next_block: &BlockVariable,
epoch_bps: &Validators<LEN>,
next_block: &BlockVariable<LEN>,
) -> HeaderVariable;

fn reconstruct_approval_message(&mut self, next_block: &BlockVariable) -> ApprovalMessage;
fn reconstruct_approval_message<const LEN: usize>(
&mut self,
next_block: &BlockVariable<LEN>,
) -> ApprovalMessage;
}

impl<L: PlonkParameters<D>, const D: usize> Sync<L, D> for CircuitBuilder<L, D> {
fn sync(
fn sync<const LEN: usize>(
&mut self,
head: &HeaderVariable,
epoch_bps: &BpsArr<ValidatorStakeVariable>,
next_block: &BlockVariable,
epoch_bps: &Validators<LEN>,
next_block: &BlockVariable<LEN>,
) -> HeaderVariable {
let not_verified =
self.ensure_not_already_verified(head, &next_block.header.inner_lite.height);
Expand Down Expand Up @@ -300,12 +303,15 @@ impl<L: PlonkParameters<D>, const D: usize> Sync<L, D> for CircuitBuilder<L, D>
&next_block.next_bps_hash,
);
self.assertx(bps_valid);
assert!(next_block.next_bps.len() == NUM_BLOCK_PRODUCER_SEATS);
assert!(next_block.next_bps.inner.len() == NUM_BLOCK_PRODUCER_SEATS);

next_block.header.to_owned()
}

fn reconstruct_approval_message(&mut self, next_block: &BlockVariable) -> ApprovalMessage {
fn reconstruct_approval_message<const LEN: usize>(
&mut self,
next_block: &BlockVariable<LEN>,
) -> ApprovalMessage {
let next_header_hash = next_block.header.hash(self);
let next_block_hash =
self.curta_sha256_pair(next_block.next_block_inner_hash, next_header_hash);
Expand Down Expand Up @@ -482,7 +488,7 @@ mod tests {

let define = |builder: &mut B| {
let header = builder.read::<HeaderVariable>();
let next_bps = builder.read::<BpsArr<ValidatorStakeVariable>>();
let next_bps = builder.read::<Validators>();

let current =
builder.ensure_epoch_is_current_or_next(&header, &header.inner_lite.epoch_id);
Expand All @@ -505,7 +511,7 @@ mod tests {
};
let writer = |input: &mut PI| {
input.write::<HeaderVariable>(header.clone().into());
input.write::<BpsArr<ValidatorStakeVariable>>(bps_to_variable(Some(bps)));
input.write::<Validators>(ValidatorsVariableValue::from_iter(bps));
};
let assertions = |mut output: PO| {
assert!(output.read::<BoolVariable>(), "epoch is current");
Expand All @@ -520,12 +526,12 @@ mod tests {
fn test_reconstruct_approval_msg() {
let (_, _, next_block) = test_state();
let define = |builder: &mut B| {
let next_block = builder.read::<BlockVariable>();
let next_block = builder.read::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>();
let os = builder.reconstruct_approval_message(&next_block);
builder.write::<ApprovalMessage>(os);
};
let writer = |input: &mut PI| {
input.write::<BlockVariable>(next_block.clone().into());
input.write::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>(next_block.clone().into());
};
let assertions = |mut output: PO| {
let created = output.read::<ApprovalMessage>();
Expand All @@ -539,7 +545,7 @@ mod tests {
fn test_raw_le_bytes() {
let (_, _, next_block) = test_state();
let define = |builder: &mut B| {
let next_block = builder.read::<BlockVariable>();
let next_block = builder.read::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>();

let mut bytes = vec![];
for target in next_block.header.inner_lite.height.targets() {
Expand All @@ -558,7 +564,7 @@ mod tests {
builder.write::<BytesVariable<8>>(BytesVariable(bytes.try_into().unwrap()));
};
let writer = |input: &mut PI| {
input.write::<BlockVariable>(next_block.clone().into());
input.write::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>(next_block.clone().into());
};
let assertions = |mut output: PO| {
let bytes = output.read::<BytesVariable<8>>();
Expand All @@ -576,6 +582,7 @@ mod tests {
/// TODO: CI for only beefy tests
#[cfg(test)]
mod beefy_tests {
use near_light_client_primitives::NUM_BLOCK_PRODUCER_SEATS;
use serial_test::serial;

use crate::{
Expand All @@ -593,7 +600,7 @@ mod beefy_tests {

let define = |builder: &mut B| {
let header = builder.read::<HeaderVariable>();
let next_bps = builder.read::<BpsArr<ValidatorStakeVariable>>();
let next_bps = builder.read::<Validators>();

let current =
builder.ensure_epoch_is_current_or_next(&header, &header.inner_lite.epoch_id);
Expand All @@ -616,7 +623,7 @@ mod beefy_tests {
};
let writer = |input: &mut PI| {
input.write::<HeaderVariable>(header.clone().into());
input.write::<BpsArr<ValidatorStakeVariable>>(bps_to_variable(Some(bps)));
input.write::<Validators>(ValidatorsVariableValue::from_iter(bps));
};
let assertions = |mut output: PO| {
assert!(output.read::<BoolVariable>(), "epoch is current");
Expand Down Expand Up @@ -662,15 +669,17 @@ mod beefy_tests {

let define = |builder: &mut B| {
let head = builder.read::<HeaderVariable>();
let bps = builder.read::<BpsArr<ValidatorStakeVariable>>();
let next_block = builder.read::<BlockVariable>();
let bps = builder.read::<Validators>();
let next_block = builder.read::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>();
let synced = builder.sync(&head, &bps, &next_block);
builder.write::<HeaderVariable>(synced);
};
let writer = |input: &mut PI| {
input.write::<HeaderVariable>(head.into());
input.write::<BpsArr<ValidatorStakeVariable>>(bps_to_variable(Some(next_bps)));
input.write::<BlockVariable>(next_block.clone().into());
input.write::<Validators<NUM_BLOCK_PRODUCER_SEATS>>(
ValidatorsVariableValue::from_iter(next_bps),
);
input.write::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>(next_block.clone().into());
};
let assertions = |mut output: PO| {
let header = output.read::<HeaderVariable>();
Expand All @@ -686,29 +695,26 @@ mod beefy_tests {
fn beefy_builder_test_bounded_signatures() {
let (_, bps, next_block) = test_state();
const BPS_AMT: usize = 15;
let r = 0..BPS_AMT;

let define = |builder: &mut B| {
let bps = builder.read::<BpsArr<ValidatorStakeVariable, BPS_AMT>>();
let next_block = builder.read::<BlockVariable>();
let bps = builder.read::<Validators<BPS_AMT>>();
let next_block = builder.read::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>();

let next_block_approvals = BpsApprovals {
signatures: next_block.approvals_after_next.signatures[0..BPS_AMT]
.to_vec()
.into(),
is_active: next_block.approvals_after_next.is_active[0..BPS_AMT]
signatures: next_block.approvals_after_next.signatures[r.clone()]
.to_vec()
.into(),
is_active: next_block.approvals_after_next.is_active[r].to_vec().into(),
};

let msg = builder.reconstruct_approval_message(&next_block);

builder.validate_signatures(&next_block_approvals, &bps, msg);
};
let writer = |input: &mut PI| {
input.write::<BpsArr<ValidatorStakeVariable, BPS_AMT>>(
bps_to_variable(Some(bps.clone()))[0..BPS_AMT].into(),
);
input.write::<BlockVariable>(next_block.into());
input.write::<Validators<BPS_AMT>>(bps.into_iter().collect());
input.write::<BlockVariable<NUM_BLOCK_PRODUCER_SEATS>>(next_block.into());
};
let assertions = |mut _output: PO| {};
builder_suite(define, writer, assertions);
Expand Down
Loading

0 comments on commit cf201f0

Please sign in to comment.