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

refactor: Re-arrange Chains traits for better composability #3912

Merged
merged 7 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions engine/multisig/src/crypto/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use crate::crypto::ECScalar;
use anyhow::Context;
use cf_chains::Bitcoin;
use cf_chains::{Bitcoin, Chain, ChainCrypto};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -32,8 +32,10 @@ impl BtcSchnorrSignature {
}
}

impl SignatureToThresholdSignature<Bitcoin> for Vec<BtcSchnorrSignature> {
fn to_threshold_signature(&self) -> <Bitcoin as cf_chains::ChainCrypto>::ThresholdSignature {
impl SignatureToThresholdSignature<<Bitcoin as Chain>::ChainCrypto> for Vec<BtcSchnorrSignature> {
fn to_threshold_signature(
&self,
) -> <<Bitcoin as Chain>::ChainCrypto as ChainCrypto>::ThresholdSignature {
self.iter().map(|s| s.to_raw()).collect()
}
}
Expand Down Expand Up @@ -62,7 +64,7 @@ pub struct BtcCryptoScheme;

impl ChainSigning for BtcSigning {
type CryptoScheme = BtcCryptoScheme;
type Chain = cf_chains::Bitcoin;
type Chain = <Bitcoin as Chain>::ChainCrypto;
ramizhasan111 marked this conversation as resolved.
Show resolved Hide resolved
const NAME: &'static str = "Bitcoin";
const CHAIN_TAG: ChainTag = ChainTag::Bitcoin;

Expand Down
3 changes: 2 additions & 1 deletion engine/multisig/src/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{
curve25519::edwards::Point, CanonicalEncoding, ChainSigning, ChainTag, CryptoScheme, CryptoTag,
ECPoint,
};
use cf_chains::Chain;
use ed25519_consensus::VerificationKeyBytes;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -49,7 +50,7 @@ pub struct Ed25519CryptoScheme;
impl ChainSigning for Ed25519Signing {
type CryptoScheme = Ed25519CryptoScheme;
// This scheme isn't implemented on the state chain.
type Chain = cf_chains::none::NoneChain;
type Chain = <cf_chains::none::NoneChain as Chain>::ChainCrypto;

const NAME: &'static str = "Ed25519";

Expand Down
10 changes: 6 additions & 4 deletions engine/multisig/src/crypto/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
// solely use "CryptoScheme" as generic parameter instead.
pub use super::secp256k1::{Point, Scalar};
use anyhow::Context;
use cf_chains::{eth::ParityBit, ChainCrypto, Ethereum};
use cf_chains::{eth::ParityBit, Chain, ChainCrypto, Ethereum};
use num_bigint::BigUint;
use secp256k1::constants::CURVE_ORDER;
use serde::{Deserialize, Serialize};
Expand All @@ -29,8 +29,10 @@ impl From<EthSchnorrSignature> for cf_chains::eth::SchnorrVerificationComponents
}
}

impl SignatureToThresholdSignature<Ethereum> for Vec<EthSchnorrSignature> {
fn to_threshold_signature(&self) -> <Ethereum as ChainCrypto>::ThresholdSignature {
impl SignatureToThresholdSignature<<Ethereum as Chain>::ChainCrypto> for Vec<EthSchnorrSignature> {
fn to_threshold_signature(
&self,
) -> <<Ethereum as Chain>::ChainCrypto as ChainCrypto>::ThresholdSignature {
self.iter()
.map(|s| s.clone().into())
.next()
Expand Down Expand Up @@ -62,7 +64,7 @@ pub struct EvmCryptoScheme;

impl ChainSigning for EthSigning {
type CryptoScheme = EvmCryptoScheme;
type Chain = cf_chains::Ethereum;
type Chain = <Ethereum as Chain>::ChainCrypto;
const NAME: &'static str = "Ethereum";
const CHAIN_TAG: ChainTag = ChainTag::Ethereum;
}
Expand Down
10 changes: 6 additions & 4 deletions engine/multisig/src/crypto/polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
curve25519::ristretto::Point, CanonicalEncoding, ChainSigning, ChainTag, CryptoScheme,
CryptoTag, ECPoint, SignatureToThresholdSignature,
};
use cf_chains::{ChainCrypto, Polkadot};
use cf_chains::{Chain, ChainCrypto, Polkadot};
use schnorrkel::context::{SigningContext, SigningTranscript};
use serde::{Deserialize, Serialize};

Expand All @@ -17,8 +17,10 @@ const SIGNING_CTX: &[u8] = b"substrate";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolkadotSignature(schnorrkel::Signature);

impl SignatureToThresholdSignature<Polkadot> for Vec<PolkadotSignature> {
fn to_threshold_signature(&self) -> <Polkadot as ChainCrypto>::ThresholdSignature {
impl SignatureToThresholdSignature<<Polkadot as Chain>::ChainCrypto> for Vec<PolkadotSignature> {
fn to_threshold_signature(
&self,
) -> <<Polkadot as Chain>::ChainCrypto as ChainCrypto>::ThresholdSignature {
self.iter()
.map(|s| s.clone().into())
.next()
Expand Down Expand Up @@ -66,7 +68,7 @@ pub struct PolkadotCryptoScheme;

impl ChainSigning for PolkadotSigning {
type CryptoScheme = PolkadotCryptoScheme;
type Chain = cf_chains::Polkadot;
type Chain = <Polkadot as Chain>::ChainCrypto;
const NAME: &'static str = "Polkadot";
const CHAIN_TAG: ChainTag = ChainTag::Polkadot;
}
Expand Down
22 changes: 11 additions & 11 deletions engine/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use self::{
muxer::{ProtocolVersion, VersionedCeremonyMessage, CURRENT_PROTOCOL_VERSION},
};
use anyhow::Context;
use cf_chains::{Bitcoin, Chain, Ethereum, Polkadot};
use cf_chains::{btc::BitcoinCrypto, dot::PolkadotCrypto, eth::EvmCrypto, ChainCrypto};
use cf_primitives::AccountId;
use futures::{Future, FutureExt};
use multisig::p2p::OutgoingMultisigStageMessages;
Expand All @@ -38,22 +38,22 @@ use utilities::{read_clean_and_decode_hex_str_file, task_scope::task_scope};
type EdPublicKey = ed25519::Public;
type XPublicKey = x25519_dalek::PublicKey;

pub struct MultisigMessageSender<C: Chain>(
pub struct MultisigMessageSender<C: ChainCrypto>(
pub UnboundedSender<OutgoingMultisigStageMessages>,
PhantomData<C>,
);

impl<C: Chain> MultisigMessageSender<C> {
impl<C: ChainCrypto> MultisigMessageSender<C> {
pub fn new(sender: UnboundedSender<OutgoingMultisigStageMessages>) -> Self {
MultisigMessageSender(sender, PhantomData)
}
}
pub struct MultisigMessageReceiver<C: Chain>(
pub struct MultisigMessageReceiver<C: ChainCrypto>(
pub UnboundedReceiver<(AccountId, VersionedCeremonyMessage)>,
PhantomData<C>,
);

impl<C: Chain> MultisigMessageReceiver<C> {
impl<C: ChainCrypto> MultisigMessageReceiver<C> {
pub fn new(receiver: UnboundedReceiver<(AccountId, VersionedCeremonyMessage)>) -> Self {
MultisigMessageReceiver(receiver, PhantomData)
}
Expand Down Expand Up @@ -89,12 +89,12 @@ pub async fn start<StateChainClient>(
settings: P2PSettings,
latest_block_hash: H256,
) -> anyhow::Result<(
MultisigMessageSender<Ethereum>,
MultisigMessageReceiver<Ethereum>,
MultisigMessageSender<Polkadot>,
MultisigMessageReceiver<Polkadot>,
MultisigMessageSender<Bitcoin>,
MultisigMessageReceiver<Bitcoin>,
MultisigMessageSender<EvmCrypto>,
MultisigMessageReceiver<EvmCrypto>,
MultisigMessageSender<PolkadotCrypto>,
MultisigMessageReceiver<PolkadotCrypto>,
MultisigMessageSender<BitcoinCrypto>,
MultisigMessageReceiver<BitcoinCrypto>,
UnboundedSender<PeerUpdate>,
impl Future<Output = anyhow::Result<()>>,
)>
Expand Down
26 changes: 13 additions & 13 deletions engine/src/p2p/muxer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use cf_chains::{Bitcoin, Ethereum, Polkadot};
use cf_chains::{btc::BitcoinCrypto, dot::PolkadotCrypto, eth::EvmCrypto};
use futures::Future;
use state_chain_runtime::AccountId;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
Expand Down Expand Up @@ -87,12 +87,12 @@ impl P2PMuxer {
all_incoming_receiver: UnboundedReceiver<(AccountId, Vec<u8>)>,
all_outgoing_sender: UnboundedSender<OutgoingMultisigStageMessages>,
) -> (
MultisigMessageSender<Ethereum>,
MultisigMessageReceiver<Ethereum>,
MultisigMessageSender<Polkadot>,
MultisigMessageReceiver<Polkadot>,
MultisigMessageSender<Bitcoin>,
MultisigMessageReceiver<Bitcoin>,
MultisigMessageSender<EvmCrypto>,
MultisigMessageReceiver<EvmCrypto>,
MultisigMessageSender<PolkadotCrypto>,
MultisigMessageReceiver<PolkadotCrypto>,
MultisigMessageSender<BitcoinCrypto>,
MultisigMessageReceiver<BitcoinCrypto>,
impl Future<Output = ()>,
) {
let (eth_outgoing_sender, eth_outgoing_receiver) = tokio::sync::mpsc::unbounded_channel();
Expand All @@ -118,12 +118,12 @@ impl P2PMuxer {
let muxer_fut = muxer.run().instrument(info_span!("P2PMuxer"));

(
MultisigMessageSender::<Ethereum>::new(eth_outgoing_sender),
MultisigMessageReceiver::<Ethereum>::new(eth_incoming_receiver),
MultisigMessageSender::<Polkadot>::new(dot_outgoing_sender),
MultisigMessageReceiver::<Polkadot>::new(dot_incoming_receiver),
MultisigMessageSender::<Bitcoin>::new(btc_outgoing_sender),
MultisigMessageReceiver::<Bitcoin>::new(btc_incoming_receiver),
MultisigMessageSender::<EvmCrypto>::new(eth_outgoing_sender),
MultisigMessageReceiver::<EvmCrypto>::new(eth_incoming_receiver),
MultisigMessageSender::<PolkadotCrypto>::new(dot_outgoing_sender),
MultisigMessageReceiver::<PolkadotCrypto>::new(dot_incoming_receiver),
MultisigMessageSender::<BitcoinCrypto>::new(btc_outgoing_sender),
MultisigMessageReceiver::<BitcoinCrypto>::new(btc_incoming_receiver),
muxer_fut,
)
}
Expand Down
19 changes: 12 additions & 7 deletions engine/src/state_chain_observer/sc_observer/crypto_compat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use cf_chains::{dot::PolkadotPublicKey, ChainCrypto};
use cf_chains::{
btc::BitcoinCrypto,
dot::{PolkadotCrypto, PolkadotPublicKey},
eth::EvmCrypto,
ChainCrypto,
};
use multisig::{
bitcoin::BtcSigning, eth::EthSigning, polkadot::PolkadotSigning, ChainSigning, CryptoScheme,
};
Expand All @@ -12,26 +17,26 @@ pub trait CryptoCompat<S: ChainSigning<Chain = C>, C: ChainCrypto> {
) -> C::AggKey;
}

impl CryptoCompat<EthSigning, cf_chains::Ethereum> for EthereumInstance {
impl CryptoCompat<EthSigning, EvmCrypto> for EthereumInstance {
fn pubkey_to_aggkey(
pubkey: <<EthSigning as ChainSigning>::CryptoScheme as CryptoScheme>::PublicKey,
) -> <cf_chains::Ethereum as ChainCrypto>::AggKey {
) -> <EvmCrypto as ChainCrypto>::AggKey {
pubkey
}
}

impl CryptoCompat<BtcSigning, cf_chains::Bitcoin> for BitcoinInstance {
impl CryptoCompat<BtcSigning, BitcoinCrypto> for BitcoinInstance {
fn pubkey_to_aggkey(
pubkey: <<BtcSigning as ChainSigning>::CryptoScheme as CryptoScheme>::PublicKey,
) -> <cf_chains::Bitcoin as ChainCrypto>::AggKey {
) -> <BitcoinCrypto as ChainCrypto>::AggKey {
cf_chains::btc::AggKey { previous: None, current: pubkey.serialize() }
}
}

impl CryptoCompat<PolkadotSigning, cf_chains::Polkadot> for PolkadotInstance {
impl CryptoCompat<PolkadotSigning, PolkadotCrypto> for PolkadotInstance {
fn pubkey_to_aggkey(
pubkey: <<PolkadotSigning as ChainSigning>::CryptoScheme as CryptoScheme>::PublicKey,
) -> <cf_chains::Polkadot as ChainCrypto>::AggKey {
) -> <PolkadotCrypto as ChainCrypto>::AggKey {
PolkadotPublicKey::from_aliased(pubkey.to_bytes())
}
}
9 changes: 6 additions & 3 deletions engine/src/state_chain_observer/sc_observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ mod crypto_compat;
mod tests;

use anyhow::{anyhow, Context};
use cf_chains::btc::{self, PreviousOrCurrent};
use cf_chains::{
btc::{self, PreviousOrCurrent},
Chain,
};
use cf_primitives::{BlockNumber, CeremonyId, EpochIndex};
use crypto_compat::CryptoCompat;
use futures::{FutureExt, StreamExt};
Expand Down Expand Up @@ -52,7 +55,7 @@ async fn handle_keygen_request<'a, StateChainClient, MultisigClient, C, I>(
MultisigClient: MultisigClientApi<C::CryptoScheme>,
StateChainClient: SignedExtrinsicApi + 'static + Send + Sync,
state_chain_runtime::Runtime: pallet_cf_vaults::Config<I>,
C: ChainSigning<Chain = <state_chain_runtime::Runtime as pallet_cf_vaults::Config<I>>::Chain>
C: ChainSigning<Chain = <<state_chain_runtime::Runtime as pallet_cf_vaults::Config<I>>::Chain as Chain>::ChainCrypto>
+ 'static,
I: CryptoCompat<C, C::Chain> + 'static + Sync + Send,
state_chain_runtime::RuntimeCall:
Expand Down Expand Up @@ -150,7 +153,7 @@ async fn handle_signing_request<'a, StateChainClient, MultisigClient, C, I>(
state_chain_runtime::RuntimeCall:
std::convert::From<pallet_cf_threshold_signature::Call<state_chain_runtime::Runtime, I>>,
Vec<C::Signature>: SignatureToThresholdSignature<
<state_chain_runtime::Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChain,
<state_chain_runtime::Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChainCrypto,
>,
{
if signers.contains(&state_chain_client.account_id()) {
Expand Down
11 changes: 7 additions & 4 deletions engine/src/state_chain_observer/sc_observer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use cf_chains::{
eth::{SchnorrVerificationComponents, Transaction},
ChainCrypto,
Chain, ChainCrypto,
};
use cf_primitives::{AccountRole, GENESIS_EPOCH};
use frame_system::Phase;
Expand Down Expand Up @@ -164,10 +164,10 @@ where
Runtime: pallet_cf_threshold_signature::Config<I>,
RuntimeCall:
std::convert::From<pallet_cf_threshold_signature::Call<Runtime, I>>,
<<Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChain as
<<Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChainCrypto as
ChainCrypto>::ThresholdSignature: std::convert::From<<C as CryptoScheme>::Signature>,
Vec<C::Signature>: SignatureToThresholdSignature<
<Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChain
<Runtime as pallet_cf_threshold_signature::Config<I>>::TargetChainCrypto

>,
{
Expand Down Expand Up @@ -310,7 +310,10 @@ mod dot_signing {

async fn should_handle_keygen_request<C, I>()
where
C: ChainSigning<Chain = <Runtime as pallet_cf_vaults::Config<I>>::Chain> + Send + Sync,
C: ChainSigning<
Chain = <<Runtime as pallet_cf_vaults::Config<I>>::Chain as Chain>::ChainCrypto,
> + Send
+ Sync,
I: CryptoCompat<C, C::Chain> + 'static + Send + Sync,
Runtime: pallet_cf_vaults::Config<I>,
RuntimeCall: std::convert::From<pallet_cf_vaults::Call<Runtime, I>>,
Expand Down
6 changes: 3 additions & 3 deletions engine/src/witness/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod chain_source;
pub mod chunked_chain_source;
pub mod epoch_source;

use cf_chains::{Chain, ChainAbi};
use cf_chains::Chain;
use futures_core::{stream::BoxStream, Future, Stream};
use futures_util::{stream, StreamExt};
use state_chain_runtime::PalletInstanceAlias;
Expand Down Expand Up @@ -105,8 +105,8 @@ where
{
}

pub trait ExternalChain: ChainAbi + PalletInstanceAlias {}
impl<T: ChainAbi + PalletInstanceAlias> ExternalChain for T {}
pub trait ExternalChain: Chain + PalletInstanceAlias {}
impl<T: Chain + PalletInstanceAlias> ExternalChain for T {}

pub trait ExternalChainSource:
ChainSource<Index = <Self::Chain as Chain>::ChainBlockNumber>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use crate::witness::common::chain_source::{ChainClient, ChainStream};
use cf_chains::{Chain, ChainCrypto};
use frame_support::CloneNoBound;
use futures_core::FusedStream;
use futures_util::{stream, StreamExt};
Expand All @@ -15,7 +16,7 @@ use crate::{
use super::{builder::ChunkedByVaultBuilder, ChunkedByVault};

pub type TxOutId<Inner> =
<<Inner as ChunkedByVault>::Chain as cf_chains::ChainCrypto>::TransactionOutId;
<<<Inner as ChunkedByVault>::Chain as Chain>::ChainCrypto as ChainCrypto>::TransactionOutId;
pub type TxOutIds<Inner> = Vec<TxOutId<Inner>>;

/// This helps ensure the set of egress items witnessed at each block are consistent across
Expand Down
12 changes: 6 additions & 6 deletions engine/src/witness/eth/key_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use cf_chains::eth::{AggKey, SchnorrVerificationComponents, TransactionFee};
use cf_chains::eth::{EvmCrypto, SchnorrVerificationComponents, TransactionFee};
use ethers::{
prelude::abigen,
types::{Bloom, TransactionReceipt},
Expand Down Expand Up @@ -56,11 +56,11 @@ impl<Inner: ChunkedByVault> ChunkedByVaultBuilder<Inner> {
where
// These are the types for EVM chains, so this adapter can be shared by all EVM chains.
Inner: ChunkedByVault<Index = u64, Hash = H256, Data = Bloom>,
Inner::Chain: cf_chains::ChainCrypto<
TransactionInId = H256,
TransactionOutId = SchnorrVerificationComponents,
AggKey = AggKey,
> + cf_chains::Chain<ChainAccount = H160, TransactionFee = TransactionFee>,
Inner::Chain: cf_chains::Chain<
ChainCrypto = EvmCrypto,
ChainAccount = H160,
TransactionFee = TransactionFee,
>,
StateChainClient: SignedExtrinsicApi + Send + Sync + 'static,
state_chain_runtime::Runtime: RuntimeHasChain<Inner::Chain>,
state_chain_runtime::RuntimeCall:
Expand Down
4 changes: 2 additions & 2 deletions state-chain/cf-integration-tests/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::threshold_signing::{
BtcKeyComponents, BtcThresholdSigner, DotKeyComponents, DotThresholdSigner, EthKeyComponents,
EthThresholdSigner, KeyUtils, ThresholdSigner,
};
use cf_chains::{dot::PolkadotSignature, eth::SchnorrVerificationComponents, ChainCrypto};
use cf_chains::{dot::PolkadotSignature, eth::SchnorrVerificationComponents, Chain, ChainCrypto};

use cf_primitives::{AccountRole, CeremonyId, EpochIndex, FlipBalance, TxId, GENESIS_EPOCH};
use cf_traits::{AccountRoleRegistry, EpochInfo};
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Engine {
fn report_keygen_outcome_for_chain<
K: KeyUtils<
SigVerification = S,
AggKey = <<T as pallet_cf_vaults::Config<I>>::Chain as ChainCrypto>::AggKey,
AggKey = <<<T as pallet_cf_vaults::Config<I>>::Chain as Chain>::ChainCrypto as ChainCrypto>::AggKey,
> + Clone,
S,
T: pallet_cf_vaults::Config<I>,
Expand Down
Loading