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

Transaction optimization #2290

Merged
merged 9 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 5 additions & 6 deletions src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ void ISerializable.Deserialize(BinaryReader reader)
if (reader.BaseStream.CanSeek)
startPosition = (int)reader.BaseStream.Position;
DeserializeUnsigned(reader);
Witnesses = reader.ReadSerializableArray<Witness>();
Witnesses = reader.ReadSerializableArray<Witness>(Signers.Length);
if (Witnesses.Length != Signers.Length) throw new FormatException();
if (startPosition >= 0)
_size = (int)reader.BaseStream.Position - startPosition;
}
Expand Down Expand Up @@ -287,7 +288,8 @@ public virtual VerifyResult VerifyStateDependent(DataCache snapshot, Transaction
uint height = NativeContract.Ledger.CurrentIndex(snapshot);
if (ValidUntilBlock <= height || ValidUntilBlock > height + MaxValidUntilBlockIncrement)
return VerifyResult.Expired;
foreach (UInt160 hash in GetScriptHashesForVerifying(snapshot))
UInt160[] hashes = GetScriptHashesForVerifying(snapshot);
foreach (UInt160 hash in hashes)
if (NativeContract.Policy.IsBlocked(snapshot, hash))
return VerifyResult.PolicyFail;
if (NativeContract.Policy.GetMaxBlockSystemFee(snapshot) < SystemFee)
Expand All @@ -297,9 +299,7 @@ public virtual VerifyResult VerifyStateDependent(DataCache snapshot, Transaction
if (!attribute.Verify(snapshot, this))
return VerifyResult.Invalid;
long net_fee = NetworkFee - Size * NativeContract.Policy.GetFeePerByte(snapshot);

UInt160[] hashes = GetScriptHashesForVerifying(snapshot);
if (hashes.Length != witnesses.Length) return VerifyResult.Invalid;
if (net_fee < 0) return VerifyResult.InsufficientFunds;

uint execFeeFactor = NativeContract.Policy.GetExecFeeFactor(snapshot);
for (int i = 0; i < hashes.Length; i++)
Expand Down Expand Up @@ -331,7 +331,6 @@ public virtual VerifyResult VerifyStateIndependent()
return VerifyResult.Invalid;
}
UInt160[] hashes = GetScriptHashesForVerifying(null);
if (hashes.Length != witnesses.Length) return VerifyResult.Invalid;
for (int i = 0; i < hashes.Length; i++)
if (witnesses[i].VerificationScript.IsStandardContract())
if (!this.VerifyWitness(null, hashes[i], witnesses[i], SmartContract.Helper.MaxVerificationGas, out _))
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ private Transaction CreateTransactionWithFee(long fee)
{
new Witness
{
InvocationScript = new byte[0],
VerificationScript = new byte[0]
InvocationScript = Array.Empty<byte>(),
VerificationScript = Array.Empty<byte>()
}
};
return mock.Object;
Expand Down
56 changes: 36 additions & 20 deletions tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public void Transaction_Serialize_Deserialize_Simple()
Signers = new Signer[] { new Signer() { Account = UInt160.Zero } },
Attributes = Array.Empty<TransactionAttribute>(),
Script = new byte[] { (byte)OpCode.PUSH1 },
Witnesses = new Witness[0] { }
Witnesses = new Witness[] { new Witness() { InvocationScript = new byte[0], VerificationScript = Array.Empty<byte>() } }
};

byte[] sTx = txSimple.ToArray();
Expand All @@ -803,7 +803,7 @@ public void Transaction_Serialize_Deserialize_Simple()
"01000000000000000000000000000000000000000000" + // empty signer
"00" + // no attributes
"0111" + // push1 script
"00"); // no witnesses
"010000"); // empty witnesses

// try to deserialize
Transaction tx2 = Neo.IO.Helper.AsSerializable<Transaction>(sTx);
Expand All @@ -824,7 +824,7 @@ public void Transaction_Serialize_Deserialize_Simple()
}
);
tx2.Script.Should().BeEquivalentTo(new byte[] { (byte)OpCode.PUSH1 });
tx2.Witnesses.Should().BeEquivalentTo(new Witness[0] { });
tx2.Witnesses.Should().BeEquivalentTo(new Witness[] { new Witness() { InvocationScript = new byte[0], VerificationScript = Array.Empty<byte>() } });
}

[TestMethod]
Expand Down Expand Up @@ -854,13 +854,13 @@ public void Transaction_Serialize_Deserialize_DistinctCosigners()
}
},
Script = new byte[] { (byte)OpCode.PUSH1 },
Witnesses = new Witness[0] { }
Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty<byte>(), VerificationScript = Array.Empty<byte>() } }
};

byte[] sTx = txDoubleCosigners.ToArray();

// no need for detailed hexstring here (see basic tests for it)
sTx.ToHexString().Should().Be("000403020100e1f505000000000100000000000000040302010209080706050403020100090807060504030201008009080706050403020100090807060504030201000100011100");
sTx.ToHexString().Should().Be("000403020100e1f5050000000001000000000000000403020102090807060504030201000908070605040302010080090807060504030201000908070605040302010001000111010000");

