forked from flowchain/blockchain-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (48 loc) · 1.52 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
var crypto = require('crypto');
var server = require('./server');
var Miner = require('./libs/mining');
var Transaction = require('./libs/utxo');
// Import genesis block
var block = require('./libs/genesis');
// Create a new miner and start to mine
var miner = new Miner();
var utxo = new Transaction();
var onmessage = function(payload) {
};
var onstart = function(node) {
console.log('----- Genesis Block -----');
console.log( JSON.stringify(block) );
console.log('----- Start mining -----');
//miner.setTransactions(['a', 'b', 'c']);
miner.setPreviousBlock(block);
utxo.setCoinBase('axEj265EB');
// Start to generate a hash
setInterval(function() {
miner.generateHash();
// A success hash is generated
if (miner.isSuccess()) {
block = miner.getNewBlock();
miner.setPreviousBlock(block);
utxo.setCoinBaseTx();
// Include non-coinbase transactions
utxo.txout(5, 'ytQw782VN');
utxo.txout(1, 'ytQw782VN');
utxo.txout(2, 'ytQw782VN');
utxo.txout(8, 'ytQw782VN');
miner.setTransactions( utxo.getTxs() );
utxo.dump();
miner.dumpMerkleTree();
console.log('Difficulty: ' + block.difficulty)
console.log('Block #' + block.no + ': ' + block.hash);
console.log(block);
console.log('\n\n\n\n');
}
}, 100);
};
/**
* Create a mining node.
*/
server.start({
onstart: onstart,
onmessage: onmessage,
});