From de247db7826eb458e51470db3d8a232a5efb1f1e Mon Sep 17 00:00:00 2001 From: erikzhang Date: Tue, 3 Sep 2019 15:17:06 +0800 Subject: [PATCH 1/4] Fix consensus --- neo/Consensus/ConsensusContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index f96409318a..e2199e091b 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -103,10 +103,10 @@ public void Deserialize(BinaryReader reader) Block.ConsensusData = reader.ReadSerializable(); ViewNumber = reader.ReadByte(); TransactionHashes = reader.ReadSerializableArray(); - if (TransactionHashes.Length == 0) + if (TransactionHashes.Length == 0 && !RequestSentOrReceived) TransactionHashes = null; Transaction[] transactions = reader.ReadSerializableArray(Block.MaxTransactionsPerBlock); - Transactions = transactions.Length == 0 ? null : transactions.ToDictionary(p => p.Hash); + Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); PreparationPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)]; for (int i = 0; i < PreparationPayloads.Length; i++) PreparationPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable() : null; From f41c3d6939eec0f2d2fa360b3d89b6792fb6990b Mon Sep 17 00:00:00 2001 From: erikzhang Date: Tue, 3 Sep 2019 21:56:24 +0800 Subject: [PATCH 2/4] More fix --- neo/Consensus/ConsensusContext.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index e2199e091b..147f1e5a40 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -103,10 +103,7 @@ public void Deserialize(BinaryReader reader) Block.ConsensusData = reader.ReadSerializable(); ViewNumber = reader.ReadByte(); TransactionHashes = reader.ReadSerializableArray(); - if (TransactionHashes.Length == 0 && !RequestSentOrReceived) - TransactionHashes = null; Transaction[] transactions = reader.ReadSerializableArray(Block.MaxTransactionsPerBlock); - Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); PreparationPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)]; for (int i = 0; i < PreparationPayloads.Length; i++) PreparationPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable() : null; @@ -119,6 +116,9 @@ public void Deserialize(BinaryReader reader) LastChangeViewPayloads = new ConsensusPayload[reader.ReadVarInt(Blockchain.MaxValidators)]; for (int i = 0; i < LastChangeViewPayloads.Length; i++) LastChangeViewPayloads[i] = reader.ReadBoolean() ? reader.ReadSerializable() : null; + if (TransactionHashes.Length == 0 && !RequestSentOrReceived) + TransactionHashes = null; + Transactions = transactions.Length == 0 && !RequestSentOrReceived ? null : transactions.ToDictionary(p => p.Hash); } public void Dispose() From a561e3e98d9fe8bb3c0e1ed9a27a386a32a5dc53 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 5 Sep 2019 19:07:25 +0800 Subject: [PATCH 3/4] Fix consensus when changing view --- neo/Consensus/ConsensusContext.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index 147f1e5a40..0d9edbb3dc 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -340,8 +340,7 @@ public void Reset(byte viewNumber) { PrevHash = Snapshot.CurrentBlockHash, Index = Snapshot.Height + 1, - NextConsensus = Blockchain.GetConsensusAddress(NativeContract.NEO.GetValidators(Snapshot).ToArray()), - ConsensusData = new ConsensusData() + NextConsensus = Blockchain.GetConsensusAddress(NativeContract.NEO.GetValidators(Snapshot).ToArray()) }; var pv = Validators; Validators = NativeContract.NEO.GetNextBlockValidators(Snapshot); @@ -390,7 +389,10 @@ public void Reset(byte viewNumber) LastChangeViewPayloads[i] = null; } ViewNumber = viewNumber; - Block.ConsensusData.PrimaryIndex = GetPrimaryIndex(viewNumber); + Block.ConsensusData = new ConsensusData + { + PrimaryIndex = GetPrimaryIndex(viewNumber) + }; Block.MerkleRoot = null; Block.Timestamp = 0; Block.Transactions = null; From 967f4f5b58436fa37222813b836ec65730513eea Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 10 Sep 2019 08:47:43 +0200 Subject: [PATCH 4/4] Add changeView reason to logs --- 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 4d8b70a333..a65916b5aa 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -202,7 +202,7 @@ private void OnChangeViewReceived(ConsensusPayload payload, ChangeView message) if (message.NewViewNumber <= expectedView) return; - Log($"{nameof(OnChangeViewReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} nv={message.NewViewNumber}"); + Log($"{nameof(OnChangeViewReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} nv={message.NewViewNumber} reason={message.Reason}"); context.ChangeViewPayloads[payload.ValidatorIndex] = payload; CheckExpectedView(message.NewViewNumber); }