Python SDK 与Fisco Bcos交互 -2
(使用 python-sdk ,把区块链作为数据库使用,查询历史数据)
需求:查询通过智能合约存储的历史信息。如
- 开始存储的是 "xsmile",通过 Fisco 提供的 call 方法,当前可以查询到 "xsmile"
- 再次调用合约存储的是 "root", 通过 call 方法查询得到的是 root
那怎么再查询到 "xsmile" 呢?call 方法是不能解决问题的。
官方文档未指明方法,可参考源代码样例:python-sdk/tests/testclient.py
1 # 获取对应的交易数据,解析出调用方法名和参数 2 txhash = receipt['transactionHash'] 3 txresponse = client.getTransactionByHash(txhash) 4 inputresult = abi_parser.parse_transaction_input(txresponse['input']) 5 print("transaction input parse:", txhash) 6 print(inputresult) 7 8 # 解析该交易在receipt里输出的output,即交易调用的方法的return值 9 outputresult = abi_parser.parse_receipt_output(inputresult['name'], receipt['output']) 10 print("receipt output :", outputresult)
Fisco 是通过智能合约 setAttr() 方法写入数据改变区块链的状态。根据交易哈希 transactionHash 获取交易信息,根据交易信息进而解析输入的参数和返回的参数。和以太坊有所区别,不需要有 getAttr() , 或者说 getttr() 方法是无法获取使用的。
合约文件(此文件需要在 python-sdk/ contracts文件夹下):
1 pragma solidity ^0.4.25; 2 3 contract BoardTrace { 4 string productId; 5 string pClass; 6 string pSize; 7 string bClass; 8 string designStaff; 9 string designCompanyName; 10 string customerName; 11 string customerAddress; 12 string customerTelePhone; 13 14 function setAttr(string memory _productId, string memory _pClass, string memory _bClass, string memory _designStaff, string memory _designCompanyName, string memory _customerName) public returns (string memory, string memory, string memory, string memory, string memory, string memory){ 15 productId = _productId; 16 pClass = _pClass; 17 bClass = _bClass; 18 designStaff = _designStaff; 19 designCompanyName = _designCompanyName; 20 customerName = _customerName; 21 22 return (productId, pClass, bClass, designStaff, designCompanyName, customerName); 23 } 24 } 25
代码(此文件需要在 python-sdk 文件夹下):
1 import sys 2 import os 3 from client.bcosclient import BcosClient 4 from client_config import client_config 5 from client.datatype_parser import DatatypeParser 6 from client.common.compiler import Compiler 7 from utils.abi import * 8 import json 9 import traceback 10 11 class FiscoBcosTest: 12 13 # _contractFilePath = "/home/xsmile/fisco/python-sdk/contracts/" 14 _contractFilePath = "/home/xsmile/software/boardTrace/easyarron/tracewood/python_sdk/contracts/" 15 16 def __init__(self): 17 self.client = BcosClient() 18 19 # 编译合约 20 # .sol 编译会自动生成 .abi, .bin 文件(当前文件下,contracts/ 下面) 21 def compileContract(self, contractName): 22 23 if os.path.isfile(client_config.solc_path) or os.path.isfile(client_config.solcjs_path): 24 Compiler.compile_file(self._contractFilePath + contractName + ".sol") 25 26 abi_file = self._contractFilePath + contractName + ".abi" 27 data_parser = DatatypeParser() 28 data_parser.load_abi_file(abi_file) 29 contract_abi = data_parser.contract_abi 30 31 return contract_abi, data_parser 32 33 # 部署合约 34 def deployContract(self, contractName): 35 #contract_abi = self.compileContract(contractName) 36 with open(self._contractFilePath + contractName + ".bin", 'r') as fi: 37 contract_bin = fi.read() 38 39 contract_result = self.client.deploy(contract_bin) 40 contract_address = contract_result['contractAddress'] 41 42 return contract_address 43 44 # 获取合约数据内容 45 def getInfo(self, contract_address, contract_abi, func): 46 res = self.client.call(contract_address, contract_abi, func) 47 return res 48 49 # 发送交易, 调用改写接口 50 def setInfo(self, contract_address, contract_abi, func, *args): 51 receipt = self.client.sendRawTransactionGetReceipt(contract_address, contract_abi, func, *args) 52 # txhash = receipt['transactionHash'] 53 # return txhash 54 return receipt 55 56 57 # 根据交易哈希获取交易回执 58 def getReceipt(self, tx_hash): 59 res_receipt = self.client.getTransactionReceipt(tx_hash) 60 return res_receipt 61 62 # 根据交易哈希获取交易信息 63 def getTransactionInfo(self, tx_hash): 64 txInfo = self.client.getTransactionByHash(tx_hash) 65 return txInfo 66 67 def finishClient(self): 68 self.client.finish() 69 70 def __str__(self): 71 return self.client.getinfo() 72 73 74 75 def run(): 76 77 fisco = FiscoBcosTest() 78 print('生成实例: ', fisco) 79 contract_name = 'BoardTrace' 80 print('--------------编译合约----------------------------') 81 contract_abi, abi_parser = fisco.compileContract(contract_name) 82 83 ''' 84 85 print('--------------部署合约----------------------------') 86 contract_address = fisco.deployContract(contract_name) 87 print('--------->>', type(contract_abi)) 88 print('合约地址: {} \nabi: {}'.format(contract_address, contract_abi)) 89 90 return ; 91 ''' 92 93 contract_address = '0xb7a506877cd874b0dc0ed1a43762193b60a7b6d4' 94 95 ''' 96 # 通过合约写入数据 97 contract_args = ['2020082911', '书桌', '沉香木', '远阪凛', '斗牛家具有限公司', '张阿喵'] 98 receipt = fisco.setInfo(contract_address, contract_abi, 'setAttr', contract_args) 99 print('--------------交易内容----------------------------') 100 print('receipt: ', receipt) 101 print('transacitonHash:', receipt['transactionHash']) 102 ''' 103 104 105 106 # input ['2020082910', '橱柜', '沉香木', '远阪凛', '斗牛家具有限公司', '张阿喵'] 107 transaction_hash = '0x13e1bcaeff87a41090f486fad4c5ab1ddc5c87d852984520303169a60e33149e' 108 109 # input ['2020082911', '书桌', '沉香木', '远阪凛', '斗牛家具有限公司', '张阿喵'] 110 # transaction_hash = '0xc5160839282dd05fda120b8b1a67a47890ef6b493de6a79a9b6bc18c80248733' 111 112 113 # 根据交易 hash 获取交易回执 114 res_receipt = fisco.getReceipt(transaction_hash) 115 print('res_receipt: {}'.format(json.dumps(res_receipt, indent=4))) 116 117 118 # 根据交易 hash 获取交易信息 119 txInfo = fisco.getTransactionInfo(transaction_hash) 120 print('txInfo: {}'.format(json.dumps(txInfo, indent=4))) 121 122 # 获取对应的交易数据,解析出调用方法名和参数 123 # print('--------------获取对应的交易输入数据----------------------------') 124 inputresult = abi_parser.parse_transaction_input(txInfo['input']) 125 # print(inputresult) 126 127 # 解析该交易在receipt里输出的output,即交易调用的方法的return值 128 print('--------------获取对应的交易输出数据----------------------------') 129 outputresult = abi_parser.parse_receipt_output(inputresult['name'], res_receipt['output']) 130 print("receipt output :", type(outputresult), outputresult) 131 132 ''' 133 # 获取合约数据 134 res = fisco.getInfo(contract_address, contract_abi, 'getAttr') 135 print('--------------获取合约数据存储信息----------------------------') 136 print('res: ', res) 137 ''' 138 139 fisco.finishClient() 140 141 if __name__ == '__main__': 142 run()
运行结果:
1 xsmile@xsmile:~/software/boardTrace/easyarron/tracewood/python_sdk$ python3 fiscoTest.py 2 生成实例: channel 127.0.0.1:20200,groupid :1 3 --------------编译合约---------------------------- 4 INFO >> compile with solc compiler : ./bin/solc/v0.4.25/solc --bin --abi /home/xsmile/software/boardTrace/easyarron/tracewood/python_sdk/contracts/BoardTrace.sol -o contracts --overwrite 5 res_receipt: { 6 "blockHash": "0x12517a791649ddaa7308a95fa47c15b7f2616192ed313179034804b402d6775d", 7 "blockNumber": "0x23", 8 "contractAddress": "0x0000000000000000000000000000000000000000", 9 "from": "0x95198b93705e394a916579e048c8a32ddfb900f7", 10 "gasUsed": "0x18c2a", 11 "input": "0x67a2ef5f00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e4b9a6e6a18c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 12 "logs": [], 13 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 14 "output": "0x00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e4b9a6e6a18c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 15 "root": "0xec7a40e8c0b4117e2ab1994b20f6f5c209990d429fa79cf5f5cdabc250a48651", 16 "status": "0x0", 17 "to": "0xb7a506877cd874b0dc0ed1a43762193b60a7b6d4", 18 "transactionHash": "0xc5160839282dd05fda120b8b1a67a47890ef6b493de6a79a9b6bc18c80248733", 19 "transactionIndex": "0x0" 20 } 21 txInfo: { 22 "blockHash": "0x12517a791649ddaa7308a95fa47c15b7f2616192ed313179034804b402d6775d", 23 "blockNumber": "0x23", 24 "from": "0x95198b93705e394a916579e048c8a32ddfb900f7", 25 "gas": "0x1c9c380", 26 "gasPrice": "0x1c9c380", 27 "hash": "0xc5160839282dd05fda120b8b1a67a47890ef6b493de6a79a9b6bc18c80248733", 28 "input": "0x67a2ef5f00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e4b9a6e6a18c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 29 "nonce": "0x1a79cba2", 30 "to": "0xb7a506877cd874b0dc0ed1a43762193b60a7b6d4", 31 "transactionIndex": "0x0", 32 "value": "0x0" 33 } 34 --------------获取对应的交易输出数据---------------------------- 35 receipt output : <class 'tuple'> ('2020082911', '书桌', '沉香木', '远阪凛', '斗牛家具有限公司', '张阿喵') 36 xsmile@xsmile:~/software/boardTrace/easyarron/tracewood/python_sdk$ python3 fiscoTest.py 37 生成实例: channel 127.0.0.1:20200,groupid :1 38 --------------编译合约---------------------------- 39 INFO >> compile with solc compiler : ./bin/solc/v0.4.25/solc --bin --abi /home/xsmile/software/boardTrace/easyarron/tracewood/python_sdk/contracts/BoardTrace.sol -o contracts --overwrite 40 res_receipt: { 41 "blockHash": "0x02d0e02c03513bfebfe9205bb954bd7f0adcd188bbad320b71c77ea4d7b4ebf7", 42 "blockNumber": "0x22", 43 "contractAddress": "0x0000000000000000000000000000000000000000", 44 "from": "0x95198b93705e394a916579e048c8a32ddfb900f7", 45 "gasUsed": "0x27534", 46 "input": "0x67a2ef5f00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e6a9b1e69f9c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 47 "logs": [], 48 "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 49 "output": "0x00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e6a9b1e69f9c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 50 "root": "0x9ba5787752ab25d50b2dcf6e16d333af9f8904458d4439a2c77dc97ddd6f978f", 51 "status": "0x0", 52 "to": "0xb7a506877cd874b0dc0ed1a43762193b60a7b6d4", 53 "transactionHash": "0x13e1bcaeff87a41090f486fad4c5ab1ddc5c87d852984520303169a60e33149e", 54 "transactionIndex": "0x0" 55 } 56 txInfo: { 57 "blockHash": "0x02d0e02c03513bfebfe9205bb954bd7f0adcd188bbad320b71c77ea4d7b4ebf7", 58 "blockNumber": "0x22", 59 "from": "0x95198b93705e394a916579e048c8a32ddfb900f7", 60 "gas": "0x1c9c380", 61 "gasPrice": "0x1c9c380", 62 "hash": "0x13e1bcaeff87a41090f486fad4c5ab1ddc5c87d852984520303169a60e33149e", 63 "input": "0x67a2ef5f00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000a32303230303832393130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006e6a9b1e69f9c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e6b289e9a699e69ca800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e8bf9ce998aae5879b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018e69697e7899be5aeb6e585b7e69c89e99990e585ace58fb800000000000000000000000000000000000000000000000000000000000000000000000000000009e5bca0e998bfe596b50000000000000000000000000000000000000000000000", 64 "nonce": "0x101f2919", 65 "to": "0xb7a506877cd874b0dc0ed1a43762193b60a7b6d4", 66 "transactionIndex": "0x0", 67 "value": "0x0" 68 } 69 --------------获取对应的交易输出数据---------------------------- 70 receipt output : <class 'tuple'> ('2020082910', '橱柜', '沉香木', '远阪凛', '斗牛家具有限公司', '张阿喵')
----------------------------------------声明----------------------------------------
----------------------------------------end----------------------------------------