Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E tests #8

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Bright Disputes
This project
This project is a dApp for raising and solving the disputes on the Substrate-based blockchains.

## Prerequisites
1. `cargo-contract 2.x`
2. `ink-wrapper 0.4.1`

## Build
To build smart contract run:
```
cargo +nightly-2022-11-28 contract build --release --manifest-path contract/Cargo.toml
```

## Run
Running smart contract can be done by running `deploy.sh` script:
```
Expand All @@ -34,4 +34,23 @@ cargo +nightly-2022-11-28 test --release --manifest-path contract/Cargo.toml
or with docker:
```
docker build -f docker/Dockerfile.testing --progress=plain .
```
```

## E2E tests
To run E2E tests on you local machine, first run a aleph-node. We can do it, by running `deploy.sh` from the running part. After that we need build contract on our local machine:
```
cargo +nightly-2022-11-28 contract build --release --manifest-path contract/Cargo.toml
```
and upload it to the node:
```
cargo contract upload --manifest-path contract/Cargo.toml --suri //Alice --url ws://localhost:9944 || true
```
after that we need to use [ink-wrapper](https://crates.io/crates/ink-wrapper) tool to generate a type-safe code for calling smart contract from our e2e tests:
```
cd tests
ink-wrapper -m ../contract/target/ink/bright_disputes.json --wasm-path ../contract/target/ink/bright_disputes.wasm | rustfmt --edition 2021 > bright_disputes.rs
```
Finally we can run a e2e tests by calling:
```
cargo +nightly test --release
```
94 changes: 72 additions & 22 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod bright_disputes {

/// Main contract storage
#[ink(storage)]
#[derive(Default)]
pub struct BrightDisputes {
last_dispute_id: DisputeId,
juries_pool: Vec<AccountId>,
Expand Down Expand Up @@ -85,6 +86,12 @@ pub mod bright_disputes {
}
}

/// Get last dispute id
#[ink(message)]
pub fn get_last_dispute_id(&self) -> DisputeId {
self.last_dispute_id
}

/// Get single dispute by id
#[ink(message)]
pub fn get_dispute(&self, dispute_id: DisputeId) -> Result<Dispute> {
Expand Down Expand Up @@ -119,13 +126,12 @@ pub mod bright_disputes {
defendant_id: AccountId,
escrow: Balance,
) -> Result<DisputeId> {
self.assert_transferred(escrow)?;
let owner_id = ink::env::caller::<ink::env::DefaultEnvironment>();
self.last_dispute_id = self.generate_dispute_id()?;
let dispute = Dispute::create(self.last_dispute_id, owner_link, defendant_id, escrow);
self.update_dispute(dispute);

self.env().transfer(self.env().account_id(), escrow)?;

self.env().emit_event(DisputeRaised {
id: self.last_dispute_id,
owner_id,
Expand All @@ -136,18 +142,17 @@ pub mod bright_disputes {
}

/// Defendant confirms his participation in dispute.
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_defendant(
&mut self,
dispute_id: DisputeId,
defendant_link: String,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
let id = dispute.id();
self.assert_transferred(dispute.escrow())?;
dispute.confirm_defendant(defendant_link)?;
dispute.set_dispute_round(DisputeRound::create(self.env().block_timestamp(), None));
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
dispute.increment_deposit();
self.update_dispute(dispute);

Expand Down Expand Up @@ -193,6 +198,7 @@ pub mod bright_disputes {
let mut jure = self.get_jure_or_assert(caller)?;
dispute.vote(Vote::create(caller, vote))?;
jure.action_done(dispute.id())?;
self.update_jure(jure);
self.update_dispute(dispute);
Ok(())
}
Expand Down Expand Up @@ -222,18 +228,17 @@ pub mod bright_disputes {
}

/// Assigned jure can confirm his participation in dispute
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_jure_participation_in_dispute(
&mut self,
dispute_id: DisputeId,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
self.assert_transferred(dispute.escrow())?;

let caller = ink::env::caller::<ink::env::DefaultEnvironment>();
let mut jure = self.get_jure_or_assert(caller)?;
jure.confirm_participation_in_dispute(dispute_id)?;
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
self.update_jure(jure);

dispute.increment_deposit();
Expand All @@ -242,18 +247,17 @@ pub mod bright_disputes {
}

/// Judge can confirm his participation in dispute
#[ink(message)]
#[ink(message, payable)]
pub fn confirm_judge_participation_in_dispute(
&mut self,
dispute_id: DisputeId,
) -> Result<()> {
let mut dispute = self.get_dispute_or_assert(dispute_id)?;
self.assert_transferred(dispute.escrow())?;

let caller = ink::env::caller::<ink::env::DefaultEnvironment>();
let mut jure = self.get_jure_or_assert(caller)?;
jure.confirm_participation_in_dispute(dispute_id)?;
self.env()
.transfer(self.env().account_id(), dispute.escrow())?;
self.update_jure(jure);

dispute.increment_deposit();
Expand All @@ -278,10 +282,10 @@ pub mod bright_disputes {
}

// Check if juries votes
for judge_id in dispute.juries() {
let judge = self.get_jure_or_assert(judge_id)?;
if judge.is_requested_for_action(dispute_id) {
dispute.move_to_banned(judge_id)?;
for jure_id in dispute.juries() {
let jure = self.get_jure_or_assert(jure_id)?;
if jure.is_requested_for_action(dispute_id) {
dispute.move_to_banned(jure_id)?;
}
}

Expand All @@ -291,10 +295,10 @@ pub mod bright_disputes {

BrightDisputesError::MajorityOfVotesNotReached => {
// Check if juries votes
for judge_id in dispute.juries() {
let judge = self.get_jure_or_assert(judge_id)?;
if judge.is_requested_for_action(dispute_id) {
dispute.move_to_banned(judge_id)?;
for jure_id in dispute.juries() {
let jure = self.get_jure_or_assert(jure_id)?;
if jure.is_requested_for_action(dispute_id) {
dispute.move_to_banned(jure_id)?;
}
}

Expand Down Expand Up @@ -420,18 +424,31 @@ pub mod bright_disputes {
}
return Ok(());
}

fn assert_transferred(&self, expected_amount: Balance) -> Result<()> {
let transferred = self.env().transferred_value();
if transferred != expected_amount {
return Err(BrightDisputesError::InvalidEscrowAmount);
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use ink::env::{test::set_caller, DefaultEnvironment};
use ink::env::{
test::{set_caller, set_value_transferred},
DefaultEnvironment,
};

use super::*;

fn create_test_bright_dispute_with_running_dispute() -> BrightDisputes {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
let mut bright_disputes = BrightDisputes::new();

set_value_transferred::<DefaultEnvironment>(10);

// Alice creates a dispute
let dispute_id = bright_disputes
.create_dispute("https://brightinventions.pl/".into(), accounts.bob, 10)
Expand Down Expand Up @@ -480,9 +497,16 @@ pub mod bright_disputes {
let owner_link = "https://brightinventions.pl/";
let escrow_amount: Balance = 15;
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(escrow_amount);

let result =
bright_disputes.create_dispute(owner_link.into(), accounts.bob, escrow_amount);
assert_eq!(result, Ok(1));

// Failed, escrow amount doesn't match the transferred value.
let result =
bright_disputes.create_dispute(owner_link.into(), accounts.bob, escrow_amount + 1);
assert_eq!(result, Err(BrightDisputesError::InvalidEscrowAmount));
}

/// Test if we can create multiple disputes.
Expand All @@ -492,6 +516,7 @@ pub mod bright_disputes {

let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);

// Alice creates first dispute
let result = bright_disputes.create_dispute(
Expand Down Expand Up @@ -519,6 +544,8 @@ pub mod bright_disputes {
let result = bright_disputes.get_dispute(1);
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

set_value_transferred::<DefaultEnvironment>(10);

bright_disputes
.create_dispute("https://brightinventions.pl/1".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand All @@ -534,6 +561,8 @@ pub mod bright_disputes {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
let mut bright_disputes = BrightDisputes::new();

set_value_transferred::<DefaultEnvironment>(10);

bright_disputes
.create_dispute("https://brightinventions.pl/1".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -565,6 +594,7 @@ pub mod bright_disputes {
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

// Create dispute
set_value_transferred::<DefaultEnvironment>(10);
bright_disputes
.create_dispute("https://brightinventions.pl".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -608,6 +638,7 @@ pub mod bright_disputes {
let result = bright_disputes.confirm_defendant(1, defendant_link.into());
assert_eq!(result, Err(BrightDisputesError::DisputeNotExist));

set_value_transferred::<DefaultEnvironment>(10);
let dispute_id = bright_disputes
.create_dispute("".into(), accounts.bob, 10)
.expect("Failed to create a dispute!");
Expand Down Expand Up @@ -663,6 +694,7 @@ pub mod bright_disputes {
fn update_defendant_description() {
let accounts = ink::env::test::default_accounts::<DefaultEnvironment>();
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);

let mut bright_disputes = BrightDisputes::new();

Expand Down Expand Up @@ -758,6 +790,7 @@ pub mod bright_disputes {

// Switch to "PickingJuriesAndJudge" state.
set_caller::<DefaultEnvironment>(accounts.alice);
set_value_transferred::<DefaultEnvironment>(10);
bright_disputes
.process_dispute_round(dispute_id)
.expect("Failed to process dispute round!");
Expand All @@ -768,14 +801,31 @@ pub mod bright_disputes {

let judge = dispute.judge().expect("Judge was not assigned!");

// Success
// Fail to confirm judge, invalid escrow
set_caller::<DefaultEnvironment>(judge);
set_value_transferred::<DefaultEnvironment>(5);
let result = bright_disputes.confirm_judge_participation_in_dispute(dispute_id);
assert_eq!(result, Err(BrightDisputesError::InvalidEscrowAmount));

// Success, confirm judge
set_caller::<DefaultEnvironment>(judge);
set_value_transferred::<DefaultEnvironment>(10);
let result = bright_disputes.confirm_judge_participation_in_dispute(dispute_id);
assert_eq!(result, Ok(()));

// Success
// Confirm juries
for jure in &dispute.juries() {
set_caller::<DefaultEnvironment>(*jure);

// Fail to confirm jure, invalid escrow
set_value_transferred::<DefaultEnvironment>(5);
assert_eq!(
bright_disputes.confirm_jure_participation_in_dispute(dispute_id),
Err(BrightDisputesError::InvalidEscrowAmount)
);

// Success
set_value_transferred::<DefaultEnvironment>(10);
assert_eq!(
bright_disputes.confirm_jure_participation_in_dispute(dispute_id),
Ok(())
Expand Down
Loading
Loading