Skip to content

Commit

Permalink
Simplified driver example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Austin committed Jul 17, 2023
1 parent 76acce5 commit 766499e
Showing 1 changed file with 31 additions and 63 deletions.
94 changes: 31 additions & 63 deletions driver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

let Blockchain = require('./blockchain.js');

// FIXME: Figure out how to remove these imports
let Block = require('./block.js');
let Client = require('./client.js');
let Miner = require('./miner.js');
Expand All @@ -10,88 +12,54 @@ let FakeNet = require('./fake-net.js');

console.log("Starting simulation. This may take a moment...");


let fakeNet = new FakeNet();

// Clients
let alice = new Client({name: "Alice", net: fakeNet});
let bob = new Client({name: "Bob", net: fakeNet});
let charlie = new Client({name: "Charlie", net: fakeNet});

// Miners
let minnie = new Miner({name: "Minnie", net: fakeNet});
let mickey = new Miner({name: "Mickey", net: fakeNet});

// Creating genesis block
let genesis = Blockchain.makeGenesis({
let bc = Blockchain.createInstance({
blockClass: Block,
clientClass: Client,
minerClass: Miner,
transactionClass: Transaction,
clientBalanceMap: new Map([
[alice, 233],
[bob, 99],
[charlie, 67],
[minnie, 400],
[mickey, 300],
]),
clients: [
{name: 'Alice', amount: 233},
{name: 'Bob', amount: 99},
{name: 'Charlie', amount: 67},
{name: 'Minnie', amount: 400, mining: true},
{name: 'Mickey', amount: 300, mining: true},
],
net: new FakeNet(),
});

// Late miner - Donald has more mining power, represented by the miningRounds.
// (Mickey and Minnie have the default of 2000 rounds).
let donald = new Miner({name: "Donald", net: fakeNet, startingBlock: genesis, miningRounds: 3000});

function showBalances(client) {
console.log(`Alice has ${client.lastBlock.balanceOf(alice.address)} gold.`);
console.log(`Bob has ${client.lastBlock.balanceOf(bob.address)} gold.`);
console.log(`Charlie has ${client.lastBlock.balanceOf(charlie.address)} gold.`);
console.log(`Minnie has ${client.lastBlock.balanceOf(minnie.address)} gold.`);
console.log(`Mickey has ${client.lastBlock.balanceOf(mickey.address)} gold.`);
console.log(`Donald has ${client.lastBlock.balanceOf(donald.address)} gold.`);
}
// Get Alice and Bob
let [alice, bob] = bc.getClients('Alice', 'Bob');

// Showing the initial balances from Alice's perspective, for no particular reason.
console.log("Initial balances:");
showBalances(alice);
alice.showAllBalances();

fakeNet.register(alice, bob, charlie, minnie, mickey);

// Miners start mining.
minnie.initialize();
mickey.initialize();
// The miners will start mining blocks when start is called. After 5 seconds,
// the code will terminate and show the final balances from Alice's perspective.
bc.start(5000, () => {
console.log("Final balances, from Alice's perspective:");
alice.showAllBalances();
});

// Alice transfers some money to Bob.
console.log(`Alice is transferring 40 gold to ${bob.address}`);
alice.postTransaction([{ amount: 40, address: bob.address }]);

setTimeout(() => {
// Late miner - Donald has more mining power, represented by the miningRounds.
// (Mickey and Minnie have the default of 2000 rounds).
let donald = new Miner({
name: "Donald",
startingBlock: bc.genesis,
miningRounds: 3000,
});

console.log();
console.log("***Starting a late-to-the-party miner***");
console.log();
fakeNet.register(donald);
bc.register(donald);
donald.initialize();
}, 2000);

// Print out the final balances after it has been running for some time.
setTimeout(() => {
console.log();
console.log(`Minnie has a chain of length ${minnie.currentBlock.chainLength}:`);

console.log();
console.log(`Mickey has a chain of length ${mickey.currentBlock.chainLength}:`);

console.log();
console.log(`Donald has a chain of length ${donald.currentBlock.chainLength}:`);

console.log();
console.log("Final balances (Minnie's perspective):");
showBalances(minnie);

console.log();
console.log("Final balances (Alice's perspective):");
showBalances(alice);

console.log();
console.log("Final balances (Donald's perspective):");
showBalances(donald);

process.exit(0);
}, 5000);

0 comments on commit 766499e

Please sign in to comment.