Skip to content

Commit

Permalink
error handling, cleanup, remove extra logs
Browse files Browse the repository at this point in the history
  • Loading branch information
driemworks committed Apr 18, 2024
1 parent 0a44c36 commit dda2a07
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 174 deletions.
7 changes: 4 additions & 3 deletions substrate/bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", features = ["etf"]}
kitchensink-runtime = { path = "../runtime" }
node-rpc = { path = "../rpc" }
node-primitives = { path = "../primitives" }

Expand All @@ -119,12 +119,13 @@ try-runtime-cli = { path = "../../../utils/frame/try-runtime/cli", optional = tr
serde_json = { workspace = true, default-features = true }

# etf dependencies
etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk.git", branch = "dpss-noscale" }
etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk.git", branch = "w3fbls-migration" }
ark-ec = "0.4.0"
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" }
w3f-bls = "0.1.3"

[dev-dependencies]
sc-keystore = { path = "../../../client/keystore" }
Expand Down
76 changes: 39 additions & 37 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::bls_crypto::{AuthorityId as BeefyId, Public as BeefyPublic};
use beefy_primitives::bls_crypto::AuthorityId as BeefyId;
use grandpa_primitives::AuthorityId as GrandpaId;
use kitchensink_runtime::{
constants::currency::*, wasm_binary_unwrap, Block, MaxNominations, SessionKeys, StakerStatus,
Expand All @@ -33,21 +33,20 @@ 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, One},
traits::{IdentifyAccount, Verify},
Perbill,
RuntimeAppPublic,
};

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


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

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

pub use kitchensink_runtime::RuntimeGenesisConfig;
pub use node_primitives::{AccountId, Balance, Signature};
Expand Down Expand Up @@ -333,6 +332,7 @@ fn configure_accounts(
Vec<AccountId>,
usize,
Vec<(AccountId, AccountId, Balance, StakerStatus<AccountId>)>,
Vec<u8>,
Vec<(BeefyId, BeefyId, Vec<u8>)>
) {
let mut endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| {
Expand Down Expand Up @@ -383,54 +383,55 @@ fn configure_accounts(

let num_endowed_accounts = endowed_accounts.len();

let genesis_shares = etf_genesis(
// Aggregate BLS signature scheme with Signature in G1 for BLS12-377 curve.
let (ibe_pp_bytes, genesis_shares) = etf_genesis::<TinyBLS377>(
initial_authorities.iter().map(|x| x.7.clone()).collect::<Vec<_>>(),
vec!["Alice", "Bob"],
vec!["Alice", "Bob", "Charlie"],
);
(initial_authorities, endowed_accounts, num_endowed_accounts, stakers, genesis_shares)
(initial_authorities, endowed_accounts, num_endowed_accounts, stakers, ibe_pp_bytes, 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,
/// return a vec of (authority id, resharing, pubkey commitment) along with ibe public key against the master secret
pub fn etf_genesis<EB: EngineBLS>(
initial_authorities: Vec<BeefyId>,
seeds: Vec<&str>
) -> (Vec<u8>, Vec<(BeefyId, BeefyId, Vec<u8>)>) {
let msk = EB::Scalar::rand(&mut OsRng);
let msk_prime = EB::Scalar::rand(&mut OsRng);

let double_secret = DoubleSecret::<EB>(msk, msk_prime);

let ibe_pub_param = EB::PublicKeyGroup::generator() * msk;
let mut ibe_pp_bytes = Vec::new();
ibe_pub_param.serialize_compressed(&mut ibe_pp_bytes).unwrap();

let genesis_resharing = double_secret.reshare(
&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()
w3f_bls::single::PublicKey::<EB>::from_bytes(&authority.to_raw_vec()[..48]).unwrap()
// EB::SignatureGroup::deserialize_compressed(
// // [48 bytes for SigGroup][96 bytes for PubKeyGroup]
// &authority.to_raw_vec()[..48]
// ).unwrap()
}).collect::<Vec<_>>(),
initial_authorities.len() as u8,
initial_authorities.len() as u8, // threshold = full set of authorities for now
&mut OsRng,
);
).unwrap();

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

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 o = t.acss_recover(&bytes, initial_authorities.len() as u8)
.expect("genesis shares should be well formatted");
let etf_id = BeefyId::from(o.public());
(auth.clone(), etf_id, bytes)
}).collect::<Vec<_>>()
}).collect::<Vec<_>>();
(ibe_pp_bytes, resharings)
}

