Skip to content

Commit ccd7f34

Browse files
authored
Merge pull request #131 from paritytech/pg/selfdestruct2
add selfdestruct tests
2 parents 4dc2658 + 6d3a3bd commit ccd7f34

File tree

7 files changed

+252
-19
lines changed

7 files changed

+252
-19
lines changed

contracts/Tracing.sol

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ contract TracingCaller {
2828
this.start(counter - 1);
2929
}
3030

31+
function destruct() external {
32+
address recipient = msg.sender;
33+
assembly {
34+
selfdestruct(recipient)
35+
}
36+
}
37+
38+
function create_and_destruct() public {
39+
TracingCallee newCallee = new TracingCallee();
40+
newCallee.destruct();
41+
}
42+
3143
function create() external returns (address) {
3244
TracingCallee newCallee = new TracingCallee();
3345
return address(newCallee);
@@ -52,6 +64,13 @@ contract TracingCallee {
5264
emit CalleeCalled(counter);
5365
}
5466

67+
function destruct() external {
68+
address recipient = msg.sender;
69+
assembly {
70+
selfdestruct(recipient)
71+
}
72+
}
73+
5574
function failingFunction() external payable {
5675
require(false, "This function always fails");
5776
}

deno.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"imports": {
2424
"@std/cli": "jsr:@std/cli@^1.0.23",
2525
"@std/encoding": "jsr:@std/encoding@^1.0.10",
26-
"viem": "npm:viem@^2.38.3",
27-
"viem/": "npm:/viem@^2.38.3/",
26+
"viem": "npm:viem@^2.39.0",
27+
"viem/": "npm:/viem@^2.39.0/",
2828
"solc": "npm:solc@^0.8.30",
29-
"@parity/resolc": "npm:@parity/resolc@^0.4.1",
29+
"@parity/resolc": "npm:@parity/resolc@^0.5.0",
3030
"@std/assert": "jsr:@std/assert@^1.0.15",
3131
"@std/expect": "jsr:@std/expect@^1.0.17",
3232
"@std/testing": "jsr:@std/testing@^1.0.16",
3333
"@std/path": "jsr:@std/path@^1.1.2",
34-
"@std/log": "jsr:@std/log@^0.224"
34+
"@std/log": "jsr:@std/log@^0.224.14"
3535
},
3636
"compilerOptions": {
3737
"strict": true

deno.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__snapshots__/all-tests.ts.snap

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,73 @@ snapshot[`call-trace debug_traceCall 1`] = `
467467
}
468468
`;
469469

470+
snapshot[`call-trace selfdestruct 1`] = `
471+
{
472+
calls: [
473+
{
474+
from: "<contract_addr>",
475+
gas: "<gas>",
476+
gasUsed: "<gas>",
477+
input: "0x",
478+
to: "0x0000000000000000000000000000000000000000",
479+
type: "SELFDESTRUCT",
480+
value: "<selfdestruct_value>",
481+
},
482+
],
483+
from: "0x0000000000000000000000000000000000000000",
484+
gas: "<gas>",
485+
gasUsed: "<gas>",
486+
input: "0x2b68b9c6",
487+
to: "<contract_addr>",
488+
type: "CALL",
489+
value: "0x0",
490+
}
491+
`;
492+
493+
snapshot[`call-trace create_and_destruct 1`] = `
494+
{
495+
calls: [
496+
{
497+
from: "<contract_addr>",
498+
gas: "<gas>",
499+
gasUsed: "<gas>",
500+
input: "<tracing_callee_init_code>",
501+
output: "<tracing_callee_runtime_code>",
502+
to: "<addr>",
503+
type: "CREATE",
504+
value: "0x0",
505+
},
506+
{
507+
calls: [
508+
{
509+
from: "<addr>",
510+
gas: "<gas>",
511+
gasUsed: "<gas>",
512+
input: "0x",
513+
to: "<contract_addr>",
514+
type: "SELFDESTRUCT",
515+
value: "<selfdestruct_value>",
516+
},
517+
],
518+
from: "<contract_addr>",
519+
gas: "<gas>",
520+
gasUsed: "<gas>",
521+
input: "0x2b68b9c6",
522+
to: "<addr>",
523+
type: "CALL",
524+
value: "0x0",
525+
},
526+
],
527+
from: "0x0000000000000000000000000000000000000000",
528+
gas: "<gas>",
529+
gasUsed: "<gas>",
530+
input: "0xfadbb018",
531+
to: "<contract_addr>",
532+
type: "CALL",
533+
value: "0x0",
534+
}
535+
`;
536+
470537
snapshot[`prestate deploy_contract.diff 1`] = `
471538
{
472539
post: {
@@ -1050,3 +1117,57 @@ snapshot[`prestate trace_tx_write_storage_twice.no_diff 1`] = `
10501117
},
10511118
}
10521119
`;
1120+
1121+
snapshot[`prestate selfdestruct.diff 1`] = `
1122+
{
1123+
post: {
1124+
"<tracing_contract_addr>": {
1125+
balance: "<balance>",
1126+
},
1127+
},
1128+
pre: {
1129+
"<tracing_contract_addr>": {
1130+
balance: "<balance>",
1131+
code: "<code>",
1132+
nonce: "<nonce>",
1133+
},
1134+
},
1135+
}
1136+
`;
1137+
1138+
snapshot[`prestate selfdestruct.no_diff 1`] = `
1139+
{
1140+
"<tracing_contract_addr>": {
1141+
balance: "<balance>",
1142+
code: "<code>",
1143+
nonce: "<nonce>",
1144+
},
1145+
}
1146+
`;
1147+
1148+
snapshot[`prestate create_and_destruct.diff 1`] = `
1149+
{
1150+
post: {
1151+
"<tracing_contract_addr>": {
1152+
nonce: "<nonce>",
1153+
},
1154+
},
1155+
pre: {
1156+
"<tracing_contract_addr>": {
1157+
balance: "<balance>",
1158+
code: "<code>",
1159+
nonce: "<nonce>",
1160+
},
1161+
},
1162+
}
1163+
`;
1164+
1165+
snapshot[`prestate create_and_destruct.no_diff 1`] = `
1166+
{
1167+
"<tracing_contract_addr>": {
1168+
balance: "<balance>",
1169+
code: "<code>",
1170+
nonce: "<nonce>",
1171+
},
1172+
}
1173+
`;

src/test-setup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ export async function setupTests() {
5757
]
5858

5959
await killProcessOnPort(8545)
60+
61+
const versionOutput = await new Deno.Command(geth, {
62+
args: ['version'],
63+
stdout: 'piped',
64+
stderr: 'piped',
65+
}).output()
66+
const version = new TextDecoder().decode(versionOutput.stdout)
67+
console.log(version)
6068
console.log('🚀 Start geth...')
6169
const gethProcess = new Deno.Command(geth, {
6270
args: gethArgs,

src/tracing-call-trace.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,43 @@ Deno.test('call-trace debug_traceCall', opts, async (t) => {
239239

240240
await matchFixture(t, res)
241241
})
242+
243+
Deno.test('call-trace selfdestruct', opts, async (t) => {
244+
const tracingCallerAddr = await getTracingCallerAddr()
245+
const res = await env.debugClient.traceCall(
246+
{
247+
to: tracingCallerAddr,
248+
data: encodeFunctionData({
249+
abi: TracingCallerAbi,
250+
functionName: 'destruct',
251+
args: [],
252+
}),
253+
},
254+
'callTracer',
255+
{ withLog: true },
256+
)
257+
258+
await matchFixture(t, res)
259+
})
260+
261+
Deno.test(
262+
'call-trace create_and_destruct',
263+
opts,
264+
async (t) => {
265+
const tracingCallerAddr = await getTracingCallerAddr()
266+
const res = await env.debugClient.traceCall(
267+
{
268+
to: tracingCallerAddr,
269+
data: encodeFunctionData({
270+
abi: TracingCallerAbi,
271+
functionName: 'create_and_destruct',
272+
args: [],
273+
}),
274+
},
275+
'callTracer',
276+
{ withLog: true },
277+
)
278+
279+
await matchFixture(t, res)
280+
},
281+
)

src/tracing-prestate.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
getPretraceFixtureReceipt,
1717
getTracingCallerAddr,
1818
} from './deploy_contracts.ts'
19+
import { TracingCallerAbi } from '../codegen/abi/TracingCaller.ts'
1920

2021
const getBlock = memoized(async () => {
2122
const receipt = await getPretraceFixtureReceipt()
@@ -445,3 +446,47 @@ Deno.test(
445446
})
446447
}),
447448
)
449+
450+
Deno.test(
451+
'prestate selfdestruct',
452+
opts,
453+
withDiffModes(async (t, config, diffMode) => {
454+
const tracingCallerAddr = await getTracingCallerAddr()
455+
const res = await env.debugClient.traceCall(
456+
{
457+
to: tracingCallerAddr,
458+
data: encodeFunctionData({
459+
abi: TracingCallerAbi,
460+
functionName: 'destruct',
461+
args: [],
462+
}),
463+
},
464+
'prestateTracer',
465+
config,
466+
)
467+
await matchFixture(t, res, diffMode)
468+
}),
469+
)
470+
471+
Deno.test(
472+
'prestate create_and_destruct',
473+
opts,
474+
withDiffModes(
475+
async (t, config, diffMode) => {
476+
const tracingCallerAddr = await getTracingCallerAddr()
477+
const res = await env.debugClient.traceCall(
478+
{
479+
to: tracingCallerAddr,
480+
data: encodeFunctionData({
481+
abi: TracingCallerAbi,
482+
functionName: 'create_and_destruct',
483+
args: [],
484+
}),
485+
},
486+
'prestateTracer',
487+
config,
488+
)
489+
await matchFixture(t, res, diffMode)
490+
},
491+
),
492+
)

0 commit comments

Comments
 (0)