Skip to content

Commit

Permalink
Changing ending of the dispute
Browse files Browse the repository at this point in the history
- dispute ends when judge issues a verdict
- majority of votes starts from  70%
- changed indexing of dispute rounds
- deposit distribution is done when judge issues a verdict
  • Loading branch information
mgralinski-bright committed Jan 10, 2024
1 parent ef0f671 commit a117104
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 217 deletions.
4 changes: 3 additions & 1 deletion cli/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use liminal_ark_relations::{

pub mod helpers;

pub const MAJORITY_OF_VOTES: f32 = 0.70; // 70%

pub struct PublicVote {
pub id: AccountId,
pub pub_key: Vec<u8>,
Expand Down Expand Up @@ -102,7 +104,7 @@ pub fn prepare_counting_inputs(
}
}

let votes_minimum: u8 = (0.75 * votes.len() as f32).ceil() as u8;
let votes_minimum: u8 = (MAJORITY_OF_VOTES * votes.len() as f32).ceil() as u8;
let votes_maximum: u8 = votes.len() as u8 - votes_minimum;
let verdict = if sum_votes >= votes_minimum {
jurors_banned = jurors_banned
Expand Down
15 changes: 0 additions & 15 deletions cli/src/bright_disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,4 @@ impl BrightDisputes {

Ok(())
}

/// Calls 'distribute_deposit' of the contract.
pub async fn distribute_deposit(
&self,
connection: &SignedConnection,
dispute_id: u32,
) -> Result<()> {
let ink_contract: Instance = (&self.contract).into();

connection
.exec(ink_contract.distribute_deposit(dispute_id))
.await?;

Ok(())
}
}
15 changes: 2 additions & 13 deletions cli/src/bright_disputes_ink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use scale::Encode as _;

#[allow(dead_code)]
pub const CODE_HASH: [u8; 32] = [
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,
30, 161, 18, 63, 166, 82, 11, 106, 86, 19, 164, 51, 127, 97, 138, 215, 222, 180, 237, 3, 161,
251, 174, 200, 203, 108, 93, 249, 37, 173, 226, 99,
];

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

/// Judge can confirm his participation in dispute
#[allow(dead_code, clippy::too_many_arguments)]
pub fn distribute_deposit(&self, dispute_id: u32) -> ink_wrapper_types::ExecCall {
let data = {
let mut data = vec![117, 233, 246, 239];
dispute_id.encode_to(&mut data);
data
};
ink_wrapper_types::ExecCall::new(self.account_id, data)
}

/// Register a verification key.
#[allow(dead_code, clippy::too_many_arguments)]
pub fn register_vk(&self, relation: Relation, vk: Vec<u8>) -> ink_wrapper_types::ExecCall {
Expand Down
5 changes: 0 additions & 5 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,4 @@ pub enum ContractCmd {
caller_account: String,
dispute_id: u32,
},
/// Distribute dispute deposit
DistributeDeposit {
caller_account: String,
dispute_id: u32,
},
}
14 changes: 1 addition & 13 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
Command, Config,
ContractCmd::{
ConfirmDefendant, ConfirmJudgeParticipation, ConfirmJurorParticipation, CountTheVotes,
CreateDispute, DistributeDeposit, GetDispute, GetDisputeFull, ProcessDisputeRound,
CreateDispute, GetDispute, GetDisputeFull, ProcessDisputeRound,
RegisterAsAnActiveJuror, UnregisterAsAnActiveJuror, UpdateDefendantDescription,
UpdateOwnerDescription, Vote,
},
Expand Down Expand Up @@ -284,18 +284,6 @@ async fn handle_contract_command(
dispute_state
);
}
DistributeDeposit {
caller_account,
dispute_id,
} => {
let account = keypair_from_string(&caller_account);
let signed_connection = SignedConnection::from_connection(connection, account.clone());

bright_dispute
.distribute_deposit(&signed_connection, dispute_id)
.await?;
info!("Deposit distributed successfully!");
}
}
Ok(())
}
Expand Down
192 changes: 47 additions & 145 deletions contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,11 @@ pub mod bright_disputes {
match verdict {
Verdict::Positive => {
dispute.end_dispute(DisputeResult::Owner, jurors_banned)?;
self.distribute_deposit(&mut dispute)?;
}
Verdict::Negative => {
dispute.end_dispute(DisputeResult::Defendant, jurors_banned)?;
self.distribute_deposit(&mut dispute)?;
}
Verdict::None => {
// Check if juries votes
Expand All @@ -396,6 +398,7 @@ pub mod bright_disputes {
// Dispute round ended, but the majority of votes is not reached.
let timestamp = self.env().block_timestamp();
if let Err(_) = dispute.next_dispute_round(timestamp) {
self.distribute_deposit(&mut dispute)?;
return Err(BrightDisputesError::MajorityOfVotesNotReached);
}
}
Expand Down Expand Up @@ -445,49 +448,6 @@ pub mod bright_disputes {
Ok(())
}

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

// Check if dispute has ended.
dispute.assert_dispute_ended()?;

// Close dispute
dispute.close_dispute()?;
self.update_dispute(dispute.clone());

let mut accounts: Vec<AccountId> = Vec::new();

// Add owner
accounts.push(dispute.owner());

// Add defendant, only if he confirmed dispute
if dispute.has_defendant_confirmed_dispute() {
accounts.push(dispute.defendant());
}

// Add judge
if let Some(judge_id) = dispute.judge() {
accounts.push(judge_id);
self.add_to_juries_pool(judge_id)?;
}

// Add juries, who were not banned.
for juror_id in dispute.juries() {
accounts.push(juror_id);
self.add_to_juries_pool(juror_id)?;
}

// Split deposit and transfer founds.
let founds = dispute.deposit() / accounts.len() as Balance;
for account in accounts {
self.env().transfer(account, founds)?;
}

Ok(())
}

/// Register a verification key.
#[ink(message)]
pub fn register_vk(&mut self, relation: Relation, vk: Vec<u8>) -> Result<()> {
Expand Down Expand Up @@ -590,6 +550,50 @@ pub mod bright_disputes {
Ok(juries)
}

/// Finish the dispute and distribute the deposit.
fn distribute_deposit(&mut self, dispute: &mut Dispute) -> Result<()> {
// Check if dispute has ended.
dispute.assert_dispute_ended()?;

// Close dispute
dispute.close_dispute()?;
self.update_dispute(dispute.clone());

let mut accounts: Vec<AccountId> = Vec::new();

// Add judge
if let Some(judge_id) = dispute.judge() {
accounts.push(judge_id);
self.add_to_juries_pool(judge_id)?;
}

// Add juries, who were not banned.
for juror_id in dispute.juries() {
accounts.push(juror_id);
self.add_to_juries_pool(juror_id)?;
}

// If the dispute reaches the maximum number of rounds,
// and the majority of votes isn't reached, return the
// deposit to the Owner and Defendant as well.
if dispute.get_dispute_result().is_none() {
// Add owner
accounts.push(dispute.owner());

// Add defendant, only if he confirmed dispute
if dispute.has_defendant_confirmed_dispute() {
accounts.push(dispute.defendant());
}
}

// Split deposit and transfer founds.
let founds = dispute.deposit() / accounts.len() as Balance;
for account in accounts {
self.env().transfer(account, founds)?;
}
Ok(())
}

fn serialize<T: CanonicalSerialize + ?Sized>(t: &T) -> Vec<u8> {
let mut bytes = vec![0; t.serialized_size()];
t.serialize(&mut bytes[..]).expect("Failed to serialize");
Expand Down Expand Up @@ -1375,107 +1379,5 @@ pub mod bright_disputes {
let result = bright_disputes.process_dispute_round(dispute_id);
assert_eq!(result, Err(BrightDisputesError::JuriesPoolIsToSmall));
}

// Check deposit distribution
#[ink::test]
fn distribute_deposit() {
mock::register_chain_extensions(());

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

let mut bright_disputes = create_test_bright_dispute_with_running_dispute();
let dispute_id = 1;

// Register charlie, eve, frank and django as a juries.
register_valid_juries(&mut bright_disputes);

// Switch to "PickingJuriesAndJudge" state.
set_caller::<DefaultEnvironment>(accounts.alice);
bright_disputes
.process_dispute_round(dispute_id)
.expect("Failed to create a dispute!");

let dispute = bright_disputes
.get_dispute(dispute_id)
.expect("Failed to get dispute!");

// Assign all juries
let assigned_juries = dispute.juries();

let mut juror_not_assigned = vec![
accounts.charlie,
accounts.eve,
accounts.frank,
accounts.django,
];
juror_not_assigned.retain(|id| !assigned_juries.contains(id));

// Confirm juror participation
set_caller::<DefaultEnvironment>(assigned_juries[0]);
bright_disputes
.confirm_juror_participation_in_dispute(dispute_id, vec![])
.expect("Failed confirm juries participation!");

set_caller::<DefaultEnvironment>(assigned_juries[1]);
bright_disputes
.confirm_juror_participation_in_dispute(dispute_id, vec![])
.expect("Failed confirm juries participation!");

set_caller::<DefaultEnvironment>(assigned_juries[2]);
bright_disputes
.confirm_juror_participation_in_dispute(dispute_id, vec![])
.expect("Failed confirm juries participation!");

// Assign judge
set_caller::<DefaultEnvironment>(juror_not_assigned[0]);
bright_disputes
.confirm_judge_participation_in_dispute(dispute_id, vec![])
.expect("Failed to confirm judge participation!");

// Switch state to "Voting" state
set_caller::<DefaultEnvironment>(accounts.alice);
bright_disputes
.process_dispute_round(dispute_id)
.expect("Failed process dispute round!");

// Juries voting
set_caller::<DefaultEnvironment>(assigned_juries[0]);
bright_disputes
.vote(dispute_id, [0u64; 4], [0u64; 4], vec![])
.expect("Failed to vote");
set_caller::<DefaultEnvironment>(assigned_juries[1]);
bright_disputes
.vote(dispute_id, [0u64; 4], [0u64; 4], vec![])
.expect("Failed to vote");
set_caller::<DefaultEnvironment>(assigned_juries[2]);
bright_disputes
.vote(dispute_id, [0u64; 4], [0u64; 4], vec![])
.expect("Failed to vote");

// Switch state to CountingTheVotes
set_caller::<DefaultEnvironment>(accounts.alice);
bright_disputes
.process_dispute_round(dispute_id)
.expect("Failed process dispute round!");

// Count the votes, dispute ends
set_caller::<DefaultEnvironment>(juror_not_assigned[0]);
bright_disputes
.issue_the_verdict(
dispute_id,
0,
0,
Verdict::Positive,
[0u64; 4],
vec![],
vec![],
)
.expect("Failed process dispute round!");

set_caller::<DefaultEnvironment>(accounts.bob);
let result = bright_disputes.distribute_deposit(dispute_id);
assert_eq!(result, Ok(()));
}
}
}
10 changes: 5 additions & 5 deletions contract/src/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct Dispute {
}

impl Dispute {
const MAX_DISPUTE_ROUNDS: u8 = 3u8;
const MAX_DISPUTE_ROUNDS: u8 = 4u8;
const INCREMENT_JURIES_BY: u8 = 2;

/// Creates a new dispute
Expand All @@ -90,7 +90,7 @@ impl Dispute {
defendant_link: None,
dispute_result: None,
dispute_round: None,
dispute_round_counter: 0u8,
dispute_round_counter: 1u8,
judge: None,
juries: Vec::new(),
banned: Vec::new(),
Expand Down Expand Up @@ -492,7 +492,7 @@ mod tests {
assert_eq!(dispute.defendant, accounts.bob);
assert_eq!(dispute.defendant_link, None);
assert_eq!(dispute.dispute_result, None);
assert_eq!(dispute.dispute_round_counter, 0u8);
assert_eq!(dispute.dispute_round_counter, 1u8);
assert_eq!(dispute.judge, None);
assert_eq!(dispute.juries.len(), 0);
assert_eq!(dispute.banned.len(), 0);
Expand Down Expand Up @@ -816,7 +816,7 @@ mod tests {
fn next_dispute_round_limit() {
let mut dispute = default_test_running_dispute();

for _ in 0..Dispute::MAX_DISPUTE_ROUNDS {
for _ in 1..Dispute::MAX_DISPUTE_ROUNDS {
let result = dispute.next_dispute_round(0u64);
assert_eq!(result, Ok(()));
}
Expand Down Expand Up @@ -944,7 +944,7 @@ mod tests {
let result = dispute.assert_dispute_ended();
assert_eq!(result, Ok(()));

dispute.dispute_round_counter = 0u8;
dispute.dispute_round_counter = 1u8;
dispute.state = DisputeState::Ended;
let result = dispute.assert_dispute_ended();
assert_eq!(result, Ok(()));
Expand Down
6 changes: 2 additions & 4 deletions doc/README_CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ which moves us to the next phase which is `Counting the Votes`. Now the role of
```
../cli/target/release/bright_disputes_cli count-the-votes //Juror4 1 25,164,133,151,251,54,205,192,212,173,218,155,210,238,98,4,36,68,162,114,94,30,134,181,187,167,219,131,227,25,202,6
```
finally we can finish the dispute and distribute the deposit:
```
../cli/target/release/bright_disputes_cli distribute-deposit //Owner 1
```
Once the judgment is issued, the dispute will automatically end and all funds will be distributed accordingly.


Loading

0 comments on commit a117104

Please sign in to comment.