Skip to content

Commit

Permalink
migrate forge interface public usage context and chain info to use re…
Browse files Browse the repository at this point in the history
…st api
  • Loading branch information
Xiao Li authored and bors-libra committed Dec 17, 2021
1 parent e85ec8f commit 32ae356
Show file tree
Hide file tree
Showing 25 changed files with 545 additions and 392 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions json-rpc/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bcs = "0.1.2"
hex = "0.4.3"
reqwest = { version = "0.11.2", features = ["blocking", "json"], default_features = false }
serde_json = "1.0.64"
tokio = { version = "1.8.1", features = ["full"] }

diem-json-rpc-types = { path = "../types" }
diem-sdk = { path = "../../sdk" }
Expand Down
10 changes: 6 additions & 4 deletions json-rpc/integration-tests/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use diem_sdk::{
};
use forge::PublicUsageContext;
use serde_json::{json, Value};
use tokio::runtime::Runtime;

pub struct JsonRpcTestHelper {
url: String,
Expand Down Expand Up @@ -158,17 +159,18 @@ impl JsonRpcTestHelper {
let mut parent = ctx.random_account();
let child1 = ctx.random_account();
let child2 = ctx.random_account();
ctx.create_parent_vasp_account(parent.authentication_key())?;
let runtime = Runtime::new().unwrap();
runtime.block_on(ctx.create_parent_vasp_account(parent.authentication_key()))?;
self.submit_and_wait(&parent.sign_with_transaction_builder(
factory.create_child_vasp_account(Currency::XUS, child1.authentication_key(), false, 0),
));
self.submit_and_wait(&parent.sign_with_transaction_builder(
factory.create_child_vasp_account(Currency::XUS, child2.authentication_key(), false, 0),
));

ctx.fund(parent.address(), amount)?;
ctx.fund(child1.address(), amount)?;
ctx.fund(child2.address(), amount)?;
runtime.block_on(ctx.fund(parent.address(), amount))?;
runtime.block_on(ctx.fund(child1.address(), amount))?;
runtime.block_on(ctx.fund(child2.address(), amount))?;

Ok((parent, child1, child2))
}
Expand Down
82 changes: 51 additions & 31 deletions json-rpc/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::{
ops::Deref,
str::FromStr,
};
use tokio::runtime::Runtime;

mod helper;
use helper::JsonRpcTestHelper;
Expand Down Expand Up @@ -94,10 +95,11 @@ impl PublicUsageTest for BlockMetadata {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());

let runtime = Runtime::new().unwrap();
// Fund and account to ensure there are some txns on the chain
let account = ctx.random_account();
ctx.create_parent_vasp_account(account.authentication_key())?;
ctx.fund(account.address(), 10)?;
runtime.block_on(ctx.create_parent_vasp_account(account.authentication_key()))?;
runtime.block_on(ctx.fund(account.address(), 10))?;

// batch request
let resp = env.send_request(json!([
Expand Down Expand Up @@ -166,9 +168,10 @@ impl Test for OldMetadata {

impl PublicUsageTest for OldMetadata {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let runtime = Runtime::new().unwrap();
// create a random accound in order to force the version hieght to be greater than 1
let account = ctx.random_account();
ctx.create_parent_vasp_account(account.authentication_key())?;
runtime.block_on(ctx.create_parent_vasp_account(account.authentication_key()))?;

let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let resp = env.send("get_metadata", json!([1]));
Expand Down Expand Up @@ -246,9 +249,10 @@ impl Test for DesignatedDealerPreburns {

impl PublicUsageTest for DesignatedDealerPreburns {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let runtime = Runtime::new().unwrap();
let factory = ctx.transaction_factory();
let mut dd = ctx.random_account();
ctx.create_designated_dealer_account(dd.authentication_key())?;
runtime.block_on(ctx.create_designated_dealer_account(dd.authentication_key()))?;

let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let address = format!("{:x}", dd.address());
Expand Down Expand Up @@ -301,7 +305,7 @@ impl PublicUsageTest for DesignatedDealerPreburns {
);

// Fund the DD account and create some Pre-burns
ctx.fund(dd.address(), 400)?;
runtime.block_on(ctx.fund(dd.address(), 400))?;

env.submit_and_wait(&dd.sign_with_transaction_builder(
factory.script(stdlib::encode_preburn_script(xus_tag(), 100)),
Expand Down Expand Up @@ -396,8 +400,9 @@ impl Test for ParentVaspAccountRole {

impl PublicUsageTest for ParentVaspAccountRole {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let runtime = Runtime::new().unwrap();
let vasp = ctx.random_account();
ctx.create_parent_vasp_account(vasp.authentication_key())?;
runtime.block_on(ctx.create_parent_vasp_account(vasp.authentication_key()))?;

let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let address = format!("{:x}", vasp.address());
Expand Down Expand Up @@ -449,9 +454,10 @@ impl PublicUsageTest for GetAccountByVersion {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let factory = ctx.transaction_factory();

let runtime = Runtime::new().unwrap();
let mut vasp = ctx.random_account();
let child = ctx.random_account();
ctx.create_parent_vasp_account(vasp.authentication_key())?;
runtime.block_on(ctx.create_parent_vasp_account(vasp.authentication_key()))?;
env.submit_and_wait(&vasp.sign_with_transaction_builder(
factory.create_child_vasp_account(Currency::XUS, child.authentication_key(), false, 0),
));
Expand Down Expand Up @@ -508,9 +514,10 @@ impl PublicUsageTest for ChildVaspAccountRole {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let factory = ctx.transaction_factory();

let runtime = Runtime::new().unwrap();
let mut parent = ctx.random_account();
let child = ctx.random_account();
ctx.create_parent_vasp_account(parent.authentication_key())?;
runtime.block_on(ctx.create_parent_vasp_account(parent.authentication_key()))?;
env.submit_and_wait(&parent.sign_with_transaction_builder(
factory.create_child_vasp_account(Currency::XUS, child.authentication_key(), false, 0),
));
Expand Down Expand Up @@ -840,9 +847,12 @@ impl AdminTest for PreburnAndBurnEvents {
let env = JsonRpcTestHelper::new(ctx.chain_info().json_rpc().to_owned());
let factory = ctx.chain_info().transaction_factory();
let mut dd = ctx.random_account();
ctx.chain_info()
.create_designated_dealer_account(Currency::XUS, dd.authentication_key())?;
ctx.chain_info().fund(Currency::XUS, dd.address(), 1000)?;
let runtime = Runtime::new().unwrap();
runtime.block_on(
ctx.chain_info()
.create_designated_dealer_account(Currency::XUS, dd.authentication_key()),
)?;
runtime.block_on(ctx.chain_info().fund(Currency::XUS, dd.address(), 1000))?;

let script = stdlib::encode_preburn_script(Currency::XUS.type_tag(), 100);
let txn = dd.sign_with_transaction_builder(factory.script(script.clone()));
Expand Down Expand Up @@ -958,9 +968,12 @@ impl AdminTest for CancleBurnEvent {
let env = JsonRpcTestHelper::new(ctx.chain_info().json_rpc().to_owned());
let factory = ctx.chain_info().transaction_factory();
let mut dd = ctx.random_account();
ctx.chain_info()
.create_designated_dealer_account(Currency::XUS, dd.authentication_key())?;
ctx.chain_info().fund(Currency::XUS, dd.address(), 1000)?;
let runtime = Runtime::new().unwrap();
runtime.block_on(
ctx.chain_info()
.create_designated_dealer_account(Currency::XUS, dd.authentication_key()),
)?;
runtime.block_on(ctx.chain_info().fund(Currency::XUS, dd.address(), 1000))?;

let txn = dd.sign_with_transaction_builder(factory.preburn(Currency::XUS, 100));
env.submit_and_wait(&txn);
Expand Down Expand Up @@ -1319,8 +1332,9 @@ impl PublicUsageTest for GetAccountTransactionsWithoutEvents {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());
let account = ctx.random_account();
ctx.create_parent_vasp_account(account.authentication_key())?;
ctx.fund(account.address(), 10)?;
let runtime = Runtime::new().unwrap();
runtime.block_on(ctx.create_parent_vasp_account(account.authentication_key()))?;
runtime.block_on(ctx.fund(account.address(), 10))?;
let response = env.send(
"get_account_transactions",
json!([treasury_compliance_account_address(), 0, 1000, false]),
Expand Down Expand Up @@ -1370,11 +1384,11 @@ impl Test for GetTransactionsWithProofs {
impl PublicUsageTest for GetTransactionsWithProofs {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());

let runtime = Runtime::new().unwrap();
// Fund and account to ensure there are some txns on the chain
let account = ctx.random_account();
ctx.create_parent_vasp_account(account.authentication_key())?;
ctx.fund(account.address(), 10)?;
runtime.block_on(ctx.create_parent_vasp_account(account.authentication_key()))?;
runtime.block_on(ctx.fund(account.address(), 10))?;

let resp = env.send("get_metadata", json!([]));

Expand Down Expand Up @@ -1497,10 +1511,12 @@ impl AdminTest for AddAndRemoveVaspDomain {
fn run<'t>(&self, ctx: &mut AdminContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.chain_info().json_rpc().to_owned());
let factory = ctx.chain_info().transaction_factory();

let runtime = Runtime::new().unwrap();
let vasp = ctx.random_account();
ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key())?;
runtime.block_on(
ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key()),
)?;

// add domain
let domain = DiemIdVaspDomainIdentifier::new("diem")
Expand Down Expand Up @@ -1626,11 +1642,11 @@ impl Test for GetEventsWithProofs {
impl PublicUsageTest for GetEventsWithProofs {
fn run<'t>(&self, ctx: &mut PublicUsageContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.url().to_owned());

let runtime = Runtime::new().unwrap();
// Fund and account to ensure there are some txns on the chain
let account = ctx.random_account();
ctx.create_parent_vasp_account(account.authentication_key())?;
ctx.fund(account.address(), 10)?;
runtime.block_on(ctx.create_parent_vasp_account(account.authentication_key()))?;
runtime.block_on(ctx.fund(account.address(), 10))?;

let responses = env.send_request(json!([
{"jsonrpc": "2.0", "method": "get_state_proof", "params": json!([0]), "id": 1},
Expand Down Expand Up @@ -1897,12 +1913,14 @@ impl Test for MultiAgentRotateAuthenticationKeyAdminScriptFunction {
impl AdminTest for MultiAgentRotateAuthenticationKeyAdminScriptFunction {
fn run<'t>(&self, ctx: &mut AdminContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.chain_info().json_rpc().to_owned());

let runtime = Runtime::new().unwrap();
// ScriptFunction
let mut vasp = ctx.random_account();

ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key())?;
runtime.block_on(
ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key()),
)?;

let new_key = AccountKey::generate(ctx.rng());
let txn = env.create_multi_agent_txn(
Expand Down Expand Up @@ -1967,12 +1985,14 @@ impl Test for MultiAgentRotateAuthenticationKeyAdminScript {
impl AdminTest for MultiAgentRotateAuthenticationKeyAdminScript {
fn run<'t>(&self, ctx: &mut AdminContext<'t>) -> Result<()> {
let env = JsonRpcTestHelper::new(ctx.chain_info().json_rpc().to_owned());

let runtime = Runtime::new().unwrap();
// Script
let mut vasp = ctx.random_account();

ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key())?;
runtime.block_on(
ctx.chain_info()
.create_parent_vasp_account(Currency::XUS, vasp.authentication_key()),
)?;
let new_key = AccountKey::generate(ctx.rng());
let txn = env.create_multi_agent_txn(
ctx.chain_info().root_account(),
Expand Down
1 change: 1 addition & 0 deletions testsuite/forge-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"
[dependencies]
anyhow = { version = "1.0", features = ["backtrace"] }
diem-sdk = { path = "../../sdk" }
diem-rest-client = { path = "../../crates/diem-rest-client"}
forge = { path = "../forge" }
itertools = "0.10.0"
rand_core = "0.6.2"
Expand Down
Loading

0 comments on commit 32ae356

Please sign in to comment.