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
mod rchain;
use rchain::{Blockchain, Block, Transaction, TransactionData};
use std::borrow::BorrowMut;
fn main() {
println!("Demo RChain Version 1\n---------");
let mut bc = Blockchain::new();
let mut genesis = Block::new(None);
let initial_users = vec!("alice", "bob");
for user in initial_users {
let create_transaction = Transaction::new(user.into(),
TransactionData::CreateUserAccount(user.into()),
0);
let token_action = Transaction::new(user.into(),
TransactionData::CreateTokens {receiver: user.into(), amount: 100_000_000},
0);
genesis.add_transaction(create_transaction);
genesis.add_transaction(token_action);
}
let mut res = bc.append_block(genesis);
println!("Genesis block successfully added: {:?}", res);
println!("Full blockchain printout");
println!("{:#?}", bc);
let mut block2 = Block::new(bc.get_last_block_hash());
block2.add_transaction(Transaction::new(
"alice".into(),
TransactionData::TransferTokens {to: "bob".into(), amount: 1}, 0));
res = bc.append_block(block2);
println!("Block added: {:?}", res);
println!("Full blockchain printout");
println!("{:#?}", bc);
println!("Blockchain valid: {:?}", bc.check_validity());
let mut bc_attack_1 = bc.clone();
let transaction_data = bc_attack_1.blocks[1].transactions[0].borrow_mut();
match transaction_data.record.borrow_mut() {
&mut TransactionData::TransferTokens {to:_, ref mut amount} => {
*amount = 100;
},
_ => {}
}
println!("Changed transaction: {:?}", transaction_data.record);
println!("Is the Blockchain still valid? {:#?}", bc_attack_1.check_validity());
let mut bc_attack_2 = bc.clone();
let transaction_data= bc_attack_2.blocks[0].transactions[1].borrow_mut();
match transaction_data.record.borrow_mut() {
&mut TransactionData::CreateTokens {receiver: _, ref mut amount} => {
*amount = 100_000_000_000;
},
_ => {}
}
println!("Is the Blockchain still valid? {:#?}", bc_attack_2.check_validity());
bc_attack_2.blocks[0].update_hash();
println!("Is the Blockchain still valid? {:#?}", bc_attack_2.check_validity());
}