-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.js
43 lines (36 loc) · 1.2 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
const { createSHA256Hash, MINT_PUBLIC_ADDRESS } = require('./helpers');
class Block {
constructor(timestamp = '', data = []) {
this.timestamp = timestamp;
this.data = data;
this.hash = this.getHash();
this.previousHash = null;
this.nonce = 0;
}
getHash() {
return createSHA256Hash(this.previousHash + this.timestamp + JSON.stringify(this.data) + this.timestamp + this.nonce);
}
mine(difficulty) {
while (!this.hash.startsWith(Array(difficulty + 1).join('0'))) {
this.nonce++;
this.hash = this.getHash();
}
}
hasValidTransactions(chain) {
let gas = 0;
let reward = 0;
this.data.forEach(transaction => {
if (transaction.from !== MINT_PUBLIC_ADDRESS) {
gas += transaction.gas;
} else {
reward += transaction.amount;
}
});
return (
reward - gas === chain.reward &&
this.data.every(transaction => transaction.isValid(transaction, chain)) &&
this.data.filter(transaction => transaction.from === MINT_PUBLIC_ADDRESS).length === 1
);
}
}
module.exports = Block;