Skip to content

Commit

Permalink
Merge pull request #61 from nguyenphuminh/merkle-fix
Browse files Browse the repository at this point in the history
Merkle fix
  • Loading branch information
nguyenphuminh authored Apr 13, 2023
2 parents 3a72013 + 27bda9a commit ac2300c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/consensus/consensus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) &&
Expand Down
5 changes: 3 additions & 2 deletions src/core/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "") {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/merkle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit ac2300c

Please sign in to comment.