Unverified Commit 953abaf3 authored by Roman Zeyde's avatar Roman Zeyde
Browse files

Sort transaction history by descending confirmation height

Now unconfirmed txns use u32::max_value as their height.

Also, remove Electrum-specific hash from query module.
parent cf2e79e7
...@@ -3,9 +3,6 @@ use bitcoin::blockdata::block::Block; ...@@ -3,9 +3,6 @@ use bitcoin::blockdata::block::Block;
use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::transaction::Transaction;
use bitcoin::consensus::encode::{deserialize, serialize}; use bitcoin::consensus::encode::{deserialize, serialize};
use bitcoin::util::hash::Sha256dHash; use bitcoin::util::hash::Sha256dHash;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
...@@ -16,8 +13,7 @@ use metrics::Metrics; ...@@ -16,8 +13,7 @@ use metrics::Metrics;
use serde_json::Value; use serde_json::Value;
use store::{ReadStore, Row}; use store::{ReadStore, Row};
use util::{ use util::{
BlockHeaderMeta, BlockMeta, BlockStatus, Bytes, FullHash, HashPrefix, HeaderEntry, BlockHeaderMeta, BlockMeta, BlockStatus, Bytes, HashPrefix, HeaderEntry, TransactionStatus,
TransactionStatus,
}; };
use errors::*; use errors::*;
...@@ -87,15 +83,15 @@ impl Status { ...@@ -87,15 +83,15 @@ impl Status {
self.funding().map(|output| output.value as i64).sum() self.funding().map(|output| output.value as i64).sum()
} }
pub fn history(&self) -> Vec<(i32, Sha256dHash)> { pub fn history(&self) -> Vec<(u32, Sha256dHash)> {
let mut txns_map = HashMap::<Sha256dHash, i32>::new(); let mut txns_map = HashMap::<Sha256dHash, u32>::new();
for f in self.funding() { for f in self.funding() {
txns_map.insert(f.txn_id, f.height as i32); txns_map.insert(f.txn_id, f.height);
} }
for s in self.spending() { for s in self.spending() {
txns_map.insert(s.txn_id, s.height as i32); txns_map.insert(s.txn_id, s.height);
} }
let mut txns: Vec<(i32, Sha256dHash)> = let mut txns: Vec<(u32, Sha256dHash)> =
txns_map.into_iter().map(|item| (item.1, item.0)).collect(); txns_map.into_iter().map(|item| (item.1, item.0)).collect();
txns.sort_unstable(); txns.sort_unstable();
txns txns
...@@ -110,15 +106,8 @@ impl Status { ...@@ -110,15 +106,8 @@ impl Status {
txns_map.insert(s.txn_id, &s.txn.as_ref().unwrap()); txns_map.insert(s.txn_id, &s.txn.as_ref().unwrap());
} }
let mut txns: Vec<&TxnHeight> = txns_map.into_iter().map(|item| item.1).collect(); let mut txns: Vec<&TxnHeight> = txns_map.into_iter().map(|item| item.1).collect();
txns.sort_by(|a, b| { // Sort in reverse confirmation height order (unconfirmed txns use u32::max_value as their height):
if a.height == 0 { txns.sort_by(|a, b| b.height.cmp(&a.height));
Ordering::Less
} else if b.height == 0 {
Ordering::Greater
} else {
b.height.cmp(&a.height)
}
});
txns txns
} }
...@@ -139,22 +128,6 @@ impl Status { ...@@ -139,22 +128,6 @@ impl Status {
outputs.sort_unstable_by_key(|out| out.height); outputs.sort_unstable_by_key(|out| out.height);
outputs outputs
} }
pub fn hash(&self) -> Option<FullHash> {
let txns = self.history();
if txns.is_empty() {
None
} else {
let mut hash = FullHash::default();
let mut sha2 = Sha256::new();
for (height, txn_id) in txns {
let part = format!("{}:{}:", txn_id.be_hex_string(), height);
sha2.input(part.as_bytes());
}
sha2.result(&mut hash);
Some(hash)
}
}
} }
#[derive(Clone)] #[derive(Clone)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment