Skip to content

Commit

Permalink
Fix transaction packing in mining
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenphuminh authored Aug 31, 2022
1 parent 22c9555 commit 25d2baf
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 25d2baf

Please sign in to comment.