Skip to content

Commit

Permalink
Merge branch 'master' into mocello/2288_moonlight_wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Sep 10, 2024
2 parents 072b528 + 2622c30 commit 2b176ec
Show file tree
Hide file tree
Showing 58 changed files with 1,669 additions and 694 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/binary_copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Rusk binary copy

on:
push:
branches:
- master

jobs:
# Job to run change detection
changes:
runs-on: core
permissions:
pull-requests: read
outputs:
run-ci: ${{ steps.filter.outputs.run-ci }}
steps:
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
run-ci:
- 'rusk/**'
- 'node/**'
- '.github/workflows/binary_copy.yml'
build:
needs: changes
if: needs.changes.outputs.run-ci == 'true'
name: Make rusk
runs-on: core
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Rust toolchain
uses: dsherret/rust-toolchain-file@v1

- name: Setting up Node 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
scope: "@dusk-network"

- name: Build node
run: make all
copy_to_host:
needs: build
runs-on: core
continue-on-error: true
steps:
- name: "Check and Copy Rusk Artifact to Host"
run: |
# Ensure the target directory exists
mkdir -p /var/opt/rusk-artifacts
# Check if the rusk artifact exists before copying
if [ -f ./target/release/rusk ]; then
echo "Rusk artifact found. Copying to host."
cp ./target/release/rusk /var/opt/rusk-artifacts
else
echo "Rusk artifact not found. Skipping copy."
fi
2 changes: 1 addition & 1 deletion .github/workflows/rusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: dsherret/rust-toolchain-file@v1
- run: cargo fmt --all -- --check
- run: cargo fmt --all -- --check
6 changes: 4 additions & 2 deletions consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

use std::time::Duration;

use node_data::message::{MESSAGE_MAX_FAILED_ITERATIONS, MESSAGE_MAX_ITER};

/// Maximum number of iterations Consensus runs per a single round.
pub const CONSENSUS_MAX_ITER: u8 = 50;
pub const CONSENSUS_MAX_ITER: u8 = MESSAGE_MAX_ITER;

/// Total credits of steps committees
pub const PROPOSAL_COMMITTEE_CREDITS: usize = 1;
pub const VALIDATION_COMMITTEE_CREDITS: usize = 64;
pub const RATIFICATION_COMMITTEE_CREDITS: usize = 64;

pub const RELAX_ITERATION_THRESHOLD: u8 = 8;
pub const RELAX_ITERATION_THRESHOLD: u8 = MESSAGE_MAX_FAILED_ITERATIONS;
pub const MAX_NUMBER_OF_TRANSACTIONS: usize = 1_000;
pub const MAX_NUMBER_OF_FAULTS: usize = 100;

Expand Down
2 changes: 1 addition & 1 deletion contracts/stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ rusk-profile = { version = "0.6", path = "../../rusk-profile" }
once_cell = { version = "1.9" }
rusk-abi = { version = "0.13.0-rc", path = "../../rusk-abi", default-features = false, features = ["host"] }
execution-core = { version = "0.1.0", path = "../../execution-core", features = ["zk"] }
rusk-prover = { version = "0.4", path = "../../rusk-prover/" }
rusk-prover = { version = "0.5", path = "../../rusk-prover/" }
rkyv = { version = "0.7", default-features = false, features = ["size_32"] }
hex = "0.4"
rand = "0.8"
Expand Down
5 changes: 5 additions & 0 deletions contracts/stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ unsafe fn get_stake(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |pk| STATE.get_stake(&pk).cloned())
}

#[no_mangle]
unsafe fn get_stake_keys(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |pk| STATE.get_stake_keys(&pk).cloned())
}

