-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.js
163 lines (121 loc) · 3.86 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
const crypto = require('crypto');
const uuidv4 = require('uuid/v4');
function Blockchain(){
this.chain=[];
this.pendingTransactions=[];
this.target=1;
this.version=1;
this.minerAward=25;
this.height=0;
this.fee=10;
this.peers=[];
this.miningInProcess = false;
this.nodeAddress,
this.currentNodeAddress=null;
}
Blockchain.prototype.createGensisBlock = function () {
let block = this.createCandidateBlock(null);
block = this.mineBlock(block);
// after creating genesis block increase difficulty
this.target=4;
}
Blockchain.prototype.getPeers = function () {
return this.peers;
}
Blockchain.prototype.addPeer = function (nodeAddress) {
this.peers.push(nodeAddress);
}
Blockchain.prototype.addNewBlock = function (newBlock) {
this.chain.push(newBlock);
this.height++;
}
Blockchain.prototype.getLastBlock = function () {
//
return this.chain[this.chain.length-1];
}
Blockchain.prototype.addNewTransaction = function (transaction) {
//
transaction.txnId = uuidv4().replace(/-/g, '')
this.pendingTransactions.push(transaction)
return transaction.txnId;
}
Blockchain.prototype.getBlockchain = function (){
//
return {
blocks:this.chain,
height:this.height,
currentNodeAddress:this.currentNodeAddress,
peers:this.peers,
pendingTransactions:this.pendingTransactions
};
}
Blockchain.prototype.addCoinbaseTranaction = function (){
console.log('coinbase transaction has been added');
let award = this.minerAward + this.fee;
let transaction = {
amount:award,
from:null,
to:this.currentNodeAddress,
fee:this.fee
}
this.addNewTransaction(transaction)
}
Blockchain.prototype.createCandidateBlock = function (previousBlockHeaderHash){
this.addCoinbaseTranaction();
const newBlock = {
header:{
version:this.version,
previousBlockHeaderHash,
timestamp:new Date().toUTCString(),
target:this.target,
merkleRoot:this.getMerkleRoot(this.pendingTransactions),
nonce:0,
},
transactions : this.pendingTransactions,
transactionCounter : this.pendingTransactions.length
}
this.pendingTransactions =[];
return newBlock;
}
Blockchain.prototype.getMerkleRoot = function (transactions) {
let hash = crypto.createHash('sha256');
let merkleRootHash=null;
if(transactions.length){
for (let transaction of transactions){
hash = hash.update(JSON.stringify(transaction));
}
merkleRootHash = hash.digest('hex');
}
return merkleRootHash;
}
Blockchain.prototype.hashBlockHeader = function (blockHeader) {
//
let hash = crypto.createHash('sha256');
hash = hash.update(JSON.stringify(blockHeader));
hash = hash.digest('hex');
return hash;
}
Blockchain.prototype.mineBlock = function (block) {
block = this.proofOfWork(block);
return block
}
Blockchain.prototype.proofOfWork = function (block) {
block.header.nonce=1;
let hash = this.hashBlockHeader(block.header)
while(hash.substring(0,this.target) !== '0'.repeat(this.target)){
block.header.nonce++;
hash = this.hashBlockHeader(block.header);
}
console.log(`mining finished and nonce is ${block.header.nonce}`)
return block;
}
Blockchain.prototype.doesBlockHasPow = function (block) {
let is_block_valid = true;
let blockHeader = block.header;
let hash = this.hashBlockHeader(blockHeader)
if(hash.substring(0,this.target) !== '0'.repeat(this.target)){
is_block_valid = false;
}
return is_block_valid;
}
module.exports= new Blockchain();