Skip to content

Commit

Permalink
Cache Transaction.Size (neo-project#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryeer authored and Luchuan committed Jan 10, 2020
1 parent 85fc308 commit 92c9559
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 35 deletions.
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; }
}

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)));

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

0 comments on commit 92c9559

Please sign in to comment.