-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.js
85 lines (70 loc) · 2.55 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
const SHA256 = require('crypto-js/sha256');
const { Tools } = require('./Tools');
const { UTXOPool } = require('./UTXOPool');
const { clone, values } = require('ramda');
class Block {
constructor(height, blockchain, previousBlockHash = '', utxoPool = new UTXOPool(), feeBeneficiaryAddress = '0xLIMIA') {
this.blockchain = blockchain;
this.feeBeneficiaryAddress = feeBeneficiaryAddress;
this.utxoPool = utxoPool;
this.transactions = {};
this._header = {
version: 1,
previousBlockHash: previousBlockHash,
merkleRootHash: '',
timestamp: Tools.createTimestamp(),
nonce: 0,
height: height
};
this.hash = this.calculateHash();
}
isRoot() {
return this._header.previousBlockHash === 'root';
}
isValid() {
return this.isRoot() ||this.hash === this.calculateHash();
}
createChild(feeBeneficiaryAddress, difficulty) {
const block = new Block(this._header.height + 1, this.blockchain,
this._header.previousBlockHash = this.hash, clone(this.utxoPool), feeBeneficiaryAddress);
return block;
// For convenience, allow the miner to immediately spend the coinbase coins
}
addTransactionIfValid(transaction) {
if (!this.isValidTransaction(transaction)) return;
this.transactions[transaction.hash] = transaction;
this.utxoPool.handleTransaction(transaction, this.feeBeneficiaryAddress);
this.hash = this.calculateHash();
}
isValidTransaction(transaction) {
return this.utxoPool.isValidTransaction(transaction) && transaction.hasValidSignature();
}
addingTransactionErrorMessage(transaction) {
if (!transaction.hasValidSignature()) return "Signature is not valid";
return this.utxoPool.addingTransactionErrorMessage(transaction);
}
calculateHash() {
this._header.merkleRootHash = this.calculateHashMerkleRoot();
return SHA256(JSON.stringify(this._header).toString()).toString();
}
calculateHashMerkleRoot() {
if (values(this.transactions).length === 0) return '';
const transactionHashes = values(this.transactions).map((transaction) => transaction.hash);
return Tools.computeMerkleRootHash(transactionHashes);
}
mineBlock(difficulty, feeBeneficiaryAddress) {
while(this.hash.substring(0, difficulty) !== Array(difficulty + 1).join('0')) {
this._header.nonce++;
this.hash = this.calculateHash();
}
if (this.isValid()) {
this.utxoPool.addUTXO(feeBeneficiaryAddress, 12.5); // TODO This needs to be added as a transaction
return true;
} else {
return false;
}
}
}
module.exports = {
Block
};