Skip to content

Commit

Permalink
fix signers witness scope
Browse files Browse the repository at this point in the history
  • Loading branch information
meevee98 committed Dec 9, 2022
1 parent 2e330a1 commit c2ef0ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Neo.TestEngine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Engine SetSigners(Signer[] signers)
newSigners.Add(signer);
wallet.AddSignerAccount(signer.Account);
}
currentTx.Signers = newSigners.Concat(currentTx.Signers).ToArray();
currentTx.Signers = newSigners.ToArray();
}
return this;
}
Expand Down
73 changes: 30 additions & 43 deletions src/Neo.TestEngine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,7 @@ public static JObject RunWithJson(JObject json)
// tx signers
if (json.ContainsProperty("signeraccounts") && json["signeraccounts"] is JArray accounts)
{
smartContractTestCase.signers = accounts.Select(account =>
{
JObject accountJson = (JObject)account;
if (!UInt160.TryParse(accountJson["account"].AsString(), out var newAccount))
{
throw new FormatException(GetInvalidTypeMessage("UInt160", "signerAccount"));
}

WitnessScope scopes;
if (!accountJson.ContainsProperty("scopes"))
{
scopes = WitnessScope.CalledByEntry;
}
else if (!Enum.TryParse(accountJson["scopes"].AsString(), out scopes))
{
throw new FormatException(GetInvalidTypeMessage("WitnessScope", "signerScope"));
}

return new Signer()
{
Account = newAccount,
Scopes = scopes,
AllowedContracts = new UInt160[] { },
AllowedGroups = new Cryptography.ECC.ECPoint[] { },
Rules = new WitnessRule[] { }
};
}).ToArray();
smartContractTestCase.signers = accounts.Select(account => SignerFromJson(account)).ToArray();
}

// calling script hash
Expand Down Expand Up @@ -481,6 +455,34 @@ private static TestBlock BlockFromJson(JToken blockJson)
return new TestBlock(block, txStates, blockHash);
}

private static Signer SignerFromJson(JToken signerJson)
{
JObject accountJson = (JObject)signerJson;
if (!UInt160.TryParse(accountJson["account"].AsString(), out var signerAccount))
{
throw new FormatException(GetInvalidTypeMessage("UInt160", "signerAccount"));
}

WitnessScope scopes;
if (!accountJson.ContainsProperty("scopes"))
{
scopes = WitnessScope.CalledByEntry;
}
else if (!Enum.TryParse(accountJson["scopes"].AsString(), out scopes))
{
throw new FormatException(GetInvalidTypeMessage("WitnessScope", "signerScope"));
}

return new Signer()
{
Account = signerAccount,
Scopes = scopes,
AllowedContracts = new UInt160[] { },
AllowedGroups = new Cryptography.ECC.ECPoint[] { },
Rules = new WitnessRule[] { }
};
}

private static Transaction TxFromJson(JToken txJson)
{
JObject txJsonObject = (JObject)txJson;
Expand All @@ -490,22 +492,7 @@ private static Transaction TxFromJson(JToken txJson)

if (txJsonObject.ContainsProperty("signers") && txJsonObject["signers"] is JArray signersJson)
{
accounts = signersJson.Select(p =>
{
if (!UInt160.TryParse(p["account"].AsString(), out var signerAccount))
{
throw new FormatException(GetInvalidTypeMessage("UInt160", "signerAccount"));
}

return new Signer()
{
Account = signerAccount,
Scopes = WitnessScope.CalledByEntry,
AllowedContracts = new UInt160[] { },
AllowedGroups = new Cryptography.ECC.ECPoint[] { },
Rules = new WitnessRule[] { }
};
}).ToArray();
accounts = signersJson.Select(p => SignerFromJson(p)).ToArray();
}
else
{
Expand Down
9 changes: 6 additions & 3 deletions src/Neo.TestEngine/TestUtils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,19 @@ public EvaluationStack ExecuteTestCaseStandard(int offset, ushort rvcount, NefFi
executedScriptHash = EntryScriptHash;
previousGasConsumed = GasConsumed;
var context = InvocationStack.Pop();
ExecutionContext? callingContext = null;
ExecutionContext? callingContext = context;
var callingState = context.GetState<ExecutionContextState>();

// Mock Calling Script Hash
if (callingScriptHash is not null)
{
callingContext = context;
var callingState = context.GetState<ExecutionContextState>();
callingState.Contract ??= new ContractState() { Hash = callingScriptHash };
callingState.ScriptHash = callingScriptHash;
}
else if (ScriptContainer is Transaction currentTx) // Mock Signer Script Hash
{
callingState.ScriptHash = currentTx.Signers[0].Account;
}

context = CreateContext(context.Script, rvcount, offset);
LoadContext(context);
Expand Down

0 comments on commit c2ef0ae

Please sign in to comment.