-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (110 loc) · 3.09 KB
/
index.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
const SHA256 = require('crypto-js/sha256');
class Blockchain {
constructor({ difficulty, genesisTimestamp = Date.now() }) {
this.chain = [this.createGenesisBlock(genesisTimestamp)];
this.difficulty = difficulty;
this.pendingTransactions = [];
this.miningReward = 5;
this.changeDifficultyOnMultiple = 100;
}
get lastBlock() {
return this.chain[this.chain.length - 1];
}
handleDifficultyChange() {
console.log('Difficulty increased');
this.difficulty += 1;
this.miningReward -= 1;
}
createGenesisBlock(genesisTimestamp) {
return new Block({
timestamp: genesisTimestamp,
transactions: [],
previousHash: '0',
});
}
isValidChain() {
for (let index = 1; index < this.chain.length; index++) {
const currentBlock = this.chain[index];
const previousBlock = this.chain[index - 1];
if (currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
if (currentBlock.previousHash !== previousBlock.hash) {
return false;
}
return true;
}
}
createTransaction(transaction) {
this.pendingTransactions.push(transaction);
}
minePendingTransactions(miningRewardAddress) {
let block = new Block({
timestamp: Date.now(),
transactions: this.pendingTransactions,
previousHash: this.lastBlock.hash,
});
block.mine(this.difficulty);
console.log('Block successfully mined!');
this.chain.push(block);
if (this.chain.length % this.changeDifficultyOnMultiple === 0) {
this.handleDifficultyChange();
}
this.pendingTransactions = [
new Transaction({
fromAddress: null,
toAddress: miningRewardAddress,
amount: this.miningReward,
}),
];
}
getBalanceForAddress(address) {
return this.chain.reduce((balance, block) => {
block.transactions.forEach(transaction => {
if (transaction.fromAddress === address) {
balance -= transaction.amount;
}
if (transaction.toAddress === address) {
balance += transaction.amount;
}
});
return balance;
}, 0);
}
}
class Block {
constructor({ previousHash = '', timestamp, transactions }) {
this.previousHash = previousHash;
this.timestamp = timestamp;
this.transactions = transactions;
this.hash = this.calculateHash();
this.nonce = 0;
}
calculateHash() {
return SHA256(
`${this.previousHash}${this.timestamp}${JSON.stringify(this.transactions)}${this.nonce}`
).toString();
}
mine(difficulty) {
while (
this.hash.substring(0, difficulty) !== Array(difficulty + 1).join('0')
) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log('Block mined');
}
}
class Transaction {
constructor({ fromAddress, toAddress, amount }) {
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.amount = amount;
}
}
const myCoin = new Blockchain({ difficulty: 1 });
[...Array(300)].forEach(() => {
myCoin.minePendingTransactions('abc');
});
console.log(myCoin);
console.log(myCoin.isValidChain());