Example #1
0
    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)
Example #2
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
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
 def signed_contract_deploy(self, bytecode, abi, fromAddress, privateKey):
     """
     签名部署合约
     :param txType:取值类型 0-主币转账交易 1-合约发布 2-合约调用 3-投票 4-权限
     :param bytecode:bytecode,二进制数组
     :param abi:abi,二进制数组
     :param fromAddress:钱包地址
     :param privateKey:钱包私钥
     :return:transactionHash
     """
     txType = encoder.encode_type(1)
     bytecode = bytecode
     abi = abi
     rlpList = [txType, bytecode, abi]
     signedData = self.get_signed_data(fromAddress, "", rlpList, privateKey)
     transactionHash = self.eth.sendRawTransaction(signedData)
     transactionHash = HexBytes(transactionHash).hex().lower()
     return transactionHash
Example #6
0
 def contract_deploy(self, bytecode, abi, fromAddress):
     """
     非签名合约部署
     :param txType:取值类型 0-主币转账交易 1-合约发布 2-合约调用 3-投票 4-权限
     :param bytecode:合约bin(wasm文件),二进制数组
     :param abi:abi(json文件),二进制数组
     :param fromAddress:钱包地址
     :return:合约部署transactionHash
     """
     txType = encoder.encode_type(1)
     bytecode = bytecode
     abi = abi
     rlpList = [txType, bytecode, abi]
     data = rlp.encode(rlpList)
     transactionHash = self.eth.sendTransaction({
         "from": fromAddress,
         "gas": "0x6fffffff",
         "gasPrice": "0x8250de00",
         "data": data,
     })
     transactionHash = HexBytes(transactionHash).hex().lower()
     return transactionHash
Example #7
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'