Skip to content

Commit b29798c

Browse files
authored
Merge pull request #30 from Terran-One/fix/missing-error-traces
Add missing error TraceLogs on instantiate & execute
2 parents 5247de7 + d6deb29 commit b29798c

File tree

2 files changed

+131
-85
lines changed

2 files changed

+131
-85
lines changed

src/modules/wasm.spec.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Coin } from '@cosmjs/amino';
22
import { toAscii, toBase64 } from '@cosmjs/encoding';
3-
import { Result } from 'ts-results';
3+
import { Err, Result } from 'ts-results';
44
import { cmd, exec, TestContract, TestContractInstance } from '../../testing/wasm-util';
55
import { CWSimulateApp } from '../CWSimulateApp';
66
import { AppResponse, Event, ReplyOn, TraceLog } from '../types';
@@ -486,6 +486,59 @@ describe('TraceLog', () => {
486486
},
487487
]);
488488
});
489+
490+
it('traces errors', async () => {
491+
const trace: TraceLog[] = [];
492+
493+
const executeMsg1 = {
494+
run: { failure: true }
495+
};
496+
await testContract.execute(
497+
info.sender,
498+
executeMsg1,
499+
[],
500+
trace,
501+
);
502+
503+
const err1 = { ok: false, err: true };
504+
const ref1 = {
505+
type: 'execute',
506+
contractAddress: testContract.address,
507+
msg: executeMsg1,
508+
info: {
509+
sender: info.sender,
510+
funds: [],
511+
},
512+
response: err1,
513+
result: err1,
514+
};
515+
expect(trace).toMatchObject([ ref1 ]);
516+
517+
const funds = [{ denom: 'uluna', amount: '99999999999999999999' }];
518+
const executeMsg2 = {
519+
debug: { msg: 'foobar' }
520+
};
521+
await testContract.execute(
522+
info.sender,
523+
executeMsg2,
524+
funds,
525+
trace,
526+
);
527+
528+
const err2 = { ok: false, err: true };
529+
const ref2 = {
530+
type: 'execute',
531+
contractAddress: testContract.address,
532+
msg: executeMsg2,
533+
info: {
534+
sender: info.sender,
535+
funds,
536+
},
537+
response: err2,
538+
result: err2,
539+
};
540+
expect(trace).toMatchObject([ ref1, ref2 ]);
541+
});
489542
});
490543

