forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.ts
More file actions
224 lines (205 loc) · 6.39 KB
/
Copy pathdiff.ts
File metadata and controls
224 lines (205 loc) · 6.39 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
// src/utils/diff.ts
/**
* On-chain state diff engine
* Compares on‑chain contract information with repository contract definitions.
* Returns a structured report suitable for audit pipelines.
*/
export type ContractInfo = {
/** ABI JSON array as produced by the compiler */
abi: any[];
/** Deployed bytecode (hex string) */
bytecode: string;
/** Arbitrary state variables keyed by name */
state: Record<string, unknown>;
};
export type DiffChange = {
/** The category of the change */
type: "abi" | "bytecode" | "state";
/** The field that differed, e.g. function name or state variable */
field: string;
/** Expected value from repo */
expected: unknown;
/** Actual value from on‑chain */
actual: unknown;
};
export type DiffResult = {
/** Overall status – drift if any change is detected */
status: "drift" | "match";
/** List of granular changes */
changes: DiffChange[];
/** ISO timestamp of the report generation */
timestamp: string;
};
type FetchOnChain = (address: string) => Promise<ContractInfo>;
/** Simple in-memory cache to avoid re-fetching from the same on-chain source within a run. */
const onChainCache = new WeakMap<FetchOnChain, Map<string, ContractInfo>>();
/**
* Compare two ABI arrays.
* Returns an array of DiffChange objects describing missing/changed members.
*/
function compareAbi(repoAbi: any[], onChainAbi: any[]): DiffChange[] {
const changes: DiffChange[] = [];
const repoFuncs = repoAbi.filter((e) => e.type === "function");
const onChainFuncs = onChainAbi.filter((e) => e.type === "function");
const repoEvents = repoAbi.filter((e) => e.type === "event");
const onChainEvents = onChainAbi.filter((e) => e.type === "event");
const mapByName = (arr: any[]) => {
const map = new Map<string, any>();
arr.forEach((item) => {
const name = item.name || "<anonymous>";
map.set(name, item);
});
return map;
};
const repoFuncMap = mapByName(repoFuncs);
const onChainFuncMap = mapByName(onChainFuncs);
// Missing functions
for (const [name, def] of repoFuncMap.entries()) {
if (!onChainFuncMap.has(name)) {
changes.push({
type: "abi",
field: name,
expected: def,
actual: undefined,
});
} else {
// Compare signatures (inputs & outputs)
const onDef = onChainFuncMap.get(name);
const sigEqual =
JSON.stringify(def.inputs) === JSON.stringify(onDef.inputs) &&
JSON.stringify(def.outputs) === JSON.stringify(onDef.outputs) &&
def.stateMutability === onDef.stateMutability;
if (!sigEqual) {
changes.push({
type: "abi",
field: name,
expected: def,
actual: onDef,
});
}
}
}
// Extra functions present on‑chain but not in repo (potential drift)
for (const name of onChainFuncMap.keys()) {
if (!repoFuncMap.has(name)) {
changes.push({
type: "abi",
field: name,
expected: undefined,
actual: onChainFuncMap.get(name),
});
}
}
// Events – same logic as functions
const repoEventMap = mapByName(repoEvents);
const onChainEventMap = mapByName(onChainEvents);
for (const [name, def] of repoEventMap.entries()) {
if (!onChainEventMap.has(name)) {
changes.push({
type: "abi",
field: `event:${name}`,
expected: def,
actual: undefined,
});
} else {
const onDef = onChainEventMap.get(name);
const inputsEqual = JSON.stringify(def.inputs) === JSON.stringify(onDef.inputs);
if (!inputsEqual) {
changes.push({
type: "abi",
field: `event:${name}`,
expected: def,
actual: onDef,
});
}
}
}
for (const name of onChainEventMap.keys()) {
if (!repoEventMap.has(name)) {
changes.push({
type: "abi",
field: `event:${name}`,
expected: undefined,
actual: onChainEventMap.get(name),
});
}
}
return changes;
}
/** Compare bytecode hashes */
function compareBytecode(repoBytecode: string, onChainBytecode: string): DiffChange[] {
if (repoBytecode === onChainBytecode) return [];
return [
{
type: "bytecode",
field: "bytecode",
expected: repoBytecode,
actual: onChainBytecode,
},
];
}
/** Shallow state comparison */
function compareState(repoState: Record<string, unknown>, onChainState: Record<string, unknown>): DiffChange[] {
const changes: DiffChange[] = [];
const allKeys = new Set([...Object.keys(repoState), ...Object.keys(onChainState)]);
for (const key of allKeys) {
const repoVal = (repoState as any)[key];
const onVal = (onChainState as any)[key];
if (JSON.stringify(repoVal) !== JSON.stringify(onVal)) {
changes.push({
type: "state",
field: key,
expected: repoVal,
actual: onVal,
});
}
}
return changes;
}
/**
* Public diff entry point.
* Caches on‑chain data for the same contract address within a process run.
*/
export async function diffContract(
address: string,
fetchOnChain: FetchOnChain,
repoInfo: ContractInfo
): Promise<DiffResult> {
// Retrieve on‑chain data, using cache when possible.
let fetcherCache = onChainCache.get(fetchOnChain);
if (!fetcherCache) {
fetcherCache = new Map<string, ContractInfo>();
onChainCache.set(fetchOnChain, fetcherCache);
}
let onChainInfo = fetcherCache.get(address);
if (!onChainInfo) {
onChainInfo = await fetchOnChain(address);
fetcherCache.set(address, onChainInfo);
}
const changes: DiffChange[] = [];
// ABI comparison
changes.push(...compareAbi(repoInfo.abi, onChainInfo.abi));
// Bytecode comparison
changes.push(...compareBytecode(repoInfo.bytecode, onChainInfo.bytecode));
// State comparison
changes.push(...compareState(repoInfo.state, onChainInfo.state));
const status = changes.length ? "drift" : "match";
return {
status,
changes,
timestamp: new Date().toISOString(),
};
}
/**
* Example fetcher stub – in a real environment this would query the blockchain node.
* It is exported for testing purposes so the test suite can provide a mock implementation.
*/
export async function stubFetchOnChain(address: string): Promise<ContractInfo> {
// Placeholder – callers should supply a concrete implementation.
// We return empty structures to keep the function signature pure.
return {
abi: [],
bytecode: "",
state: {},
};
}