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

Feature/rust rewarding #750

Merged
merged 79 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
bfc43c8
Calculating gas fees
jstuczyn Aug 9, 2021
6231821
Ability to set custom fees
jstuczyn Aug 9, 2021
0c34c38
Added extra test
jstuczyn Aug 9, 2021
f9489d9
Removed commented code
jstuczyn Aug 9, 2021
abcec68
Moved all msg types to common contract crate
jstuczyn Aug 9, 2021
7dbaab3
Temporarily disabling get_tx method
jstuczyn Aug 10, 2021
f5cad78
Finishing up nymd client API
jstuczyn Aug 10, 2021
ae1d92d
Comment fix
jstuczyn Aug 10, 2021
895a1e3
Remaining fee values
jstuczyn Aug 10, 2021
d70683c
Some cleanup
jstuczyn Aug 10, 2021
849fde0
Removed needless borrow
jstuczyn Aug 10, 2021
1d25c42
Fixed imports in contract tests
jstuczyn Aug 10, 2021
5e8b3e3
Moved error types around
jstuczyn Aug 10, 2021
74a82bf
New ValidatorClient
jstuczyn Aug 11, 2021
ae1e4da
Experiment with new type of defaults
jstuczyn Aug 11, 2021
9ff3d1d
Removed dead module
jstuczyn Aug 11, 2021
9434901
Merge branch 'develop' into feature/nymd-client-integration
jstuczyn Aug 11, 2021
0cecc2b
Merge branch 'develop' into feature/nymd-client-integration
jstuczyn Aug 11, 2021
aa3be40
Dealt with unwrap
jstuczyn Aug 12, 2021
a530514
Migrated mixnode to use new validator client
jstuczyn Aug 12, 2021
ddba2e8
Migrated gateway to use new validator client
jstuczyn Aug 12, 2021
89800fc
Mixnode and gateway adjustments
jstuczyn Aug 12, 2021
b04c7d7
More exported defaults
jstuczyn Aug 12, 2021
fe830f9
Clients using new validator client
jstuczyn Aug 12, 2021
57f8ba4
Fixed mixnode upgrade
jstuczyn Aug 12, 2021
24e1283
Moved default values to a new crate
jstuczyn Aug 12, 2021
16d8abd
Changed behaviour of validator client features
jstuczyn Aug 12, 2021
e1a6f85
Migrated basic functions of validator api
jstuczyn Aug 12, 2021
f027ac8
Updated config + fixed startup
jstuczyn Aug 12, 2021
e5c7bd6
Fixed wasm client build
jstuczyn Aug 12, 2021
e0d0c91
Integration with the explorer api
jstuczyn Aug 13, 2021
a0ec3e8
Merge branch 'develop' into feature/nymd-client-integration
jstuczyn Aug 13, 2021
57d6b5d
Removed tokio dev dependency
jstuczyn Aug 13, 2021
42ad1de
Needless borrow
jstuczyn Aug 13, 2021
bbdede6
Merge branch 'develop' into feature/nymd-client-integration
jstuczyn Aug 13, 2021
e0119eb
Fixex wasm client build
jstuczyn Aug 13, 2021
1ef2469
Fixed tauri client build
jstuczyn Aug 16, 2021
ee38ff3
Needless borrows
jstuczyn Aug 16, 2021
f2e4754
New tables for rewarding
jstuczyn Aug 24, 2021
7f2179b
Updated cosmos-sdk version
jstuczyn Aug 24, 2021
b3858c5
Removed reward-specific node status routes
jstuczyn Aug 24, 2021
a8cf93a
New rewarding-specific config entries
jstuczyn Aug 24, 2021
aaf12ca
Additional network defaults
jstuczyn Aug 24, 2021
f225eed
Initial periodic rewards from validator api
jstuczyn Aug 25, 2021
12ab836
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Aug 25, 2021
fe715d9
Replaced print with log
jstuczyn Aug 25, 2021
175c41e
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Aug 25, 2021
657017a
Filtering nodes with uptime > 0
jstuczyn Aug 26, 2021
9b64642
Additional failure logging statements
jstuczyn Aug 26, 2021
13741da
Fixed operation ordering
jstuczyn Aug 26, 2021
1aea8be
Adjusted next rewarding epoch determination
jstuczyn Aug 26, 2021
b447beb
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Aug 26, 2021
7ebaa33
Modified rewarding behaviour to keep track of rewarding in progress
jstuczyn Aug 26, 2021
17d8e30
Improved error message on config load failure
jstuczyn Aug 26, 2021
87bfc63
Additional log statement
jstuczyn Aug 27, 2021
856d67d
Adjusted rewarding gas limit calculation
jstuczyn Aug 27, 2021
227e85c
Made naming slightly more consistent
jstuczyn Aug 27, 2021
5295867
Fixed incorrect parentheses placement
jstuczyn Aug 27, 2021
8056958
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Aug 27, 2021
b4e521e
Merge branch 'feature/rust-rewarding' of github.com:nymtech/nym into …
jstuczyn Aug 27, 2021
8201965
Fixed fee calculation
jstuczyn Aug 27, 2021
d946743
Cargo fmt
jstuczyn Aug 27, 2021
332578f
Removed failed merge artifacts
jstuczyn Aug 27, 2021
02de282
Introduced comment for any future reward modification
jstuczyn Aug 31, 2021
4640d2e
typos
jstuczyn Aug 31, 2021
30dfdaf
Helper functions for the future
jstuczyn Sep 1, 2021
9e5d889
Making @mfahampshire 's life easier
jstuczyn Sep 1, 2021
318dfce
Redesigned epoch + rewarding skipped epochs (if possible)
jstuczyn Sep 1, 2021
e459005
Removed old merge artifacts
jstuczyn Sep 1, 2021
e0e30ca
Naming consistency
jstuczyn Sep 1, 2021
bcdbbe6
Constraining arguments
jstuczyn Sep 1, 2021
fd76dbc
Removed unnecessary if branch
jstuczyn Sep 1, 2021
96155ca
Ignore monitor check for current epoch
jstuczyn Sep 1, 2021
b1696cf
Additional checks for current epoch data
jstuczyn Sep 2, 2021
118223c
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Sep 20, 2021
fb859db
Monitor threshold check
jstuczyn Sep 24, 2021
c9e98b0
Merge branch 'develop' into feature/rust-rewarding
jstuczyn Sep 24, 2021
1bda018
cargo fmt
jstuczyn Sep 24, 2021
462a83e
Fixed post-merge issues in transactions.rs
jstuczyn Sep 24, 2021
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
28 changes: 24 additions & 4 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions common/client-libs/validator-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ impl<C> Client<C> {
self.mixnet_contract_address = Some(mixnet_contract_address)
}

