Skip to content

Commit

Permalink
Optimize ApplicationEngine.LoadScript() (neo-project#2186)
Browse files Browse the repository at this point in the history
Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
2 people authored and Shawn committed Jan 8, 2021
1 parent 0488f42 commit 603b79b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
13 changes: 7 additions & 6 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ public ExecutionContext LoadContract(ContractState contract, string method, Call
pcount: pcount,
rvcount: hasReturnValue ? 1 : 0,
initialPosition: md.Offset,
callFlags: callFlags,
scriptHash: contract.Hash);
configureState: p =>
{
p.CallFlags = callFlags;
p.ScriptHash = contract.Hash;
});

// Call initialization
var init = contract.Manifest.Abi.GetMethod("_initialize");
Expand All @@ -130,13 +133,11 @@ public ExecutionContext LoadContract(ContractState contract, string method, Call
return context;
}

public ExecutionContext LoadScript(Script script, ushort pcount = 0, int rvcount = -1, int initialPosition = 0, CallFlags callFlags = CallFlags.All, UInt160 scriptHash = null)
public ExecutionContext LoadScript(Script script, ushort pcount = 0, int rvcount = -1, int initialPosition = 0, Action<ExecutionContextState> configureState = null)
{
// Create and configure context
ExecutionContext context = CreateContext(script, pcount, rvcount, initialPosition);
var state = context.GetState<ExecutionContextState>();
state.CallFlags = callFlags;
state.ScriptHash = scriptHash ?? ((byte[])script).ToScriptHash();
configureState?.Invoke(context.GetState<ExecutionContextState>());
// Load context
LoadContext(context);
return context;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ExecutionContextState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Neo.SmartContract
{
internal class ExecutionContextState
public class ExecutionContextState
{
/// <summary>
/// Script hash
Expand Down
8 changes: 6 additions & 2 deletions src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapsh
{
if (NativeContract.IsNative(hash)) return false;
if (hash != witness.ScriptHash) return false;
engine.LoadScript(verification, callFlags: callFlags, scriptHash: hash, initialPosition: 0);
engine.LoadScript(verification, initialPosition: 0, configureState: p =>
{
p.CallFlags = callFlags;
p.ScriptHash = hash;
});
}

engine.LoadScript(witness.InvocationScript, callFlags: CallFlags.None);
engine.LoadScript(witness.InvocationScript, configureState: p => p.CallFlags = CallFlags.None);

if (NativeContract.IsNative(hash))
{
Expand Down
10 changes: 5 additions & 5 deletions tests/neo.UnitTests/Extensions/Nep17NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by
var engine = ApplicationEngine.Create(TriggerType.Application,
new ManualWitness(signFrom ? new UInt160(from) : null), snapshot);

engine.LoadScript(contract.Script, pcount: 4, callFlags: CallFlags.All, scriptHash: contract.Hash);
engine.LoadScript(contract.Script, pcount: 4, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.Emit(OpCode.PUSHNULL);
Expand All @@ -65,7 +65,7 @@ public static BigInteger TotalSupply(this NativeContract contract, StoreView sna
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, callFlags: CallFlags.All, scriptHash: contract.Hash);
engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("totalSupply");
Expand All @@ -83,7 +83,7 @@ public static BigInteger BalanceOf(this NativeContract contract, StoreView snaps
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 1, callFlags: CallFlags.All, scriptHash: contract.Hash);
engine.LoadScript(contract.Script, pcount: 1, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush(account);
Expand All @@ -102,7 +102,7 @@ public static BigInteger Decimals(this NativeContract contract, StoreView snapsh
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, callFlags: CallFlags.All, scriptHash: contract.Hash);
engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("decimals");
Expand All @@ -120,7 +120,7 @@ public static string Symbol(this NativeContract contract, StoreView snapshot)
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(contract.Script, pcount: 0, callFlags: CallFlags.All, scriptHash: contract.Hash);
engine.LoadScript(contract.Script, pcount: 0, configureState: p => p.ScriptHash = contract.Hash);

var script = new ScriptBuilder();
script.EmitPush("symbol");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void TestInvoke()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot, 0);
engine.LoadScript(testNativeContract.Script, callFlags: CallFlags.All, scriptHash: testNativeContract.Hash);
engine.LoadScript(testNativeContract.Script, configureState: p => p.ScriptHash = testNativeContract.Hash);

ByteString method1 = new ByteString(System.Text.Encoding.Default.GetBytes("wrongMethod"));
VMArray args1 = new VMArray();
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ internal static (BigInteger Value, bool State) Check_GetGasPerBlock(StoreView sn
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 0, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 0, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush("getGasPerBlock");
Expand All @@ -889,7 +889,7 @@ internal static (VM.Types.Boolean Value, bool State) Check_SetGasPerBlock(StoreV
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot);
var engine = ApplicationEngine.Create(TriggerType.Application, new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr), snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush(gasPerBlock);
Expand All @@ -912,7 +912,7 @@ internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[]
var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(signAccount ? new UInt160(account) : UInt160.Zero), snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 2, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 2, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();

Expand Down Expand Up @@ -940,7 +940,7 @@ internal static (bool State, bool Result) Check_RegisterValidator(StoreView snap
var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush(pubkey);
Expand All @@ -962,7 +962,7 @@ internal static ECPoint[] Check_GetCommittee(StoreView snapshot)
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 0, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 0, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush("getCommittee");
Expand All @@ -980,7 +980,7 @@ internal static (BigInteger Value, bool State) Check_UnclaimedGas(StoreView snap
{
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 2, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 2, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush(snapshot.PersistingBlock.Index);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ internal static (bool State, bool Result) Check_UnregisterCandidate(StoreView sn
var engine = ApplicationEngine.Create(TriggerType.Application,
new Nep17NativeContractExtensions.ManualWitness(Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1)).ToScriptHash()), snapshot);

engine.LoadScript(NativeContract.NEO.Script, pcount: 1, callFlags: CallFlags.All, scriptHash: NativeContract.NEO.Hash);
engine.LoadScript(NativeContract.NEO.Script, pcount: 1, configureState: p => p.ScriptHash = NativeContract.NEO.Hash);

var script = new ScriptBuilder();
script.EmitPush(pubkey);
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/UT_Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void TestSignatureRedeemScriptFee()

using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, null))
{
engine.LoadScript(invocation.Concat(verification).ToArray(), callFlags: CallFlags.None);
engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None);
engine.Execute();
engine.GasConsumed.Should().Be(fee);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public void TestCreateMultiSigRedeemScriptFee()

using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, null))
{
engine.LoadScript(invocation.Concat(verification).ToArray(), callFlags: CallFlags.None);
engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None);
engine.Execute();
engine.GasConsumed.Should().Be(fee);
}
Expand Down

0 comments on commit 603b79b

Please sign in to comment.