/// Helper function to create RuntimeGenesisConfig json patch for testing.
Expand All @@ -449,7 +450,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, genesis_shares) =
let (initial_authorities, endowed_accounts, num_endowed_accounts, stakers, round_key, genesis_shares) =
configure_accounts(initial_authorities, initial_nominators, endowed_accounts, STASH);

serde_json::json!({
Expand Down Expand Up @@ -505,6 +506,7 @@ pub fn testnet_genesis(
"authorities": Vec::<BeefyId>::new(),
"genesisBlock": Some(1),
"genesisResharing": genesis_shares,
"roundPubkey": round_key,
},
"society": { "pot": 0 },
"assets": {
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", features = ["bls-experimental", "etf"]}
sc-consensus-beefy = { path = "../../../client/consensus/beefy", features = ["bls-experimental"]}
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
2 changes: 1 addition & 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, features = ["bls-experimental", "etf"]}
sp-consensus-beefy = { path = "../../../primitives/consensus/beefy", default-features = false, features = ["bls-experimental"]}
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
4 changes: 2 additions & 2 deletions substrate/bin/node/testing/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use codec::Encode;
use kitchensink_runtime::{CheckedExtrinsic, SessionKeys, SignedExtra, UncheckedExtrinsic};
use node_cli::chain_spec::get_from_seed;
use node_primitives::{AccountId, Balance, Nonce};
use sp_core::{ecdsa, ed25519, sr25519};
use sp_core::{bls377, ed25519, sr25519};
use sp_crypto_hashing::blake2_256;
use sp_keyring::AccountKeyring;
use sp_runtime::generic::Era;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn session_keys_from_seed(seed: &str) -> SessionKeys {
im_online: get_from_seed::<sr25519::Public>(seed).into(),
authority_discovery: get_from_seed::<sr25519::Public>(seed).into(),
mixnet: get_from_seed::<sr25519::Public>(seed).into(),
beefy: get_from_seed::<ecdsa::Public>(seed).into(),
beefy: get_from_seed::<bls377::Public>(seed).into(),
}
}

Expand Down
10 changes: 4 additions & 6 deletions substrate/client/consensus/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ 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", features = ["bls-experimental", "etf"]}
sp-keystore = { path = "../../../primitives/keystore", features = ["bls-experimental"]}
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" }
# etf-crypto-primitives = { git = "https://github.com/ideal-lab5/etf-sdk.git", branch = "w3fbls-migration" }
ark-serialize = { version = "0.4.0" }
ark-bls12-377 = { version = "0.4.0", features = ["curve"], optional = true}
w3f-bls = { path = "../../../../../bls" }
w3f-bls = { version = "0.1.3", optional = true }


[dev-dependencies]
Expand All @@ -66,6 +65,5 @@ bls-experimental = [
"sp-consensus-beefy/bls-experimental",
"sp-core/bls-experimental",
"ark-bls12-377",
"w3f-bls"
]

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 = "..", features = ["bls-experimental", "etf"] }
sc-consensus-beefy = { path = "..", features = ["bls-experimental" ] }
sp-consensus-beefy = { path = "../../../../primitives/consensus/beefy" }
sc-rpc = { path = "../../../rpc" }
sp-core = { path = "../../../../primitives/core" }
Expand Down
24 changes: 15 additions & 9 deletions substrate/client/consensus/beefy/src/communication/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,24 @@ where
}

// recover the signature bytes from the payload
let raw_etf_payload = vote.commitment.payload.get_raw(
&sp_consensus_beefy::known_payloads::ETF_SIGNATURE
).expect("its ok for now");
let raw_etf_payload = vote.commitment.payload.get_raw(&sp_consensus_beefy::known_payloads::ETF_SIGNATURE)
.unwrap_or({
debug!(
target: LOG_TARGET,
"🎲 Corrupted (irrecoverable) signature on message: {:?}, from: {:?}", vote, sender
);
return Action::Discard(cost::BAD_SIGNATURE);
});

let etf_sig: Signature = Signature::decode(
&mut sp_runtime::traits::TrailingZeroInput::new(&raw_etf_payload))
.ok()
.unwrap();
.unwrap_or({
debug!(
target: LOG_TARGET,
"🎲 Corrupted (irrecoverable) signature on message: {:?}, from: {:?}", vote, sender
);
return Action::Discard(cost::BAD_SIGNATURE);
});

