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 10 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
23 changes: 18 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,29 @@ 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)
{
var startPosition = (int)reader.BaseStream.Position;
DeserializeUnsigned(reader);
Witnesses = reader.ReadSerializableArray<Witness>();
_size = (int)reader.BaseStream.Position - startPosition;
erikzhang 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