-
Notifications
You must be signed in to change notification settings - Fork 2
/
oneminer.js
93 lines (73 loc) · 2.59 KB
/
oneminer.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
"use strict";
let Government = require('./Government.js').Government;
let Delegate = require('./Delegate.js');
let Client = require('./client.js');
let fakeNet = require('./fakeNet.js');
let utils = require('./utils.js');
// Clients
let alice = new Client(fakeNet.broadcast, 100, true, "123", "alice");
let bob = new Client(fakeNet.broadcast, 200, true, "456", "bob");
let charlie = new Client(fakeNet.broadcast, 400, true, "789", "charlie");
// Delegates
let del1 = new Delegate("del1", fakeNet.broadcast);
let del2 = new Delegate("del2", fakeNet.broadcast);
let del3 = new Delegate("del3", fakeNet.broadcast);
let del4 = new Delegate("del4", fakeNet.broadcast);
// Gov
let gov = new Government(fakeNet.broadcast,3);
gov.addDelegate(del1, del2, del3, del4);
// initial accounts
let accounts = {
"123" : 100,
"456": 200,
"789": 400,
"del1" : 100,
"del2" : 100,
"del3" : 100,
"del4" : 100,
"gov": 1000,
}
// assgin accounts information to each delegate.
del1.accounts = JSON.parse(JSON.stringify(accounts));
del2.accounts = JSON.parse(JSON.stringify(accounts));
del3.accounts = JSON.parse(JSON.stringify(accounts));
del4.accounts = JSON.parse(JSON.stringify(accounts));
gov.accounts = JSON.parse(JSON.stringify(accounts));
console.log("Starting simulation. This may take a moment...");
console.log();
console.log(`Initial balances to all accounts are \n`, accounts);
console.log();
console.log(`Alice has ${alice.balance} coins.`);
console.log(`Bob has ${bob.balance} coins.`);
console.log(`Charlie has ${charlie.balance} coins.`);
console.log();
fakeNet.register(alice, bob, charlie, del1, del2, del3, del4, gov);
console.log("-----MAKING A TRANSACTION-----");
console.log();
charlie.postTransaction(50, "123");
console.log();
console.log("-----BEGIN VOTING-----");
console.log();
gov.startVotingRoundes();
console.log("-----PRINTING UPDATED BALANCES-----");
console.log();
console.log(del1.accounts);
console.log("-----PRINTING BALANCES-----");
console.log();
console.log(`Alice now has ${alice.balance} coins.`);
console.log(`Bob now has ${bob.balance} coins.`);
console.log(`Charlie now has ${charlie.balance} coins.`);
console.log(`Del 1 now has ${accounts.del1} coins.`);
console.log(`Del 2 now has ${accounts.del2} coins.`);
console.log(`Del 3 now has ${accounts.del3} coins.`);
console.log();
console.log("-----MAKING ANOTHER TRANSACTION-----");
console.log();
alice.postTransaction(10, "456");
console.log();
console.log("-----BEGIN VOTING: SECOND ROUND-----");
console.log();
gov.startVotingRoundes();
console.log("-----PRINTING UPDATED BALANCES: PART 2-----");
console.log();
console.log(del1.accounts);