-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathExpressApplicationEngineProvider.cs
More file actions
77 lines (70 loc) · 2.69 KB
/
ExpressApplicationEngineProvider.cs
File metadata and controls
77 lines (70 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (C) 2015-2023 The Neo Project.
//
// ExpressApplicationEngineProvider.cs file belongs to neo-express project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo;
using Neo.BlockchainToolkit.SmartContract;
using Neo.BlockchainToolkit.TraceDebug;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
namespace NeoExpress.Node
{
class ExpressApplicationEngineProvider : IApplicationEngineProvider
{
public ApplicationEngine? Create(TriggerType trigger, IVerifiable container, DataCache snapshot, Block persistingBlock, ProtocolSettings settings, long gas, IDiagnostic? diagnostic)
{
if (trigger == TriggerType.Application
&& container is Transaction tx
&& tx.Witnesses is not null
&& tx.Witnesses.Length > 0
&& EnumerateContractCalls(tx.Script).Any())
{
var path = Path.Combine(Environment.CurrentDirectory, $"{tx.Hash}.neo-trace");
var sink = new TraceDebugStream(File.OpenWrite(path));
return new TraceApplicationEngine(sink, trigger, container, snapshot, persistingBlock, settings, gas, diagnostic);
}
return null;
}
static IEnumerable<UInt160> EnumerateContractCalls(Script script)
{
var scriptHash = UInt160.Zero;
foreach (var i in EnumerateInstructions(script))
{
if (i.OpCode == OpCode.PUSHDATA1
&& i.Operand.Length == UInt160.Length)
{
scriptHash = new UInt160(i.Operand.Span);
}
if (i.OpCode == OpCode.SYSCALL
&& i.TokenU32 == ApplicationEngine.System_Contract_Call.Hash)
{
yield return scriptHash;
}
}
}
static IEnumerable<Instruction> EnumerateInstructions(Script script)
{
var address = 0;
var opcode = OpCode.PUSH0;
while (address < script.Length)
{
var instruction = script.GetInstruction(address);
opcode = instruction.OpCode;
yield return instruction;
address += instruction.Size;
}
if (opcode != OpCode.RET)
{
yield return Instruction.RET;
}
}
}
}