批量交易
CITA
支持批量交易,目前只能进行批量合约的调用。
简述
批量交易是由系统合约实现的,通过组装数据调用其接口来完成。
合约信息
合约地址为: 0xffffffffffffffffffffffffffffffffff02000e
接口签名如下:
======= batch-tx.sol:BatchTx =======
Function signatures:
82cc3327: multiTxs(bytes)
数据组装规则
参数类型为 bytes
,encode 规则和 ABI 一致。拼装规则如下:
- 二十字节的目标调用合约的地址
- 四字节的目标合约的调用数据的长度
- 四字节的函数签名
- ABI 格式编码的函数参数
- 目标合约的调用数据(第一条交易信息结束)
- ...(第 n 条交易信息)
拼装之后按照 bytes 的 ABI 编码即可。
以下是两个交易信息的示例:
897c71052abad4ca9a5059f070d5a3a119d1e1ec
00000004
2d910f2c
897c71052abad4ca9a5059f070d5a3a119d1e1ec
00000004
2d910f2c
操作示例
首先需要启动一条链,具体方法见快速入门部分
其中测试合约函数签名如下:
======= contracts/test_batch-tx.sol:SelfAdd =======
Function signatures:
2d910f2c: AddOne()
0c55699c: x()
其中:
AddOne()
表示对 x 加一x()
表示获取 x 数值
部署测试合约
本次示例使用发布件自带的 python 工具,需要进入容器中执行: ./env.sh
构造交易
python3 make_tx.py --code '608060405234801561001057600080fd5b5060fd8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c55699c14604e5780632d910f2c146076575b600080fd5b348015605957600080fd5b506060608a565b6040518082815260200191505060405180910390f35b348015608157600080fd5b5060886090565b005b60005481565b600160008082825401925050819055506000547f11c1a8e7158fead62641b1e07f61c32daccb5a0432cabfe33a43e8de610042f160405160405180910390a25600a165627a7a7230582021264d3aa498b31d10a5a7086d3e3ba4fb8c23f5a30b64ef8426b19ae2de29870029'
发送交易
python3 send_tx.py
获取 receipt
python3 get_receipt.py
结果如下:
{
"transactionHash": "0xcabcfc78987a955bfc38bf4cbdcaac57e6256346cc3294ad5a55e6c9d6465cdb",
"transactionIndex": "0x0",
"blockHash": "0x5d1f19c6ebc189effeadfb029090bebdd92c95ca47231d7fe7aaa916e2770f30",
"blockNumber": "0xe",
"cumulativeGasUsed": "0xc66f",
"gasUsed": "0xc66f",
"contractAddress": "0x897c71052abad4ca9a5059f070d5a3a119d1e1ec",
"logs": [],
"root": null,
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"errorMessage": null
}
其中合约地址为: 0x897c71052abad4ca9a5059f070d5a3a119d1e1ec
批量交易
- 构造交易 data:
如下
// func sig
82cc3327
// 遵循ABI格式,前64字节无用
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
897c71052abad4ca9a5059f070d5a3a119d1e1ec
00000004
2d910f2c
897c71052abad4ca9a5059f070d5a3a119d1e1ec
00000004
2d910f2c
构造交易
python3 make_tx.py --to 'ffffffffffffffffffffffffffffffffff02000e' --code '82cc332700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000897c71052abad4ca9a5059f070d5a3a119d1e1ec000000042d910f2c897c71052abad4ca9a5059f070d5a3a119d1e1ec000000042d910f2c'
发送交易
python3 send_tx.py
获取 receipt
python3 get_receipt.py
{
"transactionHash": "0x908c295288a9c93dfe4a98dba0d260279428cbcb983cc157d81f73f8c4df3f9b",
"transactionIndex": "0x0",
"blockHash": "0x8bc3e95282c604d09d22fabd4f41b1b87f930a2ee944b7e14308b6ac4fc11bb2",
"blockNumber": "0x10d",
"cumulativeGasUsed": "0x6fb2",
"gasUsed": "0x6fb2",
"contractAddress": null,
"logs": [
{
"address": "0x897c71052abad4ca9a5059f070d5a3a119d1e1ec",
"topics": [
"0x11c1a8e7158fead62641b1e07f61c32daccb5a0432cabfe33a43e8de610042f1",
"0x0000000000000000000000000000000000000000000000000000000000000001"
],
"data": "0x",
"blockHash": "0x8bc3e95282c604d09d22fabd4f41b1b87f930a2ee944b7e14308b6ac4fc11bb2",
"blockNumber": "0x10d",
"transactionHash": "0x908c295288a9c93dfe4a98dba0d260279428cbcb983cc157d81f73f8c4df3f9b",
"transactionIndex": "0x0",
"logIndex": "0x0",
"transactionLogIndex": "0x0"
},
{
"address": "0x897c71052abad4ca9a5059f070d5a3a119d1e1ec",
"topics": [
"0x11c1a8e7158fead62641b1e07f61c32daccb5a0432cabfe33a43e8de610042f1",
"0x0000000000000000000000000000000000000000000000000000000000000002"
],
"data": "0x",
"blockHash": "0x8bc3e95282c604d09d22fabd4f41b1b87f930a2ee944b7e14308b6ac4fc11bb2",
"blockNumber": "0x10d",
"transactionHash": "0x908c295288a9c93dfe4a98dba0d260279428cbcb983cc157d81f73f8c4df3f9b",
"transactionIndex": "0x0",
"logIndex": "0x1",
"transactionLogIndex": "0x1"
}
],
"root": null,
"logsBloom": "0x04000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000400000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000002000000004000000040000000000000000040000000000000000000000000000000000000000000008000000000000000000000",
"errorMessage": null
}
这里从 logs
已经可以看出两条交易都已经执行成功
验证结果
查询 x 数值
curl -X POST --data '{"jsonrpc":"2.0","method":"call","params":[{"to":"0x897c71052abad4ca9a5059f070d5a3a119d1e1ec","data":"0x0c55699c"}, "latest"],"id":2}' 127.0.0.1:1337
结果:
{"jsonrpc":"2.0","id":2,"result":"0x0000000000000000000000000000000000000000000000000000000000000002"}
批量交易成功执行