From 98e6d1c127a5b09b4dcfa6a809d49ad98eabad65 Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 22 Jul 2019 12:03:41 +0200 Subject: [PATCH] Revert "Remove minner tx hole (#934)" (#944) This reverts commit 5b321fbcf97f9fff43980d9e0108c1cbf374968c. --- neo.UnitTests/UT_Block.cs | 27 +++++++++++++++++++-------- neo/Network/P2P/Payloads/Block.cs | 13 ++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/neo.UnitTests/UT_Block.cs b/neo.UnitTests/UT_Block.cs index f6b9218070..886f5099f0 100644 --- a/neo.UnitTests/UT_Block.cs +++ b/neo.UnitTests/UT_Block.cs @@ -1,6 +1,5 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Neo.IO; using Neo.IO.Json; using Neo.Network.P2P.Payloads; using System.IO; @@ -87,13 +86,21 @@ public void Serialize() byte[] data; using (MemoryStream stream = new MemoryStream()) - using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII, true)) { - uut.Serialize(writer); - data = stream.ToArray(); + using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII, true)) + { + uut.Serialize(writer); + data = stream.ToArray(); + } } - Assert.AreEqual(data.ToHexString(), "0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d880ab04fd0000000000000000000000000000000000000000000000000100015101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"); + byte[] requiredData = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; + + data.Length.Should().Be(requiredData.Length); + for (int i = 0; i < data.Length; i++) + { + data[i].Should().Be(requiredData[i]); + } } [TestMethod] @@ -104,10 +111,14 @@ public void Deserialize() uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid - using (MemoryStream ms = new MemoryStream("0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d880ab04fd0000000000000000000000000000000000000000000000000100015101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000".HexToBytes(), false)) - using (BinaryReader reader = new BinaryReader(ms)) + byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 41, 176, 215, 72, 169, 204, 248, 197, 175, 60, 222, 16, 219, 62, 54, 236, 154, 95, 114, 6, 67, 162, 188, 180, 173, 215, 107, 61, 175, 65, 216, 128, 171, 4, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 81, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }; + int index = 0; + using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false)) { - uut.Deserialize(reader); + using (BinaryReader reader = new BinaryReader(ms)) + { + uut.Deserialize(reader); + } } assertStandardBlockTestVals(val256, merkRoot, val160, timestampVal, indexVal, scriptVal, transactionsVal); diff --git a/neo/Network/P2P/Payloads/Block.cs b/neo/Network/P2P/Payloads/Block.cs index b63035e0ed..58faf54cb1 100644 --- a/neo/Network/P2P/Payloads/Block.cs +++ b/neo/Network/P2P/Payloads/Block.cs @@ -12,7 +12,7 @@ namespace Neo.Network.P2P.Payloads public class Block : BlockBase, IInventory, IEquatable { public const int MaxContentsPerBlock = ushort.MaxValue; - public const int MaxTransactionsPerBlock = MaxContentsPerBlock; + public const int MaxTransactionsPerBlock = MaxContentsPerBlock - 1; public ConsensusData ConsensusData; public Transaction[] Transactions; @@ -41,13 +41,13 @@ public Header Header InventoryType IInventory.InventoryType => InventoryType.Block; public override int Size => base.Size - + IO.Helper.GetVarSize(Transactions.Length) //Count + + IO.Helper.GetVarSize(Transactions.Length + 1) //Count + ConsensusData.Size //ConsensusData + Transactions.Sum(p => p.Size); //Transactions public static UInt256 CalculateMerkleRoot(UInt256 consensusDataHash, params UInt256[] transactionHashes) { - List hashes = new List(transactionHashes.Length) { consensusDataHash }; + List hashes = new List(transactionHashes.Length + 1) { consensusDataHash }; hashes.AddRange(transactionHashes); return MerkleTree.ComputeRoot(hashes); } @@ -58,7 +58,7 @@ public override void Deserialize(BinaryReader reader) int count = (int)reader.ReadVarInt(MaxContentsPerBlock); if (count == 0) throw new FormatException(); ConsensusData = reader.ReadSerializable(); - Transactions = new Transaction[count]; + Transactions = new Transaction[count - 1]; for (int i = 0; i < Transactions.Length; i++) Transactions[i] = reader.ReadSerializable(); if (Transactions.Distinct().Count() != Transactions.Length) @@ -76,8 +76,7 @@ public bool Equals(Block other) public override bool Equals(object obj) { - if (!(obj is Block b)) return false; - return Equals(b); + return Equals(obj as Block); } public override int GetHashCode() @@ -93,7 +92,7 @@ public void RebuildMerkleRoot() public override void Serialize(BinaryWriter writer) { base.Serialize(writer); - writer.WriteVarInt(Transactions.Length); + writer.WriteVarInt(Transactions.Length + 1); writer.Write(ConsensusData); foreach (Transaction tx in Transactions) writer.Write(tx);