pub fn get_mixnet_contract_address(&self) -> Option<cosmrs::AccountId> {
self.mixnet_contract_address.clone()
}

pub async fn get_cached_mixnodes(&self) -> Result<Vec<MixNodeBond>, ValidatorClientError> {
Ok(self.validator_api.get_mixnodes().await?)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use cosmrs::rpc::{Error as TendermintRpcError, HttpClient, HttpClientUrl, Simple
use cosmrs::staking::{MsgDelegate, MsgUndelegate};
use cosmrs::tx::{Fee, Msg, MsgType, SignDoc, SignerInfo};
use cosmrs::{cosmwasm, rpc, tx, AccountId, Coin};
use log::debug;
use serde::Serialize;
use sha2::Digest;
use sha2::Sha256;
Expand Down Expand Up @@ -256,6 +257,48 @@ pub trait SigningCosmWasmClient: CosmWasmClient {
})
}

async fn execute_multiple<I, M>(
&self,
sender_address: &AccountId,
contract_address: &AccountId,
msgs: I,
fee: Fee,
memo: impl Into<String> + Send + 'static,
) -> Result<ExecuteResult, NymdError>
where
I: IntoIterator<Item = (M, Vec<Coin>)> + Send,
M: Serialize,
{
let messages = msgs
.into_iter()
.map(|(msg, funds)| {
cosmwasm::MsgExecuteContract {
sender: sender_address.clone(),
contract: contract_address.clone(),
msg: serde_json::to_vec(&msg)?,
funds,
}
.to_msg()
.map_err(|_| NymdError::SerializationError("MsgExecuteContract".to_owned()))
})
.collect::<Result<_, _>>()?;

let tx_res = self
.sign_and_broadcast_commit(sender_address, messages, fee, memo)
.await?
.check_response()?;

debug!(
"gas wanted: {:?}, gas used: {:?}",
tx_res.deliver_tx.gas_wanted, tx_res.deliver_tx.gas_used
);

Ok(ExecuteResult {
logs: parse_raw_logs(tx_res.deliver_tx.log)?,
transaction_hash: tx_res.hash,
})
}

