-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChain.cpp
More file actions
152 lines (125 loc) · 2.93 KB
/
Copy pathChain.cpp
File metadata and controls
152 lines (125 loc) · 2.93 KB
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
#include "Chain.h"
using namespace std;
Chain::Chain(int miners, int sign_len) {
head = tail = NULL;
size = 0;
miner = Miner(sign_len);
mr = Miner_Race(miners, sign_len);
mine_race = true;
}
Chain::Chain(int sign_len) {
head = tail = NULL;
size = 0;
miner = Miner(sign_len);
mine_race = false;
}
Chain::Chain() {
head = tail = NULL;
size = 0;
miner = Miner(2);
mine_race = false;
}
Chain::Node *Chain::new_node(Block b) {
struct Node *node = new struct Node;
node->block = b;
return node;
}
int Chain::add_block(Block b) {
struct Node *node = new_node(b);
int miner_won;
if (head == NULL) {
miner_won = mine_controller(node->block);
head = tail = node;
node->prev = node->next = NULL;
node->block_index = 0;
} else {
memcpy(node->block.prev_hash, tail->block.hash, SHA256_DIGEST_LENGTH);
miner_won = mine_controller(node->block);
node->block_index = tail->block_index + 1;
node->next = NULL;
node->prev = tail;
tail->next = node;
tail = node;
}
size++;
return miner_won;
}
void Chain::print_chain() {
struct Node *p = head;
while (p != NULL) {
cout << "#" << p->block_index << endl;
p->block.print();
cout << endl;
p = p->next;
}
}
void Chain::edit_block(int index, string new_data) {
struct Node *node = get_block(index);
if (node == NULL) {
cerr << "Block not found by index\n";
return;
}
node->block.add_data(new_data);
update_blocks_from(node);
}
Chain::Node *Chain::get_block(int index) {
if (index >= size) return NULL;
struct Node *p = head;
while (p->block_index != index) {
p = p->next;
}
return p;
}
void Chain::update_blocks_from(struct Node *node) {
while (node != NULL) {
if (node != head)
memcpy(node->block.prev_hash, node->prev->block.hash,
SHA256_DIGEST_LENGTH);
miner.hash_block(node->block);
node = node->next;
}
}
Block *Chain::block_iterator(int start) {
static struct Node *p;
if (start >= 0)
p = get_block(start);
else if (p == NULL)
return NULL;
Block *res = &(p->block);
p = p->next;
return res;
}
void Chain::remine_blocks_from(int index) {
struct Node *node = get_block(index);
if (node == NULL) {
cerr << "Block not found\n";
return;
}
while (node != NULL) {
if (node != head)
memcpy(node->block.prev_hash, node->prev->block.hash,
SHA256_DIGEST_LENGTH);
miner.mine_block(node->block);
node = node->next;
}
}
void Chain::remine_block(int index) {
struct Node *node = get_block(index);
if (node == NULL) {
cerr << "Block not found\n";
return;
}
miner.mine_block(node->block);
node = node->next;
while (node != NULL) {
memcpy(node->block.prev_hash, node->prev->block.hash, SHA256_DIGEST_LENGTH);
miner.hash_block(node->block);
node = node->next;
}
}
int Chain::mine_controller(Block &b) {
if(mine_race)
return mr.race(b);
miner.mine_block(b);
return -1;
}