rustfmt all the things
This commit is contained in:
+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