Skip to content

Commit

Permalink
Remove minner tx hole (#934)
Browse files Browse the repository at this point in the history
* Remove minner tx hole

* Fix equals
  • Loading branch information
shargon authored and vncoelho committed Jul 22, 2019
1 parent 3e7bee4 commit 5b321fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
27 changes: 8 additions & 19 deletions neo.UnitTests/UT_Block.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]
Expand All @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions neo/Network/P2P/Payloads/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Neo.Network.P2P.Payloads
public class Block : BlockBase, IInventory, IEquatable<Block>
{
public const int MaxContentsPerBlock = ushort.MaxValue;
public const int MaxTransactionsPerBlock = MaxContentsPerBlock - 1;
public const int MaxTransactionsPerBlock = MaxContentsPerBlock;

public ConsensusData ConsensusData;
public Transaction[] Transactions;
Expand Down Expand Up @@ -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<UInt256> hashes = new List<UInt256>(transactionHashes.Length + 1) { consensusDataHash };
List<UInt256> hashes = new List<UInt256>(transactionHashes.Length) { consensusDataHash };
hashes.AddRange(transactionHashes);
return MerkleTree.ComputeRoot(hashes);
}
Expand All @@ -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<ConsensusData>();
Transactions = new Transaction[count - 1];
Transactions = new Transaction[count];
for (int i = 0; i < Transactions.Length; i++)
Transactions[i] = reader.ReadSerializable<Transaction>();
if (Transactions.Distinct().Count() != Transactions.Length)
Expand All @@ -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()
Expand All @@ -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);
Expand Down

0 comments on commit 5b321fb

Please sign in to comment.