Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into salman/benchmarks-rol…
Browse files Browse the repository at this point in the history
…es-pallet
  • Loading branch information
salman01zp committed Jan 4, 2024
2 parents 3114ab3 + a2acee0 commit 0622062
Show file tree
Hide file tree
Showing 54 changed files with 2,802 additions and 1,535 deletions.
1,729 changes: 1,294 additions & 435 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tangle"
build = "build.rs"
description = "Tangle Standalone chain node"
description = "Tangle chain node"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
Expand Down
9 changes: 6 additions & 3 deletions node/src/chainspec/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sp_runtime::{
traits::{AccountIdConversion, IdentifyAccount, Verify},
BoundedVec,
};
use tangle_primitives::{BlockNumber, Signature};
use tangle_primitives::types::{BlockNumber, Signature};
use tangle_runtime::{
AccountId, BabeConfig, Balance, BalancesConfig, ClaimsConfig, EVMChainIdConfig,
Eth2ClientConfig, ImOnlineConfig, MaxVestingSchedules, Perbill, RuntimeGenesisConfig,
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn local_mainnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "TNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), 4006.into());
properties.insert("ss58Format".into(), tangle_primitives::MAINNET_SS58_PREFIX.into());

Ok(ChainSpec::from_genesis(
"Local Tangle Mainnet",
Expand All @@ -112,6 +112,9 @@ pub fn local_mainnet_config(chain_id: u64) -> Result<ChainSpec, String> {
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
Expand Down Expand Up @@ -151,7 +154,7 @@ pub fn tangle_mainnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "TNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), 4006.into());
properties.insert("ss58Format".into(), tangle_primitives::MAINNET_SS58_PREFIX.into());

Ok(ChainSpec::from_genesis(
"Tangle Mainnet",
Expand Down
65 changes: 26 additions & 39 deletions node/src/chainspec/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ use sp_runtime::{
};
use std::collections::BTreeMap;
use tangle_crypto_primitives::crypto::AuthorityId as RoleKeyId;
use tangle_primitives::BlockNumber;
use tangle_primitives::types::BlockNumber;
use tangle_testnet_runtime::{
AccountId, BabeConfig, Balance, BalancesConfig, ClaimsConfig, EVMChainIdConfig, EVMConfig,
ElectionsConfig, Eth2ClientConfig, ImOnlineConfig, MaxNominations, MaxVestingSchedules,
Perbill, RuntimeGenesisConfig, SessionConfig, Signature, StakerStatus, StakingConfig,
SudoConfig, SystemConfig, TreasuryPalletId, UNIT, WASM_BINARY,
ElectionsConfig, Eth2ClientConfig, ImOnlineConfig, MaxVestingSchedules, Perbill,
RuntimeGenesisConfig, SessionConfig, Signature, StakerStatus, StakingConfig, SudoConfig,
SystemConfig, TreasuryPalletId, UNIT, WASM_BINARY,
};
use webb_consensus_types::network_config::{Network, NetworkConfig};

Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn local_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "tTNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), 42.into());
properties.insert("ss58Format".into(), tangle_primitives::TESTNET_SS58_PREFIX.into());

Ok(ChainSpec::from_genesis(
// Name
Expand All @@ -106,29 +106,26 @@ pub fn local_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
wasm_binary,
// Initial PoA authorities
vec![
authority_keys_from_seed("Alice//stash"),
authority_keys_from_seed("Bob//stash"),
authority_keys_from_seed("Charlie//stash"),
authority_keys_from_seed("Dave//stash"),
authority_keys_from_seed("Eve//stash"),
authority_keys_from_seed("Alice"),
authority_keys_from_seed("Bob"),
authority_keys_from_seed("Charlie"),
authority_keys_from_seed("Dave"),
authority_keys_from_seed("Eve"),
],
vec![],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
vec![
get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
],
chain_id,
combine_distributions(vec![
develop::get_evm_balance_distribution(),
testnet::get_evm_balance_distribution(),
]),
testnet::get_substrate_balance_distribution(),
vec![],
vec![],
true,
)
},
Expand All @@ -153,12 +150,12 @@ pub fn tangle_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "tTNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), 42.into());
properties.insert("ss58Format".into(), tangle_primitives::TESTNET_SS58_PREFIX.into());

Ok(ChainSpec::from_genesis(
"Tangle Standalone Testnet",
"Tangle Testnet",
"tangle-testnet",
ChainType::Development,
ChainType::Live,
move || {
testnet_genesis(
wasm_binary,
Expand Down Expand Up @@ -225,7 +222,7 @@ pub fn tangle_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AccountId, BabeId, GrandpaId, ImOnlineId, RoleKeyId)>,
initial_nominators: Vec<AccountId>,
_initial_nominators: Vec<AccountId>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
chain_id: u64,
Expand All @@ -237,22 +234,12 @@ fn testnet_genesis(
const STASH: Balance = ENDOWMENT / 100;

// stakers: all validators and nominators.
let mut rng = rand::thread_rng();
let _rng = rand::thread_rng();
// stakers: all validators and nominators.
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), STASH, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
let count = rng.gen::<usize>() % limit;
let nominations = initial_authorities
.as_slice()
.choose_multiple(&mut rng, count)
.map(|choice| choice.0.clone())
.collect::<Vec<_>>();
(x.clone(), x.clone(), STASH, StakerStatus::Nominator(nominations))
}))
.collect::<Vec<_>>();
.map(|x| (x.0.clone(), x.0.clone(), UNIT, StakerStatus::Validator))
.collect();

let num_endowed_accounts = endowed_accounts.len();
let claims: Vec<(MultiAddress, Balance, Option<StatementKind>)> = endowed_accounts
Expand Down
6 changes: 3 additions & 3 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
use futures::TryFutureExt;
use sc_cli::SubstrateCli;
use sc_service::PartialComponents;
use tangle_primitives::Block;
use tangle_primitives::types::Block;

trait IdentifyChain {
fn is_mainnet(&self) -> bool;
Expand Down Expand Up @@ -73,9 +73,9 @@ impl SubstrateCli for Cli {

fn load_spec(&self, id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match id {
"" | "dev" | "local" => Box::new(chainspec::testnet::local_testnet_config(4006)?),
"" | "dev" | "local" => Box::new(chainspec::testnet::local_testnet_config(4007)?),
// generates the spec for testnet
"testnet" => Box::new(chainspec::testnet::tangle_testnet_config(4006)?),
"testnet" => Box::new(chainspec::testnet::tangle_testnet_config(4007)?),
"tangle-testnet" => Box::new(chainspec::testnet::ChainSpec::from_json_bytes(
&include_bytes!("../../chainspecs/testnet/tangle-standalone.json")[..],
)?),
Expand Down
101 changes: 87 additions & 14 deletions node/src/distributions/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.
use std::str::FromStr;
use tangle_primitives::BlockNumber;
use tangle_primitives::types::BlockNumber;
use tangle_runtime::UNIT;

use super::testnet::{get_git_root, read_contents, read_contents_to_evm_accounts};
Expand Down Expand Up @@ -82,6 +82,7 @@ pub const TWO_YEARS_BLOCKS: u64 = (2 * 365 * 24 * 60 * 60 / BLOCK_TIME) as u64;

pub const ONE_HUNDRED_POINTS: u64 = 100;

#[derive(PartialEq, Eq, Debug)]
pub struct DistributionResult {
pub claims: Vec<(MultiAddress, Balance, Option<StatementKind>)>,
pub vesting: Vec<(MultiAddress, Vec<(Balance, Balance, BlockNumber)>)>,
Expand All @@ -98,6 +99,7 @@ fn one_percent_endowment(endowment: u128) -> u128 {
}

fn vesting_per_block(endowment: u128, blocks: u64) -> u128 {
print!("Endowment {:?} Blocks {:?} ", endowment, blocks);
endowment / blocks as u128
}

Expand Down Expand Up @@ -215,24 +217,21 @@ pub fn get_substrate_balance_distribution() -> DistributionResult {
}

pub fn get_investor_balance_distribution() -> Vec<(MultiAddress, u128, u64, u64, u128)> {
// TODO : Read from actual investor file
let investor_accounts: Vec<(MultiAddress, u128)> = vec![];
investor_accounts
.into_iter()
.map(|(address, value)| {
(
address,
value,
ONE_YEAR_BLOCKS,
TWO_YEARS_BLOCKS - ONE_YEAR_BLOCKS,
one_percent_endowment(value),
)
})
.collect()
compute_balance_distribution_with_cliff_and_vesting(investor_accounts)
}

pub fn get_team_balance_distribution() -> Vec<(MultiAddress, u128, u64, u64, u128)> {
// TODO : Read from actual team file
let team_accounts: Vec<(MultiAddress, u128)> = vec![];
team_accounts
compute_balance_distribution_with_cliff_and_vesting(team_accounts)
}

pub fn compute_balance_distribution_with_cliff_and_vesting(
investor_accounts: Vec<(MultiAddress, u128)>,
) -> Vec<(MultiAddress, u128, u64, u64, u128)> {
investor_accounts
.into_iter()
.map(|(address, value)| {
(
Expand Down Expand Up @@ -283,3 +282,77 @@ pub fn get_distribution_for(

DistributionResult { claims, vesting, vesting_length: total_vesting_schedule, vesting_cliff }
}

#[test]
fn test_compute_investor_balance_distribution() {
let alice = MultiAddress::Native(AccountId32::new([0; 32]));
let bob = MultiAddress::Native(AccountId32::new([1; 32]));

let amount_per_investor = 100;

// let compute the expected output
// the expected output is that
// 1% is immedately release
// 1 year cliff (vesting starts after year 1)
// Vesting finishes 1 year after cliff
let alice_expected_response: (MultiAddress, u128, u64, u64, u128) = (
alice.clone(),
amount_per_investor,
tangle_primitives::time::DAYS * 365, // begins at one year after block 0
tangle_primitives::time::DAYS * 365, // num of blocks from beginning till fully vested
1, // 1% of 100
);
let bob_expected_response: (MultiAddress, u128, u64, u64, u128) = (
bob.clone(),
amount_per_investor,
tangle_primitives::time::DAYS * 365, // begins at one year after block 0
tangle_primitives::time::DAYS * 365, // num of blocks from beging till fully vested
1, // 1% of 100
);

assert_eq!(
compute_balance_distribution_with_cliff_and_vesting(vec![
(alice, amount_per_investor),
(bob, amount_per_investor),
]),
vec![alice_expected_response, bob_expected_response]
);
}

#[test]
fn test_get_distribution_for() {
let alice = MultiAddress::Native(AccountId32::new([0; 32]));
let bob = MultiAddress::Native(AccountId32::new([1; 32]));

let amount_per_investor = 100;

// let compute the expected output
// the expected output is that
// 1% is immedately claimable
// 1 month cliff (vesting starts after 1 month) (use 1 for easier calculation)
// at 1 month cliff, release 1/24th rewards
// Vesting finishes after 2 years (use 24 for easier calculation)
// 1/24th claimable at every month
let expected_distibution_result = DistributionResult {
claims: vec![
(alice.clone(), 1, Some(StatementKind::Regular)),
(bob.clone(), 1, Some(StatementKind::Regular)),
],
vesting: vec![
(alice.clone(), vec![(4, 4, 1), (94, 4, 1)]),
(bob.clone(), vec![(4, 4, 1), (94, 4, 1)]),
],
vesting_length: 24,
vesting_cliff: 1,
};

assert_eq!(
get_distribution_for(
vec![(alice, amount_per_investor), (bob, amount_per_investor),],
Some(StatementKind::Regular),
1,
24,
),
expected_distibution_result
);
}
5 changes: 4 additions & 1 deletion node/src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use self::mainnet::DistributionResult;

use pallet_airdrop_claims::{MultiAddress, StatementKind};

use tangle_primitives::{currency::EXISTENTIAL_DEPOSIT, Balance, BlockNumber};
use tangle_primitives::{
currency::EXISTENTIAL_DEPOSIT,
types::{Balance, BlockNumber},
};
pub mod develop;
pub mod mainnet;
pub mod testnet;
Expand Down
2 changes: 1 addition & 1 deletion node/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use fc_consensus::FrontierBlockImport;
use fc_rpc::{EthTask, OverrideHandle};
pub use fc_rpc_core::types::{FeeHistoryCache, FeeHistoryCacheLimit, FilterPool};
// Local
use tangle_testnet_runtime::opaque::Block;
use tangle_primitives::Block;

use crate::service::{FullBackend, FullClient};

Expand Down
4 changes: 2 additions & 2 deletions node/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ use sp_consensus::SelectChain;
use sp_consensus_babe::BabeApi;
use sp_keystore::KeystorePtr;
use sp_runtime::traits::Block as BlockT;
use tangle_primitives::Block;
use tangle_runtime::BlockNumber;

// Runtime
use tangle_runtime::{opaque::Block, AccountId, Balance, Hash, Index};
use tangle_runtime::{AccountId, Balance, Hash, Index};

pub mod eth;
pub mod tracing;
Expand Down
6 changes: 3 additions & 3 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ pub use sc_executor::NativeElseWasmExecutor;
use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sc_transaction_pool_api::OffchainTransactionPoolFactory;

use sp_core::U256;
use tangle_primitives::Block;

use std::{path::Path, sync::Arc, time::Duration};

#[cfg(not(feature = "testnet"))]
use tangle_runtime::{self, opaque::Block, RuntimeApi, TransactionConverter};
use tangle_runtime::{self, RuntimeApi, TransactionConverter};

#[cfg(feature = "testnet")]
use tangle_testnet_runtime::{self, opaque::Block, RuntimeApi, TransactionConverter};
use tangle_testnet_runtime::{self, RuntimeApi, TransactionConverter};

/// The minimum period of blocks on which justifications will be
/// imported and generated.
Expand Down
Loading

0 comments on commit 0622062

Please sign in to comment.