From 8108596e14eb6e30d20a98c857df46bcc302bc2d Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Wed, 12 Jun 2019 11:26:32 +0100 Subject: [PATCH 1/2] update mine_block to use V2 wallet API --- servers/src/mining/mine_block.rs | 51 ++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/servers/src/mining/mine_block.rs b/servers/src/mining/mine_block.rs index b5c5a011ba..966aa628b4 100644 --- a/servers/src/mining/mine_block.rs +++ b/servers/src/mining/mine_block.rs @@ -21,6 +21,7 @@ use rand::{thread_rng, Rng}; use std::sync::Arc; use std::thread; use std::time::Duration; +use serde_json::{json, Value}; use crate::api; use crate::chain; @@ -265,15 +266,47 @@ fn get_coinbase( /// Call the wallet API to create a coinbase output for the given block_fees. /// Will retry based on default "retry forever with backoff" behavior. fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result { - let url = format!("{}/v1/wallet/foreign/build_coinbase", dest); - match api::client::post(&url, None, &block_fees) { - Err(e) => { - error!( - "Failed to get coinbase from {}. Is the wallet listening?", - url - ); - Err(Error::WalletComm(format!("{}", e))) + let url = format!("{}/v2/foreign", dest); + let req_body = json!({ + "jsonrpc": "2.0", + "method": "build_coinbase", + "id": 1, + "params": { + "block_fees": block_fees } - Ok(res) => Ok(res), + }); + + trace!("Sending build_coinbase request: {}", req_body); + let req = api::client::create_post_request(url.as_str(), None, &req_body)?; + let res: String = api::client::send_request(req).map_err(|e| { + let report = format!("Failed to get coinbase from {}. Is the wallet listening? {}", dest, e); + error!("{}", report); + Error::WalletComm(report) + })?; + + let res: Value = serde_json::from_str(&res).unwrap(); + trace!("Response: {}", res); + if res["error"] != json!(null) { + let report = format!( + "Failed to get coinbase from {}: Error: {}, Message: {}", + dest, + res["error"]["code"], res["error"]["message"] + ); + error!("{}", report); + return Err(Error::WalletComm(report)); } + + let cb_data = res["result"]["Ok"].clone(); + trace!("cb_data: {}", cb_data); + let ret_val = match serde_json::from_value::(cb_data) { + Ok(r) => r, + Err(e) => { + let report = format!("Couldn't deserialize CbData: {}", e); + error!("{}", report); + return Err(Error::WalletComm(report)); + } + }; + + Ok(ret_val) + } From 8030299a8f8b734a02c8ffdeb96a575097672f80 Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Wed, 12 Jun 2019 11:26:42 +0100 Subject: [PATCH 2/2] rustfmt --- servers/src/mining/mine_block.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/servers/src/mining/mine_block.rs b/servers/src/mining/mine_block.rs index 966aa628b4..19c4b10b8c 100644 --- a/servers/src/mining/mine_block.rs +++ b/servers/src/mining/mine_block.rs @@ -18,10 +18,10 @@ use crate::util::RwLock; use chrono::prelude::{DateTime, NaiveDateTime, Utc}; use rand::{thread_rng, Rng}; +use serde_json::{json, Value}; use std::sync::Arc; use std::thread; use std::time::Duration; -use serde_json::{json, Value}; use crate::api; use crate::chain; @@ -279,7 +279,10 @@ fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result trace!("Sending build_coinbase request: {}", req_body); let req = api::client::create_post_request(url.as_str(), None, &req_body)?; let res: String = api::client::send_request(req).map_err(|e| { - let report = format!("Failed to get coinbase from {}. Is the wallet listening? {}", dest, e); + let report = format!( + "Failed to get coinbase from {}. Is the wallet listening? {}", + dest, e + ); error!("{}", report); Error::WalletComm(report) })?; @@ -289,8 +292,7 @@ fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result if res["error"] != json!(null) { let report = format!( "Failed to get coinbase from {}: Error: {}, Message: {}", - dest, - res["error"]["code"], res["error"]["message"] + dest, res["error"]["code"], res["error"]["message"] ); error!("{}", report); return Err(Error::WalletComm(report)); @@ -308,5 +310,4 @@ fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result }; Ok(ret_val) - }