Skip to content

Commit 9090303

Browse files
authored
Update substrate and subxt (#435)
* one test working * fix all tests * lint * remove unwraps * lint * fix the batch sizing * lint * fix some pallet mocks * clean * fix propogation tests * fix all tests * fix constraints pallet * remove commented out assert * remove todo
1 parent 162f494 commit 9090303

File tree

48 files changed

+2567
-2397
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2567
-2397
lines changed

Cargo.lock

Lines changed: 1775 additions & 1342 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cliff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ footer="""
5252
"""
5353
# postprocessors
5454
postprocessors=[
55-
{ pattern='<REPO>', replace="https://github.com/orhun/git-cliff" }, # replace repository URL
55+
{ pattern='<REPO>', replace="https://github.com/entropy/entropy-core" }, # replace repository URL
5656
]
5757

5858
[git]

crypto/constraints/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition="2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
thiserror="1.0.40"
9+
thiserror="1.0.48"
1010

1111
# parsing
1212

crypto/kvdb/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition="2021"
77
# Common
88
rand ={ version="0.8", default-features=false }
99
serde ={ version="1.0", features=["derive"] }
10-
thiserror="1.0.40"
10+
thiserror="1.0.48"
1111
hex ="0.4"
1212

1313
# Substrate

crypto/protocol/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ entropy-shared ={ path="../shared", default-features=false }
1212
synedrion ={ git="ssh://[email protected]/entropyxyz/synedrion.git", tag="v0.0.9" }
1313
serde ={ version="1.0", features=["derive"], default-features=false }
1414
serde-big-array="0.5.1"
15-
subxt ={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.29.0", default-features=false }
15+
subxt ={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.32.1", default-features=false }
1616
subxt-signer ={ version="0.31.0", features=[] }
1717
tokio ={ version="1.16", features=["sync", "rt", "macros"] }
1818
x25519-dalek ="2.0.0-pre.1"
1919
futures ="0.3"
2020
hex ="*"
2121
blake2 ="0.10.4"
22-
thiserror ="1.0.40"
22+
thiserror ="1.0.48"
2323
snow ="0.9.2"
2424
getrandom ={ version="0.2", features=["js"] }
2525
rand_core ={ version="0.6.4", features=["getrandom"] }

crypto/server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ reqwest ={ version="0.11", features=["json", "stream"] }
3030
axum ={ version="0.6.18", features=["ws"] }
3131
axum-macros="0.3.7"
3232
# Substrate
33-
subxt ={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.29.0" }
33+
subxt ={ package="subxt", git="https://github.com/paritytech/subxt.git", tag="v0.32.1" }
3434
parity-scale-codec="3.4.0"
3535
sp-core ={ version="21.0.0", default-features=false }
3636

crypto/server/src/chain_api.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![allow(clippy::all)]
2-
pub use subxt::config::PolkadotConfig as EntropyConfig;
3-
use subxt::OnlineClient;
2+
pub use subxt::PolkadotConfig as EntropyConfig;
3+
use subxt::{
4+
backend::{legacy::LegacyRpcMethods, rpc::RpcClient},
5+
OnlineClient,
6+
};
47

58
#[subxt::subxt(
69
runtime_metadata_path = "entropy_metadata.scale",
@@ -21,3 +24,11 @@ pub async fn get_api(url: &str) -> Result<OnlineClient<EntropyConfig>, subxt::Er
2124
let api = OnlineClient::<EntropyConfig>::from_url(url).await?;
2225
Ok(api)
2326
}
27+
28+
/// Creates a rpc instance to talk to chain
29+
/// Chain endpoint set on launch
30+
pub async fn get_rpc(url: &str) -> Result<LegacyRpcMethods<EntropyConfig>, subxt::Error> {
31+
let rpc_client = RpcClient::from_url(url).await?;
32+
let rpc_methods = LegacyRpcMethods::<EntropyConfig>::new(rpc_client);
33+
Ok(rpc_methods)
34+
}

crypto/server/src/helpers/substrate.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use entropy_shared::{KeyVisibility, SIGNING_PARTY_SIZE};
2-
use subxt::{ext::sp_core::sr25519, tx::PairSigner, utils::AccountId32, Config, OnlineClient};
2+
use subxt::{
3+
backend::legacy::LegacyRpcMethods, ext::sp_core::sr25519, tx::PairSigner, utils::AccountId32,
4+
Config, OnlineClient,
5+
};
36

47
use crate::{
58
chain_api::{entropy, EntropyConfig},
@@ -9,16 +12,21 @@ use crate::{
912
/// gets the subgroup of the working validator
1013
pub async fn get_subgroup(
1114
api: &OnlineClient<EntropyConfig>,
15+
rpc: &LegacyRpcMethods<EntropyConfig>,
1216
signer: &PairSigner<EntropyConfig, sr25519::Pair>,
1317
) -> Result<(Option<u8>, AccountId32), UserErr> {
1418
let mut subgroup: Option<u8> = None;
1519
let threshold_address = signer.account_id();
1620
let stash_address_query =
1721
entropy::storage().staking_extension().threshold_to_stash(threshold_address);
22+
let block_hash = rpc
23+
.chain_get_block_hash(None)
24+
.await?
25+
.ok_or_else(|| UserErr::OptionUnwrapError("Errir getting block hash"))?;
26+
1827
let stash_address = api
1928
.storage()
20-
.at_latest()
21-
.await?
29+
.at(block_hash)
2230
.fetch(&stash_address_query)
2331
.await?
2432
.ok_or_else(|| UserErr::SubgroupError("Stash Fetch Error"))?;
@@ -27,8 +35,7 @@ pub async fn get_subgroup(
2735
entropy::storage().staking_extension().signing_groups(i as u8);
2836
let signing_group_addresses = api
2937
.storage()
30-
.at_latest()
31-
.await?
38+
.at(block_hash)
3239
.fetch(&signing_group_addresses_query)
3340
.await?
3441
.ok_or_else(|| UserErr::SubgroupError("Subgroup Error"))?;
@@ -43,13 +50,17 @@ pub async fn get_subgroup(
4350
/// Returns all the addresses of a specific subgroup
4451
pub async fn return_all_addresses_of_subgroup(
4552
api: &OnlineClient<EntropyConfig>,
53+
rpc: &LegacyRpcMethods<EntropyConfig>,
4654
subgroup: u8,
4755
) -> Result<Vec<AccountId32>, UserErr> {
4856
let subgroup_addresses_query = entropy::storage().staking_extension().signing_groups(subgroup);
57+
let block_hash = rpc
58+
.chain_get_block_hash(None)
59+
.await?
60+
.ok_or_else(|| UserErr::OptionUnwrapError("Errir getting block hash"))?;
4961
let subgroup_addresses = api
5062
.storage()
51-
.at_latest()
52-
.await?
63+
.at(block_hash)
5364
.fetch(&subgroup_addresses_query)
5465
.await?
5566
.ok_or_else(|| UserErr::SubgroupError("Subgroup Error"))?;
@@ -59,14 +70,18 @@ pub async fn return_all_addresses_of_subgroup(
5970
/// Queries the user's program from the chain
6071
pub async fn get_program(
6172
substrate_api: &OnlineClient<EntropyConfig>,
73+
rpc: &LegacyRpcMethods<EntropyConfig>,
6274
sig_req_account: &<EntropyConfig as Config>::AccountId,
6375
) -> Result<Vec<u8>, UserErr> {
6476
let bytecode_address = entropy::storage().constraints().bytecode(sig_req_account);
77+
let block_hash = rpc
78+
.chain_get_block_hash(None)
79+
.await?
80+
.ok_or_else(|| UserErr::OptionUnwrapError("Errir getting block hash"))?;
6581

6682
substrate_api
6783
.storage()
68-
.at_latest()
69-
.await?
84+
.at(block_hash)
7085
.fetch(&bytecode_address)
7186
.await?
7287
.ok_or(UserErr::NoProgramDefined)
@@ -77,6 +92,7 @@ pub async fn get_program(
7792
#[cfg(test)]
7893
pub async fn make_register(
7994
api: &OnlineClient<EntropyConfig>,
95+
rpc: &LegacyRpcMethods<EntropyConfig>,
8096
sig_req_keyring: sr25519::Pair,
8197
constraint_account: &AccountId32,
8298
key_visibility: KeyVisibility,
@@ -86,8 +102,8 @@ pub async fn make_register(
86102
let sig_req_account = PairSigner::<EntropyConfig, sr25519::Pair>::new(sig_req_keyring);
87103

88104
let registering_query = entropy::storage().relayer().registering(sig_req_account.account_id());
89-
let is_registering_1 =
90-
api.storage().at_latest().await.unwrap().fetch(&registering_query).await.unwrap();
105+
let block_hash = rpc.chain_get_block_hash(None).await.unwrap().unwrap();
106+
let is_registering_1 = api.storage().at(block_hash).fetch(&registering_query).await.unwrap();
91107
assert!(is_registering_1.is_none());
92108

93109
// register the user
@@ -109,21 +125,26 @@ pub async fn make_register(
109125
.await
110126
.unwrap();
111127

112-
let query_registering_status =
113-
api.storage().at_latest().await.unwrap().fetch(&registering_query).await;
128+
let block_hash_2 = rpc.chain_get_block_hash(None).await.unwrap().unwrap();
129+
130+
let query_registering_status = api.storage().at(block_hash_2).fetch(&registering_query).await;
114131
assert!(query_registering_status.unwrap().unwrap().is_registering);
115132
}
116133

117134
/// Returns wether an account is registered
118135
pub async fn get_key_visibility(
119136
api: &OnlineClient<EntropyConfig>,
137+
rpc: &LegacyRpcMethods<EntropyConfig>,
120138
who: &<EntropyConfig as Config>::AccountId,
121139
) -> Result<KeyVisibility, UserErr> {
122140
let registered_info_query = entropy::storage().relayer().registered(who);
141+
let block_hash = rpc
142+
.chain_get_block_hash(None)
143+
.await?
144+
.ok_or_else(|| UserErr::OptionUnwrapError("Errir getting block hash"))?;
123145
let result = api
124146
.storage()
125-
.at_latest()
126-
.await?
147+
.at(block_hash)
127148
.fetch(&registered_info_query)
128149
.await?
129150
.ok_or_else(|| UserErr::NotRegistering("Register Onchain first"))?;

crypto/server/src/helpers/tests.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rand_core::OsRng;
1111
use serial_test::serial;
1212
use sp_keyring::AccountKeyring;
1313
use subxt::{
14+
backend::legacy::LegacyRpcMethods,
1415
ext::sp_core::{sr25519, Pair},
1516
tx::PairSigner,
1617
utils::{AccountId32 as SubxtAccountId32, Static},
@@ -21,7 +22,7 @@ use testing_utils::substrate_context::testing_context;
2122

2223
use crate::{
2324
app,
24-
chain_api::{entropy, get_api, EntropyConfig},
25+
chain_api::{entropy, get_api, get_rpc, EntropyConfig},
2526
get_signer,
2627
helpers::{
2728
launch::{
@@ -172,15 +173,20 @@ pub async fn update_programs(
172173
}
173174

174175
/// Verify that a Registering account has all confirmation, and that it is registered.
175-
pub async fn check_if_confirmation(api: &OnlineClient<EntropyConfig>, key: &sr25519::Pair) {
176+
pub async fn check_if_confirmation(
177+
api: &OnlineClient<EntropyConfig>,
178+
rpc: &LegacyRpcMethods<EntropyConfig>,
179+
key: &sr25519::Pair,
180+
) {
176181
let signer = PairSigner::<EntropyConfig, sr25519::Pair>::new(key.clone());
177182
let registering_query = entropy::storage().relayer().registering(signer.account_id());
178183
let registered_query = entropy::storage().relayer().registered(signer.account_id());
179-
let is_registering = api.storage().at_latest().await.unwrap().fetch(&registering_query).await;
184+
let block_hash = rpc.chain_get_block_hash(None).await.unwrap().unwrap();
185+
let is_registering = api.storage().at(block_hash.clone()).fetch(&registering_query).await;
180186
// cleared from is_registering state
181187
assert!(is_registering.unwrap().is_none());
182188
let is_registered =
183-
api.storage().at_latest().await.unwrap().fetch(&registered_query).await.unwrap();
189+
api.storage().at(block_hash.clone()).fetch(&registered_query).await.unwrap();
184190
assert_eq!(is_registered.as_ref().unwrap().verifying_key.0.len(), 33usize);
185191
assert_eq!(is_registered.unwrap().key_visibility, Static(KeyVisibility::Public));
186192
}
@@ -192,19 +198,21 @@ async fn test_get_signing_group() {
192198
let cxt = testing_context().await;
193199
setup_client().await;
194200
let api = get_api(&cxt.node_proc.ws_url).await.unwrap();
201+
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();
202+
195203
let p_alice = <sr25519::Pair as Pair>::from_string(DEFAULT_MNEMONIC, None).unwrap();
196204
let signer_alice = PairSigner::<EntropyConfig, sr25519::Pair>::new(p_alice);
197-
let result_alice = get_subgroup(&api, &signer_alice).await.unwrap().0;
205+
let result_alice = get_subgroup(&api, &rpc, &signer_alice).await.unwrap().0;
198206
assert_eq!(result_alice, Some(0));
199207

200208
let p_bob = <sr25519::Pair as Pair>::from_string(DEFAULT_BOB_MNEMONIC, None).unwrap();
201209
let signer_bob = PairSigner::<EntropyConfig, sr25519::Pair>::new(p_bob);
202-
let result_bob = get_subgroup(&api, &signer_bob).await.unwrap().0;
210+
let result_bob = get_subgroup(&api, &rpc, &signer_bob).await.unwrap().0;
203211
assert_eq!(result_bob, Some(1));
204212

205213
let p_charlie = <sr25519::Pair as Pair>::from_string("//Charlie//stash", None).unwrap();
206214
let signer_charlie = PairSigner::<EntropyConfig, sr25519::Pair>::new(p_charlie);
207-
let result_charlie = get_subgroup(&api, &signer_charlie).await;
215+
let result_charlie = get_subgroup(&api, &rpc, &signer_charlie).await;
208216
assert!(result_charlie.is_err());
209217

210218
clean_tests();

crypto/server/src/helpers/user.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use entropy_shared::{KeyVisibility, SETUP_TIMEOUT_SECONDS};
88
use parity_scale_codec::Encode;
99
use sp_core::crypto::AccountId32;
1010
use subxt::{
11+
backend::legacy::LegacyRpcMethods,
1112
ext::sp_core::{sr25519, Bytes},
1213
tx::PairSigner,
1314
utils::AccountId32 as SubxtAccountId32,
@@ -97,6 +98,7 @@ pub async fn do_dkg(
9798
/// Send's user key share to other members of signing subgroup
9899
pub async fn send_key(
99100
api: &OnlineClient<EntropyConfig>,
101+
rpc: &LegacyRpcMethods<EntropyConfig>,
100102
stash_address: &SubxtAccountId32,
101103
addresses_in_subgroup: &mut Vec<SubxtAccountId32>,
102104
user_registration_info: UserRegistrationInfo,
@@ -108,12 +110,16 @@ pub async fn send_key(
108110
.position(|address| *address == *stash_address)
109111
.ok_or_else(|| UserErr::OptionUnwrapError("Validator not in subgroup"))?,
110112
);
113+
let block_hash = rpc
114+
.chain_get_block_hash(None)
115+
.await?
116+
.ok_or_else(|| UserErr::OptionUnwrapError("Errir getting block hash"))?;
117+
111118
for validator in addresses_in_subgroup {
112119
let server_info_query = entropy::storage().staking_extension().threshold_servers(validator);
113120
let server_info = api
114121
.storage()
115-
.at_latest()
116-
.await?
122+
.at(block_hash)
117123
.fetch(&server_info_query)
118124
.await?
119125
.ok_or_else(|| UserErr::OptionUnwrapError("Server Info Fetch Error"))?;

crypto/server/src/main.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ use tracing::Level;
115115
use validator::api::get_random_server_info;
116116

117117
use self::{
118-
chain_api::get_api,
118+
chain_api::{get_api, get_rpc},
119119
signing_client::{api::*, ListenerState},
120120
user::api::*,
121121
};
@@ -164,11 +164,12 @@ async fn main() {
164164
// Below deals with syncing the kvdb
165165
if args.sync {
166166
let api = get_api(&configuration.endpoint).await.expect("Issue acquiring chain API");
167+
let rpc = get_rpc(&configuration.endpoint).await.expect("Issue acquiring chain RPC");
167168
let mut is_syncing = true;
168169
let sleep_time = Duration::from_secs(20);
169170
// wait for chain to be fully synced before starting key swap
170171
while is_syncing {
171-
let health = api.rpc().system_health().await.expect("Issue checking chain health");
172+
let health = rpc.system_health().await.expect("Issue checking chain health");
172173
is_syncing = health.is_syncing;
173174
if is_syncing {
174175
println!("chain syncing, retrying {is_syncing:?}");
@@ -177,30 +178,34 @@ async fn main() {
177178
}
178179
// TODO: find a proper batch size
179180
let batch_size = 10;
180-
let key_amount = 10_000;
181181
let signer = get_signer(&kv_store).await.expect("Issue acquiring threshold signer key");
182-
let has_fee_balance = check_balance_for_fees(&api, signer.account_id(), MIN_BALANCE)
182+
let has_fee_balance = check_balance_for_fees(&api, &rpc, signer.account_id(), MIN_BALANCE)
183183
.await
184184
.expect("Issue checking chain for signer balance");
185185
if !has_fee_balance {
186186
panic!("threshold account needs balance: {:?}", signer.account_id());
187187
}
188188
// if not in subgroup retry until you are
189-
let mut my_subgroup = get_subgroup(&api, &signer).await;
189+
let mut my_subgroup = get_subgroup(&api, &rpc, &signer).await;
190190
while my_subgroup.is_err() {
191191
println!("you are not currently a validator, retrying");
192192
thread::sleep(sleep_time);
193-
my_subgroup = Ok(get_subgroup(&api, &signer).await.expect("Failed to get subgroup."));
193+
my_subgroup =
194+
Ok(get_subgroup(&api, &rpc, &signer).await.expect("Failed to get subgroup."));
194195
}
195196
let (sbgrp, validator_stash) = my_subgroup.expect("Failed to get subgroup.");
196-
let key_server_info =
197-
get_random_server_info(&api, sbgrp.expect("failed to get subgroup"), validator_stash)
198-
.await
199-
.expect("Issue getting registered keys from chain.");
197+
let key_server_info = get_random_server_info(
198+
&api,
199+
&rpc,
200+
sbgrp.expect("failed to get subgroup"),
201+
validator_stash,
202+
)
203+
.await
204+
.expect("Issue getting registered keys from chain.");
200205
let ip_address =
201206
String::from_utf8(key_server_info.endpoint).expect("failed to parse IP address.");
202207
let recip_key = x25519_dalek::PublicKey::from(key_server_info.x25519_public_key);
203-
let all_keys = get_all_keys(&api, key_amount).await.expect("failed to get all keys.");
208+
let all_keys = get_all_keys(&api, &rpc).await.expect("failed to get all keys.");
204209
let _ = get_and_store_values(
205210
all_keys, &kv_store, ip_address, batch_size, args.dev, &recip_key, &signer,
206211
)

0 commit comments

Comments
 (0)