-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchain_test.rs
150 lines (125 loc) · 4.45 KB
/
chain_test.rs
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
use std::sync::Arc;
use blockchaintree::{
block, chain, tools,
transaction::{self, Transactionable},
};
use primitive_types::U256;
#[tokio::test]
async fn init_flush_get_block_by_height_chain_test() {
let main_chain = chain::MainChain::new("./BlockChainTree").unwrap();
main_chain.flush().await.unwrap();
drop(main_chain);
let main_chain = chain::MainChain::new("./BlockChainTree").unwrap();
let height = main_chain.get_height();
// generate block
let basic_data = block::BasicInfo {
timestamp: 160000,
pow: [0; 32],
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
height,
difficulty: [101; 32],
founder: [6; 33],
};
let main_block = block::TransactionBlock::new(
U256::from_dec_str("1").unwrap(),
basic_data,
[0; 32],
vec![[0; 32], [1; 32]],
);
main_chain.add_block(Arc::new(main_block)).unwrap();
let height = main_chain.get_height();
let block = main_chain.find_by_height(&(height - 1)).unwrap();
assert!(block.is_some());
let block = block.unwrap();
assert_eq!([6; 33], *block.get_founder());
assert_eq!(160000, block.get_info().timestamp);
assert_eq!([0; 32], block.get_info().pow);
assert_eq!(height - 1, block.get_info().height);
assert_eq!([101; 32], block.get_info().difficulty);
assert_eq!(U256::from_dec_str("1").unwrap(), block.get_fee());
assert_eq!([0; 32], block.get_merkle_root());
}
#[tokio::test]
async fn init_get_transaction_chain_test() {
let main_chain = chain::MainChain::new("./BlockChainTree").unwrap();
let transaction = transaction::Transaction::new_signed(
[20; 33],
[20; 33],
100,
U256::from_dec_str("3627836287").unwrap(),
Some(vec![228, 123]),
[33; 64],
);
main_chain.add_transactions(&[transaction.clone()]).unwrap();
let got_transaction = main_chain
.get_transaction(&tools::hash(&transaction.dump().unwrap()))
.unwrap()
.unwrap();
assert_eq!(transaction.get_data(), got_transaction.get_data());
assert_eq!(transaction.get_amount(), got_transaction.get_amount());
assert_eq!(transaction.get_sender(), got_transaction.get_sender());
assert_eq!(transaction.get_receiver(), got_transaction.get_receiver());
assert_eq!(transaction.get_dump_size(), got_transaction.get_dump_size());
assert_eq!(transaction.get_timestamp(), got_transaction.get_timestamp());
assert_eq!(transaction.get_signature(), got_transaction.get_signature());
}
#[tokio::test]
async fn init_flush_get_block_by_height_deriv_chain_test() {
let deriv_chain = chain::DerivativeChain::new(
"./BlockChainTree",
"deadbeef",
&[
57, 26, 43, 126, 188, 137, 234, 205, 234, 97, 128, 221, 242, 186, 198, 206, 3, 25, 250,
35, 169, 60, 208, 8, 94, 13, 60, 218, 72, 73, 207, 80,
],
)
.unwrap();
deriv_chain.flush().await.unwrap();
drop(deriv_chain);
let deriv_chain = chain::DerivativeChain::new(
"./BlockChainTree",
"deadbeef",
&[
57, 26, 43, 126, 188, 137, 234, 205, 234, 97, 128, 221, 242, 186, 198, 206, 3, 25, 250,
35, 169, 60, 208, 8, 94, 13, 60, 218, 72, 73, 207, 80,
],
)
.unwrap();
// generate block
let basic_data = block::BasicInfo {
timestamp: 160000,
pow: [0; 32],
previous_hash: unsafe { [0; 32].try_into().unwrap_unchecked() },
height: U256::from_dec_str("0").unwrap(),
difficulty: [101; 32],
founder: [6; 33],
};
let derivative_block = block::DerivativeBlock {
default_info: basic_data,
};
deriv_chain.add_block(&derivative_block).unwrap();
let block = deriv_chain.find_by_height(&U256::zero()).unwrap();
assert!(block.is_some());
let block = block.unwrap();
assert_eq!(
derivative_block.default_info.timestamp,
block.default_info.timestamp
);
assert_eq!(derivative_block.default_info.pow, block.default_info.pow);
assert_eq!(
derivative_block.default_info.previous_hash,
block.default_info.previous_hash
);
assert_eq!(
derivative_block.default_info.height,
block.default_info.height
);
assert_eq!(
derivative_block.default_info.difficulty,
block.default_info.difficulty
);
assert_eq!(
derivative_block.default_info.founder,
block.default_info.founder
);
}