Compare commits

..

9 Commits

Author SHA1 Message Date
Mark Sinclair ae0883241e Network Explorer: change prod env to round robin DNS 2021-12-17 13:07:16 +02:00
Mark Sinclair 11baa99c4b Network Explorer improvements:
- fix up API urls after Network Explorer API changes
- set currency denominations in `.env` file
- set API endpoints in `.env` file
2021-12-17 13:07:16 +02:00
Mark Sinclair 8bc23434ab Network Explorer API improvements:
- upgrade `okapi` for swagger generation across multiple resources
- switched `GET mix-node` to `GET mix-nodes`
- added error message when no geolocation env var is set and process continues
2021-12-17 13:07:16 +02:00
Mark Sinclair 8f6daf1e03 Network Explorer: configure URLs with .env file 2021-12-17 13:07:15 +02:00
Bogdan-Ștefan Neacșu 7f63377d22 Update contract addresses 2021-12-17 13:03:28 +02:00
Bogdan-Ștefan Neacșu 46cb3eca38 Do not set proxy only for this time 2021-12-17 13:02:01 +02:00
Bogdan-Ștefan Neacșu 7b22872c6b Short node identity signature check
Fix tests
2021-12-17 13:02:01 +02:00
Bogdan-Ștefan Neacșu 3342cb13c7 Update network defaults 2021-12-17 13:01:59 +02:00
Bogdan-Ștefan Neacșu 3cd5fc3b22 Make develop branch agnostic of the network 2021-12-17 12:48:29 +02:00
254 changed files with 28814 additions and 26445 deletions
-3
View File
@@ -1,3 +0,0 @@
unreleased=true
future-release=v0.12.0
since-tag=v0.11.0
-1
View File
@@ -1 +0,0 @@
2.7.5
+879 -228
View File
File diff suppressed because it is too large Load Diff
Generated
+18 -19
View File
@@ -240,14 +240,6 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d6dff4a1892b54d70af377bf7a17064192e822865791d812957f21e3108c325"
[[package]]
name = "bandwidth-claim-contract"
version = "0.1.0"
dependencies = [
"schemars",
"serde",
]
[[package]]
name = "base-x"
version = "0.2.8"
@@ -620,7 +612,7 @@ dependencies = [
[[package]]
name = "client-core"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"config",
"crypto",
@@ -1092,7 +1084,6 @@ dependencies = [
"blake3",
"bs58",
"cipher",
"config",
"digest 0.9.0",
"ed25519-dalek",
"generic-array 0.14.4",
@@ -1102,7 +1093,6 @@ dependencies = [
"nymsphinx-types",
"pemstore",
"rand 0.7.3",
"subtle-encoding",
"x25519-dalek",
]
@@ -1669,6 +1659,14 @@ dependencies = [
"termcolor",
]
[[package]]
name = "erc20-bridge-contract"
version = "0.1.0"
dependencies = [
"schemars",
"serde",
]
[[package]]
name = "error-chain"
version = "0.12.4"
@@ -3573,7 +3571,7 @@ dependencies = [
[[package]]
name = "nym-client"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"clap",
"client-core",
@@ -3607,7 +3605,7 @@ dependencies = [
[[package]]
name = "nym-client-wasm"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"coconut-interface",
"console_error_panic_hook",
@@ -3631,9 +3629,8 @@ dependencies = [
[[package]]
name = "nym-gateway"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"bandwidth-claim-contract",
"bip39",
"bs58",
"clap",
@@ -3645,6 +3642,7 @@ dependencies = [
"dashmap",
"dirs",
"dotenv",
"erc20-bridge-contract",
"futures",
"gateway-client",
"gateway-requests",
@@ -3674,7 +3672,7 @@ dependencies = [
[[package]]
name = "nym-mixnode"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"bs58",
"clap",
@@ -3696,6 +3694,7 @@ dependencies = [
"rocket",
"serde",
"serial_test",
"subtle-encoding",
"tokio",
"tokio-util",
"toml",
@@ -3708,7 +3707,7 @@ dependencies = [
[[package]]
name = "nym-network-requester"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"clap",
"dirs",
@@ -3729,7 +3728,7 @@ dependencies = [
[[package]]
name = "nym-socks5-client"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"clap",
"client-core",
@@ -3764,7 +3763,7 @@ dependencies = [
[[package]]
name = "nym-validator-api"
version = "0.12.0"
version = "0.11.0"
dependencies = [
"anyhow",
"clap",
+1 -1
View File
@@ -25,7 +25,7 @@ members = [
"common/config",
"common/credentials",
"common/crypto",
"common/bandwidth-claim-contract",
"common/erc20-bridge-contract",
"common/mixnet-contract",
"common/mixnode-common",
"common/network-defaults",
+5 -5
View File
@@ -40,13 +40,13 @@ Node, node operator and delegator rewards are determined according to the princi
|<img src="https://render.githubusercontent.com/render/math?math=R">|global share of rewards available, starts at 2% of the reward pool.
|<img src="https://render.githubusercontent.com/render/math?math=R_{i}">|node reward for mixnode `i`.
|<img src="https://render.githubusercontent.com/render/math?math=\sigma_{i}">|ratio of total node stake (node bond + all delegations) to the token circulating supply.
|<img src="https://render.githubusercontent.com/render/math?math=\lambda_{i}">|ratio of stake operator has pledged to their node to the token circulating supply.
|<img src="https://render.githubusercontent.com/render/math?math=\omega_{i}">|fraction of total effort undertaken by node `i`, set to `1/k`.
|<img src="https://render.githubusercontent.com/render/math?math=k">|number of nodes stakeholders are incentivised to create, set by the validators, a matter of governance. Currently determined by the `reward set` size, and set to 720 in testnet Sandbox.
|<img src="https://render.githubusercontent.com/render/math?math=\lambda_{i}">|ratio of stake operator has plaged to their node to the token circulating supply.
|<img src="https://render.githubusercontent.com/render/math?math=\omega_{i}">|fraction of total effort undertaken by node `i`, set to `1/k` in testnet Milhon.
|<img src="https://render.githubusercontent.com/render/math?math=k">|number of nodes stakeholders are incentivised to create, set by the validators, a matter of governance. Currently determined by the `active set` size, and set to 5000 in testnet Milhon.
|<img src="https://render.githubusercontent.com/render/math?math=\alpha">|Sybil attack resistance parameter - the higher this parameter is set the stronger the reduction in competitivness gets for a Sybil attacker.
|<img src="https://render.githubusercontent.com/render/math?math=PM_{i}">|declared profit margin of operator `i`, defaults to 10% in.
|<img src="https://render.githubusercontent.com/render/math?math=PM_{i}">|declared profit margin of operator `i`, defaults to 10% in testnet Milhon.
|<img src="https://render.githubusercontent.com/render/math?math=PF_{i}">|uptime of node `i`, scaled to 0 - 1, for the rewarding epoch
|<img src="https://render.githubusercontent.com/render/math?math=PP_{i}">|cost of operating node `i` for the duration of the rewarding eopoch, set to 40 NYMT.
|<img src="https://render.githubusercontent.com/render/math?math=PP_{i}">|cost of operating node `i` for the duration of the rewarding eopoch, set to 40 Nym for testnet Milhon.
Node reward for node `i` is determined as:
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "client-core"
version = "0.12.0"
version = "0.11.0"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
edition = "2018"
-14
View File
@@ -117,10 +117,6 @@ impl<T: NymConfig> Config<T> {
self.client.id = id;
}
pub fn with_testnet_mode(&mut self, testnet_mode: bool) {
self.client.testnet_mode = testnet_mode;
}
pub fn with_gateway_id<S: Into<String>>(&mut self, id: S) {
self.client.gateway_id = id.into();
}
@@ -157,10 +153,6 @@ impl<T: NymConfig> Config<T> {
self.client.id.clone()
}
pub fn get_testnet_mode(&self) -> bool {
self.client.testnet_mode
}
pub fn get_nym_root_directory(&self) -> PathBuf {
self.client.nym_root_directory.clone()
}
@@ -281,11 +273,6 @@ pub struct Client<T> {
/// ID specifies the human readable ID of this particular client.
id: String,
/// Indicates whether this client is running in a testnet mode, thus attempting
/// to claim bandwidth without presenting bandwidth credentials.
#[serde(default)]
testnet_mode: bool,
/// Addresses to APIs running on validator from which the client gets the view of the network.
validator_api_urls: Vec<Url>,
@@ -348,7 +335,6 @@ impl<T: NymConfig> Default for Client<T> {
Client {
version: env!("CARGO_PKG_VERSION").to_string(),
id: "".to_string(),
testnet_mode: false,
validator_api_urls: default_api_endpoints(),
private_identity_key_file: Default::default(),
public_identity_key_file: Default::default(),
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "nym-client"
version = "0.12.0"
version = "0.11.0"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>", "Jędrzej Stuczyński <andrew@nymtech.net>"]
edition = "2018"
rust-version = "1.56"
+1 -5
View File
@@ -5,7 +5,7 @@ pub(crate) fn config_template() -> &'static str {
// While using normal toml marshalling would have been way simpler with less overhead,
// I think it's useful to have comments attached to the saved config file to explain behaviour of
// particular fields.
// Note: any changes to the template must be reflected in the appropriate structs.
// Note: any changes to the template must be reflected in the appropriate structs in verloc.
r#"
# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
@@ -19,10 +19,6 @@ version = '{{ client.version }}'
# Human readable ID of this particular client.
id = '{{ client.id }}'
# Indicates whether this client is running in a testnet mode, thus attempting
# to claim bandwidth without presenting bandwidth credentials.
testnet_mode = {{ client.testnet_mode }}
# Addresses to APIs running on validator from which the client gets the view of the network.
validator_api_urls = [
{{#each client.validator_api_urls }}
-4
View File
@@ -207,10 +207,6 @@ impl NymClient {
Some(bandwidth_controller),
);
if self.config.get_base().get_testnet_mode() {
gateway_client.set_testnet_mode(true)
}
gateway_client
.authenticate_and_start()
.await
+1 -6
View File
@@ -30,7 +30,7 @@ use topology::{filter::VersionFilterable, gateway};
use url::Url;
use crate::client::config::Config;
use crate::commands::{override_config, TESTNET_MODE_ARG_NAME};
use crate::commands::override_config;
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
let app = App::new("init")
@@ -65,11 +65,6 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
.long("fastmode")
.hidden(true) // this will prevent this flag from being displayed in `--help`
.help("Mostly debug-related option to increase default traffic rate so that you would not need to modify config post init")
)
.arg(
Arg::with_name(TESTNET_MODE_ARG_NAME)
.long(TESTNET_MODE_ARG_NAME)
.help("Set this client to work in a testnet mode that would attempt to use gateway without bandwidth credential requirement")
);
#[cfg(not(feature = "coconut"))]
let app = app
-6
View File
@@ -5,8 +5,6 @@ use crate::client::config::{Config, SocketType};
use clap::ArgMatches;
use url::Url;
pub(crate) const TESTNET_MODE_ARG_NAME: &str = "testnet-mode";
pub(crate) mod init;
pub(crate) mod run;
pub(crate) mod upgrade;
@@ -54,9 +52,5 @@ pub(crate) fn override_config(mut config: Config, matches: &ArgMatches) -> Confi
config.get_base_mut().with_eth_private_key(eth_private_key);
}
if matches.is_present(TESTNET_MODE_ARG_NAME) {
config.get_base_mut().with_testnet_mode(true)
}
config
}
+1 -6
View File
@@ -3,7 +3,7 @@
use crate::client::config::Config;
use crate::client::NymClient;
use crate::commands::{override_config, TESTNET_MODE_ARG_NAME};
use crate::commands::override_config;
use clap::{App, Arg, ArgMatches};
use config::NymConfig;
use log::*;
@@ -38,11 +38,6 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
.long("port")
.help("Port for the socket (if applicable) to listen on")
.takes_value(true)
)
.arg(
Arg::with_name(TESTNET_MODE_ARG_NAME)
.long(TESTNET_MODE_ARG_NAME)
.help("Set this client to work in a testnet mode that would attempt to use gateway without bandwidth credential requirement")
);
#[cfg(not(feature = "coconut"))]
let app = app
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "nym-socks5-client"
version = "0.12.0"
version = "0.11.0"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>"]
edition = "2018"
rust-version = "1.56"
+1 -5
View File
@@ -5,7 +5,7 @@ pub(crate) fn config_template() -> &'static str {
// While using normal toml marshalling would have been way simpler with less overhead,
// I think it's useful to have comments attached to the saved config file to explain behaviour of
// particular fields.
// Note: any changes to the template must be reflected in the appropriate structs.
// Note: any changes to the template must be reflected in the appropriate structs in verloc.
r#"
# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
@@ -19,10 +19,6 @@ version = '{{ client.version }}'
# Human readable ID of this particular client.
id = '{{ client.id }}'
# Indicates whether this client is running in a testnet mode, thus attempting
# to claim bandwidth without presenting bandwidth credentials.
testnet_mode = {{ client.testnet_mode }}
# Addresses to APIs running on validator from which the client gets the view of the network.
validator_api_urls = [
{{#each client.validator_api_urls }}
-4
View File
@@ -195,10 +195,6 @@ impl NymClient {
Some(bandwidth_controller),
);
if self.config.get_base().get_testnet_mode() {
gateway_client.set_testnet_mode(true)
}
gateway_client
.authenticate_and_start()
.await
+1 -6
View File
@@ -28,7 +28,7 @@ use topology::{filter::VersionFilterable, gateway};
use url::Url;
use crate::client::config::Config;
use crate::commands::{override_config, TESTNET_MODE_ARG_NAME};
use crate::commands::override_config;
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
let app = App::new("init")
@@ -65,11 +65,6 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
.long("fastmode")
.hidden(true) // this will prevent this flag from being displayed in `--help`
.help("Mostly debug-related option to increase default traffic rate so that you would not need to modify config post init")
)
.arg(
Arg::with_name(TESTNET_MODE_ARG_NAME)
.long(TESTNET_MODE_ARG_NAME)
.help("Set this client to work in a testnet mode that would attempt to use gateway without bandwidth credential requirement")
);
#[cfg(not(feature = "coconut"))]
let app = app
-6
View File
@@ -9,8 +9,6 @@ pub(crate) mod init;
pub(crate) mod run;
pub(crate) mod upgrade;
pub(crate) const TESTNET_MODE_ARG_NAME: &str = "testnet-mode";
fn parse_validators(raw: &str) -> Vec<Url> {
raw.split(',')
.map(|raw_validator| {
@@ -50,9 +48,5 @@ pub(crate) fn override_config(mut config: Config, matches: &ArgMatches) -> Confi
config.get_base_mut().with_eth_private_key(eth_private_key);
}
if matches.is_present(TESTNET_MODE_ARG_NAME) {
config.get_base_mut().with_testnet_mode(true)
}
config
}
+1 -6
View File
@@ -3,7 +3,7 @@
use crate::client::config::Config;
use crate::client::NymClient;
use crate::commands::{override_config, TESTNET_MODE_ARG_NAME};
use crate::commands::override_config;
use clap::{App, Arg, ArgMatches};
use config::NymConfig;
use log::*;
@@ -44,11 +44,6 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
.long("port")
.help("Port for the socket to listen on")
.takes_value(true)
)
.arg(
Arg::with_name(TESTNET_MODE_ARG_NAME)
.long(TESTNET_MODE_ARG_NAME)
.help("Set this client to work in a testnet mode that would attempt to use gateway without bandwidth credential requirement")
);
#[cfg(not(feature = "coconut"))]
let app = app
+1 -1
View File
@@ -1,7 +1,7 @@
[package]
name = "nym-client-wasm"
authors = ["Dave Hrycyszyn <futurechimp@users.noreply.github.com>", "Jedrzej Stuczynski <andrew@nymtech.net>"]
version = "0.12.0"
version = "0.11.0"
edition = "2018"
keywords = ["nym", "sphinx", "wasm", "webassembly", "privacy", "client"]
license = "Apache-2.0"
-13
View File
@@ -27,7 +27,6 @@ const DEFAULT_GATEWAY_RESPONSE_TIMEOUT: Duration = Duration::from_millis(1_500);
#[wasm_bindgen]
pub struct NymClient {
validator_server: Url,
testnet_mode: bool,
// TODO: technically this doesn't need to be an Arc since wasm is run on a single thread
// however, once we eventually combine this code with the native-client's, it will make things
@@ -73,7 +72,6 @@ impl NymClient {
on_message: None,
on_gateway_connect: None,
testnet_mode: false,
}
}
@@ -86,11 +84,6 @@ impl NymClient {
self.on_gateway_connect = Some(on_connect)
}
pub fn set_testnet_mode(&mut self, testnet_mode: bool) {
console_log!("Setting testnet mode to {}", testnet_mode);
self.testnet_mode = testnet_mode;
}
fn self_recipient(&self) -> Recipient {
Recipient::new(
*self.identity.public_key(),
@@ -108,8 +101,6 @@ impl NymClient {
// Right now it's impossible to have async exported functions to take `&self` rather than self
pub async fn initial_setup(self) -> Self {
let testnet_mode = self.testnet_mode;
#[cfg(feature = "coconut")]
let bandwidth_controller = Some(BandwidthController::new(
vec![self.validator_server.clone()],
@@ -135,10 +126,6 @@ impl NymClient {
bandwidth_controller,
);
if testnet_mode {
gateway_client.set_testnet_mode(true)
}
gateway_client
.authenticate_and_start()
.await
@@ -40,7 +40,6 @@ const DEFAULT_RECONNECTION_BACKOFF: Duration = Duration::from_secs(5);
pub struct GatewayClient {
authenticated: bool,
testnet_mode: bool,
bandwidth_remaining: i64,
gateway_address: String,
gateway_identity: identity::PublicKey,
@@ -76,7 +75,6 @@ impl GatewayClient {
) -> Self {
GatewayClient {
authenticated: false,
testnet_mode: false,
bandwidth_remaining: 0,
gateway_address,
gateway_identity,
@@ -92,10 +90,6 @@ impl GatewayClient {
}
}
pub fn set_testnet_mode(&mut self, testnet_mode: bool) {
self.testnet_mode = testnet_mode
}
// TODO: later convert into proper builder methods
pub fn with_reconnection_on_failure(&mut self, should_reconnect_on_failure: bool) {
self.should_reconnect_on_failure = should_reconnect_on_failure
@@ -125,7 +119,6 @@ impl GatewayClient {
GatewayClient {
authenticated: false,
testnet_mode: false,
bandwidth_remaining: 0,
gateway_address,
gateway_identity,
@@ -520,17 +513,6 @@ impl GatewayClient {
Ok(())
}
async fn try_claim_testnet_bandwidth(&mut self) -> Result<(), GatewayClientError> {
let msg = ClientControlRequest::ClaimFreeTestnetBandwidth.into();
self.bandwidth_remaining = match self.send_websocket_message(msg).await? {
ServerResponse::Bandwidth { available_total } => Ok(available_total),
ServerResponse::Error { message } => Err(GatewayClientError::GatewayError(message)),
_ => Err(GatewayClientError::UnexpectedResponse),
}?;
Ok(())
}
pub async fn claim_bandwidth(&mut self) -> Result<(), GatewayClientError> {
if !self.authenticated {
return Err(GatewayClientError::NotAuthenticated);
@@ -543,10 +525,6 @@ impl GatewayClient {
}
warn!("Not enough bandwidth. Trying to get more bandwidth, this might take a while");
if self.testnet_mode {
info!("The client is running in testnet mode - attempting to claim bandwidth without a credential");
return self.try_claim_testnet_bandwidth().await;
}
#[cfg(feature = "coconut")]
let credential = self
@@ -202,28 +202,14 @@ impl DirectSecp256k1HdWalletBuilder {
#[cfg(test)]
mod tests {
use super::*;
use network_defaults::BECH32_PREFIX;
#[test]
fn generating_account_addresses() {
let (addr1, addr2, addr3) = match BECH32_PREFIX {
"punk" => (
"punk1jw6mp7d5xqc7w6xm79lha27glmd0vdt32a3fj2",
"punk1h5hgn94nsq4kh99rjj794hr5h5q6yfm22mcqqn",
"punk17n9flp6jflljg6fp05dsy07wcprf2uuujse962",
),
"nymt" => (
"nymt1jw6mp7d5xqc7w6xm79lha27glmd0vdt339me94",
"nymt1h5hgn94nsq4kh99rjj794hr5h5q6yfm23rjshv",
"nymt17n9flp6jflljg6fp05dsy07wcprf2uuufgn4d4",
),
_ => panic!("Test needs to be updated with new bech32 prefix"),
};
// test vectors produced from our js wallet
let mnemonic_address = vec![
("crush minute paddle tobacco message debate cabin peace bar jacket execute twenty winner view sure mask popular couch penalty fragile demise fresh pizza stove", addr1),
("acquire rebel spot skin gun such erupt pull swear must define ill chief turtle today flower chunk truth battle claw rigid detail gym feel", addr2),
("step income throw wheat mobile ship wave drink pool sudden upset jaguar bar globe rifle spice frost bless glimpse size regular carry aspect ball", addr3)
("crush minute paddle tobacco message debate cabin peace bar jacket execute twenty winner view sure mask popular couch penalty fragile demise fresh pizza stove", "nymt1jw6mp7d5xqc7w6xm79lha27glmd0vdt339me94"),
("acquire rebel spot skin gun such erupt pull swear must define ill chief turtle today flower chunk truth battle claw rigid detail gym feel", "nymt1h5hgn94nsq4kh99rjj794hr5h5q6yfm23rjshv"),
("step income throw wheat mobile ship wave drink pool sudden upset jaguar bar globe rifle spice frost bless glimpse size regular carry aspect ball", "nymt17n9flp6jflljg6fp05dsy07wcprf2uuufgn4d4")
];
for (mnemonic, address) in mnemonic_address.into_iter() {
-2
View File
@@ -19,9 +19,7 @@ x25519-dalek = "1.1"
ed25519-dalek = "1.0"
log = "0.4"
rand = { version = "0.7.3", features = ["wasm-bindgen"] }
subtle-encoding = { version = "0.5", features = ["bech32-preview"]}
# internal
nymsphinx-types = { path = "../nymsphinx/types" }
pemstore = { path = "../../common/pemstore" }
config = { path="../../common/config" }
@@ -189,13 +189,6 @@ impl PrivateKey {
let sig = expanded_secret_key.sign(message, &public_key.0);
Signature(sig)
}
/// Signs text with the provided Ed25519 private key, returning a base58 signature
pub fn sign_text(&self, text: &str) -> String {
let signature_bytes = self.sign(text.as_ref()).to_bytes();
let signature = bs58::encode(signature_bytes).into_string();
signature
}
}
impl PemStorableKey for PrivateKey {
@@ -1,87 +0,0 @@
use config::defaults;
use subtle_encoding::bech32;
#[derive(Debug, Clone, PartialEq)]
pub enum Bech32Error {
DecodeFailed(String),
WrongPrefix(String),
}
/// Try to decode the address (to make sure it's a valid bech32 encoding)
pub fn try_bech32_decode(address: &str) -> Result<String, Bech32Error> {
match bech32::decode(address) {
Err(e) => Err(Bech32Error::DecodeFailed(e.to_string())),
Ok((prefix, _)) => Ok(prefix),
}
}
pub fn validate_bech32_prefix(address: &str) -> Result<(), Bech32Error> {
let prefix = try_bech32_decode(address)?;
if prefix == defaults::BECH32_PREFIX {
Ok(())
} else {
Err(Bech32Error::WrongPrefix(format!(
"your bech32 address prefix should be {}, not {}",
defaults::BECH32_PREFIX,
prefix
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
mod decoding_bech32_addresses {
use super::*;
#[test]
fn total_crap_fails() {
let res = try_bech32_decode("crap");
assert_eq!(
Err(Bech32Error::DecodeFailed("bad encoding".to_string())),
res
);
}
#[test]
fn bad_checksum_fails() {
let chopped_address = "punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu"; // this has the final "0" chopped off
let res = try_bech32_decode(chopped_address);
assert_eq!(
Err(Bech32Error::DecodeFailed("checksum mismatch".to_string())),
res
);
}
#[test]
fn good_address_returns_prefix() {
let prefix = try_bech32_decode("punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu0");
assert_eq!(Ok("punk".to_string()), prefix);
}
}
#[cfg(test)]
mod ensuring_correct_bech32_prefix {
use super::*;
#[test]
fn wrong_prefix_fails() {
assert_eq!(
Err(Bech32Error::WrongPrefix(
"your bech32 address prefix should be nymt, not punk".to_string()
)),
validate_bech32_prefix("punk1h3w4nj7kny5dfyjw2le4vm74z03v9vd4dstpu0")
)
}
#[test]
fn correct_prefix_works() {
assert_eq!(
Ok(()),
validate_bech32_prefix("nymt1z9egw0knv47nmur0p8vk4rcx59h9gg4zuxrrr9")
)
}
}
}
-1
View File
@@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
pub mod asymmetric;
pub mod bech32_address_validation;
pub mod crypto_hash;
pub mod hkdf;
pub mod hmac;
@@ -1,5 +1,5 @@
[package]
name = "bandwidth-claim-contract"
name = "erc20-bridge-contract"
version = "0.1.0"
edition = "2018"
+2 -2
View File
@@ -3,8 +3,8 @@
fn main() {
match option_env!("NETWORK") {
Some("milhon") => println!("cargo:rustc-cfg=network=\"milhon\"",),
None | Some("sandbox") => println!("cargo:rustc-cfg=network=\"sandbox\"",),
None | Some("milhon") => println!("cargo:rustc-cfg=network=\"milhon\"",),
Some("sandbox") => println!("cargo:rustc-cfg=network=\"sandbox\"",),
Some("qa") => println!("cargo:rustc-cfg=network=\"qa\""),
_ => panic!("No such network"),
}
+1 -1
View File
@@ -6,7 +6,7 @@ pub const DENOM: &str = "upunk";
pub const DEFAULT_MIXNET_CONTRACT_ADDRESS: &str = "punk10pyejy66429refv3g35g2t7am0was7yalwrzen";
pub const DEFAULT_VESTING_CONTRACT_ADDRESS: &str = "";
pub const BANDWIDTH_CLAIM_CONTRACT_ADDRESS: &str = "punk1jld76tqw4wnpfenmay2xkv86nr3j0w426eka82";
pub const COSMOS_CONTRACT_ADDRESS: &str = "punk1jld76tqw4wnpfenmay2xkv86nr3j0w426eka82";
pub const REWARDING_VALIDATOR_ADDRESS: &str = "punk1v9qauwdq5terag6uvfsdytcs2d0sdmfdy7hgk3";
pub const ETH_CONTRACT_ADDRESS: [u8; 20] =
hex_literal::hex!("9fEE3e28c17dbB87310A51F13C4fbf4331A6f102");
+2 -2
View File
@@ -4,9 +4,9 @@
pub const BECH32_PREFIX: &str = "nymt";
pub const DENOM: &str = "unymt";
pub const DEFAULT_MIXNET_CONTRACT_ADDRESS: &str = "nymt1ghd753shjuwexxywmgs4xz7x2q732vcnstz02j";
pub const DEFAULT_MIXNET_CONTRACT_ADDRESS: &str = "nymt14hj2tavq8fpesdwxxcu44rty3hh90vhuysqrsr";
pub const DEFAULT_VESTING_CONTRACT_ADDRESS: &str = "nymt1nc5tatafv6eyq7llkr2gv50ff9e22mnfp9pc5s";
pub const BANDWIDTH_CLAIM_CONTRACT_ADDRESS: &str = "nymt17p9rzwnnfxcjp32un9ug7yhhzgtkhvl9f8xzkv";
pub const COSMOS_CONTRACT_ADDRESS: &str = "nymt17p9rzwnnfxcjp32un9ug7yhhzgtkhvl9f8xzkv";
pub const REWARDING_VALIDATOR_ADDRESS: &str = "nymt17zujduc46wvkwvp6f062mm5xhr7jc3fewvqu9e";
pub const ETH_CONTRACT_ADDRESS: [u8; 20] =
hex_literal::hex!("9fEE3e28c17dbB87310A51F13C4fbf4331A6f102");
+21 -32
View File
@@ -45,27 +45,6 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d6dff4a1892b54d70af377bf7a17064192e822865791d812957f21e3108c325"
[[package]]
name = "bandwidth-claim"
version = "0.1.0"
dependencies = [
"bandwidth-claim-contract",
"config",
"cosmwasm-std",
"cosmwasm-storage",
"schemars",
"serde",
"thiserror",
]
[[package]]
name = "bandwidth-claim-contract"
version = "0.1.0"
dependencies = [
"schemars",
"serde",
]
[[package]]
name = "base64"
version = "0.13.0"
@@ -236,6 +215,19 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "cosmos_contract"
version = "0.1.0"
dependencies = [
"config",
"cosmwasm-std",
"cosmwasm-storage",
"erc20-bridge-contract",
"schemars",
"serde",
"thiserror",
]
[[package]]
name = "cosmwasm-crypto"
version = "1.0.0-beta2"
@@ -317,7 +309,6 @@ dependencies = [
"blake3",
"bs58",
"cipher",
"config",
"digest 0.9.0",
"ed25519-dalek",
"generic-array 0.14.4",
@@ -327,7 +318,6 @@ dependencies = [
"nymsphinx-types",
"pemstore",
"rand",
"subtle-encoding",
"x25519-dalek",
]
@@ -514,6 +504,14 @@ dependencies = [
"syn",
]
[[package]]
name = "erc20-bridge-contract"
version = "0.1.0"
dependencies = [
"schemars",
"serde",
]
[[package]]
name = "fake-simd"
version = "0.1.2"
@@ -1330,15 +1328,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "subtle-encoding"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dcb1ed7b8330c5eed5441052651dd7a12c75e2ed88f2ec024ae1fa3a5e59945"
dependencies = [
"zeroize",
]
[[package]]
name = "syn"
version = "1.0.81"
+1 -1
View File
@@ -1,5 +1,5 @@
[workspace]
members = ["bandwidth-claim", "mixnet", "vesting"]
members = ["erc20-bridge", "mixnet", "vesting"]
[profile.release]
opt-level = 3
@@ -1,6 +0,0 @@
node_modules
../.env
#Hardhat files
cache
artifacts
@@ -1,6 +0,0 @@
module.exports = {
skipFiles: [
'CosmosToken.sol',
'Gravity.sol'
]
};
@@ -1,17 +0,0 @@
# Basic Bandwidth Credential Generator
This directory contains the contract and unit tests for the `BandwidthGenerator` smart contract.
This contract allows users to generate Basic Bandwidth Credentials (BBCs) on the Nym cosmos blockchain using ERC20 representations of NYM as payment, utilising the Cosmos Gravity Bridge for cross-chain payment.
BBCs are credentials that will be presented to Gateways by a Nym Client, and represent a certain amount of bandwidth which can be sent through the Nym Mixnet.
By default 1 NYM = 1 GB of bandwidth. The ratio of NYM - bandwidth is denominated in bytes, and represented in the smart contract by the `BytesPerToken` variable. This variable can be adjusted by the contract owner.
The amount of bandwidth bought is calculated according to the following formula:
`(Token amount in 'wei' * BytesPerToken) / 10**18`
## Usage
* `npm install`
* `npx hardhat compile`
* `npx hardhat test`
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/6dfbc7f87ae3c2727d63a3fba8d02a41.json"
}
File diff suppressed because one or more lines are too long
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/6dfbc7f87ae3c2727d63a3fba8d02a41.json"
}
@@ -1,194 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "IERC20",
"sourceName": "@openzeppelin/contracts/token/ERC20/IERC20.sol",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../../build-info/6dfbc7f87ae3c2727d63a3fba8d02a41.json"
}
@@ -1,233 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "IERC20Metadata",
"sourceName": "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/6dfbc7f87ae3c2727d63a3fba8d02a41.json"
}
@@ -1,10 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "Context",
"sourceName": "@openzeppelin/contracts/utils/Context.sol",
"abi": [],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,10 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "SafeMath",
"sourceName": "@openzeppelin/contracts/math/SafeMath.sol",
"abi": [],
"bytecode": "0x60566023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212207ceb5b9e8bb631a9723802d1487d75b0f9db1e57e569e37561e9d35cd45a718c64736f6c63430006060033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212207ceb5b9e8bb631a9723802d1487d75b0f9db1e57e569e37561e9d35cd45a718c64736f6c63430006060033",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,194 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "IERC20",
"sourceName": "@openzeppelin/contracts/token/ERC20/IERC20.sol",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,10 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "SafeERC20",
"sourceName": "@openzeppelin/contracts/token/ERC20/SafeERC20.sol",
"abi": [],
"bytecode": "0x60566023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220a10ad72c41cead301751dbbfdbfa8dc0de387237baaadf00c593410d921ba53164736f6c63430006060033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220a10ad72c41cead301751dbbfdbfa8dc0de387237baaadf00c593410d921ba53164736f6c63430006060033",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,10 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "Address",
"sourceName": "@openzeppelin/contracts/utils/Address.sol",
"abi": [],
"bytecode": "0x60566023600b82828239805160001a607314601657fe5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220bf51151e2a4e160194baa49fc39a27bad1253743e398fefe6fb6935ec7d37a5e64736f6c63430006060033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220bf51151e2a4e160194baa49fc39a27bad1253743e398fefe6fb6935ec7d37a5e64736f6c63430006060033",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,10 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "Context",
"sourceName": "@openzeppelin/contracts/utils/Context.sol",
"abi": [],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
@@ -1,16 +0,0 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "ReentrancyGuard",
"sourceName": "@openzeppelin/contracts/utils/ReentrancyGuard.sol",
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
File diff suppressed because one or more lines are too long
@@ -1,4 +0,0 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/10e5405221d21329b74f12bf00d0cd2d.json"
}
File diff suppressed because one or more lines are too long
@@ -1,403 +0,0 @@
{
"_format": "hh-sol-cache-2",
"files": {
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/contracts/BandwidthGenerator.sol": {
"lastModificationDate": 1639657373736,
"contentHash": "ac31a05f19ad88d4c28011a830d29b56",
"sourceName": "contracts/BandwidthGenerator.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"./CosmosToken.sol",
"./Gravity.sol",
"@openzeppelin/contracts/access/Ownable.sol",
"@openzeppelin/contracts/math/SafeMath.sol"
],
"versionPragmas": [
"0.6.6"
],
"artifacts": [
"BandwidthGenerator"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/contracts/CosmosToken.sol": {
"lastModificationDate": 1639657191231,
"contentHash": "0f05f96ee3c1151b6cd4b699aa3167e9",
"sourceName": "contracts/CosmosToken.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"@openzeppelin/contracts/token/ERC20/ERC20.sol"
],
"versionPragmas": [
"^0.6.6"
],
"artifacts": [
"CosmosERC20"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/contracts/Gravity.sol": {
"lastModificationDate": 1639657191231,
"contentHash": "eaa1cd71cea24d419ef5f67a66dc672e",
"sourceName": "contracts/Gravity.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"@openzeppelin/contracts/math/SafeMath.sol",
"@openzeppelin/contracts/token/ERC20/IERC20.sol",
"@openzeppelin/contracts/token/ERC20/SafeERC20.sol",
"@openzeppelin/contracts/utils/Address.sol",
"@openzeppelin/contracts/utils/ReentrancyGuard.sol",
"./CosmosToken.sol"
],
"versionPragmas": [
"^0.6.6"
],
"artifacts": [
"Gravity"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/access/Ownable.sol": {
"lastModificationDate": 1639657303711,
"contentHash": "6748815a5b45c4aeeda56819f41190e0",
"sourceName": "@openzeppelin/contracts/access/Ownable.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"../utils/Context.sol"
],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"Ownable"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/math/SafeMath.sol": {
"lastModificationDate": 1639657303763,
"contentHash": "e03e12206057e809eb76c5f681170c32",
"sourceName": "@openzeppelin/contracts/math/SafeMath.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"SafeMath"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol": {
"lastModificationDate": 1639657303507,
"contentHash": "8065b340476f61365c076897199425f1",
"sourceName": "@openzeppelin/contracts/token/ERC20/ERC20.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"../../utils/Context.sol",
"./IERC20.sol",
"../../math/SafeMath.sol"
],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"ERC20"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/utils/Context.sol": {
"lastModificationDate": 1639657303395,
"contentHash": "2adbd82f6d055a4751566d4671512b03",
"sourceName": "@openzeppelin/contracts/utils/Context.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"Context"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol": {
"lastModificationDate": 1639657303639,
"contentHash": "e0a41531d159d3a32f84b7a3ecf9fabb",
"sourceName": "@openzeppelin/contracts/token/ERC20/IERC20.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"IERC20"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/token/ERC20/SafeERC20.sol": {
"lastModificationDate": 1639657303759,
"contentHash": "33e22842646d746e5c4124c2fdc051aa",
"sourceName": "@openzeppelin/contracts/token/ERC20/SafeERC20.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [
"./IERC20.sol",
"../../math/SafeMath.sol",
"../../utils/Address.sol"
],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"SafeERC20"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/utils/Address.sol": {
"lastModificationDate": 1639657303363,
"contentHash": "7aa46886ff5abe7515496208a5e2ce5a",
"sourceName": "@openzeppelin/contracts/utils/Address.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.6.2 <0.8.0"
],
"artifacts": [
"Address"
]
},
"/home/max/dev/nymtech/nym/contracts/basic-bandwidth-generation/node_modules/@openzeppelin/contracts/utils/ReentrancyGuard.sol": {
"lastModificationDate": 1639657303747,
"contentHash": "1c60f58cee45c61469e1aea31e4dd879",
"sourceName": "@openzeppelin/contracts/utils/ReentrancyGuard.sol",
"solcConfig": {
"version": "0.6.6",
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers"
],
"": [
"ast"
]
}
}
}
},
"imports": [],
"versionPragmas": [
">=0.6.0 <0.8.0"
],
"artifacts": [
"ReentrancyGuard"
]
}
}
}
@@ -1,93 +0,0 @@
pragma solidity 0.6.6;
import "./CosmosToken.sol";
import "./Gravity.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
/**
* @title BandwidthGenerator
* @dev Contract for generating Basic Bandwidth Credentials (BBCs) on the Nym cosmos blockchain,
* using ERC20 representations of NYM as payment. Utilises the Gravity Bridge for cross-chain payment.
*
* Credentials represent a certain amount of bandwidth which can be sent through the Nym Mixnet.
* By default 1 NYM = 1 GB of bandwidth. The `BytesPerToken` amount can be adjusted by the contract owner.
*
* The amount of bandwidth bought is calculated according to the following formula:
* `(Token amount in 'wei' / 10**18) * BytesPerToken`
*/
contract BandwidthGenerator is Ownable {
using SafeMath for uint256;
CosmosERC20 public erc20;
Gravity public gravityBridge;
uint256 public BytesPerToken;
event BBCredentialPurchased(
uint256 Bandwidth,
uint256 indexed VerificationKey,
bytes SignedVerificationKey,
bytes32 indexed CosmosRecipient
);
event RatioChanged(
uint256 indexed NewBytesPerToken
);
/**
* @param _erc20 Address of the erc20NYM deployed through the Gravity Bridge.
* @param _gravityBridge Address of the deployed Gravity Bridge.
*/
constructor(CosmosERC20 _erc20, Gravity _gravityBridge) public {
require(address(_erc20) != address(0), "BandwidthGenerator: erc20 address cannot be null");
require(address(_gravityBridge) != address(0), "BandwidthGenerator: gravity bridge address cannot be null");
erc20 = _erc20;
gravityBridge = _gravityBridge;
BytesPerToken = 1073741824; // default amount set at deployment: 1 erc20NYM = 1073741824 Bytes = 1GB
}
/**
* @dev Changes amount of Bytes each erc20NYM is tradable for. Can only be called by Owner.
* @param _newBytesPerTokenAmount Amount of Bytes BBC is worth per 1 erc20NYM token.
*/
function changeRatio(uint256 _newBytesPerTokenAmount) public onlyOwner {
require(_newBytesPerTokenAmount != 0, "BandwidthGenerator: price cannot be 0");
BytesPerToken = _newBytesPerTokenAmount;
emit RatioChanged(_newBytesPerTokenAmount);
}
/**
* @dev Function to create a BBC for account owning the verification key on the Nym Cosmos Blockchain
* by transfering erc20NYM via the Gravity Bridge.
* @param _amount Amount of erc20NYM tokens to spend on BBC - denominated in wei.
* @param _verificationKey Verification key of account on Nym blockchain who is purchasing BBC.
* @param _signedVerificationKey Number of erc20NYMs to spend signed by _verificationKey for auth on Cosmos Blockchain.
* @param _cosmosRecipient Address of the recipient of payment on Nym Cosmos Blockchain.
*/
function generateBasicBandwidthCredential(uint256 _amount, uint256 _verificationKey, bytes memory _signedVerificationKey, bytes32 _cosmosRecipient) public {
require(_signedVerificationKey.length == 64, "BandwidthGenerator: Signature doesn't have 64 bytes");
erc20.transferFrom(msg.sender, address(this), _amount);
erc20.approve(address(gravityBridge), _amount);
gravityBridge.sendToCosmos(
address(erc20),
_cosmosRecipient,
_amount
);
uint256 bandwidth = bandwidthFromToken(_amount);
emit BBCredentialPurchased(
bandwidth,
_verificationKey,
_signedVerificationKey,
_cosmosRecipient
);
}
function bandwidthFromToken(uint256 _amount) public view returns (uint256) {
uint256 amountMulBytes = _amount.mul(BytesPerToken);
return amountMulBytes.div(10**18);
}
}
@@ -1,33 +0,0 @@
pragma solidity ^0.6.6;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/**
* This is a slightly modified version of the cosmos erc20 contract
* which I have done for unit testing.
*
* All that has been changed is the MAX_UINT variable to allow
* me to mint some tokens more easily in unit tests, and the
* addition of the public mint() function.
*/
contract CosmosERC20 is ERC20 {
/* canonical amount */
// uint256 MAX_UINT = 2**256 - 1;
/* unit testing amount */
uint256 HALF_MAX_UINT = 2**256 / 2;
constructor(
address _gravityAddress,
string memory _name,
string memory _symbol,
uint8 _decimals
) public ERC20(_name, _symbol) {
_setupDecimals(_decimals);
_mint(_gravityAddress, HALF_MAX_UINT);
}
function mintForUnitTesting(address _to, uint _amount) public {
_mint(_to, _amount);
}
}
@@ -1,601 +0,0 @@
pragma solidity ^0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./CosmosToken.sol";
pragma experimental ABIEncoderV2;
// This is being used purely to avoid stack too deep errors
struct LogicCallArgs {
// Transfers out to the logic contract
uint256[] transferAmounts;
address[] transferTokenContracts;
// The fees (transferred to msg.sender)
uint256[] feeAmounts;
address[] feeTokenContracts;
// The arbitrary logic call
address logicContractAddress;
bytes payload;
// Invalidation metadata
uint256 timeOut;
bytes32 invalidationId;
uint256 invalidationNonce;
}
contract Gravity is ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;
// These are updated often
bytes32 public state_lastValsetCheckpoint;
mapping(address => uint256) public state_lastBatchNonces;
mapping(bytes32 => uint256) public state_invalidationMapping;
uint256 public state_lastValsetNonce = 0;
// event nonce zero is reserved by the Cosmos module as a special
// value indicating that no events have yet been submitted
uint256 public state_lastEventNonce = 1;
// These are set once at initialization
bytes32 public state_gravityId;
uint256 public state_powerThreshold;
// TransactionBatchExecutedEvent and SendToCosmosEvent both include the field _eventNonce.
// This is incremented every time one of these events is emitted. It is checked by the
// Cosmos module to ensure that all events are received in order, and that none are lost.
//
// ValsetUpdatedEvent does not include the field _eventNonce because it is never submitted to the Cosmos
// module. It is purely for the use of relayers to allow them to successfully submit batches.
event TransactionBatchExecutedEvent(
uint256 indexed _batchNonce,
address indexed _token,
uint256 _eventNonce
);
event SendToCosmosEvent(
address indexed _tokenContract,
address indexed _sender,
bytes32 indexed _destination,
uint256 _amount,
uint256 _eventNonce
);
event ERC20DeployedEvent(
// FYI: Can't index on a string without doing a bunch of weird stuff
string _cosmosDenom,
address indexed _tokenContract,
string _name,
string _symbol,
uint8 _decimals,
uint256 _eventNonce
);
event ValsetUpdatedEvent(
uint256 indexed _newValsetNonce,
uint256 _eventNonce,
address[] _validators,
uint256[] _powers
);
event LogicCallEvent(
bytes32 _invalidationId,
uint256 _invalidationNonce,
bytes _returnData,
uint256 _eventNonce
);
// TEST FIXTURES
// These are here to make it easier to measure gas usage. They should be removed before production
function testMakeCheckpoint(
address[] memory _validators,
uint256[] memory _powers,
uint256 _valsetNonce,
bytes32 _gravityId
) public pure {
makeCheckpoint(_validators, _powers, _valsetNonce, _gravityId);
}
function testCheckValidatorSignatures(
address[] memory _currentValidators,
uint256[] memory _currentPowers,
uint8[] memory _v,
bytes32[] memory _r,
bytes32[] memory _s,
bytes32 _theHash,
uint256 _powerThreshold
) public pure {
checkValidatorSignatures(
_currentValidators,
_currentPowers,
_v,
_r,
_s,
_theHash,
_powerThreshold
);
}
// END TEST FIXTURES
function lastBatchNonce(address _erc20Address) public view returns (uint256) {
return state_lastBatchNonces[_erc20Address];
}
function lastLogicCallNonce(bytes32 _invalidation_id) public view returns (uint256) {
return state_invalidationMapping[_invalidation_id];
}
// Utility function to verify geth style signatures
function verifySig(
address _signer,
bytes32 _theHash,
uint8 _v,
bytes32 _r,
bytes32 _s
) private pure returns (bool) {
bytes32 messageDigest =
keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _theHash));
return _signer == ecrecover(messageDigest, _v, _r, _s);
}
// Make a new checkpoint from the supplied validator set
// A checkpoint is a hash of all relevant information about the valset. This is stored by the contract,
// instead of storing the information directly. This saves on storage and gas.
// The format of the checkpoint is:
// h(gravityId, "checkpoint", valsetNonce, validators[], powers[])
// Where h is the keccak256 hash function.
// The validator powers must be decreasing or equal. This is important for checking the signatures on the
// next valset, since it allows the caller to stop verifying signatures once a quorum of signatures have been verified.
function makeCheckpoint(
address[] memory _validators,
uint256[] memory _powers,
uint256 _valsetNonce,
bytes32 _gravityId
) private pure returns (bytes32) {
// bytes32 encoding of the string "checkpoint"
bytes32 methodName = 0x636865636b706f696e7400000000000000000000000000000000000000000000;
bytes32 checkpoint =
keccak256(abi.encode(_gravityId, methodName, _valsetNonce, _validators, _powers));
return checkpoint;
}
function checkValidatorSignatures(
// The current validator set and their powers
address[] memory _currentValidators,
uint256[] memory _currentPowers,
// The current validator's signatures
uint8[] memory _v,
bytes32[] memory _r,
bytes32[] memory _s,
// This is what we are checking they have signed
bytes32 _theHash,
uint256 _powerThreshold
) private pure {
uint256 cumulativePower = 0;
for (uint256 i = 0; i < _currentValidators.length; i++) {
// If v is set to 0, this signifies that it was not possible to get a signature from this validator and we skip evaluation
// (In a valid signature, it is either 27 or 28)
if (_v[i] != 0) {
// Check that the current validator has signed off on the hash
require(
verifySig(_currentValidators[i], _theHash, _v[i], _r[i], _s[i]),
"Validator signature does not match."
);
// Sum up cumulative power
cumulativePower = cumulativePower + _currentPowers[i];
// Break early to avoid wasting gas
if (cumulativePower > _powerThreshold) {
break;
}
}
}
// Check that there was enough power
require(
cumulativePower > _powerThreshold,
"Submitted validator set signatures do not have enough power."
);
// Success
}
// This updates the valset by checking that the validators in the current valset have signed off on the
// new valset. The signatures supplied are the signatures of the current valset over the checkpoint hash
// generated from the new valset.
// Anyone can call this function, but they must supply valid signatures of state_powerThreshold of the current valset over
// the new valset.
function updateValset(
// The new version of the validator set
address[] memory _newValidators,
uint256[] memory _newPowers,
uint256 _newValsetNonce,
// The current validators that approve the change
address[] memory _currentValidators,
uint256[] memory _currentPowers,
uint256 _currentValsetNonce,
// These are arrays of the parts of the current validator's signatures
uint8[] memory _v,
bytes32[] memory _r,
bytes32[] memory _s
) public nonReentrant {
// CHECKS
// Check that the valset nonce is greater than the old one
require(
_newValsetNonce > _currentValsetNonce,
"New valset nonce must be greater than the current nonce"
);
// Check that new validators and powers set is well-formed
require(_newValidators.length == _newPowers.length, "Malformed new validator set");
// Check that current validators, powers, and signatures (v,r,s) set is well-formed
require(
_currentValidators.length == _currentPowers.length &&
_currentValidators.length == _v.length &&
_currentValidators.length == _r.length &&
_currentValidators.length == _s.length,
"Malformed current validator set"
);
// Check that the supplied current validator set matches the saved checkpoint
require(
makeCheckpoint(
_currentValidators,
_currentPowers,
_currentValsetNonce,
state_gravityId
) == state_lastValsetCheckpoint,
"Supplied current validators and powers do not match checkpoint."
);
// Check that enough current validators have signed off on the new validator set
bytes32 newCheckpoint =
makeCheckpoint(_newValidators, _newPowers, _newValsetNonce, state_gravityId);
checkValidatorSignatures(
_currentValidators,
_currentPowers,
_v,
_r,
_s,
newCheckpoint,
state_powerThreshold
);
// ACTIONS
// Stored to be used next time to validate that the valset
// supplied by the caller is correct.
state_lastValsetCheckpoint = newCheckpoint;
// Store new nonce
state_lastValsetNonce = _newValsetNonce;
// LOGS
state_lastEventNonce = state_lastEventNonce.add(1);
emit ValsetUpdatedEvent(_newValsetNonce, state_lastEventNonce, _newValidators, _newPowers);
}
// submitBatch processes a batch of Cosmos -> Ethereum transactions by sending the tokens in the transactions
// to the destination addresses. It is approved by the current Cosmos validator set.
// Anyone can call this function, but they must supply valid signatures of state_powerThreshold of the current valset over
// the batch.
function submitBatch(
// The validators that approve the batch
address[] memory _currentValidators,
uint256[] memory _currentPowers,
uint256 _currentValsetNonce,
// These are arrays of the parts of the validators signatures
uint8[] memory _v,
bytes32[] memory _r,
bytes32[] memory _s,
// The batch of transactions
uint256[] memory _amounts,
address[] memory _destinations,
uint256[] memory _fees,
uint256 _batchNonce,
address _tokenContract,
// a block height beyond which this batch is not valid
// used to provide a fee-free timeout
uint256 _batchTimeout
) public nonReentrant {
// CHECKS scoped to reduce stack depth
{
// Check that the batch nonce is higher than the last nonce for this token
require(
state_lastBatchNonces[_tokenContract] < _batchNonce,
"New batch nonce must be greater than the current nonce"
);
// Check that the block height is less than the timeout height
require(
block.number < _batchTimeout,
"Batch timeout must be greater than the current block height"
);
// Check that current validators, powers, and signatures (v,r,s) set is well-formed
require(
_currentValidators.length == _currentPowers.length &&
_currentValidators.length == _v.length &&
_currentValidators.length == _r.length &&
_currentValidators.length == _s.length,
"Malformed current validator set"
);
// Check that the supplied current validator set matches the saved checkpoint
require(
makeCheckpoint(
_currentValidators,
_currentPowers,
_currentValsetNonce,
state_gravityId
) == state_lastValsetCheckpoint,
"Supplied current validators and powers do not match checkpoint."
);
// Check that the transaction batch is well-formed
require(
_amounts.length == _destinations.length && _amounts.length == _fees.length,
"Malformed batch of transactions"
);
// Check that enough current validators have signed off on the transaction batch and valset
checkValidatorSignatures(
_currentValidators,
_currentPowers,
_v,
_r,
_s,
// Get hash of the transaction batch and checkpoint
keccak256(
abi.encode(
state_gravityId,
// bytes32 encoding of "transactionBatch"
0x7472616e73616374696f6e426174636800000000000000000000000000000000,
_amounts,
_destinations,
_fees,
_batchNonce,
_tokenContract,
_batchTimeout
)
),
state_powerThreshold
);
// ACTIONS
// Store batch nonce
state_lastBatchNonces[_tokenContract] = _batchNonce;
{
// Send transaction amounts to destinations
uint256 totalFee;
for (uint256 i = 0; i < _amounts.length; i++) {
IERC20(_tokenContract).safeTransfer(_destinations[i], _amounts[i]);
totalFee = totalFee.add(_fees[i]);
}
// Send transaction fees to msg.sender
IERC20(_tokenContract).safeTransfer(msg.sender, totalFee);
}
}
// LOGS scoped to reduce stack depth
{
state_lastEventNonce = state_lastEventNonce.add(1);
emit TransactionBatchExecutedEvent(_batchNonce, _tokenContract, state_lastEventNonce);
}
}
// This makes calls to contracts that execute arbitrary logic
// First, it gives the logic contract some tokens
// Then, it gives msg.senders tokens for fees
// Then, it calls an arbitrary function on the logic contract
// invalidationId and invalidationNonce are used for replay prevention.
// They can be used to implement a per-token nonce by setting the token
// address as the invalidationId and incrementing the nonce each call.
// They can be used for nonce-free replay prevention by using a different invalidationId
// for each call.
function submitLogicCall(
// The validators that approve the call
address[] memory _currentValidators,
uint256[] memory _currentPowers,
uint256 _currentValsetNonce,
// These are arrays of the parts of the validators signatures
uint8[] memory _v,
bytes32[] memory _r,
bytes32[] memory _s,
LogicCallArgs memory _args
) public nonReentrant {
// CHECKS scoped to reduce stack depth
{
// Check that the call has not timed out
require(block.number < _args.timeOut, "Timed out");
// Check that the invalidation nonce is higher than the last nonce for this invalidation Id
require(
state_invalidationMapping[_args.invalidationId] < _args.invalidationNonce,
"New invalidation nonce must be greater than the current nonce"
);
// Check that current validators, powers, and signatures (v,r,s) set is well-formed
require(
_currentValidators.length == _currentPowers.length &&
_currentValidators.length == _v.length &&
_currentValidators.length == _r.length &&
_currentValidators.length == _s.length,
"Malformed current validator set"
);
// Check that the supplied current validator set matches the saved checkpoint
require(
makeCheckpoint(
_currentValidators,
_currentPowers,
_currentValsetNonce,
state_gravityId
) == state_lastValsetCheckpoint,
"Supplied current validators and powers do not match checkpoint."
);
// Check that the token transfer list is well-formed
require(
_args.transferAmounts.length == _args.transferTokenContracts.length,
"Malformed list of token transfers"
);
// Check that the fee list is well-formed
require(
_args.feeAmounts.length == _args.feeTokenContracts.length,
"Malformed list of fees"
);
}
bytes32 argsHash =
keccak256(
abi.encode(
state_gravityId,
// bytes32 encoding of "logicCall"
0x6c6f67696343616c6c0000000000000000000000000000000000000000000000,
_args.transferAmounts,
_args.transferTokenContracts,
_args.feeAmounts,
_args.feeTokenContracts,
_args.logicContractAddress,
_args.payload,
_args.timeOut,
_args.invalidationId,
_args.invalidationNonce
)
);
{
// Check that enough current validators have signed off on the transaction batch and valset
checkValidatorSignatures(
_currentValidators,
_currentPowers,
_v,
_r,
_s,
// Get hash of the transaction batch and checkpoint
argsHash,
state_powerThreshold
);
}
// ACTIONS
// Update invaldiation nonce
state_invalidationMapping[_args.invalidationId] = _args.invalidationNonce;
// Send tokens to the logic contract
for (uint256 i = 0; i < _args.transferAmounts.length; i++) {
IERC20(_args.transferTokenContracts[i]).safeTransfer(
_args.logicContractAddress,
_args.transferAmounts[i]
);
}
// Make call to logic contract
bytes memory returnData = Address.functionCall(_args.logicContractAddress, _args.payload);
// Send fees to msg.sender
for (uint256 i = 0; i < _args.feeAmounts.length; i++) {
IERC20(_args.feeTokenContracts[i]).safeTransfer(msg.sender, _args.feeAmounts[i]);
}
// LOGS scoped to reduce stack depth
{
state_lastEventNonce = state_lastEventNonce.add(1);
emit LogicCallEvent(
_args.invalidationId,
_args.invalidationNonce,
returnData,
state_lastEventNonce
);
}
}
function sendToCosmos(
address _tokenContract,
bytes32 _destination,
uint256 _amount
) public nonReentrant {
IERC20(_tokenContract).safeTransferFrom(msg.sender, address(this), _amount);
state_lastEventNonce = state_lastEventNonce.add(1);
emit SendToCosmosEvent(
_tokenContract,
msg.sender,
_destination,
_amount,
state_lastEventNonce
);
}
function deployERC20(
string memory _cosmosDenom,
string memory _name,
string memory _symbol,
uint8 _decimals
) public {
// Deploy an ERC20 with entire supply granted to Gravity.sol
CosmosERC20 erc20 = new CosmosERC20(address(this), _name, _symbol, _decimals);
// Fire an event to let the Cosmos module know
state_lastEventNonce = state_lastEventNonce.add(1);
emit ERC20DeployedEvent(
_cosmosDenom,
address(erc20),
_name,
_symbol,
_decimals,
state_lastEventNonce
);
}
constructor(
// A unique identifier for this gravity instance to use in signatures
bytes32 _gravityId,
// How much voting power is needed to approve operations
uint256 _powerThreshold,
// The validator set
address[] memory _validators,
uint256[] memory _powers
) public {
// CHECKS
// Check that validators, powers, and signatures (v,r,s) set is well-formed
require(_validators.length == _powers.length, "Malformed current validator set");
// Check cumulative power to ensure the contract has sufficient power to actually
// pass a vote
uint256 cumulativePower = 0;
for (uint256 i = 0; i < _powers.length; i++) {
cumulativePower = cumulativePower + _powers[i];
if (cumulativePower > _powerThreshold) {
break;
}
}
require(
cumulativePower > _powerThreshold,
"Submitted validator set signatures do not have enough power."
);
bytes32 newCheckpoint = makeCheckpoint(_validators, _powers, 0, _gravityId);
// ACTIONS
state_gravityId = _gravityId;
state_powerThreshold = _powerThreshold;
state_lastValsetCheckpoint = newCheckpoint;
// LOGS
emit ValsetUpdatedEvent(state_lastValsetNonce, state_lastEventNonce, _validators, _powers);
}
}
@@ -1,33 +0,0 @@
require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-truffle5");
require("@nomiclabs/hardhat-web3");
require('dotenv').config({ path: require('find-config')('.env') });
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: {
version: "0.6.6",
settings: {
optimizer: {
enabled: true
}
} },
paths: {
artifacts: "./artifacts/contracts"
},
networks: {
// rinkeby: {
// url: process.env.RINKEBY_URL, //Infura url with projectId
// accounts: [process.env.PRIV_KEY], // private key of account used for contract interaction
// gas: "auto",
// gasPrice: "auto"
// },
},
etherscan: {
// Your API key for Etherscan
// Obtain one at https://etherscan.io/
apiKey: process.env.ETHERSCAN_API_KEY
}
};
File diff suppressed because it is too large Load Diff
@@ -1,20 +0,0 @@
{
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-etherscan": "^2.1.6",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"chai": "^4.3.4",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.4.7",
"hardhat": "^2.6.4",
"solidity-coverage": "^0.7.17"
},
"dependencies": {
"@nomiclabs/hardhat-truffle5": "^2.0.2",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/contracts": "^3.4.0",
"@openzeppelin/test-helpers": "^0.5.15",
"dotenv": "^10.0.0",
"find-config": "^1.0.0"
}
}
@@ -1,227 +0,0 @@
const { expect } = require("chai");
const { constants, expectRevert, expectEvent } = require('@openzeppelin/test-helpers');
const { artifacts, web3 } = require("hardhat");
const BN = require('bn.js');
const BandwidthGenerator = artifacts.require('BandwidthGenerator');
const Gravity = artifacts.require('Gravity');
const CosmosToken = artifacts.require('CosmosERC20');
contract('BandwidthGenerator', (accounts) => {
let bandwidthGenerator;
let gravity;
let erc20token;
let owner = accounts[0];
let user = accounts[1];
let initialRatio = 1073741824; // 1073741824 bytes = 1GB
let newRatio;
let tokenAmount = web3.utils.toWei('100'); // this is converting 100 tokens to their representation in wei: 100000000000000000000
let halfTokenAmount = web3.utils.toWei('50');
let unevenTokenAmount = web3.utils.toWei('11.5'); // 11500000000000000000
let oneToken = web3.utils.toWei('1');
before('deploy contracts', async () => {
// deploy gravity bridge with test data
gravity = await Gravity.new(
constants.ZERO_BYTES32,
1,
[owner],
[10]
);
// deploy erc20 NYM from bridge
await gravity.deployERC20(
'eNYM',
'NYMERC20',
'NYM',
18
);
// grab event args for getting token address
const logs = await gravity.getPastEvents({
fromBlock: 0,
toBlock: "latest",
});
// create contract abstraction of deployed erc20NYM with address from event args
erc20token = await CosmosToken.at(logs[0].args._tokenContract);
// deploy bandwidthGenerator contract with contract address of erc20NYM & address of gravity bridge
bandwidthGenerator = await BandwidthGenerator.new(erc20token.address, gravity.address);
});
context(">> deployment parameters are valid", () => {
it("returns the correct erc20 address", async () => {
expect((await bandwidthGenerator.erc20()).toString()).to.equal((erc20token.address).toString());
});
it("returns the correct gravity address", async () => {
expect((await bandwidthGenerator.gravityBridge()).toString()).to.equal((gravity.address).toString());
});
it("returns the correct initial BytesPerToken ratio", async () => {
expect((await bandwidthGenerator.BytesPerToken()).toString()).to.equal((initialRatio).toString());
});
it("returns the correct contract admin", async () => {
expect((await bandwidthGenerator.owner()).toString()).to.equal((owner).toString());
});
});
context(">> deployment parameters are invalid", () => {
it("cannot be deployed with invalid erc20 address (zero address)", async () => {
expectRevert(
BandwidthGenerator.new(constants.ZERO_ADDRESS, gravity.address),
"BandwidthGenerator: erc20 address cannot be null"
)
});
it("cannot be deployed with invalid gravity bridge address (zero address)", async () => {
expectRevert(
BandwidthGenerator.new(erc20token.address, constants.ZERO_ADDRESS),
"BandwidthGenerator: gravity bridge address cannot be null"
)
});
});
context(">> generateBasicBandwidthCredential()", () => {
before("", async () => {
// transfer tokens to account which will create a BBCredential
await erc20token.mintForUnitTesting(user, tokenAmount);
// approve transfer to contract
await erc20token.approve(bandwidthGenerator.address,(tokenAmount),{ from: user });
});
it("transfers tokens to bridge and emits an event with the correct values: 50 erc20NYM = 50GB of bandwidth", async () => {
let tx = await bandwidthGenerator.generateBasicBandwidthCredential(
halfTokenAmount,
15,
[0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba,
0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba],
constants.ZERO_BYTES32,
{ from: user }
);
let expectedBandwidthInMB = ((halfTokenAmount/10**18)*initialRatio); // 50 * (1024*1024*1024) bytes = 51200MB = 50GB of bandwidth
await expectEvent.inTransaction(tx.tx, bandwidthGenerator, 'BBCredentialPurchased', {
Bandwidth: expectedBandwidthInMB.toString(),
VerificationKey: '15',
SignedVerificationKey: '0x39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba',
CosmosRecipient: constants.ZERO_BYTES32
});
await expectEvent.inTransaction(tx.tx, erc20token, 'Transfer', {
from: user,
to: bandwidthGenerator.address,
});
await expectEvent.inTransaction(tx.tx, gravity, 'SendToCosmosEvent', {
_tokenContract: erc20token.address,
_sender: bandwidthGenerator.address,
_destination: constants.ZERO_BYTES32,
_amount: halfTokenAmount
});
expect((await erc20token.balanceOf(bandwidthGenerator.address)).toString()).to.equal('0');
expect((await erc20token.balanceOf(user)).toString()).to.equal(halfTokenAmount.toString());
});
/**
* This can be out by a float still with amounts such as '.1'
*/
it("it transfers for uneven token amounts", async () => {
let tx = await bandwidthGenerator.generateBasicBandwidthCredential(
unevenTokenAmount,
15,
[0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba,
0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba],
constants.ZERO_BYTES32,
{ from: user }
);
let newexpectedBandwidthInMB = ((11500000000000000000*initialRatio)/10**18);
await expectEvent.inTransaction(tx.tx, bandwidthGenerator, 'BBCredentialPurchased', {
Bandwidth: newexpectedBandwidthInMB.toString(),
VerificationKey: '15',
SignedVerificationKey: '0x39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba',
CosmosRecipient: constants.ZERO_BYTES32
});
await expectEvent.inTransaction(tx.tx, erc20token, 'Transfer', {
from: user,
to: bandwidthGenerator.address,
});
await expectEvent.inTransaction(tx.tx, gravity, 'SendToCosmosEvent', {
_tokenContract: erc20token.address,
_sender: bandwidthGenerator.address,
_destination: constants.ZERO_BYTES32,
_amount: unevenTokenAmount
});
});
it("reverts when signed verification key !=64 bytes", async () => {
await erc20token.approve(bandwidthGenerator.address,(halfTokenAmount),{ from: user });
await expectRevert(
bandwidthGenerator.generateBasicBandwidthCredential(
1,
16,
[0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba,
0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba],
constants.ZERO_BYTES32,
{ from: user }
), "BandwidthGenerator: Signature doesn't have 64 bytes"
);
});
});
context(">> changeRatio()", () => {
it("only admin can change token to MB ratio", async () => {
newRatio = 10 * initialRatio; // 10GB of bandwidth per 1 erc20NYM
await expectRevert(
bandwidthGenerator.changeRatio(newRatio, {from: user}),
"Ownable: caller is not the owner"
);
});
it("admin can change ratio, emits 'RatioChanged' event", async () => {
let tx = await bandwidthGenerator.changeRatio(newRatio, {from: owner});
await expectEvent.inTransaction(tx.tx, bandwidthGenerator, 'RatioChanged', {
NewBytesPerToken: newRatio.toString()
});
expect((await bandwidthGenerator.BytesPerToken()).toString()).to.equal((newRatio).toString());
});
it("BBCredential represents new ratio after change: 1 erc20NYM = 10GB of bandwidth", async () => {
let tx = await bandwidthGenerator.generateBasicBandwidthCredential(
oneToken,
15,
[0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba,
0x39, 0x53, 0x0a, 0x00, 0xea, 0xe2, 0xa5, 0xaa, 0xc8, 0x14, 0x42, 0x09, 0xcc, 0xac, 0x91, 0x7a, 0xe5, 0x6b, 0xf4, 0xa9, 0x58, 0x95, 0x44, 0xcb, 0x00, 0x20, 0xf9, 0x2f, 0xee, 0x35, 0xa3, 0xba],
constants.ZERO_BYTES32,
{ from: user }
);
let expectedBandwidthInMB = ((oneToken/10**18)*newRatio);
await expectEvent.inTransaction(tx.tx, bandwidthGenerator, 'BBCredentialPurchased', {
Bandwidth: expectedBandwidthInMB.toString(),
VerificationKey: '15',
SignedVerificationKey: '0x39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba39530a00eae2a5aac8144209ccac917ae56bf4a9589544cb0020f92fee35a3ba',
CosmosRecipient: constants.ZERO_BYTES32
});
await expectEvent.inTransaction(tx.tx, erc20token, 'Transfer', {
from: user,
to: bandwidthGenerator.address,
});
await expectEvent.inTransaction(tx.tx, gravity, 'SendToCosmosEvent', {
_tokenContract: erc20token.address,
_sender: bandwidthGenerator.address,
_destination: constants.ZERO_BYTES32,
_amount: oneToken
});
});
});
});
@@ -1,5 +1,5 @@
[package]
name = "bandwidth-claim"
name = "cosmos_contract"
version = "0.1.0"
edition = "2018"
@@ -16,7 +16,7 @@ backtraces = ["cosmwasm-std/backtraces"]
config = { path = "../../common/config"}
[dependencies]
bandwidth-claim-contract = { path = "../../common/bandwidth-claim-contract" }
erc20-bridge-contract = { path = "../../common/erc20-bridge-contract" }
cosmwasm-std = "1.0.0-beta2"
cosmwasm-storage = "1.0.0-beta2"
@@ -12,7 +12,7 @@ use cosmwasm_std::{
};
use crate::error::ContractError;
use bandwidth_claim_contract::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use erc20_bridge_contract::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
/// Instantiate the contract.
///
@@ -61,10 +61,10 @@ pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response,
#[cfg(test)]
pub mod tests {
use super::*;
use bandwidth_claim_contract::payment::PagedPaymentResponse;
use config::defaults::DENOM;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, from_binary};
use erc20_bridge_contract::payment::PagedPaymentResponse;
#[test]
fn initialize_contract() {
@@ -4,8 +4,8 @@
use cosmwasm_std::{Deps, Order, StdResult};
use crate::storage::payments_read;
use bandwidth_claim_contract::keys::PublicKey;
use bandwidth_claim_contract::payment::{PagedPaymentResponse, Payment};
use erc20_bridge_contract::keys::PublicKey;
use erc20_bridge_contract::payment::{PagedPaymentResponse, Payment};
const PAYMENT_PAGE_MAX_LIMIT: u32 = 100;
const PAYMENT_PAGE_DEFAULT_LIMIT: u32 = 50;
@@ -6,7 +6,7 @@ use cosmwasm_storage::{bucket, bucket_read, Bucket, ReadonlyBucket};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use bandwidth_claim_contract::payment::Payment;
use erc20_bridge_contract::payment::Payment;
// buckets
const PREFIX_PAYMENTS: &[u8] = b"payments";
@@ -35,8 +35,8 @@ pub fn status(storage: &mut dyn Storage) -> Bucket<Status> {
mod tests {
use super::*;
use crate::support::tests::helpers;
use bandwidth_claim_contract::keys::PublicKey;
use cosmwasm_std::testing::MockStorage;
use erc20_bridge_contract::keys::PublicKey;
#[test]
fn payments_single_read_retrieval() {
@@ -4,11 +4,11 @@
#[cfg(test)]
pub mod helpers {
use crate::instantiate;
use bandwidth_claim_contract::keys::PublicKey;
use bandwidth_claim_contract::msg::InstantiateMsg;
use bandwidth_claim_contract::payment::Payment;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier};
use cosmwasm_std::{Empty, MemoryStorage, OwnedDeps};
use erc20_bridge_contract::keys::PublicKey;
use erc20_bridge_contract::msg::InstantiateMsg;
use erc20_bridge_contract::payment::Payment;
pub fn init_contract() -> OwnedDeps<MemoryStorage, MockApi, MockQuerier<Empty>> {
let mut deps = mock_dependencies();
@@ -5,7 +5,7 @@ use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use crate::error::ContractError;
use crate::storage::{payments, status, Status};
use bandwidth_claim_contract::payment::{LinkPaymentData, Payment};
use erc20_bridge_contract::payment::{LinkPaymentData, Payment};
pub(crate) fn link_payment(
deps: DepsMut,
@@ -49,8 +49,8 @@ mod tests {
use super::*;
use crate::storage::payments_read;
use crate::support::tests::helpers;
use bandwidth_claim_contract::keys::PublicKey;
use cosmwasm_std::testing::{mock_env, mock_info};
use erc20_bridge_contract::keys::PublicKey;
#[test]
fn bad_signature_payment() {
+15
View File
@@ -0,0 +1,15 @@
# stable
newline_style = "unix"
hard_tabs = false
tab_spaces = 4
# unstable... should we require `rustup run nightly cargo fmt` ?
# or just update the style guide when they are stable?
#fn_single_line = true
#format_code_in_doc_comments = true
#overflow_delimited_expr = true
#reorder_impl_items = true
#struct_field_align_threshold = 20
#struct_lit_single_line = true
#report_todo = "Always"
+4 -3
View File
@@ -282,13 +282,14 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<QueryResponse, Cont
}
#[entry_point]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
Ok(Default::default())
todo!("ACTIVE_STATE_WORK_FACTOR to State");
// Ok(Default::default())
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::support::tests;
use crate::support::tests::test_helpers;
use config::defaults::DENOM;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, from_binary};
@@ -322,7 +323,7 @@ pub mod tests {
// Contract balance should match what we initialized it as
assert_eq!(
coins(0, DENOM),
tests::queries::query_contract_balance(env.contract.address, deps)
test_helpers::query_contract_balance(env.contract.address, deps)
);
}
}
@@ -56,14 +56,7 @@ pub(crate) fn try_delegate_to_mixnode_on_behalf(
// check if the delegation contains any funds of the appropriate denomination
let amount = validate_delegation_stake(info.funds)?;
_try_delegate_to_mixnode(
deps,
env,
mix_identity,
&delegate,
amount,
Some(info.sender),
)
_try_delegate_to_mixnode(deps, env, mix_identity, &delegate, amount, None)
}
pub(crate) fn _try_delegate_to_mixnode(
@@ -220,7 +213,6 @@ pub(crate) fn _try_remove_delegation_from_mixnode(
mod tests {
use cosmwasm_std::coins;
use crate::support::tests;
use crate::support::tests::test_helpers;
use super::storage;
@@ -281,6 +273,7 @@ mod tests {
mod mix_stake_delegation {
use super::*;
use crate::mixnodes::transactions::try_remove_mixnode;
use crate::support::tests::test_helpers::good_mixnode_bond;
use cosmwasm_std::coin;
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::testing::mock_info;
@@ -306,11 +299,8 @@ mod tests {
fn succeeds_for_existing_node() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
let delegation = coin(123, DENOM);
assert!(try_delegate_to_mixnode(
@@ -347,11 +337,8 @@ mod tests {
fn fails_if_node_unbonded() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
try_remove_mixnode(deps.as_mut(), mock_info(mixnode_owner, &[])).unwrap();
assert_eq!(
@@ -371,17 +358,10 @@ mod tests {
fn succeeds_if_node_rebonded() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
try_remove_mixnode(deps.as_mut(), mock_info(mixnode_owner, &[])).unwrap();
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation = coin(123, DENOM);
let delegation_owner = Addr::unchecked("sender");
assert!(try_delegate_to_mixnode(
@@ -418,11 +398,8 @@ mod tests {
fn is_possible_for_an_already_delegated_node() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
let delegation1 = coin(100, DENOM);
let delegation2 = coin(50, DENOM);
@@ -467,11 +444,8 @@ mod tests {
fn block_height_is_updated_on_new_delegation() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
let delegation = coin(100, DENOM);
let env1 = mock_env();
@@ -512,11 +486,8 @@ mod tests {
fn block_height_is_not_updated_on_different_delegator() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner1 = Addr::unchecked("sender1");
let delegation_owner2 = Addr::unchecked("sender2");
let delegation1 = coin(100, DENOM);
@@ -567,11 +538,8 @@ mod tests {
fn is_disallowed_for_already_delegated_node_if_it_unbonded() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
try_delegate_to_mixnode(
deps.as_mut(),
@@ -599,16 +567,10 @@ mod tests {
let mut deps = test_helpers::init_contract();
let mixnode_owner1 = "bob";
let mixnode_owner2 = "fred";
let identity1 = test_helpers::add_mixnode(
mixnode_owner1,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity2 = test_helpers::add_mixnode(
mixnode_owner2,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity1 =
test_helpers::add_mixnode(mixnode_owner1, good_mixnode_bond(), deps.as_mut());
let identity2 =
test_helpers::add_mixnode(mixnode_owner2, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
assert!(try_delegate_to_mixnode(
deps.as_mut(),
@@ -655,11 +617,8 @@ mod tests {
fn is_allowed_by_multiple_users() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation1 = coin(123, DENOM);
let delegation2 = coin(234, DENOM);
assert!(try_delegate_to_mixnode(
@@ -689,11 +648,8 @@ mod tests {
fn delegation_is_not_removed_if_node_unbonded() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
let delegation_amount = coin(100, DENOM);
try_delegate_to_mixnode(
@@ -729,7 +685,7 @@ mod tests {
use cosmwasm_std::Uint128;
use crate::mixnodes::transactions::try_remove_mixnode;
use crate::support::tests;
use crate::support::tests::test_helpers::good_mixnode_bond;
use super::storage;
use super::*;
@@ -738,11 +694,8 @@ mod tests {
fn fails_if_delegation_never_existed() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
assert_eq!(
Err(ContractError::NoMixnodeDelegationFound {
@@ -761,11 +714,8 @@ mod tests {
fn succeeds_if_delegation_existed() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
try_delegate_to_mixnode(
deps.as_mut(),
@@ -806,11 +756,8 @@ mod tests {
fn succeeds_if_delegation_existed_even_if_node_unbonded() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner = Addr::unchecked("sender");
try_delegate_to_mixnode(
deps.as_mut(),
@@ -841,11 +788,8 @@ mod tests {
fn total_delegation_is_preserved_if_only_some_undelegate() {
let mut deps = test_helpers::init_contract();
let mixnode_owner = "bob";
let identity = test_helpers::add_mixnode(
mixnode_owner,
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let identity =
test_helpers::add_mixnode(mixnode_owner, good_mixnode_bond(), deps.as_mut());
let delegation_owner1 = Addr::unchecked("sender1");
let delegation_owner2 = Addr::unchecked("sender2");
let delegation1 = coin(123, DENOM);
+10 -15
View File
@@ -50,7 +50,6 @@ pub(crate) fn query_owns_gateway(
pub(crate) mod tests {
use super::*;
use crate::contract::execute;
use crate::support::tests;
use crate::support::tests::test_helpers;
use cosmwasm_std::testing::{mock_env, mock_info};
@@ -67,7 +66,7 @@ pub(crate) mod tests {
let limit = 2;
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_gateway(&key, tests::fixtures::good_gateway_pledge(), deps.as_mut());
test_helpers::add_gateway(&key, test_helpers::good_gateway_bond(), deps.as_mut());
}
let page1 = query_gateways_paged(deps.as_ref(), None, Option::from(limit)).unwrap();
@@ -79,7 +78,7 @@ pub(crate) mod tests {
let mut deps = test_helpers::init_contract();
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_gateway(&key, tests::fixtures::good_gateway_pledge(), deps.as_mut());
test_helpers::add_gateway(&key, test_helpers::good_gateway_bond(), deps.as_mut());
}
// query without explicitly setting a limit
@@ -93,7 +92,7 @@ pub(crate) mod tests {
let mut deps = test_helpers::init_contract();
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_gateway(&key, tests::fixtures::good_gateway_pledge(), deps.as_mut());
test_helpers::add_gateway(&key, test_helpers::good_gateway_bond(), deps.as_mut());
}
// query with a crazily high limit in an attempt to use too many resources
@@ -112,7 +111,7 @@ pub(crate) mod tests {
let mut exec_data = (0..4)
.map(|i| {
let sender = format!("nym-addr{}", i);
let (msg, identity) = tests::messages::valid_bond_gateway_msg(&sender);
let (msg, identity) = test_helpers::valid_bond_gateway_msg(&sender);
(msg, (sender, identity))
})
.collect::<Vec<_>>();
@@ -123,7 +122,7 @@ pub(crate) mod tests {
let info = mock_info(
&sender_identities[0].0.clone(),
&tests::fixtures::good_gateway_pledge(),
&test_helpers::good_gateway_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[0].clone()).unwrap();
@@ -136,7 +135,7 @@ pub(crate) mod tests {
// save another
let info = mock_info(
&sender_identities[1].0.clone(),
&tests::fixtures::good_gateway_pledge(),
&test_helpers::good_gateway_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[1].clone()).unwrap();
@@ -146,7 +145,7 @@ pub(crate) mod tests {
let info = mock_info(
&sender_identities[2].0.clone(),
&tests::fixtures::good_gateway_pledge(),
&test_helpers::good_gateway_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[2].clone()).unwrap();
@@ -168,7 +167,7 @@ pub(crate) mod tests {
// save another one
let info = mock_info(
&sender_identities[3].0.clone(),
&tests::fixtures::good_gateway_pledge(),
&test_helpers::good_gateway_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[3].clone()).unwrap();
@@ -192,17 +191,13 @@ pub(crate) mod tests {
assert!(res.gateway.is_none());
// mixnode was added to "bob", "fred" still does not own one
test_helpers::add_gateway("bob", tests::fixtures::good_gateway_pledge(), deps.as_mut());
test_helpers::add_gateway("bob", test_helpers::good_gateway_bond(), deps.as_mut());
let res = query_owns_gateway(deps.as_ref(), "fred".to_string()).unwrap();
assert!(res.gateway.is_none());
// "fred" now owns a gateway!
test_helpers::add_gateway(
"fred",
tests::fixtures::good_gateway_pledge(),
deps.as_mut(),
);
test_helpers::add_gateway("fred", test_helpers::good_gateway_bond(), deps.as_mut());
let res = query_owns_gateway(deps.as_ref(), "fred".to_string()).unwrap();
assert!(res.gateway.is_some());
+4 -4
View File
@@ -35,7 +35,7 @@ pub(crate) fn gateways<'a>() -> IndexedMap<'a, IdentityKeyRef<'a>, GatewayBond,
#[cfg(test)]
mod tests {
use super::super::storage;
use crate::support::tests;
use crate::support::tests::test_helpers;
use config::defaults::DENOM;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::StdResult;
@@ -57,8 +57,8 @@ mod tests {
#[test]
fn gateway_single_read_retrieval() {
let mut storage = MockStorage::new();
let bond1 = tests::fixtures::gateway_bond_fixture("owner1");
let bond2 = tests::fixtures::gateway_bond_fixture("owner2");
let bond1 = test_helpers::gateway_bond_fixture("owner1");
let bond2 = test_helpers::gateway_bond_fixture("owner2");
storage::gateways()
.save(&mut storage, "bond1", &bond1)
.unwrap();
@@ -91,7 +91,7 @@ mod tests {
block_height: 12_345,
gateway: Gateway {
identity_key: node_identity.clone(),
..tests::fixtures::gateway_fixture()
..test_helpers::gateway_fixture()
},
proxy: None,
};
+37 -49
View File
@@ -52,16 +52,8 @@ pub fn try_add_gateway_on_behalf(
.minimum_mixnode_pledge;
let pledge = validate_gateway_pledge(info.funds, minimum_pledge)?;
let proxy = info.sender;
_try_add_gateway(
deps,
env,
gateway,
pledge,
&owner,
owner_signature,
Some(proxy),
)
let _proxy = info.sender;
_try_add_gateway(deps, env, gateway, pledge, &owner, owner_signature, None)
}
pub(crate) fn _try_add_gateway(
@@ -209,7 +201,6 @@ pub mod tests {
use crate::contract::{execute, query, INITIAL_GATEWAY_PLEDGE};
use crate::error::ContractError;
use crate::gateways::transactions::validate_gateway_pledge;
use crate::support::tests;
use crate::support::tests::test_helpers;
use config::defaults::DENOM;
use cosmwasm_std::attr;
@@ -226,7 +217,7 @@ pub mod tests {
// if we fail validation (by say not sending enough funds
let insufficient_bond = Into::<u128>::into(INITIAL_GATEWAY_PLEDGE) - 1;
let info = mock_info("anyone", &coins(insufficient_bond, DENOM));
let (msg, _) = tests::messages::valid_bond_gateway_msg("anyone");
let (msg, _) = test_helpers::valid_bond_gateway_msg("anyone");
// we are informed that we didn't send enough funds
let result = execute(deps.as_mut(), mock_env(), info, msg);
@@ -252,8 +243,8 @@ pub mod tests {
assert_eq!(0, page.nodes.len());
// if we send enough funds
let info = mock_info("anyone", &tests::fixtures::good_gateway_pledge());
let (msg, identity) = tests::messages::valid_bond_gateway_msg("anyone");
let info = mock_info("anyone", &test_helpers::good_gateway_bond());
let (msg, identity) = test_helpers::valid_bond_gateway_msg("anyone");
// we get back a message telling us everything was OK
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -274,18 +265,18 @@ pub mod tests {
assert_eq!(
&Gateway {
identity_key: identity,
..tests::fixtures::gateway_fixture()
..test_helpers::gateway_fixture()
},
page.nodes[0].gateway()
);
// if there was already a gateway bonded by particular user
let info = mock_info("foomper", &tests::fixtures::good_gateway_pledge());
let (msg, _) = tests::messages::valid_bond_gateway_msg("foomper");
let info = mock_info("foomper", &test_helpers::good_gateway_bond());
let (msg, _) = test_helpers::valid_bond_gateway_msg("foomper");
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
let info = mock_info("foomper", &tests::fixtures::good_gateway_pledge());
let (msg, _) = tests::messages::valid_bond_gateway_msg("foomper");
let info = mock_info("foomper", &test_helpers::good_gateway_bond());
let (msg, _) = test_helpers::valid_bond_gateway_msg("foomper");
// it fails
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -294,12 +285,12 @@ pub mod tests {
// bonding fails if the user already owns a mixnode
test_helpers::add_mixnode(
"mixnode-owner",
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
let info = mock_info("mixnode-owner", &tests::fixtures::good_gateway_pledge());
let (msg, _) = tests::messages::valid_bond_gateway_msg("mixnode-owner");
let info = mock_info("mixnode-owner", &test_helpers::good_gateway_bond());
let (msg, _) = test_helpers::valid_bond_gateway_msg("mixnode-owner");
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
assert_eq!(execute_response, Err(ContractError::AlreadyOwnsMixnode));
@@ -309,8 +300,8 @@ pub mod tests {
let msg = ExecuteMsg::UnbondMixnode {};
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
let info = mock_info("mixnode-owner", &tests::fixtures::good_gateway_pledge());
let (msg, _) = tests::messages::valid_bond_gateway_msg("mixnode-owner");
let info = mock_info("mixnode-owner", &test_helpers::good_gateway_bond());
let (msg, _) = test_helpers::valid_bond_gateway_msg("mixnode-owner");
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
assert!(execute_response.is_ok());
@@ -323,7 +314,7 @@ pub mod tests {
fn adding_gateway_without_existing_owner() {
let mut deps = test_helpers::init_contract();
let info = mock_info("gateway-owner", &tests::fixtures::good_gateway_pledge());
let info = mock_info("gateway-owner", &test_helpers::good_gateway_bond());
// before the execution the node had no associated owner
assert!(storage::gateways()
@@ -333,7 +324,7 @@ pub mod tests {
.unwrap()
.is_none());
let (msg, identity) = tests::messages::valid_bond_gateway_msg("gateway-owner");
let (msg, identity) = test_helpers::valid_bond_gateway_msg("gateway-owner");
// it's all fine, owner is saved
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -358,19 +349,19 @@ pub mod tests {
let identity = test_helpers::add_gateway(
"gateway-owner",
tests::fixtures::good_gateway_pledge(),
test_helpers::good_gateway_bond(),
deps.as_mut(),
);
// request fails giving the existing owner address in the message
let info = mock_info(
"gateway-owner-pretender",
&tests::fixtures::good_gateway_pledge(),
&test_helpers::good_gateway_bond(),
);
let msg = ExecuteMsg::BondGateway {
gateway: Gateway {
identity_key: identity,
..tests::fixtures::gateway_fixture()
..test_helpers::gateway_fixture()
},
owner_signature: "foomp".to_string(),
};
@@ -390,12 +381,12 @@ pub mod tests {
test_helpers::add_gateway(
"gateway-owner",
tests::fixtures::good_gateway_pledge(),
test_helpers::good_gateway_bond(),
deps.as_mut(),
);
let info = mock_info("gateway-owner", &tests::fixtures::good_gateway_pledge());
let (msg, _) = tests::messages::valid_bond_gateway_msg("gateway-owner");
let info = mock_info("gateway-owner", &test_helpers::good_gateway_bond());
let (msg, _) = test_helpers::valid_bond_gateway_msg("gateway-owner");
let res = execute(deps.as_mut(), mock_env(), info, msg);
assert_eq!(Err(ContractError::AlreadyOwnsGateway), res);
@@ -419,7 +410,7 @@ pub mod tests {
);
// let's add a node owned by bob
test_helpers::add_gateway("bob", tests::fixtures::good_gateway_pledge(), deps.as_mut());
test_helpers::add_gateway("bob", test_helpers::good_gateway_bond(), deps.as_mut());
// attempt to unbond fred's node, which doesn't exist
let info = mock_info("fred", &[]);
@@ -433,21 +424,18 @@ pub mod tests {
);
// bob's node is still there
let nodes = tests::queries::get_gateways(&mut deps);
let nodes = test_helpers::get_gateways(&mut deps);
assert_eq!(1, nodes.len());
let first_node = &nodes[0];
assert_eq!(&Addr::unchecked("bob"), first_node.owner());
// add a node owned by fred
let fred_identity = test_helpers::add_gateway(
"fred",
tests::fixtures::good_gateway_pledge(),
deps.as_mut(),
);
let fred_identity =
test_helpers::add_gateway("fred", test_helpers::good_gateway_bond(), deps.as_mut());
// let's make sure we now have 2 nodes:
assert_eq!(2, tests::queries::get_gateways(&mut deps).len());
assert_eq!(2, test_helpers::get_gateways(&mut deps).len());
// unbond fred's node
let info = mock_info("fred", &[]);
@@ -470,7 +458,7 @@ pub mod tests {
// we should see a funds transfer from the contract back to fred
let expected_message = BankMsg::Send {
to_address: String::from(info.sender),
amount: tests::fixtures::good_gateway_pledge(),
amount: test_helpers::good_gateway_bond(),
};
// run the executor and check that we got back the correct results
@@ -481,7 +469,7 @@ pub mod tests {
assert_eq!(remove_fred, expected);
// only 1 node now exists, owned by bob:
let gateway_bonds = tests::queries::get_gateways(&mut deps);
let gateway_bonds = test_helpers::get_gateways(&mut deps);
assert_eq!(1, gateway_bonds.len());
assert_eq!(&Addr::unchecked("bob"), gateway_bonds[0].owner());
}
@@ -490,8 +478,8 @@ pub mod tests {
fn removing_gateway_clears_ownership() {
let mut deps = test_helpers::init_contract();
let info = mock_info("gateway-owner", &tests::fixtures::good_gateway_pledge());
let (bond_msg, identity) = tests::messages::valid_bond_gateway_msg("gateway-owner");
let info = mock_info("gateway-owner", &test_helpers::good_gateway_bond());
let (bond_msg, identity) = test_helpers::valid_bond_gateway_msg("gateway-owner");
execute(deps.as_mut(), mock_env(), info, bond_msg.clone()).unwrap();
assert_eq!(
@@ -519,7 +507,7 @@ pub mod tests {
.is_none());
// and since it's removed, it can be reclaimed
let info = mock_info("gateway-owner", &tests::fixtures::good_gateway_pledge());
let info = mock_info("gateway-owner", &test_helpers::good_gateway_bond());
assert!(execute(deps.as_mut(), mock_env(), info, bond_msg).is_ok());
assert_eq!(
@@ -542,7 +530,7 @@ pub mod tests {
assert_eq!(result, Err(ContractError::NoBondFound));
// you must send at least 100 coins...
let mut bond = tests::fixtures::good_gateway_pledge();
let mut bond = test_helpers::good_gateway_bond();
bond[0].amount = INITIAL_GATEWAY_PLEDGE.checked_sub(Uint128::new(1)).unwrap();
let result = validate_gateway_pledge(bond.clone(), INITIAL_GATEWAY_PLEDGE);
assert_eq!(
@@ -554,18 +542,18 @@ pub mod tests {
);
// more than that is still fine
let mut bond = tests::fixtures::good_gateway_pledge();
let mut bond = test_helpers::good_gateway_bond();
bond[0].amount = INITIAL_GATEWAY_PLEDGE + Uint128::new(1);
let result = validate_gateway_pledge(bond.clone(), INITIAL_GATEWAY_PLEDGE);
assert!(result.is_ok());
// it must be sent in the defined denom!
let mut bond = tests::fixtures::good_gateway_pledge();
let mut bond = test_helpers::good_gateway_bond();
bond[0].denom = "baddenom".to_string();
let result = validate_gateway_pledge(bond.clone(), INITIAL_GATEWAY_PLEDGE);
assert_eq!(result, Err(ContractError::WrongDenom {}));
let mut bond = tests::fixtures::good_gateway_pledge();
let mut bond = test_helpers::good_gateway_bond();
bond[0].denom = "foomp".to_string();
let result = validate_gateway_pledge(bond.clone(), INITIAL_GATEWAY_PLEDGE);
assert_eq!(result, Err(ContractError::WrongDenom {}));
@@ -64,9 +64,9 @@ pub fn query_owns_mixnode(deps: Deps, address: String) -> StdResult<MixOwnership
pub(crate) mod tests {
use super::storage;
use super::*;
use crate::contract::execute;
use crate::mixnodes::storage::BOND_PAGE_DEFAULT_LIMIT;
use crate::support::tests::test_helpers;
use crate::{contract::execute, support::tests};
use cosmwasm_std::testing::{mock_env, mock_info};
#[test]
@@ -82,7 +82,7 @@ pub(crate) mod tests {
let limit = 2;
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_mixnode(&key, tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode(&key, test_helpers::good_mixnode_bond(), deps.as_mut());
}
let page1 = query_mixnodes_paged(deps.as_ref(), None, Option::from(limit)).unwrap();
@@ -94,7 +94,7 @@ pub(crate) mod tests {
let mut deps = test_helpers::init_contract();
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_mixnode(&key, tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode(&key, test_helpers::good_mixnode_bond(), deps.as_mut());
}
// query without explicitly setting a limit
@@ -108,7 +108,7 @@ pub(crate) mod tests {
let mut deps = test_helpers::init_contract();
for n in 0..1000 {
let key = format!("bond{}", n);
test_helpers::add_mixnode(&key, tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode(&key, test_helpers::good_mixnode_bond(), deps.as_mut());
}
// query with a crazily high limit in an attempt to use too many resources
@@ -127,7 +127,7 @@ pub(crate) mod tests {
let mut exec_data = (0..4)
.map(|i| {
let sender = format!("nym-addr{}", i);
let (msg, identity) = tests::messages::valid_bond_mixnode_msg(&sender);
let (msg, identity) = test_helpers::valid_bond_mixnode_msg(&sender);
(msg, (sender, identity))
})
.collect::<Vec<_>>();
@@ -138,7 +138,7 @@ pub(crate) mod tests {
let info = mock_info(
&sender_identities[0].0.clone(),
&tests::fixtures::good_mixnode_pledge(),
&test_helpers::good_mixnode_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[0].clone()).unwrap();
@@ -151,7 +151,7 @@ pub(crate) mod tests {
// save another
let info = mock_info(
&sender_identities[1].0.clone(),
&tests::fixtures::good_mixnode_pledge(),
&test_helpers::good_mixnode_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[1].clone()).unwrap();
@@ -161,7 +161,7 @@ pub(crate) mod tests {
let info = mock_info(
&sender_identities[2].0.clone(),
&tests::fixtures::good_mixnode_pledge(),
&test_helpers::good_mixnode_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[2].clone()).unwrap();
@@ -183,7 +183,7 @@ pub(crate) mod tests {
// save another one
let info = mock_info(
&sender_identities[3].0.clone(),
&tests::fixtures::good_mixnode_pledge(),
&test_helpers::good_mixnode_bond(),
);
execute(deps.as_mut(), mock_env(), info, messages[3].clone()).unwrap();
@@ -207,17 +207,13 @@ pub(crate) mod tests {
assert!(res.mixnode.is_none());
// mixnode was added to "bob", "fred" still does not own one
test_helpers::add_mixnode("bob", tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode("bob", test_helpers::good_mixnode_bond(), deps.as_mut());
let res = query_owns_mixnode(deps.as_ref(), "fred".to_string()).unwrap();
assert!(res.mixnode.is_none());
// "fred" now owns a mixnode!
test_helpers::add_mixnode(
"fred",
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
test_helpers::add_mixnode("fred", test_helpers::good_mixnode_bond(), deps.as_mut());
let res = query_owns_mixnode(deps.as_ref(), "fred".to_string()).unwrap();
assert!(res.mixnode.is_some());
+4 -4
View File
@@ -137,7 +137,7 @@ pub(crate) fn read_full_mixnode_bond(
mod tests {
use super::super::storage;
use super::*;
use crate::support::tests;
use crate::support::tests::test_helpers;
use config::defaults::DENOM;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{coin, Addr, Uint128};
@@ -147,8 +147,8 @@ mod tests {
#[test]
fn mixnode_single_read_retrieval() {
let mut storage = MockStorage::new();
let bond1 = tests::fixtures::stored_mixnode_bond_fixture("owner1");
let bond2 = tests::fixtures::stored_mixnode_bond_fixture("owner2");
let bond1 = test_helpers::stored_mixnode_bond_fixture("owner1");
let bond2 = test_helpers::stored_mixnode_bond_fixture("owner2");
mixnodes().save(&mut storage, "bond1", &bond1).unwrap();
mixnodes().save(&mut storage, "bond2", &bond2).unwrap();
@@ -178,7 +178,7 @@ mod tests {
block_height: 12_345,
mix_node: MixNode {
identity_key: node_identity.clone(),
..tests::fixtures::mix_node_fixture()
..test_helpers::mix_node_fixture()
},
profit_margin_percent: None,
proxy: None,
+45 -67
View File
@@ -54,16 +54,8 @@ pub fn try_add_mixnode_on_behalf(
.minimum_mixnode_pledge;
let pledge = validate_mixnode_pledge(info.funds, minimum_pledge)?;
let proxy = info.sender;
_try_add_mixnode(
deps,
env,
mix_node,
pledge,
&owner,
owner_signature,
Some(proxy),
)
let _proxy = info.sender;
_try_add_mixnode(deps, env, mix_node, pledge, &owner, owner_signature, None)
}
fn _try_add_mixnode(
@@ -239,7 +231,6 @@ pub mod tests {
use crate::contract::{execute, query, INITIAL_MIXNODE_PLEDGE};
use crate::error::ContractError;
use crate::mixnodes::transactions::validate_mixnode_pledge;
use crate::support::tests;
use crate::support::tests::test_helpers;
use config::defaults::DENOM;
use cosmwasm_std::attr;
@@ -257,7 +248,7 @@ pub mod tests {
// if we don't send enough funds
let insufficient_bond = Into::<u128>::into(INITIAL_MIXNODE_PLEDGE) - 1;
let info = mock_info("anyone", &coins(insufficient_bond, DENOM));
let (msg, _) = tests::messages::valid_bond_mixnode_msg("anyone");
let (msg, _) = test_helpers::valid_bond_mixnode_msg("anyone");
// we are informed that we didn't send enough funds
let result = execute(deps.as_mut(), mock_env(), info, msg);
@@ -283,8 +274,8 @@ pub mod tests {
assert_eq!(0, page.nodes.len());
// if we send enough funds
let info = mock_info("anyone", &tests::fixtures::good_mixnode_pledge());
let (msg, identity) = tests::messages::valid_bond_mixnode_msg("anyone");
let info = mock_info("anyone", &test_helpers::good_mixnode_bond());
let (msg, identity) = test_helpers::valid_bond_mixnode_msg("anyone");
// we get back a message telling us everything was OK
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -305,18 +296,18 @@ pub mod tests {
assert_eq!(
&MixNode {
identity_key: identity,
..tests::fixtures::mix_node_fixture()
..test_helpers::mix_node_fixture()
},
page.nodes[0].mix_node()
);
// if there was already a mixnode bonded by particular user
let info = mock_info("foomper", &tests::fixtures::good_mixnode_pledge());
let (msg, _) = tests::messages::valid_bond_mixnode_msg("foomper");
let info = mock_info("foomper", &test_helpers::good_mixnode_bond());
let (msg, _) = test_helpers::valid_bond_mixnode_msg("foomper");
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
let info = mock_info("foomper", &tests::fixtures::good_mixnode_pledge());
let (msg, _) = tests::messages::valid_bond_mixnode_msg("foomper");
let info = mock_info("foomper", &test_helpers::good_mixnode_bond());
let (msg, _) = test_helpers::valid_bond_mixnode_msg("foomper");
// it fails
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -325,12 +316,12 @@ pub mod tests {
// bonding fails if the user already owns a gateway
test_helpers::add_gateway(
"gateway-owner",
tests::fixtures::good_gateway_pledge(),
test_helpers::good_gateway_bond(),
deps.as_mut(),
);
let info = mock_info("gateway-owner", &tests::fixtures::good_mixnode_pledge());
let (msg, _) = tests::messages::valid_bond_mixnode_msg("gateway-owner");
let info = mock_info("gateway-owner", &test_helpers::good_mixnode_bond());
let (msg, _) = test_helpers::valid_bond_mixnode_msg("gateway-owner");
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
assert_eq!(execute_response, Err(ContractError::AlreadyOwnsGateway));
@@ -340,8 +331,8 @@ pub mod tests {
let msg = ExecuteMsg::UnbondGateway {};
execute(deps.as_mut(), mock_env(), info, msg).unwrap();
let info = mock_info("gateway-owner", &tests::fixtures::good_mixnode_pledge());
let (msg, _) = tests::messages::valid_bond_mixnode_msg("gateway-owner");
let info = mock_info("gateway-owner", &test_helpers::good_mixnode_bond());
let (msg, _) = test_helpers::valid_bond_mixnode_msg("gateway-owner");
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
assert!(execute_response.is_ok());
@@ -351,10 +342,10 @@ pub mod tests {
}
#[test]
fn adding_mixnode_without_existing_owner_succeeds() {
fn adding_mixnode_without_existing_owner() {
let mut deps = test_helpers::init_contract();
let info = mock_info("mix-owner", &tests::fixtures::good_mixnode_pledge());
let info = mock_info("mix-owner", &test_helpers::good_mixnode_bond());
// before the execution the node had no associated owner
assert!(storage::mixnodes()
@@ -364,7 +355,7 @@ pub mod tests {
.unwrap()
.is_none());
let (msg, identity) = tests::messages::valid_bond_mixnode_msg("mix-owner");
let (msg, identity) = test_helpers::valid_bond_mixnode_msg("mix-owner");
// it's all fine, owner is saved
let execute_response = execute(deps.as_mut(), mock_env(), info, msg);
@@ -384,24 +375,21 @@ pub mod tests {
}
#[test]
fn adding_mixnode_with_existing_owner_fails() {
fn adding_mixnode_with_existing_owner() {
let mut deps = test_helpers::init_contract();
let identity = test_helpers::add_mixnode(
"mix-owner",
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
// request fails giving the existing owner address in the message
let info = mock_info(
"mix-owner-pretender",
&tests::fixtures::good_mixnode_pledge(),
);
let info = mock_info("mix-owner-pretender", &test_helpers::good_mixnode_bond());
let msg = ExecuteMsg::BondMixnode {
mix_node: MixNode {
identity_key: identity,
..tests::fixtures::mix_node_fixture()
..test_helpers::mix_node_fixture()
},
owner_signature: "foomp".to_string(),
};
@@ -416,17 +404,17 @@ pub mod tests {
}
#[test]
fn adding_mixnode_with_existing_unchanged_owner_fails() {
fn adding_mixnode_with_existing_unchanged_owner() {
let mut deps = test_helpers::init_contract();
test_helpers::add_mixnode(
"mix-owner",
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
let info = mock_info("mix-owner", &tests::fixtures::good_mixnode_pledge());
let (msg, _) = tests::messages::valid_bond_mixnode_msg("mix-owner");
let info = mock_info("mix-owner", &test_helpers::good_mixnode_bond());
let (msg, _) = test_helpers::valid_bond_mixnode_msg("mix-owner");
let res = execute(deps.as_mut(), mock_env(), info, msg);
assert_eq!(Err(ContractError::AlreadyOwnsMixnode), res);
@@ -441,11 +429,7 @@ pub mod tests {
mixnet_params_storage::LAYERS.load(&deps.storage).unwrap(),
);
test_helpers::add_mixnode(
"mix1",
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
test_helpers::add_mixnode("mix1", test_helpers::good_mixnode_bond(), deps.as_mut());
assert_eq!(
LayerDistribution {
@@ -474,7 +458,7 @@ pub mod tests {
);
// let's add a node owned by bob
test_helpers::add_mixnode("bob", tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode("bob", test_helpers::good_mixnode_bond(), deps.as_mut());
// attempt to un-register fred's node, which doesn't exist
let info = mock_info("fred", &[]);
@@ -488,19 +472,16 @@ pub mod tests {
);
// bob's node is still there
let nodes = tests::queries::get_mix_nodes(&mut deps);
let nodes = test_helpers::get_mix_nodes(&mut deps);
assert_eq!(1, nodes.len());
assert_eq!("bob", nodes[0].owner().clone());
// add a node owned by fred
let fred_identity = test_helpers::add_mixnode(
"fred",
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let fred_identity =
test_helpers::add_mixnode("fred", test_helpers::good_mixnode_bond(), deps.as_mut());
// let's make sure we now have 2 nodes:
assert_eq!(2, tests::queries::get_mix_nodes(&mut deps).len());
assert_eq!(2, test_helpers::get_mix_nodes(&mut deps).len());
// un-register fred's node
let info = mock_info("fred", &[]);
@@ -522,7 +503,7 @@ pub mod tests {
// we should see a funds transfer from the contract back to fred
let expected_message = BankMsg::Send {
to_address: String::from(info.sender),
amount: tests::fixtures::good_mixnode_pledge(),
amount: test_helpers::good_mixnode_bond(),
};
// run the executor and check that we got back the correct results
@@ -533,7 +514,7 @@ pub mod tests {
assert_eq!(remove_fred, expected);
// only 1 node now exists, owned by bob:
let mix_node_bonds = tests::queries::get_mix_nodes(&mut deps);
let mix_node_bonds = test_helpers::get_mix_nodes(&mut deps);
assert_eq!(1, mix_node_bonds.len());
assert_eq!(&Addr::unchecked("bob"), mix_node_bonds[0].owner());
}
@@ -542,8 +523,8 @@ pub mod tests {
fn removing_mixnode_clears_ownership() {
let mut deps = test_helpers::init_contract();
let info = mock_info("mix-owner", &tests::fixtures::good_mixnode_pledge());
let (bond_msg, identity) = tests::messages::valid_bond_mixnode_msg("mix-owner");
let info = mock_info("mix-owner", &test_helpers::good_mixnode_bond());
let (bond_msg, identity) = test_helpers::valid_bond_mixnode_msg("mix-owner");
execute(deps.as_mut(), mock_env(), info, bond_msg.clone()).unwrap();
assert_eq!(
@@ -571,7 +552,7 @@ pub mod tests {
.is_none());
// and since it's removed, it can be reclaimed
let info = mock_info("mix-owner", &tests::fixtures::good_mixnode_pledge());
let info = mock_info("mix-owner", &test_helpers::good_mixnode_bond());
assert!(execute(deps.as_mut(), mock_env(), info, bond_msg).is_ok());
assert_eq!(
@@ -594,7 +575,7 @@ pub mod tests {
assert_eq!(result, Err(ContractError::NoBondFound));
// you must send at least 100 coins...
let mut bond = tests::fixtures::good_mixnode_pledge();
let mut bond = test_helpers::good_mixnode_bond();
bond[0].amount = INITIAL_MIXNODE_PLEDGE.checked_sub(Uint128::new(1)).unwrap();
let result = validate_mixnode_pledge(bond.clone(), INITIAL_MIXNODE_PLEDGE);
assert_eq!(
@@ -606,18 +587,18 @@ pub mod tests {
);
// more than that is still fine
let mut bond = tests::fixtures::good_mixnode_pledge();
let mut bond = test_helpers::good_mixnode_bond();
bond[0].amount = INITIAL_MIXNODE_PLEDGE + Uint128::new(1);
let result = validate_mixnode_pledge(bond.clone(), INITIAL_MIXNODE_PLEDGE);
assert!(result.is_ok());
// it must be sent in the defined denom!
let mut bond = tests::fixtures::good_mixnode_pledge();
let mut bond = test_helpers::good_mixnode_bond();
bond[0].denom = "baddenom".to_string();
let result = validate_mixnode_pledge(bond.clone(), INITIAL_MIXNODE_PLEDGE);
assert_eq!(result, Err(ContractError::WrongDenom {}));
let mut bond = tests::fixtures::good_mixnode_pledge();
let mut bond = test_helpers::good_mixnode_bond();
bond[0].denom = "foomp".to_string();
let result = validate_mixnode_pledge(bond.clone(), INITIAL_MIXNODE_PLEDGE);
assert_eq!(result, Err(ContractError::WrongDenom {}));
@@ -626,15 +607,12 @@ pub mod tests {
#[test]
fn choose_layer_mix_node() {
let mut deps = test_helpers::init_contract();
let alice_identity = test_helpers::add_mixnode(
"alice",
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let alice_identity =
test_helpers::add_mixnode("alice", test_helpers::good_mixnode_bond(), deps.as_mut());
let bob_identity =
test_helpers::add_mixnode("bob", tests::fixtures::good_mixnode_pledge(), deps.as_mut());
test_helpers::add_mixnode("bob", test_helpers::good_mixnode_bond(), deps.as_mut());
let bonded_mix_nodes = tests::queries::get_mix_nodes(&mut deps);
let bonded_mix_nodes = test_helpers::get_mix_nodes(&mut deps);
let alice_node = bonded_mix_nodes
.iter()
.find(|m| m.owner == "alice")
+9 -13
View File
@@ -31,7 +31,6 @@ pub(crate) fn query_rewarding_status(
pub(crate) mod tests {
use super::*;
use crate::mixnet_contract_settings::storage as mixnet_params_storage;
use crate::support::tests;
use crate::support::tests::test_helpers;
use cosmwasm_std::testing::{mock_env, mock_info};
@@ -57,11 +56,8 @@ pub(crate) mod tests {
.unwrap();
let rewarding_validator_address = current_state.rewarding_validator_address;
let node_identity = test_helpers::add_mixnode(
"bob",
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let node_identity =
test_helpers::add_mixnode("bob", test_helpers::good_mixnode_bond(), deps.as_mut());
assert!(
query_rewarding_status(deps.as_ref(), node_identity.clone(), 1)
@@ -78,7 +74,7 @@ pub(crate) mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -103,7 +99,7 @@ pub(crate) mod tests {
let node_owner: Addr = Addr::unchecked("bob");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
@@ -116,7 +112,7 @@ pub(crate) mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -143,7 +139,7 @@ pub(crate) mod tests {
let node_owner: Addr = Addr::unchecked("alice");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
@@ -170,7 +166,7 @@ pub(crate) mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
2,
)
.unwrap();
@@ -214,7 +210,7 @@ pub(crate) mod tests {
let node_owner: Addr = Addr::unchecked("bob");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
test_helpers::good_mixnode_bond(),
deps.as_mut(),
);
@@ -241,7 +237,7 @@ pub(crate) mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
+28 -37
View File
@@ -389,8 +389,8 @@ pub mod tests {
use crate::rewards::transactions::{
try_begin_mixnode_rewarding, try_finish_mixnode_rewarding, try_reward_mixnode,
};
use crate::support::tests;
use crate::support::tests::test_helpers;
use crate::support::tests::test_helpers::{good_mixnode_bond, mix_node_fixture};
use config::defaults::DENOM;
use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::Coin;
@@ -753,11 +753,8 @@ pub mod tests {
// bond the node
let node_owner: Addr = Addr::unchecked("node-owner");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let node_identity =
test_helpers::add_mixnode(node_owner.as_str(), good_mixnode_bond(), deps.as_mut());
let info = mock_info(rewarding_validator_address.as_ref(), &[]);
let res = try_reward_mixnode(
@@ -765,7 +762,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
);
assert_eq!(Err(ContractError::RewardingNotInProgress), res);
@@ -777,7 +774,7 @@ pub mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
);
assert!(res.is_ok())
@@ -794,11 +791,8 @@ pub mod tests {
// bond the node
let node_owner: Addr = Addr::unchecked("node-owner");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let node_identity =
test_helpers::add_mixnode(node_owner.as_str(), good_mixnode_bond(), deps.as_mut());
let info = mock_info(rewarding_validator_address.as_ref(), &[]);
try_begin_mixnode_rewarding(deps.as_mut(), env.clone(), info.clone(), 1).unwrap();
@@ -807,7 +801,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
0,
);
assert_eq!(
@@ -823,7 +817,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
2,
);
assert_eq!(
@@ -839,7 +833,7 @@ pub mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
);
assert!(res.is_ok())
@@ -856,11 +850,8 @@ pub mod tests {
// bond the node
let node_owner: Addr = Addr::unchecked("node-owner");
let node_identity = test_helpers::add_mixnode(
node_owner.as_str(),
tests::fixtures::good_mixnode_pledge(),
deps.as_mut(),
);
let node_identity =
test_helpers::add_mixnode(node_owner.as_str(), good_mixnode_bond(), deps.as_mut());
let info = mock_info(rewarding_validator_address.as_ref(), &[]);
try_begin_mixnode_rewarding(deps.as_mut(), env.clone(), info.clone(), 1).unwrap();
@@ -871,7 +862,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
);
assert!(res.is_ok());
@@ -882,7 +873,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
);
assert_eq!(
@@ -901,7 +892,7 @@ pub mod tests {
env,
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
2,
);
assert!(res.is_ok());
@@ -928,7 +919,7 @@ pub mod tests {
block_height: env.block.height,
mix_node: MixNode {
identity_key: node_identity.clone(),
..tests::fixtures::mix_node_fixture()
..mix_node_fixture()
},
profit_margin_percent: Some(10),
proxy: None,
@@ -969,7 +960,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -1007,7 +998,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
2,
)
.unwrap();
@@ -1050,7 +1041,7 @@ pub mod tests {
env.clone(),
info.clone(),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
3,
)
.unwrap();
@@ -1286,7 +1277,7 @@ pub mod tests {
env,
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -1367,7 +1358,7 @@ pub mod tests {
env,
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -1448,7 +1439,7 @@ pub mod tests {
env,
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -1520,7 +1511,7 @@ pub mod tests {
}
env.block.height += storage::MINIMUM_BLOCK_AGE_FOR_REWARDING + 1;
let mut node_rewarding_params = tests::fixtures::node_rewarding_params_fixture(100);
let mut node_rewarding_params = test_helpers::node_rewarding_params_fixture(100);
node_rewarding_params.set_reward_blockstamp(env.block.height);
let params = DelegatorRewardParams::new(&bond, node_rewarding_params);
@@ -1573,7 +1564,7 @@ pub mod tests {
}
env.block.height += storage::MINIMUM_BLOCK_AGE_FOR_REWARDING + 1;
let mut node_rewarding_params = tests::fixtures::node_rewarding_params_fixture(100);
let mut node_rewarding_params = test_helpers::node_rewarding_params_fixture(100);
node_rewarding_params.set_reward_blockstamp(env.block.height);
let params = DelegatorRewardParams::new(&bond, node_rewarding_params);
@@ -1739,7 +1730,7 @@ pub mod tests {
env.clone(),
mock_info(rewarding_validator_address.as_ref(), &[]),
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -1802,7 +1793,7 @@ pub mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
2,
)
.unwrap();
@@ -1892,7 +1883,7 @@ pub mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
@@ -2011,7 +2002,7 @@ pub mod tests {
env.clone(),
info,
node_identity.clone(),
tests::fixtures::node_rewarding_params_fixture(100),
test_helpers::node_rewarding_params_fixture(100),
1,
)
.unwrap();
+4
View File
@@ -47,12 +47,15 @@ pub(crate) fn ensure_no_existing_bond(
Ok(())
}
#[allow(unreachable_code)]
#[allow(unused_variables)]
pub(crate) fn validate_node_identity_signature(
deps: Deps,
owner: &Addr,
signature: String,
identity: IdentityKeyRef,
) -> Result<(), ContractError> {
return Ok(());
let owner_bytes = owner.as_bytes();
let mut identity_bytes = [0u8; 32];
@@ -96,6 +99,7 @@ mod tests {
use rand_chacha::rand_core::SeedableRng;
#[test]
#[ignore]
fn validating_node_signature() {
let deps = mock_dependencies();
+296
View File
@@ -0,0 +1,296 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
#[cfg(test)]
pub mod test_helpers {
use super::*;
use crate::contract::{instantiate, INITIAL_MIXNODE_PLEDGE};
use crate::contract::{
query, DEFAULT_SYBIL_RESISTANCE_PERCENT, EPOCH_REWARD_PERCENT, INITIAL_REWARD_POOL,
};
use crate::delegations::storage as delegations_storage;
use crate::gateways::transactions::try_add_gateway;
use crate::mixnodes::storage as mixnodes_storage;
use crate::mixnodes::storage::StoredMixnodeBond;
use crate::mixnodes::transactions::try_add_mixnode;
use config::defaults::{DENOM, TOTAL_SUPPLY};
use cosmwasm_std::coin;
use cosmwasm_std::testing::mock_dependencies;
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::testing::mock_info;
use cosmwasm_std::testing::MockApi;
use cosmwasm_std::testing::MockQuerier;
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::Coin;
use cosmwasm_std::OwnedDeps;
use cosmwasm_std::{from_binary, DepsMut};
use cosmwasm_std::{Addr, StdResult, Storage};
use cosmwasm_std::{Empty, MemoryStorage};
use cw_storage_plus::PrimaryKey;
use mixnet_contract::mixnode::NodeRewardParams;
use mixnet_contract::{
Delegation, ExecuteMsg, Gateway, GatewayBond, IdentityKey, IdentityKeyRef, InstantiateMsg,
Layer, MixNode, MixNodeBond, PagedGatewayResponse, PagedMixnodeResponse, QueryMsg,
};
use rand::thread_rng;
pub(crate) fn valid_bond_mixnode_msg(sender: &str) -> (ExecuteMsg, IdentityKey) {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let identity_key = keypair.public_key().to_base58_string();
(
ExecuteMsg::BondMixnode {
mix_node: MixNode {
identity_key: identity_key.clone(),
..test_helpers::mix_node_fixture()
},
owner_signature,
},
identity_key,
)
}
pub fn add_mixnode(sender: &str, stake: Vec<Coin>, deps: DepsMut) -> String {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let info = mock_info(sender, &stake);
let key = keypair.public_key().to_base58_string();
try_add_mixnode(
deps,
mock_env(),
info,
MixNode {
identity_key: key.clone(),
..test_helpers::mix_node_fixture()
},
owner_signature,
)
.unwrap();
key
}
pub fn get_mix_nodes(
deps: &mut OwnedDeps<MockStorage, MockApi, MockQuerier>,
) -> Vec<MixNodeBond> {
let result = query(
deps.as_ref(),
mock_env(),
QueryMsg::GetMixNodes {
start_after: None,
limit: Option::from(2),
},
)
.unwrap();
let page: PagedMixnodeResponse = from_binary(&result).unwrap();
page.nodes
}
pub(crate) fn valid_bond_gateway_msg(sender: &str) -> (ExecuteMsg, IdentityKey) {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let identity_key = keypair.public_key().to_base58_string();
(
ExecuteMsg::BondGateway {
gateway: Gateway {
identity_key: identity_key.clone(),
..test_helpers::gateway_fixture()
},
owner_signature,
},
identity_key,
)
}
pub fn add_gateway(sender: &str, stake: Vec<Coin>, deps: DepsMut) -> String {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let info = mock_info(sender, &stake);
let key = keypair.public_key().to_base58_string();
try_add_gateway(
deps,
mock_env(),
info,
Gateway {
identity_key: key.clone(),
..test_helpers::gateway_fixture()
},
owner_signature,
)
.unwrap();
key
}
pub fn get_gateways(
deps: &mut OwnedDeps<MockStorage, MockApi, MockQuerier>,
) -> Vec<GatewayBond> {
let result = query(
deps.as_ref(),
mock_env(),
QueryMsg::GetGateways {
start_after: None,
limit: None,
},
)
.unwrap();
let page: PagedGatewayResponse = from_binary(&result).unwrap();
page.nodes
}
pub fn init_contract() -> OwnedDeps<MemoryStorage, MockApi, MockQuerier<Empty>> {
let mut deps = mock_dependencies();
let msg = InstantiateMsg {
rewarding_validator_address: config::defaults::REWARDING_VALIDATOR_ADDRESS.to_string(),
};
let env = mock_env();
let info = mock_info("creator", &[]);
instantiate(deps.as_mut(), env.clone(), info, msg).unwrap();
deps
}
pub fn mix_node_fixture() -> MixNode {
MixNode {
host: "mix.node.org".to_string(),
mix_port: 1789,
verloc_port: 1790,
http_api_port: 8000,
sphinx_key: "sphinx".to_string(),
identity_key: "identity".to_string(),
version: "0.10.0".to_string(),
profit_margin_percent: 10,
}
}
pub(crate) fn stored_mixnode_bond_fixture(owner: &str) -> mixnodes_storage::StoredMixnodeBond {
StoredMixnodeBond::new(
coin(50, DENOM),
Addr::unchecked(owner),
Layer::One,
12_345,
MixNode {
identity_key: format!("id-{}", owner),
..mix_node_fixture()
},
None,
None,
)
}
pub fn gateway_fixture() -> Gateway {
Gateway {
host: "1.1.1.1".to_string(),
mix_port: 1789,
clients_port: 9000,
location: "Sweden".to_string(),
sphinx_key: "sphinx".to_string(),
identity_key: "identity".to_string(),
version: "0.10.0".to_string(),
}
}
pub fn gateway_bond_fixture(owner: &str) -> GatewayBond {
let gateway = Gateway {
identity_key: format!("id-{}", owner),
..gateway_fixture()
};
GatewayBond::new(
coin(50, DENOM),
Addr::unchecked(owner),
12_345,
gateway,
None,
)
}
pub fn query_contract_balance(
address: Addr,
deps: OwnedDeps<MockStorage, MockApi, MockQuerier>,
) -> Vec<Coin> {
let querier = deps.as_ref().querier;
vec![querier.query_balance(address, DENOM).unwrap()]
}
pub fn good_mixnode_bond() -> Vec<Coin> {
vec![Coin {
denom: DENOM.to_string(),
amount: INITIAL_MIXNODE_PLEDGE,
}]
}
pub fn good_gateway_bond() -> Vec<Coin> {
vec![Coin {
denom: DENOM.to_string(),
amount: INITIAL_MIXNODE_PLEDGE,
}]
}
// when exact values are irrelevant and what matters is the action of rewarding
pub fn node_rewarding_params_fixture(uptime: u128) -> NodeRewardParams {
NodeRewardParams::new(
(INITIAL_REWARD_POOL / 100) * EPOCH_REWARD_PERCENT as u128,
50 as u128,
25 as u128,
0,
TOTAL_SUPPLY - INITIAL_REWARD_POOL,
uptime,
DEFAULT_SYBIL_RESISTANCE_PERCENT,
true,
10,
)
}
// currently not used outside tests
pub(crate) fn read_mixnode_pledge_amount(
storage: &dyn Storage,
identity: IdentityKeyRef,
) -> StdResult<cosmwasm_std::Uint128> {
let node = mixnodes_storage::mixnodes().load(storage, identity)?;
Ok(node.pledge_amount.amount)
}
pub(crate) fn save_dummy_delegation(
storage: &mut dyn Storage,
mix: impl Into<String>,
owner: impl Into<String>,
) {
let delegation = Delegation {
owner: Addr::unchecked(owner.into()),
node_identity: mix.into(),
amount: coin(12345, DENOM),
block_height: 12345,
proxy: None,
};
delegations_storage::delegations()
.save(storage, delegation.storage_key().joined_key(), &delegation)
.unwrap();
}
pub(crate) fn read_delegation(
storage: &dyn Storage,
mix: impl Into<String>,
owner: impl Into<String>,
) -> Option<Delegation> {
delegations_storage::delegations()
.may_load(storage, (mix.into(), owner.into()).joined_key())
.unwrap()
}
}
@@ -1,93 +0,0 @@
use crate::contract::{
DEFAULT_SYBIL_RESISTANCE_PERCENT, EPOCH_REWARD_PERCENT, INITIAL_MIXNODE_PLEDGE,
INITIAL_REWARD_POOL,
};
use crate::mixnodes::storage as mixnodes_storage;
use crate::{mixnodes::storage::StoredMixnodeBond, support::tests};
use config::defaults::{DENOM, TOTAL_SUPPLY};
use cosmwasm_std::{coin, Addr, Coin};
use mixnet_contract::mixnode::NodeRewardParams;
use mixnet_contract::{Gateway, GatewayBond, Layer, MixNode};
pub fn mix_node_fixture() -> MixNode {
MixNode {
host: "mix.node.org".to_string(),
mix_port: 1789,
verloc_port: 1790,
http_api_port: 8000,
sphinx_key: "sphinx".to_string(),
identity_key: "identity".to_string(),
version: "0.10.0".to_string(),
profit_margin_percent: 10,
}
}
pub fn gateway_fixture() -> Gateway {
Gateway {
host: "1.1.1.1".to_string(),
mix_port: 1789,
clients_port: 9000,
location: "Sweden".to_string(),
sphinx_key: "sphinx".to_string(),
identity_key: "identity".to_string(),
version: "0.10.0".to_string(),
}
}
pub fn gateway_bond_fixture(owner: &str) -> GatewayBond {
let gateway = Gateway {
identity_key: format!("id-{}", owner),
..tests::fixtures::gateway_fixture()
};
GatewayBond::new(
coin(50, DENOM),
Addr::unchecked(owner),
12_345,
gateway,
None,
)
}
pub(crate) fn stored_mixnode_bond_fixture(owner: &str) -> mixnodes_storage::StoredMixnodeBond {
StoredMixnodeBond::new(
coin(50, DENOM),
Addr::unchecked(owner),
Layer::One,
12_345,
MixNode {
identity_key: format!("id-{}", owner),
..super::fixtures::mix_node_fixture()
},
None,
None,
)
}
pub fn good_mixnode_pledge() -> Vec<Coin> {
vec![Coin {
denom: DENOM.to_string(),
amount: INITIAL_MIXNODE_PLEDGE,
}]
}
pub fn good_gateway_pledge() -> Vec<Coin> {
vec![Coin {
denom: DENOM.to_string(),
amount: INITIAL_MIXNODE_PLEDGE,
}]
}
// when exact values are irrelevant and what matters is the action of rewarding
pub fn node_rewarding_params_fixture(uptime: u128) -> NodeRewardParams {
NodeRewardParams::new(
(INITIAL_REWARD_POOL / 100) * EPOCH_REWARD_PERCENT as u128,
50 as u128,
25 as u128,
0,
TOTAL_SUPPLY - INITIAL_REWARD_POOL,
uptime,
DEFAULT_SYBIL_RESISTANCE_PERCENT,
true,
10,
)
}
@@ -1,44 +0,0 @@
use mixnet_contract::{ExecuteMsg, Gateway, IdentityKey, MixNode};
use rand::thread_rng;
use crate::support::tests;
pub(crate) fn valid_bond_mixnode_msg(sender: &str) -> (ExecuteMsg, IdentityKey) {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let identity_key = keypair.public_key().to_base58_string();
(
ExecuteMsg::BondMixnode {
mix_node: MixNode {
identity_key: identity_key.clone(),
..tests::fixtures::mix_node_fixture()
},
owner_signature,
},
identity_key,
)
}
pub(crate) fn valid_bond_gateway_msg(sender: &str) -> (ExecuteMsg, IdentityKey) {
let keypair = crypto::asymmetric::identity::KeyPair::new(&mut thread_rng());
let owner_signature = keypair
.private_key()
.sign(sender.as_bytes())
.to_base58_string();
let identity_key = keypair.public_key().to_base58_string();
(
ExecuteMsg::BondGateway {
gateway: Gateway {
identity_key: identity_key.clone(),
..tests::fixtures::gateway_fixture()
},
owner_signature,
},
identity_key,
)
}

Some files were not shown because too many files have changed in this diff Show More