491544
describe('Query', () => {

src/modules/wasm/module.ts

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { toBech32 } from '@cosmjs/encoding';
2+
import { Map } from 'immutable';
3+
import { Err, Ok, Result } from 'ts-results';
24
import type { CWSimulateApp } from '../../CWSimulateApp';
3-
5+
import { NEVER_IMMUTIFY, Transactional, TransactionalLens } from '../../store/transactional';
46
import {
57
AppResponse,
68
Binary,
@@ -11,19 +13,18 @@ import {
1113
ContractResponse,
1214
Event,
1315
ExecuteEnv,
16+
ExecuteTraceLog,
1417
ReplyMsg,
1518
ReplyOn,
19+
ReplyTraceLog,
1620
SubMsg,
1721
TraceLog,
1822
DebugLog,
1923
Snapshot,
2024
} from '../../types';
21-
import { Map } from 'immutable';
22-
import { Err, Ok, Result } from 'ts-results';
2325
import { fromBinary, toBinary } from '../../util';
24-
import { NEVER_IMMUTIFY, Transactional, TransactionalLens } from '../../store/transactional';
25-
import { buildAppResponse, buildContractAddress } from './wasm-util';
2626
import Contract from './contract';
27+
import { buildAppResponse, buildContractAddress } from './wasm-util';
2728

2829
type WasmData = {
2930
lastCodeId: number;
@@ -209,16 +210,31 @@ export class WasmModule {
209210
trace: TraceLog[] = []
210211
): Promise<Result<AppResponse, string>> {
211212
return await this.chain.pushBlock(async () => {
212-
const snapshot = this.store.db.data;
213-
214213
// first register the contract instance
215214
const contractAddress = this.registerContractInstance(sender, codeId, label).unwrap();
216215
let logs = [] as DebugLog[];
217216

218217
const contract = await this.getContract(contractAddress).init();
218+
const tracebase: Omit<ExecuteTraceLog, 'response' | 'result'> = {
219+
[NEVER_IMMUTIFY]: true,
220+
type: 'instantiate',
221+
contractAddress,
222+
msg: instantiateMsg,
223+
info: { sender, funds },
224+
logs,
225+
env: contract.getExecutionEnv(),
226+
storeSnapshot: this.store.db.data,
227+
}
219228

220229
const send = this.chain.bank.send(sender, contract.address, funds);
221-
if (send.err) return send;
230+
if (send.err) {
231+
trace.push({
232+
...tracebase,
233+
response: send,
234+
result: send,
235+
});
236+
return send;
237+
}
222238

223239
// then call instantiate
224240
let response = contract.instantiate(
@@ -229,23 +245,12 @@ export class WasmModule {
229245
);
230246

231247
if (response.err) {
232-
let result = Err(response.val);
233248
trace.push({
234-
[NEVER_IMMUTIFY]: true,
235-
type: 'instantiate' as 'instantiate',
236-
contractAddress,
237-
msg: instantiateMsg,
249+
...tracebase,
238250
response,
239-
info: {
240-
sender,
241-
funds,
242-
},
243-
env: this.getExecutionEnv(contractAddress),
244-
logs,
245-
storeSnapshot: snapshot,
246-
result,
251+
result: response,
247252
});
248-
return result;
253+
return response;
249254
}
250255
else {
251256
let customEvent: Event = {
@@ -271,20 +276,11 @@ export class WasmModule {
271276
);
272277

273278
trace.push({
274-
[NEVER_IMMUTIFY]: true,
275-
type: 'instantiate' as 'instantiate',
276-
contractAddress,
277-
msg: instantiateMsg,
279+
...tracebase,
278280
response,
279-
info: {
280-
sender,
281-
funds,
282-
},
283-
env: this.getExecutionEnv(contractAddress),
284-
logs,
281+
result,
285282
trace: subtrace,
286283
storeSnapshot: this.store.db.data,
287-
result,
288284
});
289285

290286
return result;
@@ -301,13 +297,29 @@ export class WasmModule {
301297
trace: TraceLog[] = []
302298
): Promise<Result<AppResponse, string>> {
303299
return await this.chain.pushBlock(async () => {
304-
const snapshot = this.store.db.data;
305300
const contract = await this.getContract(contractAddress).init();
306-
const env = contract.getExecutionEnv();
307301
const logs: DebugLog[] = [];
302+
303+
const tracebase: Omit<ExecuteTraceLog, 'response' | 'result'> = {
304+
[NEVER_IMMUTIFY]: true,
305+
type: 'execute',
306+
contractAddress,
307+
msg: executeMsg,
308+
logs,
309+
env: contract.getExecutionEnv(),
310+
info: { sender, funds },
311+
storeSnapshot: this.store.db.data,
312+
}
308313

309314
const send = this.chain.bank.send(sender, contractAddress, funds);
310-
if (send.err) return send;
315+
if (send.err) {
316+
trace.push({
317+
...tracebase,
318+
response: send,
319+
result: send,
320+
});
321+
return send;
322+
}
311323

312324
const response = contract.execute(
313325
sender,
@@ -317,25 +329,12 @@ export class WasmModule {
317329
);
318330

319331
if (response.err) {
320-
const result = Err(response.val);
321-
322332
trace.push({
323-
[NEVER_IMMUTIFY]: true,
324-
type: 'execute' as 'execute',
325-
contractAddress,
326-
msg: executeMsg,
333+
...tracebase,
327334
response,
328-
env,
329-
info: {
330-
sender,
331-
funds,
332-
},
333-
logs,
334-
storeSnapshot: snapshot,
335-
result,
335+
result: response,
336336
});
337-
338-
return result;
337+
return response;
339338
}
340339
else {
341340
let customEvent = {
@@ -363,20 +362,11 @@ export class WasmModule {
363362
);
364363

365364
trace.push({
366-
[NEVER_IMMUTIFY]: true,
367-
type: 'execute' as 'execute',
368-
contractAddress,
369-
msg: executeMsg,
365+
...tracebase,
370366
response,
371-
info: {
372-
sender,
373-
funds,
374-
},
375-
env,
367+
result,
376368
trace: subtrace,
377-
logs,
378369
storeSnapshot: this.store.db.data,
379-
result,
380370
});
381371

382372
return result;
@@ -490,25 +480,25 @@ export class WasmModule {
490480
): Promise<Result<AppResponse, string>> {
491481
const logs: DebugLog[] = [];
492482
const contract = this.getContract(contractAddress);
493-
const env = contract.getExecutionEnv();
494483
const response = contract.reply(replyMsg, logs);
495484

485+
const tracebase: Omit<ReplyTraceLog, 'response' | 'result'> = {
486+
[NEVER_IMMUTIFY]: true,
487+
type: 'reply',
488+
contractAddress,
489+
msg: replyMsg,
490+
env: contract.getExecutionEnv(),
491+
logs,
492+
storeSnapshot: this.store.db.data,
493+
}
494+
496495
if (response.err) {
497-
const result = Err(response.val);
498-
499496
trace.push({
500-
[NEVER_IMMUTIFY]: true,
501-
type: 'reply' as 'reply',
502-
contractAddress,
503-
env,
504-
msg: replyMsg,
497+
...tracebase,
505498
response,
506-
logs,
507-
storeSnapshot: this.store.db.data,
508-
result,
499+
result: response,
509500
});
510-
511-
return result;
501+
return response;
512502
}
513503
else {
514504
const customEvent = {
@@ -541,16 +531,10 @@ export class WasmModule {
541531
);
542532

543533
trace.push({
544-
[NEVER_IMMUTIFY]: true,
545-
type: 'reply' as 'reply',
546-
contractAddress,
547-
msg: replyMsg,
548-
env,
534+
...tracebase,
549535
response,
550-
trace: subtrace,
551-
logs,
552-
storeSnapshot: this.store.db.data,
553536
result,
537+
storeSnapshot: this.store.db.data,
554538
});
555539

556540
return result;
@@ -659,6 +643,15 @@ export class WasmModule {
659643
return storage ? lensFromSnapshot(storage) : this.store;
660644
}
661645

646+
protected pushTrace(traces: TraceLog[], details: Omit<TraceLog, typeof NEVER_IMMUTIFY | 'env'>) {
647+
//@ts-ignore
648+
traces.push({
649+
[NEVER_IMMUTIFY]: true,
650+
...details,
651+
env: this.getExecutionEnv(details.contractAddress),
652+
});
653+
}
654+
662655
get lastCodeId() { return this.store.get('lastCodeId') }
663656
get lastInstanceId() { return this.store.get('lastInstanceId') }
664657
}

0 commit comments

Comments
 (0)