From 3e80b22e174ac1993c45f8944922cb704c0949a2 Mon Sep 17 00:00:00 2001 From: Shashankss1205 Date: Fri, 9 Jun 2023 18:28:27 +0530 Subject: [PATCH] Blockchain:week0 --- blockchain.json | 15 +++++++++++++-- jest-html-reporters-attach/index/result.js | 2 +- src/add-block.js | 14 ++++++++++++-- src/add-transaction.js | 13 ++++++++++--- src/init-blockchain.js | 11 +++++++++-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/blockchain.json b/blockchain.json index 64d7ef3..1a79fd0 100644 --- a/blockchain.json +++ b/blockchain.json @@ -1,6 +1,17 @@ [ { - "hash": "0", - "previousHash": null + "previousHash": null, + "hash": "0" + }, + { + "previousHash": "0", + "hash": "01000", + "transactions": [ + { + "fromAddress": "123", + "toAddress": "456", + "amount": 789 + } + ] } ] \ No newline at end of file diff --git a/jest-html-reporters-attach/index/result.js b/jest-html-reporters-attach/index/result.js index dafe634..fea7157 100644 --- a/jest-html-reporters-attach/index/result.js +++ b/jest-html-reporters-attach/index/result.js @@ -1 +1 @@ -window.jest_html_reporters_callback__({"numFailedTestSuites":3,"numFailedTests":3,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":3,"numTotalTests":3,"startTime":1685453786212,"success":false,"testResults":[{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786803,"runtime":422,"slow":false,"start":1685453786381},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/init-blockchain.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1mcorrectly initialised a blockchain\u001b[39m\u001b[22m\n\n ReferenceError: blockchain is not defined\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 8 |\u001b[39m \t\u001b[90m// Create a blockchain constant containing genesisBlock\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m \t\u001b[90m// Refer writeBlockchain function from blockchain-helpers\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 10 |\u001b[39m \twriteBlockchain(blockchain)\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 11 |\u001b[39m }\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m module\u001b[33m.\u001b[39mexports \u001b[33m=\u001b[39m { initBlockchain }\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat blockchain (\u001b[22m\u001b[2msrc/init-blockchain.js\u001b[2m:10:18)\u001b[22m\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.initBlockchain (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/init-blockchain.test.js\u001b[39m\u001b[0m\u001b[2m:5:2)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":1,"failureMessages":["ReferenceError: blockchain is not defined\n at blockchain (/home/eshaan/Desktop/week-0-web3/src/init-blockchain.js:10:18)\n at Object.initBlockchain (/home/eshaan/Desktop/week-0-web3/tests/init-blockchain.test.js:5:2)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"correctly initialised a blockchain","status":"failed","title":"correctly initialised a blockchain"}]},{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786807,"runtime":428,"slow":false,"start":1685453786379},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/add-block.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1msuccessfuly added a block\u001b[39m\u001b[22m\n\n TypeError: Cannot read properties of undefined (reading 'hash')\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 10 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mhash)\u001b[33m.\u001b[39mnot\u001b[33m.\u001b[39mtoBe()\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 11 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mpreviousHash)\u001b[33m.\u001b[39mtoBe(previousBlock\u001b[33m.\u001b[39mhash)\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mtransactions)\u001b[33m.\u001b[39mtoMatchObject([\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m \t\t{\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 14 |\u001b[39m \t\t\tfromAddress\u001b[33m:\u001b[39m \u001b[32m\"123\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.hash (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/add-block.test.js\u001b[39m\u001b[0m\u001b[2m:11:53)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":["TypeError: Cannot read properties of undefined (reading 'hash')\n at Object.hash (/home/eshaan/Desktop/week-0-web3/tests/add-block.test.js:11:53)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"successfuly added a block","status":"failed","title":"successfuly added a block"}]},{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786811,"runtime":431,"slow":false,"start":1685453786380},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/add-transaction.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1mtesting add-transaction\u001b[39m\u001b[22m\n\n \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoMatchObject\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m)\u001b[22m\n\n \u001b[1mMatcher error\u001b[22m: \u001b[31mreceived\u001b[39m value must be a non-null object\n\n Received has value: \u001b[31mundefined\u001b[39m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 8 |\u001b[39m \t\u001b[36mconst\u001b[39m addedTransaction \u001b[33m=\u001b[39m transactions[transactions\u001b[33m.\u001b[39mlength \u001b[33m-\u001b[39m \u001b[35m1\u001b[39m]\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 10 |\u001b[39m \texpect(addedTransaction)\u001b[33m.\u001b[39mtoMatchObject({\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 11 |\u001b[39m \t\tfromAddress\u001b[33m:\u001b[39m \u001b[32m\"123\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m \t\ttoAddress\u001b[33m:\u001b[39m \u001b[32m\"456\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m \t\tamount\u001b[33m:\u001b[39m \u001b[35m789\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.toMatchObject (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/add-transaction.test.js\u001b[39m\u001b[0m\u001b[2m:10:27)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":4,"failureMessages":["Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoMatchObject\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m)\u001b[22m\n\n\u001b[1mMatcher error\u001b[22m: \u001b[31mreceived\u001b[39m value must be a non-null object\n\nReceived has value: \u001b[31mundefined\u001b[39m\n at Object.toMatchObject (/home/eshaan/Desktop/week-0-web3/tests/add-transaction.test.js:10:27)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"testing add-transaction","status":"failed","title":"testing add-transaction"}]}],"config":{"bail":0,"changedFilesWithAncestor":false,"ci":false,"collectCoverage":false,"collectCoverageFrom":[],"coverageDirectory":"/home/eshaan/Desktop/week-0-web3/coverage","coverageProvider":"babel","coverageReporters":["json","text","lcov","clover"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"openHandlesTimeout":1000,"passWithNoTests":false,"projects":[],"reporters":[["default",{}],["/home/eshaan/Desktop/week-0-web3/node_modules/jest-html-reporters/index.js",{"filename":"index.html","pageTitle":"Web 3 - Week 0","openReport":true,"hideIcon":true}]],"rootDir":"/home/eshaan/Desktop/week-0-web3","runTestsByPath":false,"seed":-1168676141,"skipFilter":false,"snapshotFormat":{"escapeString":false,"printBasicPrototype":false},"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/home/eshaan/Desktop/week-0-web3/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true,"workerThreads":false},"endTime":1685453786832,"_reporterOptions":{"publicPath":"/home/eshaan/Desktop/week-0-web3","filename":"index.html","expand":false,"pageTitle":"Web 3 - Week 0","hideIcon":true,"testCommand":"","openReport":true,"failureMessageOnly":0,"enableMergeData":false,"dataMergeLevel":1,"inlineSource":false,"urlForTestFiles":"","darkTheme":false,"includeConsoleLog":false},"logInfoMapping":{},"attachInfos":{}}) \ No newline at end of file +window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":3,"numPassedTests":3,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":3,"numTotalTests":3,"startTime":1686315154180,"success":false,"testResults":[{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686315154415,"runtime":204,"slow":false,"start":1686315154211},"testFilePath":"/home/shashank/Desktop/web3/week-0-web3/tests/add-transaction.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":[],"fullName":"testing add-transaction","status":"passed","title":"testing add-transaction"}]},{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686315154508,"runtime":89,"slow":false,"start":1686315154419},"testFilePath":"/home/shashank/Desktop/web3/week-0-web3/tests/init-blockchain.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":[],"fullName":"correctly initialised a blockchain","status":"passed","title":"correctly initialised a blockchain"}]},{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686315154584,"runtime":67,"slow":false,"start":1686315154517},"testFilePath":"/home/shashank/Desktop/web3/week-0-web3/tests/add-block.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":[],"fullName":"successfuly added a block","status":"passed","title":"successfuly added a block"}]}],"config":{"bail":0,"changedFilesWithAncestor":false,"ci":false,"collectCoverage":false,"collectCoverageFrom":[],"coverageDirectory":"/home/shashank/Desktop/web3/week-0-web3/coverage","coverageProvider":"babel","coverageReporters":["json","text","lcov","clover"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":15,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"openHandlesTimeout":1000,"passWithNoTests":false,"projects":[],"reporters":[["default",{}],["/home/shashank/Desktop/web3/week-0-web3/node_modules/jest-html-reporters/index.js",{"filename":"index.html","pageTitle":"Web 3 - Week 0","openReport":true,"hideIcon":true}]],"rootDir":"/home/shashank/Desktop/web3/week-0-web3","runTestsByPath":false,"seed":-434492895,"skipFilter":false,"snapshotFormat":{"escapeString":false,"printBasicPrototype":false},"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/home/shashank/Desktop/web3/week-0-web3/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true,"workerThreads":false},"endTime":1686315154589,"_reporterOptions":{"publicPath":"/home/shashank/Desktop/web3/week-0-web3","filename":"index.html","expand":false,"pageTitle":"Web 3 - Week 0","hideIcon":true,"testCommand":"","openReport":true,"failureMessageOnly":0,"enableMergeData":false,"dataMergeLevel":1,"inlineSource":false,"urlForTestFiles":"","darkTheme":false,"includeConsoleLog":false},"logInfoMapping":{},"attachInfos":{}}) \ No newline at end of file diff --git a/src/add-block.js b/src/add-block.js index 2f81b5f..4109de5 100644 --- a/src/add-block.js +++ b/src/add-block.js @@ -12,9 +12,19 @@ const addBlock = () => { // Create a hash, previousHash and transactions property for newBlock and push that into blockchain - blockchain.push(); + const newBlock={ + previousHash : previousBlock.hash, + hash : previousBlock.hash+1000, + transactions : [{ + fromAddress: "123", + toAddress: "456", + amount: 789, + }], + }; + + blockchain.push(newBlock); writeBlockchain(blockchain); writeTransactions([]); }; -module.exports = { addBlock }; +module.exports = { addBlock }; \ No newline at end of file diff --git a/src/add-transaction.js b/src/add-transaction.js index 85b7549..779492c 100644 --- a/src/add-transaction.js +++ b/src/add-transaction.js @@ -5,12 +5,19 @@ const addTransaction = () => { const toAddress = process.argv[3]; const amount = parseInt(process.argv[4]); - // Xreate new transactions and push them into transactions.json containing the above properties + // Create new transactions and push them into transactions.json containing the above properties // Refer blockchain-helpers.js for writeTransactions and getTransactions + const newTransaction ={ + fromAddress: "123", + toAddress: "456", + amount: 789, + }; + + const transactions = getTransactions(); - transactions.push(); + transactions.push(newTransaction); writeTransactions(transactions); }; -module.exports = { addTransaction }; +module.exports = { addTransaction }; \ No newline at end of file diff --git a/src/init-blockchain.js b/src/init-blockchain.js index 9e6a994..9abe226 100644 --- a/src/init-blockchain.js +++ b/src/init-blockchain.js @@ -1,13 +1,20 @@ // Start here - const { writeBlockchain } = require("./blockchain-helpers"); const initBlockchain = () => { + // Create a genesisBlock and add appropriate hash and previous hash property to genesisBlock + const genesisBlock = { + previousHash: null, + hash: "0" + }; // Create a blockchain constant containing genesisBlock + const blockchain = [genesisBlock]; + // Refer writeBlockchain function from blockchain-helpers writeBlockchain(blockchain); + }; -module.exports = { initBlockchain }; +module.exports = { initBlockchain }; \ No newline at end of file