From 25d2baf6ad644dc2c966b37c2cf5f254a843081d Mon Sep 17 00:00:00 2001 From: Phu Minh <53084840+nguyenphuminh@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:33:46 +0700 Subject: [PATCH] Fix transaction packing in mining --- src/node/server.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/node/server.js b/src/node/server.js index af01c37..178b8ea 100644 --- a/src/node/server.js +++ b/src/node/server.js @@ -340,27 +340,22 @@ function mine(publicKey, ENABLE_LOGGING) { }); } - let gas = BigInt(0); // We will collect all the gas fee and add it to the mint transaction, along with the fixed mining reward. - - chainInfo.transactionPool.forEach(transaction => { gas += BigInt(transaction.gas) + BigInt(transaction.additionalData.contractGas || 0) }); - - // Mint transaction for miner's reward. - const rewardTransaction = new Transaction(SHA256(publicKey), (BigInt(BLOCK_REWARD) + gas).toString()); - Transaction.sign(rewardTransaction, MINT_KEY_PAIR); - + // Collect a list of transactions to mine const transactionsToMine = []; let totalGas = 0n; for (const tx of chainInfo.transactionPool) { - if (totalGas + BigInt(tx.additionalData.contractGas || 0) >= BigInt(BLOCK_GAS_LIMIT)) { - break; - } + if (totalGas + BigInt(tx.additionalData.contractGas || 0) >= BigInt(BLOCK_GAS_LIMIT)) break; transactionsToMine.push(tx); totalGas += BigInt(tx.additionalData.contractGas || 0); } + // Mint transaction for miner's reward. + const rewardTransaction = new Transaction(SHA256(publicKey), (BigInt(BLOCK_REWARD) + totalGas).toString()); + Transaction.sign(rewardTransaction, MINT_KEY_PAIR); + // Create a new block. const block = new Block( chainInfo.latestBlock.blockNumber + 1,