1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::collections::HashMap;
use chain::{Transaction, TransactionOutput, OutPoint};
use storage::TransactionOutputProvider;
use miner::{DoubleSpendCheckResult, HashedOutPoint, NonFinalDoubleSpendSet};
use verification::TransactionError;
use super::super::types::{MemoryPoolRef, StorageRef};
pub struct MemoryPoolTransactionOutputProvider {
storage_provider: StorageRef,
mempool_inputs: HashMap<HashedOutPoint, Option<TransactionOutput>>,
nonfinal_spends: Option<NonFinalDoubleSpendSet>,
}
impl MemoryPoolTransactionOutputProvider {
pub fn for_transaction(storage: StorageRef, memory_pool: &MemoryPoolRef, transaction: &Transaction) -> Result<Self, TransactionError> {
let memory_pool = memory_pool.read();
let check_result = memory_pool.check_double_spend(transaction);
match check_result {
DoubleSpendCheckResult::DoubleSpend(_, hash, index) => Err(TransactionError::UsingSpentOutput(hash, index)),
DoubleSpendCheckResult::NoDoubleSpend => Ok(MemoryPoolTransactionOutputProvider {
storage_provider: storage,
mempool_inputs: transaction.inputs.iter()
.map(|input| (
input.previous_output.clone().into(),
memory_pool.transaction_output(&input.previous_output, usize::max_value()),
)).collect(),
nonfinal_spends: None,
}),
DoubleSpendCheckResult::NonFinalDoubleSpend(nonfinal_spends) => Ok(MemoryPoolTransactionOutputProvider {
storage_provider: storage,
mempool_inputs: transaction.inputs.iter()
.map(|input| (
input.previous_output.clone().into(),
memory_pool.transaction_output(&input.previous_output, usize::max_value()),
)).collect(),
nonfinal_spends: Some(nonfinal_spends),
}),
}
}
}
impl TransactionOutputProvider for MemoryPoolTransactionOutputProvider {
fn transaction_output(&self, prevout: &OutPoint, transaction_index: usize) -> Option<TransactionOutput> {
let hashed_prevout: HashedOutPoint = prevout.clone().into();
if let Some(ref nonfinal_spends) = self.nonfinal_spends {
if nonfinal_spends.dependent_spends.contains(&hashed_prevout) {
return None;
}
}
if let Some(output) = self.mempool_inputs.get(&hashed_prevout) {
if let Some(ref output) = *output {
return Some(output.clone());
}
}
self.storage_provider.transaction_output(prevout, transaction_index)
}
fn is_spent(&self, prevout: &OutPoint) -> bool {
if let Some(ref nonfinal_spends) = self.nonfinal_spends {
if nonfinal_spends.double_spends.contains(&prevout.clone().into()) {
return false;
}
}
self.storage_provider.is_spent(prevout)
}
}
#[cfg(test)]
mod tests {
extern crate test_data;
use std::sync::Arc;
use parking_lot::RwLock;
use chain::OutPoint;
use storage::{TransactionOutputProvider};
use db::BlockChainDatabase;
use miner::MemoryPool;
use super::MemoryPoolTransactionOutputProvider;
#[test]
fn when_transaction_depends_on_removed_nonfinal_transaction() {
let dchain = &mut test_data::ChainBuilder::new();
test_data::TransactionBuilder::with_output(10).store(dchain)
.reset().set_input(&dchain.at(0), 0).add_output(20).lock().store(dchain)
.reset().set_input(&dchain.at(1), 0).add_output(30).store(dchain)
.reset().set_input(&dchain.at(0), 0).add_output(40).store(dchain);
let storage = Arc::new(BlockChainDatabase::init_test_chain(vec![test_data::genesis().into()]));
let memory_pool = Arc::new(RwLock::new(MemoryPool::new()));
{
memory_pool.write().insert_verified(dchain.at(0).into());
memory_pool.write().insert_verified(dchain.at(1).into());
memory_pool.write().insert_verified(dchain.at(2).into());
}
let provider = MemoryPoolTransactionOutputProvider::for_transaction(storage, &memory_pool, &dchain.at(3)).unwrap();
assert_eq!(provider.is_spent(&OutPoint { hash: dchain.at(0).hash(), index: 0, }), false);
assert_eq!(provider.is_spent(&OutPoint { hash: dchain.at(1).hash(), index: 0, }), false);
assert_eq!(provider.is_spent(&OutPoint { hash: dchain.at(2).hash(), index: 0, }), false);
assert_eq!(provider.transaction_output(&OutPoint { hash: dchain.at(0).hash(), index: 0, }, 0), Some(dchain.at(0).outputs[0].clone()));
assert_eq!(provider.transaction_output(&OutPoint { hash: dchain.at(1).hash(), index: 0, }, 0), None);
assert_eq!(provider.transaction_output(&OutPoint { hash: dchain.at(2).hash(), index: 0, }, 0), None);
}
}