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"),
}
}
}