Simplify api commits (#3423)

* simplify api with unspent by commitment

* fix chain tests
This commit is contained in:
Antioch Peverell
2020-08-17 18:19:29 +01:00
committed by GitHub
parent d1b90f89a0
commit 599bf22cfc
6 changed files with 41 additions and 51 deletions
+3 -3
View File
@@ -61,7 +61,7 @@ impl HeaderHandler {
Some((_, o)) => o,
None => return Err(ErrorKind::NotFound.into()),
};
match w(&self.chain)?.get_header_for_output(&oid) {
match w(&self.chain)?.get_header_for_output(oid.commitment()) {
Ok(header) => Ok(BlockHeaderPrintable::from_header(&header)),
Err(_) => Err(ErrorKind::NotFound.into()),
}
@@ -94,7 +94,7 @@ impl HeaderHandler {
Some((_, o)) => o,
None => return Err(ErrorKind::NotFound.into()),
};
match w(&self.chain)?.get_header_for_output(&oid) {
match w(&self.chain)?.get_header_for_output(oid.commitment()) {
Ok(header) => return Ok(header.hash()),
Err(_) => return Err(ErrorKind::NotFound.into()),
}
@@ -179,7 +179,7 @@ impl BlockHandler {
Some((_, o)) => o,
None => return Err(ErrorKind::NotFound.into()),
};
match w(&self.chain)?.get_header_for_output(&oid) {
match w(&self.chain)?.get_header_for_output(oid.commitment()) {
Ok(header) => return Ok(header.hash()),
Err(_) => return Err(ErrorKind::NotFound.into()),
}
+12 -26
View File
@@ -14,7 +14,7 @@
use crate::chain;
use crate::chain::types::CommitPos;
use crate::core::core::{OutputFeatures, OutputIdentifier};
use crate::core::core::OutputIdentifier;
use crate::rest::*;
use crate::types::*;
use crate::util;
@@ -33,42 +33,28 @@ pub fn w<T>(weak: &Weak<T>) -> Result<Arc<T>, Error> {
fn get_unspent(
chain: &Arc<chain::Chain>,
id: &str,
) -> Result<Option<(CommitPos, OutputIdentifier)>, Error> {
) -> Result<Option<(OutputIdentifier, CommitPos)>, Error> {
let c = util::from_hex(id)
.map_err(|_| ErrorKind::Argument(format!("Not a valid commitment: {}", id)))?;
let commit = Commitment::from_vec(c);
// 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)
let outputs = [
OutputIdentifier::new(OutputFeatures::Plain, &commit),
OutputIdentifier::new(OutputFeatures::Coinbase, &commit),
];
for x in outputs.iter() {
if let Some(output_pos) = chain.get_unspent(x)? {
return Ok(Some((output_pos, x.clone())));
}
}
Ok(None)
let res = chain.get_unspent(commit)?;
Ok(res)
}
/// Retrieves an output from the chain given a commit id (a tiny bit iteratively)
/// Retrieves an output from the chain given a commitment.
pub fn get_output(
chain: &Weak<chain::Chain>,
id: &str,
) -> Result<Option<(Output, OutputIdentifier)>, Error> {
let chain = w(chain)?;
let (output_pos, identifier) = match get_unspent(&chain, id)? {
let (out, pos) = match get_unspent(&chain, id)? {
Some(x) => x,
None => return Ok(None),
};
Ok(Some((
Output::new(&identifier.commit, output_pos.height, output_pos.pos),
identifier,
Output::new(&out.commitment(), pos.height, pos.pos),
out,
)))
}
@@ -80,14 +66,14 @@ pub fn get_output_v2(
include_merkle_proof: bool,
) -> Result<Option<(OutputPrintable, OutputIdentifier)>, Error> {
let chain = w(chain)?;
let (output_pos, identifier) = match get_unspent(&chain, id)? {
let (out, pos) = match get_unspent(&chain, id)? {
Some(x) => x,
None => return Ok(None),
};
let output = chain.get_unspent_output_at(output_pos.pos)?;
let output = chain.get_unspent_output_at(pos.pos)?;
let header = if include_merkle_proof && output.is_coinbase() {
chain.get_header_by_height(output_pos.height).ok()
chain.get_header_by_height(pos.height).ok()
} else {
None
};
@@ -100,5 +86,5 @@ pub fn get_output_v2(
include_merkle_proof,
)?;
Ok(Some((output_printable, identifier)))
Ok(Some((output_printable, out)))
}
+5 -3
View File
@@ -291,7 +291,7 @@ impl OutputPrintable {
};
let out_id = core::OutputIdentifier::from(output);
let pos = chain.get_unspent(&out_id)?;
let pos = chain.get_unspent(out_id.commitment())?;
let spent = pos.is_none();
@@ -301,8 +301,10 @@ impl OutputPrintable {
// api is currently doing the right thing here:
// An output can be spent and then subsequently reused and the new instance unspent.
// This would result in a height that differs from the provided block height.
let output_pos = pos.map(|x| x.pos).unwrap_or(0);
let block_height = pos.map(|x| x.height).or(block_header.map(|x| x.height));
let output_pos = pos.map(|(_, x)| x.pos).unwrap_or(0);
let block_height = pos
.map(|(_, x)| x.height)
.or(block_header.map(|x| x.height));
let proof = if include_proof {
Some(output.proof_bytes().to_hex())