Exemple #1
0
def contract_transaction(url):
    """初始化web3"""
    pt = PlatonContractTransaction(url)
    address = pt.w3.toChecksumAddress(pt.eth.coinbase)
    pt.w3.personal.unlockAccount(address, '88888888', 99999999)
    """部署合约"""
    resp = pt.contract_deploy(get_byte_code(wasm_path),
                              get_abi_bytes(abi_path), address)
    result = pt.eth.waitForTransactionReceipt(resp)
    contract_address = result['contractAddress']
    """发送交易"""
    data_list = [encoder.encode_type(2), encoder.encode_string('set')]
    trans_resp = pt.contract_transaction(address, contract_address, data_list)
    result = pt.w3.eth.waitForTransactionReceipt(trans_resp)
    """查看eventData"""
    topics = result['logs'][0]['topics']
    data = result['logs'][0]['data']
    event = Event(json.load(open(abi_path)))
    event_data = event.event_data(topics, data)
    assert 'set success' in event_data['create'][0]
    """call方法查询交易"""
    call_data_list = [encoder.encode_type(2), encoder.encode_string('get')]
    receive = pt.contract_call(address, contract_address, call_data_list)
    receive = int.from_bytes(receive, 'big')
    assert receive == 11
    def test_consensus_and_deconsensus_contract_transaction(self, pt1):
        '''
        非共识节点&共识节点 wasm合约交易、event事件、call方法测试
        '''
        pt = PlatonContractTransaction(pt1)
        wasm_path = abspath('./data/contract/sum.wasm')
        abi_path = abspath('./data/contract/sum.cpp.abi.json')
        address = pt.eth.coinbase
        contract_address = contract_deploy(pt, wasm_path, abi_path)

        # 发送合约交易
        data_list = [encoder.encode_type(
            2), encoder.encode_string('set')]
        trans_hex = pt.contract_transaction(
            address, contract_address, data_list)
        try:
            result = pt.eth.waitForTransactionReceipt(trans_hex)
        except:
            assert False, "等待超时,交易哈希:{}".format(trans_hex)
        # 解析event事件
        topics = result['logs'][0]['topics']
        data = result['logs'][0]['data']
        event = Event(json.load(open(abi_path)))
        event_data = event.event_data(topics, data)
        assert 'set success' in event_data['create'][0], '合约交易失败,event事件无"set success"'
        # call方法查询交易结果
        data_list_for_call = [encoder.encode_type(
            2), encoder.encode_string('get')]
        receive = pt.contract_call(
            address, contract_address, data_list_for_call)
        decoded_sum_result = int.from_bytes(receive, 'big')
        assert decoded_sum_result == 1, '自增失败,预期自增后结果为1,实际值为{}'.format(
            decoded_sum_result)
    def test_calc_contracts(self, wasm_path, abi_path, set_value, expect_value):
        '''
        各算法合约计算结果测试
        '''
        pt_one = PlatonContractTransaction(self.nocollusion_url)
        address = pt_one.eth.coinbase
        contract_address = contract_deploy(
            pt_one, wasm_path, abi_path)

        # 合约set方法存入int型数值
        data_list = [encoder.encode_type(2), encoder.encode_string('set'),
                     encoder.encode_int('int64', set_value)]
        trans_hex = pt_one.contract_transaction(
            address, contract_address, data_list)
        try:
            result = pt_one.eth.waitForTransactionReceipt(trans_hex)
        except:
            assert False, "等待超时,交易哈希:{}".format(trans_hex)
        # 合约get方法获取存入的数值校验是否一致
        data_list_for_call = [encoder.encode_type(
            2), encoder.encode_string('get')]
        receive = pt_one.contract_call(
            address, contract_address, data_list_for_call)
        get_value = contract_call_result_decode(receive)
        assert get_value == expect_value, 'get结果异常,预期值:{},实际结果:{}'.format(
            expect_value, get_value)
    def test_consensus_and_deconsensus_signed_contract_set_and_get_function(self, pt1):
        '''
        非共识节点&共识节点 wasm签名合约输入、输出结果校验
        '''
        pt = PlatonContractTransaction(pt1)
        wasm_path = abspath('./data/contract/inputtest.wasm')
        abi_path = abspath(
            './data/contract/inputtest.cpp.abi.json')
        address = pt.eth.coinbase
        contract_address = self._singed_contract_deploy(
            pt, wasm_path, abi_path)
        set_value = -100

        # 合约set方法存入int型数值
        data_list = [encoder.encode_type(2), encoder.encode_string('set'),
                     encoder.encode_int('int64', set_value)]
        trans_hex = pt.signed_contract_transaction(
            address, contract_address, data_list, self.private_key)
        result = ''
        for i in range(3):
            try:
                result = pt.eth.waitForTransactionReceipt(trans_hex)
            except:
                pt.reconnect()
        assert result != '', "等待超时,交易哈希:{}".format(trans_hex)

        # 合约get方法获取存入的数值校验是否一致
        data_list_for_call = [encoder.encode_type(
            2), encoder.encode_string('get')]
        receive = pt.contract_call(
            address, contract_address, data_list_for_call)
        get_value = contract_call_result_decode(receive)
        assert get_value == set_value, 'get结果异常,预期值:{},实际结果:{}'.format(
            set_value, get_value)
        time.sleep(1)
def encode(data_dict):
    if "int" in data_dict["type"]:
        if data_dict["type"] == "int":
            data_dict["type"] = "int8"
        encode_result = encoder.encode_int(data_dict["type"],
                                           data_dict["value"])
    elif data_dict["type"] == "double":
        encode_result = pack('f', data_dict["value"])
    elif data_dict["type"] == "long":
        encode_result = encoder.encode_int("int64", data_dict["value"])
    elif data_dict["type"] == "bool":
        encode_result = encoder.encode_boolean(data_dict["value"])
    elif data_dict["type"] == "char":
        encode_result = encoder.encode_string(data_dict["value"])
    else:
        raise Exception("""参数类型不正确!
        类型必须是int,int8,uint8,int16,uint16,int32,uint32,int64,uint64,double,long,bool,char
        """)
    return encode_result
Exemple #6
0
 resp = wt.contract_deploy(
     1,
     get_byte_code(
         r'D:\workspace\CTest\Platon-contract\build\user\inputtest\inputtest.wasm'
     ),
     get_abi_bytes(
         r'D:\workspace\CTest\Platon-contract\build\user\inputtest\inputtest.cpp.abi.json'
     ), addrress)
 result = wt.eth.waitForTransactionReceipt(resp)
 contractAddress = result['contractAddress']
 print(contractAddress)
 """发送交易"""
 # contractAddress = '0x6c54DD7c43bc1EC156b6aA684A61610a9ec967A9'
 dataList = [
     encoder.encode_type(2),
     encoder.encode_string('set'),
     encoder.encode_int('int64', 100)
 ]
 transResp = wt.contract_transaction(addrress, contractAddress, dataList)
 result = wt.w3.eth.waitForTransactionReceipt(transResp)
 print(result)
 """查看eventData"""
 topics = result['logs'][0]['topics']
 print(topics)
 data = result['logs'][0]['data']
 print(data)
 event = Event(
     json.load(
         open(
             r'D:\workspace\CTest\Platon-contract\build\user\inputtest\inputtest.cpp.abi.json'
         )))