-
Notifications
You must be signed in to change notification settings - Fork 61
/
helpers.ts
439 lines (393 loc) · 16.7 KB
/
helpers.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import { utils } from "@coral-xyz/anchor";
import {
AccountMeta,
CompiledInstruction,
LoadedAddresses,
Message,
MessageCompiledInstruction,
PartiallyDecodedInstruction,
PublicKey,
TransactionInstruction,
VersionedMessage,
VersionedTransactionResponse,
} from "@solana/web3.js";
import { ProgramLogContext } from "./interfaces";
export function hexToBuffer(data: string) {
const rawHex = data.startsWith("0x") ? data.slice(2) : data;
return Buffer.from(rawHex);
}
/**
* Parse transaction message and extract account metas
* @param message transaction message
* @returns parsed accounts metas
*/
export function parseTransactionAccounts<T extends Message | VersionedMessage>(
message: T,
loadedAddresses: T extends VersionedMessage ? LoadedAddresses | undefined : undefined = undefined,
): AccountMeta[] {
const accounts: PublicKey[] = message.version === "legacy" ? message.accountKeys : message.staticAccountKeys;
const readonlySignedAccountsCount = message.header.numReadonlySignedAccounts;
const readonlyUnsignedAccountsCount = message.header.numReadonlyUnsignedAccounts;
const requiredSignaturesAccountsCount = message.header.numRequiredSignatures;
const totalAccounts = accounts.length;
let parsedAccounts: AccountMeta[] = accounts.map((account, idx) => {
const isWritable =
idx < requiredSignaturesAccountsCount - readonlySignedAccountsCount ||
(idx >= requiredSignaturesAccountsCount && idx < totalAccounts - readonlyUnsignedAccountsCount);
return {
isSigner: idx < requiredSignaturesAccountsCount,
isWritable,
pubkey: new PublicKey(account),
} as AccountMeta;
});
const [ALTWritable, ALTReadOnly] =
message.version === "legacy" ? [[], []] : loadedAddresses ? [loadedAddresses.writable, loadedAddresses.readonly] : [[], []]; // message.getAccountKeys({ accountKeysFromLookups: loadedAddresses }).keySegments().slice(1); // omit static keys
if (ALTWritable) parsedAccounts = [...parsedAccounts, ...ALTWritable.map((pubkey) => ({ isSigner: false, isWritable: true, pubkey }))];
if (ALTReadOnly) parsedAccounts = [...parsedAccounts, ...ALTReadOnly.map((pubkey) => ({ isSigner: false, isWritable: false, pubkey }))];
return parsedAccounts;
}
/**
* Converts compiled instruction into common TransactionInstruction
* @param compiledInstruction
* @param parsedAccounts account meta, result of {@link parseTransactionAccounts}
* @returns TransactionInstruction
*/
export function compiledInstructionToInstruction<Ix extends CompiledInstruction | MessageCompiledInstruction>(
compiledInstruction: Ix,
parsedAccounts: AccountMeta[],
): TransactionInstruction {
if (typeof compiledInstruction.data === "string") {
const ci = compiledInstruction as CompiledInstruction;
return new TransactionInstruction({
data: utils.bytes.bs58.decode(ci.data),
programId: parsedAccounts[ci.programIdIndex].pubkey,
keys: ci.accounts.map((accountIdx) => parsedAccounts[accountIdx]),
});
} else {
const ci = compiledInstruction as MessageCompiledInstruction;
return new TransactionInstruction({
data: Buffer.from(ci.data),
programId: parsedAccounts[ci.programIdIndex].pubkey,
keys: ci.accountKeyIndexes.map((accountIndex) => {
if (accountIndex >= parsedAccounts.length)
throw new Error(
`Trying to resolve account at index ${accountIndex} while parsedAccounts is only ${parsedAccounts.length}. \
Looks like you're trying to parse versioned transaction, make sure that LoadedAddresses are passed to the \
parseTransactionAccounts function`,
);
return parsedAccounts[accountIndex];
}),
});
}
}
function parsedAccountsToMeta(accounts: PublicKey[], accountMeta: AccountMeta[]): AccountMeta[] {
const meta = accountMeta.map((m) => ({ pk: m.pubkey.toString(), ...m }));
return accounts.map((account) => {
const encoded = account.toString();
const found = meta.find((item) => item.pk === encoded);
if (!found) throw new Error(`Account ${encoded} not present in account meta!`);
return found;
});
}
export function parsedInstructionToInstruction(parsedInstruction: PartiallyDecodedInstruction, accountMeta: AccountMeta[]): TransactionInstruction {
return new TransactionInstruction({
data: utils.bytes.bs58.decode(parsedInstruction.data),
programId: parsedInstruction.programId,
keys: parsedAccountsToMeta(parsedInstruction.accounts, accountMeta),
});
}
/**
* Converts transaction response with CPI into artifical transaction that contains all instructions from tx and CPI
* @param transaction transactionResponse to convert from
* @returns Transaction object
*/
export function flattenTransactionResponse(transaction: VersionedTransactionResponse): TransactionInstruction[] {
const result: TransactionInstruction[] = [];
if (transaction === null || transaction === undefined) return [];
const txInstructions = transaction.transaction.message.compiledInstructions;
const accountsMeta = parseTransactionAccounts(transaction.transaction.message, transaction.meta?.loadedAddresses);
const orderedCII = (transaction?.meta?.innerInstructions || []).sort((a, b) => a.index - b.index);
const totalCalls =
(transaction.meta?.innerInstructions || []).reduce((accumulator, cii) => accumulator + cii.instructions.length, 0) + txInstructions.length;
let lastPushedIx = -1;
let callIndex = -1;
for (const CII of orderedCII) {
// push original instructions until we meet CPI
while (lastPushedIx !== CII.index) {
lastPushedIx += 1;
callIndex += 1;
result.push(compiledInstructionToInstruction(txInstructions[lastPushedIx], accountsMeta));
}
for (const CIIEntry of CII.instructions) {
result.push(compiledInstructionToInstruction(CIIEntry, accountsMeta));
callIndex += 1;
}
}
while (callIndex < totalCalls - 1) {
lastPushedIx += 1;
callIndex += 1;
result.push(compiledInstructionToInstruction(txInstructions[lastPushedIx], accountsMeta));
}
return result;
}
/**
* @private
*/
function newLogContext(programId: string, depth: number, id: number, instructionIndex: number): ProgramLogContext {
return {
logMessages: [],
dataLogs: [],
rawLogs: [],
errors: [],
programId,
depth,
id,
instructionIndex,
};
}
function generateLogsParsingRegex() {
const knownMsgs = [
"{}'s writable privilege escalated",
"{}'s signer privilege escalated",
"Unknown program {}",
"Account {} is not executable",
"Unable to deserialize config account: {}",
"account {} is not in account list",
"account {} signer_key().is_none()",
"account[{}].signer_key() does not match Config data)",
"account {} is not in stored signer list",
"account[0].signer_key().is_none()",
"new config contains duplicate keys",
"too few signers: {}; expected: {}",
"instruction data too large",
"Checking if destination stake is mergeable",
"Checking if source stake is mergeable",
"Merging stake accounts",
"expected uninitialized stake account owner to be {}, not {}",
"expected uninitialized stake account data len to be {}, not {}",
"expected uninitialized stake account to be uninitialized",
"expected vote account owner to be {}, not {}",
"stake is not active",
"redelegating to the same vote account not permitted",
"invalid stake account data",
"Unable to merge due to metadata mismatch",
"Unable to merge due to voter mismatch",
"Unable to merge due to stake deactivation",
"invalid proof data",
"proof verification failed: {}",
"proof_verification failed: {}",
"CloseContextState",
"VerifyZeroBalance",
"VerifyWithdraw",
"VerifyCiphertextCiphertextEquality",
"VerifyTransfer",
"VerifyTransferWithFee",
"VerifyPubkeyValidity",
"VerifyRangeProof",
"VerifyBatchedRangeProof64",
"VerifyBatchedRangeProof128",
"VerifyBatchedRangeProof256",
"VerifyCiphertextCommitmentEquality",
"VerifyGroupedCiphertext2HandlesValidity",
"VerifyBatchedGroupedCiphertext2HandlesValidity",
"VerifyFeeSigma",
"VerifyGroupedCiphertext3HandlesValidity",
"VerifyBatchedGroupedCiphertext3HandlesValidity",
"Create: address {} does not match derived address {}",
"Allocate: 'to' account {} must sign",
"Allocate: account {} already in use",
"Allocate: requested {}, max allowed {}",
"Assign: account {} must sign",
"Create Account: account {} already in use",
"Transfer: `from` must not carry data",
"Transfer: insufficient lamports {}, need {}",
"Transfer: `from` account {} must sign",
"Transfer: 'from' account {} must sign",
"Transfer: 'from' address {} does not match derived address {}",
"Advance nonce account: recent blockhash list is empty",
"Initialize nonce account: recent blockhash list is empty",
"Advance nonce account: Account {} must be writeable",
"Advance nonce account: Account {} must be a signer",
"Advance nonce account: nonce can only advance once per slot",
"Advance nonce account: Account {} state is invalid",
"Withdraw nonce account: Account {} must be writeable",
"Withdraw nonce account: insufficient lamports {}, need {}",
"Withdraw nonce account: nonce can only advance once per slot",
"Withdraw nonce account: Account {} must sign",
"Initialize nonce account: Account {} must be writeable",
"Initialize nonce account: insufficient lamports {}, need {}",
"Initialize nonce account: Account {} state is invalid",
"Authorize nonce account: Account {} must be writeable",
"Authorize nonce account: Account {} state is invalid",
"Authorize nonce account: Account {} must sign",
"Failed to get runtime environment: {}",
"Failed to register syscalls: {}",
"Write overflow: {} < {}",
"Poseidon hashing {} sequences is not supported",
"Overflow while calculating the compute cost",
"{} Hashing {} sequences in one syscall is over the limit {}",
"Invalid account info pointer `{}': {} != {}",
"Internal error: index mismatch for account {}",
"Account data size realloc limited to {} in inner instructions",
"Table account must not be allocated",
"Authority account must be a signer",
"Payer account must be a signer",
"{} is not a recent slot",
"Table address must match derived address: {}",
"Lookup table is already frozen",
"Deactivated tables cannot be frozen",
"Empty lookup tables cannot be frozen",
"Deactivated tables cannot be extended",
"Lookup table is full and cannot contain more addresses",
"Must extend with at least one address",
"Extended lookup table length {} would exceed max capacity of {}",
"Lookup table is frozen",
"Lookup table is already deactivated",
"Lookup table cannot be the recipient of reclaimed lamports",
"Lookup table is not deactivated",
"Table cannot be closed until it's fully deactivated in {} blocks",
];
const prepareLineForRegex = (l: string) =>
l.replaceAll("{}", ".*").replaceAll("(", "\\(").replaceAll(")", "\\)").replaceAll("]", "\\]").replaceAll("[", "\\[");
const regexTemplate = `\
(?<logTruncated>^Log truncated$)|\
(?<programInvoke>^Program (?<invokeProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) invoke \\[(?<level>\\d+)\\]$)|\
(?<programSuccessResult>^Program (?<successResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) success$)|\
(?<programFailedResult>^Program (?<failedResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) failed: (?<failedResultErr>.*)$)|\
(?<programCompleteFailedResult>^Program failed to complete: (?<failedCompleteError>.*)$)|\
(?<programLog>^Program log: (?<logMessage>.*)$)|\
(?<programData>^Program data: (?<data>.*)$)|\
(?<programConsumed>^Program (?<consumedProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) consumed (?<consumedComputeUnits>\\d*) of (?<allComputedUnits>\\d*) compute units$)|\
(?<programReturn>^Program return: (?<returnProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) (?<returnMessage>.*)$)|\
(?<errorMessage>^(${knownMsgs.map(prepareLineForRegex).join("|")})$)`;
return new RegExp(regexTemplate, "s");
}
type ImmediateLogContext = {
id: number;
currentInstruction: number;
currentDepth: number;
};
type FullLogContext = {
immediate: ImmediateLogContext;
callStack: string[];
callIds: number[];
};
function programEnter(context: FullLogContext, invokedProgram: string, invokeLevel: number): ProgramLogContext {
if (invokeLevel != context.immediate.currentDepth + 1)
throw new Error(`invoke depth mismatch, log: ${invokeLevel}, expected: ${context.immediate.currentDepth}`);
context.immediate.id += 1;
context.immediate.currentDepth += 1;
context.callStack.push(invokedProgram);
context.callIds.push(context.immediate.id);
return newLogContext(invokedProgram, invokeLevel, context.immediate.id, context.immediate.currentInstruction);
}
function programExit(context: FullLogContext, exitedProgram: string): number {
const lastProgram = context.callStack.pop();
const lastCallIndex = context.callIds.pop();
if (lastCallIndex === undefined) throw new Error("callIds malformed");
if (lastProgram != exitedProgram) throw new Error("[ProramExit] callstack mismatch");
context.immediate.currentDepth -= 1;
if (context.immediate.currentDepth === 0) {
context.immediate.currentInstruction += 1;
}
return lastCallIndex;
}
/**
* Parses transaction logs and provides additional context such as
* - programId that generated the message
* - call id of instruction, that generated the message
* - call depth of instruction
* - data messages, log messages and error messages
* @param logs logs from TransactionResponse.meta.logs
* @returns parsed logs with call depth and additional context
*/
export function parseLogs(logs: string[]): ProgramLogContext[] {
const parserRe = generateLogsParsingRegex();
const programLogs: ProgramLogContext[] = [];
const immediate: ImmediateLogContext = {
id: -1,
currentInstruction: 0,
currentDepth: 0,
};
const context: FullLogContext = {
immediate,
callStack: [],
callIds: [],
};
const getCurrentCallId = (c: FullLogContext) => c.callIds[c.callIds.length - 1];
const getCurrentProgram = (c: FullLogContext) => c.callStack[c.callStack.length - 1];
for (const log of logs) {
const match = parserRe.exec(log);
if (!match || !match.groups) {
throw new Error(`Failed to parse log line: ${log}`);
}
if (match.groups.programInvoke) {
const program = match.groups.invokeProgramId;
const level = Number(match.groups.level);
const newProgramContext = programEnter(context, program, level);
newProgramContext.rawLogs.push(log);
programLogs.push(newProgramContext);
} else if (match.groups.programSuccessResult) {
const lastCallIndex = programExit(context, match.groups.successResultProgramId);
programLogs[lastCallIndex].rawLogs.push(log);
} else if (match.groups.programFailedResult) {
const lastCallIndex = programExit(context, match.groups.failedResultProgramId);
programLogs[lastCallIndex].rawLogs.push(log);
programLogs[lastCallIndex].errors.push(log);
} else if (match.groups.programCompleteFailedResult) {
const currentCall = getCurrentCallId(context);
programLogs[currentCall].rawLogs.push(log);
programLogs[currentCall].errors.push(match.groups.failedCompleteError);
} else {
const currentCall = getCurrentCallId(context);
programLogs[currentCall].rawLogs.push(log);
if (match.groups.logTruncated) {
programLogs[currentCall].invokeResult = "Log truncated";
} else if (match.groups.programLog) {
programLogs[currentCall].logMessages.push(match.groups.logMessage);
} else if (match.groups.programData) {
programLogs[currentCall].dataLogs.push(match.groups.data);
} else if (match.groups.programConsumed) {
programLogs[currentCall].unitsConsumed = Number(match.groups.consumedComputeUnits);
} else if (match.groups.programReturn) {
const returnProgram = match.groups.returnProgramId;
if (getCurrentProgram(context) != returnProgram) throw new Error("[InvokeReturn]: callstack mismatch");
programLogs[currentCall].invokeResult = match.groups.returnMessage;
} else if (match.groups.errorMessage) {
programLogs[currentCall].errors.push(log);
}
}
}
return programLogs;
}
/** Python script to extract native solana logs
*
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
import os
regex = r"ic_msg!\((\s|.)*?,\s*?\"(?P<log>.*?)\""
def print_logs(data):
matches = re.finditer(regex, data)
for m in matches:
print(m.group('log'))
def open_files_in_directory(directory):
for root, dirs, files in os.walk(directory):
for filename in files:
if '.rs' not in filename:
continue
file_path = os.path.join(root, filename)
try:
with open(file_path, 'r', encoding='utf-8') as file:
print(f'Opened file: {file_path}')
content = file.read()
print_logs(content)
except Exception as e:
print(f'Could not open file {file_path}: {e}')
for d in dirs:
if d == '.' or d == '..':
continue
open_files_in_directory(os.path.join(root, d))
if __name__ == "__main__":
open_files_in_directory('INPUT DIR HERE')
*/