// back to transaction (should fail, due to non-distinct cosigners)
Transaction tx2 = null;
Expand Down Expand Up @@ -904,14 +904,13 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners()
Attributes = Array.Empty<TransactionAttribute>(),
Signers = cosigners1, // max + 1 (should fail)
Script = new byte[] { (byte)OpCode.PUSH1 },
Witnesses = new Witness[0] { }
Witnesses = new Witness[] { new Witness() { InvocationScript = new byte[0], VerificationScript = Array.Empty<byte>() } }
};

byte[] sTx1 = txCosigners1.ToArray();

// back to transaction (should fail, due to non-distinct cosigners)
Transaction tx1 = Neo.IO.Helper.AsSerializable<Transaction>(sTx1);
Assert.IsNotNull(tx1);
Assert.ThrowsException<FormatException>(() => Neo.IO.Helper.AsSerializable<Transaction>(sTx1));

// ----------------------------
// this should fail (max + 1)
Expand All @@ -938,7 +937,7 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners()
Attributes = Array.Empty<TransactionAttribute>(),
Signers = cosigners, // max + 1 (should fail)
Script = new byte[] { (byte)OpCode.PUSH1 },
Witnesses = new Witness[0] { }
Witnesses = new Witness[] { new Witness() { InvocationScript = new byte[0], VerificationScript = Array.Empty<byte>() } }
};

byte[] sTx2 = txCosigners.ToArray();
Expand Down Expand Up @@ -1110,11 +1109,18 @@ public void Test_VerifyStateIndependent()
SystemFee = 0,
ValidUntilBlock = 0,
Version = 0,
Witnesses = new Witness[0],
Witnesses = new[]
{
new Witness
{
InvocationScript = Array.Empty<byte>(),
VerificationScript = Array.Empty<byte>()
}
}
};
tx.VerifyStateIndependent().Should().Be(VerifyResult.Invalid);
tx.Script = new byte[0];
tx.VerifyStateIndependent().Should().Be(VerifyResult.Invalid);
tx.VerifyStateIndependent().Should().Be(VerifyResult.Succeed);

var walletA = TestUtils.GenerateTestWallet();
var walletB = TestUtils.GenerateTestWallet();
Expand Down Expand Up @@ -1177,16 +1183,27 @@ public void Test_VerifyStateDependent()
var tx = new Transaction()
{
Attributes = Array.Empty<TransactionAttribute>(),
NetworkFee = 0,
NetworkFee = 55000,
Nonce = (uint)Environment.TickCount,
Script = new byte[0],
Script = Array.Empty<byte>(),
Signers = new Signer[] { new Signer() { Account = UInt160.Zero } },
SystemFee = 0,
ValidUntilBlock = height + 1,
Version = 0,
Witnesses = new Witness[0],
Witnesses = new Witness[] {
new Witness() { InvocationScript = Array.Empty<byte>(), VerificationScript = new byte[0] },
new Witness() { InvocationScript = Array.Empty<byte>(), VerificationScript = new byte[1] }
}
};

// Fake balance

var key = NativeContract.GAS.CreateStorageKey(20, tx.Sender);
var balance = snapshot.GetAndChange(key, () => new StorageItem(new AccountState()));
balance.GetInteroperable<AccountState>().Balance = tx.NetworkFee;

tx.VerifyStateDependent(snapshot, new TransactionVerificationContext()).Should().Be(VerifyResult.Invalid);
balance.GetInteroperable<AccountState>().Balance = 0;
tx.SystemFee = 10;
tx.VerifyStateDependent(snapshot, new TransactionVerificationContext()).Should().Be(VerifyResult.InsufficientFunds);

Expand All @@ -1211,15 +1228,14 @@ public void Test_VerifyStateDependent()

// Fake balance

var key = NativeContract.GAS.CreateStorageKey(20, acc.ScriptHash);
var entry = snapshot.GetAndChange(key, () => new StorageItem(new AccountState()));

entry.GetInteroperable<AccountState>().Balance = 10000 * NativeContract.GAS.Factor;

snapshot.Commit();
snapshot = Blockchain.Singleton.GetSnapshot();
key = NativeContract.GAS.CreateStorageKey(20, acc.ScriptHash);
balance = snapshot.GetAndChange(key, () => new StorageItem(new AccountState()));
balance.GetInteroperable<AccountState>().Balance = 10000 * NativeContract.GAS.Factor;

// Make transaction

snapshot.Commit();
tx = walletA.MakeTransaction(new TransferOutput[]
{
new TransferOutput()
Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/Network/P2P/UT_Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ public void Compression()
{
Nonce = 1,
Version = 0,
Attributes = new TransactionAttribute[0],
Attributes = Array.Empty<TransactionAttribute>(),
Script = new byte[] { (byte)OpCode.PUSH1 },
Signers = new Signer[] { new Signer() { Account = UInt160.Zero } },
Witnesses = new Witness[0],
Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty<byte>(), VerificationScript = new byte[0] } },
};

var msg = Message.Create(MessageCommand.Transaction, payload);
var buffer = msg.ToArray();

buffer.Length.Should().Be(54);
buffer.Length.Should().Be(56);

payload.Script = new byte[100];
for (int i = 0; i < payload.Script.Length; i++) payload.Script[i] = (byte)OpCode.PUSH2;
Expand Down