Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETF MILESTONE 2 #1

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
907 changes: 744 additions & 163 deletions Cargo.lock

Large diffs are not rendered by default.

898 changes: 898 additions & 0 deletions chainspec.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,8 @@ sp_api::impl_runtime_apis! {
.map(|p| p.encode())
.map(beefy_primitives::OpaqueKeyOwnershipProof::new)
}

fn read_share(_: u8) -> Option<Vec<u8>> { None }
}

#[api_version(2)]
Expand Down
3 changes: 3 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,9 @@ sp_api::impl_runtime_apis! {
.map(|p| p.encode())
.map(beefy_primitives::OpaqueKeyOwnershipProof::new)
}

fn read_share(_: u8) -> Option<Vec<u8>> { None }

}

impl mmr::MmrApi<Block, Hash, BlockNumber> for Runtime {
Expand Down
14 changes: 11 additions & 3 deletions substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ rand = "0.8"
# primitives
sp-authority-discovery = { path = "../../../primitives/authority-discovery" }
sp-consensus-babe = { path = "../../../primitives/consensus/babe" }
beefy-primitives = { package = "sp-consensus-beefy", path = "../../../primitives/consensus/beefy" }
beefy-primitives = { package = "sp-consensus-beefy", path = "../../../primitives/consensus/beefy", features = ["bls-experimental"]}
grandpa-primitives = { package = "sp-consensus-grandpa", path = "../../../primitives/consensus/grandpa" }
sp-api = { path = "../../../primitives/api" }
sp-core = { path = "../../../primitives/core" }
Expand Down Expand Up @@ -81,7 +81,7 @@ sc-network-sync = { path = "../../../client/network/sync" }
sc-network-statement = { path = "../../../client/network/statement" }
sc-consensus-slots = { path = "../../../client/consensus/slots" }
sc-consensus-babe = { path = "../../../client/consensus/babe" }
beefy = { package = "sc-consensus-beefy", path = "../../../client/consensus/beefy" }
beefy = { package = "sc-consensus-beefy", path = "../../../client/consensus/beefy", features = ["bls-experimental"]}
grandpa = { package = "sc-consensus-grandpa", path = "../../../client/consensus/grandpa" }
mmr-gadget = { path = "../../../client/merkle-mountain-range" }
sc-rpc = { path = "../../../client/rpc" }
Expand All @@ -107,7 +107,7 @@ pallet-im-online = { path = "../../../frame/im-online", default-features = false
pallet-skip-feeless-payment = { path = "../../../frame/transaction-payment/skip-feeless-payment", default-features = false }

# node-specific dependencies
kitchensink-runtime = { path = "../runtime" }
kitchensink-runtime = { path = "../runtime", features = ["etf"]}
node-rpc = { path = "../rpc" }
node-primitives = { path = "../primitives" }

Expand All @@ -118,6 +118,14 @@ node-inspect = { package = "staging-node-inspect", path = "../inspect", optional
try-runtime-cli = { path = "../../../utils/frame/try-runtime/cli", optional = true }
serde_json = { workspace = true, default-features = true }

# etf dependencies
etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk.git", branch = "dpss-noscale" }
ark-std = "0.4.0"
ark-serialize = "0.4.0"
ark-bls12-377 = { version = "0.4.0", features = ["curve"] }
sp-application-crypto = { path = "../../../primitives/application-crypto" }
w3f-bls = { path = "../../../../../bls" }

[dev-dependencies]
sc-keystore = { path = "../../../client/keystore" }
sc-client-db = { path = "../../../client/db" }
Expand Down
98 changes: 92 additions & 6 deletions substrate/bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//! Substrate chain configurations.

use beefy_primitives::ecdsa_crypto::AuthorityId as BeefyId;
use beefy_primitives::bls_crypto::{AuthorityId as BeefyId, Public as BeefyPublic};
use grandpa_primitives::AuthorityId as GrandpaId;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, Block, MaxNominations, SessionKeys, StakerStatus,
Expand All @@ -33,10 +33,22 @@ use sp_consensus_babe::AuthorityId as BabeId;
use sp_core::{crypto::UncheckedInto, sr25519, Pair, Public};
use sp_mixnet::types::AuthorityId as MixnetId;
use sp_runtime::{
traits::{IdentifyAccount, Verify},
traits::{IdentifyAccount, Verify, One},
Perbill,
RuntimeAppPublic,
};

use w3f_bls::{
single_pop_aggregator::SignatureAggregatorAssumingPoP, DoublePublicKeyScheme, EngineBLS, Keypair, Message, PublicKey, PublicKeyInSignatureGroup, Signed, TinyBLS, TinyBLS377,
};


use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::UniformRand;

use rand::rngs::OsRng;
use etf_crypto_primitives::dpss::acss::HighThresholdACSS;

pub use kitchensink_runtime::RuntimeGenesisConfig;
pub use node_primitives::{AccountId, Balance, Signature};

Expand Down Expand Up @@ -104,7 +116,9 @@ fn configure_accounts_for_staging_testnet() -> (
//
// and
//
// for i in 1 2 3 4 ; do for j in session; do subkey inspect --scheme ecdsa "$secret"//fir//$j//$i; done; done
// TODO: This functionality doesn't exist yet but it would be nice to have...
// we can add this but we need to modify the sc_cli arg_enums to allow for the bls377 arg
// for i in 1 2 3 4 ; do for j in session; do subkey inspect --scheme bls377 "$secret"//fir//$j//$i; done; done

let initial_authorities: Vec<(
AccountId,
Expand Down Expand Up @@ -244,6 +258,12 @@ pub fn staging_testnet_config() -> ChainSpec {
.build()
}

/// Helper function to generate a crypto pair from seed.
pub fn get_pair_from_seed<TPublic: Public>(seed: &str) -> TPublic::Pair {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
}

/// Helper function to generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
Expand All @@ -259,6 +279,15 @@ where
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

/// Helper function to generate an account ID from seed.
pub fn get_account_id_from_raw<TPublic: Public>(seed: &str) -> AccountId
where
AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}


/// Helper function to generate stash, controller and session key from seed.
pub fn authority_keys_from_seed(
seed: &str,
Expand Down Expand Up @@ -304,6 +333,7 @@ fn configure_accounts(
Vec<AccountId>,
usize,
Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
Vec<(BeefyId, BeefyId, Vec<u8>)>
) {
let mut endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| {
vec![
Expand Down Expand Up @@ -353,7 +383,54 @@ fn configure_accounts(

let num_endowed_accounts = endowed_accounts.len();

(initial_authorities, endowed_accounts, num_endowed_accounts, stakers)
let genesis_shares = etf_genesis(
initial_authorities.iter().map(|x| x.7.clone()).collect::<Vec<_>>(),
vec!["Alice", "Bob"],
);
(initial_authorities, endowed_accounts, num_endowed_accounts, stakers, genesis_shares)
}

/// Helper function to prepare initial secrets and resharing for ETF conensus
/// return a vec of (authority id, resharing, pubkey commitment)
pub fn etf_genesis(
initial_authorities: Vec<BeefyId>,
seeds: Vec<&str>
) -> Vec<(BeefyId, BeefyId, Vec<u8>)> {
let msk = ark_bls12_377::Fr::rand(&mut OsRng);
let msk_prime = ark_bls12_377::Fr::rand(&mut OsRng);

let genesis_resharing = HighThresholdACSS::reshare(
msk,
msk_prime,
&initial_authorities.iter().map(|authority| {
// NO: that's 144 bytes, we only want the first 48 of them (48 + 96 bytes for both keypairs)
ark_bls12_377::G1Projective::deserialize_compressed(
&authority.to_raw_vec()[..48]
).unwrap()
}).collect::<Vec<_>>(),
initial_authorities.len() as u8,
&mut OsRng,
);

initial_authorities.iter().enumerate().map(|(idx, auth)| {
let pok = &genesis_resharing[idx].1;
let mut bytes = Vec::new();
pok.serialize_compressed(&mut bytes).unwrap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to unwrap this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do. It could be an expect instead, but it's fine if this panics since it's a critical failure if the pok can't be serialized on genesis anyway.


let seed = seeds[idx];
// let alice_secret = w3f_bls::SecretKey::<TinyBLS377>::from_seed(format!("//{}", seed.clomne));
// let alice_public = alice_secret.into_public();

// let mut alice_kp = w3f_bls::Keypair {
// secret: alice_secret,
// public: alice_public
// };
let test = get_pair_from_seed::<BeefyId>(seed);
let t = sp_core::bls::Pair::<TinyBLS377>::from(test);
let o = t.acss_recover(&bytes).expect("genesis shares should be well formatted");
let etf_id = BeefyId::from(o.public());
(auth.clone(), etf_id, bytes)
}).collect::<Vec<_>>()
}

/// Helper function to create RuntimeGenesisConfig json patch for testing.
Expand All @@ -372,7 +449,7 @@ pub fn testnet_genesis(
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
) -> serde_json::Value {
let (initial_authorities, endowed_accounts, num_endowed_accounts, stakers) =
let (initial_authorities, endowed_accounts, num_endowed_accounts, stakers, genesis_shares) =
configure_accounts(initial_authorities, initial_nominators, endowed_accounts, STASH);

serde_json::json!({
Expand Down Expand Up @@ -424,6 +501,11 @@ pub fn testnet_genesis(
"babe": {
"epochConfig": Some(kitchensink_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
"beefy": {
"authorities": Vec::<BeefyId>::new(),
"genesisBlock": Some(1),
"genesisResharing": genesis_shares,
},
"society": { "pot": 0 },
"assets": {
// This asset is used by the NIS pallet as counterpart currency.
Expand All @@ -438,7 +520,11 @@ pub fn testnet_genesis(

fn development_config_genesis_json() -> serde_json::Value {
testnet_genesis(
vec![authority_keys_from_seed("Alice")],
vec![
authority_keys_from_seed("Alice"),
// authority_keys_from_seed("Bob"),
// authority_keys_from_seed("Charlie")
Comment on lines +529 to +530
Copy link

@juangirini juangirini Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// authority_keys_from_seed("Bob"),
// authority_keys_from_seed("Charlie")

],
vec![],
get_account_id_from_seed::<sr25519::Public>("Alice"),
None,
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sc-chain-spec = { path = "../../../client/chain-spec" }
sc-client-api = { path = "../../../client/api" }
sc-consensus-babe = { path = "../../../client/consensus/babe" }
sc-consensus-babe-rpc = { path = "../../../client/consensus/babe/rpc" }
sc-consensus-beefy = { path = "../../../client/consensus/beefy" }
sc-consensus-beefy = { path = "../../../client/consensus/beefy", features = ["bls-experimental", "etf"]}
sc-consensus-beefy-rpc = { path = "../../../client/consensus/beefy/rpc" }
sc-consensus-grandpa = { path = "../../../client/consensus/grandpa" }
sc-consensus-grandpa-rpc = { path = "../../../client/consensus/grandpa/rpc" }
Expand Down
4 changes: 3 additions & 1 deletion substrate/bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ primitive-types = { version = "0.12.0", default-features = false, features = ["c
# primitives
sp-authority-discovery = { path = "../../../primitives/authority-discovery", default-features = false, features = ["serde"] }
sp-consensus-babe = { path = "../../../primitives/consensus/babe", default-features = false, features = ["serde"] }
sp-consensus-beefy = { path = "../../../primitives/consensus/beefy", default-features = false }
sp-consensus-beefy = { path = "../../../primitives/consensus/beefy", default-features = false, features = ["bls-experimental", "etf"]}
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa", default-features = false, features = ["serde"] }
sp-block-builder = { path = "../../../primitives/block-builder", default-features = false }
sp-genesis-builder = { default-features = false, path = "../../../primitives/genesis-builder" }
Expand Down Expand Up @@ -428,3 +428,5 @@ experimental = [
"frame-system/experimental",
"pallet-example-tasks/experimental",
]

etf = []
36 changes: 34 additions & 2 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use pallet_tx_pause::RuntimeCallNameOf;
use sp_api::impl_runtime_apis;
use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
use sp_consensus_beefy::{
ecdsa_crypto::{AuthorityId as BeefyId, Signature as BeefySignature},
bls_crypto::{AuthorityId as BeefyId, Signature as BeefySignature},
mmr::MmrLeafVersion,
};
use sp_consensus_grandpa::AuthorityId as GrandpaId;
Expand Down Expand Up @@ -1599,7 +1599,7 @@ parameter_types! {

impl pallet_beefy_mmr::Config for Runtime {
type LeafVersion = LeafVersion;
type BeefyAuthorityToMerkleLeaf = pallet_beefy_mmr::BeefyEcdsaToEthereum;
type BeefyAuthorityToMerkleLeaf = pallet_beefy_mmr::BeefyBlsToEthereum;
type LeafExtra = Vec<u8>;
type BeefyDataProvider = ();
}
Expand Down Expand Up @@ -3011,6 +3011,38 @@ impl_runtime_apis! {
.map(|p| p.encode())
.map(sp_consensus_beefy::OpaqueKeyOwnershipProof::new)
}

fn read_share(who: BeefyId) -> Option<Vec<u8>> {
let authorities = pallet_beefy::Authorities::<Runtime>::get();
if let Some(at) = authorities.iter().position(|auth| auth.eq(&who)) {
let shares = pallet_beefy::Shares::<Runtime>::get();
if at as usize >= shares.len() {
return None;
}
return Some(shares[at as usize].clone().into_inner());
}
None
}

fn read_commitment(who: BeefyId) -> Option<BeefyId> {

let authorities = pallet_beefy::Authorities::<Runtime>::get();
if let Some(at) = authorities.iter().position(|auth| auth.eq(&who)) {
let commitments = pallet_beefy::Commitments::<Runtime>::get();
if at as usize >= commitments.len() {
return None;
}
return Some(commitments[at as usize].clone());
}
None


// let commitments = pallet_beefy::Commitments::<Runtime>::get();
// if at as usize >= commitments.len() {
// return None;
// }
// Some(commitments[at as usize].clone().into_inner())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// let commitments = pallet_beefy::Commitments::<Runtime>::get();
// if at as usize >= commitments.len() {
// return None;
// }
// Some(commitments[at as usize].clone().into_inner())

}
}

impl pallet_mmr::primitives::MmrApi<
Expand Down
10 changes: 9 additions & 1 deletion substrate/client/consensus/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ sp-consensus = { path = "../../../primitives/consensus/common" }
sp-consensus-beefy = { path = "../../../primitives/consensus/beefy" }
sp-core = { path = "../../../primitives/core" }
sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" }
sp-keystore = { path = "../../../primitives/keystore" }
sp-keystore = { path = "../../../primitives/keystore", features = ["bls-experimental", "etf"]}
sp-mmr-primitives = { path = "../../../primitives/merkle-mountain-range" }
sp-runtime = { path = "../../../primitives/runtime" }
tokio = "1.22.0"
# etf-crypto-primitives = { path = "../../../../../etf-sdk/etf-crypto-primitives" }
etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk.git", branch = "dpss-noscale" }
ark-serialize = { version = "0.4.0" }
ark-bls12-377 = { version = "0.4.0", features = ["curve"], optional = true}
w3f-bls = { path = "../../../../../bls" }


[dev-dependencies]
Expand All @@ -60,4 +65,7 @@ bls-experimental = [
"sp-application-crypto/bls-experimental",
"sp-consensus-beefy/bls-experimental",
"sp-core/bls-experimental",
"ark-bls12-377",
]

etf = []
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ log = { workspace = true, default-features = true }
parking_lot = "0.12.1"
serde = { features = ["derive"], workspace = true, default-features = true }
thiserror = { workspace = true }
sc-consensus-beefy = { path = ".." }
sc-consensus-beefy = { path = "..", features = ["bls-experimental", "etf"] }
sp-consensus-beefy = { path = "../../../../primitives/consensus/beefy" }
sc-rpc = { path = "../../../rpc" }
sp-core = { path = "../../../../primitives/core" }
Expand Down
Loading
Loading