rustfmt all the things
This commit is contained in:
+12
-10
@@ -32,9 +32,8 @@ where
|
||||
{
|
||||
let client = hyper::Client::new();
|
||||
let res = check_error(client.get(url).send())?;
|
||||
serde_json::from_reader(res).map_err(|e| {
|
||||
Error::Internal(format!("Server returned invalid JSON: {}", e))
|
||||
})
|
||||
serde_json::from_reader(res)
|
||||
.map_err(|e| Error::Internal(format!("Server returned invalid JSON: {}", e)))
|
||||
}
|
||||
|
||||
/// Helper function to easily issue a HTTP POST request with the provided JSON
|
||||
@@ -45,9 +44,8 @@ pub fn post<'a, IN>(url: &'a str, input: &IN) -> Result<(), Error>
|
||||
where
|
||||
IN: Serialize,
|
||||
{
|
||||
let in_json = serde_json::to_string(input).map_err(|e| {
|
||||
Error::Internal(format!("Could not serialize data to JSON: {}", e))
|
||||
})?;
|
||||
let in_json = serde_json::to_string(input)
|
||||
.map_err(|e| Error::Internal(format!("Could not serialize data to JSON: {}", e)))?;
|
||||
let client = hyper::Client::new();
|
||||
let _res = check_error(client.post(url).body(&mut in_json.as_bytes()).send())?;
|
||||
Ok(())
|
||||
@@ -61,13 +59,17 @@ fn check_error(res: hyper::Result<Response>) -> Result<Response, Error> {
|
||||
let mut response = res.unwrap();
|
||||
match response.status.class() {
|
||||
StatusClass::Success => Ok(response),
|
||||
StatusClass::ServerError => {
|
||||
Err(Error::Internal(format!("Server error: {}", err_msg(&mut response))))
|
||||
}
|
||||
StatusClass::ServerError => Err(Error::Internal(format!(
|
||||
"Server error: {}",
|
||||
err_msg(&mut response)
|
||||
))),
|
||||
StatusClass::ClientError => if response.status == StatusCode::NotFound {
|
||||
Err(Error::NotFound)
|
||||
} else {
|
||||
Err(Error::Argument(format!("Argument error: {}", err_msg(&mut response))))
|
||||
Err(Error::Argument(format!(
|
||||
"Argument error: {}",
|
||||
err_msg(&mut response)
|
||||
)))
|
||||
},
|
||||
_ => Err(Error::Internal(format!("Unrecognized error."))),
|
||||
}
|
||||
|
||||
+9
-17
@@ -13,7 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
use std::io::Read;
|
||||
use std::sync::{Arc, Weak, RwLock};
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
use std::thread;
|
||||
|
||||
use iron::prelude::*;
|
||||
@@ -24,7 +24,7 @@ use serde::Serialize;
|
||||
use serde_json;
|
||||
|
||||
use chain;
|
||||
use core::core::{OutputIdentifier, Transaction, OutputFeatures};
|
||||
use core::core::{OutputFeatures, OutputIdentifier, Transaction};
|
||||
use core::core::hash::{Hash, Hashed};
|
||||
use core::ser;
|
||||
use pool;
|
||||
@@ -43,7 +43,6 @@ fn w<T>(weak: &Weak<T>) -> Arc<T> {
|
||||
weak.upgrade().unwrap()
|
||||
}
|
||||
|
||||
|
||||
// RESTful index of available api endpoints
|
||||
// GET /v1/
|
||||
struct IndexHandler {
|
||||
@@ -74,15 +73,16 @@ impl UtxoHandler {
|
||||
|
||||
// We need the features here to be able to generate the necessary hash
|
||||
// to compare against the hash in the output MMR.
|
||||
// For now we can just try both (but this probably needs to be part of the api params)
|
||||
// For now we can just try both (but this probably needs to be part of the api
|
||||
// params)
|
||||
let outputs = [
|
||||
OutputIdentifier::new(OutputFeatures::DEFAULT_OUTPUT, &commit),
|
||||
OutputIdentifier::new(OutputFeatures::COINBASE_OUTPUT, &commit)
|
||||
OutputIdentifier::new(OutputFeatures::COINBASE_OUTPUT, &commit),
|
||||
];
|
||||
|
||||
for x in outputs.iter() {
|
||||
if let Ok(_) = w(&self.chain).is_unspent(&x) {
|
||||
return Ok(Utxo::new(&commit))
|
||||
return Ok(Utxo::new(&commit));
|
||||
}
|
||||
}
|
||||
Err(Error::NotFound)
|
||||
@@ -117,16 +117,12 @@ impl UtxoHandler {
|
||||
commitments: Vec<Commitment>,
|
||||
include_proof: bool,
|
||||
) -> BlockOutputs {
|
||||
let header = w(&self.chain)
|
||||
.get_header_by_height(block_height)
|
||||
.unwrap();
|
||||
let header = w(&self.chain).get_header_by_height(block_height).unwrap();
|
||||
let block = w(&self.chain).get_block(&header.hash()).unwrap();
|
||||
let outputs = block
|
||||
.outputs
|
||||
.iter()
|
||||
.filter(|output| {
|
||||
commitments.is_empty() || commitments.contains(&output.commit)
|
||||
})
|
||||
.filter(|output| commitments.is_empty() || commitments.contains(&output.commit))
|
||||
.map(|output| {
|
||||
OutputPrintable::from_output(output, w(&self.chain), &block, include_proof)
|
||||
})
|
||||
@@ -406,11 +402,7 @@ pub struct BlockHandler {
|
||||
impl BlockHandler {
|
||||
fn get_block(&self, h: &Hash) -> Result<BlockPrintable, Error> {
|
||||
let block = w(&self.chain).get_block(h).map_err(|_| Error::NotFound)?;
|
||||
Ok(BlockPrintable::from_block(
|
||||
&block,
|
||||
w(&self.chain),
|
||||
false,
|
||||
))
|
||||
Ok(BlockPrintable::from_block(&block, w(&self.chain), false))
|
||||
}
|
||||
|
||||
fn get_compact_block(&self, h: &Hash) -> Result<CompactBlockPrintable, Error> {
|
||||
|
||||
+2
-2
@@ -14,15 +14,15 @@
|
||||
|
||||
extern crate grin_chain as chain;
|
||||
extern crate grin_core as core;
|
||||
extern crate grin_pool as pool;
|
||||
extern crate grin_p2p as p2p;
|
||||
extern crate grin_pool as pool;
|
||||
extern crate grin_store as store;
|
||||
extern crate grin_util as util;
|
||||
|
||||
extern crate hyper;
|
||||
extern crate iron;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate iron;
|
||||
extern crate mount;
|
||||
extern crate regex;
|
||||
#[macro_use]
|
||||
|
||||
+90
-55
@@ -163,7 +163,9 @@ pub struct Utxo {
|
||||
|
||||
impl Utxo {
|
||||
pub fn new(commit: &pedersen::Commitment) -> Utxo {
|
||||
Utxo { commit: PrintableCommitment(commit.clone()) }
|
||||
Utxo {
|
||||
commit: PrintableCommitment(commit.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,15 +184,19 @@ impl PrintableCommitment {
|
||||
}
|
||||
|
||||
impl serde::ser::Serialize for PrintableCommitment {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
|
||||
S: serde::ser::Serializer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&util::to_hex(self.to_vec()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::de::Deserialize<'de> for PrintableCommitment {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
|
||||
D: serde::de::Deserializer<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(PrintableCommitmentVisitor)
|
||||
}
|
||||
}
|
||||
@@ -204,9 +210,13 @@ impl<'de> serde::de::Visitor<'de> for PrintableCommitmentVisitor {
|
||||
formatter.write_str("a Pedersen commitment")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where
|
||||
E: serde::de::Error, {
|
||||
Ok(PrintableCommitment(pedersen::Commitment::from_vec(util::from_hex(String::from(v)).unwrap())))
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(PrintableCommitment(pedersen::Commitment::from_vec(
|
||||
util::from_hex(String::from(v)).unwrap(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,12 +247,14 @@ impl OutputPrintable {
|
||||
block: &core::Block,
|
||||
include_proof: bool,
|
||||
) -> OutputPrintable {
|
||||
let output_type =
|
||||
if output.features.contains(core::transaction::OutputFeatures::COINBASE_OUTPUT) {
|
||||
OutputType::Coinbase
|
||||
} else {
|
||||
OutputType::Transaction
|
||||
};
|
||||
let output_type = if output
|
||||
.features
|
||||
.contains(core::transaction::OutputFeatures::COINBASE_OUTPUT)
|
||||
{
|
||||
OutputType::Coinbase
|
||||
} else {
|
||||
OutputType::Transaction
|
||||
};
|
||||
|
||||
let out_id = core::OutputIdentifier::from_output(&output);
|
||||
let spent = chain.is_unspent(&out_id).is_err();
|
||||
@@ -253,13 +265,14 @@ impl OutputPrintable {
|
||||
None
|
||||
};
|
||||
|
||||
// Get the Merkle proof for all unspent coinbase outputs (to verify maturity on spend).
|
||||
// We obtain the Merkle proof by rewinding the PMMR.
|
||||
// We require the rewind() to be stable even after the PMMR is pruned and compacted
|
||||
// so we can still recreate the necessary proof.
|
||||
// Get the Merkle proof for all unspent coinbase outputs (to verify maturity on
|
||||
// spend). We obtain the Merkle proof by rewinding the PMMR.
|
||||
// We require the rewind() to be stable even after the PMMR is pruned and
|
||||
// compacted so we can still recreate the necessary proof.
|
||||
let mut merkle_proof = None;
|
||||
if output.features.contains(core::transaction::OutputFeatures::COINBASE_OUTPUT)
|
||||
&& !spent
|
||||
if output
|
||||
.features
|
||||
.contains(core::transaction::OutputFeatures::COINBASE_OUTPUT) && !spent
|
||||
{
|
||||
merkle_proof = chain.get_merkle_proof(&out_id, &block).ok()
|
||||
};
|
||||
@@ -285,13 +298,17 @@ impl OutputPrintable {
|
||||
}
|
||||
|
||||
pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> {
|
||||
self.proof.clone().ok_or_else(|| ser::Error::HexError(format!("output range_proof missing")))
|
||||
self.proof
|
||||
.clone()
|
||||
.ok_or_else(|| ser::Error::HexError(format!("output range_proof missing")))
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::ser::Serialize for OutputPrintable {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
|
||||
S: serde::ser::Serializer {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("OutputPrintable", 7)?;
|
||||
state.serialize_field("output_type", &self.output_type)?;
|
||||
state.serialize_field("commit", &util::to_hex(self.commit.0.to_vec()))?;
|
||||
@@ -308,8 +325,10 @@ impl serde::ser::Serialize for OutputPrintable {
|
||||
}
|
||||
|
||||
impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
|
||||
D: serde::de::Deserializer<'de> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::de::Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(field_identifier, rename_all = "snake_case")]
|
||||
enum Field {
|
||||
@@ -319,7 +338,7 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
Spent,
|
||||
Proof,
|
||||
ProofHash,
|
||||
MerkleProof
|
||||
MerkleProof,
|
||||
}
|
||||
|
||||
struct OutputPrintableVisitor;
|
||||
@@ -331,8 +350,10 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
formatter.write_str("a print able Output")
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where
|
||||
A: MapAccess<'de>, {
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: MapAccess<'de>,
|
||||
{
|
||||
let mut output_type = None;
|
||||
let mut commit = None;
|
||||
let mut switch_commit_hash = None;
|
||||
@@ -346,15 +367,15 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
Field::OutputType => {
|
||||
no_dup!(output_type);
|
||||
output_type = Some(map.next_value()?)
|
||||
},
|
||||
}
|
||||
Field::Commit => {
|
||||
no_dup!(commit);
|
||||
|
||||
let val: String = map.next_value()?;
|
||||
let vec = util::from_hex(val.clone())
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
let vec =
|
||||
util::from_hex(val.clone()).map_err(serde::de::Error::custom)?;
|
||||
commit = Some(pedersen::Commitment::from_vec(vec));
|
||||
},
|
||||
}
|
||||
Field::SwitchCommitHash => {
|
||||
no_dup!(switch_commit_hash);
|
||||
|
||||
@@ -362,11 +383,11 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
let hash = core::SwitchCommitHash::from_hex(&val.clone())
|
||||
.map_err(serde::de::Error::custom)?;
|
||||
switch_commit_hash = Some(hash)
|
||||
},
|
||||
}
|
||||
Field::Spent => {
|
||||
no_dup!(spent);
|
||||
spent = Some(map.next_value()?)
|
||||
},
|
||||
}
|
||||
Field::Proof => {
|
||||
no_dup!(proof);
|
||||
|
||||
@@ -380,13 +401,16 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
bytes[i] = vec[i];
|
||||
}
|
||||
|
||||
proof = Some(pedersen::RangeProof { proof: bytes, plen: vec.len() })
|
||||
proof = Some(pedersen::RangeProof {
|
||||
proof: bytes,
|
||||
plen: vec.len(),
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
Field::ProofHash => {
|
||||
no_dup!(proof_hash);
|
||||
proof_hash = Some(map.next_value()?)
|
||||
},
|
||||
}
|
||||
Field::MerkleProof => {
|
||||
no_dup!(merkle_proof);
|
||||
if let Some(hex) = map.next_value::<Option<String>>()? {
|
||||
@@ -412,7 +436,14 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
|
||||
}
|
||||
}
|
||||
|
||||
const FIELDS: &'static [&'static str] = &["output_type", "commit", "switch_commit_hash", "spent", "proof", "proof_hash"];
|
||||
const FIELDS: &'static [&'static str] = &[
|
||||
"output_type",
|
||||
"commit",
|
||||
"switch_commit_hash",
|
||||
"spent",
|
||||
"proof",
|
||||
"proof_hash",
|
||||
];
|
||||
deserializer.deserialize_struct("OutputPrintable", FIELDS, OutputPrintableVisitor)
|
||||
}
|
||||
}
|
||||
@@ -523,14 +554,17 @@ impl BlockPrintable {
|
||||
chain: Arc<chain::Chain>,
|
||||
include_proof: bool,
|
||||
) -> BlockPrintable {
|
||||
let inputs = block.inputs
|
||||
let inputs = block
|
||||
.inputs
|
||||
.iter()
|
||||
.map(|x| util::to_hex(x.commitment().0.to_vec()))
|
||||
.collect();
|
||||
let outputs = block
|
||||
.outputs
|
||||
.iter()
|
||||
.map(|output| OutputPrintable::from_output(output, chain.clone(), &block, include_proof))
|
||||
.map(|output| {
|
||||
OutputPrintable::from_output(output, chain.clone(), &block, include_proof)
|
||||
})
|
||||
.collect();
|
||||
let kernels = block
|
||||
.kernels
|
||||
@@ -559,19 +593,18 @@ pub struct CompactBlockPrintable {
|
||||
}
|
||||
|
||||
impl CompactBlockPrintable {
|
||||
/// Convert a compact block into a printable representation suitable for api response
|
||||
/// Convert a compact block into a printable representation suitable for
|
||||
/// api response
|
||||
pub fn from_compact_block(
|
||||
cb: &core::CompactBlock,
|
||||
chain: Arc<chain::Chain>,
|
||||
) -> CompactBlockPrintable {
|
||||
let block = chain.get_block(&cb.hash()).unwrap();
|
||||
let out_full = cb
|
||||
.out_full
|
||||
let out_full = cb.out_full
|
||||
.iter()
|
||||
.map(|x| OutputPrintable::from_output(x, chain.clone(), &block, false))
|
||||
.collect();
|
||||
let kern_full = cb
|
||||
.kern_full
|
||||
let kern_full = cb.kern_full
|
||||
.iter()
|
||||
.map(|x| TxKernelPrintable::from_txkernel(x))
|
||||
.collect();
|
||||
@@ -611,15 +644,16 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn serialize_output() {
|
||||
let hex_output = "{\
|
||||
\"output_type\":\"Coinbase\",\
|
||||
\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\",\
|
||||
\"switch_commit_hash\":\"85daaf11011dc11e52af84ebe78e2f2d19cbdc76000000000000000000000000\",\
|
||||
\"spent\":false,\
|
||||
\"proof\":null,\
|
||||
\"proof_hash\":\"ed6ba96009b86173bade6a9227ed60422916593fa32dd6d78b25b7a4eeef4946\",\
|
||||
\"merkle_proof\":null\
|
||||
}";
|
||||
let hex_output =
|
||||
"{\
|
||||
\"output_type\":\"Coinbase\",\
|
||||
\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\",\
|
||||
\"switch_commit_hash\":\"85daaf11011dc11e52af84ebe78e2f2d19cbdc76000000000000000000000000\",\
|
||||
\"spent\":false,\
|
||||
\"proof\":null,\
|
||||
\"proof_hash\":\"ed6ba96009b86173bade6a9227ed60422916593fa32dd6d78b25b7a4eeef4946\",\
|
||||
\"merkle_proof\":null\
|
||||
}";
|
||||
let deserialized: OutputPrintable = serde_json::from_str(&hex_output).unwrap();
|
||||
let serialized = serde_json::to_string(&deserialized).unwrap();
|
||||
assert_eq!(serialized, hex_output);
|
||||
@@ -627,7 +661,8 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn serialize_utxo() {
|
||||
let hex_commit = "{\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\"}";
|
||||
let hex_commit =
|
||||
"{\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\"}";
|
||||
let deserialized: Utxo = serde_json::from_str(&hex_commit).unwrap();
|
||||
let serialized = serde_json::to_string(&deserialized).unwrap();
|
||||
assert_eq!(serialized, hex_commit);
|
||||
|
||||
Reference in New Issue
Block a user