当前位置:   article > 正文

区块链-FISCO BCOS Python SDK使用_fisco bcos区块链系统中使用python sdk进行压力测试

fisco bcos区块链系统中使用python sdk进行压力测试

1、控制台的使用

  • -下载sdk的工程
git clone https://github.com/FISCO-BCOS/python-sdk
  • 1
  • 安装依赖
cd python-sdk 
pip install -r requirements.txt
#如果网络不通,可以更换国内服务器
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
  • 1
  • 2
  • 3
  • 4
    • 创建用户
python-sdk$ python3 console.py newaccount larry larry123

INFO >> user input : ['newaccount', 'larry', 'larry123']

starting : larry larry123  , if save:False
new address :    0x56730bD892FDcBbf13c2d8AB8Fd716D1D9231923
new privkey :    0xe2c36a79077866ac8b514d313baf4e84668775fbc340a914ce0ddf702e6ff35a
new pubkey :     0x7b188f09a34189b3f3352a8b56c54a6c91577d9967c8cedb6bd47936e4adaa4f3e829b9fa5dc275f5773bff0905fc08b75a3f7a1bc5eee39d2fba1cf8e4f9baa
encrypt use time : 0.825 s
save to file : [bin/accounts/larry.keystore]
>>-------------------------------------------------------
INFO >> read [bin/accounts/larry.keystore] again after new account,address & keys in file:
decrypt use time : 0.809 s
address:         0x56730bD892FDcBbf13c2d8AB8Fd716D1D9231923
privkey:         0xe2c36a79077866ac8b514d313baf4e84668775fbc340a914ce0ddf702e6ff35a
pubkey :         0x7b188f09a34189b3f3352a8b56c54a6c91577d9967c8cedb6bd47936e4adaa4f3e829b9fa5dc275f5773bff0905fc08b75a3f7a1bc5eee39d2fba1cf8e4f9baa

account store in file: [bin/accounts/larry.keystore]

**** please remember your password !!! *****
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

将生成的密钥倒入到Webase-front

在这里插入图片描述

配置python环境

将client_config.template修改成 client_config.py

    """
    类成员变量,便于用.调用和区分命名空间
    """
    # 整个客户端的全局配置,影响console相关的账户目录、日志目录、合约目录等
    # crypto_type : 大小写不敏感:"GM" for 国密, "ECDSA" 或其他是椭圆曲线默认实现。
    crypto_type = "ECDSA"
    # crypto_type = "GM"
    ssl_type = crypto_type  # 和节点tls通信方式,如设为gm,则使用国密证书认证和加密
    # ssl_type = "GM"
    set_crypto_type(crypto_type)  # 使其全局生效
    # 默认日志输出目录,该目录不会自动建,必须先建立
    logdir = "bin/logs"
    # 合约相关路径
    contract_dir = "contracts"
    contract_info_file = "bin/contract.ini"  # 保存已部署合约信息的文件
    # 账号文件相关路径
    account_keyfile_path = "bin/accounts"  # 保存keystore文件的路径,在此路径下,keystore文件以 [name].keystore命名
    # account_keyfile = "pyaccount.keystore"
    account_keyfile = "projectuser.pem"
    account_password = ""  # 实际使用时建议改为复杂密码
    gm_account_keyfile = ""  # 国密账号的存储文件,可以加密存储,如果留空则不加载
    gm_account_password = ""  # 如果不设密码,置为None或""则不加密
    
    # ---------编译器 compiler related--------------
    # path of solc compiler
    solc_path = "bin/solc/solc.exe"
    # solc_path = "bin/solc/solc6.exe"
    solcjs_path = "solcjs"
    gm_solc_path = "./bin/solc/solc-gm.exe"
    # ---------console mode, support user input--------------
    background = True

    # ------------------FISCO BCOS3.0 Begin----------------------------------------
    # FISCO BCOS3.0的配置段,如连接FISCO BCOS2.0版本,无需关心此段
    # FISCO BCOS3.0 c底层sdk的配置,都在bcos3_config_file里,无需配置在此文件
    bcos3_lib_path = "bcos3sdklib"
    bcos3_config_file = "bcos3sdklib/bcos3_sdk_config.ini"
    bcos3_group = "group0"
    bcos3_check_node_version = True  # 是否在初始化后验证一次node版本
    bcos3_when_version_mismatch = "WARN"  # WARN 或 "ERROR" ,如果版本不匹配,WARN只是打个警告,ERROR就抛异常了,建议WARN
    bcos3_major_version = 3
    bcos3_max_miner_version = 2  # 目前最大版本号验证到3.2,后续新版本验证后持续更新
    # -------------------FISCO BCOS3.0 End-----------------------------------------
    
    # --------------------------------------
    # FISCO BCOS2.0的配置段,如连接FISCO BCOS3.0版本,无需关心此段
    # keyword used to represent the RPC Protocol
    PROTOCOL_RPC = "rpc"
    # keyword used to represent the Channel Protocol
    PROTOCOL_CHANNEL = "channel"
    fiscoChainId = 1  # 链ID,和要通信的节点*必须*一致
    groupid = 1  # 群组ID,和要通信的节点*必须*一致,如和其他群组通信,修改这一项,或者设置bcosclient.py里对应的成员变量
    client_protocol = "channel"  # or PROTOCOL_CHANNEL to use channel prototol
    # client_protocol = PROTOCOL_CHANNEL
    remote_rpcurl = ""  # 采用rpc通信时,节点的rpc端口,和要通信的节点*必须*一致,如采用channel协议通信,这里可以留空
    channel_host = "192.168.1.135"  # 采用channel通信时,节点的channel ip地址,如采用rpc协议通信,这里可以留空
    channel_port = 20200  # 节点的channel 端口,如采用rpc协议通信,这里可以留空
    channel_ca = "bin/ca.crt"  # 采用channel协议时,需要设置链证书,如采用rpc协议通信,这里可以留空
    channel_node_cert = "bin/sdk.crt"  # 采用channel协议时,需要设置sdk证书,如采用rpc协议通信,这里可以留空
    channel_node_key = "bin/sdk.key"  # 采用channel协议时,需要设置sdk私钥,如采用rpc协议通信,这里可以留空
    channel_en_crt = ""  # 仅国密双证书使用,加密证书
    channel_en_key = ""  # 仅国密双证书使用,加密key
  • 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

