Simplify api commits (#3423)
* simplify api with unspent by commitment * fix chain tests
This commit is contained in:
+9
-9
@@ -568,11 +568,14 @@ impl Chain {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns Ok(Some(pos)) if output is unspent.
|
||||
/// Returns Ok(Some((out, pos))) if output is unspent.
|
||||
/// Returns Ok(None) if output is spent.
|
||||
/// Returns Err if something went wrong beyond not finding the output.
|
||||
pub fn get_unspent(&self, output_ref: &OutputIdentifier) -> Result<Option<CommitPos>, Error> {
|
||||
self.txhashset.read().get_unspent(output_ref)
|
||||
pub fn get_unspent(
|
||||
&self,
|
||||
commit: Commitment,
|
||||
) -> Result<Option<(OutputIdentifier, CommitPos)>, Error> {
|
||||
self.txhashset.read().get_unspent(commit)
|
||||
}
|
||||
|
||||
/// Retrieves an unspent output using its PMMR position
|
||||
@@ -1387,17 +1390,14 @@ impl Chain {
|
||||
}
|
||||
|
||||
/// Gets the block header in which a given output appears in the txhashset.
|
||||
pub fn get_header_for_output(
|
||||
&self,
|
||||
output_ref: &OutputIdentifier,
|
||||
) -> Result<BlockHeader, Error> {
|
||||
pub fn get_header_for_output(&self, commit: Commitment) -> Result<BlockHeader, Error> {
|
||||
let header_pmmr = self.header_pmmr.read();
|
||||
let txhashset = self.txhashset.read();
|
||||
let output_pos = match txhashset.get_unspent(output_ref)? {
|
||||
let (_, pos) = match txhashset.get_unspent(commit)? {
|
||||
Some(o) => o,
|
||||
None => return Err(ErrorKind::OutputNotFound.into()),
|
||||
};
|
||||
let hash = header_pmmr.get_header_hash_by_height(output_pos.height)?;
|
||||
let hash = header_pmmr.get_header_hash_by_height(pos.height)?;
|
||||
Ok(self.get_block_header(&hash)?)
|
||||
}
|
||||
|
||||
|
||||
@@ -255,15 +255,17 @@ impl TxHashSet {
|
||||
/// Check if an output is unspent.
|
||||
/// We look in the index to find the output MMR pos.
|
||||
/// Then we check the entry in the output MMR and confirm the hash matches.
|
||||
pub fn get_unspent(&self, output_id: &OutputIdentifier) -> Result<Option<CommitPos>, Error> {
|
||||
let commit = output_id.commit;
|
||||
pub fn get_unspent(
|
||||
&self,
|
||||
commit: Commitment,
|
||||
) -> Result<Option<(OutputIdentifier, CommitPos)>, Error> {
|
||||
match self.commit_index.get_output_pos_height(&commit) {
|
||||
Ok(Some(pos)) => {
|
||||
let output_pmmr: ReadonlyPMMR<'_, Output, _> =
|
||||
ReadonlyPMMR::at(&self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
|
||||
if let Some(out) = output_pmmr.get_data(pos.pos) {
|
||||
if out == *output_id {
|
||||
Ok(Some(pos))
|
||||
if out.commitment() == commit {
|
||||
Ok(Some((out, pos)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -696,11 +696,11 @@ fn spend_in_fork_and_compact() {
|
||||
assert_eq!(head.height, 6);
|
||||
assert_eq!(head.hash(), prev_main.hash());
|
||||
assert!(chain
|
||||
.get_unspent(&OutputIdentifier::from(&tx2.outputs()[0]))
|
||||
.get_unspent(tx2.outputs()[0].commitment())
|
||||
.unwrap()
|
||||
.is_some());
|
||||
assert!(chain
|
||||
.get_unspent(&OutputIdentifier::from(&tx1.outputs()[0]))
|
||||
.get_unspent(tx1.outputs()[0].commitment())
|
||||
.unwrap()
|
||||
.is_none());
|
||||
|
||||
@@ -717,11 +717,11 @@ fn spend_in_fork_and_compact() {
|
||||
assert_eq!(head.height, 7);
|
||||
assert_eq!(head.hash(), prev_fork.hash());
|
||||
assert!(chain
|
||||
.get_unspent(&OutputIdentifier::from(&tx2.outputs()[0]))
|
||||
.get_unspent(tx2.outputs()[0].commitment())
|
||||
.unwrap()
|
||||
.is_some());
|
||||
assert!(chain
|
||||
.get_unspent(&OutputIdentifier::from(&tx1.outputs()[0]))
|
||||
.get_unspent(tx1.outputs()[0].commitment())
|
||||
.unwrap()
|
||||
.is_none());
|
||||
|
||||
@@ -796,7 +796,7 @@ fn output_header_mappings() {
|
||||
chain.process_block(b, chain::Options::MINE).unwrap();
|
||||
|
||||
let header_for_output = chain
|
||||
.get_header_for_output(&OutputIdentifier::from(&reward_outputs[n - 1]))
|
||||
.get_header_for_output(reward_outputs[n - 1].commitment())
|
||||
.unwrap();
|
||||
assert_eq!(header_for_output.height, n as u64);
|
||||
|
||||
@@ -806,7 +806,7 @@ fn output_header_mappings() {
|
||||
// Check all output positions are as expected
|
||||
for n in 1..15 {
|
||||
let header_for_output = chain
|
||||
.get_header_for_output(&OutputIdentifier::from(&reward_outputs[n - 1]))
|
||||
.get_header_for_output(reward_outputs[n - 1].commitment())
|
||||
.unwrap();
|
||||
assert_eq!(header_for_output.height, n as u64);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user