async fn send_tokens(
&self,
sender_address: &AccountId,
Expand Down
17 changes: 7 additions & 10 deletions common/client-libs/validator-client/src/nymd/fee_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use crate::nymd::GasPrice;
use cosmrs::tx::{Fee, Gas};
use cosmrs::Coin;
use cosmwasm_std::Uint128;

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Operation {
Expand All @@ -28,13 +27,7 @@ pub enum Operation {
}

pub(crate) fn calculate_fee(gas_price: &GasPrice, gas_limit: Gas) -> Coin {
let limit_uint128 = Uint128::from(gas_limit.value());
let amount = gas_price.amount * limit_uint128;
assert!(amount.u128() <= u64::MAX as u128);
Coin {
denom: gas_price.denom.clone(),
amount: (amount.u128() as u64).into(),
}
gas_price * gas_limit
}

impl Operation {
Expand All @@ -61,16 +54,20 @@ impl Operation {
}
}

pub(crate) fn determine_fee(&self, gas_price: &GasPrice, gas_limit: Option<Gas>) -> Fee {
pub(crate) fn determine_custom_fee(gas_price: &GasPrice, gas_limit: Gas) -> Fee {
// we need to know 2 of the following 3 parameters (the third one is being implicit) in order to construct Fee:
// (source: https://docs.cosmos.network/v0.42/basics/gas-fees.html)
// - gas price
// - gas limit
// - fees
let gas_limit = gas_limit.unwrap_or_else(|| self.default_gas_limit());
let fee = calculate_fee(gas_price, gas_limit);
Fee::from_amount_and_gas(fee, gas_limit)
}

pub(crate) fn determine_fee(&self, gas_price: &GasPrice, gas_limit: Option<Gas>) -> Fee {
let gas_limit = gas_limit.unwrap_or_else(|| self.default_gas_limit());
Self::determine_custom_fee(gas_price, gas_limit)
}
}

#[cfg(test)]
Expand Down
47 changes: 45 additions & 2 deletions common/client-libs/validator-client/src/nymd/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

use crate::nymd::error::NymdError;
use config::defaults;
use cosmrs::Denom;
use cosmwasm_std::Decimal;
use cosmrs::tx::Gas;
use cosmrs::{Coin, Denom};
use cosmwasm_std::{Decimal, Fraction, Uint128};
use std::ops::Mul;
use std::str::FromStr;

/// A gas price, i.e. the price of a single unit of gas. This is typically a fraction of
Expand All @@ -18,6 +20,36 @@ pub struct GasPrice {
pub denom: Denom,
}

impl<'a> Mul<Gas> for &'a GasPrice {
type Output = Coin;

fn mul(self, gas_limit: Gas) -> Self::Output {
let limit_uint128 = Uint128::from(gas_limit.value());
let mut amount = self.amount * limit_uint128;

let gas_price_numerator = self.amount.numerator();
let gas_price_denominator = self.amount.denominator();

// gas price is a fraction of the smallest fee token unit, so we must ensure that
// for any multiplication, we have rounded up
//
// I don't really like the this solution as it has a theoretical chance of
// overflowing (internally cosmwasm uses U256 to avoid that)
// however, realistically that is impossible to happen as the resultant value
// would have to be way higher than our token limit of 10^15 (1 billion of tokens * 1 million for denomination)
// and max value of u128 is approximately 10^38
if limit_uint128.u128() * gas_price_numerator > amount.u128() * gas_price_denominator {
amount += Uint128::new(1);
}

assert!(amount.u128() <= u64::MAX as u128);
Coin {
denom: self.denom.clone(),
amount: (amount.u128() as u64).into(),
}
}
}

impl FromStr for GasPrice {
type Err = NymdError;

Expand Down Expand Up @@ -78,4 +110,15 @@ mod tests {
assert!("0.025 upunk".parse::<GasPrice>().is_err());
assert!("0.025UPUNK".parse::<GasPrice>().is_err());
}

#[test]
fn gas_limit_multiplication() {
// real world example that caused an issue when the result was rounded down
let gas_price: GasPrice = "0.025upunk".parse().unwrap();
let gas_limit: Gas = 157500u64.into();

let fee = &gas_price * gas_limit;
// the failing behaviour was result value of 3937
assert_eq!(fee.amount, 3938u64.into());
}
}
31 changes: 30 additions & 1 deletion common/client-libs/validator-client/src/nymd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::nymd::fee_helpers::Operation;
use crate::nymd::wallet::DirectSecp256k1HdWallet;
use cosmrs::rpc::endpoint::broadcast;
use cosmrs::rpc::{Error as TendermintRpcError, HttpClientUrl};
use cosmrs::tx::{Fee, Gas};

use cosmwasm_std::Coin;
use mixnet_contract::{
Expand All @@ -31,6 +30,8 @@ pub use crate::nymd::gas_price::GasPrice;
pub use cosmrs::rpc::HttpClient as QueryNymdClient;
pub use cosmrs::Coin as CosmosCoin;
pub use cosmrs::{AccountId, Denom};
pub use cosmrs::tendermint::Time as TendermintTime;
pub use cosmrs::tx::{Fee, Gas};
pub use signing_client::Client as SigningNymdClient;

pub mod cosmwasm_client;
Expand Down Expand Up @@ -150,6 +151,17 @@ impl<C> NymdClient<C> {
operation.determine_fee(&self.gas_price, gas_limit)
}

pub fn calculate_custom_fee(&self, gas_limit: impl Into<Gas>) -> Fee {
Operation::determine_custom_fee(&self.gas_price, gas_limit.into())
}

pub async fn get_current_block_timestamp(&self) -> Result<TendermintTime, NymdError>
where
C: CosmWasmClient + Sync,
{
Ok(self.client.get_block(None).await?.block.header.time)
}

pub async fn get_balance(&self, address: &AccountId) -> Result<Option<CosmosCoin>, NymdError>
where
C: CosmWasmClient + Sync,
Expand Down Expand Up @@ -391,6 +403,23 @@ impl<C> NymdClient<C> {
.await
}

pub async fn execute_multiple<I, M>(
&self,
contract_address: &AccountId,
msgs: I,
fee: Fee,
memo: impl Into<String> + Send + 'static,
) -> Result<ExecuteResult, NymdError>
where
C: SigningCosmWasmClient + Sync,
I: IntoIterator<Item = (M, Vec<CosmosCoin>)> + Send,
M: Serialize,
{
self.client
.execute_multiple(self.address(), contract_address, msgs, fee, memo)
.await
}

pub async fn upload(
&self,
wasm_code: Vec<u8>,
Expand Down
1 change: 1 addition & 0 deletions common/network-defaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2018"

[dependencies]
url = "2.2"
time = { version = "0.3", features = ["macros"] }
6 changes: 6 additions & 0 deletions common/network-defaults/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2020 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use std::time::Duration;
use time::OffsetDateTime;
use url::Url;

pub struct ValidatorDetails<'a> {
Expand Down Expand Up @@ -80,3 +82,7 @@ pub const DEFAULT_SOCKS5_LISTENING_PORT: u16 = 1080;
pub const DEFAULT_VALIDATOR_API_PORT: u16 = 8080;

pub const VALIDATOR_API_VERSION: &str = "v1";

// REWARDING
pub const DEFAULT_FIRST_EPOCH_START: OffsetDateTime = time::macros::datetime!(2021-08-23 12:00 UTC);
pub const DEFAULT_EPOCH_LENGTH: Duration = Duration::from_secs(24 * 60 * 60); // 24h
21 changes: 19 additions & 2 deletions contracts/mixnet/Cargo.lock

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

Loading