add specific network/coin/balance types (#619)

* add specific network/coin/balance types

* misc fixes

* fix clippy

* misc fixes

* fix pr comments

* Make halting for external networks

* fix encode/decode
This commit is contained in:
akildemir
2024-10-07 05:16:11 +03:00
committed by GitHub
parent d7ecab605e
commit 435f1d9ae1
91 changed files with 1536 additions and 1055 deletions

View File

@@ -7,7 +7,7 @@ use rand_core::{RngCore, OsRng};
use ciphersuite::{group::ff::PrimeField, Ciphersuite, Ristretto};
use serai_client::primitives::NetworkId;
use serai_client::primitives::ExternalNetworkId;
use messages::{ProcessorMessage, CoordinatorMessage};
use serai_message_queue::{Service, Metadata, client::MessageQueue};
@@ -25,7 +25,7 @@ mod tests;
static UNIQUE_ID: OnceLock<Mutex<u16>> = OnceLock::new();
pub fn processor_instance(
network: NetworkId,
network: ExternalNetworkId,
port: u32,
message_queue_key: <Ristretto as Ciphersuite>::F,
) -> Vec<TestBodySpecification> {
@@ -33,10 +33,9 @@ pub fn processor_instance(
OsRng.fill_bytes(&mut entropy);
let network_str = match network {
NetworkId::Serai => panic!("starting a processor for Serai"),
NetworkId::Bitcoin => "bitcoin",
NetworkId::Ethereum => "ethereum",
NetworkId::Monero => "monero",
ExternalNetworkId::Bitcoin => "bitcoin",
ExternalNetworkId::Ethereum => "ethereum",
ExternalNetworkId::Monero => "monero",
};
let image = format!("{network_str}-processor");
serai_docker_tests::build(image.clone());
@@ -57,7 +56,7 @@ pub fn processor_instance(
.into(),
)];
if network == NetworkId::Ethereum {
if network == ExternalNetworkId::Ethereum {
serai_docker_tests::build("ethereum-relayer".to_string());
res.push(
TestBodySpecification::with_image(
@@ -80,7 +79,7 @@ pub fn processor_instance(
pub type Handles = (String, String, String, String);
pub fn processor_stack(
network: NetworkId,
network: ExternalNetworkId,
network_hostname_override: Option<String>,
) -> (Handles, <Ristretto as Ciphersuite>::F, Vec<TestBodySpecification>) {
let (network_composition, network_rpc_port) = network_instance(network);
@@ -106,10 +105,9 @@ pub fn processor_stack(
for (name, composition) in [
Some((
match network {
NetworkId::Serai => unreachable!(),
NetworkId::Bitcoin => "bitcoin",
NetworkId::Ethereum => "ethereum",
NetworkId::Monero => "monero",
ExternalNetworkId::Bitcoin => "bitcoin",
ExternalNetworkId::Ethereum => "ethereum",
ExternalNetworkId::Monero => "monero",
},
network_composition,
)),
@@ -161,7 +159,7 @@ pub fn processor_stack(
}
pub struct Coordinator {
network: NetworkId,
network: ExternalNetworkId,
network_handle: String,
#[allow(unused)]
@@ -177,7 +175,7 @@ pub struct Coordinator {
impl Coordinator {
pub fn new(
network: NetworkId,
network: ExternalNetworkId,
ops: &DockerOperations,
handles: Handles,
coord_key: <Ristretto as Ciphersuite>::F,
@@ -213,7 +211,7 @@ impl Coordinator {
let mut iters = 0;
while iters < 60 {
match network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::rpc::Rpc;
// Bitcoin's Rpc::new will test the connection
@@ -221,7 +219,7 @@ impl Coordinator {
break;
}
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
use std::sync::Arc;
use ethereum_serai::{
alloy::{
@@ -270,7 +268,7 @@ impl Coordinator {
break;
}
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::rpc::Rpc;
@@ -284,7 +282,6 @@ impl Coordinator {
break;
}
}
NetworkId::Serai => panic!("processor is booting with external network of Serai"),
}
println!("external network RPC has yet to boot, waiting 1 sec, attempt {iters}");
@@ -337,7 +334,7 @@ impl Coordinator {
pub async fn add_block(&self, ops: &DockerOperations) -> ([u8; 32], Vec<u8>) {
let rpc_url = network_rpc(self.network, ops, &self.network_handle);
match self.network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::{
bitcoin::{consensus::Encodable, network::Network, Script, Address},
rpc::Rpc,
@@ -360,7 +357,7 @@ impl Coordinator {
block.consensus_encode(&mut block_buf).unwrap();
(hash, block_buf)
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
use ethereum_serai::alloy::{
simple_request_transport::SimpleRequest,
rpc_types::{BlockTransactionsKind, BlockNumberOrTag},
@@ -397,7 +394,7 @@ impl Coordinator {
.into_bytes();
(hash.into(), state)
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, scalar::Scalar};
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{rpc::Rpc, address::Network, ViewPair};
@@ -415,14 +412,13 @@ impl Coordinator {
let hash = rpc.get_block_hash(rpc.get_height().await.unwrap() - 1).await.unwrap();
(hash, rpc.get_block(hash).await.unwrap().serialize())
}
NetworkId::Serai => panic!("processor tests adding block to Serai"),
}
}
pub async fn sync(&self, ops: &DockerOperations, others: &[Coordinator]) {
let rpc_url = network_rpc(self.network, ops, &self.network_handle);
match self.network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::{bitcoin::consensus::Encodable, rpc::Rpc};
let rpc = Rpc::new(rpc_url).await.expect("couldn't connect to the Bitcoin RPC");
@@ -452,7 +448,7 @@ impl Coordinator {
}
}
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
use ethereum_serai::alloy::{
simple_request_transport::SimpleRequest,
rpc_types::{BlockTransactionsKind, BlockNumberOrTag},
@@ -503,7 +499,7 @@ impl Coordinator {
//assert_eq!(expected_number, new_number);
}
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::rpc::Rpc;
@@ -534,14 +530,13 @@ impl Coordinator {
}
}
}
NetworkId::Serai => panic!("processors tests syncing Serai nodes"),
}
}
pub async fn publish_transaction(&self, ops: &DockerOperations, tx: &[u8]) {
let rpc_url = network_rpc(self.network, ops, &self.network_handle);
match self.network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::{
bitcoin::{consensus::Decodable, Transaction},
rpc::Rpc,
@@ -551,7 +546,7 @@ impl Coordinator {
Rpc::new(rpc_url).await.expect("couldn't connect to the coordinator's Bitcoin RPC");
rpc.send_raw_transaction(&Transaction::consensus_decode(&mut &*tx).unwrap()).await.unwrap();
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
use ethereum_serai::alloy::{
simple_request_transport::SimpleRequest,
rpc_client::ClientBuilder,
@@ -564,7 +559,7 @@ impl Coordinator {
);
let _ = provider.send_raw_transaction(tx).await.unwrap();
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{transaction::Transaction, rpc::Rpc};
@@ -573,15 +568,15 @@ impl Coordinator {
.expect("couldn't connect to the coordinator's Monero RPC");
rpc.publish_transaction(&Transaction::read(&mut &*tx).unwrap()).await.unwrap();
}
NetworkId::Serai => panic!("processor tests broadcasting block to Serai"),
}
}
pub async fn publish_eventuality_completion(&self, ops: &DockerOperations, tx: &[u8]) {
match self.network {
NetworkId::Bitcoin | NetworkId::Monero => self.publish_transaction(ops, tx).await,
NetworkId::Ethereum => (),
NetworkId::Serai => panic!("processor tests broadcasting block to Serai"),
ExternalNetworkId::Bitcoin | ExternalNetworkId::Monero => {
self.publish_transaction(ops, tx).await
}
ExternalNetworkId::Ethereum => (),
}
}
@@ -592,7 +587,7 @@ impl Coordinator {
) -> Option<Vec<u8>> {
let rpc_url = network_rpc(self.network, ops, &self.network_handle);
match self.network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::{bitcoin::consensus::Encodable, rpc::Rpc};
let rpc =
@@ -614,7 +609,7 @@ impl Coordinator {
None
}
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
/*
let provider = RootProvider::<_, Ethereum>::new(
ClientBuilder::default().transport(SimpleRequest::new(rpc_url.clone()), true),
@@ -664,7 +659,7 @@ impl Coordinator {
None
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::rpc::Rpc;
@@ -679,7 +674,6 @@ impl Coordinator {
None
}
}
NetworkId::Serai => panic!("processor tests broadcasting block to Serai"),
}
}
}

View File

@@ -4,9 +4,9 @@ use rand_core::{RngCore, OsRng};
use scale::Encode;
use serai_client::{
primitives::{Amount, NetworkId, Coin, Balance, ExternalAddress},
validator_sets::primitives::ExternalKey,
in_instructions::primitives::{InInstruction, RefundableInInstruction, Shorthand},
primitives::{Amount, ExternalAddress, ExternalBalance, ExternalCoin, ExternalNetworkId},
validator_sets::primitives::ExternalKey,
};
use dockertest::{PullPolicy, Image, StartPolicy, TestBodySpecification, DockerOperations};
@@ -52,37 +52,32 @@ pub fn monero_instance() -> (TestBodySpecification, u32) {
(composition, XMR_PORT)
}
pub fn network_instance(network: NetworkId) -> (TestBodySpecification, u32) {
pub fn network_instance(network: ExternalNetworkId) -> (TestBodySpecification, u32) {
match network {
NetworkId::Bitcoin => bitcoin_instance(),
NetworkId::Ethereum => ethereum_instance(),
NetworkId::Monero => monero_instance(),
NetworkId::Serai => {
panic!("Serai is not a valid network to spawn an instance of for a processor")
}
ExternalNetworkId::Bitcoin => bitcoin_instance(),
ExternalNetworkId::Ethereum => ethereum_instance(),
ExternalNetworkId::Monero => monero_instance(),
}
}
pub fn network_rpc(network: NetworkId, ops: &DockerOperations, handle: &str) -> String {
pub fn network_rpc(network: ExternalNetworkId, ops: &DockerOperations, handle: &str) -> String {
let (ip, port) = ops
.handle(handle)
.host_port(match network {
NetworkId::Bitcoin => BTC_PORT,
NetworkId::Ethereum => ETH_PORT,
NetworkId::Monero => XMR_PORT,
NetworkId::Serai => panic!("getting port for external network yet it was Serai"),
ExternalNetworkId::Bitcoin => BTC_PORT,
ExternalNetworkId::Ethereum => ETH_PORT,
ExternalNetworkId::Monero => XMR_PORT,
})
.unwrap();
format!("http://{RPC_USER}:{RPC_PASS}@{ip}:{port}")
}
pub fn confirmations(network: NetworkId) -> usize {
pub fn confirmations(network: ExternalNetworkId) -> usize {
use processor::networks::*;
match network {
NetworkId::Bitcoin => Bitcoin::CONFIRMATIONS,
NetworkId::Ethereum => Ethereum::<serai_db::MemDb>::CONFIRMATIONS,
NetworkId::Monero => Monero::CONFIRMATIONS,
NetworkId::Serai => panic!("getting confirmations required for Serai"),
ExternalNetworkId::Bitcoin => Bitcoin::CONFIRMATIONS,
ExternalNetworkId::Ethereum => Ethereum::<serai_db::MemDb>::CONFIRMATIONS,
ExternalNetworkId::Monero => Monero::CONFIRMATIONS,
}
}
@@ -108,11 +103,11 @@ pub enum Wallet {
// TODO: Merge these functions with the processor's tests, which offers very similar functionality
impl Wallet {
pub async fn new(network: NetworkId, ops: &DockerOperations, handle: String) -> Wallet {
pub async fn new(network: ExternalNetworkId, ops: &DockerOperations, handle: String) -> Wallet {
let rpc_url = network_rpc(network, ops, &handle);
match network {
NetworkId::Bitcoin => {
ExternalNetworkId::Bitcoin => {
use bitcoin_serai::{
bitcoin::{
secp256k1::{SECP256K1, SecretKey},
@@ -153,7 +148,7 @@ impl Wallet {
Wallet::Bitcoin { private_key, public_key, input_tx: funds }
}
NetworkId::Ethereum => {
ExternalNetworkId::Ethereum => {
use ciphersuite::{group::ff::Field, Secp256k1};
use ethereum_serai::alloy::{
primitives::{U256, Address},
@@ -185,7 +180,7 @@ impl Wallet {
Wallet::Ethereum { rpc_url: rpc_url.clone(), key, nonce: 0 }
}
NetworkId::Monero => {
ExternalNetworkId::Monero => {
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, scalar::Scalar};
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{rpc::Rpc, address::Network, ViewPair};
@@ -210,7 +205,6 @@ impl Wallet {
last_tx: (height, block.miner_transaction.hash()),
}
}
NetworkId::Serai => panic!("creating a wallet for for Serai"),
}
}
@@ -219,7 +213,7 @@ impl Wallet {
ops: &DockerOperations,
to: &ExternalKey,
instruction: Option<InInstruction>,
) -> (Vec<u8>, Balance) {
) -> (Vec<u8>, ExternalBalance) {
match self {
Wallet::Bitcoin { private_key, public_key, ref mut input_tx } => {
use bitcoin_serai::bitcoin::{
@@ -298,7 +292,7 @@ impl Wallet {
let mut buf = vec![];
tx.consensus_encode(&mut buf).unwrap();
*input_tx = tx;
(buf, Balance { coin: Coin::Bitcoin, amount: Amount(AMOUNT) })
(buf, ExternalBalance { coin: ExternalCoin::Bitcoin, amount: Amount(AMOUNT) })
}
Wallet::Ethereum { rpc_url, key, ref mut nonce } => {
@@ -400,7 +394,10 @@ impl Wallet {
// We drop the bottom 10 decimals
(
bytes,
Balance { coin: Coin::Ether, amount: Amount(u64::try_from(eight_decimals).unwrap()) },
ExternalBalance {
coin: ExternalCoin::Ether,
amount: Amount(u64::try_from(eight_decimals).unwrap()),
},
)
}
@@ -417,7 +414,7 @@ impl Wallet {
};
use processor::{additional_key, networks::Monero};
let rpc_url = network_rpc(NetworkId::Monero, ops, handle);
let rpc_url = network_rpc(ExternalNetworkId::Monero, ops, handle);
let rpc = SimpleRequestRpc::new(rpc_url).await.expect("couldn't connect to the Monero RPC");
// Prepare inputs
@@ -485,7 +482,7 @@ impl Wallet {
last_tx.0 = current_height;
last_tx.1 = tx.hash();
(tx.serialize(), Balance { coin: Coin::Monero, amount: Amount(AMOUNT) })
(tx.serialize(), ExternalBalance { coin: ExternalCoin::Monero, amount: Amount(AMOUNT) })
}
}
}

View File

@@ -8,11 +8,12 @@ use dkg::{Participant, tests::clone_without};
use messages::{coordinator::*, SubstrateContext};
use serai_client::{
primitives::{
BlockHash, Amount, Balance, crypto::RuntimePublic, PublicKey, SeraiAddress, NetworkId,
},
in_instructions::primitives::{
InInstruction, InInstructionWithBalance, Batch, SignedBatch, batch_message,
batch_message, Batch, InInstruction, InInstructionWithBalance, SignedBatch,
},
primitives::{
crypto::RuntimePublic, Amount, BlockHash, ExternalBalance, ExternalNetworkId, PublicKey,
SeraiAddress, EXTERNAL_NETWORKS,
},
validator_sets::primitives::Session,
};
@@ -189,7 +190,7 @@ pub(crate) async fn substrate_block(
#[test]
fn batch_test() {
for network in [NetworkId::Bitcoin, NetworkId::Ethereum, NetworkId::Monero] {
for network in EXTERNAL_NETWORKS {
let (coordinators, test) = new_test(network);
test.run(|ops| async move {
@@ -255,15 +256,14 @@ fn batch_test() {
instructions: if let Some(instruction) = &instruction {
vec![InInstructionWithBalance {
instruction: instruction.clone(),
balance: Balance {
balance: ExternalBalance {
coin: balance_sent.coin,
amount: Amount(
balance_sent.amount.0 -
(2 * match network {
NetworkId::Bitcoin => Bitcoin::COST_TO_AGGREGATE,
NetworkId::Ethereum => Ethereum::<MemDb>::COST_TO_AGGREGATE,
NetworkId::Monero => Monero::COST_TO_AGGREGATE,
NetworkId::Serai => panic!("minted for Serai?"),
ExternalNetworkId::Bitcoin => Bitcoin::COST_TO_AGGREGATE,
ExternalNetworkId::Ethereum => Ethereum::<MemDb>::COST_TO_AGGREGATE,
ExternalNetworkId::Monero => Monero::COST_TO_AGGREGATE,
}),
),
},
@@ -322,7 +322,9 @@ fn batch_test() {
},
)
.await;
if instruction.is_some() || (instruction.is_none() && (network == NetworkId::Monero)) {
if instruction.is_some() ||
(instruction.is_none() && (network == ExternalNetworkId::Monero))
{
assert!(plans.is_empty());
} else {
// If no instruction was used, and the processor csn presume the origin, it'd have
@@ -335,7 +337,7 @@ fn batch_test() {
// With the latter InInstruction not existing, we should've triggered a refund if the origin
// was detectable
// Check this is trying to sign a Plan
if network != NetworkId::Monero {
if network != ExternalNetworkId::Monero {
let mut refund_id = None;
for coordinator in &mut coordinators {
match coordinator.recv_message().await {

View File

@@ -3,8 +3,8 @@ use std::{collections::HashMap, time::SystemTime};
use dkg::{Participant, ThresholdParams, tests::clone_without};
use serai_client::{
primitives::{NetworkId, BlockHash, PublicKey},
validator_sets::primitives::{Session, KeyPair},
primitives::{BlockHash, PublicKey, EXTERNAL_NETWORKS},
validator_sets::primitives::{KeyPair, Session},
};
use messages::{SubstrateContext, key_gen::KeyGenId, CoordinatorMessage, ProcessorMessage};
@@ -144,7 +144,7 @@ pub(crate) async fn key_gen(coordinators: &mut [Coordinator]) -> KeyPair {
#[test]
fn key_gen_test() {
for network in [NetworkId::Bitcoin, NetworkId::Ethereum, NetworkId::Monero] {
for network in EXTERNAL_NETWORKS {
let (coordinators, test) = new_test(network);
test.run(|ops| async move {

View File

@@ -1,7 +1,5 @@
use ciphersuite::{Ciphersuite, Ristretto};
use serai_client::primitives::NetworkId;
use dockertest::DockerTest;
use crate::*;
@@ -17,7 +15,9 @@ mod send;
pub(crate) const COORDINATORS: usize = 4;
pub(crate) const THRESHOLD: usize = ((COORDINATORS * 2) / 3) + 1;
fn new_test(network: NetworkId) -> (Vec<(Handles, <Ristretto as Ciphersuite>::F)>, DockerTest) {
fn new_test(
network: ExternalNetworkId,
) -> (Vec<(Handles, <Ristretto as Ciphersuite>::F)>, DockerTest) {
let mut coordinators = vec![];
let mut test = DockerTest::new().with_network(dockertest::Network::Isolated);
let mut eth_handle = None;
@@ -25,7 +25,7 @@ fn new_test(network: NetworkId) -> (Vec<(Handles, <Ristretto as Ciphersuite>::F)
let (handles, coord_key, compositions) = processor_stack(network, eth_handle.clone());
// TODO: Remove this once https://github.com/foundry-rs/foundry/issues/7955
// This has all processors share an Ethereum node until we can sync controlled nodes
if network == NetworkId::Ethereum {
if network == ExternalNetworkId::Ethereum {
eth_handle = eth_handle.or_else(|| Some(handles.0.clone()));
}
coordinators.push((handles, coord_key));

View File

@@ -8,9 +8,9 @@ use dkg::{Participant, tests::clone_without};
use messages::{sign::SignId, SubstrateContext};
use serai_client::{
primitives::{BlockHash, NetworkId, Amount, Balance, SeraiAddress},
coins::primitives::{OutInstruction, OutInstructionWithBalance},
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
in_instructions::primitives::{Batch, InInstruction, InInstructionWithBalance},
primitives::{Amount, BlockHash, ExternalBalance, SeraiAddress, EXTERNAL_NETWORKS},
validator_sets::primitives::Session,
};
@@ -147,7 +147,7 @@ pub(crate) async fn sign_tx(
#[test]
fn send_test() {
for network in [NetworkId::Bitcoin, NetworkId::Ethereum, NetworkId::Monero] {
for network in EXTERNAL_NETWORKS {
let (coordinators, test) = new_test(network);
test.run(|ops| async move {
@@ -202,10 +202,9 @@ fn send_test() {
let amount_minted = Amount(
balance_sent.amount.0 -
(2 * match network {
NetworkId::Bitcoin => Bitcoin::COST_TO_AGGREGATE,
NetworkId::Ethereum => Ethereum::<MemDb>::COST_TO_AGGREGATE,
NetworkId::Monero => Monero::COST_TO_AGGREGATE,
NetworkId::Serai => panic!("minted for Serai?"),
ExternalNetworkId::Bitcoin => Bitcoin::COST_TO_AGGREGATE,
ExternalNetworkId::Ethereum => Ethereum::<MemDb>::COST_TO_AGGREGATE,
ExternalNetworkId::Monero => Monero::COST_TO_AGGREGATE,
}),
);
@@ -215,7 +214,7 @@ fn send_test() {
block: BlockHash(block_with_tx.unwrap()),
instructions: vec![InInstructionWithBalance {
instruction,
balance: Balance { coin: balance_sent.coin, amount: amount_minted },
balance: ExternalBalance { coin: balance_sent.coin, amount: amount_minted },
}],
};
@@ -245,7 +244,7 @@ fn send_test() {
block: substrate_block_num,
burns: vec![OutInstructionWithBalance {
instruction: OutInstruction { address: wallet.address(), data: None },
balance: Balance { coin: balance_sent.coin, amount: amount_minted },
balance: ExternalBalance { coin: balance_sent.coin, amount: amount_minted },
}],
batches: vec![batch.batch.id],
},