forked from ethereumjs/ultralight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestProvider.ts
151 lines (129 loc) · 6.28 KB
/
testProvider.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
import { UltralightProvider } from '../../portalnetwork/src/client/provider'
import { NetworkId } from '../../portalnetwork/src/networks/types'
const testBlockHash = '0x95b0950557cbc3e6647766adb719f80f7c7d192f4429b6026cdbd2cbe6a64294'
const testContract = '0x6b175474e89094c44da98b954eedeac495271d0f'
const testStorage = '0x0000000000000000000000000000000000000000000000000000000000000000'
const historicalBlock = 1048576
async function findHistoricalAccount(
provider: UltralightProvider,
blockNumber: number,
): Promise<string | null> {
try {
const block: any = await provider.request({
method: 'eth_getBlockByNumber',
params: [blockNumber, true],
})
if (
block &&
block.result &&
block.result.transactions &&
block.result.transactions.length > 0
) {
const contractCreation = block.result.transactions.find((tx: any) => !tx.to)
if (contractCreation) {
console.log('Found contract creation transaction')
return contractCreation.from
}
console.log('Using first transaction sender')
return block.result.transactions[0].from
}
return null
} catch (error) {
console.error('Error finding historical account:', error)
return null
}
}
async function main() {
const provider = await UltralightProvider.create({
bootnodes: [
'enr:-Jy4QIs2pCyiKna9YWnAF0zgf7bT0GzlAGoF8MEKFJOExmtofBIqzm71zDvmzRiiLkxaEJcs_Amr7XIhLI74k1rtlXICY5Z0IDAuMS4xLWFscGhhLjEtMTEwZjUwgmlkgnY0gmlwhKEjVaWJc2VjcDI1NmsxoQL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S4pYfg3VkcIIjjA',
'enr:-Ia4QIU9U3zrP2DM7sfpgLJbbYpg12sWeXNeYcpKN49-6fhRCng0IUoVRI2E51mN-2eKJ4tbTimxNLaAnbA7r7fxVjcTY2aCaWSCdjSCaXCEwiErQYlzZWNwMjU2azGhAxOroJ3HceYvdD2yK1q9w8c9tgrISJso8q_JXI6U0Xwng3VkcIIjjA',
'enr:-IS4QFV_wTNknw7qiCGAbHf6LxB-xPQCktyrCEZX-b-7PikMOIKkBg-frHRBkfwhI3XaYo_T-HxBYmOOQGNwThkBBHYDgmlkgnY0gmlwhKRc9_OJc2VjcDI1NmsxoQKHPt5CQ0D66ueTtSUqwGjfhscU_LiwS28QvJ0GgJFd-YN1ZHCCE4k',
'enr:-IS4QDpUz2hQBNt0DECFm8Zy58Hi59PF_7sw780X3qA0vzJEB2IEd5RtVdPUYZUbeg4f0LMradgwpyIhYUeSxz2Tfa8DgmlkgnY0gmlwhKRc9_OJc2VjcDI1NmsxoQJd4NAVKOXfbdxyjSOUJzmA4rjtg43EDeEJu1f8YRhb_4N1ZHCCE4o',
'enr:-IS4QGG6moBhLW1oXz84NaKEHaRcim64qzFn1hAG80yQyVGNLoKqzJe887kEjthr7rJCNlt6vdVMKMNoUC9OCeNK-EMDgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQLJhXByb3LmxHQaqgLDtIGUmpANXaBbFw3ybZWzGqb9-IN1ZHCCE4k',
'enr:-IS4QA5hpJikeDFf1DD1_Le6_ylgrLGpdwn3SRaneGu9hY2HUI7peHep0f28UUMzbC0PvlWjN8zSfnqMG07WVcCyBhADgmlkgnY0gmlwhKRc9-KJc2VjcDI1NmsxoQJMpHmGj1xSP1O-Mffk_jYIHVcg6tY5_CjmWVg1gJEsPIN1ZHCCE4o',
],
bindAddress: '0.0.0.0',
supportedNetworks: [
{ networkId: NetworkId.HistoryNetwork, maxStorage: 1024 },
{ networkId: NetworkId.StateNetwork, maxStorage: 1024 },
],
})
console.log('Provider created:', provider.portal.discv5.enr.nodeId)
await provider.portal.start()
console.log('portal started')
while (provider.portal.network()['0x500b']?.routingTable.values().length === 0) {
console.log('Waiting for network to start...')
await new Promise((resolve) => setTimeout(resolve, 1000))
}
console.log('Testing eth_getBlockByHash...')
const block: any = await provider.request({
method: 'eth_getBlockByHash',
params: [testBlockHash, false],
})
console.log('Block by hash retrieved:', block)
console.log('Testing eth_getBlockByNumber...')
const blockByNumber = await provider.request({
method: 'eth_getBlockByNumber',
params: [historicalBlock, false],
})
console.log('Block by number retrieved:', blockByNumber)
console.log(`Looking for accounts in block ${historicalBlock}...`)
// const testAddress = await findHistoricalAccount(provider, historicalBlock)
const testAddress = '0x3DC00AaD844393c110b61aED5849b7c82104e748'
console.log('test address ', testAddress)
if (!testAddress) {
console.error('Could not find a historical account to test with')
}
console.log(`Found historical address: ${testAddress}`)
console.log('Testing eth_getTransactionCount...')
const transactionCount = await provider.request({
method: 'eth_getTransactionCount',
params: [testAddress, historicalBlock],
})
console.log('Transaction count:', transactionCount)
console.log('Testing eth_getCode...')
const code = await provider.request({
method: 'eth_getCode',
params: [testContract, '100'],
})
console.log('Contract code retrieved:', code)
console.log('Testing eth_getBalance...')
const balance = await provider.request({
method: 'eth_getBalance',
params: [testAddress, historicalBlock],
})
console.log('Account balance:', balance)
console.log('Testing eth_getStorageAt...')
const storage = await provider.request({
method: 'eth_getStorageAt',
params: [testContract, testStorage, historicalBlock],
})
console.log('Storage value:', storage)
console.log('Testing eth_call...')
const callData = {
to: testAddress,
data: '0x06fdde03',
from: testContract,
}
const callResult = await provider.request({
method: 'eth_call',
params: [callData, historicalBlock],
})
console.log('Contract call result:', callResult)
try {
await provider.request({
method: 'eth_unsupportedMethod',
params: [],
})
} catch (error) {
console.log('Expected error for unsupported method:', error.message)
}
process.exit(0)
}
main().catch((err) => console.error(err))