mwixnet/src/node.rs

240 lines
6.4 KiB
Rust
Raw Normal View History

2022-02-11 04:33:35 +03:00
use crate::secp::Commitment;
use grin_api::client;
use grin_api::json_rpc::{build_request, Request, Response};
2022-04-27 06:13:44 +03:00
use grin_api::{OutputPrintable, OutputType, Tip};
2022-02-11 04:33:35 +03:00
use grin_core::consensus::COINBASE_MATURITY;
use grin_core::core::{Input, OutputFeatures, Transaction};
use grin_util::ToHex;
use serde_json::json;
use std::net::SocketAddr;
2022-08-01 06:17:08 +03:00
use std::sync::Arc;
2022-08-22 20:03:10 +03:00
use thiserror::Error;
2022-02-11 04:33:35 +03:00
pub trait GrinNode: Send + Sync {
/// Retrieves the unspent output with a matching commitment
2022-08-22 20:03:10 +03:00
fn get_utxo(&self, output_commit: &Commitment) -> Result<Option<OutputPrintable>, NodeError>;
2022-04-27 06:13:44 +03:00
/// Gets the height of the chain tip
2022-08-22 20:03:10 +03:00
fn get_chain_height(&self) -> Result<u64, NodeError>;
2022-04-27 06:13:44 +03:00
/// Posts a transaction to the grin node
2022-08-22 20:03:10 +03:00
fn post_tx(&self, tx: &Transaction) -> Result<(), NodeError>;
}
/// Error types for interacting with nodes
#[derive(Error, Debug)]
pub enum NodeError {
#[error("Error decoding JSON response: {0:?}")]
DecodeResponseError(serde_json::Error),
#[error("JSON-RPC API communication error: {0:?}")]
ApiCommError(grin_api::Error),
#[error("Error decoding JSON-RPC response: {0:?}")]
ResponseParseError(grin_api::json_rpc::Error),
2022-04-27 06:13:44 +03:00
}
2022-04-27 23:30:15 +03:00
/// Checks if a commitment is in the UTXO set
2022-08-22 20:03:10 +03:00
pub fn is_unspent(node: &Arc<dyn GrinNode>, commit: &Commitment) -> Result<bool, NodeError> {
let utxo = node.get_utxo(&commit)?;
Ok(utxo.is_some())
2022-04-27 23:30:15 +03:00
}
2022-04-27 06:13:44 +03:00
/// Checks whether a commitment is spendable at the block height provided
pub fn is_spendable(
node: &Arc<dyn GrinNode>,
output_commit: &Commitment,
next_block_height: u64,
2022-08-22 20:03:10 +03:00
) -> Result<bool, NodeError> {
let output = node.get_utxo(&output_commit)?;
if let Some(out) = output {
let is_coinbase = match out.output_type {
OutputType::Coinbase => true,
OutputType::Transaction => false,
};
if is_coinbase {
if let Some(block_height) = out.block_height {
if block_height + COINBASE_MATURITY < next_block_height {
return Ok(false);
}
} else {
return Ok(false);
}
}
return Ok(true);
}
Ok(false)
2022-04-27 06:13:44 +03:00
}
/// Builds an input for an unspent output commitment
2022-08-22 20:03:10 +03:00
pub fn build_input(
node: &Arc<dyn GrinNode>,
output_commit: &Commitment,
) -> Result<Option<Input>, NodeError> {
let output = node.get_utxo(&output_commit)?;
2022-02-11 04:33:35 +03:00
if let Some(out) = output {
let features = match out.output_type {
OutputType::Coinbase => OutputFeatures::Coinbase,
OutputType::Transaction => OutputFeatures::Plain,
};
2022-04-27 06:13:44 +03:00
let input = Input::new(features, out.commit);
return Ok(Some(input));
}
2022-04-27 06:13:44 +03:00
Ok(None)
2022-04-27 06:13:44 +03:00
}
/// HTTP (JSON-RPC) implementation of the 'GrinNode' trait
2022-02-11 04:33:35 +03:00
#[derive(Clone)]
2022-04-27 06:13:44 +03:00
pub struct HttpGrinNode {
node_url: SocketAddr,
node_api_secret: Option<String>,
2022-02-11 04:33:35 +03:00
}
2022-04-27 06:13:44 +03:00
const ENDPOINT: &str = "/v2/foreign";
impl HttpGrinNode {
pub fn new(node_url: &SocketAddr, node_api_secret: &Option<String>) -> HttpGrinNode {
HttpGrinNode {
node_url: node_url.to_owned(),
node_api_secret: node_api_secret.to_owned(),
}
}
fn send_json_request<D: serde::de::DeserializeOwned>(
&self,
method: &str,
params: &serde_json::Value,
2022-08-22 20:03:10 +03:00
) -> Result<D, NodeError> {
let url = format!("http://{}{}", self.node_url, ENDPOINT);
let req = build_request(method, params);
let res =
2022-08-22 20:03:10 +03:00
client::post::<Request, Response>(url.as_str(), self.node_api_secret.clone(), &req)
.map_err(NodeError::ApiCommError)?;
let parsed = res
.clone()
.into_result()
.map_err(NodeError::ResponseParseError)?;
Ok(parsed)
}
2022-02-11 04:33:35 +03:00
}
2022-04-27 06:13:44 +03:00
impl GrinNode for HttpGrinNode {
2022-08-22 20:03:10 +03:00
fn get_utxo(&self, output_commit: &Commitment) -> Result<Option<OutputPrintable>, NodeError> {
let commits: Vec<String> = vec![output_commit.to_hex()];
let start_height: Option<u64> = None;
let end_height: Option<u64> = None;
let include_proof: Option<bool> = Some(false);
let include_merkle_proof: Option<bool> = Some(false);
let params = json!([
Some(commits),
start_height,
end_height,
include_proof,
include_merkle_proof
]);
let outputs = self.send_json_request::<Vec<OutputPrintable>>("get_outputs", &params)?;
if outputs.is_empty() {
return Ok(None);
}
Ok(Some(outputs[0].clone()))
}
2022-08-22 20:03:10 +03:00
fn get_chain_height(&self) -> Result<u64, NodeError> {
let params = json!([]);
let tip_json = self.send_json_request::<serde_json::Value>("get_tip", &params)?;
2022-08-22 20:03:10 +03:00
let tip = serde_json::from_value::<Tip>(tip_json["Ok"].clone())
.map_err(NodeError::DecodeResponseError)?;
2022-08-22 20:03:10 +03:00
Ok(tip.height)
}
2022-08-22 20:03:10 +03:00
fn post_tx(&self, tx: &Transaction) -> Result<(), NodeError> {
let params = json!([tx, true]);
self.send_json_request::<serde_json::Value>("push_transaction", &params)?;
Ok(())
}
2022-04-27 06:13:44 +03:00
}
2022-02-11 04:33:35 +03:00
2022-08-01 06:17:08 +03:00
#[cfg(test)]
pub mod mock {
2022-08-22 20:03:10 +03:00
use super::{GrinNode, NodeError};
2022-08-01 06:17:08 +03:00
use crate::secp::Commitment;
use grin_api::{OutputPrintable, OutputType};
use grin_core::core::Transaction;
use std::collections::HashMap;
use std::sync::RwLock;
/// Implementation of 'GrinNode' trait that mocks a grin node instance.
/// Use only for testing purposes.
pub struct MockGrinNode {
utxos: HashMap<Commitment, OutputPrintable>,
txns_posted: RwLock<Vec<Transaction>>,
}
2022-04-27 06:13:44 +03:00
2022-08-01 06:17:08 +03:00
impl MockGrinNode {
pub fn new() -> MockGrinNode {
MockGrinNode {
utxos: HashMap::new(),
txns_posted: RwLock::new(Vec::new()),
}
}
2022-08-01 06:17:08 +03:00
pub fn add_utxo(&mut self, output_commit: &Commitment, utxo: &OutputPrintable) {
self.utxos.insert(output_commit.clone(), utxo.clone());
}
2022-08-01 06:17:08 +03:00
pub fn add_default_utxo(&mut self, output_commit: &Commitment) {
let utxo = OutputPrintable {
output_type: OutputType::Transaction,
commit: output_commit.to_owned(),
spent: false,
proof: None,
proof_hash: String::from(""),
block_height: None,
merkle_proof: None,
mmr_index: 0,
};
self.add_utxo(&output_commit, &utxo);
}
2022-08-01 06:17:08 +03:00
pub fn get_posted_txns(&self) -> Vec<Transaction> {
let read = self.txns_posted.read().unwrap();
read.clone()
}
}
2022-08-01 06:17:08 +03:00
impl GrinNode for MockGrinNode {
2022-08-22 20:03:10 +03:00
fn get_utxo(
&self,
output_commit: &Commitment,
) -> Result<Option<OutputPrintable>, NodeError> {
2022-08-01 06:17:08 +03:00
if let Some(utxo) = self.utxos.get(&output_commit) {
return Ok(Some(utxo.clone()));
}
2022-02-11 04:33:35 +03:00
2022-08-01 06:17:08 +03:00
Ok(None)
}
2022-08-22 20:03:10 +03:00
fn get_chain_height(&self) -> Result<u64, NodeError> {
2022-08-01 06:17:08 +03:00
Ok(100)
}
2022-08-22 20:03:10 +03:00
fn post_tx(&self, tx: &Transaction) -> Result<(), NodeError> {
2022-08-01 06:17:08 +03:00
let mut write = self.txns_posted.write().unwrap();
write.push(tx.clone());
Ok(())
}
}
}