Skip to content

Commit b0f872e

Browse files
committed
nit
1 parent a60e972 commit b0f872e

File tree

7 files changed

+176
-10
lines changed

7 files changed

+176
-10
lines changed

contracts/Pretrace.sol

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/// @title PretraceFixture
5+
/// @notice A simple contract to exercise storage, balance, and nonce changes
6+
contract PretraceFixture {
7+
uint256 public value;
8+
mapping(address => uint256) public balances;
9+
10+
constructor() payable {
11+
balances[msg.sender] = msg.value;
12+
value = 42;
13+
}
14+
15+
function writeStorage(uint256 _value) external {
16+
value = _value;
17+
}
18+
19+
function readStorage() external view returns (uint256) {
20+
return value;
21+
}
22+
23+
function deposit() external payable {
24+
balances[msg.sender] += msg.value;
25+
}
26+
27+
function withdraw(uint256 amount) external {
28+
require(balances[msg.sender] >= amount, 'Insufficient balance');
29+
balances[msg.sender] -= amount;
30+
(bool sent, ) = msg.sender.call{value: amount}('');
31+
require(sent, 'Transfer failed');
32+
}
33+
34+
function getContractBalance() external view returns (uint256) {
35+
return address(this).balance;
36+
}
37+
38+
function getExternalBalance(
39+
address account
40+
) external view returns (uint256) {
41+
return account.balance;
42+
}
43+
44+
function createChild() external returns (address) {
45+
PretraceFixtureChild c = new PretraceFixtureChild();
46+
return address(c);
47+
}
48+
49+
function callContract(address childAddr) external {
50+
PretraceFixtureChild(childAddr).increment();
51+
}
52+
53+
function delegatecallContract(address childAddr) external {
54+
(bool success, ) = childAddr.delegatecall(
55+
abi.encodeWithSelector(PretraceFixtureChild.increment.selector)
56+
);
57+
require(success, "Delegatecall failed");
58+
}}
59+
60+
/// @title Child
61+
/// @notice A disposable child contract used to test contract deployment and calls
62+
contract PretraceFixtureChild {
63+
uint256 public value = 1;
64+
65+
function increment() external {
66+
value += 1;
67+
}
68+
69+
}

contracts/Storage.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ contract Storage {
1616
n2 = 100;
1717
}
1818

19+
function gethash() public view returns (bytes32) {
20+
return blockhash(block.number);
21+
}
22+
1923
/**
2024
* @dev Store value in variable
2125
* @param num value to store

src/cli/dao-hack-traces.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import {
1212
} from 'viem'
1313
import { Dao, DaoAttacker } from '../codegen/addresses.ts'
1414

15-
const result = await env.debugClient.traceCall({
16-
to: DaoAttacker,
17-
data: encodeFunctionData({
18-
abi: abis.DaoAttacker,
19-
functionName: 'attack',
20-
args: [],
21-
}),
22-
})
15+
const result = await env.debugClient.traceCall(
16+
{
17+
to: DaoAttacker,
18+
data: encodeFunctionData({
19+
abi: abis.DaoAttacker,
20+
functionName: 'attack',
21+
args: [],
22+
}),
23+
},
24+
'callTracer'
25+
)
2326
console.log(JSON.stringify(visit(result, visitor), null, 2))
2427

2528
function visitor(key: string, value: any) {

src/cli/dao-playground.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Run with
22
// deno task build --filter dao
3-
// deno --env-file --allow-all ./cli/dao-playground.ts
3+
// deno --env-file --allow-all ./src/cli/dao-playground.ts
44
import { env } from '../tools/lib/index.ts'
55
import { abis } from '../codegen/abis.ts'
66
import { Dao, DaoAttacker } from '../codegen/addresses.ts'

src/cli/playground.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// deno --env-file --allow-all src/cli/playground.ts
2+
import { env } from '../tools/lib/index.ts'
3+
import { abis } from '../codegen/abis.ts'
4+
import { Storage } from '../codegen/addresses.ts'
5+
6+
{
7+
const result = await env.wallet.readContract({
8+
address: Storage,
9+
abi: abis.Storage,
10+
functionName: 'gethash',
11+
args: [],
12+
})
13+
console.log(result)
14+
}

src/cli/pretrace-playground.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Run with
2+
// deno task build --filter storage
3+
// deno --env-file --allow-all src/cli/pretrace-playground.ts
4+
import { env } from '../tools/lib/index.ts'
5+
import { abis } from '../codegen/abis.ts'
6+
import { PretraceFixture } from '../codegen/addresses.ts'
7+
import { encodeFunctionData } from 'viem'
8+
9+
{
10+
// const res = await env.debugClient.traceCall(
11+
// {
12+
// to: PretraceFixture,
13+
// data: encodeFunctionData({
14+
// abi: abis.Storage,
15+
// functionName: 'write_n1_read_n2',
16+
// args: [BigInt(2n)],
17+
// }),
18+
// },
19+
// 'prestateTracer',
20+
// { diffMode: false, disableCode: true }
21+
// )
22+
//
23+
//
24+
25+
const res = await env.debugClient.traceCall(
26+
{
27+
data: '0x68874f1f0000000000000000000000003ed62137c5db927cb137c26455969116bf0c23cb',
28+
from:"0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac",
29+
to:"0xc01ee7f10ea4af4673cfff62710e1d7792aba8f3"
30+
},
31+
"0xe969d81cd5a97ce04a512fb73cb78b9a630eba6bb49a9141aa850ca5201f700c",
32+
33+
'prestateTracer',
34+
{"tracer":"prestateTracer","tracerConfig":{"diffMode":false}}
35+
36+
)
37+
}
38+
//
39+
// const { request } = await env.wallet.simulateContract({
40+
// address: PretraceFixture,
41+
// abi: abis.PretraceFixture,
42+
// functionName: 'writeStorage',
43+
// args: [BigInt(4n)],
44+
// })
45+
// const hash = await env.wallet.writeContract(request)
46+
// let receipt = await env.wallet.waitForTransactionReceipt({
47+
// hash,
48+
// })
49+
//
50+
// console.log(receipt)
51+
//
52+
// const res = await env.debugClient.traceBlock(
53+
// receipt.blockNumber,
54+
// 'prestateTracer',
55+
// { diffMode: false, disableCode: true }
56+
// )
57+
// console.log(res)

src/tools/deploy.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@ import { deploy } from './lib/index.ts'
1717
// await deploy({ name: 'ATracing', args: [bAddress] })
1818
// await deploy({ name: 'Storage', args: [] })
1919

20-
await deploy({ name: 'MyToken', args: ['MTK'] })
20+
await deploy({
21+
name: 'Storage',
22+
args: [],
23+
})
24+
25+
// // await deploy({
26+
// // name: 'DaoAttacker',
27+
// // args: [address, 1n],
28+
// // value: parseEther('1'),
29+
// // })
30+
//
31+
// await deploy({
32+
// name: 'PretraceFixtureChild',
33+
// args: [],
34+
// })
35+
//
36+
// await deploy({
37+
// name: 'PretraceFixture',
38+
// args: [],
39+
// })

0 commit comments

Comments
 (0)