基本操作
查看块高度
执行命令:
$ cita-cli rpc blockNumber
命令返回:
{
"id": 1,
"jsonrpc": "2.0",
"result": "0x283"
}
其中 result
表示本次查询时的块高度为 0x283
。 CITA 默认 3s 出一个块,重复执行查询命令,可以观察到 result 在持续的增长。
部署合约
执行命令:
cita-cli rpc sendRawTransaction \
--code 0x608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058205aed214856a5c433292a354261c9eb88eed1396c83dabbe105bde142e49838ac0029 \
--private-key 0x3ef2627393529fed043c7dbfd9358a4ae47a88a59949b07e7631722fd6959002
该命令将合约的字节码部署到区块链上。
命令返回:
{
"id": 4,
"jsonrpc": "2.0",
"result": {
"hash": "0xec4b93bab3c815950f7342781c735895a4c9dcbf50a4e9f4d28204253560f3f4",
"status": "OK"
}
}
查看回执
执行命令:
cita-cli rpc getTransactionReceipt --hash 0xec4b93bab3c815950f7342781c735895a4c9dcbf50a4e9f4d28204253560f3f4
其中的 hash
为部署合约的交易 hash
,所查询的结果为部署合约的交易回执。
命令返回:
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"blockHash": "0x876bcba12dfebcb4629e04922a0bd6e9489185c9e3d8d4a27b7d88bd761a0cb2",
"blockNumber": "0x10",
"contractAddress": "0xb701924639d802dc8093d1a43dffa9939a4506de",
"cumulativeQuotaUsed": "0x17d9d",
"errorMessage": null,
"logs": [
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"quotaUsed": "0x17d9d",
"root": null,
"transactionHash": "0xec4b93bab3c815950f7342781c735895a4c9dcbf50a4e9f4d28204253560f3f4",
"transactionIndex": "0x0"
}
}
查看交易
执行命令:
cita-cli rpc getTransaction --hash 0xec4b93bab3c815950f7342781c735895a4c9dcbf50a4e9f4d28204253560f3f4
其中的 hash
为部署合约的交易 hash
,所查询的结果为部署合约的交易信息。
命令返回:
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"blockHash": "0x876bcba12dfebcb4629e04922a0bd6e9489185c9e3d8d4a27b7d88bd761a0cb2",
"blockNumber": "0x10",
"content": "0x0af002122038306335313266393861343134663732383065616131333437353763356134351880ade20420662afe01608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058205aed214856a5c433292a354261c9eb88eed1396c83dabbe105bde142e49838ac0029322000000000000000000000000000000000000000000000000000000000000000004002522000000000000000000000000000000000000000000000000000000000000000011241b42d8e68fb9897dc14cca3ca1ee525305c21c14beb7378d25dda0761e1c69ea56a03327702e98c4701b1345550e5f49bb7945cde6dacf4bb8c643a460350e8d001",
"from": "0x37d1c7449bfe76fe9c445e626da06265e9377601",
"hash": "0xec4b93bab3c815950f7342781c735895a4c9dcbf50a4e9f4d28204253560f3f4",
"index": "0x0"
}
}
其中 content
为交易数据。由于数据经过了签名处理,所以与部署合约时指定的 --code
不一致。可以使用 cita-cli
提供的 decode-unverifiedTransaction
命令来解码。
执行命令:
cita-cli tx decode-unverifiedTransaction --content 0x0af002122038306335313266393861343134663732383065616131333437353763356134351880ade20420662afe01608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058205aed214856a5c433292a354261c9eb88eed1396c83dabbe105bde142e49838ac0029322000000000000000000000000000000000000000000000000000000000000000004002522000000000000000000000000000000000000000000000000000000000000000011241b42d8e68fb9897dc14cca3ca1ee525305c21c14beb7378d25dda0761e1c69ea56a03327702e98c4701b1345550e5f49bb7945cde6dacf4bb8c643a460350e8d001
命令返回:
{
"crypto": 0,
"signature": "0xb42d8e68fb9897dc14cca3ca1ee525305c21c14beb7378d25dda0761e1c69ea56a03327702e98c4701b1345550e5f49bb7945cde6dacf4bb8c643a460350e8d001",
"transaction": {
"chain_id": 0,
"chain_id_v1": "0x0000000000000000000000000000000000000000000000000000000000000001",
"data": "0x608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058205aed214856a5c433292a354261c9eb88eed1396c83dabbe105bde142e49838ac0029",
"encrypted_hash": "0x50541211bd367adc0a83747831ba2b8449cbd9694f4901e1b6f4bf1970728539",
"nonce": "80c512f98a414f7280eaa134757c5a45",
"pub_key": "0x9dc6fc7856f5271e6e8c45e5c5fe22d2ff699ac3b24497599be77803d3c25fb4e2fe7da616c65a291910c947c89923009f354634421bddd0a25cd0a509bcf6a9",
"quota": 10000000,
"sender": "0x37d1c7449bfe76fe9c445e626da06265e9377601",
"to": "",
"to_v1": "0x0000000000000000000000000000000000000000",
"valid_until_block": 102,
"value": "0x0000000000000000000000000000000000000000000000000000000000000000",
"version": 2
}
}
其中的 data
数据便与部署合约中指定的 code
相一致。
调用合约
执行命令,调用 set 函数:
cita-cli rpc sendRawTransaction \
--code 0x60fe47b10000000000000000000000000000000000000000000000000000000000000001 \
--private-key 0x3ef2627393529fed043c7dbfd9358a4ae47a88a59949b07e7631722fd6959002 \
--address 0xb701924639d802dc8093d1a43dffa9939a4506de
注意:这里的 address
要与部署合约回执中的 contractAddress
相一致。
命令返回:
{
"id": 4,
"jsonrpc": "2.0",
"result": {
"hash": "0xfa3673433191e7daa190601318295e53a92aa62b15573b8a4a439e7ee58be55e",
"status": "OK"
}
}
停止节点
以节点 0 为例,执行命令:
$ bin/cita stop test-chain/0
stop...ok
其他操作
更多其他操作使用以下命令查看帮助信息:
$ bin/cita help
详细说明请查看 CITA 命令说明。
Notice
请不要先进到 bin 目录,再执行以上的部署操作,错误示范:
shell $ cd bin $ cita setup test-chain/0
请勿在一台服务器上运行多个容器。因为虽然 CITA 在 Docker 中运行,但是容器并没有做网络隔离。
请不要同时在 host 系统里面运行 CITA 以及相关的 RabbitMQ 等软件,以免造成端口冲突。