Skip to content

Commit

Permalink
Allow contracts to load script at runtime dynamically (#2756)
Browse files Browse the repository at this point in the history
* Allow contracts to load script at runtime dynamically

* Improve

* Prevent notifications in dynamic scripts

* Fix UT

* Fix CalledByEntryCondition

* rename

* readonly
  • Loading branch information
erikzhang authored Sep 16, 2022
1 parent 90cf23b commit 27f63be
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Neo.IO;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -90,6 +91,12 @@ partial class ApplicationEngine
/// </summary>
public static readonly InteropDescriptor System_Runtime_GetEntryScriptHash = Register("System.Runtime.GetEntryScriptHash", nameof(EntryScriptHash), 1 << 4, CallFlags.None);

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.LoadScript.
/// Loads a script at rumtime.
/// </summary>
public static readonly InteropDescriptor System_Runtime_LoadScript = Register("System.Runtime.LoadScript", nameof(RuntimeLoadScript), 1 << 15, CallFlags.AllowCall);

/// <summary>
/// The <see cref="InteropDescriptor"/> of System.Runtime.CheckWitness.
/// Determines whether the specified account has witnessed the current transaction.
Expand Down Expand Up @@ -189,6 +196,27 @@ protected internal StackItem GetScriptContainer()
return interop.ToStackItem(ReferenceCounter);
}

/// <summary>
/// The implementation of System.Runtime.LoadScript.
/// Loads a script at rumtime.
/// </summary>
protected internal void RuntimeLoadScript(byte[] script, CallFlags callFlags, Array args)
{
if ((callFlags & ~CallFlags.All) != 0)
throw new ArgumentOutOfRangeException(nameof(callFlags));

ExecutionContextState state = CurrentContext.GetState<ExecutionContextState>();
ExecutionContext context = LoadScript(script, configureState: p =>
{
p.CallingContext = CurrentContext;
p.CallFlags = callFlags & state.CallFlags & CallFlags.ReadOnly;
p.IsDynamicCall = true;
});

for (int i = args.Count - 1; i >= 0; i--)
context.EvaluationStack.Push(args[i]);
}

/// <summary>
/// The implementation of System.Runtime.CheckWitness.
/// Determines whether the specified account has witnessed the current transaction.
Expand Down

0 comments on commit 27f63be

Please sign in to comment.