Skip to content

Commit

Permalink
Fixing e2d and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgralinski-bright committed Dec 28, 2023
1 parent c8587f2 commit 06946bb
Show file tree
Hide file tree
Showing 19 changed files with 1,886 additions and 783 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -20,6 +20,8 @@ jobs:
with:
toolchain: 1.69
override: true
- name: Updating submodules
run: git submodule update --init --recursive
- name: Update Rust environment
run: rustup component add rust-src --toolchain 1.69-x86_64-unknown-linux-gnu
- name: Adding WebAssembly target
Expand Down
22 changes: 14 additions & 8 deletions README.md
Expand Up @@ -59,6 +59,12 @@ docker build -f docker/Dockerfile.testing --progress=plain .
```

## E2E tests
Currently, we have four major tests for testing different endings of the dispute:
* No majority of votes.
* Verdict against the owner of the dispute.
* Verdict against the defendant of the dispute.
* Testing dispute rounds.

To run E2E tests on your local machine, first run a aleph-node, build and deploy smart contract. We can do it, by running `deploy.sh` script:
```
bash scripts/deploy.sh
Expand All @@ -72,13 +78,13 @@ ink-wrapper -m ../contract/target/ink/bright_disputes.json --wasm-path ../contra

Finally we can run a e2e tests by calling:
```
cargo +nightly test --release
cargo +nightly-2023-04-19 test --release
```

# TODO:
* Store priv keys for jurors and judge in the JSON file and print it on the cmd
* Judge should issue a verdict and point the banned juries...
* Run/Fix all unit test
* Run/Fix all e2e test
* Update documentation + show cases

The output of the e2e test is:
```
test bright_disputes_test::test_dispute_verdict_none ... ok
test bright_disputes_test::test_dispute_verdict_positive ... ok
test bright_disputes_test::test_dispute_verdict_negative ... ok
test bright_disputes_test::test_dispute_rounds ... ok
```
14 changes: 14 additions & 0 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions cli/Cargo.toml
Expand Up @@ -6,10 +6,6 @@ edition = "2021"
license = "MIT"
publish = false

[lib]
name = "bright_disputes"
path = "src/lib.rs"

[dependencies]
anyhow = "1.0"
inquire = "0.6.2"
Expand All @@ -22,6 +18,7 @@ tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["fmt", "json", "env-filter"] }

bright-disputes-lib = { path="lib" }
ark-std = { version = "^0.3.0", default-features = false }
ark-ed-on-bls12-381 = { version = "^0.3.0", features = ["r1cs"] }
ark-serialize = { version = "^0.3.0", default-features = false }
Expand Down
165 changes: 48 additions & 117 deletions cli/src/bright_disputes.rs
Expand Up @@ -3,25 +3,20 @@ use std::path::Path;

use aleph_client::{contract::ContractInstance, AccountId, SignedConnection};
use anyhow::{anyhow, Result};
use ark_ed_on_bls12_381::EdwardsAffine as JubJubAffine;
use ark_ed_on_bls12_381::EdwardsProjective as JubJub;
use ark_std::{vec::Vec, One, Zero};
use ark_std::vec::Vec;
use bright_disputes_lib::{
helpers::{account_id_to_string, to_ink_account_id},
prepare_counting_inputs, prepare_voting_inputs, PublicVote,
};
use ink_wrapper_types::{Connection as _, SignedConnection as _};
use liminal_ark_relations::environment::CircuitField;
use liminal_ark_relations::disputes::{
ecdh::{Ecdh, EcdhScheme},
field_to_vote, hash_to_field, make_shared_key_hash, make_two_to_one_hash, vote_to_filed,
VerdictNegativeRelationWithFullInput, VerdictNoneRelationWithFullInput,
VerdictPositiveRelationWithFullInput, VerdictRelation, VoteRelationWithFullInput,
MAX_VOTES_LEN,
VerdictRelation,
};
use tracing::info;

use crate::{
bright_disputes_ink::{Dispute, Instance, Verdict},
generate_proof,
helpers::{account_id_to_string, to_ink_account_id},
};
use crate::bright_disputes_ink::{Dispute, Instance, Verdict};

