Skip to content

Commit

Permalink
Merge pull request #105 from agoraxyz/I91-benchmarking
Browse files Browse the repository at this point in the history
I91 benchmarking
  • Loading branch information
OnyxSkyscape authored Feb 22, 2023
2 parents dc1e578 + 95cf365 commit f318786
Show file tree
Hide file tree
Showing 34 changed files with 1,710 additions and 419 deletions.
Binary file modified gn-client/artifacts/metadata.scale
Binary file not shown.
76 changes: 67 additions & 9 deletions gn-client/examples/guild/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use sp_keyring::AccountKeyring;
use std::collections::BTreeMap;
use std::sync::Arc;

const RETRIES: u8 = 10;
const SLEEP_DURATION_MS: u64 = 1000;

pub struct Accounts {
pub substrate: Arc<Signer>,
pub eth: LocalWallet,
Expand Down Expand Up @@ -68,23 +71,78 @@ pub async fn prefunded_accounts(
}

#[cfg(not(feature = "external-oracle"))]
pub async fn register_operators(api: Api, accounts: impl Iterator<Item = &Accounts>) {
let register_operator_futures = accounts
.map(|account| {
pub async fn register_operators(
api: Api,
root: Arc<Signer>,
accounts: impl Iterator<Item = &Accounts>,
) {
println!("registring operators");
for (i, account) in accounts.enumerate() {
let payload = tx::register_operator(account.substrate.account_id());
tx::send_tx_in_block(api.clone(), &tx::sudo(payload), Arc::clone(&root))
.await
.unwrap();
println!("\toperator {i} registered");
}

println!("operator registrations in block");
}

#[cfg(not(feature = "external-oracle"))]
pub async fn activate_operators(api: Api, accounts: impl Iterator<Item = &Accounts>) {
println!("activating operators");
let tx_futures = accounts
.map(|acc| {
tx::send_owned_tx(
api.clone(),
tx::register_operator(),
Arc::clone(&account.substrate),
tx::activate_operator(),
Arc::clone(&acc.substrate),
TxStatus::InBlock,
)
})
.collect::<Vec<_>>();

try_join_all(register_operator_futures)
.await
.expect("failed to register operators");
try_join_all(tx_futures).await.unwrap();

println!("operator registrations in block");
println!("operators activated");
}

pub async fn wait_for_active_operator(api: Api) {
let mut i = 0;
loop {
let active_operators = query::active_operators(api.clone())
.await
.expect("failed to fetch active operators");
if active_operators.is_empty() {
i += 1;
println!("waiting for active operators");
if i == RETRIES {
panic!("no active operators found");
}
tokio::time::sleep(std::time::Duration::from_millis(SLEEP_DURATION_MS)).await;
} else {
println!("found an active operator");
break;
}
}
}

pub async fn wait_for_oracle_answers(api: Api) {
let mut i = 0;
loop {
let oracle_requests = query::oracle_requests(api.clone(), PAGE_SIZE)
.await
.expect("failed to fetch oracle requests");
if !oracle_requests.is_empty() {
i += 1;
if i == RETRIES {
panic!("ran out of retries while checking oracle requests")
}
tokio::time::sleep(std::time::Duration::from_millis(SLEEP_DURATION_MS)).await;
} else {
break;
}
}
}

pub async fn create_dummy_guilds(
Expand Down
44 changes: 15 additions & 29 deletions gn-client/examples/guild/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,33 @@ use gn_common::pad::padded_id;
use gn_test_data::*;
use std::sync::Arc;

const RETRIES: u8 = 10;
const SLEEP_DURATION_MS: u64 = 500;

pub async fn join(api: Api, alice: Arc<Signer>) {
let operators = prefunded_accounts(api.clone(), Arc::clone(&alice), N_TEST_ACCOUNTS).await;
pub async fn join(api: Api, root: Arc<Signer>) {
let operators = prefunded_accounts(api.clone(), Arc::clone(&root), N_TEST_ACCOUNTS).await;
#[cfg(not(feature = "external-oracle"))]
{
let registering_operators = operators.values();
register_operators(api.clone(), registering_operators).await;
let registered_operators = query::registered_operators(api.clone())
register_operators(api.clone(), Arc::clone(&root), operators.values()).await;
activate_operators(api.clone(), operators.values()).await;
let active_operators = query::active_operators(api.clone())
.await
.expect("failed to fetch registered operators");
.expect("failed to fetch active operators");

for registered in &registered_operators {
if registered != alice.account_id() {
assert!(operators.get(registered).is_some());
}
for active in &active_operators {
assert!(operators.get(active).is_some());
}
}

#[cfg(not(feature = "external-oracle"))]
{
wait_for_active_operator(api.clone()).await;
}

register_users(api.clone(), &operators).await;

#[cfg(not(feature = "external-oracle"))]
send_dummy_oracle_answers(api.clone(), &operators).await;

// wait for all transactions to be finalized
let mut i = 0;
loop {
let oracle_requests = query::oracle_requests(api.clone(), PAGE_SIZE)
.await
.expect("failed to fetch oracle requests");
if !oracle_requests.is_empty() {
i += 1;
if i == RETRIES {
panic!("ran out of retries while checking oracle requests")
}
tokio::time::sleep(std::time::Duration::from_millis(SLEEP_DURATION_MS)).await;
} else {
break;
}
}
wait_for_oracle_answers(api.clone()).await;

for (i, (id, accounts)) in operators.iter().enumerate() {
let user_identity = query::user_identity(api.clone(), id)
Expand All @@ -63,7 +49,7 @@ pub async fn join(api: Api, alice: Arc<Signer>) {
assert_eq!(user_identity, expected);
}

create_dummy_guilds(api.clone(), alice, operators.values()).await;
create_dummy_guilds(api.clone(), root, operators.values()).await;

join_guilds(api.clone(), &operators).await;

Expand Down
4 changes: 4 additions & 0 deletions gn-client/examples/guild/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod common;
mod join;
mod register;
mod token;

use common::api_with_alice;
Expand All @@ -12,13 +13,15 @@ use std::str::FromStr;
enum Example {
Join,
Token,
Register,
}

impl FromStr for Example {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"join" => Ok(Self::Join),
"register" => Ok(Self::Register),
"token" => Ok(Self::Token),
_ => Err(format!("no example with name {s}")),
}
Expand Down Expand Up @@ -52,6 +55,7 @@ async fn main() {

match opt.example {
Example::Join => join::join(api, alice).await,
Example::Register => register::register(api, alice).await,
Example::Token => token::token(api, alice).await,
}
}
14 changes: 14 additions & 0 deletions gn-client/examples/guild/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use gn_client::{
tx::{self, Signer},
Api,
};

use std::sync::Arc;

pub async fn register(api: Api, root: Arc<Signer>) {
let payload = tx::register_operator(root.account_id());
tx::send_tx_in_block(api, &tx::sudo(payload), root)
.await
.unwrap();
println!("root registered as operator");
}
43 changes: 22 additions & 21 deletions gn-client/examples/guild/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(not(feature = "external-oracle"))]
use crate::common::*;
use ethers::types::{Address, U256};
use gn_client::{
Expand Down Expand Up @@ -29,46 +28,48 @@ const GNOSIS_ERC721_ID_1: &str = "5819774";
const ADDRESS: &str = "e43878ce78934fe8007748ff481f03b8ee3b97de";
const SIGNATURE: &str = "a7d8263c96a8bb689d462b2782a45b81f02777607c27d1b322a1c46910482e274320fbf353a543a1504dc3c0ded9c2930dffc4b15541d97da7b240f40416f12a1b";

pub async fn token(api: Api, alice: Arc<Signer>) {
pub async fn token(api: Api, root: Arc<Signer>) {
let mut signature = [0u8; 65];
hex::decode_to_slice(SIGNATURE, &mut signature).expect("this should not fail");
signature[64] -= 27; // ethereum's eip-115 normalization stuff
let mut address = [0u8; 20];
hex::decode_to_slice(ADDRESS, &mut address).expect("this should not fail");

#[cfg(not(feature = "external-oracle"))]
let operators = prefunded_accounts(api.clone(), Arc::clone(&alice), N_TEST_ACCOUNTS).await;
let operators = prefunded_accounts(api.clone(), Arc::clone(&root), N_TEST_ACCOUNTS).await;

#[cfg(not(feature = "external-oracle"))]
{
let registering_operators = operators.values();
register_operators(api.clone(), registering_operators).await;
let registered_operators = query::registered_operators(api.clone())
register_operators(api.clone(), Arc::clone(&root), operators.values()).await;
activate_operators(api.clone(), operators.values()).await;
let active_operators = query::active_operators(api.clone())
.await
.expect("failed to fetch registered operators");
.expect("failed to fetch active operators");

for registered in &registered_operators {
if registered != alice.account_id() {
assert!(operators.get(registered).is_some());
}
for active in &active_operators {
assert!(operators.get(active).is_some());
}
}

// register alice with test evm address + signature
#[cfg(feature = "external-oracle")]
{
wait_for_active_operator(api.clone()).await;
}
// register root with test evm address + signature
let evm_identity =
IdentityWithAuth::Ecdsa(Identity::Address20(address), EcdsaSignature(signature));

let index = 0;
let tx_payload = tx::register(evm_identity, index);
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to register");

#[cfg(not(feature = "external-oracle"))]
send_dummy_oracle_answers(api.clone(), &operators).await;

loop {
let user_identity = query::user_identity(api.clone(), alice.account_id())
let user_identity = query::user_identity(api.clone(), root.account_id())
.await
.expect("failed to fetch user identities");
if user_identity.len() == 1 {
Expand Down Expand Up @@ -137,28 +138,28 @@ pub async fn token(api: Api, alice: Arc<Signer>) {
};

let tx_payload = tx::create_guild(TOKEN_GUILD, vec![1, 2, 3]);
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to create guild");

println!("GUILD CREATED");

let tx_payload = tx::create_unfiltered_role(TOKEN_GUILD, FIRST_ROLE, first_reqs).unwrap();
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to create guild");

println!("FIRST ROLE CREATED");

let tx_payload = tx::create_unfiltered_role(TOKEN_GUILD, SECOND_ROLE, second_reqs).unwrap();
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to create guild");

println!("SECOND ROLE CREATED");

let tx_payload = tx::join(TOKEN_GUILD, FIRST_ROLE, None);
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to join guild");

Expand All @@ -175,15 +176,15 @@ pub async fn token(api: Api, alice: Arc<Signer>) {
.await
.expect("failed to query members");
if members.len() == 1 {
assert_eq!(members.get(0).unwrap(), alice.account_id());
assert_eq!(members.get(0).unwrap(), root.account_id());
break;
}
}

println!("FIRST_ROLE JOINED");

let tx_payload = tx::join(TOKEN_GUILD, SECOND_ROLE, None);
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&alice))
tx::send_tx_in_block(api.clone(), &tx_payload, Arc::clone(&root))
.await
.expect("failed to join guild");

Expand All @@ -200,7 +201,7 @@ pub async fn token(api: Api, alice: Arc<Signer>) {
.await
.expect("failed to query members");
if members.len() == 1 {
assert_eq!(members.get(0).unwrap(), alice.account_id());
assert_eq!(members.get(0).unwrap(), root.account_id());
break;
}
}
Expand Down
15 changes: 13 additions & 2 deletions gn-client/src/query/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use subxt::storage::address::{StorageHasher, StorageMapKey};

use std::collections::BTreeMap;

pub async fn registered_operators(api: Api) -> Result<Vec<AccountId>, SubxtError> {
let operators = runtime::storage().oracle().operators();
pub async fn active_operators(api: Api) -> Result<Vec<AccountId>, SubxtError> {
let operators = runtime::storage().oracle().active_operators();
Ok(api
.storage()
.at(None)
Expand All @@ -20,6 +20,17 @@ pub async fn registered_operators(api: Api) -> Result<Vec<AccountId>, SubxtError
.unwrap_or_default())
}

pub async fn is_operator_registered(api: Api, id: &AccountId) -> Result<bool, SubxtError> {
let operator = runtime::storage().oracle().registered_operators(id);
Ok(api
.storage()
.at(None)
.await?
.fetch(&operator)
.await?
.is_some())
}

pub async fn user_identity(api: Api, user_id: &AccountId) -> Result<Vec<Identity>, SubxtError> {
let mut i = 0u8;
let mut identities = Vec::new();
Expand Down
Loading

0 comments on commit f318786

Please sign in to comment.