-
Notifications
You must be signed in to change notification settings - Fork 0
/
blokchain.js
51 lines (32 loc) · 977 Bytes
/
blokchain.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
'use strict';
const Block = require('../types/block');
const Chain = require('../types/chain');
const genesis = require('../assets/block');
async function main () {
let chain = new Chain();
chain.on('candidate', async function (block) {
console.log('chain received candidate block:', block);
let candidate = new Block(block);
let isValid = candidate.validate();
candidate.compute();
console.log('isValid:', isValid);
if (!isValid) {
return false;
}
await chain.append(candidate);
console.log('chain length:', chain['@data'].length);
if (chain['@data'].length < max) {
await chain.mine();
} else {
console.log('Chain filled!');
console.log('Chain:', chain);
}
});
let start = new Block(genesis);
await chain.append(start);
console.log('starting... chain:', chain['@id']);
console.log('mining...');
await chain.mine();
console.log('chain id:', chain['@id']);
}
main();