-
Notifications
You must be signed in to change notification settings - Fork 62
/
testclient.py
143 lines (130 loc) · 6.93 KB
/
testclient.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''
@author: kentzhang
@date: 2019-06
'''
import sys
from client.contractnote import ContractNote
from client.bcosclient import (
BcosClient,
BcosError
)
import os
from eth_utils import to_checksum_address, keccak
from client.datatype_parser import DatatypeParser
from eth_utils import decode_hex,encode_hex
client = BcosClient()
info = client.getinfo()
print(info)
# 从文件加载abi定义 ,可事先用solc生成,或用console2.py deploy一次 SimpleInfo合约
contractFile = r"contracts\SimpleInfo.abi"
abi_parser = DatatypeParser()
abi_parser.load_abi_file(contractFile)
contract_abi = abi_parser.contract_abi
# 部署合约 可事先用solc生成
print("\n>>Deploy:---------------------------------------------------------------------")
with open(r"contracts\SimpleInfo.bin", 'r') as load_f:
contract_bin = load_f.read()
load_f.close()
result = client.deploy(contract_bin)
print("deploy", result)
print("new address : ", result["contractAddress"])
contract_name = contractname = os.path.splitext(os.path.basename(contractFile))[0]
memo = "tx:" + result["transactionHash"]
# 把部署结果存入文件备查
ContractNote.save_address_to_contract_note(
contract_name, result["contractAddress"], int(
result["blockNumber"], 16), memo)
# 发送交易,调用一个改写数据的接口
print("\n>>sendRawTransaction:----------------------------------------------------------")
to_address = result['contractAddress'] # use new deploy address
args = ['simplename', 2024, to_checksum_address('0x7029c502b4F824d19Bd7921E9cb74Ef92392FB1c')]
receipt = client.sendRawTransactionGetReceipt(to_address, contract_abi, "set", args)
print(receipt)
# 解析receipt里的log
logresult = abi_parser.parse_event_logs(receipt["logs"])
for log in logresult:
if 'eventname' in log:
print("log name: {} , data: {}".format(log['eventname'], log['eventdata']))
# 获取对应的交易数据,解析出调用方法名和参数
txhash = receipt['transactionHash']
txresponse = client.getTransactionByHash(txhash)
inputresult = abi_parser.parse_transaction_input(txresponse['input'])
print("transaction input parse:", txhash)
print(inputresult)
# 解析该交易在receipt里输出的output,即交易调用的方法的return值
outputresult = abi_parser.parse_receipt_output(inputresult['name'], receipt['output'])
print("receipt output :", outputresult)
# 调用一下call,获取数据
print("\n>>Call:------------------------------------------------------------------------")
res = client.call(to_address, contract_abi, "getbalance")
print("call getbalance result:", res)
res = client.call(to_address, contract_abi, "getbalance1", [100])
print("call getbalance1 result:", res)
res = client.call(to_address, contract_abi, "getname")
print("call getname:", res)
res = client.call(to_address, contract_abi, "getall")
print("call getall result:", res)
# 以下是查询类的接口,大部分是返回json,可以根据对fisco bcos rpc接口json格式的理解,进行字段获取和转码
'''
useful helper:
int(num,16) hex -> int
hex(num) : int -> hex
'''
doQueryTest = False
if doQueryTest:
print("\n>>---------------------------------------------------------------------")
res = client.getNodeVersion()
print("\n>>---------------------------------------------------------------------")
print("getClientVersion", res)
print("\n>>---------------------------------------------------------------------")
try:
res = client.getBlockNumber()
print("getBlockNumber", res)
except BcosError as e:
print("bcos client error,", e.info())
print("\n>>---------------------------------------------------------------------")
print("getPeers", client.getPeers())
print("\n>>---------------------------------------------------------------------")
print("getBlockByNumber", client.getBlockByNumber(50))
print("\n>>---------------------------------------------------------------------")
print("getBlockHashByNumber", client.getBlockHashByNumber(50))
print("\n>>---------------------------------------------------------------------")
print("getBlockByHash", client.getBlockByHash(
"0xe7588bf4ee5a6fb5aae9bdcc2c4f3c58cf7a789b15a4daa6617ed594b5ba3951"))
print("\n>>---------------------------------------------------------------------")
print("getTransactionByHash", client.getTransactionByHash(
"0x41fa9a0ce36d486ee2bf6d2219b68b44ca300ec7aeb07f8f2aa9c225655d2b61"))
print("\n>>---------------------------------------------------------------------")
print("getTransactionByBlockHashAndIndex", client.getTransactionByBlockHashAndIndex(
"0xe7588bf4ee5a6fb5aae9bdcc2c4f3c58cf7a789b15a4daa6617ed594b5ba3951", 0))
print("\n>>---------------------------------------------------------------------")
print("getTransactionByBlockNumberAndIndex", client.getTransactionByBlockNumberAndIndex(50, 0))
print("\n>>---------------------------------------------------------------------")
print("getTransactionReceipt", client.getTransactionReceipt(
"0x41fa9a0ce36d486ee2bf6d2219b68b44ca300ec7aeb07f8f2aa9c225655d2b61"))
print("\n>>---------------------------------------------------------------------")
print("getPendingTransactions", client.getPendingTransactions())
print("\n>>---------------------------------------------------------------------")
print("getPendingTxSize", client.getPendingTxSize())
print("\n>>---------------------------------------------------------------------")
print("getCode", client.getCode("0x83592a3cf1af302612756b8687c8dc7935c0ad1d"))
print("\n>>---------------------------------------------------------------------")
print("getTotalTransactionCount", client.getTotalTransactionCount())
print("\n>>---------------------------------------------------------------------")
print("getSystemConfigByKey", client.getSystemConfigByKey("tx_count_limit"))
print("\n>>---------------------------------------------------------------------")
print("getPbftView", int(client.getPbftView(), 16))
print("\n>>---------------------------------------------------------------------")
print("getSealerList", client.getSealerList())
print("\n>>---------------------------------------------------------------------")
print("getObserverList", client.getObserverList())
print("\n>>---------------------------------------------------------------------")
print("getConsensusStatus", client.getConsensusStatus())
print("\n>>---------------------------------------------------------------------")
print("getSyncStatus", client.getSyncStatus())
print("\n>>---------------------------------------------------------------------")
print("getGroupPeers", client.getGroupPeers())
print("\n>>---------------------------------------------------------------------")
print("getNodeIDList", client.getNodeIDList())
print("\n>>---------------------------------------------------------------------")
print("getGroupList", client.getGroupList())