Skip to content

Commit

Permalink
Fixed a problem with nonce
Browse files Browse the repository at this point in the history
- Fixed a problem with nonce in transaction pool: Nonce starts at 0 when checking, it's supposed to be the current nonce of the sender, not 0.
- Add more info to logs.
  • Loading branch information
nguyenphuminh authored Jan 13, 2024
1 parent 065827e commit 2a0a4cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/core/txPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function addTransaction(transaction, chainInfo, stateDB) {
try {
transaction = Transaction.deserialize(transaction);
} catch (e) {
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool: Can not deserialize transaction.`);

// If transaction can not be deserialized, it's faulty
return;
Expand All @@ -24,7 +24,7 @@ async function addTransaction(transaction, chainInfo, stateDB) {
await Transaction.isValid(transaction, stateDB)) ||
BigInt(transaction.additionalData.contractGas || 0) > BigInt(BLOCK_GAS_LIMIT)
) {
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool: Transaction is invalid.`);

return;
}
Expand All @@ -36,13 +36,13 @@ async function addTransaction(transaction, chainInfo, stateDB) {
const txSenderAddress = SHA256(txSenderPubkey);

if (!(await stateDB.keys().all()).includes(txSenderAddress)) {
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool: Sender does not exist.`);

return;
}

// Check nonce
let maxNonce = 0;
let maxNonce = deserializeState(await stateDB.get(txSenderAddress)).nonce;

for (const tx of txPool) {
const poolTxSenderPubkey = Transaction.getPubKey(transaction);
Expand All @@ -54,7 +54,7 @@ async function addTransaction(transaction, chainInfo, stateDB) {
}

if (maxNonce + 1 !== transaction.nonce) {
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool: Transaction has nonce lower than nonce in pool.`);

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async function startServer(options) {
// This is pretty much the same as addTransaction, but we will send the transaction to other connected nodes if it's valid.

// Check nonce
let maxNonce = 0;
let maxNonce = deserializeState(await stateDB.get(txSenderAddress)).nonce;

for (const tx of chainInfo.transactionPool) {
const poolTxSenderPubkey = Transaction.getPubKey(transaction);
Expand Down

0 comments on commit 2a0a4cc

Please sign in to comment.