Skip to content

Commit

Permalink
Sort transaction order in Block constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Jun 18, 2019
1 parent 41c94f5 commit 9a7cc89
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
26 changes: 26 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Expand Up @@ -814,6 +814,32 @@ public void GetNonce()
Assert.Equal(4, _blockChain.GetNonce(address));
}

[Fact]
public void ValidateNonce()
{
var privateKey = new PrivateKey();
var actions = new[] { new DumbAction() };

Block<DumbAction> genesis = TestUtils.MineGenesis<DumbAction>();
_blockChain.Append(genesis);

Transaction<DumbAction>[] txsA =
{
_fx.MakeTransaction(actions, privateKey: privateKey, nonce: 1),
_fx.MakeTransaction(actions, privateKey: privateKey, nonce: 0),
};
Block<DumbAction> b1 = TestUtils.MineNext(genesis, txsA);
_blockChain.ValidateNonce(b1);

Transaction<DumbAction>[] txsB =
{
_fx.MakeTransaction(actions, privateKey: privateKey, nonce: 1),
};
Block<DumbAction> b2 = TestUtils.MineNext(genesis, txsB);
Assert.Throws<InvalidTxNonceException>(() =>
_blockChain.ValidateNonce(b2));
}

private sealed class NullPolicy<T> : IBlockPolicy<T>
where T : IAction, new()
{
Expand Down
6 changes: 2 additions & 4 deletions Libplanet/Blockchain/BlockChain.cs
Expand Up @@ -322,11 +322,9 @@ DateTimeOffset currentTime
@namespace,
index - 1
);
List<Transaction<T>> transactions = Store
IEnumerable<Transaction<T>> transactions = Store
.IterateStagedTransactionIds()
.Select(Store.GetTransaction<T>)
.OrderBy(tx => tx.Nonce)
.ToList();
.Select(Store.GetTransaction<T>);

Block<T> block = Block<T>.Mine(
index: index,
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Blocks/Block.cs
Expand Up @@ -38,7 +38,7 @@ public class Block<T> : ISerializable
Miner = miner;
PreviousHash = previousHash;
Timestamp = timestamp;
Transactions = transactions;
Transactions = transactions.OrderBy(tx => tx.Nonce).ToList();
Hash = Hashcash.Hash(ToBencodex(false, false));
}

Expand Down

0 comments on commit 9a7cc89

Please sign in to comment.