forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontractCallGraph.ts
More file actions
276 lines (256 loc) · 9.71 KB
/
Copy pathcontractCallGraph.ts
File metadata and controls
276 lines (256 loc) · 9.71 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Contract call graph logic (issue #53)
*
* Pure functions for building and querying the directed call-graph data model.
* Cytoscape element definitions are produced here so the component stays thin
* and these functions stay unit-testable without a DOM.
*/
import type { ElementDefinition } from 'cytoscape';
// ---------------------------------------------------------------------------
// Domain types
// ---------------------------------------------------------------------------
/** A single contract function (graph node). */
export interface CallGraphNode {
/** Unique identifier — matches the `id` used in edges. */
id: string;
/** Display label shown inside the node. */
label: string;
/**
* Semantic category drives node colour.
* - `entry` — publicly callable top-level functions
* - `internal` — private/helper functions called only from within the contract
* - `external` — cross-contract calls (other deployed contracts)
* - `event` — emitted events (sink nodes, no outgoing calls)
*/
kind: 'entry' | 'internal' | 'external' | 'event';
/** Optional gas cost hint — surfaced in the tooltip. */
gasCost?: number;
}
/** A directed call relationship between two functions (graph edge). */
export interface CallGraphEdge {
/** Source node id (caller). */
source: string;
/** Target node id (callee). */
target: string;
/** Optional label shown on the edge (e.g. "conditional", "loop"). */
label?: string;
/** When true the edge is drawn as dashed to indicate a conditional call. */
conditional?: boolean;
}
/** The full call graph definition passed to the component. */
export interface CallGraph {
nodes: CallGraphNode[];
edges: CallGraphEdge[];
}
// ---------------------------------------------------------------------------
// Cytoscape element builder
// ---------------------------------------------------------------------------
/**
* Convert a `CallGraph` into a flat array of Cytoscape `ElementDefinition`
* objects ready to pass to `cytoscape({ elements })`.
*/
export function buildCytoscapeElements(graph: CallGraph): ElementDefinition[] {
const nodeElements: ElementDefinition[] = graph.nodes.map((node) => ({
group: 'nodes' as const,
data: {
id: node.id,
label: node.label,
kind: node.kind,
gasCost: node.gasCost ?? 0,
},
}));
const edgeElements: ElementDefinition[] = graph.edges.map((edge, i) => ({
group: 'edges' as const,
data: {
id: `edge-${edge.source}-${edge.target}-${i}`,
source: edge.source,
target: edge.target,
label: edge.label ?? '',
conditional: edge.conditional ?? false,
},
}));
return [...nodeElements, ...edgeElements];
}
// ---------------------------------------------------------------------------
// Graph analysis helpers (all pure, exported for unit tests)
// ---------------------------------------------------------------------------
/**
* Return the set of node ids that have no incoming edges (root callers).
* These are the true entry-points of the call graph.
*/
export function findRoots(graph: CallGraph): string[] {
const targeted = new Set(graph.edges.map((e) => e.target));
return graph.nodes
.filter((n) => !targeted.has(n.id))
.map((n) => n.id);
}
/**
* Return the set of node ids that have no outgoing edges (leaf callees).
*/
export function findLeaves(graph: CallGraph): string[] {
const sources = new Set(graph.edges.map((e) => e.source));
return graph.nodes
.filter((n) => !sources.has(n.id))
.map((n) => n.id);
}
/**
* Count the number of unique callers for each node (in-degree).
* Useful for highlighting heavily-depended-on functions.
*/
export function computeInDegree(graph: CallGraph): Record<string, number> {
const degrees: Record<string, number> = {};
for (const node of graph.nodes) {
degrees[node.id] = 0;
}
for (const edge of graph.edges) {
degrees[edge.target] = (degrees[edge.target] ?? 0) + 1;
}
return degrees;
}
/**
* Return all direct callees of a given node id.
*/
export function getCallees(graph: CallGraph, nodeId: string): string[] {
return graph.edges
.filter((e) => e.source === nodeId)
.map((e) => e.target);
}
/**
* Return all direct callers of a given node id.
*/
export function getCallers(graph: CallGraph, nodeId: string): string[] {
return graph.edges
.filter((e) => e.target === nodeId)
.map((e) => e.source);
}
// ---------------------------------------------------------------------------
// Cytoscape stylesheet
// ---------------------------------------------------------------------------
/** Colour palette per node kind. */
export const NODE_COLORS: Record<CallGraphNode['kind'], { bg: string; border: string; text: string }> = {
entry: { bg: '#6366f1', border: '#4338ca', text: '#ffffff' },
internal: { bg: '#0ea5e9', border: '#0284c7', text: '#ffffff' },
external: { bg: '#f59e0b', border: '#d97706', text: '#ffffff' },
event: { bg: '#10b981', border: '#059669', text: '#ffffff' },
};
/** Build a Cytoscape stylesheet array for the call graph. */
export function buildStylesheet(): any[] {
return [
{
selector: 'node',
style: {
'label': 'data(label)',
'text-valign': 'center',
'text-halign': 'center',
'font-size': '11px',
'font-family': 'ui-monospace, monospace',
'width': 'label',
'height': 'label',
'padding': '10px',
'border-width': 2,
'shape': 'round-rectangle',
'color': '#ffffff',
'text-wrap': 'wrap',
'text-max-width': '120px',
} as any,
},
// Per-kind node colours
...(['entry', 'internal', 'external', 'event'] as CallGraphNode['kind'][]).map((kind) => ({
selector: `node[kind = "${kind}"]`,
style: {
'background-color': NODE_COLORS[kind].bg,
'border-color': NODE_COLORS[kind].border,
} as any,
})),
// Selected node highlight
{
selector: 'node:selected',
style: {
'border-width': 3,
'border-color': '#f8fafc',
'overlay-color': '#6366f1',
'overlay-padding': '4px',
'overlay-opacity': 0.2,
} as any,
},
// Edges
{
selector: 'edge',
style: {
'width': 1.5,
'line-color': '#94a3b8',
'target-arrow-color': '#94a3b8',
'target-arrow-shape': 'triangle',
'curve-style': 'bezier',
'label': 'data(label)',
'font-size': '9px',
'color': '#94a3b8',
'text-background-color': '#f8fafc',
'text-background-opacity': 0.8,
'text-background-padding': '2px',
} as any,
},
// Conditional edges — dashed
{
selector: 'edge[conditional = 1]',
style: {
'line-style': 'dashed',
'line-dash-pattern': [6, 3],
} as any,
},
// Dimmed state when a neighbour is focused
{
selector: '.dimmed',
style: {
'opacity': 0.2,
} as any,
},
{
selector: '.highlighted',
style: {
'border-width': 3,
'border-color': '#f8fafc',
} as any,
},
];
}
// ---------------------------------------------------------------------------
// Seed / demo data
// ---------------------------------------------------------------------------
/**
* Representative call graph for the Vero smart contract suite.
* Injectable via props so real static-analysis output can replace it.
*/
export const DEFAULT_CALL_GRAPH: CallGraph = {
nodes: [
{ id: 'cast_vote', label: 'cast_vote', kind: 'entry', gasCost: 12_500_000 },
{ id: 'register_task', label: 'register_task', kind: 'entry', gasCost: 9_800_000 },
{ id: 'tally_votes', label: 'tally_votes', kind: 'entry', gasCost: 41_200_000 },
{ id: 'set_role', label: 'set_role', kind: 'entry', gasCost: 7_400_000 },
{ id: 'get_reputation', label: 'get_reputation', kind: 'entry', gasCost: 3_100_000 },
{ id: 'verify_guardian', label: 'verify_guardian', kind: 'internal', gasCost: 1_200_000 },
{ id: 'check_threshold', label: 'check_threshold', kind: 'internal', gasCost: 800_000 },
{ id: 'update_score', label: 'update_score', kind: 'internal', gasCost: 2_400_000 },
{ id: 'read_reputation', label: 'read_reputation', kind: 'external', gasCost: 500_000 },
{ id: 'evt_vote_cast', label: 'VoteCast', kind: 'event' },
{ id: 'evt_task_reg', label: 'TaskRegistered', kind: 'event' },
{ id: 'evt_role_set', label: 'RoleSet', kind: 'event' },
{ id: 'evt_tally_done', label: 'TallyComplete', kind: 'event' },
],
edges: [
{ source: 'cast_vote', target: 'verify_guardian', label: '' },
{ source: 'cast_vote', target: 'check_threshold', label: '', conditional: true },
{ source: 'cast_vote', target: 'update_score', label: '' },
{ source: 'cast_vote', target: 'evt_vote_cast', label: '' },
{ source: 'register_task', target: 'verify_guardian', label: '' },
{ source: 'register_task', target: 'evt_task_reg', label: '' },
{ source: 'tally_votes', target: 'check_threshold', label: '' },
{ source: 'tally_votes', target: 'update_score', label: '' },
{ source: 'tally_votes', target: 'evt_tally_done', label: '' },
{ source: 'set_role', target: 'verify_guardian', label: '' },
{ source: 'set_role', target: 'evt_role_set', label: '' },
{ source: 'get_reputation', target: 'read_reputation', label: '' },
{ source: 'update_score', target: 'read_reputation', label: '' },
{ source: 'verify_guardian', target: 'read_reputation', label: '', conditional: true },
],
};