Skip to content

Commit 8a85fb9

Browse files
committed
adds unit tests
Signed-off-by: Konstantina Blazhukova <[email protected]>
1 parent b434d9f commit 8a85fb9

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

packages/relay/tests/lib/debug.spec.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,100 @@ describe('Debug API Test Suite', async function () {
489489
}
490490
});
491491

492+
describe('prestateTracer', async function () {
493+
const prestateTracer: TracerType = TracerType.PrestateTracer;
494+
const mockPrestateResult = {
495+
'0xc37f417fa09933335240fca72dd257bfbde9c275': {
496+
balance: '0x100000000',
497+
nonce: 2,
498+
code: '0x',
499+
storage: {},
500+
},
501+
'0x637a6a8e5a69c087c24983b05261f63f64ed7e9b': {
502+
balance: '0x200000000',
503+
nonce: 1,
504+
code: '0x608060405234801561001057600080fd5b50600436106100415760003560e01c8063',
505+
storage: {
506+
'0x0': '0x1',
507+
'0x1': '0x2',
508+
},
509+
},
510+
};
511+
512+
beforeEach(() => {
513+
sinon.stub(debugService, 'prestateTracer').resolves(mockPrestateResult);
514+
});
515+
516+
afterEach(() => {
517+
sinon.restore();
518+
});
519+
520+
it('should successfully trace transaction with prestateTracer', async function () {
521+
const tracerObject = { tracer: prestateTracer };
522+
const result = await debugService.traceTransaction(transactionHash, tracerObject, requestDetails);
523+
524+
expect(result).to.deep.equal(mockPrestateResult);
525+
expect(result).to.be.an('object');
526+
expect(Object.keys(result)).to.have.lengthOf(2);
527+
528+
for (const address of Object.keys(result)) {
529+
expect(result[address]).to.have.all.keys(['balance', 'nonce', 'code', 'storage']);
530+
expect(result[address].nonce).to.be.a('number');
531+
expect(result[address].code).to.exist;
532+
expect(result[address].storage).to.be.an('object');
533+
}
534+
});
535+
536+
it('should trace transaction with prestateTracer and onlyTopCall=true', async function () {
537+
const tracerObject = { tracer: prestateTracer, tracerConfig: { onlyTopCall: true } };
538+
const result = await debugService.traceTransaction(transactionHash, tracerObject, requestDetails);
539+
540+
expect(result).to.deep.equal(mockPrestateResult);
541+
542+
// Verify that prestateTracer was called with onlyTopCall=true
543+
const prestateTracerStub = debugService.prestateTracer as sinon.SinonStub;
544+
expect(prestateTracerStub.calledOnce).to.be.true;
545+
expect(prestateTracerStub.calledWith(transactionHash, true, requestDetails)).to.be.true;
546+
});
547+
548+
it('should trace transaction with prestateTracer and onlyTopCall=false (default)', async function () {
549+
const tracerObject = { tracer: prestateTracer, tracerConfig: { onlyTopCall: false } };
550+
const result = await debugService.traceTransaction(transactionHash, tracerObject, requestDetails);
551+
552+
expect(result).to.deep.equal(mockPrestateResult);
553+
554+
// Verify that prestateTracer was called with onlyTopCall=false
555+
const prestateTracerStub = debugService.prestateTracer as sinon.SinonStub;
556+
expect(prestateTracerStub.calledOnce).to.be.true;
557+
expect(prestateTracerStub.calledWith(transactionHash, false, requestDetails)).to.be.true;
558+
});
559+
560+
it('should handle empty prestate result', async function () {
561+
const emptyResult = {};
562+
(debugService.prestateTracer as sinon.SinonStub).resolves(emptyResult);
563+
564+
const tracerObject = { tracer: prestateTracer };
565+
const result = await debugService.traceTransaction(transactionHash, tracerObject, requestDetails);
566+
567+
expect(result).to.deep.equal(emptyResult);
568+
expect(result).to.be.an('object');
569+
expect(Object.keys(result)).to.have.lengthOf(0);
570+
});
571+
572+
it('should propagate errors from prestateTracer', async function () {
573+
const expectedError = predefined.RESOURCE_NOT_FOUND('Failed to retrieve contract results');
574+
(debugService.prestateTracer as sinon.SinonStub).rejects(expectedError);
575+
576+
const tracerObject = { tracer: prestateTracer };
577+
578+
await RelayAssertions.assertRejection(expectedError, debugService.traceTransaction, true, debugService, [
579+
transactionHash,
580+
tracerObject,
581+
requestDetails,
582+
]);
583+
});
584+
});
585+
492586
describe('Invalid scenarios', async function () {
493587
let notFound: { _status: { messages: { message: string }[] } };
494588

0 commit comments

Comments
 (0)