-
Notifications
You must be signed in to change notification settings - Fork 62
/
block.js
132 lines (116 loc) · 3.02 KB
/
block.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
var crypto = require('crypto');
var ByteBuffer = require('bytebuffer');
var _ = require('lodash');
var Transaction = require('./transaction');
function Block(data) {
this.data = _.assign({
version: 0,
height: 0,
size: 0,
timestamp: 0,
generatorId: 0,
previousHash: '',
merkleHash: '',
transactions: []
}, data);
this.transactions = [];
var size = 0;
for (var i in this.data.transactions) {
var t = new Transaction(this.data.transactions[i]);
size += t.getSize();
this.transactions.push(t);
}
if (this.transactions.length === 0) {
var coinbaseTrs = new Transaction({
amount: 5,
recipient: 'somebody',
sender: 'nobody'
});
size += coinbaseTrs.getSize();
this.transactions.push(coinbaseTrs);
}
for (var i in this.transactions) {
this.data.transactions.push(this.transactions[i].getData());
}
if (!this.data.size) {
this.data.size = size;
}
if (!this.data.merkleHash) {
this.data.merkleHash = this.calculateMerkleHash();
}
if (!this.data.hash) {
this.data.hash = this.calculateHash();
}
}
Block.prototype.addTransaction = function(trs) {
this.transactions.push(trs);
this.size += trs.getSize();
this.data.transactions.push(trs.getData());
this.data.merkleHash = this.calculateMerkleHash();
this.data.hash = this.calculateHash();
}
Block.prototype.getData = function() {
return this.data;
}
Block.prototype.getVersion = function() {
return this.data.version;
}
Block.prototype.getHeight = function() {
return this.data.height;
}
Block.prototype.getSize = function() {
return this.data.size;
}
Block.prototype.getTimestamp = function() {
return this.data.timestamp;
}
Block.prototype.getGeneratorId = function() {
return this.data.generatorId;
}
Block.prototype.getPreviousHash = function() {
return this.data.previousHash;
}
Block.prototype.getHash = function() {
return this.data.hash;
}
Block.prototype.getMerkleHash = function() {
return this.data.merkleHash;
}
Block.prototype.getTransactions = function() {
return this.transactions;
}
Block.prototype.calculateMerkleHash = function() {
var hashes = [];
this.transactions.forEach(function(t) {
hashes.push(t.getHash());
});
while (hashes.length > 1) {
var tmp = [];
for (var i = 0; i < hashes.length / 2; ++i) {
var md = crypto.createHash('sha256');
md.update(hashes[i*2]);
md.update(hashes[i*2+1]);
tmp.push(md.digest().toString('hex'));
}
if (hashes.length % 2 === 1) {
tmp.push(hashes[hashes.length - 1]);
}
hashes = tmp;
}
return hashes[0];
}
Block.prototype.calculateHash = function() {
var buf = new ByteBuffer();
var d = this.data;
buf.writeInt(d.version);
buf.writeInt(d.height);
buf.writeInt(d.size);
buf.writeInt(d.timestamp);
buf.writeInt(d.generatorId);
buf.writeIString(d.previousHash);
buf.writeIString(d.merkleHash);
buf.flip();
var bytes = buf.toBuffer();
return crypto.createHash('sha256').update(bytes).digest().toString('hex');
}
module.exports = Block;