-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmine.js
More file actions
62 lines (46 loc) · 1.57 KB
/
mine.js
File metadata and controls
62 lines (46 loc) · 1.57 KB
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
const Block = require('./models/Block');
const Mempool = require('./models/Mempool');
const Transaction = require('./models/Transaction');
const UTXO = require('./models/UTXO');
const db = require('./db');
const {PUBLIC_KEY} = require('./config');
const TARGET_DIFFICULTY = BigInt("0x0" + "F".repeat(63));
const BLOCK_REWARD = 10;
let mining = true;
mine();
function startMining() {
mining = true;
mine();
}
function stopMining() {
mining = false;
}
function poolTransaction(txn) {
db.mempool.addPendingTransaction(new Transaction(txn[0], txn[1], txn[2]));
//console.log(db.mempool.pendingTransactions);
}
function mine() {
if(!mining) return;
const block = new Block(db.blockchain.blocks[db.blockchain.blocks.length-1].blockNumber+1);
// TODO: add transactions from the mempool
//const coinbaseUTXO = new UTXO(PUBLIC_KEY, BLOCK_REWARD);
const coinbaseTX = new Transaction("", PUBLIC_KEY, BLOCK_REWARD);
block.addTransaction(coinbaseTX);
block.addTransactions(db.mempool.removePendingTxns());
block.previousHash = db.blockchain.getLatestBlock().currentHash;
// proof of work
while(BigInt('0x' + block.hash()) >= TARGET_DIFFICULTY) {
block.nonce++;
}
block.difficulty = TARGET_DIFFICULTY;
block.currentHash = block.hash();
db.blockchain.addBlock(block);
console.log(`Mined block #${db.blockchain.blockHeight()} with a hash of ${block.hash()} at nonce ${block.nonce}`);
console.log(db.blockchain.blocks[db.blockchain.blocks.length-1].transactions);
setTimeout(mine, 10000);
}
module.exports = {
startMining,
stopMining,
poolTransaction,
};