impl From<&ContractInstance> for Instance {
fn from(contract: &ContractInstance) -> Self {
Expand Down Expand Up @@ -198,33 +193,19 @@ impl BrightDisputes {
.get_juror_public_key(connection, dispute_id, judge_id)
.await?;

let judge_pub_key = Ecdh::<JubJub>::deserialize_public_key(judge_pub_key);
let juror_priv_key = Ecdh::<JubJub>::deserialize_private_key(private_key);
let shared_key = Ecdh::<JubJub>::make_shared_key(judge_pub_key, juror_priv_key);
let hashed_shared_key = make_shared_key_hash(shared_key);
let encrypted_vote = vote_to_filed(vote) + hashed_shared_key;
let new_encrypted_all_votes =
make_two_to_one_hash(encrypted_vote, hash_to_field(dispute.votes_hash));

let circuit = VoteRelationWithFullInput::new(
encrypted_vote.0 .0,
dispute.votes_hash,
new_encrypted_all_votes.0 .0,
let (encrypted_vote, new_encrypted_all_votes, proof) = prepare_voting_inputs(
vote,
hashed_shared_key.0 .0,
);
let proof = generate_proof(circuit, vote_pk_file)?;
dispute.votes_hash,
judge_pub_key,
private_key,
vote_pk_file,
)?;

info!(target: "bright_disputes_cli", "Proof generated");

let ink_contract: Instance = (&self.contract).into();
connection
.exec(ink_contract.vote(
dispute_id,
encrypted_vote.0 .0,
new_encrypted_all_votes.0 .0,
proof,
))
.exec(ink_contract.vote(dispute_id, encrypted_vote, new_encrypted_all_votes, proof))
.await?;

Ok(())
Expand Down Expand Up @@ -312,95 +293,45 @@ impl BrightDisputes {
&self,
connection: &SignedConnection,
dispute_id: u32,
private_key: Vec<u8>,
judge_private_key: Vec<u8>,
verdict_none_pk: &Path,
verdict_negative_pk: &Path,
verdict_positive_pk: &Path,
) -> Result<()> {
let dispute = self.get_dispute(connection, dispute_id).await?;
let judge_priv_key = Ecdh::<JubJub>::deserialize_private_key(private_key.clone());

let mut jurors_banned = Vec::<ink_primitives::AccountId>::new();
let mut decoded_votes = Vec::<u8>::new();
let mut shared_keys = Vec::<[u64; 4]>::new();
let mut all_votes_hashed = hash_to_field([1u64; 4]);
let mut hashed_votes = CircuitField::one();
let mut sum_votes: u8 = 0;
for i in 0..MAX_VOTES_LEN as usize {
if let Some(vote) = dispute.votes.get(i) {
let juror_pub_key = self
.get_juror_public_key(connection, dispute_id, vote.juror)
.await?;
let juror_pub_key = Ecdh::<JubJub>::deserialize_public_key(juror_pub_key);
let shared_key = Ecdh::<JubJub>::make_shared_key(juror_pub_key, judge_priv_key);
let hashed_shared_key = make_shared_key_hash(shared_key);
shared_keys.push(hashed_shared_key.0 .0);

let decode_vote =
field_to_vote(hash_to_field(vote.vote) - hashed_shared_key.clone());
hashed_votes = make_two_to_one_hash(hash_to_field(vote.vote), hashed_votes);
all_votes_hashed = make_two_to_one_hash(hash_to_field(vote.vote), all_votes_hashed);
if decode_vote > 0 {
jurors_banned.push(vote.juror);
}
sum_votes += decode_vote;
decoded_votes.push(decode_vote);
} else {
let shared_key =
Ecdh::<JubJub>::make_shared_key(JubJubAffine::zero(), judge_priv_key);
let hashed_shared_key = make_shared_key_hash(shared_key);

decoded_votes.push(0u8);
shared_keys.push(hashed_shared_key.0 .0);
hashed_votes = make_two_to_one_hash(
vote_to_filed(0u8) + hashed_shared_key.clone(),
hashed_votes,
);
}
}

let votes_minimum: u8 = (0.75 * dispute.juries.len() as f32).ceil() as u8;
let votes_maximum: u8 = dispute.juries.len() as u8 - votes_minimum;
let verdict = if sum_votes >= votes_maximum {
Verdict::Positive()
} else if sum_votes <= votes_minimum {
Verdict::Negative()
} else {
Verdict::None()
};
let mut jurors_public_key = Vec::<Vec<u8>>::new();
for vote in &dispute.votes {
let key = self
.get_juror_public_key(connection, dispute_id, vote.juror)
.await?;
jurors_public_key.push(key);
}

let proof = match verdict {
Verdict::Positive() => generate_proof(
VerdictPositiveRelationWithFullInput::new(
votes_minimum,
VerdictRelation::Positive as u8,
hashed_votes.0 .0,
decoded_votes,
shared_keys,
),
verdict_positive_pk,
),
Verdict::Negative() => generate_proof(
VerdictNegativeRelationWithFullInput::new(
votes_maximum,
VerdictRelation::Negative as u8,
hashed_votes.0 .0,
decoded_votes,
shared_keys,
),
verdict_negative_pk,
),
Verdict::None() => generate_proof(
VerdictNoneRelationWithFullInput::new(
votes_maximum,
votes_minimum,
VerdictRelation::None as u8,
hashed_votes.0 .0,
decoded_votes,
shared_keys,
),
let votes: Vec<PublicVote> = dispute
.votes
.iter()
.zip(jurors_public_key.iter())
.map(|(&ref vote, &ref key)| PublicVote {
id: vote.juror,
pub_key: key.clone(),
hashed_vote: vote.vote,
})
.collect();

let (votes_maximum, votes_minimum, verdict, hashed_votes, jurors_banned, proof) =
prepare_counting_inputs(
judge_private_key,
votes,
verdict_none_pk,
),
verdict_negative_pk,
verdict_positive_pk,
)?;

let ink_verdict = match verdict {
VerdictRelation::Positive => Verdict::Positive(),
VerdictRelation::Negative => Verdict::Negative(),
VerdictRelation::None => Verdict::None(),
};

info!(target: "bright_disputes_cli", "Proof generated");
Expand All @@ -411,10 +342,10 @@ impl BrightDisputes {
dispute_id,
votes_maximum,
votes_minimum,
verdict,
hashed_votes.0 .0,
ink_verdict,
hashed_votes,
jurors_banned,
proof.unwrap(),
proof,
))
.await?;

Expand Down
15 changes: 13 additions & 2 deletions cli/src/bright_disputes_ink.rs
Expand Up @@ -4,8 +4,8 @@ use scale::Encode as _;

#[allow(dead_code)]
pub const CODE_HASH: [u8; 32] = [
130, 214, 182, 242, 229, 248, 203, 44, 209, 112, 26, 249, 97, 59, 55, 140, 79, 16, 61, 217,
180, 9, 197, 216, 115, 133, 245, 120, 19, 53, 156, 20,
134, 34, 57, 191, 103, 29, 44, 56, 12, 231, 88, 242, 64, 144, 53, 34, 131, 56, 170, 238, 192,
142, 229, 240, 82, 204, 67, 181, 250, 239, 22, 82,
];

#[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
Expand Down Expand Up @@ -214,6 +214,17 @@ impl Instance {
ink_wrapper_types::ReadCall::new(self.account_id, data)
}

/// Get juries pool
#[allow(dead_code, clippy::too_many_arguments)]
pub fn get_juries_pool(
&self,
) -> ink_wrapper_types::ReadCall<
Result<Vec<ink_primitives::AccountId>, ink_wrapper_types::InkLangError>,
> {
let data = vec![3, 234, 94, 249];
ink_wrapper_types::ReadCall::new(self.account_id, data)
}

/// Get single dispute by id
#[allow(dead_code, clippy::too_many_arguments)]
pub fn remove_dispute(&self, dispute_id: u32) -> ink_wrapper_types::ExecCall {
Expand Down
13 changes: 0 additions & 13 deletions cli/src/helpers.rs

This file was deleted.

23 changes: 0 additions & 23 deletions cli/src/lib.rs

This file was deleted.

0 comments on commit 06946bb

Please sign in to comment.