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

Stake integration #2408

Closed
wants to merge 12 commits into from
Closed
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
19 changes: 14 additions & 5 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,13 @@ pub const PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO: u64 = 125;
// TODO: modify the value directly in ConsensusConstants
pub const PSEUDO_CONSENSUS_CONSTANTS_WIP0027_COLLATERAL_AGE: u32 = 13440;

/// Maximum weight units that a block can devote to `StakeTransaction`s.
pub const PSEUDO_CONSENSUS_CONSTANTS_POS_MAX_STAKE_BLOCK_WEIGHT: u32 = 10_000_000;

/// Minimum amount of nanoWits that a `StakeTransaction` can add, and minimum amount that can be
/// left in stake by an `UnstakeTransaction`.
pub const PSEUDO_CONSENSUS_CONSTANTS_POS_MIN_STAKE_NANOWITS: u64 = 10_000_000_000_000;

/// Struct that will implement all the development defaults
pub struct Development;

Expand Down
4 changes: 4 additions & 0 deletions data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ rand_distr = "0.4.3"
[[bench]]
name = "sort_active_identities"
harness = false

[[bench]]
name = "staking"
harness = false
85 changes: 85 additions & 0 deletions data_structures/benches/staking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#[macro_use]
extern crate bencher;
use bencher::Bencher;
use rand::Rng;
use witnet_data_structures::staking::prelude::*;

fn populate(b: &mut Bencher) {
let mut stakes = Stakes::<String, u64, u64, u64>::default();
let mut i = 1;

b.iter(|| {
let address = format!("{i}");
let coins = i;
let epoch = i;
stakes.add_stake(address, coins, epoch).unwrap();

i += 1;
});
}

fn rank(b: &mut Bencher) {
let mut stakes = Stakes::<String, u64, u64, u64>::default();
let mut i = 1;

let stakers = 100_000;
let rf = 10;

let mut rng = rand::thread_rng();

loop {
let coins = i;
let epoch = i;
let address = format!("{}", rng.gen::<u64>());

stakes.add_stake(address, coins, epoch).unwrap();

i += 1;

if i == stakers {
break;
}
}

b.iter(|| {
let rank = stakes.rank(Capability::Mining, i);
let mut top = rank.take(usize::try_from(stakers / rf).unwrap());
let _first = top.next();
let _last = top.last();

i += 1;
})
}

fn query_power(b: &mut Bencher) {
let mut stakes = Stakes::<String, u64, u64, u64>::default();
let mut i = 1;

let stakers = 100_000;

loop {
let coins = i;
let epoch = i;
let address = format!("{i}");

stakes.add_stake(address, coins, epoch).unwrap();

i += 1;

if i == stakers {
break;
}
}

i = 1;

b.iter(|| {
let address = format!("{i}");
let _power = stakes.query_power(&address, Capability::Mining, i);

i += 1;
})
}

benchmark_main!(benches);
benchmark_group!(benches, populate, rank, query_power);
2 changes: 1 addition & 1 deletion data_structures/examples/transactions_pool_overhead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn random_transaction() -> (Transaction, u64) {
} else {
let dr_output = random_dr_output();
Transaction::DataRequest(DRTransaction {
body: DRTransactionBody::new(inputs, outputs, dr_output),
body: DRTransactionBody::new(inputs, dr_output, outputs),
signatures: vec![signature; num_inputs],
})
};
Expand Down
44 changes: 44 additions & 0 deletions data_structures/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum Capability {
/// The base block mining and superblock voting capability
Mining = 0,
/// The universal HTTP GET / HTTP POST / WIP-0019 RNG capability
Witnessing = 1,
}

#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct CapabilityMap<T>
where
T: Default,
{
pub mining: T,
pub witnessing: T,
}

impl<T> CapabilityMap<T>
where
T: Copy + Default,
{
#[inline]
pub fn get(&self, capability: Capability) -> T {
match capability {
Capability::Mining => self.mining,
Capability::Witnessing => self.witnessing,
}
}

#[inline]
pub fn update(&mut self, capability: Capability, value: T) {
match capability {
Capability::Mining => self.mining = value,
Capability::Witnessing => self.witnessing = value,
}
}

#[inline]
pub fn update_all(&mut self, value: T) {
self.mining = value;
self.witnessing = value;
}
}
Loading
Loading