Skip to content

Commit

Permalink
Merge branch 'feature/remove-unecessary-params-from-rosetta-client' i…
Browse files Browse the repository at this point in the history
…nto arbitrum_local_testnet_setup
  • Loading branch information
ManojJiSharma committed Dec 14, 2023
2 parents 0fc302c + 4733f47 commit e262998
Show file tree
Hide file tree
Showing 22 changed files with 1,090 additions and 351 deletions.
356 changes: 355 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions chains/arbitrum/server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Result;
use rosetta_config_ethereum::{EthereumMetadata, EthereumMetadataParams};
use rosetta_config_ethereum::{
EthereumMetadata, EthereumMetadataParams, Query as EthQuery, QueryResult as EthQueryResult,
};
use rosetta_core::{
crypto::{address::Address, PublicKey},
types::{
Expand Down Expand Up @@ -50,6 +52,8 @@ impl BlockchainClient for ArbitrumClient {
type MetadataParams = ArbitrumMetadataParams;
type Metadata = ArbitrumMetadata;
type EventStream<'a> = <MaybeWsEthereumClient as BlockchainClient>::EventStream<'a>;
type Call = EthQuery;
type CallResult = EthQueryResult;

fn config(&self) -> &BlockchainConfig {
self.client.config()
Expand Down Expand Up @@ -112,7 +116,7 @@ impl BlockchainClient for ArbitrumClient {
self.client.block_transaction(block_identifier, tx).await
}

async fn call(&self, req: &CallRequest) -> Result<Value> {
async fn call(&self, req: &EthQuery) -> Result<EthQueryResult> {
self.client.call(req).await
}

Expand Down
3 changes: 2 additions & 1 deletion chains/arbitrum/testing/rosetta-testing-arbitrum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"

[dev-dependencies]
alloy-sol-types = { version = "0.5" }
ethers-solc = "2.0"
sha3 = "0.10"
url = "2.4"
url = "2.4"
80 changes: 45 additions & 35 deletions chains/arbitrum/testing/rosetta-testing-arbitrum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,37 @@ impl ArbitrumEnv {

#[cfg(test)]
mod tests {
use alloy_sol_types::{sol, SolCall};
use ethers::{
providers::{Http, Middleware, Provider},
signers::{LocalWallet, Signer},
types::{
transaction::eip2718::TypedTransaction, Bytes, TransactionRequest, H160, U256, U64,
transaction::eip2718::TypedTransaction, Bytes, TransactionRequest, H160, H256, U256,
U64,
},
utils::hex,
};
use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc};
use rosetta_client::Wallet;
use rosetta_config_ethereum::{AtBlock, CallResult};
use rosetta_core::{types::PartialBlockIdentifier, BlockchainClient};
use rosetta_server_arbitrum::ArbitrumClient;
use sha3::Digest;
use std::{collections::BTreeMap, path::Path, str::FromStr, thread, time::Duration};
use url::Url;

sol! {
interface TestContract {
event AnEvent();
function emitEvent() external;

function identity(bool a) external view returns (bool);
}
}

use super::*;

//Test for start the arbitrum default node (nitro-testnode)
#[tokio::test]
#[ignore]
async fn start_new() {
match ArbitrumEnv::new().await {
Ok(arbitrum_env) => {
Expand All @@ -111,7 +121,6 @@ mod tests {
}

#[tokio::test]
#[ignore]
async fn cleanup_success() {
// Assuming cleanup is successful
let result = ArbitrumEnv::cleanup().await;
Expand All @@ -128,7 +137,6 @@ mod tests {

//must run this test before running below tests.
#[tokio::test]
#[ignore]
pub async fn for_incress_blocknumber() -> Result<()> {
let rpc_url_str = "http://localhost:8547";
let rpc_url = Url::parse(rpc_url_str).expect("Invalid URL");
Expand Down Expand Up @@ -171,7 +179,6 @@ mod tests {
}

#[tokio::test]
#[ignore]
async fn network_status() {
match ArbitrumClient::new("dev", "ws://127.0.0.1:8548").await {
Ok(client) => {
Expand Down Expand Up @@ -238,7 +245,6 @@ mod tests {
}

#[tokio::test]
#[ignore]
async fn test_account() {
let result = ArbitrumClient::new("dev", "ws://127.0.0.1:8548").await;
assert!(result.is_ok(), "Error creating ArbitrumClient");
Expand Down Expand Up @@ -293,7 +299,6 @@ mod tests {
}

#[tokio::test]
#[ignore]
#[allow(clippy::needless_raw_string_hashes)]
async fn test_smart_contract() -> Result<()> {
let result = ArbitrumClient::new("dev", "ws://127.0.0.1:8548").await;
Expand All @@ -313,31 +318,30 @@ mod tests {
.await?;

let bytes = compile_snippet(
r#"
r"
event AnEvent();
function emitEvent() public {
emit AnEvent();
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let contract_address =
receipt.get("contractAddress").and_then(serde_json::Value::as_str).unwrap();
let tx_hash =
wallet.eth_send_call(contract_address, "function emitEvent()", &[], 0).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let logs = receipt.get("logs").and_then(serde_json::Value::as_array).unwrap();
assert_eq!(logs.len(), 1);
let topic = logs[0]["topics"][0].as_str().unwrap();
let expected = format!("0x{}", hex::encode(sha3::Keccak256::digest("AnEvent()")));
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let contract_address = receipt.contract_address.unwrap();
let tx_hash = {
let call = TestContract::emitEventCall {};
wallet.eth_send_call(contract_address.0, call.abi_encode(), 0).await?
};
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
assert_eq!(receipt.logs.len(), 1);
let topic = receipt.logs[0].topics[0];
let expected = H256(sha3::Keccak256::digest("AnEvent()").into());
assert_eq!(topic, expected);

Ok(())
}

#[tokio::test]
#[ignore]
#[allow(clippy::needless_raw_string_hashes)]
async fn test_smart_contract_view() -> Result<()> {
let result = ArbitrumClient::new("dev", "ws://127.0.0.1:8548").await;
Expand All @@ -356,26 +360,32 @@ mod tests {
)
.await?;
let bytes = compile_snippet(
r#"
r"
function identity(bool a) public view returns (bool) {
return a;
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let contract_address = receipt["contractAddress"].as_str().unwrap();

let response = wallet
.eth_view_call(
contract_address,
"function identity(bool a) returns (bool)",
&["true".into()],
None,
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let contract_address = receipt.contract_address.unwrap();

let response = {
let call = TestContract::identityCall { a: true };
wallet
.eth_view_call(contract_address.0, call.abi_encode(), AtBlock::Latest)
.await?
};
assert_eq!(
response,
CallResult::Success(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1
]
.to_vec()
)
.await?;
let result: Vec<String> = serde_json::from_value(response)?;
assert_eq!(result[0], "true");
);
Ok(())
}
}
2 changes: 2 additions & 0 deletions chains/astar/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ subxt = { workspace = true, features = ["substrate-compat"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

[dev-dependencies]
alloy-primitives = { version = "0.5" }
alloy-sol-types = { version = "0.5" }
ethers-solc = "2.0"
rosetta-client.workspace = true
rosetta-docker = { workspace = true, features = ["tests"] }
Expand Down
82 changes: 51 additions & 31 deletions chains/astar/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use rosetta_config_astar::metadata::{
dev as astar_metadata,
dev::runtime_types::{frame_system::AccountInfo, pallet_balances::types::AccountData},
};
use rosetta_config_ethereum::{EthereumMetadata, EthereumMetadataParams};
use rosetta_config_ethereum::{
EthereumMetadata, EthereumMetadataParams, Query as EthQuery, QueryResult as EthQueryResult,
};
use rosetta_core::{
crypto::{
address::{Address, AddressFormat},
PublicKey,
},
types::{
Block, BlockIdentifier, CallRequest, Coin, PartialBlockIdentifier, Transaction,
TransactionIdentifier,
Block, BlockIdentifier, Coin, PartialBlockIdentifier, Transaction, TransactionIdentifier,
},
BlockchainClient, BlockchainConfig,
};
use rosetta_server::ws::default_client;
use rosetta_server_ethereum::MaybeWsEthereumClient;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sp_core::crypto::Ss58AddressFormat;
use std::sync::Arc;
use subxt::{
Expand Down Expand Up @@ -124,6 +124,8 @@ impl BlockchainClient for AstarClient {
type MetadataParams = AstarMetadataParams;
type Metadata = AstarMetadata;
type EventStream<'a> = <MaybeWsEthereumClient as BlockchainClient>::EventStream<'a>;
type Call = EthQuery;
type CallResult = EthQueryResult;

fn config(&self) -> &BlockchainConfig {
self.client.config()
Expand Down Expand Up @@ -217,7 +219,6 @@ impl BlockchainClient for AstarClient {
async fn block(&self, block_identifier: &PartialBlockIdentifier) -> Result<Block> {
self.client.block(block_identifier).await
}


async fn block_transaction(
&self,
Expand All @@ -227,7 +228,7 @@ impl BlockchainClient for AstarClient {
self.client.block_transaction(block_identifier, tx).await
}

async fn call(&self, req: &CallRequest) -> Result<Value> {
async fn call(&self, req: &EthQuery) -> Result<EthQueryResult> {
self.client.call(req).await
}

Expand All @@ -236,14 +237,26 @@ impl BlockchainClient for AstarClient {
}
}

#[allow(clippy::ignored_unit_patterns)]
#[cfg(test)]
mod tests {
use super::*;
use alloy_sol_types::{sol, SolCall};
use ethers_solc::{artifacts::Source, CompilerInput, EvmVersion, Solc};
use rosetta_config_ethereum::{AtBlock, CallResult};
use rosetta_docker::Env;
use sha3::Digest;
use std::{collections::BTreeMap, path::Path};

sol! {
interface TestContract {
event AnEvent();
function emitEvent() external;

function identity(bool a) external view returns (bool);
}
}

pub async fn client_from_config(config: BlockchainConfig) -> Result<AstarClient> {
let url = config.node_uri.to_string();
AstarClient::from_config(config, url.as_str()).await
Expand Down Expand Up @@ -304,24 +317,25 @@ mod tests {
wallet.faucet(faucet, None).await?;

let bytes = compile_snippet(
r#"
r"
event AnEvent();
function emitEvent() public {
emit AnEvent();
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;

let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let contract_address = receipt.get("contractAddress").and_then(Value::as_str).unwrap();
let tx_hash =
wallet.eth_send_call(contract_address, "function emitEvent()", &[], 0).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let logs = receipt.get("logs").and_then(Value::as_array).unwrap();
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let contract_address = receipt.contract_address.unwrap();
let tx_hash = {
let data = TestContract::emitEventCall::SELECTOR.to_vec();
wallet.eth_send_call(contract_address.0, data, 0).await?
};
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let logs = receipt.logs;
assert_eq!(logs.len(), 1);
let topic = logs[0]["topics"][0].as_str().unwrap();
let expected = format!("0x{}", hex::encode(sha3::Keccak256::digest("AnEvent()")));
let topic = logs[0].topics[0];
let expected = H256::from_slice(sha3::Keccak256::digest("AnEvent()").as_ref());
assert_eq!(topic, expected);
env.shutdown().await?;
Ok(())
Expand All @@ -339,26 +353,32 @@ mod tests {
wallet.faucet(faucet, None).await?;

let bytes = compile_snippet(
r#"
r"
function identity(bool a) public view returns (bool) {
return a;
}
"#,
",
)?;
let tx_hash = wallet.eth_deploy_contract(bytes).await?;
let receipt = wallet.eth_transaction_receipt(&tx_hash).await?;
let contract_address = receipt["contractAddress"].as_str().unwrap();

let response = wallet
.eth_view_call(
contract_address,
"function identity(bool a) returns (bool)",
&["true".into()],
None,
let receipt = wallet.eth_transaction_receipt(tx_hash).await?.unwrap();
let contract_address = receipt.contract_address.unwrap();

let response = {
let call = TestContract::identityCall { a: true };
wallet
.eth_view_call(contract_address.0, call.abi_encode(), AtBlock::Latest)
.await?
};
assert_eq!(
response,
CallResult::Success(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1
]
.to_vec()
)
.await?;
let result: Vec<String> = serde_json::from_value(response)?;
assert_eq!(result[0], "true");
);
env.shutdown().await?;
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions chains/bitcoin/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rosetta-config-bitcoin.workspace = true
rosetta-core.workspace = true
serde_json = "1.0"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
void = "1.0"

[dev-dependencies]
rosetta-docker = { workspace = true, features = ["tests"] }
Loading

0 comments on commit e262998

Please sign in to comment.