Skip to content

Commit

Permalink
Fix nonce validation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Jun 4, 2019
1 parent 36821ff commit 0b89584
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -368,20 +368,7 @@ bool render
HashDigest<SHA256>? tip =
Store.IndexBlockHash(Id.ToString(), -1);

foreach (Transaction<T> tx in block.Transactions)
{
Address signer = tx.Signer;
long nonce = Store.GetTxNonce(Id.ToString(), signer);

if (!nonce.Equals(tx.Nonce))
{
throw new InvalidTxNonceException(
tx.Id,
nonce,
tx.Nonce,
"Transaction nonce is invalid.");
}
}
ValidateNonce(block);

evaluations = block.Evaluate(
currentTime,
Expand Down Expand Up @@ -423,6 +410,31 @@ bool render
}
}

internal void ValidateNonce(Block<T> block)
{
var nonces = new Dictionary<Address, long>();
foreach (Transaction<T> tx in block.Transactions)
{
Address signer = tx.Signer;
if (!nonces.TryGetValue(signer, out long nonce))
{
nonce =
Store.GetTxNonce(Id.ToString(), signer);
}

if (!nonce.Equals(tx.Nonce))
{
throw new InvalidTxNonceException(
tx.Id,
nonce,
tx.Nonce,
"Transaction nonce is invalid.");
}

nonces[signer] = nonce + 1;
}
}

internal HashDigest<SHA256> FindBranchPoint(BlockLocator locator)
{
try
Expand Down

0 comments on commit 0b89584

Please sign in to comment.