如果使用fisco3.x还需要配置bcos3sdklib/bcos3_sdk_config.ini文件

[common]
    ; if ssl connection is disabled, default: false
    ; disable_ssl = true
    ; thread pool size for network message sending recving handing
    thread_pool_size = 8
    ; send message timeout(ms)
    message_timeout_ms = 10000

; ssl cert config items,  
[cert]
    ; ssl_type: ssl or sm_ssl, default: ssl
    ssl_type = ssl
    ; directory the certificates located in, defaul: ./conf
    ca_path=./bcos3sdklib
    ; the ca certificate file
    ca_cert=ca.crt
    ; the node private key file
    sdk_key=sdk.key
    ; the node certificate file
    sdk_cert=sdk.crt

[peers]
# supported ipv4 and ipv6 
    node.0=192.168.1.135:20200
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

调用智能合约

先编写一下智能合约,并部署在区块链网络里

pragma solidity ^0.8.7;

contract hello {
    string public name;
   constructor(string memory _name) public {
        name = _name;
    }
    
    function setName(string memory _name) public {
        name = _name;
    }
    
    function getName() public returns(string memory){
        return name;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

部署后可以得到地址于ABI信息,合约地址为:0x6849f21d1e455e9f0712b1e99fa4fcd23758e8f1

合约调用前,需要先将fisco-bcos节点的证书文件拷贝至本工程的bin目录下,下面代码使用的abi和合约地址(0x6849f21d1e455e9f0712b1e99fa4fcd23758e8f1)为之前部署的信息。
将abi信息保存到abi_file文件里

import sys
sys.path.append("./python-sdk")
from bcos3sdk.bcos3client import Bcos3Client
from client.datatype_parser import DatatypeParser
client = Bcos3Client() # 
# client = BcosClient() 如果用的fisco2.x使用这个方法

def init_abi(aibfile):
    data_parser = DatatypeParser()
    data_parser.load_abi_file(aibfile)
    return data_parser.contract_abi
#  查询数据
data = client.call('0x6849f21d1e455e9f0712b1e99fa4fcd23758e8f1', init_abi(config.abi_file), "getName", [])
print(data)

data = client.sendRawTransactionGetReceipt("0x6849f21d1e455e9f0712b1e99fa4fcd23758e8f1",init_abi(config.abi_file),"setName",["zhangsan"])
print(data)
data = client.call("0x6849f21d1e455e9f0712b1e99fa4fcd23758e8f1",init_abi(config.abi_file),"getName")
print(data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用SDK获取区块数据并解析input信息

def Block():
    blockNum = client.getBlockNumber()
    print(blockNum)
    data = []
    for i in range(0, blockNum+1):
        data.append(pare_trans(client.getBlockByNumber(i)))
    response = {'code': 0, 'message': data}
    return response
    
def pare_trans(block_info):
    transactions = block_info['transactions']
    data = []
    for transaction in transactions:
        # print(transaction)
        input = transaction['input']
        result = trans_input(input)
        transaction['input'] = result
        data.append(transaction)
    block_info['transactions'] = data
    return block_info

def trans_input(input):
    data_parser = DatatypeParser()
    data_parser.load_abi_file(config.face_gate_abi_file)
    result = data_parser.parse_transaction_input(input)
    # print(result)
    return result
  • 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

END!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/712521
推荐阅读
相关标签
  

闽ICP备14008679号