if BeefyKeystore::verify(&vote.id, &vote.signature, &vote.commitment.encode())
// && BeefyKeystore::verify(&vote.id, &etf_sig, &round.to_string().as_bytes())
Expand All @@ -324,10 +334,6 @@ where
);
Action::Keep(self.votes_topic, benefit::VOTE_MESSAGE)
} else {
info!(
target: LOG_TARGET,
"🎲 The etf signature WAS NOT verified! AH FUCK!~!!!!!!!!!!!!!!!!!!",
);
debug!(
target: LOG_TARGET,
"🥩 Bad signature on message: {:?}, from: {:?}", vote, sender
Expand Down
22 changes: 7 additions & 15 deletions substrate/client/consensus/beefy/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ use codec::Decode;
use log::{info, warn};
use std::marker::PhantomData;

use etf_crypto_primitives::{
proofs::hashed_el_gamal_sigma::BatchPoK,
dpss::acss::HighThresholdACSS
};

use crate::{error, LOG_TARGET};

/// A BEEFY specific keystore implemented as a `Newtype`. This is basically a
Expand Down Expand Up @@ -217,6 +212,7 @@ impl<AuthorityId: AuthorityIdBound> BeefyKeystore<AuthorityId> {
public: &AuthorityId,
pok_bytes: &[u8],
message: &[u8],
threshold: u8,
) -> Result<<AuthorityId as RuntimeAppPublic>::Signature, error::Error> {
let store = self.0.clone().ok_or_else(|| error::Error::Keystore("no Keystore".into()))
.map_err(|_| ())
Expand All @@ -230,21 +226,17 @@ impl<AuthorityId: AuthorityIdBound> BeefyKeystore<AuthorityId> {
&public,
pok_bytes,
message,
threshold,
).map_err(|_| {
error::Error::Signature(format!(
"Failed to recover a key from the provided proof of knowledge"
))
})?;

let mut signature_byte_array: &[u8] = sig.as_ref();
// should this be runtimeapppublic instead?
let signature = <AuthorityId as RuntimeAppPublic>::Signature::decode(
&mut signature_byte_array,
)
// let signature = bls377::Signature::decode(
// &mut signature_byte_array,
// )
.map_err(|_| {
).map_err(|_| {
error::Error::Signature(format!(
"invalid signature {:?} for key {:?}",
signature_byte_array, public
Expand Down Expand Up @@ -485,7 +477,7 @@ pub mod tests {

let store: BeefyKeystore<AuthorityId> = Some(store).into();

let msg = b"are you involved or commited?";
let msg = b"are you involved or committed?";

let sig1 = store.sign(&alice, msg).unwrap();
let sig2 = Keyring::<AuthorityId>::Alice.sign(msg);
Expand Down Expand Up @@ -521,7 +513,7 @@ pub mod tests {

let alice = Keyring::Alice.public();

let msg = b"are you involved or commited?";
let msg = b"are you involved or committed?";
let sig = store.sign(&alice, msg).err().unwrap();
let err = Error::Signature(expected_error_message.to_string());

Expand All @@ -544,7 +536,7 @@ pub mod tests {
let store: BeefyKeystore<ecdsa_crypto::Public> = None.into();

let alice = Keyring::Alice.public();
let msg = b"are you involved or commited";
let msg = b"are you involved or committed";

let sig = store.sign(&alice, msg).err().unwrap();
let err = Error::Keystore("no Keystore".to_string());
Expand All @@ -568,7 +560,7 @@ pub mod tests {
let alice = Keyring::Alice.public();

// `msg` and `sig` match
let msg = b"are you involved or commited?";
let msg = b"are you involved or committed?";
let sig = store.sign(&alice, msg).unwrap();
assert!(BeefyKeystore::verify(&alice, &sig, msg));

Expand Down
5 changes: 0 additions & 5 deletions substrate/client/consensus/beefy/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ where
&mut self,
vote: VoteMessage<NumberFor<B>, AuthorityId, Signature>,
) -> VoteImportResult<B> {

info!("TONY CALLING ADD VOTE");

let num = vote.commitment.block_number;
let vote_key = (vote.id.clone(), num);

Expand Down Expand Up @@ -173,8 +170,6 @@ where
{
if let Some(round) = self.rounds.remove_entry(&vote.commitment) {
return VoteImportResult::RoundConcluded(self.signed_commitment(round))
} else {
info!("TONY ROUND NOT CONCLUDED BUT WE DID TRY!");
}
}
VoteImportResult::Ok
Expand Down
Loading

0 comments on commit dda2a07

Please sign in to comment.