-
Notifications
You must be signed in to change notification settings - Fork 62
/
blockchain.js
213 lines (196 loc) · 5.79 KB
/
blockchain.js
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
var assert = require('assert');
var EventEmitter = require('events');
var util = require('util');
var slots = require('./slots');
var protocol = require('./protocol');
var Block = require('./block');
var Transaction = require('./transaction');
var HashList = require('./hashlist');
var Pbft = require('./pbft');
var COIN = 100000000;
function BlockChain(node) {
EventEmitter.call(this);
this.node = node;
this.genesis = new Block({
height: 0,
timestamp: 1462953000,
previousHash: '',
generatorId: 0,
transactions: [
{
amount: 100000000 * COIN,
timestamp: 1462953000,
recipient: 'neo',
sender: '',
}
]
});
this.pendingTransactions = {};
this.transactionIndex = {};
this.chain = new HashList();
this.chain.add(this.genesis.getHash(), this.genesis);
this.pbft = new Pbft(this);
this.lastSlot = 0;
}
util.inherits(BlockChain, EventEmitter);
BlockChain.prototype.start = function() {
var self = this;
setImmediate(function nextLoop() {
self.loop_(function() {
setTimeout(nextLoop, 1000);
});
});
}
BlockChain.prototype.hasTransaction = function(trs) {
var id = trs.getHash();
return !!this.pendingTransactions[id] || !!this.transactionIndex[id];
}
BlockChain.prototype.validateTransaction = function(trs) {
return !!trs;
}
BlockChain.prototype.addTransaction = function(trs) {
this.pendingTransactions[trs.getHash()] = trs;
}
BlockChain.prototype.hasBlock = function(hash) {
return !!this.chain.get(hash) || this.pbft.hasBlock(hash);
}
BlockChain.prototype.validateBlock = function(block) {
if (!block) {
return false;
}
var lastBlock = this.chain.last();
return block.getHeight() === lastBlock.getHeight() + 1 &&
block.getPreviousHash() === lastBlock.getHash();
}
BlockChain.prototype.addBlock = function(block) {
if (Flags.pbft && !this.node.isBad) {
var slotNumber = slots.getSlotNumber(slots.getTime(block.getTimestamp() * 1000));
this.pbft.addBlock(block, slotNumber);
} else {
this.commitBlock(block);
this.isBusy = false;
}
}
BlockChain.prototype.commitBlock = function(block) {
this.chain.add(block.getHash(), block);
var transactions = block.getTransactions();
for (var i in transactions) {
this.transactionIndex[transactions[i].getHash()] = transactions[i];
}
}
BlockChain.prototype.processMessage = function(msg) {
switch (msg.type) {
case protocol.MessageType.Transaction:
var trs = new Transaction(msg.body);
if (!this.hasTransaction(trs)) {
if (this.validateTransaction(trs)) {
this.node.broadcast(msg);
this.addTransaction(trs);
}
}
break;
case protocol.MessageType.Block:
var block = new Block(msg.body);
if (!this.hasBlock(block.getHash())) {
if (this.validateBlock(block)) {
this.node.broadcast(msg);
this.addBlock(block);
}
}
break;
default:
if (Flags.pbft && !this.node.isBad) {
this.pbft.processMessage(msg);
}
break;
}
}
BlockChain.prototype.createBlock = function(cb) {
var lastBlock = this.chain.last();
assert(!!lastBlock);
var newBlock = new Block({
height: lastBlock.getHeight() + 1,
timestamp: Math.floor(Date.now() / 1000),
previousHash: lastBlock.getHash(),
generatorId: this.node.id,
});
for (var k in this.pendingTransactions) {
newBlock.addTransaction(this.pendingTransactions[k]);
}
this.pendingTransactions = {};
return newBlock;
}
BlockChain.prototype.printBlockChain = function() {
var output = '';
this.chain.each(function(block, i) {
output += util.format('(%d:%s:%d) -> ', i, block.getHash().substr(0, 6), block.getGeneratorId());
});
console.log('node ' + this.node.id, output);
}
BlockChain.prototype.makeFork_ = function() {
var lastBlock = this.chain.last();
assert(!!lastBlock);
var height = lastBlock.getHeight() + 1;
var timestamp = Math.floor(Date.now() / 1000);
var block1 = new Block({
height: height,
timestamp: timestamp,
previousHash: lastBlock.getHash(),
generatorId: this.node.id
});
block1.addTransaction(new Transaction({
amount: 1000,
recipient: 'alice',
sender: 'cracker'
}));
var block2 = new Block({
height: height,
timestamp: timestamp,
previousHash: lastBlock.getHash(),
generatorId: this.node.id
});
block2.addTransaction(new Transaction({
amount: 1000,
recipient: 'bob',
sender: 'cracker'
}));
console.log('fork on node: %d, height: %d, fork1: %s, fork2: %s', this.node.id, lastBlock.getHeight() + 1, block1.getHash(), block2.getHash());
var i = 0;
for (var id in this.node.peers) {
if (i++ % 2 === 0) {
console.log('send fork1 to', id);
this.node.peers[id].send(protocol.blockMessage(block1.getData()));
} else {
console.log('send fork2 to', id);
this.node.peers[id].send(protocol.blockMessage(block2.getData()));
}
}
this.addBlock(block1);
}
BlockChain.prototype.loop_ = function(cb) {
var currentSlot = slots.getSlotNumber();
var lastBlock = this.chain.last();
assert(!!lastBlock);
// this.printBlockChain();
var lastSlot = slots.getSlotNumber(slots.getTime(lastBlock.getTimestamp() * 1000));
if (currentSlot === lastSlot || Date.now() % 10000 > 5000) {
return cb();
}
if (Flags.pbft && this.lastSlot === currentSlot) {
return cb();
}
var delegateId = currentSlot % slots.delegates;
if (this.node.id === delegateId) {
if (!this.node.isBad) {
var block = this.createBlock();
console.log('slot: %d, height: %d, nodeId: %d', currentSlot, block.getHeight(), this.node.id);
this.addBlock(block);
this.emit('new-message', protocol.blockMessage(block.getData()));
this.lastSlot = currentSlot;
} else {
this.makeFork_();
}
}
cb();
}
module.exports = BlockChain;