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

Call _deploy during deploy and update #1933

Merged
merged 17 commits into from
Sep 21, 2020
29 changes: 20 additions & 9 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ protected internal ContractState CreateContract(byte[] script, byte[] manifest)
if (!contract.Manifest.IsValid(hash)) throw new InvalidOperationException($"Invalid Manifest Hash: {hash}");

Snapshot.Contracts.Add(hash, contract);

// Execute _deploy

ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod("_deploy");
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (md != null)
CallContractInternal(contract, md, new Array(ReferenceCounter), CallFlags.All);

return contract;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -121,12 +128,18 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg

ContractState contract = Snapshot.Contracts.TryGet(contractHash);
if (contract is null) throw new InvalidOperationException($"Called Contract Does Not Exist: {contractHash}");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) throw new InvalidOperationException($"Method {method} Does Not Exist In Contract {contractHash}");

ContractManifest currentManifest = Snapshot.Contracts.TryGet(CurrentScriptHash)?.Manifest;

if (currentManifest != null && !currentManifest.CanCall(contract.Manifest, method))
throw new InvalidOperationException($"Cannot Call Method {method} Of Contract {contractHash} From Contract {CurrentScriptHash}");

CallContractInternal(contract, md, args, flags);
}

private void CallContractInternal(ContractState contract, ContractMethodDescriptor method, Array args, CallFlags flags)
{
if (invocationCounter.TryGetValue(contract.ScriptHash, out var counter))
{
invocationCounter[contract.ScriptHash] = counter + 1;
Expand All @@ -142,27 +155,25 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg
UInt160 callingScriptHash = state.ScriptHash;
CallFlags callingFlags = state.CallFlags;

ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) throw new InvalidOperationException($"Method {method} Does Not Exist In Contract {contractHash}");
if (args.Count != md.Parameters.Length) throw new InvalidOperationException($"Method {method} Expects {md.Parameters.Length} Arguments But Receives {args.Count} Arguments");
ExecutionContext context_new = LoadScript(contract.Script, md.Offset);
if (args.Count != method.Parameters.Length) throw new InvalidOperationException($"Method {method.Name} Expects {method.Parameters.Length} Arguments But Receives {args.Count} Arguments");
ExecutionContext context_new = LoadScript(contract.Script, method.Offset);
state = context_new.GetState<ExecutionContextState>();
state.CallingScriptHash = callingScriptHash;
state.CallFlags = flags & callingFlags;

if (NativeContract.IsNative(contractHash))
if (NativeContract.IsNative(contract.ScriptHash))
{
context_new.EvaluationStack.Push(args);
context_new.EvaluationStack.Push(method);
context_new.EvaluationStack.Push(method.Name);
}
else
{
for (int i = args.Count - 1; i >= 0; i--)
context_new.EvaluationStack.Push(args[i]);
}

md = contract.Manifest.Abi.GetMethod("_initialize");
if (md != null) LoadContext(context_new.Clone(md.Offset));
method = contract.Manifest.Abi.GetMethod("_initialize");
if (method != null) LoadContext(context_new.Clone(method.Offset));
}

protected internal bool IsStandardContract(UInt160 hash)
Expand Down