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 7 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
13 changes: 12 additions & 1 deletion src/neo/Ledger/PoolItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ internal class PoolItem : IComparable<PoolItem>
/// </summary>
public DateTime LastBroadcastTimestamp;

/// <summary>
/// Cache Tx's FeePerByte to avoid recompute when comparing
/// </summary>
public long TxFeePerByteCache;
eryeer marked this conversation as resolved.
Show resolved Hide resolved

internal PoolItem(Transaction tx)
{
Tx = tx;
Timestamp = TimeProvider.Current.UtcNow;
LastBroadcastTimestamp = Timestamp;
TxFeePerByteCache = tx.FeePerByte;
}

public int CompareTo(Transaction otherTx)
Expand All @@ -48,7 +54,12 @@ public int CompareTo(Transaction otherTx)
public int CompareTo(PoolItem otherItem)
{
if (otherItem == null) return 1;
return CompareTo(otherItem.Tx);
if (otherItem.Tx == null) return 1;
int ret = TxFeePerByteCache.CompareTo(otherItem.TxFeePerByteCache);
if (ret != 0) return ret;
ret = Tx.NetworkFee.CompareTo(otherItem.Tx.NetworkFee);
if (ret != 0) return ret;
return otherItem.Tx.Hash.CompareTo(Tx.Hash);
}
}
}
22 changes: 17 additions & 5 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,28 @@ public UInt256 Hash
sizeof(long) + //NetworkFee
sizeof(uint); //ValidUntilBlock

public int Size => HeaderSize +
Attributes.GetVarSize() + //Attributes
Cosigners.GetVarSize() + //Cosigners
Script.GetVarSize() + //Script
Witnesses.GetVarSize(); //Witnesses
/// <summary>
/// Assigned when tx is deserialized
/// </summary>
private int _size;
public int Size
{
get
{
return _size == 0 ? HeaderSize +
Attributes.GetVarSize() + //Attributes
Cosigners.GetVarSize() + //Cosigners
Script.GetVarSize() + //Script
Witnesses.GetVarSize() //Witnesses
: _size;
}
}

void ISerializable.Deserialize(BinaryReader reader)
{
DeserializeUnsigned(reader);
Witnesses = reader.ReadSerializableArray<Witness>();
_size = (int)reader.BaseStream.Position;
eryeer marked this conversation as resolved.
Show resolved Hide resolved
}

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