Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Sep 6, 2024
1 parent 30f4dc7 commit 37e1d46
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 60 deletions.
2 changes: 2 additions & 0 deletions contracts/stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ hex = "0.4"
rand = "0.8"
ff = { version = "0.13", default-features = false }
criterion = "0.5"
serde = "*"
serde_json = "*"

[[bench]]
name = "get_provisioners"
Expand Down
54 changes: 24 additions & 30 deletions contracts/stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl StakeState {
loaded_stake.amount =
Some(StakeAmount::new(value, rusk_abi::block_height()));

rusk_abi::emit("stake", StakeEvent { account, value });
let event = StakeEvent { account, value };
rusk_abi::emit_raw("stake", event.to_json());

let key = account.to_bytes();
self.previous_block_state
Expand Down Expand Up @@ -275,7 +276,8 @@ impl StakeState {
stake.reward += value;

let account = *account;
rusk_abi::emit("reward", StakeEvent { account, value });
let event = StakeEvent { account, value };
rusk_abi::emit_raw("reward", event.to_json());
}

/// Total amount burned since the genesis
Expand Down Expand Up @@ -322,13 +324,11 @@ impl StakeState {
stake_amount.eligibility =
next_epoch(rusk_abi::block_height()) + to_shift;

rusk_abi::emit(
"suspended",
StakeEvent {
account: *account,
value: stake_amount.eligibility,
},
);
let event = StakeEvent {
account: *account,
value: stake_amount.eligibility,
};
rusk_abi::emit_raw("suspended", event.to_json());
}

// Slash the provided amount or calculate the percentage according to
Expand All @@ -340,13 +340,11 @@ impl StakeState {
if to_slash > 0 {
stake_amount.lock_amount(to_slash);

rusk_abi::emit(
"slash",
StakeEvent {
account: *account,
value: to_slash,
},
);
let event = StakeEvent {
account: *account,
value: to_slash,
};
rusk_abi::emit_raw("slash", event.to_json());
}

let key = account.to_bytes();
Expand Down Expand Up @@ -390,13 +388,11 @@ impl StakeState {
stake_amount.eligibility =
next_epoch(rusk_abi::block_height()) + to_shift;

rusk_abi::emit(
"suspended",
StakeEvent {
account: *account,
value: stake_amount.eligibility,
},
);
let event = StakeEvent {
account: *account,
value: stake_amount.eligibility,
};
rusk_abi::emit_raw("suspended", event.to_json());

// Slash the provided amount or calculate the percentage according to
// hard faults
Expand All @@ -412,13 +408,11 @@ impl StakeState {
// Update the total burnt amount
self.burnt_amount += to_slash;

rusk_abi::emit(
"hard_slash",
StakeEvent {
account: *account,
value: to_slash,
},
);
let event = StakeEvent {
account: *account,
value: to_slash,
};
rusk_abi::emit_raw("hard_slash", event.to_json());
}

let key = account.to_bytes();
Expand Down
7 changes: 2 additions & 5 deletions contracts/stake/tests/common/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ pub fn assert_event<S>(
assert_eq!(staking_event_data.value, should_amount);
assert_eq!(staking_event_data.account.to_bytes(), should_pk.to_bytes());
} else {
let staking_event_data =
check_archived_root::<StakeEvent>(event.data.as_slice())
let staking_event_data: StakeEvent =
serde_json::from_slice(&event.data)
.expect("Stake event data should deserialize correctly");
let staking_event_data: StakeEvent = staking_event_data
.deserialize(&mut Infallible)
.expect("Infallible");
assert_eq!(staking_event_data.value, should_amount);
assert_eq!(staking_event_data.account.to_bytes(), should_pk.to_bytes());
}
Expand Down
4 changes: 4 additions & 0 deletions execution-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ bytecheck = { version = "0.6", default-features = false }
rand = { version = "0.8", default-features = false }
ff = { version = "0.13", default-features = false }

bs58 = { version="0.5", default-features = false, features = ["alloc"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version="1.0", default-features = false, features = ["alloc"] }

# zk-dependencies
dusk-plonk = { version = "0.20", default-features = false, features = ["rkyv-impl", "alloc"], optional = true }

Expand Down
30 changes: 5 additions & 25 deletions execution-core/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

//! Types used by Dusk's stake contract.
mod events;

use alloc::vec::Vec;

use bytecheck::CheckBytes;
Expand All @@ -17,12 +19,14 @@ use crate::{
PublicKey as BlsPublicKey, SecretKey as BlsSecretKey,
Signature as BlsSignature,
},
transfer::withdraw::{Withdraw as TransferWithdraw, WithdrawReceiver},
transfer::withdraw::Withdraw as TransferWithdraw,
ContractId,
};

use crate::{dusk, Dusk};

pub use events::*;

/// ID of the genesis stake contract
pub const STAKE_CONTRACT: ContractId = crate::reserved(0x2);

Expand Down Expand Up @@ -190,30 +194,6 @@ impl Withdraw {
}
}

/// Event emitted after a stake contract operation is performed.
#[derive(Debug, Clone, Archive, Deserialize, Serialize)]
#[archive_attr(derive(CheckBytes))]
pub struct StakeEvent {
/// Account associated to the event.
pub account: BlsPublicKey,
/// Value of the relevant operation, be it `stake`, `reward` or `slash`.
///
/// In case of `suspended` the amount refers to the next eligibility
pub value: u64,
}

/// Event emitted after a stake contract operation is performed.
#[derive(Debug, Clone, Archive, Deserialize, Serialize)]
#[archive_attr(derive(CheckBytes))]
pub struct StakeWithReceiverEvent {
/// Account associated to the event.
pub account: BlsPublicKey,
/// Value of the relevant operation, be it `unstake` or `withdraw`.
pub value: u64,
/// The receiver of the action
pub receiver: Option<WithdrawReceiver>,
}

/// The minimum amount of Dusk one can stake.
pub const MINIMUM_STAKE: Dusk = dusk(1_000.0);

Expand Down
1 change: 1 addition & 0 deletions rusk-abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub use piecrust_uplink::{
call_with_limit,
caller,
emit,
emit_raw,
feed,
limit,
self_id,
Expand Down

0 comments on commit 37e1d46

Please sign in to comment.