-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.js
149 lines (125 loc) · 4.32 KB
/
driver.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
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
const { SpartanDarkBlockchain } = require("./spartan-dark-blockchain.js");
const { SpartanDarkBlock } = require("./spartan-dark-block.js");
const { SpartanDarkClient } = require("./spartan-dark-client.js");
const SpartanDarkMiner = require("./spartan-dark-miner.js");
const { TranMint } = require("./spartan-dark-tran-mint.js");
const { TranPour } = require("./spartan-dark-tran-pour.js");
const { FakeNet } = require("spartan-gold");
console.log("Starting simulation. This may take a moment...\n");
let fakeNet = new FakeNet();
// Clients
let alice = new SpartanDarkClient({ name: "Alice", net: fakeNet });
let bob = new SpartanDarkClient({ name: "Bob", net: fakeNet });
let charlie = new SpartanDarkClient({ name: "Charlie", net: fakeNet });
// Miners
let minnie = new SpartanDarkMiner({ name: "Minnie", net: fakeNet });
let mickey = new SpartanDarkMiner({ name: "Mickey", net: fakeNet });
// Creating genesis block
let genesis = SpartanDarkBlockchain.makeGenesis({
blockClass: SpartanDarkBlock,
mintTransactionClass: TranMint,
pourTransactionClass: TranPour,
clientBalanceMap: new Map([
[alice, 233],
[bob, 99],
[charlie, 67],
[minnie, 400],
[mickey, 300],
]),
coinbaseAmount: 1,
});
function showBalances() {
console.log(`Alice has ${alice.confirmedBalance} coins.`);
console.log(`Bob has ${bob.confirmedBalance} coins.`);
console.log(`Charlie has ${charlie.confirmedBalance} coins.`);
console.log(`Minnie has ${minnie.confirmedBalance} coins.`);
console.log(`Mickey has ${mickey.confirmedBalance} coins.\n`);
}
// Showing the initial balances from Alice's perspective, for no particular reason.
console.log("Initial balances:");
showBalances();
fakeNet.register(alice, bob, charlie, minnie, mickey);
// Miners start mining.
minnie.initialize();
mickey.initialize();
//const start = Date.now();
//Mint new coins
//for (let i = 1; i <= 2; i++) {
console.log("Charlie is minting coin of value 2");
charlie.mint(2);
console.log("Alice is minting coin of value 2");
alice.mint(7);
console.log("Charlie is minting coin of value 9");
charlie.mint(9);
/**
* Balances now:
* alice: 7
* charlie: 11
*/
//console.log("Bob is minting coin of value 50");
//need to wait for sometime so that minting process begins so that the client finds some coins in their purse
setTimeout(() => {
charlie.spend(alice, 5);
}, 100);
/**
* alice: 7+5 = 12
* charlie: 11-5 = 6
*/
console.log("Bob is minting coin of value 50");
bob.mint(50);
/**
* alice: 12
* charlie: 6
* bob: 50
*/
setTimeout(() => {
bob.spend(alice, 20);
}, 100);
/**
* alice: 12 + 20 = 32
* charlie: 6
* bob: 50 - 20 = 30
*/
//FLAW: Alice cannot spend here coz they havem't yet received he amount sent previously by Bob and Charlie. So they do not have sufficient balance. this is happening coz javascript is single-threaded and so for alice to spend, they would need the previous transactions to have gotten completed
setTimeout(() => {
alice.spend(bob, 4);
}, 100);
// charlie.mint(2);
// setTimeout(() => {
// charlie.spend(alice, 5);
// }, 10000);
// console.log("Alice is minting coin of value 2");
// alice.mint(7);
// console.log("Charlie is minting coin of value 9");
// charlie.mint(9);
/**
* alice: 32 - 4 = 28
* charlie: 6
* bob: 30 + 4 = 34
*/
//}
//ASK: too large a timeout. Ask how to trigger after spend is done.
//FLAW: the timeout increases with the amount of spending added due to the time the proving method takes
setTimeout(() => {
console.log();
console.log(
`Minnie has a chain of length ${minnie.currentBlock.chainLength}`
);
console.log(
`Mickey has a chain of length ${mickey.currentBlock.chainLength}`
);
console.log("Final balances:");
console.log("Alice now has coins of total value: " + alice.getBalance());
//console.log("Alice wallet: ");
//console.log(alice.SpartanDarks);
console.log("Charlie now has coins of total value: " + charlie.getBalance());
//console.log("Charlie wallet: ");
//console.log(charlie.SpartanDarks);
console.log("Bob now has coins of total value: " + bob.getBalance());
//console.log("Bob wallet: ");
//console.log(bob.SpartanDarks);
//alice.getBalance().then((balance) => {console.log("Alice now has coins of total value: "+balance);});
//charlie.getBalance().then((balance) => {console.log("Charlie now has coins of total value: "+balance);});
process.exit(0);
}, 70000);