Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
vivienbcr committed Nov 27, 2023
2 parents 2c64c94 + 317c551 commit 302f480
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 117 deletions.
109 changes: 17 additions & 92 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn get_enabled_protocol_network() -> Option<HashMap<Protocol, Vec<Network>>>
for (proto, networks) in &config.proto_opts {
let mut net_names = Vec::new();
for net in networks {
net_names.push(*net.0);
net_names.push(net.0.clone());
}
res.insert(*proto, net_names);
}
Expand Down Expand Up @@ -165,13 +165,7 @@ where
let s: Value = serde_json::from_str(&proto_config.to_string()).unwrap();
s.as_object().unwrap().iter().for_each(|(net, opts)| {
debug!("Deserialize network {}", net);
let network = Network::from(net.clone());
let network = match network {
Some(n) => n,
_ => {
panic!("Unknown network: {} found in configuration file for protocol {}", net, proto)
}
};
let network = net.clone();
let o: Value = serde_json::from_str(&opts.to_string()).unwrap();
/*
* Deserialize Network options
Expand All @@ -186,10 +180,10 @@ where
)
.unwrap();

net_opts.insert(network, net_opt);
net_opts.insert(network.clone(), net_opt);
}
None => {
net_opts.insert(network, global.networks_options.clone());
net_opts.insert(network.clone(), global.networks_options.clone());
}
}
/*
Expand Down Expand Up @@ -258,7 +252,7 @@ where
debug!(
"Protocol: {} Network: {} Providers: {}",
protocol.to_string(),
network.to_string(),
network,
providers.len()
);
net_providers.insert(network, providers);
Expand Down Expand Up @@ -709,75 +703,9 @@ impl std::fmt::Display for Protocol {
write!(f, "{}", s)
}
}
#[derive(Deserialize, Serialize, Debug, Clone, Eq, Hash, PartialEq, Copy)]
pub enum Network {
#[serde(rename = "mainnet")]
Mainnet,
#[serde(rename = "testnet")]
Testnet,
#[serde(rename = "goerli")]
Goerli,
#[serde(rename = "sepolia")]
Sepolia,
#[serde(rename = "holesky")]
Holesky,
#[serde(rename = "volta")]
Volta,
#[serde(rename = "mumbai")]
Mumbai,
#[serde(rename = "ghostnet")]
Ghostnet,
#[serde(rename = "kusama")]
Kusama,
#[serde(rename = "westend")]
Westend,
#[serde(rename = "moonriver")]
Moonriver,
#[serde(rename = "testnet2")]
Testnet2,
#[serde(rename = "fuji")]
Fuji,
}
impl Network {
fn from(s: String) -> Option<Self> {
match s.as_str() {
"mainnet" => Some(Network::Mainnet),
"testnet" => Some(Network::Testnet),
"goerli" => Some(Network::Goerli),
"sepolia" => Some(Network::Sepolia),
"holesky" => Some(Network::Holesky),
"volta" => Some(Network::Volta),
"mumbai" => Some(Network::Mumbai),
"ghostnet" => Some(Network::Ghostnet),
"kusama" => Some(Network::Kusama),
"westend" => Some(Network::Westend),
"moonriver" => Some(Network::Moonriver),
"testnet2" => Some(Network::Testnet2),
"fuji" => Some(Network::Fuji),
_ => None,
}
}
}
impl std::fmt::Display for Network {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Network::Mainnet => "mainnet",
Network::Testnet => "testnet",
Network::Goerli => "goerli",
Network::Sepolia => "sepolia",
Network::Holesky => "holesky",
Network::Volta => "volta",
Network::Mumbai => "mumbai",
Network::Ghostnet => "ghostnet",
Network::Kusama => "kusama",
Network::Westend => "westend",
Network::Moonriver => "moonriver",
Network::Testnet2 => "testnet2",
Network::Fuji => "fuji",
};
write!(f, "{}", s)
}
}

pub type Network = String;

#[derive(Serialize, Debug, Clone)]
pub struct Endpoint {
pub url: String,
Expand Down Expand Up @@ -1149,7 +1077,7 @@ mod test {

// Test bitcoin provider
let bitcoin_net_provider = config.proto_providers.get(&Protocol::Bitcoin).unwrap();
let bitcoin_mainnet_providers = bitcoin_net_provider.get(&Network::Mainnet).unwrap();
let bitcoin_mainnet_providers = bitcoin_net_provider.get("mainnet").unwrap();
assert_eq!(
bitcoin_mainnet_providers.len(),
4,
Expand Down Expand Up @@ -1271,15 +1199,14 @@ mod test {
"Bitcoin mainnet blockcypher url should be set"
);
let bitcoin_network_options = config.proto_opts.get(&Protocol::Bitcoin).unwrap();
let bitcoin_mainnet_network_options =
bitcoin_network_options.get(&Network::Mainnet).unwrap();
let bitcoin_mainnet_network_options = bitcoin_network_options.get("mainnet").unwrap();
assert_eq!(
bitcoin_mainnet_network_options.head_length, 9,
"Bitcoin mainnet head_length should be set to 9"
);
// Test ethereum provider
let ethereum_net_provider = config.proto_providers.get(&Protocol::Ethereum).unwrap();
let ethereum_mainnet_providers = ethereum_net_provider.get(&Network::Mainnet).unwrap();
let ethereum_mainnet_providers = ethereum_net_provider.get("mainnet").unwrap();
assert_eq!(
ethereum_mainnet_providers.len(),
1,
Expand Down Expand Up @@ -1311,8 +1238,7 @@ mod test {
"First Ethereum mainnet rate should be equal to 24"
);
let ethereum_network_options = config.proto_opts.get(&Protocol::Ethereum).unwrap();
let ethereum_mainnet_network_options =
ethereum_network_options.get(&Network::Mainnet).unwrap();
let ethereum_mainnet_network_options = ethereum_network_options.get("mainnet").unwrap();
assert_eq!(
ethereum_mainnet_network_options.head_length,
config.global.networks_options.head_length,
Expand All @@ -1323,7 +1249,7 @@ mod test {
"Ethereum mainnet tick_rate should be eq to global tick_rate"
);
// Test Ethereum sepolia
let ethereum_sepolia_providers = ethereum_net_provider.get(&Network::Sepolia).unwrap();
let ethereum_sepolia_providers = ethereum_net_provider.get("sepolia").unwrap();
assert_eq!(
ethereum_sepolia_providers.len(),
1,
Expand Down Expand Up @@ -1354,8 +1280,7 @@ mod test {
e.config.rate, 27,
"First Ethereum sepolia rate should be equal to 27"
);
let ethereum_sepolia_network_options =
ethereum_network_options.get(&Network::Sepolia).unwrap();
let ethereum_sepolia_network_options = ethereum_network_options.get("sepolia").unwrap();
assert_eq!(
ethereum_sepolia_network_options.head_length, config.global.networks_options.head_length,
"Ethereum sepolia head_length should be set to config.global.networks_options.head_length"
Expand Down Expand Up @@ -1426,7 +1351,7 @@ mod test {
let bitcoin_provider = proto_providers.get(&Protocol::Bitcoin).unwrap();
// should contain network mainnet
assert_eq!(
bitcoin_provider.contains_key(&Network::Mainnet),
bitcoin_provider.contains_key("mainnet"),
true,
"bitcoin_provider should contain mainnet"
);
Expand All @@ -1437,12 +1362,12 @@ mod test {
);
// sould contain 2 providers
assert_eq!(
bitcoin_provider.get(&Network::Mainnet).unwrap().len(),
bitcoin_provider.get("mainnet").unwrap().len(),
2,
"bitcoin_provider mainnet should have 2 endpoints"
);
// Each provider should have default values
let mainnet_providers = bitcoin_provider.get(&Network::Mainnet).unwrap();
let mainnet_providers = bitcoin_provider.get("mainnet").unwrap();
let expected_urls = vec![
"https://rpc-bitcoin-1.com".to_string(),
"https://rpc-bitcoin-2.com".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/bitcoin_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod test {
tests::setup();
let url = env::var("BITCOIN_NODE_URL").unwrap();
let mut bitcoin_node =
BitcoinNode::test_new(url.as_str(), Protocol::Bitcoin, Network::Mainnet);
BitcoinNode::test_new(url.as_str(), Protocol::Bitcoin, String::from("mainnet"));
let res = bitcoin_node.get_best_block_hash().await;
assert!(
res.is_ok(),
Expand All @@ -230,7 +230,7 @@ mod test {
tests::setup();
let url = env::var("BITCOIN_NODE_URL").unwrap();
let mut bitcoin_node =
BitcoinNode::test_new(url.as_str(), Protocol::Bitcoin, Network::Mainnet);
BitcoinNode::test_new(url.as_str(), Protocol::Bitcoin, String::from("mainnet"));
let res = bitcoin_node
.get_block(&"00000000000000000005bdd33e8c4ac8b3b1754f72416b9cb88ce278ea25f6ce")
.await;
Expand Down
6 changes: 4 additions & 2 deletions src/endpoints/blockcypher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ mod tests {
async fn blockcypherget_chain_height() {
tests::setup();
let url = env::var("BLOCKCYPHER_URL").unwrap();
let mut blockcypher = Blockcypher::test_new(&url, Protocol::Bitcoin, Network::Mainnet);
let mut blockcypher =
Blockcypher::test_new(&url, Protocol::Bitcoin, String::from("mainnet"));
let chain_state = blockcypher.get_chain_height().await.unwrap();
assert!(chain_state.height > 0);
}
Expand All @@ -189,7 +190,8 @@ mod tests {
let n_block = 5;
let height = 100;
let url = env::var("BLOCKCYPHER_URL").unwrap();
let mut blockcypher = Blockcypher::test_new(&url, Protocol::Bitcoin, Network::Mainnet);
let mut blockcypher =
Blockcypher::test_new(&url, Protocol::Bitcoin, String::from("mainnet"));
let res = blockcypher
.get_blocks_from_height(height, n_block)
.await
Expand Down
10 changes: 5 additions & 5 deletions src/endpoints/ethereum_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl EthereumNode {
JsonRpcParams::String(format!("0x{:x}", block_number)),
JsonRpcParams::Bool(txs),
],
id: id,
id,
};
id += 1;
batch.push(body);
Expand Down Expand Up @@ -214,7 +214,7 @@ mod test {
let mut ethereum_node = EthereumNode::test_new(
&env::var("ETHEREUM_NODE_URL").unwrap(),
Protocol::Ethereum,
Network::Mainnet,
String::from("mainnet"),
);
let block = ethereum_node
.get_block_by_number(None, false)
Expand All @@ -230,7 +230,7 @@ mod test {
let mut ethereum_node = EthereumNode::test_new(
&env::var("ETHEREUM_NODE_URL").unwrap(),
Protocol::Ethereum,
Network::Mainnet,
String::from("mainnet"),
);
let block = ethereum_node
.get_block_by_number(None, false)
Expand Down Expand Up @@ -269,7 +269,7 @@ mod test {
let mut ethereum_node = EthereumNode::test_new(
&env::var("ETHEREUM_NODE_URL").unwrap(),
Protocol::Ethereum,
Network::Mainnet,
String::from("mainnet"),
);
let res = ethereum_node.parse_top_blocks(5, None).await.unwrap();
assert_eq!(res.blocks.len(), 5);
Expand All @@ -280,7 +280,7 @@ mod test {
let mut ethereum_node = EthereumNode::test_new(
&env::var("EWF_NODE_URL").unwrap(),
Protocol::Ethereum,
Network::Mainnet,
String::from("mainnet"),
);
let res = ethereum_node.parse_top_blocks(5, None).await.unwrap();
assert_eq!(res.blocks.len(), 5);
Expand Down
8 changes: 4 additions & 4 deletions src/endpoints/polkadot_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ mod tests {
let mut endpoint = PolkadotNode::test_new(
"https://rpc.polkadot.io",
Protocol::Polkadot,
Network::Mainnet,
String::from("mainnet"),
);
let res = endpoint.parse_top_blocks(10, None).await;
assert!(res.is_ok());
Expand All @@ -225,7 +225,7 @@ mod tests {
let mut endpoint = PolkadotNode::test_new(
"https://rpc.polkadot.io",
Protocol::Polkadot,
Network::Mainnet,
String::from("mainnet"),
);
let res = endpoint.get_finalized_head().await;
assert!(res.is_ok());
Expand All @@ -237,7 +237,7 @@ mod tests {
let mut endpoint = PolkadotNode::test_new(
"https://rpc.polkadot.io",
Protocol::Polkadot,
Network::Mainnet,
String::from("mainnet"),
);
let res = endpoint
.get_blocks(vec![
Expand Down Expand Up @@ -266,7 +266,7 @@ mod tests {
let mut endpoint = PolkadotNode::test_new(
"https://rpc.polkadot.io",
Protocol::Polkadot,
Network::Mainnet,
String::from("mainnet"),
);
let block_hashs = vec![
"0xf752e2ec759fc921b7fb63f5c4c3798ed608059dc239dfb2f2d41bb50190dd35".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/starknet_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ mod tests {
let mut starknet_node = StarknetNode::test_new(
&env::var("STARKNET_NODE_URL").unwrap(),
Protocol::Starknet,
Network::Mainnet,
String::from("mainnet"),
);
let head = starknet_node.get_head().await;
assert!(head.is_ok());
Expand All @@ -201,7 +201,7 @@ mod tests {
let mut starknet_node = StarknetNode::test_new(
&env::var("STARKNET_NODE_URL").unwrap(),
Protocol::Starknet,
Network::Mainnet,
String::from("mainnet"),
);
let head = starknet_node.get_head().await.unwrap();
let mut block_numbers = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/subscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ mod tests {
let mut subscan = Subscan::test_new(
"https://polkadot.api.subscan.io",
Protocol::Polkadot,
Network::Mainnet,
String::from("mainnet"),
);
let blockchain = subscan.parse_top_blocks(5, None).await;
assert!(blockchain.is_ok(), "Subscan should return a blockchain");
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/tezos_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod tests {
async fn tezos_get_block() {
tests::setup();
let url = env::var("TEZOS_NODE_URL").unwrap();
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, Network::Mainnet);
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, String::from("mainnet"));
let r = tezos_node.get_block(None).await;
assert!(r.is_ok());
let block_head = r.unwrap();
Expand All @@ -267,7 +267,7 @@ mod tests {
async fn tezos_count_tx() {
tests::setup();
let url = env::var("TEZOS_NODE_URL").unwrap();
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, Network::Mainnet);
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, String::from("mainnet"));
let r = tezos_node.get_block(None).await;

assert!(r.is_ok());
Expand All @@ -276,7 +276,7 @@ mod tests {
async fn tezos_parse_top_blocks() {
tests::setup();
let url = env::var("TEZOS_NODE_URL").unwrap();
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, Network::Mainnet);
let mut tezos_node = TezosNode::test_new(&url, Protocol::Tezos, String::from("mainnet"));
let r = tezos_node.parse_top_blocks(10, None).await;
assert!(r.is_ok());
let blockchain = r.unwrap();
Expand Down
Loading

0 comments on commit 302f480

Please sign in to comment.