diff --git a/neo.UnitTests/UT_Block.cs b/neo.UnitTests/UT_Block.cs index 886f5099f0..f6b9218070 100644 --- a/neo.UnitTests/UT_Block.cs +++ b/neo.UnitTests/UT_Block.cs @@ -1,5 +1,6 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.IO; using Neo.IO.Json; using Neo.Network.P2P.Payloads; using System.IO; @@ -86,21 +87,13 @@ public void Serialize() byte[] data; using (MemoryStream stream = new MemoryStream()) + using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII, true)) { - using (BinaryWriter writer = new BinaryWriter(stream, Encoding.ASCII, true)) - { - uut.Serialize(writer); - data = stream.ToArray(); - } + uut.Serialize(writer); + data = stream.ToArray(); } - 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]); - } + Assert.AreEqual(data.ToHexString(), "0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d880ab04fd0000000000000000000000000000000000000000000000000100015101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000"); } [TestMethod] @@ -111,14 +104,10 @@ public void Deserialize() uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid - 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)) + using (MemoryStream ms = new MemoryStream("0000000000000000000000000000000000000000000000000000000000000000000000000f29b0d748a9ccf8c5af3cde10db3e36ec9a5f720643a2bcb4add76b3daf41d880ab04fd0000000000000000000000000000000000000000000000000100015101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000".HexToBytes(), false)) + using (BinaryReader reader = new BinaryReader(ms)) { - using (BinaryReader reader = new BinaryReader(ms)) - { - uut.Deserialize(reader); - } + 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 58faf54cb1..b63035e0ed 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 - 1; + public const int MaxTransactionsPerBlock = MaxContentsPerBlock; 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 + 1) //Count + + IO.Helper.GetVarSize(Transactions.Length) //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 + 1) { consensusDataHash }; + List hashes = new List(transactionHashes.Length) { 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 - 1]; + Transactions = new Transaction[count]; for (int i = 0; i < Transactions.Length; i++) Transactions[i] = reader.ReadSerializable(); if (Transactions.Distinct().Count() != Transactions.Length) @@ -76,7 +76,8 @@ public bool Equals(Block other) public override bool Equals(object obj) { - return Equals(obj as Block); + if (!(obj is Block b)) return false; + return Equals(b); } public override int GetHashCode() @@ -92,7 +93,7 @@ public void RebuildMerkleRoot() public override void Serialize(BinaryWriter writer) { base.Serialize(writer); - writer.WriteVarInt(Transactions.Length + 1); + writer.WriteVarInt(Transactions.Length); writer.Write(ConsensusData); foreach (Transaction tx in Transactions) writer.Write(tx);