From 766499ebe50b84af47d9573e4357fd79c919748d Mon Sep 17 00:00:00 2001 From: Tom Austin Date: Mon, 17 Jul 2023 15:53:01 -0700 Subject: [PATCH] Simplified driver example --- driver.js | 94 ++++++++++++++++++------------------------------------- 1 file changed, 31 insertions(+), 63 deletions(-) diff --git a/driver.js b/driver.js index 0fb6139..3bd6d37 100644 --- a/driver.js +++ b/driver.js @@ -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'); @@ -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);