Skip to content

Commit

Permalink
Move serai-client off serai-runtime, MIT licensing it
Browse files Browse the repository at this point in the history
Uses a full-fledged serai-abi to do so.

Removes use of UncheckedExtrinsic as a pointlessly (for us) length-prefixed
block with a more complicated signing algorithm than advantageous.

In the future, we should considering consolidating the various primitives
crates. I'm not convinced we benefit from one primitives crate per pallet.
  • Loading branch information
kayabaNerve committed Dec 7, 2023
1 parent 6416e00 commit c511a54
Show file tree
Hide file tree
Showing 38 changed files with 485 additions and 379 deletions.
216 changes: 109 additions & 107 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion coordinator/src/cosign_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<D: Db> CosignEvaluator<D> {
// included the set_keys and one didn't
// Because set_keys will force a cosign, it will force detection of distinct blocks
// re: set_keys using keys prior to set_keys (assumed amenable to all)
let serai = self.serai.as_of(block.header().parent_hash.into());
let serai = self.serai.as_of(block.header.parent_hash.into());

let Some(set_with_keys) = set_with_keys_fn(&serai, cosign.network).await? else {
return Ok(());
Expand Down
4 changes: 2 additions & 2 deletions coordinator/src/substrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ async fn handle_new_blocks<D: Db, Pro: Processors>(
// Get the keys as of the prior block
// That means if this block is setting new keys (which won't lock in until we process this
// block), we won't freeze up waiting for the yet-to-be-processed keys to sign this block
let serai = serai.as_of(actual_block.header().parent_hash.into());
let serai = serai.as_of(actual_block.header.parent_hash.into());

has_no_cosigners = Some(actual_block.clone());

Expand Down Expand Up @@ -583,7 +583,7 @@ pub async fn scan_task<D: Db, Pro: Processors>(
loop {
match serai.latest_finalized_block().await {
Ok(latest) => {
if latest.header().number >= next_substrate_block {
if latest.header.number >= next_substrate_block {
return latest;
} else {
sleep(Duration::from_secs(3)).await;
Expand Down
27 changes: 4 additions & 23 deletions coordinator/src/tests/tributary/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::collections::HashMap;
use zeroize::Zeroizing;
use rand_core::{RngCore, OsRng};

use scale::Decode;

use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
use frost::Participant;

Expand Down Expand Up @@ -340,27 +338,10 @@ async fn dkg_test() {
let spec = spec.clone();
let key_pair = key_pair.clone();
async move {
// Version, Pallet, Call, Network, Key Pair, Signature
let expected_len = 1 + 1 + 1 + 1 + 32 + 1 + key_pair.1.len() + 64;
// It's length prefixed
assert_eq!(tx.len(), 2 + expected_len);
let expected_len = u16::try_from(expected_len).unwrap();

// Check the encoded length
// This is the compact encoding from SCALE, specifically the two-byte length encoding case
let bottom_six = expected_len & 0b111111;
let upper_eight = expected_len >> 6;
assert_eq!(u8::try_from((bottom_six << 2) | 1).unwrap(), tx[0]);
assert_eq!(u8::try_from(upper_eight).unwrap(), tx[1]);

// Version
assert_eq!(tx[2], 4);

// Call
let tx = serai_client::runtime::RuntimeCall::decode(&mut &tx[3 ..]).unwrap();
match tx {
serai_client::runtime::RuntimeCall::ValidatorSets(
serai_client::runtime::validator_sets::Call::set_keys {
assert_eq!(tx.signature, None);
match tx.call {
serai_client::abi::Call::ValidatorSets(
serai_client::abi::validator_sets::Call::set_keys {
network,
key_pair: set_key_pair,
signature,
Expand Down
6 changes: 3 additions & 3 deletions coordinator/src/tributary/dkg_removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use frost::{
use frost_schnorrkel::Schnorrkel;

use serai_client::{
Public,
Public, SeraiAddress,
validator_sets::primitives::{musig_context, remove_participant_message},
};

Expand Down Expand Up @@ -190,7 +190,7 @@ impl DkgRemoval {
preprocesses: HashMap<Participant, Vec<u8>>,
removed: [u8; 32],
mut shares: HashMap<Participant, Vec<u8>>,
) -> Result<(Vec<Public>, [u8; 64]), Participant> {
) -> Result<(Vec<SeraiAddress>, [u8; 64]), Participant> {
// TODO: Remove this ugly blob
let shares = {
let mut shares_participants = shares.keys().cloned().collect::<Vec<_>>();
Expand All @@ -213,7 +213,7 @@ impl DkgRemoval {
new_shares
};

let mut signers = shares.keys().cloned().map(Public).collect::<Vec<_>>();
let mut signers = shares.keys().cloned().map(SeraiAddress).collect::<Vec<_>>();
signers.sort();

let machine = Self::share_internal(spec, key, attempt, preprocesses, removed)
Expand Down
6 changes: 3 additions & 3 deletions coordinator/src/tributary/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frost::dkg::Participant;

use scale::{Encode, Decode};
use serai_client::{
Public, Signature,
Public, SeraiAddress, Signature,
validator_sets::primitives::{ValidatorSet, KeyPair},
SeraiValidatorSets,
};
Expand Down Expand Up @@ -174,7 +174,7 @@ pub(crate) async fn handle_application_tx<
D: Db,
Pro: Processors,
FPst: Future<Output = ()>,
PST: Clone + Fn(ValidatorSet, PstTxType, Vec<u8>) -> FPst,
PST: Clone + Fn(ValidatorSet, PstTxType, serai_client::Transaction) -> FPst,
FPtt: Future<Output = ()>,
PTT: Clone + Fn(Transaction) -> FPtt,
FRid: Future<Output = ()>,
Expand Down Expand Up @@ -734,7 +734,7 @@ pub(crate) async fn handle_application_tx<

let tx = serai_client::SeraiValidatorSets::remove_participant(
spec.set().network,
Public(data.plan),
SeraiAddress(data.plan),
signers,
Signature(signature),
);
Expand Down
4 changes: 2 additions & 2 deletions coordinator/src/tributary/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn handle_block<
D: Db,
Pro: Processors,
FPst: Future<Output = ()>,
PST: Clone + Fn(ValidatorSet, PstTxType, Vec<u8>) -> FPst,
PST: Clone + Fn(ValidatorSet, PstTxType, serai_client::Transaction) -> FPst,
FPtt: Future<Output = ()>,
PTT: Clone + Fn(Transaction) -> FPtt,
FRid: Future<Output = ()>,
Expand Down Expand Up @@ -148,7 +148,7 @@ pub(crate) async fn handle_new_blocks<
D: Db,
Pro: Processors,
FPst: Future<Output = ()>,
PST: Clone + Fn(ValidatorSet, PstTxType, Vec<u8>) -> FPst,
PST: Clone + Fn(ValidatorSet, PstTxType, serai_client::Transaction) -> FPst,
FPtt: Future<Output = ()>,
PTT: Clone + Fn(Transaction) -> FPtt,
FRid: Future<Output = ()>,
Expand Down
2 changes: 1 addition & 1 deletion substrate/abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
scale = { package = "parity-scale-codec", version = "3", features = ["derive"] }
scale-info = { version = "2", features = ["derive"] }

borsh = { version = "1", features = ["derive", "de_strict_order"], optional = true }
serde = { version = "1", features = ["derive", "alloc"], optional = true }

sp-application-crypto = { git = "https://github.com/serai-dex/substrate" }
sp-core = { git = "https://github.com/serai-dex/substrate" }
sp-runtime = { git = "https://github.com/serai-dex/substrate" }

Expand Down
6 changes: 4 additions & 2 deletions substrate/abi/src/babe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use sp_consensus_babe::EquivocationProof;

use serai_primitives::Header;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub struct ReportEquivocation {
pub equivocation_proof: Box<EquivocationProof<Header>>,
pub key_owner_proof: (),
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
// We could define a Babe Config here and use the literal pallet_babe::Call
// The disadvantage to this would be the complexity and presence of junk fields such as `__Ignore`
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub enum Call {
report_equivocation(ReportEquivocation),
report_equivocation_unsigned(ReportEquivocation),
Expand Down
4 changes: 2 additions & 2 deletions substrate/abi/src/coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serai_primitives::{Balance, SeraiAddress};
pub use serai_coins_primitives as primitives;
use primitives::OutInstructionWithBalance;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
Expand All @@ -12,7 +12,7 @@ pub enum Call {
burn_with_instruction { instruction: OutInstructionWithBalance },
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
Expand Down
4 changes: 2 additions & 2 deletions substrate/abi/src/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type PoolId = Coin;
type PoolCoinId = Coin;
type MaxSwapPathLength = sp_core::ConstU32<3>;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
add_liquidity {
Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum Call {
},
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
PoolCreated {
Expand Down
7 changes: 4 additions & 3 deletions substrate/abi/src/grandpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ use sp_consensus_grandpa::EquivocationProof;

use serai_primitives::{BlockNumber, SeraiAddress};

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub struct ReportEquivocation {
pub equivocation_proof: Box<EquivocationProof<[u8; 32], BlockNumber>>,
pub key_owner_proof: (),
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub enum Call {
report_equivocation(ReportEquivocation),
report_equivocation_unsigned(ReportEquivocation),
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
NewAuthorities { authority_set: Vec<(SeraiAddress, u64)> },
// TODO: Remove these
Paused,
Resumed,
}
4 changes: 2 additions & 2 deletions substrate/abi/src/in_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use serai_primitives::*;
pub use serai_in_instructions_primitives as primitives;
use primitives::SignedBatch;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
execute_batch { batch: SignedBatch },
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
Expand Down
35 changes: 33 additions & 2 deletions substrate/abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ pub mod grandpa;

pub use serai_primitives as primitives;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub enum Call {
System,
Timestamp(timestamp::Call),
TransactionPayment,
Coins(coins::Call),
LiquidityTokens(coins::Call),
Dex(dex::Call),
Expand All @@ -29,14 +31,43 @@ pub enum Call {
Grandpa(grandpa::Call),
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
// TODO: Remove this
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub enum TransactionPaymentEvent {
TransactionFeePaid { who: serai_primitives::SeraiAddress, actual_fee: u64, tip: u64 },
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
pub enum Event {
System(system::Event),
Timestamp,
TransactionPayment(TransactionPaymentEvent),
Coins(coins::Event),
LiquidityTokens(coins::Event),
Dex(dex::Event),
ValidatorSets(validator_sets::Event),
InInstructions(in_instructions::Event),
Signals(signals::Event),
Babe,
Grandpa(grandpa::Event),
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Extra {
pub era: sp_runtime::generic::Era,
pub nonce: scale::Compact<u32>,
pub tip: scale::Compact<u64>,
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SignedPayloadExtra {
pub spec_version: u32,
pub tx_version: u32,
pub genesis: [u8; 32],
pub mortality_checkpoint: [u8; 32],
}

pub type Transaction = primitives::Transaction<Call, Extra>;
4 changes: 2 additions & 2 deletions substrate/abi/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serai_validator_sets_primitives::ValidatorSet;
pub use serai_signals_primitives as primitives;
use primitives::SignalId;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
Expand All @@ -16,7 +16,7 @@ pub enum Call {
stand_against { signal_id: SignalId, for_network: NetworkId },
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
Expand Down
8 changes: 7 additions & 1 deletion substrate/abi/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use frame_support::dispatch::{DispatchInfo, DispatchError};

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
use serai_primitives::SeraiAddress;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
ExtrinsicSuccess { dispatch_info: DispatchInfo },
ExtrinsicFailed { dispatch_error: DispatchError, dispatch_info: DispatchInfo },
CodeUpdated,
NewAccount { account: SeraiAddress },
KilledAccount { account: SeraiAddress },
Remarked { sender: SeraiAddress, hash: [u8; 32] },
}
5 changes: 2 additions & 3 deletions substrate/abi/src/timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
set { now: u64 },
set { now: scale::Compact<u64> },
}
4 changes: 2 additions & 2 deletions substrate/abi/src/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use serai_validator_sets_primitives as primitives;
use serai_primitives::*;
use serai_validator_sets_primitives::*;

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Call {
set_keys {
Expand Down Expand Up @@ -31,7 +31,7 @@ pub enum Call {
},
}

#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode)]
#[derive(Clone, PartialEq, Eq, Debug, scale::Encode, scale::Decode, scale_info::TypeInfo)]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Event {
Expand Down
Loading

0 comments on commit c511a54

Please sign in to comment.