运行轻节点
注意:目前Conflux轻节点不支持eSpace(EVM完全兼容空间)的RPC方法。
概览
Node version: conflux-rust v2.1.0
.
轻节点是Conflux网络中的特殊节点,该节点仅存储区块头,并按照需要从其他节点检索数据。 这就意味着默认的轻节点既不会存储交易也不会存储状态树。 与完整节点和存档节点相比,轻节点大大减少了磁盘和带宽使用,特别是在高TPS下。 作为权衡,轻节点上的RPC查询具有更高的延迟。
轻节点在它们的头图 GHAST上执行共识,它们还使用Merkle证明和其他类似的机制对按需检索的每个项目进行验证。 按需检索的项目包括账户、布隆过滤器、交易和交易收据。 虽然轻节点需要依赖它们的对等节点来满足 RPC 查询,但它们以一种无需信任的方式进行这个过程。
目前的轻节点实现仍然被认为是实验性的,因此预期存在错误。 如果您遇到任何问题,请通过在conflux-rust仓库上开启一个问题来告诉我们。
运行轻节点
轻节点可以在hydra.toml
设置文件中启用(对于测试网是testnet.toml
),在node_type
变量中设置。
node_type = "light"
或者,可以使用
--light
在命令行中启用轻节点:
首先从conflux-rust仓库下载最新版本,或按照本指南从源代码构建。 然后,您可以使用这些命令运行节点:
> cd run
> ./conflux --config hydra.toml
与完整节点一样,一旦控制台
打印出以下信息,您就会知道您的节点已经与网络完全同步了:
Catch-up mode: false
与轻节点交互
与全节点和归档节点一样,您可以通过HTTP
、TCP
或WebSocket
连接与轻节点交互。 默认情况下,本地HTTP查询通过端口12539
调用。 有关详细信息,请参阅JSON-RPC文档。
RPC查询
轻节点支持大多数Conflux RPC API,对于其他的API支持也在开发中。 由于轻节点需要查询它们的同伴节点来完成RPC请求,总体延迟略有增加。 (对于cfx_getLogs
,延迟会显著增加。)
> curl -X POST --data '{ "jsonrpc": "2.0", "method": "cfx_clientVersion", "id": 1 }' -H "Content-Type: application/json" localhost:12539
{ "jsonrpc": "2.0", "result": "conflux-rust-1.0.0", "id": 1 }
> curl -X POST --data '{ "jsonrpc":"2.0", "method":"cfx_getBalance", "params": ["cfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg"], "id": 2 }' -H "Content-Type: application/json" localhost:12539
{ "jsonrpc": "2.0", "result": "0x5fc346d4363f84249d4a", "id": 2 }
> curl -X POST --data '{ "jsonrpc": "2.0", "method": "cfx_getLogs", "params": [{ "address": "cfx:type.contract:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp", "fromEpoch": "0x1c8b8", "toEpoch": "0x1c8d6" }], "id": 3}' -H "Content-Type: application/json" localhost:12539
{ "jsonrpc": "2.0", "result": [{ "address": "CFX:TYPE.CONTRACT:ACC7UAWF5UBTNMEZVHU9DHC6SGHEA0403Y2DGPYFJP", "blockHash": "0x694898c77602511b6c411860ec230ac7ca58c08a4cbe3cad904e724b2eb97fee", "data": "0x0000000000000000000000000000000000000000000000049b9ca9a694340000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000141da5f533abef1b82a4a6d698415b8a56894b7b410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "epochNumber": "0x1c8bf", "logIndex": "0x0", "topics": ["0x06b541ddaa720db2b10a4d0cdac39b8d360425fc073085fac19bc82614677987","0x0000000000000000000000001da5f533abef1b82a4a6d698415b8a56894b7b41","0x0000000000000000000000001da5f533abef1b82a4a6d698415b8a56894b7b41","0x00000000000000000000000080bb30efc5683758128b404fe5da03432eb16634"], "transactionHash": "0x7dcfeb245369e509f2d154f2d5523e3ebe0b54f1d420e02edf56c70cdcae278d", "transactionIndex": "0x0", "transactionLogIndex": "0x0" },{ "address": "CFX:TYPE.CONTRACT:ACC7UAWF5UBTNMEZVHU9DHC6SGHEA0403Y2DGPYFJP", "blockHash": "0x694898c77602511b6c411860ec230ac7ca58c08a4cbe3cad904e724b2eb97fee", "data": "0x0000000000000000000000000000000000000000000000049b9ca9a694340000", "epochNumber": "0x1c8bf", "logIndex": "0x1", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000001da5f533abef1b82a4a6d698415b8a56894b7b41","0x00000000000000000000000080bb30efc5683758128b404fe5da03432eb16634"], "transactionHash": "0x7dcfeb245369e509f2d154f2d5523e3ebe0b54f1d420e02edf56c70cdcae278d", "transactionIndex": "0x0", "transactionLogIndex": "0x1" }], "id": 3 } -H "Content-Type: application/json" localhost:12539
JavaScript
轻节点支持JavaScript SDK(js-conflux-sdk)的大部分功能。 您可以使用以下命令安装SDK:
npm install --save js-conflux-sdk
然后,您可以查询区块链和发送交易:
const { Conflux, Drip } = require('js-conflux-sdk');
const PRIVATE_KEY = '0x...';
const RECEIVER = 'cfx:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg';
async function main() {
const conflux = new Conflux({ url: 'http://localhost:12539' });
// query node version
const client_version = await conflux.provider.call('cfx_clientVersion');
console.log('client_version:', client_version);
// query account balance
const balance = await conflux.getBalance('cfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg');
console.log('balance:', balance.toString(10));
// query smart contract logs
const logs = await conflux.getLogs({
address: 'cfx:type.contract:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp',
fromEpoch: 116920,
toEpoch: 116950,
});
console.log('logs:', logs);
// send transaction
const account = conflux.wallet.addPrivateKey(PRIVATE_KEY);
const tx = {
from: account.address,
to: RECEIVER,
value: Drip.fromCFX(0.1),
gasPrice: 1000000000,
};
try {
const receipt = await conflux.sendTransaction(tx).executed();
console.log('receipt:', receipt);
} catch (e) {
console.error(e);
}
}
main();
其他SDK
虽然尚未经过测试,但预期轻节点也能与Java、Python和Go SDK适配。
故障排除
为什么在调用合约方法时出错?
如果您运行以下代码:
const admin = await cfx.InternalContract('AdminControl').getAdmin('cfx:type.contract:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp');
console.log('admin:', admin);
您将得到此错误:
RPCError: This API is not implemented yet
at HttpProvider.call
at processTicksAndRejections
at async Conflux.call
at async MethodTransaction.call
at async MethodTransaction.then {
code: -32000,
data: 'Tracking issue: https://github.com/Conflux-Chain/conflux-rust/issues/1461'
}
这是因为合约调用使用cfx_call
RPC API,而轻节点尚未支持此API。
假设您想向智能合约发送交易:
conflux.InternalContract('AdminControl').setAdmin('cfx:type.contract:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp', 'cfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg').sendTransaction({
from: account,
}).executed();
您将得到一个类似的错误。 这是因为对于合约交易,js-conflux-sdk
会自动尝试使用cfx_estimateGasAndCollateral
RPC估算燃料限制和存储限制,而这在轻节点上尚未支持。 您可以通过手动设置这些参数来解决这个问题:
conflux.InternalContract('AdminControl').setAdmin('cfx:type.contract:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp', 'cfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg').sendTransaction({
from: account,
gas: 1000000000,
storageLimit: '0x0',
gasPrice: '0x1',
}).executed();
如果您遇到了This API is not implemented yet
错误,您可以在conflux对象上设置debug日志记录器,以找出是哪个RPC造成了这个问题。
const cfx = new Conflux({
url: 'http://localhost:12539',
logger: console,
});