From 9130d342a6dfd8ef867bc7a493ee8890c362336d Mon Sep 17 00:00:00 2001 From: tonititi Date: Wed, 24 Apr 2024 12:39:49 +0700 Subject: [PATCH 1/6] GREEN-42 add gasUsed and transaction root for returned block data --- package-lock.json | 290 +++++++++++++++++++++++++++++++++++++- package.json | 1 + src/external/Collector.ts | 29 +++- 3 files changed, 316 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index e44a6326..839b1bee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "got": "11.8.6", "jayson": "3.6.5", "jsonwebtoken": "9.0.0", + "merkle-patricia-tree": "^4.2.4", "pm2": "5.3.0", "ts-node": "10.9.1", "typescript": "4.5.5", @@ -711,6 +712,11 @@ "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==" }, + "node_modules/@types/abstract-leveldown": { + "version": "7.2.5", + "resolved": "https://registry.npmjs.org/@types/abstract-leveldown/-/abstract-leveldown-7.2.5.tgz", + "integrity": "sha512-/2B0nQF4UdupuxeKTJA2+Rj1D+uDemo6P4kMwKCpbfpnzeVaWSELTsAw4Lxn3VJD6APtRrZOCuYo+4nHUQfTfg==" + }, "node_modules/@types/better-sqlite3": { "version": "7.5.0", "resolved": "https://registry.npmjs.org/@types/better-sqlite3/-/better-sqlite3-7.5.0.tgz", @@ -837,6 +843,21 @@ "@types/node": "*" } }, + "node_modules/@types/level-errors": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/level-errors/-/level-errors-3.0.2.tgz", + "integrity": "sha512-gyZHbcQ2X5hNXf/9KS2qGEmgDe9EN2WDM3rJ5Ele467C0nA1sLhtmv1bZiPMDYfAYCfPWft0uQIaTvXbASSTRA==" + }, + "node_modules/@types/levelup": { + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/@types/levelup/-/levelup-4.3.3.tgz", + "integrity": "sha512-K+OTIjJcZHVlZQN1HmU64VtrC0jC3dXWQozuEIR9zVvltIk90zaGPM2AgT+fIkChpzHhFE3YnvFLCbLtzAmexA==", + "dependencies": { + "@types/abstract-leveldown": "*", + "@types/level-errors": "*", + "@types/node": "*" + } + }, "node_modules/@types/lodash": { "version": "4.14.195", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.195.tgz", @@ -1382,6 +1403,21 @@ "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", "dev": true }, + "node_modules/abstract-leveldown": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-6.3.0.tgz", + "integrity": "sha512-TU5nlYgta8YrBMNpc9FwQzRbiXsj49gsALsXadbGHt9CROPzX5fB0rWDR5mtdpOOKa5XqRFpbj1QroPAoPzVjQ==", + "dependencies": { + "buffer": "^5.5.0", + "immediate": "^3.2.3", + "level-concat-iterator": "~2.0.0", + "level-supports": "~1.0.0", + "xtend": "~4.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -2398,6 +2434,33 @@ "node": ">=10" } }, + "node_modules/deferred-leveldown": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/deferred-leveldown/-/deferred-leveldown-5.3.0.tgz", + "integrity": "sha512-a59VOT+oDy7vtAbLRCZwWgxu2BaCfd5Hk7wxJd48ei7I+nsg8Orlb9CLG0PMZienk9BSUKgeAqkO2+Lw+1+Ukw==", + "dependencies": { + "abstract-leveldown": "~6.2.1", + "inherits": "^2.0.3" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/deferred-leveldown/node_modules/abstract-leveldown": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-6.2.3.tgz", + "integrity": "sha512-BsLm5vFMRUrrLeCcRc+G0t2qOaTzpoJQLOubq2XM72eNpjF5UdU5o/5NvlNhx95XHcAvcl8OMXr4mlg/fRgUXQ==", + "dependencies": { + "buffer": "^5.5.0", + "immediate": "^3.2.3", + "level-concat-iterator": "~2.0.0", + "level-supports": "~1.0.0", + "xtend": "~4.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/define-data-property": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", @@ -2550,6 +2613,20 @@ "node": ">= 0.8" } }, + "node_modules/encoding-down": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/encoding-down/-/encoding-down-6.3.0.tgz", + "integrity": "sha512-QKrV0iKR6MZVJV08QY0wp1e7vF6QbhnbQhb07bwpEyuz4uZiZgPlEGdkCROuFkUwdxlFaiPIhjyarH1ee/3vhw==", + "dependencies": { + "abstract-leveldown": "^6.2.1", + "inherits": "^2.0.3", + "level-codec": "^9.0.0", + "level-errors": "^2.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -2569,6 +2646,17 @@ "node": ">=8.6" } }, + "node_modules/errno": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.8.tgz", + "integrity": "sha512-dJ6oBr5SQ1VSd9qkk7ByRgb/1SH4JZjCHSW/mr63/QcXO9zLVxvJ6Oy13nio03rxpSnVDDjFor75SjVeZWPW/A==", + "dependencies": { + "prr": "~1.0.1" + }, + "bin": { + "errno": "cli.js" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -4138,6 +4226,11 @@ "integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==", "dev": true }, + "node_modules/immediate": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.3.0.tgz", + "integrity": "sha512-HR7EVodfFUdQCTIeySw+WDRFJlPcLOJbXfwwZ7Oom6tjsvZ3bOkCDJHehQC3nxJrv7+f9XecwazynjU8e4Vw3Q==" + }, "node_modules/import-fresh": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz", @@ -4605,6 +4698,112 @@ "node": ">=0.2.0" } }, + "node_modules/level-codec": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/level-codec/-/level-codec-9.0.2.tgz", + "integrity": "sha512-UyIwNb1lJBChJnGfjmO0OR+ezh2iVu1Kas3nvBS/BzGnx79dv6g7unpKIDNPMhfdTEGoc7mC8uAu51XEtX+FHQ==", + "dependencies": { + "buffer": "^5.6.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-concat-iterator": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/level-concat-iterator/-/level-concat-iterator-2.0.1.tgz", + "integrity": "sha512-OTKKOqeav2QWcERMJR7IS9CUo1sHnke2C0gkSmcR7QuEtFNLLzHQAvnMw8ykvEcv0Qtkg0p7FOwP1v9e5Smdcw==", + "engines": { + "node": ">=6" + } + }, + "node_modules/level-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/level-errors/-/level-errors-2.0.1.tgz", + "integrity": "sha512-UVprBJXite4gPS+3VznfgDSU8PTRuVX0NXwoWW50KLxd2yw4Y1t2JUR5In1itQnudZqRMT9DlAM3Q//9NCjCFw==", + "dependencies": { + "errno": "~0.1.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-iterator-stream": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/level-iterator-stream/-/level-iterator-stream-4.0.2.tgz", + "integrity": "sha512-ZSthfEqzGSOMWoUGhTXdX9jv26d32XJuHz/5YnuHZzH6wldfWMOVwI9TBtKcya4BKTyTt3XVA0A3cF3q5CY30Q==", + "dependencies": { + "inherits": "^2.0.4", + "readable-stream": "^3.4.0", + "xtend": "^4.0.2" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-mem": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/level-mem/-/level-mem-5.0.1.tgz", + "integrity": "sha512-qd+qUJHXsGSFoHTziptAKXoLX87QjR7v2KMbqncDXPxQuCdsQlzmyX+gwrEHhlzn08vkf8TyipYyMmiC6Gobzg==", + "dependencies": { + "level-packager": "^5.0.3", + "memdown": "^5.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-packager": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/level-packager/-/level-packager-5.1.1.tgz", + "integrity": "sha512-HMwMaQPlTC1IlcwT3+swhqf/NUO+ZhXVz6TY1zZIIZlIR0YSn8GtAAWmIvKjNY16ZkEg/JcpAuQskxsXqC0yOQ==", + "dependencies": { + "encoding-down": "^6.3.0", + "levelup": "^4.3.2" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-supports": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/level-supports/-/level-supports-1.0.1.tgz", + "integrity": "sha512-rXM7GYnW8gsl1vedTJIbzOrRv85c/2uCMpiiCzO2fndd06U/kUXEEU9evYn4zFggBOg36IsBW8LzqIpETwwQzg==", + "dependencies": { + "xtend": "^4.0.2" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/level-ws": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/level-ws/-/level-ws-2.0.0.tgz", + "integrity": "sha512-1iv7VXx0G9ec1isqQZ7y5LmoZo/ewAsyDHNA8EFDW5hqH2Kqovm33nSFkSdnLLAK+I5FlT+lo5Cw9itGe+CpQA==", + "dependencies": { + "inherits": "^2.0.3", + "readable-stream": "^3.1.0", + "xtend": "^4.0.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/levelup": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/levelup/-/levelup-4.4.0.tgz", + "integrity": "sha512-94++VFO3qN95cM/d6eBXvd894oJE0w3cInq9USsyQzzoJxmiYzPAocNcuGCPGGjoXqDVJcr3C1jzt1TSjyaiLQ==", + "dependencies": { + "deferred-leveldown": "~5.3.0", + "level-errors": "~2.0.0", + "level-iterator-stream": "~4.0.0", + "level-supports": "~1.0.0", + "xtend": "~4.0.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -4675,6 +4874,11 @@ "node": ">=10" } }, + "node_modules/ltgt": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ltgt/-/ltgt-2.2.1.tgz", + "integrity": "sha512-AI2r85+4MquTw9ZYqabu4nMwy9Oftlfa/e/52t9IjtfG+mGBbTNdAoZ3RQKLHR6r0wQnwZnPIEh/Ya6XTWAKNA==" + }, "node_modules/make-error": { "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", @@ -4709,6 +4913,42 @@ "node": ">= 0.6" } }, + "node_modules/memdown": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/memdown/-/memdown-5.1.0.tgz", + "integrity": "sha512-B3J+UizMRAlEArDjWHTMmadet+UKwHd3UjMgGBkZcKAxAYVPS9o0Yeiha4qvz7iGiL2Sb3igUft6p7nbFWctpw==", + "dependencies": { + "abstract-leveldown": "~6.2.1", + "functional-red-black-tree": "~1.0.1", + "immediate": "~3.2.3", + "inherits": "~2.0.1", + "ltgt": "~2.2.0", + "safe-buffer": "~5.2.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/memdown/node_modules/abstract-leveldown": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-6.2.3.tgz", + "integrity": "sha512-BsLm5vFMRUrrLeCcRc+G0t2qOaTzpoJQLOubq2XM72eNpjF5UdU5o/5NvlNhx95XHcAvcl8OMXr4mlg/fRgUXQ==", + "dependencies": { + "buffer": "^5.5.0", + "immediate": "^3.2.3", + "level-concat-iterator": "~2.0.0", + "level-supports": "~1.0.0", + "xtend": "~4.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/memdown/node_modules/immediate": { + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.2.3.tgz", + "integrity": "sha512-RrGCXRm/fRVqMIhqXrGEX9rRADavPiDFSoMb/k64i9XMk8uH4r/Omi5Ctierj6XzNecwDbO4WuFbDD1zmpl3Tg==" + }, "node_modules/meow": { "version": "9.0.0", "resolved": "https://registry.npmjs.org/meow/-/meow-9.0.0.tgz", @@ -4763,6 +5003,34 @@ "node": ">= 8" } }, + "node_modules/merkle-patricia-tree": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/merkle-patricia-tree/-/merkle-patricia-tree-4.2.4.tgz", + "integrity": "sha512-eHbf/BG6eGNsqqfbLED9rIqbsF4+sykEaBn6OLNs71tjclbMcMOk1tEPmJKcNcNCLkvbpY/lwyOlizWsqPNo8w==", + "dependencies": { + "@types/levelup": "^4.3.0", + "ethereumjs-util": "^7.1.4", + "level-mem": "^5.0.1", + "level-ws": "^2.0.0", + "readable-stream": "^3.6.0", + "semaphore-async-await": "^1.5.1" + } + }, + "node_modules/merkle-patricia-tree/node_modules/ethereumjs-util": { + "version": "7.1.5", + "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-7.1.5.tgz", + "integrity": "sha512-SDl5kKrQAudFBUe5OJM9Ac6WmMyYmXX/6sTmLZ3ffG2eY6ZIGBes3pEDxNN6V72WyOw4CPD5RomKdsa8DAAwLg==", + "dependencies": { + "@types/bn.js": "^5.1.0", + "bn.js": "^5.1.2", + "create-hash": "^1.1.2", + "ethereum-cryptography": "^0.1.3", + "rlp": "^2.2.4" + }, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/methods": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", @@ -5683,6 +5951,11 @@ "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, + "node_modules/prr": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", + "integrity": "sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==" + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", @@ -6173,6 +6446,14 @@ "node": ">=10.0.0" } }, + "node_modules/semaphore-async-await": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/semaphore-async-await/-/semaphore-async-await-1.5.1.tgz", + "integrity": "sha512-b/ptP11hETwYWpeilHXXQiV5UJNJl7ZWWooKRE5eBIYWoom6dZ0SluCIdCtKycsMtZgKWE01/qAw6jblw1YVhg==", + "engines": { + "node": ">=4.1" + } + }, "node_modules/semver": { "version": "7.5.3", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz", @@ -7496,6 +7777,14 @@ } } }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "engines": { + "node": ">=0.4" + } + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", @@ -7540,4 +7829,3 @@ } } } - diff --git a/package.json b/package.json index 47a8087c..02f696f9 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "got": "11.8.6", "jayson": "3.6.5", "jsonwebtoken": "9.0.0", + "merkle-patricia-tree": "^4.2.4", "pm2": "5.3.0", "ts-node": "10.9.1", "typescript": "4.5.5", diff --git a/src/external/Collector.ts b/src/external/Collector.ts index 8691691d..389d2ef2 100644 --- a/src/external/Collector.ts +++ b/src/external/Collector.ts @@ -15,7 +15,9 @@ import { Err, NewErr, NewInternalErr } from './Err' import { nestedCountersInstance } from '../utils/nestedCounters' import { BlockCacheManager } from '../cache/BlockCacheManager' import { sleep } from '../utils' - +import { BaseTrie } from 'merkle-patricia-tree' +import RLP from 'rlp' +import { BigNumber } from 'ethers' class Collector extends BaseExternal { private blockCacheManager: BlockCacheManager @@ -245,6 +247,19 @@ class Collector extends BaseExternal { return await this.inner_getBlock(blockSearchValue, blockSearchType, details) } + async calculateTransactionRoot(txns: String[]): Promise { + const trie = new BaseTrie() + for (let i = 0; i < txns.length; i++) { + // i now is also the transactionIndex of transaction in the block + const currTxnHash = txns[i] + const path = Buffer.from(RLP.encode(i)) + await trie.put(path, Buffer.from(currTxnHash)) + } + const evaluatedTxnRoot = '0x' + trie.root.toString('hex') + console.log(evaluatedTxnRoot) + return evaluatedTxnRoot + } + async inner_getBlock( blockSearchValue: string, blockSearchType: 'hex_num' | 'hash' | 'tag', @@ -305,15 +320,19 @@ class Collector extends BaseExternal { } const txQuery = `${this.baseUrl}/api/transaction?blockNumber=${blockNumber}` - + let blockGasUsed = '0x0' + // This parameter is used to determine the total gas used of transactions in the block + // It is used to calculate the block gas used resultBlock.transactions = await axios .get(txQuery) .then((response) => { if (!response.data.success) return [] return response.data.transactions.map((tx: any) => { + let thisDecodeTransaction = this.decodeTransaction(tx) + blockGasUsed = BigNumber.from(blockGasUsed).add(thisDecodeTransaction.gas).toHexString() //need to review the safety of this for caching and support that this could change! // UPDATE: We're now handling the response as per the "details" flag by mutating the "transactions" field. The default cache storage contains the full transaction details. - return this.decodeTransaction(tx) + return thisDecodeTransaction }) }) .catch((e) => { @@ -321,6 +340,10 @@ class Collector extends BaseExternal { console.error('collector.getBlock could not get txs for the block', e) return [] }) + // Now we have the block gas used then return it to the RPC method + resultBlock.gasUsed = blockGasUsed + // Start to calculate the transaction root fot this block + resultBlock.transactionsRoot = await this.calculateTransactionRoot(readableBlock.tranactions) if (CONFIG.enableBlockCache) this.blockCacheManager.update(blockSearchValue, blockSearchType, resultBlock) From 0aa15deaf7db58cc4f88814eda286b6ccaa3ae02 Mon Sep 17 00:00:00 2001 From: tonititi Date: Wed, 8 May 2024 12:40:52 +0700 Subject: [PATCH 2/6] GREEN-42 fix feedback for PR-15 --- package-lock.json | 3 ++- package.json | 3 ++- src/external/Collector.ts | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 839b1bee..baeea4ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@ethereumjs/rlp": "^5.0.2", "@ethereumjs/tx": "3.4.0", "@ethereumjs/util": "^9.0.2", + "@ethersproject/bignumber": "^5.7.0", "@hapi/sntp": "^4.0.0", "@shardus/archiver-discovery": "1.1.0", "@shardus/crypto-utils": "4.1.3", @@ -28,7 +29,7 @@ "got": "11.8.6", "jayson": "3.6.5", "jsonwebtoken": "9.0.0", - "merkle-patricia-tree": "^4.2.4", + "merkle-patricia-tree": "4.2.4", "pm2": "5.3.0", "ts-node": "10.9.1", "typescript": "4.5.5", diff --git a/package.json b/package.json index 02f696f9..5f5fa631 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@ethereumjs/rlp": "^5.0.2", "@ethereumjs/tx": "3.4.0", "@ethereumjs/util": "^9.0.2", + "@ethersproject/bignumber": "^5.7.0", "@hapi/sntp": "^4.0.0", "@shardus/archiver-discovery": "1.1.0", "@shardus/crypto-utils": "4.1.3", @@ -36,7 +37,7 @@ "got": "11.8.6", "jayson": "3.6.5", "jsonwebtoken": "9.0.0", - "merkle-patricia-tree": "^4.2.4", + "merkle-patricia-tree": "4.2.4", "pm2": "5.3.0", "ts-node": "10.9.1", "typescript": "4.5.5", diff --git a/src/external/Collector.ts b/src/external/Collector.ts index 389d2ef2..591d0908 100644 --- a/src/external/Collector.ts +++ b/src/external/Collector.ts @@ -276,7 +276,7 @@ class Collector extends BaseExternal { //instead of look up by key we need to give the inp type and block const cachedBlock = this.blockCacheManager.get(blockSearchValue, blockSearchType) - //should we retry for tranactions if there are not any?? + //should we retry for transactions if there are not any?? if (cachedBlock) { //if we dont need details we must adjust the return value that we got from cache if (details === false) { @@ -343,7 +343,7 @@ class Collector extends BaseExternal { // Now we have the block gas used then return it to the RPC method resultBlock.gasUsed = blockGasUsed // Start to calculate the transaction root fot this block - resultBlock.transactionsRoot = await this.calculateTransactionRoot(readableBlock.tranactions) + resultBlock.transactionsRoot = await this.calculateTransactionRoot(resultBlock.transactions) if (CONFIG.enableBlockCache) this.blockCacheManager.update(blockSearchValue, blockSearchType, resultBlock) From bf116d832c9a8d21fa17c3b6b1b8708036a40725 Mon Sep 17 00:00:00 2001 From: tonititi Date: Mon, 20 May 2024 12:55:19 +0700 Subject: [PATCH 3/6] GREEN-42 merge with dev --- src/external/Collector.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/external/Collector.ts b/src/external/Collector.ts index 591d0908..386d79d5 100644 --- a/src/external/Collector.ts +++ b/src/external/Collector.ts @@ -17,7 +17,7 @@ import { BlockCacheManager } from '../cache/BlockCacheManager' import { sleep } from '../utils' import { BaseTrie } from 'merkle-patricia-tree' import RLP from 'rlp' -import { BigNumber } from 'ethers' +import { BigNumber } from '@ethersproject/bignumber' class Collector extends BaseExternal { private blockCacheManager: BlockCacheManager @@ -247,7 +247,7 @@ class Collector extends BaseExternal { return await this.inner_getBlock(blockSearchValue, blockSearchType, details) } - async calculateTransactionRoot(txns: String[]): Promise { + async calculateTransactionRoot(txns: string[]): Promise { const trie = new BaseTrie() for (let i = 0; i < txns.length; i++) { // i now is also the transactionIndex of transaction in the block From 81c5748eab83ddab7a8bfbc3e9760cc81d9140b7 Mon Sep 17 00:00:00 2001 From: tonititi Date: Tue, 21 May 2024 12:22:47 +0700 Subject: [PATCH 4/6] add package-lock.json --- package-lock.json | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/package-lock.json b/package-lock.json index baeea4ca..85467cd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -273,6 +273,59 @@ "@scure/bip39": "1.2.2" } }, + "node_modules/@ethersproject/bignumber": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/bignumber/-/bignumber-5.7.0.tgz", + "integrity": "sha512-n1CAdIHRWjSucQO3MC1zPSVgV/6dy/fjL9pMrPP9peL+QxEg9wOsVqwD4+818B6LUEtaXzVHQiuivzRoxPxUGw==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ], + "dependencies": { + "@ethersproject/bytes": "^5.7.0", + "@ethersproject/logger": "^5.7.0", + "bn.js": "^5.2.1" + } + }, + "node_modules/@ethersproject/bytes": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/bytes/-/bytes-5.7.0.tgz", + "integrity": "sha512-nsbxwgFXWh9NyYWo+U8atvmMsSdKJprTcICAkvbBffT75qDocbuggBU0SJiVK2MuTrp0q+xvLkTnGMPK1+uA9A==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ], + "dependencies": { + "@ethersproject/logger": "^5.7.0" + } + }, + "node_modules/@ethersproject/logger": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/@ethersproject/logger/-/logger-5.7.0.tgz", + "integrity": "sha512-0odtFdXu/XHtjQXJYA3u9G0G8btm0ND5Cu8M7i5vhEcE8/HmF4Lbdqanwyv4uQTr2tx6b7fQRmgLrsnpQlmnig==", + "funding": [ + { + "type": "individual", + "url": "https://gitcoin.co/grants/13/ethersjs-complete-simple-and-tiny-2" + }, + { + "type": "individual", + "url": "https://www.buymeacoffee.com/ricmoo" + } + ] + }, "node_modules/@hapi/boom": { "version": "9.1.4", "resolved": "https://registry.npmjs.org/@hapi/boom/-/boom-9.1.4.tgz", From afe7041514957d3c5d4702a1ad619dd954b57c5e Mon Sep 17 00:00:00 2001 From: akirapham Date: Sat, 25 May 2024 04:53:02 +0700 Subject: [PATCH 5/6] fix: import rlp deps --- src/external/Collector.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/external/Collector.ts b/src/external/Collector.ts index 386d79d5..c352aac0 100644 --- a/src/external/Collector.ts +++ b/src/external/Collector.ts @@ -16,7 +16,7 @@ import { nestedCountersInstance } from '../utils/nestedCounters' import { BlockCacheManager } from '../cache/BlockCacheManager' import { sleep } from '../utils' import { BaseTrie } from 'merkle-patricia-tree' -import RLP from 'rlp' +import * as RLP from 'rlp' import { BigNumber } from '@ethersproject/bignumber' class Collector extends BaseExternal { private blockCacheManager: BlockCacheManager @@ -251,12 +251,12 @@ class Collector extends BaseExternal { const trie = new BaseTrie() for (let i = 0; i < txns.length; i++) { // i now is also the transactionIndex of transaction in the block + // eslint-disable-next-line security/detect-object-injection const currTxnHash = txns[i] const path = Buffer.from(RLP.encode(i)) await trie.put(path, Buffer.from(currTxnHash)) } const evaluatedTxnRoot = '0x' + trie.root.toString('hex') - console.log(evaluatedTxnRoot) return evaluatedTxnRoot } @@ -299,6 +299,7 @@ class Collector extends BaseExternal { /* prettier-ignore */ if (verbose) console.log(`Collector: getBlock blockQuery: ${blockQuery}`) const response = await axios.get(blockQuery).then((response) => response.data) + console.log('blockQuery', blockQuery, response) if (!response.success) return null const { readableBlock, number } = response @@ -328,7 +329,7 @@ class Collector extends BaseExternal { .then((response) => { if (!response.data.success) return [] return response.data.transactions.map((tx: any) => { - let thisDecodeTransaction = this.decodeTransaction(tx) + const thisDecodeTransaction = this.decodeTransaction(tx) blockGasUsed = BigNumber.from(blockGasUsed).add(thisDecodeTransaction.gas).toHexString() //need to review the safety of this for caching and support that this could change! // UPDATE: We're now handling the response as per the "details" flag by mutating the "transactions" field. The default cache storage contains the full transaction details. @@ -343,7 +344,9 @@ class Collector extends BaseExternal { // Now we have the block gas used then return it to the RPC method resultBlock.gasUsed = blockGasUsed // Start to calculate the transaction root fot this block - resultBlock.transactionsRoot = await this.calculateTransactionRoot(resultBlock.transactions) + resultBlock.transactionsRoot = await this.calculateTransactionRoot( + resultBlock.transactions.map((tx: any) => tx.hash) + ) if (CONFIG.enableBlockCache) this.blockCacheManager.update(blockSearchValue, blockSearchType, resultBlock) From f2209002b55c351c2f89f8dee03cea872bdc77d0 Mon Sep 17 00:00:00 2001 From: akirapham Date: Sat, 25 May 2024 05:06:45 +0700 Subject: [PATCH 6/6] fix: compute blockGasUsed as actual sum of gas spent by all txes --- src/external/Collector.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/external/Collector.ts b/src/external/Collector.ts index c352aac0..ffb322ad 100644 --- a/src/external/Collector.ts +++ b/src/external/Collector.ts @@ -299,7 +299,6 @@ class Collector extends BaseExternal { /* prettier-ignore */ if (verbose) console.log(`Collector: getBlock blockQuery: ${blockQuery}`) const response = await axios.get(blockQuery).then((response) => response.data) - console.log('blockQuery', blockQuery, response) if (!response.success) return null const { readableBlock, number } = response @@ -329,11 +328,19 @@ class Collector extends BaseExternal { .then((response) => { if (!response.data.success) return [] return response.data.transactions.map((tx: any) => { - const thisDecodeTransaction = this.decodeTransaction(tx) - blockGasUsed = BigNumber.from(blockGasUsed).add(thisDecodeTransaction.gas).toHexString() - //need to review the safety of this for caching and support that this could change! + const decodedTx = this.decodeTransaction(tx) + let gasUsedByThisTx = decodedTx.gas + if ( + tx.wrappedEVMAccount && + tx.wrappedEVMAccount.readableReceipt && + tx.wrappedEVMAccount.readableReceipt.gasUsed + ) { + gasUsedByThisTx = tx.wrappedEVMAccount.readableReceipt.gasUsed + } + blockGasUsed = BigNumber.from(blockGasUsed).add(gasUsedByThisTx).toHexString() + // need to review the safety of this for caching and support that this could change! // UPDATE: We're now handling the response as per the "details" flag by mutating the "transactions" field. The default cache storage contains the full transaction details. - return thisDecodeTransaction + return decodedTx }) }) .catch((e) => { @@ -477,7 +484,6 @@ class Collector extends BaseExternal { nestedCountersInstance.countEvent('collector', 'decodeTransaction') let result: any = null let txObj = null - try { const raw = tx.originalTxData.tx.raw as string txObj = TransactionFactory.fromSerializedData(toBuffer(raw))