Skip to content

Commit

Permalink
Revert "Remove minner tx hole (#934)" (#944)
Browse files Browse the repository at this point in the history
This reverts commit 5b321fb.
  • Loading branch information
shargon authored and erikzhang committed Jul 22, 2019
1 parent 5b321fb commit 98e6d1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
27 changes: 19 additions & 8 deletions neo.UnitTests/UT_Block.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]
Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 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;
public const int MaxTransactionsPerBlock = MaxContentsPerBlock - 1;

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

0 comments on commit 98e6d1c

Please sign in to comment.