Commit 8dc2de66 authored by kenshin-samourai's avatar kenshin-samourai
Browse files

remove height from TxRow

parent de6ec832
......@@ -63,13 +63,13 @@ impl Parser {
let mut rows = Vec::<Row>::new();
for block in blocks {
let blockhash = block.bitcoin_hash();
if let Some(header) = self.current_headers.header_by_blockhash(&blockhash) {
if let Some(_header) = self.current_headers.header_by_blockhash(&blockhash) {
if self.indexed_blockhashes
.lock()
.expect("indexed_blockhashes")
.insert(blockhash)
{
rows.extend(index_block(&block, header.height()));
rows.extend(index_block(&block));
}
}
}
......
......@@ -6,8 +6,7 @@ use bitcoin::util::hash::BitcoinHash;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use std::collections::HashSet;
use std::sync::RwLock;
use crate::daemon::Daemon;
......@@ -116,19 +115,18 @@ pub struct TxKey {
pub txid: FullHash,
}
#[derive(Serialize, Deserialize)]
pub struct TxRow {
pub key: TxKey,
pub height: u32, // value
}
impl TxRow {
pub fn new(txid: &Sha256dHash, height: u32) -> TxRow {
pub fn new(txid: &Sha256dHash) -> TxRow {
TxRow {
key: TxKey {
code: b'T',
txid: full_hash(&txid[..]),
},
height,
}
}
......@@ -142,16 +140,13 @@ impl TxRow {
pub fn to_row(&self) -> Row {
Row {
key: bincode::serialize(&self.key).unwrap(),
value: bincode::serialize(&self.height).unwrap(),
key: bincode::serialize(&self).unwrap(),
value: vec![],
}
}
pub fn from_row(row: &Row) -> TxRow {
TxRow {
key: bincode::deserialize(&row.key).expect("failed to parse TxKey"),
height: bincode::deserialize(&row.value).expect("failed to parse height"),
}
bincode::deserialize(&row.key).expect("failed to parse TxRow")
}
}
......@@ -169,10 +164,7 @@ pub fn compute_script_hash(data: &[u8]) -> FullHash {
hash
}
pub fn index_transaction<'a>(
txn: &'a Transaction,
height: usize,
) -> impl 'a + Iterator<Item = Row> {
pub fn index_transaction<'a>(txn: &'a Transaction) -> impl 'a + Iterator<Item = Row> {
let null_hash = Sha256dHash::default();
let txid: Sha256dHash = txn.txid();
......@@ -190,13 +182,12 @@ pub fn index_transaction<'a>(
.enumerate()
.map(move |(vout, output)| TxOutRow::new(&txid, vout as u32, &output).to_row());
// Persist transaction ID and confirmed height
inputs
.chain(outputs)
.chain(std::iter::once(TxRow::new(&txid, height as u32).to_row()))
.chain(std::iter::once(TxRow::new(&txid).to_row()))
}
pub fn index_block<'a>(block: &'a Block, height: usize) -> impl 'a + Iterator<Item = Row> {
pub fn index_block<'a>(block: &'a Block) -> impl 'a + Iterator<Item = Row> {
let blockhash = block.bitcoin_hash();
// Persist block hash and header
let row = Row {
......@@ -210,7 +201,7 @@ pub fn index_block<'a>(block: &'a Block, height: usize) -> impl 'a + Iterator<It
block
.txdata
.iter()
.flat_map(move |txn| index_transaction(&txn, height))
.flat_map(move |txn| index_transaction(&txn))
.chain(std::iter::once(row))
}
......@@ -322,10 +313,6 @@ impl Index {
info!("{:?} ({} left to index)", latest_header, new_headers.len());
};
let height_map = HashMap::<Sha256dHash, usize>::from_iter(
new_headers.iter().map(|h| (*h.hash(), h.height())),
);
let chan = SyncChannel::new(1);
let sender = chan.sender();
let blockhashes: Vec<Sha256dHash> = new_headers.iter().map(|h| *h.hash()).collect();
......@@ -356,10 +343,7 @@ impl Index {
let rows_iter = batch.iter().flat_map(|block| {
let blockhash = block.bitcoin_hash();
let height = *height_map
.get(&blockhash)
.unwrap_or_else(|| panic!("missing header for block {}", blockhash));
index_block(block, height).chain(std::iter::once(last_indexed_block(&blockhash)))
index_block(block).chain(std::iter::once(last_indexed_block(&blockhash)))
});
store.write(rows_iter);
......
......@@ -24,7 +24,7 @@ impl MempoolStore {
}
fn add(&mut self, tx: &Transaction) {
let rows = index_transaction(tx, 0);
let rows = index_transaction(tx);
for row in rows {
let (key, value) = row.into_pair();
self.map.entry(key).or_insert_with(|| vec![]).push(value);
......@@ -32,7 +32,7 @@ impl MempoolStore {
}
fn remove(&mut self, tx: &Transaction) {
let rows = index_transaction(tx, 0);
let rows = index_transaction(tx);
for row in rows {
let (key, value) = row.into_pair();
let no_values_left = {
......
use bitcoin::consensus::encode::deserialize;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use crate::app::App;
......@@ -13,7 +12,6 @@ use crate::util::HashPrefix;
pub struct FundingOutput {
pub txn_id: Sha256dHash,
pub height: u32,
pub output_index: usize,
}
......@@ -21,7 +19,6 @@ type OutPoint = (Sha256dHash, usize); // (txid, output_index)
struct SpendingInput {
txn_id: Sha256dHash,
height: u32,
funding_output: OutPoint,
}
......@@ -39,26 +36,19 @@ impl Status {
self.confirmed.1.iter().chain(self.mempool.1.iter())
}
pub fn history(&self) -> Vec<(i32, Sha256dHash)> {
let mut txns_map = HashMap::<Sha256dHash, i32>::new();
pub fn history(&self) -> Vec<Sha256dHash> {
let mut txns = vec![];
for f in self.funding() {
txns_map.insert(f.txn_id, f.height as i32);
txns.push(f.txn_id);
}
for s in self.spending() {
txns_map.insert(s.txn_id, s.height as i32);
txns.push(s.txn_id);
}
let mut txns: Vec<(i32, Sha256dHash)> =
txns_map.into_iter().map(|item| (item.1, item.0)).collect();
txns.sort_unstable();
txns
}
}
struct TxnHeight {
txn_id: Sha256dHash,
height: u32,
}
fn txrows_by_prefix(
store: &dyn ReadStore,
txid_prefix: HashPrefix
......@@ -115,15 +105,12 @@ impl Query {
&self,
store: &dyn ReadStore,
prefixes: Vec<HashPrefix>,
) -> Result<Vec<TxnHeight>> {
) -> Result<Vec<Sha256dHash>> {
let mut txns = vec![];
for txid_prefix in prefixes {
for tx_row in txrows_by_prefix(store, txid_prefix) {
let txid: Sha256dHash = deserialize(&tx_row.key.txid).unwrap();
txns.push(TxnHeight {
txn_id: txid,
height: tx_row.height,
})
txns.push(txid)
}
}
Ok(txns)
......@@ -140,10 +127,9 @@ impl Query {
let spending_txns = self.load_txns_by_prefix(store, prefixes)?;
for t in &spending_txns {
if t.txn_id == funding.txn_id {
if *t == funding.txn_id {
spending_inputs.push(SpendingInput {
txn_id: t.txn_id,
height: t.height,
txn_id: *t,
funding_output: (funding.txn_id, funding.output_index),
})
}
......@@ -171,8 +157,7 @@ impl Query {
let funding_txns = self.load_txns_by_prefix(store, vec![row.txid_prefix])?;
for t in &funding_txns {
result.push(FundingOutput {
txn_id: t.txn_id,
height: t.height,
txn_id: *t,
output_index: row.vout as usize,
})
}
......
......@@ -59,7 +59,7 @@ impl Connection {
status
.history()
.into_iter()
.map(|item| json!({"height": item.0, "tx_hash": item.1.to_hex()}))
.map(|item| json!({"tx_hash": item.to_hex()}))
.collect()
)))
}
......
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