#[no_mangle]
unsafe fn burnt_amount(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.burnt_amount())
Expand Down
86 changes: 51 additions & 35 deletions contracts/stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use execution_core::{
signatures::bls::PublicKey as BlsPublicKey,
stake::{
next_epoch, Reward, SlashEvent, Stake, StakeAmount, StakeData,
StakeEvent, Withdraw, EPOCH, MINIMUM_STAKE, STAKE_CONTRACT,
StakeEvent, StakeKeys, Withdraw, EPOCH, MINIMUM_STAKE, STAKE_CONTRACT,
STAKE_WARNINGS,
},
transfer::TRANSFER_CONTRACT,
Expand All @@ -33,7 +33,7 @@ use crate::*;
/// valid stake.
#[derive(Debug, Default, Clone)]
pub struct StakeState {
stakes: BTreeMap<[u8; BlsPublicKey::SIZE], (StakeData, BlsPublicKey)>,
stakes: BTreeMap<[u8; BlsPublicKey::SIZE], (StakeData, StakeKeys)>,
burnt_amount: u64,
previous_block_state:
BTreeMap<[u8; BlsPublicKey::SIZE], (Option<StakeData>, BlsPublicKey)>,
Expand Down Expand Up @@ -71,7 +71,8 @@ impl StakeState {
self.check_new_block();

let value = stake.value();
let account = *stake.account();
let keys = *stake.keys();
let account = keys.account;
let nonce = stake.nonce();
let signature = *stake.signature();

Expand All @@ -80,7 +81,11 @@ impl StakeState {
}

let prev_stake = self.get_stake(&account).copied();
let loaded_stake = self.load_or_create_stake_mut(&account);
let (loaded_stake, loaded_keys) = self.load_or_create_stake_mut(&keys);

// Update the funds key with the newly provided one
// This operation will rollback if the signature is invalid
*loaded_keys = keys;

// ensure the stake is at least the minimum and that there isn't an
// amount staked already
Expand All @@ -103,7 +108,9 @@ impl StakeState {
}

let digest = stake.signature_message().to_vec();
if !rusk_abi::verify_bls(digest, account, signature) {
let pk = keys.multisig_pk().expect("Invalid MultisigPublicKey");

if !rusk_abi::verify_bls_multisig(digest, pk, signature) {
panic!("Invalid signature!");
}

Expand All @@ -118,7 +125,7 @@ impl StakeState {
loaded_stake.amount =
Some(StakeAmount::new(value, rusk_abi::block_height()));

rusk_abi::emit("stake", StakeEvent { account, value });
rusk_abi::emit("stake", StakeEvent { keys, value });

let key = account.to_bytes();
self.previous_block_state
Expand All @@ -134,7 +141,7 @@ impl StakeState {
let value = transfer_withdraw.value();
let signature = *unstake.signature();

let loaded_stake = self
let (loaded_stake, keys) = self
.get_stake_mut(&account)
.expect("A stake should exist in the map to be unstaked!");
let prev_stake = Some(*loaded_stake);
Expand All @@ -153,7 +160,9 @@ impl StakeState {

// check signature is correct
let digest = unstake.signature_message().to_vec();
if !rusk_abi::verify_bls(digest, account, signature) {
let pk = keys.multisig_pk().expect("Invalid MultisigPublicKey");

if !rusk_abi::verify_bls_multisig(digest, pk, signature) {
panic!("Invalid signature!");
}

Expand All @@ -166,7 +175,7 @@ impl StakeState {
// update the state accordingly
loaded_stake.amount = None;

rusk_abi::emit("unstake", StakeEvent { account, value });
rusk_abi::emit("unstake", StakeEvent { keys: *keys, value });

let key = account.to_bytes();
self.previous_block_state
Expand All @@ -176,12 +185,12 @@ impl StakeState {

pub fn withdraw(&mut self, withdraw: Withdraw) {
let transfer_withdraw = withdraw.transfer_withdraw();
let account = *withdraw.account();
let account = withdraw.account();
let value = transfer_withdraw.value();
let signature = *withdraw.signature();

let loaded_stake = self
.get_stake_mut(&account)
let (loaded_stake, keys) = self
.get_stake_mut(account)
.expect("A stake should exist in the map to be unstaked!");

// ensure there is a non-zero reward, and that the withdrawal is exactly
Expand All @@ -196,7 +205,8 @@ impl StakeState {

// check signature is correct
let digest = withdraw.signature_message().to_vec();
if !rusk_abi::verify_bls(digest, account, signature) {
let pk = keys.multisig_pk().expect("Invalid MultisigPublicKey");
if !rusk_abi::verify_bls_multisig(digest, pk, signature) {
panic!("Invalid signature!");
}

Expand All @@ -208,58 +218,64 @@ impl StakeState {

// update the state accordingly
loaded_stake.reward -= value;
rusk_abi::emit("withdraw", StakeEvent { account, value });
rusk_abi::emit("withdraw", StakeEvent { keys: *keys, value });
}

/// Gets a reference to a stake.
pub fn get_stake(&self, key: &BlsPublicKey) -> Option<&StakeData> {
self.stakes.get(&key.to_bytes()).map(|(s, _)| s)
}

/// Gets the keys linked to to a stake.
pub fn get_stake_keys(&self, key: &BlsPublicKey) -> Option<&StakeKeys> {
self.stakes.get(&key.to_bytes()).map(|(_, k)| k)
}

/// Gets a mutable reference to a stake.
pub fn get_stake_mut(
&mut self,
key: &BlsPublicKey,
) -> Option<&mut StakeData> {
self.stakes.get_mut(&key.to_bytes()).map(|(s, _)| s)
) -> Option<&mut (StakeData, StakeKeys)> {
self.stakes.get_mut(&key.to_bytes())
}

/// Pushes the given `stake` onto the state for a given `account`.
pub fn insert_stake(&mut self, account: BlsPublicKey, stake: StakeData) {
self.stakes.insert(account.to_bytes(), (stake, account));
/// Pushes the given `stake` onto the state for a given `keys`.
pub fn insert_stake(&mut self, keys: StakeKeys, stake: StakeData) {
self.stakes.insert(keys.account.to_bytes(), (stake, keys));
}

/// Gets a mutable reference to the stake of a given key. If said stake
/// Gets a mutable reference to the stake of a given `keys`. If said stake
/// doesn't exist, a default one is inserted and a mutable reference
/// returned.
pub(crate) fn load_or_create_stake_mut(
&mut self,
account: &BlsPublicKey,
) -> &mut StakeData {
let is_missing = self.stakes.get(&account.to_bytes()).is_none();
keys: &StakeKeys,
) -> &mut (StakeData, StakeKeys) {
let key = keys.account.to_bytes();
let is_missing = self.stakes.get(&key).is_none();

if is_missing {
let stake = StakeData::EMPTY;
self.stakes.insert(account.to_bytes(), (stake, *account));
self.stakes.insert(key, (stake, *keys));
}

// SAFETY: unwrap is ok since we're sure we inserted an element
self.stakes
.get_mut(&account.to_bytes())
.map(|(s, _)| s)
.unwrap()
self.stakes.get_mut(&key).unwrap()
}

/// Rewards a `account` with the given `value`. If a stake does not exist
/// in the map for the key one will be created.
/// Rewards a `account` with the given `value`.
///
/// *PANIC* If a stake does not exist in the map
pub fn reward(&mut self, rewards: Vec<Reward>) {
// since we assure that reward is called at least once per block, this
// call is necessary to ensure that there are no inconsistencies in
// `prev_block_state`.
self.check_new_block();

for reward in &rewards {
let stake = self.load_or_create_stake_mut(&reward.account);
let (stake, _) = self
.get_stake_mut(&reward.account)
.expect("Stake to exists to be rewarded");

// Reset faults counters
stake.faults = 0;
Expand Down Expand Up @@ -289,7 +305,7 @@ impl StakeState {
pub fn slash(&mut self, account: &BlsPublicKey, to_slash: Option<u64>) {
self.check_new_block();

let stake = self
let (stake, _) = self
.get_stake_mut(account)
.expect("The stake to slash should exist");
let prev_stake = Some(*stake);
Expand Down Expand Up @@ -340,7 +356,7 @@ impl StakeState {
let key = account.to_bytes();
self.previous_block_state
.entry(key)
.or_insert((prev_stake, *account));
.or_insert_with(|| (prev_stake, *account));
}

/// Slash the given `to_slash` amount from an `account`'s stake.
Expand All @@ -355,7 +371,7 @@ impl StakeState {
) {
self.check_new_block();

let stake = self
let (stake, _) = self
.get_stake_mut(account)
.expect("The stake to slash should exist");

Expand Down Expand Up @@ -405,7 +421,7 @@ impl StakeState {
let key = account.to_bytes();
self.previous_block_state
.entry(key)
.or_insert((prev_stake, *account));
.or_insert_with(|| (prev_stake, *account));
}

/// Sets the burnt amount
Expand Down
5 changes: 4 additions & 1 deletion contracts/stake/tests/common/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub fn assert_event<S>(
.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());
assert_eq!(
staking_event_data.keys.account.to_bytes(),
should_pk.to_bytes()
);
}
}

Expand Down
3 changes: 2 additions & 1 deletion contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub fn create_transaction<const I: usize>(
let chain_id =
chain_id(session).expect("Getting the chain ID should succeed");

PhoenixTransaction::new::<StdRng, LocalProver>(
PhoenixTransaction::new(
rng,
sender_sk,
change_pk,
Expand All @@ -198,6 +198,7 @@ pub fn create_transaction<const I: usize>(
gas_price,
chain_id,
data.map(Into::into),
&LocalProver,
)
.expect("creating the creation shouldn't fail")
.into()
Expand Down
Loading

0 comments on commit 2b176ec

Please sign in to comment.