关键字 parity attach,geth attach
很少写博客,习惯了自己写笔记。今天主要分享一下一次自编译修改geth的经验。
以前一直用geth,因为出块速度提升的原因节点区块数据越来越大,而我这边又并不需要溯源的全节点信息,所以转战到parity使用snapshot同步来减少区块数据的存储。
之前也有简单研究过parity的接口和配置,但是parity本身没有控制台(client),只能通过rpc去调用(curl 或者 postman在服务器上都不是很方便)。虽然可以使用geth attach,但是parity本身没有admin模块,导致不好管理连接的节点,有时候需要为节点添加/删除新节点,只能curl去调用 rpc很不习惯。
于是开始折腾怎么实现 Geth JavaScript console 的parity版本。
1、nodejs模块
最初发现有个nodejs的@parity/api 模块可以使用,功能实现很齐全。
npm install @parity/api
$ node
On node console :
>// import the actual Api class
>const Api = require("@parity/api");
>// do the setup
>const provider = new Api.Provider.Http('http://localhost:8545');
>const api = new Api(provider);
// use api.parity.addReservedPeer
api.parity.addReservedPeer("enode://d64d5f74b1715c525dc88e87a52eca1574c09593ed29401d205ecfef9fbfe52fa308f966bab3a5966da1bb74212fecdb328cddceb572c38b536c597166784347@203080240034.static.ctinets.com:35423").then((data)=> {console.log(data)})
这样每次必须进入node控制台再执行js命令创建provider很是麻烦,因为自己不太熟nodejs,不知道怎么让node在执行时候自动预加载执行一个js(预初始化provider)。故弃之.....。
如果有大神知道怎么用node直接启动时候初始化parity api可以留言指点一下☺。可以尝试将这个模块打包封装成npm-cli,再加入web3.js来完全替代geth的console。
2、修改geth代码
又回到了原点,既然geth可以attach调用geth节点的rpc,那么肯定能够修改后让geth attch功能支持parity。于是我把注意打到了geth源码上。
geth的 javaScript console其实也是基于nodejs的,geth内嵌一个nodejs解析器,rpc的调用都是使用的node 调用web3.js。开始想着把@parity/api模块集成到geth中,还是因为对node不熟悉停下了脚步。在看geth源码时候发现其实并非所有的接口都是直接使用的web3.js,其中admin等模块其实在go-ethereum-master\internal\web3ext\web3ext.go中声明的web3扩展功能,都是封装jsonrpc格式后直接调用节点rpc/ipc的。于是尝试着将parity模块加入到web3ext中。
var Modules = map[string]string{
"accounting": AccountingJs,
"admin": AdminJs,
"chequebook": ChequebookJs,
"clique": CliqueJs,
"ethash": EthashJs,
"debug": DebugJs,
"eth": EthJs,
"miner": MinerJs,
"net": NetJs,
"personal": PersonalJs,
"rpc": RpcJs,
"shh": ShhJs,
"swarmfs": SwarmfsJs,
"txpool": TxpoolJs,
"les": LESJs,
"parity": ParityJs,
}
const ParityJs = `
web3._extend({
property: 'parity',
methods: [
new web3._extend.Property({
name: 'netPeers',
getter: 'parity_netPeers'
}),
new web3._extend.Method({
name: 'addReservedPeer',
call: 'parity_addReservedPeer',
params: 1
}),
new web3._extend.Method({
name: 'call',
call: 'parity_call',
params: 1
}),
new web3._extend.Method({
name: 'cidV0',
call: 'parity_cidV0',
params: 1
}),
new web3._extend.Method({
name: 'composeTransaction',
call: 'parity_composeTransaction',
params: 1
}),
new web3._extend.Method({
name: 'nextNonce',
call: 'parity_nextNonce',
params: 1
}),
new web3._extend.Method({
name: 'removeReservedPeer',
call: 'parity_removeReservedPeer',
params: 1
}),
new web3._extend.Method({
name: 'removeTransaction',
call: 'parity_removeTransaction',
params: 1
}),
new web3._extend.Method({
name: 'decryptMessage',
call: 'parity_decryptMessage',
params: 2
}),
new web3._extend.Method({
name: 'getBlockReceipts',
call: 'parity_getBlockReceipts',
params: 1,
inputFormatter: [web3._extend.utils.toHex]
}),
new web3._extend.Method({
name: 'setMinGasPrice',
call: 'parity_setMinGasPrice',
params: 1,
inputFormatter: [web3._extend.utils.toHex]
}),
new web3._extend.Method({
name: 'setMaxTransactionGas',
call: 'parity_setMaxTransactionGas',
params: 1,
inputFormatter: [web3._extend.utils.toHex]
}),
new web3._extend.Method({
name: 'setMode',
call: 'parity_setMode',
params: 1
}),
new web3._extend.Method({
name: 'removeReservedPeer',
call: 'parity_removeReservedPeer',
params: 1
}),
new web3._extend.Method({
name: 'removeAddress',
call: 'parity_removeAddress',
params: 1
}),
new web3._extend.Method({
name: 'encryptMessage',
call: 'parity_encryptMessage',
params: 2
}),
new web3._extend.Method({
name: 'setAccountMeta',
call: 'parity_setAccountMeta',
params: 2
}),
new web3._extend.Method({
name: 'setAccountName',
call: 'parity_setAccountName',
params: 2
}),
new web3._extend.Method({
name: 'testPassword',
call: 'parity_testPassword',
params: 2
}),
new web3._extend.Method({
name: 'changePassword',
call: 'parity_changePassword',
params: 3
}),
new web3._extend.Method({
name: 'exportAccount',
call: 'parity_exportAccount',
params: 2
}),
new web3._extend.Property({
name: 'allTransactionHashes',
getter: 'parity_allTransactionHashes'
}),
new web3._extend.Property({
name: 'allTransactions',
getter: 'parity_allTransactions'
}),
new web3._extend.Property({
name: 'consensusCapability',
getter: 'parity_consensusCapability'
}),
new web3._extend.Property({
name: 'chain',
getter: 'parity_chain'
}),
new web3._extend.Property({
name: 'chainStatus',
getter: 'parity_chainStatus'
}),
new web3._extend.Property({
name: 'netPort',
getter: 'parity_netPort'
}),
new web3._extend.Property({
name: 'pendingTransactions',
getter: 'parity_pendingTransactions'
}),
new web3._extend.Property({
name: 'pendingTransactionsStats',
getter: 'parity_pendingTransactionsStats'
}),
new web3._extend.Property({
name: 'registryAddress',
getter: 'parity_registryAddress'
}),
new web3._extend.Property({
name: 'rpcSettings',
getter: 'parity_rpcSettings'
}),
new web3._extend.Property({
name: 'unsignedTransactionsCount',
getter: 'parity_unsignedTransactionsCount'
}),
new web3._extend.Property({
name: 'enode',
getter: 'parity_enode'
}),
new web3._extend.Property({
name: 'mode',
getter: 'parity_mode'
}),
new web3._extend.Property({
name: 'nodeKind',
getter: 'parity_nodeKind'
}),
new web3._extend.Property({
name: 'nodeName',
getter: 'parity_nodeName'
}),
new web3._extend.Property({
name: 'wsUrl',
getter: 'parity_wsUrl'
}),
new web3._extend.Property({
name: 'allAccountsInfo',
getter: 'parity_allAccountsInfo'
}),
new web3._extend.Property({
name: 'acceptNonReservedPeers',
getter: 'parity_acceptNonReservedPeers'
}),
new web3._extend.Property({
name: 'dropNonReservedPeers',
getter: 'parity_dropNonReservedPeers'
}),
new web3._extend.Property({
name: 'executeUpgrade',
getter: 'parity_executeUpgrade'
}),
new web3._extend.Property({
name: 'upgradeReady',
getter: 'parity_upgradeReady'
}),
]
});
`
理论上这样就可以用geth的console去调用parity的接口了。参考官方文档编译geth,我使用的是ubuntu系统,编译后centos也可以直接运行的。
具体编译过程参考官方github,此处不再赘述。
看一下效果图吧😄😄😄😄😄😄😄😄😄😄
有需要的可以自己参考本文添加traces、pubsub等其他parity专有的模块rpc,让geth完整支持parity接口。
此处我上传一下我自己编译的geth,编译时间:2019/12/07/18:36,从github拉取的最新代码。
参考来源:
https://ethereum.stackexchange.com/questions/13196/how-to-open-the-javascript-console-on-parity
https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu
https://github.com/ethereum/go-ethereum/wiki/Installation-instructions-for-Windows