private void CheckOpcodesMenuItem_Click(object sender, EventArgs e) { // set it reaonly to not change the script ScriptText.ReadOnly = true; ParameterTable.Rows[0].Selected = true; TrackerOutputText.CreateLogMessage(($"Running CheckOpcodes with Thread {Thread.CurrentThread.ManagedThreadId.ToString()} \n"), Utils.Helper.MessageClass.Information); var engine = new SCTrackerExecutionEngine(TrackerOutputText, null, Crypto.Default); engine.SCTrackerLoadScript(ScriptText.Text.HexToBytes()); // loads avm ScriptBuilder sb = ParameterTable.GetScriptBuilder(TrackerOutputText, engine); engine.LoadScript(sb.ToArray()); TrackerOutputText.CreateLogMessage(($"[Contract:0x{engine.GetScriptHash()}] Added parameters as hex = {sb.ToArray().ToHexString()} \n"), Utils.Helper.MessageClass.Information); TrackerOutputText.CreateLogMessage("\tTracking:\n", Utils.Helper.MessageClass.Information); engine.SCTrackerExecute(); // start execution string state = engine.State == VMState.HALT ? "OK" : "FAIL"; string stateMessage = $"[Contract:0x{engine.GetScriptHash()}] Result = {state} \n"; Utils.Helper.MessageClass mClass = Utils.Helper.MessageClass.Information; OpCode currentOpCode = OpCode.NOP; string result = "No result"; if (engine.State == VMState.HALT) { result = engine.ResultStack.Peek().ToParameter().ToString(); mClass = Utils.Helper.MessageClass.Success; } else if (engine.State == VMState.FAULT) { int pos = engine.InvocationStack.Peek().InstructionPointer * 2 - 2; ScriptText.SelectionStart = pos; ScriptText.SelectionLength = 2; ScriptText.SelectionColor = Color.Red; mClass = Utils.Helper.MessageClass.Error; } else { int pos = engine.InvocationStack.Peek().InstructionPointer * 2 - 2; ScriptText.SelectionStart = pos; ScriptText.SelectionLength = 2; ScriptText.SelectionColor = Color.DarkGoldenrod; currentOpCode = (OpCode)Convert.ToByte(ScriptText.Text.Substring(pos, 2), 16); result = currentOpCode.ToString() + " at position " + pos / 2; mClass = Utils.Helper.MessageClass.Warning; } TrackerOutputText.CreateLogMessage(($"[Contract:0x{engine.GetScriptHash()}] Result = {result} \n"), mClass); TrackerOutputText.CreateLogMessage(stateMessage, mClass); TrackerOutputText.CreateLogMessage("CheckOpcodes Finishes \n", Utils.Helper.MessageClass.Information); // put it back to be changed ScriptText.ReadOnly = false; }
public static ScriptBuilder GetScriptBuilder(this DataGridView ParameterTable, RichTextBox Logger, SCTrackerExecutionEngine engine) { ScriptBuilder sb = new ScriptBuilder(); for (int i = ParameterTable.Rows.Count - 2; i >= 0; --i) { string type = ParameterTable.Rows[i].Cells[0].Value.ToString(); string value = ParameterTable.Rows[i].Cells[1].Value.ToString(); switch (type) { case "byte[]": sb.EmitPush(value.HexToBytes()); break; case "BigInteger": sb.EmitPush(BigInteger.Parse(value)); break; case "bool": sb.EmitPush(Convert.ToBoolean(value)); break; case "string": sb.EmitPush(value); break; default: break; } Logger.CreateLogMessage(($"[Contract:0x{engine.GetScriptHash()}] Add parameter {type} = {value} \n"), Utils.Helper.MessageClass.Information); } return(sb); }