From 9865969ee242725546648dc887e3c0fb1fa887f2 Mon Sep 17 00:00:00 2001 From: Kevin Ho Date: Thu, 26 Sep 2019 16:47:48 -0700 Subject: [PATCH 1/3] tweaks to try to fix 2.1, 3.1 not syncing --- ch3/3.1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch3/3.1/README.md b/ch3/3.1/README.md index f024b9db..4a3ee363 100644 --- a/ch3/3.1/README.md +++ b/ch3/3.1/README.md @@ -121,4 +121,4 @@ To test your PoWClient, go into the `test` folder and run `node PoWAllMinerTest.
-> As always, if you have questions or get stuck please hit up the community on the [forum!](https://forum.cryptoeconomics.study) +> As always, if you have questions or get stuck please hit up the community on the [forum!](https://forum.cryptoeconomics.study) \ No newline at end of file From fd2d5edcacf4c1749b41a34958465192900daa5d Mon Sep 17 00:00:00 2001 From: Kevin Ho Date: Tue, 1 Oct 2019 09:41:57 -0700 Subject: [PATCH 2/3] updated links, dev env docs --- ch3/3.1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch3/3.1/README.md b/ch3/3.1/README.md index 4a3ee363..f024b9db 100644 --- a/ch3/3.1/README.md +++ b/ch3/3.1/README.md @@ -121,4 +121,4 @@ To test your PoWClient, go into the `test` folder and run `node PoWAllMinerTest.
-> As always, if you have questions or get stuck please hit up the community on the [forum!](https://forum.cryptoeconomics.study) \ No newline at end of file +> As always, if you have questions or get stuck please hit up the community on the [forum!](https://forum.cryptoeconomics.study) From 421e5187fb294ef6710079d75c25b5d506cb85df Mon Sep 17 00:00:00 2001 From: Santiago Gonzalez Toral Date: Sun, 13 Oct 2019 18:04:39 -0500 Subject: [PATCH 3/3] eslint + fix missing bracket --- ch1/1.5/paypal.js | 310 +++++++++++++++++++++++----------------------- 1 file changed, 155 insertions(+), 155 deletions(-) diff --git a/ch1/1.5/paypal.js b/ch1/1.5/paypal.js index 436573fc..de54492c 100644 --- a/ch1/1.5/paypal.js +++ b/ch1/1.5/paypal.js @@ -1,48 +1,49 @@ -const EthCrypto = require("eth-crypto"); -const Client = require("./Client.js"); +const EthCrypto = require('eth-crypto'); +const Client = require('./Client.js'); // Our naive implementation of a centralized payment processor class Paypal extends Client { - // initialize Paypal's state + // initialize Paypal's state constructor(genesis) { super(); // the state of the network (accounts and balances) this.state = { - // Paypal's address + // Paypal's address [this.wallet.address]: { - // Paypal's initial balance - balance: 1000000, - // Paypal's initial nonce - nonce: 0 - } + // Paypal's initial balance + balance: 1000000, + // Paypal's initial nonce + nonce: 0, + }, }; - // pending transaction pool - this.pendingTx = []; + // pending transaction pool + this.pendingTx = []; // the history of transactions this.txHistory = []; - // the network fee - // TODO - // blacklist of accounts to be censored - // TODO + // the network fee + // TODO + this.networkFee = 1; + // blacklist of accounts to be censored + // TODO } - // Removes funds from user accounts and adds them to Paypal's balance - // - In reality, it would be far easier for Paypal to mint themselves extra cash so they could look like a legit operation on the outside, but really just print money for themselves whenever they wanted. Then they would never be accused of stealing, but they could steal as much as they wanted. Yay magic internet money! + // Removes funds from user accounts and adds them to Paypal's balance + // - In reality, it would be far easier for Paypal to mint themselves extra cash so they could look like a legit operation on the outside, but really just print money for themselves whenever they wanted. Then they would never be accused of stealing, but they could steal as much as they wanted. Yay magic internet money! stealAllFunds() { - // sums up all the value in the network - // TODO - // removes everyone's balance - // TODO - // adds that value to Paypal's wallet - // TODO + // sums up all the value in the network + // TODO + // removes everyone's balance + // TODO + // adds that value to Paypal's wallet + // TODO } - // Mints funds without adding the transaction to the history - // - In reality, this would not work as corporations have to get audited for tax purposes and whatnot, so they'd have to call this something clever like a network upgrade to invest in R&D or a "restructuring". In either case, there would be a glossy marketing campaign put together to make you feel good about it :) - mintSecretFunds(amount) { - // it's literally just 1 line of code... - // TODO - } + // Mints funds without adding the transaction to the history + // - In reality, this would not work as corporations have to get audited for tax purposes and whatnot, so they'd have to call this something clever like a network upgrade to invest in R&D or a "restructuring". In either case, there would be a glossy marketing campaign put together to make you feel good about it :) + mintSecretFunds(amount) { + // it's literally just 1 line of code... + // TODO + } // Checks that the sender of a transaction is the same as the signer checkTxSignature(tx) { @@ -50,71 +51,69 @@ class Paypal extends Client { const sig = this.verify(tx.sig, this.hash(tx.contents), tx.contents.from); // return an error if the signature is invalid if (!sig) { - console.log("Invalid Signature"); + console.log('Invalid Signature'); return false; // return true if the transaction is valid - } else { - return true; } + return true; } // Check if the user's address is on a blacklist. If not, check is the user's address is already in the state, and if not, add the user's address to the state checkUserAddress(tx) { - // check if the sender or receiver are on the blacklist - // TODO - // if the sender or receiver are banned, return false - // TODO + // check if the sender or receiver are on the blacklist + // TODO + // if the sender or receiver are banned, return false + // TODO // check if the sender is in the state if (!(tx.contents.to in this.state)) { - // if the sender is not in the state, add their address and initialize an empty balance and nonce of 0 + // if the sender is not in the state, add their address and initialize an empty balance and nonce of 0 this.state[tx.contents.to] = { - balance: 0, - nonce: 0 + balance: 0, + nonce: 0, }; } // check if the receiver is in the state if (!(tx.contents.from in this.state)) { - // if the receiver is not in the state, add their address and initialize an empty balance and nonce of 0 + // if the receiver is not in the state, add their address and initialize an empty balance and nonce of 0 this.state[tx.contents.from] = { - balance: 0, - nonce: 0 + balance: 0, + nonce: 0, }; } // if the checks on both accounts pass (they're both in the state), return true return true; } - // Check that the transaction nonce matches the nonce that Paypal has for the sender's account - // note: we first have to make sure that the account is in Paypal's state before we can check it's nonce - checkTxNonce(tx) { - // if the transaction nonce is greater than the nonce Paypal has for that account + // Check that the transaction nonce matches the nonce that Paypal has for the sender's account + // note: we first have to make sure that the account is in Paypal's state before we can check it's nonce + checkTxNonce(tx) { + // if the transaction nonce is greater than the nonce Paypal has for that account if (tx.contents.nonce > this.state[tx.contents.from].nonce) { - // and if the transaction is not already in the pendingTX pool + // and if the transaction is not already in the pendingTX pool if (!(tx.contents.from in this.pendingTx)) { - // add the transaction to the pendingTx pool - this.pendingTx.push(tx) + // add the transaction to the pendingTx pool + this.pendingTx.push(tx); } - // return false to signal that nothing more needs to be done and the transaction does not need to be processed - return false - } - // if the transaction nonce is the same as the nonce Paypal has for that account - if (tx.contents.nonce === this.state[tx.contents.from].nonce) { - // return true to signal that it is ok to proceed to the next operation - return true - } - // if the transaction nonce is less than the nonce Paypal has for that account - if (tx.contents.nonce < this.state[tx.contents.from].nonce) { - // return false to signal that the transaction is invalid and the transaction should not be processed - return false + // return false to signal that nothing more needs to be done and the transaction does not need to be processed + return false; } - - } + // if the transaction nonce is the same as the nonce Paypal has for that account + if (tx.contents.nonce === this.state[tx.contents.from].nonce) { + // return true to signal that it is ok to proceed to the next operation + return true; + } + // if the transaction nonce is less than the nonce Paypal has for that account + if (tx.contents.nonce < this.state[tx.contents.from].nonce) { + // return false to signal that the transaction is invalid and the transaction should not be processed + return false; + } + } // Check that the transaction is valid based on the type checkTxType(tx) { // if mint - if (tx.contents.type === "mint") { - // check that the sender is PayPal + if (tx.contents.type === 'mint') { + // check that the sender is PayPal if (tx.contents.from !== this.wallet.address) { // if a check fails, return an error stating why console.log("Non-Paypal Clients can't mint!"); @@ -124,78 +123,79 @@ class Paypal extends Client { return true; } // if check - if (tx.contents.type === "check") { + if (tx.contents.type === 'check') { const user = tx.contents.from; console.log(`Your balance is: ${this.state[user].balance}`); // return false so that Paypal does not process the tx return false; } // if send - if (tx.contents.type === "send") { - // check that the transaction amount is positive and the sender has an account balance greater than or equal to the transaction amount + if (tx.contents.type === 'send') { + // check that the transaction amount is positive and the sender has an account balance greater than or equal to the transaction amount if (this.state[tx.contents.from].balance - tx.contents.amount < 0) { // if a check fails, print an error to the console stating why and return false - console.log("Not enough money!"); + console.log('Not enough money!'); return false; } // if a check passes, return true return true; } - // if cancel - if (tx.contents.type === 'cancel') { - // get the nonce of the transaction to be cancelled - const cancelNonce = tx.contents.amount; - // check pending transactions - for (let i in this.pendingTx) { - const pendingTx = this.pendingTx[i] - // if the nonce of the transaction to be cancelled matches a pending transaction - if (pendingTx.contents.nonce === cancelNonce) { - // make sure that the user who is cancelling the transaction is the same user who signed the transaction to be cancelled - if (pendingTx.contents.sender === tx.contents.sender) { - // delete the old transaction - delete this.state.pendingTx[pendingTx] - } - } - } - // check already processed transactions - for (let i in this.txHistory) { - const oldTx = this.txHistory[i] - // if the nonce matches delete the transaction and reverse the old transaction - if (oldTx.contents.nonce === cancelNonce) { - // make sure that the user who is cancelling the transaction is the same user who signed the transaction to be cancelled - if (oldTx.contents.from === tx.contents.from) { - // take money back from the receiver - this.state[oldTx.contents.to].balance -= oldTx.contents.amount - // give the sender back their money - this.state[oldTx.contents.from].balance += oldTx.contents.amount - } - } - } - // add the cancellation transaction to the txHistory - this.txHistory.push(tx) - // charge a fee to the user for cancelling a transaction - this.state[tx.contents.from].balance -= 1; - // add that fee to Paypal's wallet balance - const paypalWallet = this.wallet.address - this.state[paypalWallet].balance += 1; - } + // if cancel + if (tx.contents.type === 'cancel') { + // get the nonce of the transaction to be cancelled + const cancelNonce = tx.contents.amount; + // check pending transactions + for (const i in this.pendingTx) { + const pendingTx = this.pendingTx[i]; + // if the nonce of the transaction to be cancelled matches a pending transaction + if (pendingTx.contents.nonce === cancelNonce) { + // make sure that the user who is cancelling the transaction is the same user who signed the transaction to be cancelled + if (pendingTx.contents.sender === tx.contents.sender) { + // delete the old transaction + delete this.state.pendingTx[pendingTx]; + } + } + } + // check already processed transactions + for (const i in this.txHistory) { + const oldTx = this.txHistory[i]; + // if the nonce matches delete the transaction and reverse the old transaction + if (oldTx.contents.nonce === cancelNonce) { + // make sure that the user who is cancelling the transaction is the same user who signed the transaction to be cancelled + if (oldTx.contents.from === tx.contents.from) { + // take money back from the receiver + this.state[oldTx.contents.to].balance -= oldTx.contents.amount; + // give the sender back their money + this.state[oldTx.contents.from].balance += oldTx.contents.amount; + } + } + } + // add the cancellation transaction to the txHistory + this.txHistory.push(tx); + // charge a fee to the user for cancelling a transaction + this.state[tx.contents.from].balance -= 1; + // add that fee to Paypal's wallet balance + const paypalWallet = this.wallet.address; + this.state[paypalWallet].balance += 1; + } } // Checks if a transaction is valid checkTx(tx) { - // check that the signature is valid - if (this.checkTxSignature(tx)) { + // check that the signature is valid + if (this.checkTxSignature(tx)) { // check that the sender and receiver are in the state if (this.checkUserAddress(tx)) { // check that the type is valid if (this.checkTxType(tx)) { - // check that the nonce is valid - if (this.checkTxNonce(tx)) { - // if all checks pass return true - return true; - } + // check that the nonce is valid + if (this.checkTxNonce(tx)) { + // if all checks pass return true + return true; + } } } + } // if any checks fail return false return false; } @@ -206,57 +206,57 @@ class Paypal extends Client { this.state[tx.contents.from].balance -= tx.contents.amount; // then increase the balance of the transaction receiver this.state[tx.contents.to].balance += tx.contents.amount; - // then increment the nonce of the transaction sender - this.state[tx.contents.from].nonce += 1; + // then increment the nonce of the transaction sender + this.state[tx.contents.from].nonce += 1; // then add the transaction to the transaction history this.txHistory.push(tx); // return true once the transaction is processed return true; } - // Processes pending TX + // Processes pending TX processPendingTx() { - // for every transaction in the pendingTx pool - for (let tx in this.pendingTx) { - // get the sender address - let sender = this.pendingTx[tx].contents.from - // get the nonce Paypal has for that account - let paypalSenderNonce = this.state[sender].nonce - // get the nonce on the transaction - let txNonce = this.pendingTx[tx].contents.nonce - // if the nonce Paypal has for an account matches the nonce that is on the transaction - if (paypalSenderNonce === txNonce) { - // copy the pending transaction into memory - let txToProcess = this.pendingTx[tx] - // delete the transaction from the pendingTx pool - delete this.pendingTx[tx]; - // process the transaction - this.processTx(txToProcess); - // note: it is important to delete the transaction form the pendingTx pool BEFORE processing the transaction, otherwise we could get stuck in an infinite loop processing transactions and never deleting them - } - } + // for every transaction in the pendingTx pool + for (const tx in this.pendingTx) { + // get the sender address + const sender = this.pendingTx[tx].contents.from; + // get the nonce Paypal has for that account + const paypalSenderNonce = this.state[sender].nonce; + // get the nonce on the transaction + const txNonce = this.pendingTx[tx].contents.nonce; + // if the nonce Paypal has for an account matches the nonce that is on the transaction + if (paypalSenderNonce === txNonce) { + // copy the pending transaction into memory + const txToProcess = this.pendingTx[tx]; + // delete the transaction from the pendingTx pool + delete this.pendingTx[tx]; + // process the transaction + this.processTx(txToProcess); + // note: it is important to delete the transaction form the pendingTx pool BEFORE processing the transaction, otherwise we could get stuck in an infinite loop processing transactions and never deleting them + } + } } - // Charges a fee to use the network - chargeFee(tx) { - // removes the network fee from the sender's balance - // TODO - // adds the network fee to payment operator's balance - // TODO - } + // Charges a fee to use the network + chargeFee(tx) { + // removes the network fee from the sender's balance + // TODO + // adds the network fee to payment operator's balance + // TODO + } - // Checks if a transaction is valid, then processes it, then checks if there are any valid transactions in the pending transaction pool and processes those too - processTx(tx) { - // charge a fee to use the network - // TODO - // check the transaction is valid - if (this.checkTx(tx)) { - // apply the transaction to Paypal's state - this.applyTransaction(tx); - // check if any pending transactions are now valid, and if so process them too - this.processPendingTx() - } - } + // Checks if a transaction is valid, then processes it, then checks if there are any valid transactions in the pending transaction pool and processes those too + processTx(tx) { + // charge a fee to use the network + // TODO + // check the transaction is valid + if (this.checkTx(tx)) { + // apply the transaction to Paypal's state + this.applyTransaction(tx); + // check if any pending transactions are now valid, and if so process them too + this.processPendingTx(); + } + } } // export the Paypal module so that other files can use it