From bebf26b4c50c5830cdd71baca4837df8b32652d5 Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 11 Nov 2019 13:38:06 +0100 Subject: [PATCH 01/10] Fix 1219 --- neo/Consensus/ConsensusService.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 574082043a..38961f41d9 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -122,8 +122,11 @@ private void CheckCommits() if (context.CommitPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { Block block = context.CreateBlock(); - Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); - localNode.Tell(new LocalNode.Relay { Inventory = block }); + if (context.EnsureHeader() != null) + { + Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); + localNode.Tell(new LocalNode.Relay { Inventory = block }); + } } } From f49ccada1a6584a5ad3d1dcd511cacb1b78a04fd Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 11 Nov 2019 13:42:01 +0100 Subject: [PATCH 02/10] Refactor --- neo/Consensus/ConsensusContext.cs | 2 +- neo/Consensus/ConsensusService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index e7c2649782..a7c0190fb4 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -92,7 +92,7 @@ public Block CreateBlock() } Block.Witness = sc.GetWitnesses()[0]; Block.Transactions = TransactionHashes.Select(p => Transactions[p]).ToArray(); - return Block; + return EnsureHeader(); } public void Deserialize(BinaryReader reader) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 38961f41d9..00764a103c 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -122,7 +122,7 @@ private void CheckCommits() if (context.CommitPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { Block block = context.CreateBlock(); - if (context.EnsureHeader() != null) + if (block != null) { Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); localNode.Tell(new LocalNode.Relay { Inventory = block }); From 5cab7ef8860f1435f0f67b10a21c77109ccc6cc6 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Wed, 13 Nov 2019 16:30:15 +0800 Subject: [PATCH 03/10] Update ConsensusContext.cs --- neo/Consensus/ConsensusContext.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index a7c0190fb4..d05b4a64ea 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -82,6 +82,7 @@ public ConsensusContext(Wallet wallet, Store store) public Block CreateBlock() { + EnsureHeader(); Contract contract = Contract.CreateMultiSigContract(M, Validators); ContractParametersContext sc = new ContractParametersContext(Block); for (int i = 0, j = 0; i < Validators.Length && j < M; i++) @@ -92,7 +93,7 @@ public Block CreateBlock() } Block.Witness = sc.GetWitnesses()[0]; Block.Transactions = TransactionHashes.Select(p => Transactions[p]).ToArray(); - return EnsureHeader(); + return Block; } public void Deserialize(BinaryReader reader) From 296ad5dd000068c909a292caea3f08bbe8eb110e Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 13 Nov 2019 11:30:16 +0100 Subject: [PATCH 04/10] Remove if --- neo/Consensus/ConsensusService.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 00764a103c..574082043a 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -122,11 +122,8 @@ private void CheckCommits() if (context.CommitPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { Block block = context.CreateBlock(); - if (block != null) - { - Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); - localNode.Tell(new LocalNode.Relay { Inventory = block }); - } + Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); + localNode.Tell(new LocalNode.Relay { Inventory = block }); } } From 188bebc87aa3fe058a087a5ffc820837ddf27308 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 13:17:39 +0100 Subject: [PATCH 05/10] UT --- neo.UnitTests/Consensus/UT_ConsensusContext.cs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/neo.UnitTests/Consensus/UT_ConsensusContext.cs index 9a0dd39ba7..6ab534ce22 100644 --- a/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -119,6 +119,8 @@ private Block SignBlock(ConsensusContext context) { context.Block.MerkleRoot = null; + // Fake commits + for (int x = 0; x < _validatorKeys.Length; x++) { _context.MyIndex = x; @@ -127,19 +129,7 @@ private Block SignBlock(ConsensusContext context) _context.CommitPayloads[_context.MyIndex] = com; } - // Manual block sign - - Contract contract = Contract.CreateMultiSigContract(context.M, context.Validators); - ContractParametersContext sc = new ContractParametersContext(context.Block); - for (int i = 0, j = 0; i < context.Validators.Length && j < context.M; i++) - { - if (context.CommitPayloads[i]?.ConsensusMessage.ViewNumber != context.ViewNumber) continue; - sc.AddSignature(contract, context.Validators[i], context.CommitPayloads[i].GetDeserializedMessage().Signature); - j++; - } - context.Block.Witness = sc.GetWitnesses()[0]; - context.Block.Transactions = context.TransactionHashes.Select(p => context.Transactions[p]).ToArray(); - return context.Block; + return context.CreateBlock(); } private void EnsureContext(ConsensusContext context, params Transaction[] expected) From 51af5be4256879956339882f81585e8ffc3c3317 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 13:24:37 +0100 Subject: [PATCH 06/10] Fix CheckPreparations --- neo/Consensus/ConsensusService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 574082043a..2d8c4c6777 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -147,7 +147,7 @@ private void CheckExpectedView(byte viewNumber) private void CheckPreparations() { - if (context.PreparationPayloads.Count(p => p != null) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) + if (context.PreparationPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { ConsensusPayload payload = context.MakeCommit(); Log($"send commit"); From 4cba7ffddb58b9acd05a31e68b47e8318eed6c6a Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 13:33:48 +0100 Subject: [PATCH 07/10] Ensure load only this view --- neo/Consensus/ConsensusContext.cs | 26 ++++++++++++++++++++++---- neo/Consensus/ConsensusService.cs | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index d05b4a64ea..b104cf9386 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -109,10 +109,10 @@ public void Deserialize(BinaryReader reader) ViewNumber = reader.ReadByte(); TransactionHashes = reader.ReadSerializableArray(); Transaction[] transactions = reader.ReadSerializableArray(Block.MaxTransactionsPerBlock); - PreparationPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); - CommitPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); - ChangeViewPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); - LastChangeViewPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); + PreparationPayloads = ReadConsensusPayload(reader, ViewNumber); + CommitPayloads = ReadConsensusPayload(reader, ViewNumber); + ChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber); + LastChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber); if (TransactionHashes.Length == 0 && !RequestSentOrReceived) TransactionHashes = null; Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); @@ -124,6 +124,24 @@ public void Deserialize(BinaryReader reader) } } + private static ConsensusPayload[] ReadConsensusPayload(BinaryReader reader, byte viewNumber) + { + // Read + var payloads = reader.ReadNullableArray(Blockchain.MaxValidators); + + // Remove other views + for (int x = 0; x < payloads.Length; x++) + { + var p = payloads[x]; + if (p != null && p.ConsensusMessage.ViewNumber != viewNumber) + { + payloads[x] = null; + } + } + + return payloads; + } + public void Dispose() { Snapshot?.Dispose(); diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 2d8c4c6777..574082043a 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -147,7 +147,7 @@ private void CheckExpectedView(byte viewNumber) private void CheckPreparations() { - if (context.PreparationPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) + if (context.PreparationPayloads.Count(p => p != null) >= context.M && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { ConsensusPayload payload = context.MakeCommit(); Log($"send commit"); From 84ec52a4028fe90c04d09179a1ec010f77ed4a48 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 13:37:40 +0100 Subject: [PATCH 08/10] Clean ut --- neo.UnitTests/Consensus/UT_ConsensusContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/neo.UnitTests/Consensus/UT_ConsensusContext.cs index 6ab534ce22..821b6f8da5 100644 --- a/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -4,7 +4,6 @@ using Neo.Consensus; using Neo.IO; using Neo.Network.P2P.Payloads; -using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.Wallets; using System; From 058df1f6f7649d9c371769186b21393b035a436a Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 14:04:42 +0100 Subject: [PATCH 09/10] Fix UT --- neo.UnitTests/Consensus/UT_Consensus.cs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index c8d8fbf6c4..6ed37700a8 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -177,36 +177,31 @@ public void TestSerializeAndDeserializeConsensusContext() consensusContext.Transactions = txs.ToDictionary(p => p.Hash); consensusContext.PreparationPayloads = new ConsensusPayload[consensusContext.Validators.Length]; - var prepareRequestMessage = new PrepareRequest - { - TransactionHashes = consensusContext.TransactionHashes, - Timestamp = 23 - }; - consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, prepareRequestMessage, 6, new[] { (byte)'3', (byte)'!' }); - consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 0, new[] { (byte)'t', (byte)'e' }); - consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 1, new[] { (byte)'s', (byte)'t' }); + consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, new PrepareRequest { TransactionHashes = consensusContext.TransactionHashes, Timestamp = 23, ViewNumber = 2 }, 6, new[] { (byte)'3', (byte)'!' }); + consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 0, new[] { (byte)'t', (byte)'e' }); + consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 1, new[] { (byte)'s', (byte)'t' }); consensusContext.PreparationPayloads[2] = null; - consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 3, new[] { (byte)'1', (byte)'2' }); + consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 3, new[] { (byte)'1', (byte)'2' }); consensusContext.PreparationPayloads[4] = null; consensusContext.PreparationPayloads[5] = null; consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length]; using (SHA256 sha256 = SHA256.Create()) { - consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()) }, 3, new[] { (byte)'3', (byte)'4' }); - consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()) }, 3, new[] { (byte)'6', (byte)'7' }); + consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'3', (byte)'4' }); + consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'6', (byte)'7' }); } consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestampMS(); consensusContext.ChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; - consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 6 }, 0, new[] { (byte)'A' }); - consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 5 }, 1, new[] { (byte)'B' }); + consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 6 }, 0, new[] { (byte)'A' }); + consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 5 }, 1, new[] { (byte)'B' }); consensusContext.ChangeViewPayloads[2] = null; - consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' }); + consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' }); consensusContext.ChangeViewPayloads[4] = null; consensusContext.ChangeViewPayloads[5] = null; - consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 1 }, 6, new[] { (byte)'D' }); + consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 1 }, 6, new[] { (byte)'D' }); consensusContext.LastChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; From 07b0bef6a844d9c79c72dd951649b829a8b40f22 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 15 Nov 2019 15:38:46 +0100 Subject: [PATCH 10/10] Revert ConsensusContext changes --- neo.UnitTests/Consensus/UT_Consensus.cs | 25 ++++++++++++++---------- neo/Consensus/ConsensusContext.cs | 26 ++++--------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index 6ed37700a8..c8d8fbf6c4 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -177,31 +177,36 @@ public void TestSerializeAndDeserializeConsensusContext() consensusContext.Transactions = txs.ToDictionary(p => p.Hash); consensusContext.PreparationPayloads = new ConsensusPayload[consensusContext.Validators.Length]; - consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, new PrepareRequest { TransactionHashes = consensusContext.TransactionHashes, Timestamp = 23, ViewNumber = 2 }, 6, new[] { (byte)'3', (byte)'!' }); - consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 0, new[] { (byte)'t', (byte)'e' }); - consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 1, new[] { (byte)'s', (byte)'t' }); + var prepareRequestMessage = new PrepareRequest + { + TransactionHashes = consensusContext.TransactionHashes, + Timestamp = 23 + }; + consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, prepareRequestMessage, 6, new[] { (byte)'3', (byte)'!' }); + consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 0, new[] { (byte)'t', (byte)'e' }); + consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 1, new[] { (byte)'s', (byte)'t' }); consensusContext.PreparationPayloads[2] = null; - consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash, ViewNumber = 2 }, 3, new[] { (byte)'1', (byte)'2' }); + consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse { PreparationHash = consensusContext.PreparationPayloads[6].Hash }, 3, new[] { (byte)'1', (byte)'2' }); consensusContext.PreparationPayloads[4] = null; consensusContext.PreparationPayloads[5] = null; consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length]; using (SHA256 sha256 = SHA256.Create()) { - consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'3', (byte)'4' }); - consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()), ViewNumber = 2 }, 3, new[] { (byte)'6', (byte)'7' }); + consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()) }, 3, new[] { (byte)'3', (byte)'4' }); + consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()) }, 3, new[] { (byte)'6', (byte)'7' }); } consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestampMS(); consensusContext.ChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; - consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 6 }, 0, new[] { (byte)'A' }); - consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 5 }, 1, new[] { (byte)'B' }); + consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 6 }, 0, new[] { (byte)'A' }); + consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 5 }, 1, new[] { (byte)'B' }); consensusContext.ChangeViewPayloads[2] = null; - consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' }); + consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = uint.MaxValue }, 3, new[] { (byte)'C' }); consensusContext.ChangeViewPayloads[4] = null; consensusContext.ChangeViewPayloads[5] = null; - consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 2, Timestamp = 1 }, 6, new[] { (byte)'D' }); + consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView { ViewNumber = 1, Timestamp = 1 }, 6, new[] { (byte)'D' }); consensusContext.LastChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length]; diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index b104cf9386..d05b4a64ea 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -109,10 +109,10 @@ public void Deserialize(BinaryReader reader) ViewNumber = reader.ReadByte(); TransactionHashes = reader.ReadSerializableArray(); Transaction[] transactions = reader.ReadSerializableArray(Block.MaxTransactionsPerBlock); - PreparationPayloads = ReadConsensusPayload(reader, ViewNumber); - CommitPayloads = ReadConsensusPayload(reader, ViewNumber); - ChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber); - LastChangeViewPayloads = ReadConsensusPayload(reader, ViewNumber); + PreparationPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); + CommitPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); + ChangeViewPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); + LastChangeViewPayloads = reader.ReadNullableArray(Blockchain.MaxValidators); if (TransactionHashes.Length == 0 && !RequestSentOrReceived) TransactionHashes = null; Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); @@ -124,24 +124,6 @@ public void Deserialize(BinaryReader reader) } } - private static ConsensusPayload[] ReadConsensusPayload(BinaryReader reader, byte viewNumber) - { - // Read - var payloads = reader.ReadNullableArray(Blockchain.MaxValidators); - - // Remove other views - for (int x = 0; x < payloads.Length; x++) - { - var p = payloads[x]; - if (p != null && p.ConsensusMessage.ViewNumber != viewNumber) - { - payloads[x] = null; - } - } - - return payloads; - } - public void Dispose() { Snapshot?.Dispose();