-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGraphExecutionCycle.cs
79 lines (71 loc) · 2.93 KB
/
GraphExecutionCycle.cs
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
78
79
using Newtonsoft.Json;
using NodeBlock.Engine.Attributes;
using NodeBlock.Engine.Debugging;
using NodeBlock.Engine.Nodes.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
namespace NodeBlock.Engine
{
public class GraphExecutionCycle
{
public BlockGraph Graph { get; }
public long Timestamp { get; }
public Node StartNode { get; }
public bool DebugTraceEnabled = false;
public List<Node> ExecutedNodesInCycle;
public Debugging.GraphTrace Trace;
public Dictionary<string, NodeParameter> InstanciateParametersForCycle;
public FunctionContext CurrentFunctionContext { get; set; }
public GraphExecutionCycle(BlockGraph graph, long timestamp, Node startNode, Dictionary<string, NodeParameter> parameters = null)
{
Graph = graph;
Timestamp = timestamp;
StartNode = startNode;
ExecutedNodesInCycle = new List<Node>();
this.Trace = new Debugging.GraphTrace(this);
this.AddExecutedNode(StartNode);
this.InstanciateParametersForCycle = parameters != null ? parameters : this.StartNode.InstanciatedParametersForCycle();
}
public void Execute()
{
this.InstanciateParametersForCycle.ToList().ForEach(x =>
{
this.StartNode.OutParameters[x.Key].Value = x.Value.Value;
});
this.StartNode.BeginCycle();
BigInteger usedGas = this.GetCycleExecutedGasPrice();
this.Graph.currentContext.AddCycleCost(decimal.Parse(usedGas.ToString()));
if(DebugTraceEnabled)
this.Graph.AppendLog("debug", this.Trace.ToString());
}
public BigInteger GetCycleExecutedGasPrice()
{
BigInteger total = new BigInteger();
this.ExecutedNodesInCycle.ForEach(x =>
{
if (x.GetType().GetCustomAttributes(typeof(NodeGasConfiguration), true).Length == 0) return;
var gasConfig = x.GetType().GetCustomAttributes(typeof(NodeGasConfiguration), true)[0] as NodeGasConfiguration;
total += gasConfig.BlockGasPrice;
});
return total;
}
public int GetCycleMaxExecutionTime()
{
var baseTime = 1000 * 60;
var maxTimeout = 0;
foreach(var nodeWithTimeout in this.Graph.Nodes.Where(x => x.Value.CustomTimeout > 0))
{
if (nodeWithTimeout.Value.CustomTimeout > maxTimeout) maxTimeout = (int)nodeWithTimeout.Value.CustomTimeout;
}
return baseTime + maxTimeout;
}
public TraceItem AddExecutedNode(Node node)
{
this.ExecutedNodesInCycle.Add(node);
return this.Trace.AppendTrace(node);
}
}
}