-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-getAllNodes.mjs
More file actions
96 lines (86 loc) · 3.39 KB
/
check-getAllNodes.mjs
File metadata and controls
96 lines (86 loc) · 3.39 KB
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
#!/usr/bin/env node
/**
* 使用 curl 测试链上 getAllNodes 合约调用是否正常
* 用法: node scripts/check-getAllNodes.mjs
*/
import { execSync } from 'node:child_process'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
const __dirname = dirname(fileURLToPath(import.meta.url))
const require = createRequire(import.meta.url)
const { ethers } = require(join(__dirname, '../node_modules/ethers'))
const newNodeInfoABI = require(join(__dirname, '../src/util/newNodeInfoABI.json'))
const RPC = 'https://rpc1.conet.network'
const CONTRACT = '0x6d7a526BFD03E90ea8D19eDB986577395a139872'
function curlRpc(payload) {
return execSync(
`curl -s -X POST "${RPC}" -H "Content-Type: application/json" -d '${payload}'`,
{ encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 }
)
}
const iface = new ethers.Interface(newNodeInfoABI)
const data = iface.encodeFunctionData('getAllNodes', [0, 100])
const payload = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_call',
params: [
{ to: CONTRACT, data },
'latest'
],
id: 1
})
// 1. 先检查 chainId 和合约是否存在
const chainIdPayload = JSON.stringify({ jsonrpc: '2.0', method: 'eth_chainId', params: [], id: 0 })
const chainIdRes = JSON.parse(curlRpc(chainIdPayload))
console.log('--- 1. RPC 链信息 ---\n')
console.log('chainId:', chainIdRes.result ? parseInt(chainIdRes.result, 16) : chainIdRes)
const getCodePayload = JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getCode',
params: [CONTRACT, 'latest'],
id: 2
})
console.log('\n--- 2. 检查合约是否存在 (eth_getCode) ---\n')
const getCodeResult = JSON.parse(curlRpc(getCodePayload))
const code = getCodeResult.result
console.log('合约代码长度:', code === '0x' ? 0 : (code?.length ?? 0) - 2, 'bytes')
console.log(code === '0x' || !code ? '⚠️ 该地址无合约代码' : '✓ 有合约代码')
console.log('')
console.log('--- 3. 测试 getAllNodes(0, 100) ---\n')
console.log('RPC:', RPC)
console.log('合约:', CONTRACT)
console.log('calldata:', data.slice(0, 20) + '...\n')
console.log('curl 命令:\ncurl -s -X POST "' + RPC + '" -H "Content-Type: application/json" -d \'' + payload + '\'\n')
console.log('--- 返回结果 ---')
try {
const result = curlRpc(payload)
const json = JSON.parse(result)
if (json.error) {
console.log('RPC 错误:', json.error)
} else {
const hex = json.result
console.log('原始返回 (前 200 字符):', (hex || 'null').slice(0, 200))
console.log('返回长度:', hex ? hex.length : 0, '字符')
if (hex === '0x' || !hex) {
console.log('\n⚠️ 返回 0x 或空,说明合约可能:')
console.log(' - 该地址无合约或未部署 getAllNodes')
console.log(' - RPC 链与合约部署链不一致')
console.log(' - 合约 revert')
} else if (hex.length > 10) {
console.log('\n✓ 链上有有效返回,尝试解码...')
try {
const decoded = iface.decodeFunctionResult('getAllNodes', hex)
const nodes = decoded[0] || []
console.log('解码成功,节点数:', nodes.length)
if (nodes.length > 0) {
console.log('首条:', JSON.stringify(nodes[0], (_, v) => typeof v === 'bigint' ? v.toString() : v).slice(0, 150) + '...')
}
} catch (e) {
console.log('解码失败:', e.message)
}
}
}
} catch (e) {
console.error('执行失败:', e.message)
}