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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use crate::prelude::*;
use anyhow::{anyhow, Result};
use chrono::Utc;
use rand::{CryptoRng, Rng};
use std::{collections::HashMap, sync::atomic::AtomicBool};
#[derive(Clone, Debug)]
pub struct Ledger<N: Network> {
canon_blocks: Blocks<N>,
orphan_blocks: HashMap<u32, Block<N>>,
memory_pool: MemoryPool<N>,
}
impl<N: Network> Ledger<N> {
pub fn new() -> Result<Self> {
Ok(Self {
canon_blocks: Blocks::new()?,
orphan_blocks: Default::default(),
memory_pool: MemoryPool::new(),
})
}
pub fn latest_block_height(&self) -> u32 {
self.canon_blocks.latest_block_height()
}
pub fn latest_block_hash(&self) -> N::BlockHash {
self.canon_blocks.latest_block_hash()
}
pub fn latest_ledger_root(&self) -> N::LedgerRoot {
self.canon_blocks.latest_ledger_root()
}
pub fn latest_block_timestamp(&self) -> Result<i64> {
self.canon_blocks.latest_block_timestamp()
}
pub fn latest_block_difficulty_target(&self) -> Result<u64> {
self.canon_blocks.latest_block_difficulty_target()
}
pub fn latest_cumulative_weight(&self) -> Result<u128> {
self.canon_blocks.latest_cumulative_weight()
}
pub fn latest_block_transactions(&self) -> Result<&Transactions<N>> {
self.canon_blocks.latest_block_transactions()
}
pub fn latest_block(&self) -> Result<Block<N>> {
self.canon_blocks.latest_block()
}
pub fn contains_ledger_root(&self, ledger_root: &N::LedgerRoot) -> bool {
self.canon_blocks.contains_ledger_root(ledger_root)
}
pub fn contains_block_hash(&self, block_hash: &N::BlockHash) -> bool {
self.canon_blocks.contains_block_hash(block_hash)
}
pub fn contains_transaction(&self, transaction: &Transaction<N>) -> bool {
self.canon_blocks.contains_transaction(transaction)
}
pub fn add_next_block(&mut self, block: &Block<N>) -> Result<()> {
self.canon_blocks.add_next(block)?;
Ok(())
}
pub fn add_orphan_block(&mut self, block: &Block<N>) -> Result<()> {
if self.canon_blocks.contains_block_hash(&block.hash()) {
return Err(anyhow!("Orphan block already exists in canon chain"));
}
self.orphan_blocks.insert(block.height(), block.clone());
Ok(())
}
pub fn add_unconfirmed_transaction(&mut self, transaction: &Transaction<N>) -> Result<()> {
if !self.canon_blocks.contains_ledger_root(&transaction.ledger_root()) {
return Err(anyhow!("Transaction references a non-existent ledger root"));
}
for serial_number in transaction.serial_numbers() {
if self.canon_blocks.contains_serial_number(serial_number) {
return Err(anyhow!("Transaction contains a serial number already in existence"));
}
}
for commitment in transaction.commitments() {
if self.canon_blocks.contains_commitment(commitment) {
return Err(anyhow!("Transaction contains a commitment already in existence"));
}
}
self.memory_pool.add_transaction(transaction)?;
Ok(())
}
pub fn mine_next_block<R: Rng + CryptoRng>(
&mut self,
recipient: Address<N>,
is_public: bool,
terminator: &AtomicBool,
rng: &mut R,
) -> Result<Record<N>> {
let previous_block_hash = self.latest_block_hash();
let block_height = self.latest_block_height() + 1;
let block_timestamp = std::cmp::max(Utc::now().timestamp(), self.latest_block_timestamp()?.saturating_add(1));
let difficulty_target = if N::NETWORK_ID == 2 && block_height <= crate::testnet2::V12_UPGRADE_BLOCK_HEIGHT {
Blocks::<N>::compute_difficulty_target(self.latest_block()?.header(), block_timestamp, block_height)
} else if N::NETWORK_ID == 2 {
let anchor_block_header = self
.canon_blocks
.get_block_header(crate::testnet2::V12_UPGRADE_BLOCK_HEIGHT)?;
Blocks::<N>::compute_difficulty_target(anchor_block_header, block_timestamp, block_height)
} else {
Blocks::<N>::compute_difficulty_target(N::genesis_block().header(), block_timestamp, block_height)
};
let cumulative_weight = self
.latest_cumulative_weight()?
.saturating_add((u64::MAX / difficulty_target) as u128);
let amount = Block::<N>::block_reward(block_height);
let (coinbase_transaction, coinbase_record) =
Transaction::<N>::new_coinbase(recipient, amount, is_public, rng)?;
let transactions = Transactions::from(&[vec![coinbase_transaction], self.memory_pool.transactions()].concat())?;
let previous_ledger_root = self.canon_blocks.latest_ledger_root();
let template = BlockTemplate::new(
previous_block_hash,
block_height,
block_timestamp,
difficulty_target,
cumulative_weight,
previous_ledger_root,
transactions,
coinbase_record.clone(),
);
let block = Block::mine(&template, terminator, rng)?;
self.add_next_block(&block)?;
self.memory_pool.clear_all_transactions();
Ok(coinbase_record)
}
pub fn to_ledger_tree(&self) -> &LedgerTree<N> {
self.canon_blocks.to_ledger_tree()
}
pub fn to_ledger_proof(&self, commitment: N::Commitment) -> Result<LedgerProof<N>> {
self.canon_blocks.to_ledger_proof(commitment)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{testnet1::Testnet1, testnet2::Testnet2};
use rand::thread_rng;
#[test]
fn test_new() {
let ledger = Ledger::<Testnet1>::new().unwrap();
assert_eq!(0, ledger.latest_block_height());
let ledger = Ledger::<Testnet2>::new().unwrap();
assert_eq!(0, ledger.latest_block_height());
}
#[test]
fn test_mine_next_block() {
let rng = &mut thread_rng();
{
let mut ledger = Ledger::<Testnet1>::new().unwrap();
let recipient = Account::<Testnet1>::new(rng);
assert_eq!(0, ledger.latest_block_height());
ledger
.mine_next_block(recipient.address(), true, &AtomicBool::new(false), rng)
.unwrap();
assert_eq!(1, ledger.latest_block_height());
}
{
let mut ledger = Ledger::<Testnet2>::new().unwrap();
let recipient = Account::<Testnet2>::new(rng);
assert_eq!(0, ledger.latest_block_height());
ledger
.mine_next_block(recipient.address(), true, &AtomicBool::new(false), rng)
.unwrap();
assert_eq!(1, ledger.latest_block_height());
}
}
}