From 9a7cc897f6e3beb1f5e294d0bb7eecc7959dbcb4 Mon Sep 17 00:00:00 2001 From: Seunghun Lee Date: Mon, 17 Jun 2019 14:08:46 +0900 Subject: [PATCH] Sort transaction order in Block constructor --- Libplanet.Tests/Blockchain/BlockChainTest.cs | 26 ++++++++++++++++++++ Libplanet/Blockchain/BlockChain.cs | 6 ++--- Libplanet/Blocks/Block.cs | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Libplanet.Tests/Blockchain/BlockChainTest.cs b/Libplanet.Tests/Blockchain/BlockChainTest.cs index e0793680e5b..3184c4e0632 100644 --- a/Libplanet.Tests/Blockchain/BlockChainTest.cs +++ b/Libplanet.Tests/Blockchain/BlockChainTest.cs @@ -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 genesis = TestUtils.MineGenesis(); + _blockChain.Append(genesis); + + Transaction[] txsA = + { + _fx.MakeTransaction(actions, privateKey: privateKey, nonce: 1), + _fx.MakeTransaction(actions, privateKey: privateKey, nonce: 0), + }; + Block b1 = TestUtils.MineNext(genesis, txsA); + _blockChain.ValidateNonce(b1); + + Transaction[] txsB = + { + _fx.MakeTransaction(actions, privateKey: privateKey, nonce: 1), + }; + Block b2 = TestUtils.MineNext(genesis, txsB); + Assert.Throws(() => + _blockChain.ValidateNonce(b2)); + } + private sealed class NullPolicy : IBlockPolicy where T : IAction, new() { diff --git a/Libplanet/Blockchain/BlockChain.cs b/Libplanet/Blockchain/BlockChain.cs index 9e86fb7f5fb..59a43fc48e3 100644 --- a/Libplanet/Blockchain/BlockChain.cs +++ b/Libplanet/Blockchain/BlockChain.cs @@ -322,11 +322,9 @@ DateTimeOffset currentTime @namespace, index - 1 ); - List> transactions = Store + IEnumerable> transactions = Store .IterateStagedTransactionIds() - .Select(Store.GetTransaction) - .OrderBy(tx => tx.Nonce) - .ToList(); + .Select(Store.GetTransaction); Block block = Block.Mine( index: index, diff --git a/Libplanet/Blocks/Block.cs b/Libplanet/Blocks/Block.cs index 361cb9fb4dc..98e006dd0e4 100644 --- a/Libplanet/Blocks/Block.cs +++ b/Libplanet/Blocks/Block.cs @@ -38,7 +38,7 @@ public class Block : ISerializable Miner = miner; PreviousHash = previousHash; Timestamp = timestamp; - Transactions = transactions; + Transactions = transactions.OrderBy(tx => tx.Nonce).ToList(); Hash = Hashcash.Hash(ToBencodex(false, false)); }