Skip to content

Commit

Permalink
Add a draft of tx submit test
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Jun 11, 2020
1 parent ff44ea7 commit 373f093
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 44 deletions.
34 changes: 28 additions & 6 deletions core/loadtest/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ impl RpcClient {
Ok(tx_hash)
}

/// Sends the transaction to the ZKSync server and returns raw response.
pub async fn send_tx_raw(
&self,
tx: FranklinTx,
eth_signature: Option<PackedEthSignature>,
) -> Result<Output, failure::Error> {
let msg = JsonRpcRequest::submit_tx(tx, eth_signature);

self.post_raw(&msg).await
}

/// Requests and returns information about a ZKSync account given its address.
pub async fn account_state_info(
&self,
Expand Down Expand Up @@ -123,6 +134,22 @@ impl RpcClient {
&self,
message: impl serde::Serialize,
) -> Result<serde_json::Value, failure::Error> {
let reply: Output = self.post_raw(message).await?;

let ret = match reply {
Output::Success(v) => v.result,
Output::Failure(v) => failure::bail!("RPC error: {}", v.error),
};

Ok(ret)
}

/// Performs a POST query to the JSON RPC endpoint,
/// and decodes the response, returning the decoded `serde_json::Value`.
/// `Ok` is returned only for successful calls, for any kind of error
/// the `Err` variant is returned (including the failed RPC method
/// execution response).
async fn post_raw(&self, message: impl serde::Serialize) -> Result<Output, failure::Error> {
let res = self
.client
.post(&self.rpc_addr)
Expand All @@ -137,12 +164,7 @@ impl RpcClient {
}
let reply: Output = res.json().await.unwrap();

let ret = match reply {
Output::Success(v) => v.result,
Output::Failure(v) => failure::bail!("RPC error: {}", v.error),
};

Ok(ret)
Ok(reply)
}
}

Expand Down
78 changes: 40 additions & 38 deletions core/loadtest/src/scenarios/api_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
//! creating an integration test will be more fluent.
// Built-in deps
use std::time::{Duration, Instant};
use std::time::Duration;
// External deps
use num::BigUint;
use tokio::time;
use web3::transports::{EventLoopHandle, Http};
// Workspace deps
use models::{
Expand All @@ -23,6 +22,7 @@ use models::{
};
use testkit::zksync_account::ZksyncAccount;
// Local deps
use self::submit_tx::SubmitTxTester;
use crate::{
rpc_client::RpcClient,
scenarios::{
Expand All @@ -34,8 +34,10 @@ use crate::{
test_accounts::TestAccount,
};

mod submit_tx;

#[derive(Debug)]
struct TestExecutor {
pub struct TestExecutor {
rpc_client: RpcClient,

/// Main account to be used in test.
Expand Down Expand Up @@ -87,10 +89,10 @@ impl TestExecutor {
pub async fn run_test(&mut self) -> Result<(), failure::Error> {
self.initialize().await?;
self.deposit().await?;
// self.initial_transfer().await?;
// self.funds_rotation().await?;
// self.collect_funds().await?;
// self.withdraw().await?;

// Actual test runners.
SubmitTxTester::new(self).run().await?;

self.finish().await?;

Ok(())
Expand Down Expand Up @@ -174,16 +176,16 @@ impl TestExecutor {
Ok(())
}

// /// Obtains a fee required for the transfer operation.
// async fn transfer_fee(&self, to_acc: &ZksyncAccount) -> BigUint {
// let fee = self
// .rpc_client
// .get_tx_fee("Transfer", to_acc.address, "ETH")
// .await
// .expect("Can't get tx fee");
/// Obtains a fee required for the transfer operation.
async fn transfer_fee(&self, to_acc: &ZksyncAccount) -> BigUint {
let fee = self
.rpc_client
.get_tx_fee("Transfer", to_acc.address, "ETH")
.await
.expect("Can't get tx fee");

// closest_packable_fee_amount(&fee)
// }
closest_packable_fee_amount(&fee)
}

/// Obtains a fee required for the withdraw operation.
async fn withdraw_fee(&self, to_acc: &ZksyncAccount) -> BigUint {
Expand All @@ -196,28 +198,28 @@ impl TestExecutor {
closest_packable_fee_amount(&fee)
}

// /// Creates a signed transfer transaction.
// /// Sender and receiver are chosen from the generated
// /// accounts, determined by its indices.
// fn sign_transfer(
// &self,
// from: &ZksyncAccount,
// to: &ZksyncAccount,
// amount: impl Into<BigUint>,
// fee: impl Into<BigUint>,
// ) -> (FranklinTx, Option<PackedEthSignature>) {
// let (tx, eth_signature) = from.sign_transfer(
// 0, // ETH
// "ETH",
// amount.into(),
// fee.into(),
// &to.address,
// None,
// true,
// );

// (FranklinTx::Transfer(Box::new(tx)), Some(eth_signature))
// }
/// Creates a signed transfer transaction.
/// Sender and receiver are chosen from the generated
/// accounts, determined by its indices.
fn sign_transfer(
&self,
from: &ZksyncAccount,
to: &ZksyncAccount,
amount: impl Into<BigUint>,
fee: impl Into<BigUint>,
) -> (FranklinTx, Option<PackedEthSignature>) {
let (tx, eth_signature) = from.sign_transfer(
0, // ETH
"ETH",
amount.into(),
fee.into(),
&to.address,
None,
true,
);

(FranklinTx::Transfer(Box::new(tx)), Some(eth_signature))
}
}

/// Runs the real-life test scenario.
Expand Down
55 changes: 55 additions & 0 deletions core/loadtest/src/scenarios/api_test/submit_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Tests for `submit_tx` RPC method.
// External deps
use jsonrpc_core::types::response::Output;
// Workspace deps
use models::node::tx::PackedEthSignature;
// Local deps
use super::TestExecutor;

pub struct SubmitTxTester<'a>(&'a TestExecutor);

impl<'a> SubmitTxTester<'a> {
pub fn new(executor: &'a TestExecutor) -> Self {
Self(executor)
}

pub async fn run(self) -> Result<(), failure::Error> {
self.incorrect_signature().await?;

Ok(())
}

pub async fn incorrect_signature(&self) -> Result<(), failure::Error> {
let main_account = &self.0.main_account;

let transfer_fee = self.0.transfer_fee(&main_account.zk_acc).await;

let (transfer, _) = self.0.sign_transfer(
&main_account.zk_acc,
&main_account.zk_acc,
1u32,
transfer_fee,
);

let fake_signature =
PackedEthSignature::deserialize_packed(&[0; 65]).expect("Can't deserialize signature");
let eth_sign = Some(fake_signature);

let reply = self.0.rpc_client.send_tx_raw(transfer, eth_sign).await?;

match reply {
Output::Success(v) => {
failure::bail!(
"Got successful response for an incorrect signature: {:?}",
v
);
}
Output::Failure(v) => {
println!("Failed as expected: {:?}", v);
}
};

Ok(())
}
}

0 comments on commit 373f093

Please sign in to comment.