Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Transaction.Size #1282

Merged
merged 18 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 104 additions & 29 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using Neo.Wallets;
using System;
Expand All @@ -28,22 +27,36 @@ public class Transaction : IEquatable<Transaction>, IInventory, IInteroperable
/// </summary>
private const int MaxCosigners = 16;

public byte Version;
public uint Nonce;
public UInt160 Sender;
/// <summary>
/// Distributed to NEO holders.
/// </summary>
public long SystemFee;
/// <summary>
/// Distributed to consensus nodes.
/// </summary>
public long NetworkFee;
public uint ValidUntilBlock;
public TransactionAttribute[] Attributes;
public Cosigner[] Cosigners { get; set; }
public byte[] Script;
public Witness[] Witnesses { get; set; }
private byte version;
private uint nonce;
private UInt160 sender;
private long sysfee;
private long netfee;
private uint validUntilBlock;
private TransactionAttribute[] attributes;
private Cosigner[] cosigners;
private byte[] script;
private Witness[] witnesses;

public const int HeaderSize =
sizeof(byte) + //Version
sizeof(uint) + //Nonce
20 + //Sender
sizeof(long) + //SystemFee
sizeof(long) + //NetworkFee
sizeof(uint); //ValidUntilBlock

public TransactionAttribute[] Attributes
{
get => attributes;
set { attributes = value; _hash = null; _size = 0; }
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
}

public Cosigner[] Cosigners
{
get => cosigners;
set { cosigners = value; _hash = null; _size = 0; }
}

/// <summary>
/// The <c>NetworkFee</c> for the transaction divided by its <c>Size</c>.
Expand All @@ -66,24 +79,86 @@ public UInt256 Hash

InventoryType IInventory.InventoryType => InventoryType.TX;

public const int HeaderSize =
sizeof(byte) + //Version
sizeof(uint) + //Nonce
20 + //Sender
sizeof(long) + //SystemFee
sizeof(long) + //NetworkFee
sizeof(uint); //ValidUntilBlock
/// <summary>
/// Distributed to consensus nodes.
/// </summary>
public long NetworkFee
{
get => netfee;
set { netfee = value; _hash = null; }
}

public int Size => HeaderSize +
Attributes.GetVarSize() + //Attributes
Cosigners.GetVarSize() + //Cosigners
Script.GetVarSize() + //Script
Witnesses.GetVarSize(); //Witnesses
public uint Nonce
{
get => nonce;
set { nonce = value; _hash = null; }
}

public byte[] Script
{
get => script;
set { script = value; _hash = null; _size = 0; }
}

public UInt160 Sender
{
get => sender;
set { sender = value; _hash = null; }
}

private int _size;
public int Size
{
get
{
if (_size == 0)
{
_size = HeaderSize +
Attributes.GetVarSize() + //Attributes
Cosigners.GetVarSize() + //Cosigners
Script.GetVarSize() + //Script
Witnesses.GetVarSize(); //Witnesses
}
return _size;
}
}

/// <summary>
/// Distributed to NEO holders.
/// </summary>
public long SystemFee
{
get => sysfee;
set { sysfee = value; _hash = null; }
}

public uint ValidUntilBlock
{
get => validUntilBlock;
set { validUntilBlock = value; _hash = null; }
}

public byte Version
{
get => version;
set { version = value; _hash = null; }
}

public Witness[] Witnesses
{
get => witnesses;
set { witnesses = value; _size = 0; }
}

void ISerializable.Deserialize(BinaryReader reader)
{
int startPosition = -1;
if (reader.BaseStream.CanSeek)
startPosition = (int)reader.BaseStream.Position;
DeserializeUnsigned(reader);
Witnesses = reader.ReadSerializableArray<Witness>();
if (startPosition >= 0)
_size = (int)reader.BaseStream.Position - startPosition;
}

public void DeserializeUnsigned(BinaryReader reader)
Expand Down
12 changes: 6 additions & 6 deletions tests/neo.UnitTests/Ledger/UT_PoolItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public void PoolItem_CompareTo_Hash()
PoolItem pitem1 = new PoolItem(tx1);
PoolItem pitem2 = new PoolItem(tx2);

// pitem2 < pitem1 (fee) => -1
pitem2.CompareTo(pitem1).Should().Be(-1);
// pitem2.tx.Hash < pitem1.tx.Hash => 1 descending order
pitem2.CompareTo(pitem1).Should().Be(1);

// pitem1 > pitem2 (fee) => 1
pitem1.CompareTo(pitem2).Should().Be(1);
// pitem2.tx.Hash > pitem1.tx.Hash => -1 descending order
pitem1.CompareTo(pitem2).Should().Be(-1);
}
}

Expand All @@ -96,7 +96,7 @@ public Transaction GenerateTxWithFirstByteOfHashGreaterThanOrEqualTo(byte firstH
do
{
tx = GenerateTx(networkFee, size);
} while (tx.Hash >= new UInt256(TestUtils.GetByteArray(32, firstHashByte)));
} while (tx.Hash < new UInt256(TestUtils.GetByteArray(32, firstHashByte)));

shargon marked this conversation as resolved.
Show resolved Hide resolved
return tx;
}
Expand All @@ -107,7 +107,7 @@ public Transaction GenerateTxWithFirstByteOfHashLessThanOrEqualTo(byte firstHash
do
{
tx = GenerateTx(networkFee, size);
} while (tx.Hash <= new UInt256(TestUtils.GetByteArray(32, firstHashByte)));
} while (tx.Hash > new UInt256(TestUtils.GetByteArray(32, firstHashByte)));

return tx;
}
Expand Down