From 98bf2f65323f1a23ed6f7fee936ada37569472cf Mon Sep 17 00:00:00 2001 From: Phu Minh <53084840+nguyenphuminh@users.noreply.github.com> Date: Thu, 13 Apr 2023 10:25:46 +0700 Subject: [PATCH 1/2] Fix transaction trie --- src/consensus/consensus.js | 3 ++- src/core/block.js | 5 +++-- src/core/merkle.js | 2 +- src/node/server.js | 4 ++-- src/utils/utils.js | 6 +++++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/consensus/consensus.js b/src/consensus/consensus.js index 6110621..df0c333 100644 --- a/src/consensus/consensus.js +++ b/src/consensus/consensus.js @@ -3,6 +3,7 @@ const Block = require("../core/block"); const { log16 } = require("../utils/utils"); const { buildMerkleTree } = require("../core/merkle"); const { BLOCK_REWARD, BLOCK_TIME } = require("../config.json"); +const { indexTxns } = require("../utils/utils"); async function verifyBlock(newBlock, chainInfo, stateDB, codeDB, enableLogging = false) { // Check if the block is valid or not, if yes, we will push it to the chain, update the difficulty, chain state and the transaction pool. @@ -44,7 +45,7 @@ async function verifyBlock(newBlock, chainInfo, stateDB, codeDB, enableLogging = newBlock.blockNumber - 1 === chainInfo.latestBlock.blockNumber && // Check transaction hash - buildMerkleTree(newBlock.transactions).val === newBlock.txRoot && + buildMerkleTree(indexTxns(newBlock.transactions)).val === newBlock.txRoot && // Check gas limit Block.hasValidGasLimit(newBlock) && diff --git a/src/core/block.js b/src/core/block.js index b768bea..89d9cf9 100644 --- a/src/core/block.js +++ b/src/core/block.js @@ -6,6 +6,7 @@ const Transaction = require("./transaction"); const { buildMerkleTree } = require("./merkle"); const { BLOCK_REWARD, BLOCK_GAS_LIMIT, EMPTY_HASH } = require("../config.json"); const jelscript = require("./runtime"); +const { indexTxns } = require("../utils/utils"); class Block { constructor(blockNumber = 1, timestamp = Date.now(), transactions = [], difficulty = 1, parentHash = "", coinbase = "") { @@ -17,8 +18,8 @@ class Block { this.difficulty = difficulty; // Difficulty to mine block this.parentHash = parentHash; // Parent (previous) block's hash this.nonce = 0; // Nonce - this.txRoot = buildMerkleTree(transactions).val; // Merkle root of transactions - this.coinbase = coinbase; // Address to receive reward + this.txRoot = buildMerkleTree(indexTxns(transactions)).val; // Merkle root of transactions + this.coinbase = coinbase; // Address to receive reward this.hash = Block.getHash(this); // Hash of the block } diff --git a/src/core/merkle.js b/src/core/merkle.js index 2359854..403aed2 100644 --- a/src/core/merkle.js +++ b/src/core/merkle.js @@ -34,7 +34,7 @@ function verifyMerkleProof(leaves, root) { function buildMerkleTree(items) { if (items.length === 0) return Node(SHA256("0")); - let hashList = items.map(transaction => Node(SHA256(JSON.stringify(transaction)))); + let hashList = items.map(item => Node(SHA256(item))); if (hashList.length % 2 !== 0 && hashList.length !== 1) { hashList.push(hashList[hashList.length-1]); diff --git a/src/node/server.js b/src/node/server.js index dee5dff..6983cbe 100644 --- a/src/node/server.js +++ b/src/node/server.js @@ -16,7 +16,7 @@ const { addTransaction, clearDepreciatedTxns }= require("../core/txPool"); const rpc = require("../rpc/rpc"); const TYPE = require("./message-types"); const { verifyBlock, updateDifficulty } = require("../consensus/consensus"); -const { parseJSON } = require("../utils/utils"); +const { parseJSON, indexTxns } = require("../utils/utils"); const jelscript = require("../core/runtime"); const { buildMerkleTree } = require("../core/merkle"); @@ -445,7 +445,7 @@ async function mine(publicKey, ENABLE_LOGGING) { block.transactions = transactionsToMine; // Add transactions to block block.hash = Block.getHash(block); // Re-hash with new transactions - block.txRoot = buildMerkleTree(block.transactions).val; // Re-gen transaction root with new transactions + block.txRoot = buildMerkleTree(indexTxns(block.transactions)).val; // Re-gen transaction root with new transactions // Mine the block. mine(block, chainInfo.difficulty) diff --git a/src/utils/utils.js b/src/utils/utils.js index 71b3705..47f010a 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -30,4 +30,8 @@ function parseJSON(value) { return parsed; } -module.exports = { log16, isNumber, parseJSON, bigIntable }; +function indexTxns(transactions) { + return transactions.map((txn, index) => index.toString() + JSON.stringify(txn)); +} + +module.exports = { log16, isNumber, parseJSON, bigIntable, indexTxns }; From 27bda9af20b9d3e67185ce94422235a81e879297 Mon Sep 17 00:00:00 2001 From: Phu Minh <53084840+nguyenphuminh@users.noreply.github.com> Date: Thu, 13 Apr 2023 10:27:26 +0700 Subject: [PATCH 2/2] Update versions --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a53a115..7dbae61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jechain", - "version": "0.21.0", + "version": "0.22.0", "description": "Node for JeChain - an experimental smart contract blockchain network", "main": "./index.js", "scripts": {