use darkfi_serial::deserialize_async;
use log::{error, warn};
use tinyjson::JsonValue;
use darkfi::{
rpc::jsonrpc::{
ErrorCode::{InternalError, InvalidParams},
JsonError, JsonResponse, JsonResult,
},
tx::Transaction,
util::encoding::base64,
};
use super::DarkfiNode;
use crate::{server_error, RpcError};
impl DarkfiNode {
pub async fn tx_simulate(&self, id: u16, params: JsonValue) -> JsonResult {
let params = params.get::<Vec<JsonValue>>().unwrap();
if params.len() != 1 || !params[0].is_string() {
return JsonError::new(InvalidParams, None, id).into()
}
if !*self.validator.synced.read().await {
error!(target: "darkfid::rpc::tx_simulate", "Blockchain is not synced");
return server_error(RpcError::NotSynced, id, None)
}
let tx_enc = params[0].get::<String>().unwrap().trim();
let tx_bytes = match base64::decode(tx_enc) {
Some(v) => v,
None => {
error!(target: "darkfid::rpc::tx_simulate", "Failed decoding base64 transaction");
return server_error(RpcError::ParseError, id, None)
}
};
let tx: Transaction = match deserialize_async(&tx_bytes).await {
Ok(v) => v,
Err(e) => {
error!(target: "darkfid::rpc::tx_simulate", "Failed deserializing bytes into Transaction: {}", e);
return server_error(RpcError::ParseError, id, None)
}
};
let result = self.validator.append_tx(&tx, false).await;
if result.is_err() {
error!(
target: "darkfid::rpc::tx_simulate", "Failed to validate state transition: {}",
result.err().unwrap()
);
return server_error(RpcError::TxSimulationFail, id, None)
};
JsonResponse::new(JsonValue::Boolean(true), id).into()
}
pub async fn tx_broadcast(&self, id: u16, params: JsonValue) -> JsonResult {
let params = params.get::<Vec<JsonValue>>().unwrap();
if params.len() != 1 || !params[0].is_string() {
return JsonError::new(InvalidParams, None, id).into()
}
if !*self.validator.synced.read().await {
error!(target: "darkfid::rpc::tx_broadcast", "Blockchain is not synced");
return server_error(RpcError::NotSynced, id, None)
}
let tx_enc = params[0].get::<String>().unwrap().trim();
let tx_bytes = match base64::decode(tx_enc) {
Some(v) => v,
None => {
error!(target: "darkfid::rpc::tx_broadcast", "Failed decoding base64 transaction");
return server_error(RpcError::ParseError, id, None)
}
};
let tx: Transaction = match deserialize_async(&tx_bytes).await {
Ok(v) => v,
Err(e) => {
error!(target: "darkfid::rpc::tx_broadcast", "Failed deserializing bytes into Transaction: {}", e);
return server_error(RpcError::ParseError, id, None)
}
};
let error_message = if self.rpc_client.is_some() {
"Failed to append transaction to mempool"
} else {
"Failed to validate state transition"
};
if let Err(e) = self.validator.append_tx(&tx, self.rpc_client.is_some()).await {
error!(target: "darkfid::rpc::tx_broadcast", "{}: {}", error_message, e);
return server_error(RpcError::TxSimulationFail, id, None)
};
self.p2p_handler.p2p.broadcast(&tx).await;
if !self.p2p_handler.p2p.is_connected() {
warn!(target: "darkfid::rpc::tx_broadcast", "No connected channels to broadcast tx");
}
let tx_hash = tx.hash().to_string();
JsonResponse::new(JsonValue::String(tx_hash), id).into()
}
pub async fn tx_pending(&self, id: u16, params: JsonValue) -> JsonResult {
let params = params.get::<Vec<JsonValue>>().unwrap();
if !params.is_empty() {
return JsonError::new(InvalidParams, None, id).into()
}
if !*self.validator.synced.read().await {
error!(target: "darkfid::rpc::tx_pending", "Blockchain is not synced");
return server_error(RpcError::NotSynced, id, None)
}
let pending_txs = match self.validator.blockchain.get_pending_txs() {
Ok(v) => v,
Err(e) => {
error!(target: "darkfid::rpc::tx_pending", "Failed fetching pending txs: {}", e);
return JsonError::new(InternalError, None, id).into()
}
};
let pending_txs: Vec<JsonValue> =
pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
JsonResponse::new(JsonValue::Array(pending_txs), id).into()
}
pub async fn tx_clean_pending(&self, id: u16, params: JsonValue) -> JsonResult {
let params = params.get::<Vec<JsonValue>>().unwrap();
if !params.is_empty() {
return JsonError::new(InvalidParams, None, id).into()
}
if !*self.validator.synced.read().await {
error!(target: "darkfid::rpc::tx_clean_pending", "Blockchain is not synced");
return server_error(RpcError::NotSynced, id, None)
}
let pending_txs = match self.validator.blockchain.get_pending_txs() {
Ok(v) => v,
Err(e) => {
error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {}", e);
return JsonError::new(InternalError, None, id).into()
}
};
if let Err(e) = self.validator.blockchain.remove_pending_txs(&pending_txs) {
error!(target: "darkfid::rpc::tx_clean_pending", "Failed fetching pending txs: {}", e);
return JsonError::new(InternalError, None, id).into()
};
let pending_txs: Vec<JsonValue> =
pending_txs.iter().map(|x| JsonValue::String(x.hash().to_string())).collect();
JsonResponse::new(JsonValue::Array(pending_txs), id).into()
}
pub async fn tx_calculate_gas(&self, id: u16, params: JsonValue) -> JsonResult {
let params = params.get::<Vec<JsonValue>>().unwrap();
if params.len() != 2 || !params[0].is_string() || !params[1].is_bool() {
return JsonError::new(InvalidParams, None, id).into()
}
if !*self.validator.synced.read().await {
error!(target: "darkfid::rpc::tx_calculate_gas", "Blockchain is not synced");
return server_error(RpcError::NotSynced, id, None)
}
let tx_enc = params[0].get::<String>().unwrap().trim();
let tx_bytes = match base64::decode(tx_enc) {
Some(v) => v,
None => {
error!(target: "darkfid::rpc::tx_calculate_gas", "Failed decoding base64 transaction");
return server_error(RpcError::ParseError, id, None)
}
};
let tx: Transaction = match deserialize_async(&tx_bytes).await {
Ok(v) => v,
Err(e) => {
error!(target: "darkfid::rpc::tx_calculate_gas", "Failed deserializing bytes into Transaction: {}", e);
return server_error(RpcError::ParseError, id, None)
}
};
let include_fee = params[1].get::<bool>().unwrap();
let result = self.validator.calculate_gas(&tx, *include_fee).await;
if result.is_err() {
error!(
target: "darkfid::rpc::tx_calculate_gas", "Failed to validate state transition: {}",
result.err().unwrap()
);
return server_error(RpcError::TxGasCalculationFail, id, None)
};
JsonResponse::new(JsonValue::Number(result.unwrap().total_gas_used() as f64), id).into()
}
}