2019-01-21 10:48:40 -08:00
|
|
|
use bincode::serialize;
|
2019-01-29 17:03:32 -08:00
|
|
|
use log::*;
|
2019-01-21 10:48:40 -08:00
|
|
|
use reqwest;
|
|
|
|
use reqwest::header::CONTENT_TYPE;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use solana::rpc_request::get_rpc_request_str;
|
2019-01-28 10:53:29 -08:00
|
|
|
use solana::thin_client::new_fullnode;
|
2019-01-29 17:03:32 -08:00
|
|
|
use solana_sdk::hash::Hash;
|
2019-01-21 10:48:40 -08:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
|
|
use solana_sdk::system_transaction::SystemTransaction;
|
|
|
|
use std::fs::remove_dir_all;
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rpc_send_tx() {
|
2019-01-29 17:03:32 -08:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-02-26 19:26:42 -08:00
|
|
|
let (server, leader_data, alice, ledger_path) = new_fullnode();
|
2019-02-05 08:03:52 -08:00
|
|
|
let server_exit = server.run(None);
|
2019-01-21 10:48:40 -08:00
|
|
|
let bob_pubkey = Keypair::new().pubkey();
|
|
|
|
|
2019-01-29 17:03:32 -08:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "getLastId",
|
|
|
|
"params": json!([])
|
|
|
|
});
|
|
|
|
let rpc_addr = leader_data.rpc;
|
|
|
|
let rpc_string = get_rpc_request_str(rpc_addr, false);
|
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
|
|
|
let last_id_vec = bs58::decode(json["result"].as_str().unwrap())
|
|
|
|
.into_vec()
|
|
|
|
.unwrap();
|
|
|
|
let last_id = Hash::new(&last_id_vec);
|
|
|
|
|
|
|
|
info!("last_id: {:?}", last_id);
|
2019-02-01 07:36:35 -08:00
|
|
|
let tx = SystemTransaction::new_move(&alice, bob_pubkey, 20, last_id, 0);
|
2019-01-21 10:48:40 -08:00
|
|
|
let serial_tx = serialize(&tx).unwrap();
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "sendTransaction",
|
|
|
|
"params": json!([serial_tx])
|
|
|
|
});
|
|
|
|
let rpc_addr = leader_data.rpc;
|
|
|
|
let rpc_string = get_rpc_request_str(rpc_addr, false);
|
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
|
|
|
|
let signature = &json["result"];
|
|
|
|
|
|
|
|
let mut confirmed_tx = false;
|
|
|
|
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let request = json!({
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "confirmTransaction",
|
|
|
|
"params": [signature],
|
|
|
|
});
|
|
|
|
|
2019-02-27 09:58:03 -08:00
|
|
|
for _ in 0..solana_sdk::timing::DEFAULT_TICKS_PER_SLOT {
|
2019-01-21 10:48:40 -08:00
|
|
|
let mut response = client
|
|
|
|
.post(&rpc_string)
|
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request.to_string())
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
let response_json_text = response.text().unwrap();
|
|
|
|
let json: Value = serde_json::from_str(&response_json_text).unwrap();
|
|
|
|
|
|
|
|
if true == json["result"] {
|
|
|
|
confirmed_tx = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-29 17:03:32 -08:00
|
|
|
sleep(Duration::from_millis(500));
|
2019-01-21 10:48:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(confirmed_tx, true);
|
|
|
|
|
2019-02-05 08:03:52 -08:00
|
|
|
server_exit();
|
2019-01-21 10:48:40 -08:00
|
|
|
remove_dir_all(ledger_path).unwrap();
|
|
|
|
}
|