web3 solidity变量无法设置值

时间:2017-11-23 02:10:10

标签: transactions blockchain ethereum solidity web3

我的代码

https://gist.github.com/tlatkdgus1/4885faa14ca123024fbb3fd194404352

结果

https://gist.github.com/tlatkdgus1/e87c7f247fb3d0376613d25e8286ec80

Geth的

https://gist.github.com/tlatkdgus1/5939f90a7b5c478f78c8ec0ecca45b5b

抱歉,我无法很好地使用stackoverflow,因此请在gist中上传源代码




那么如何设置可靠性变量值?

尝试列表

1. solidity setInt (X)
2. gas increase (X)
3. solidity variable name change (X)

我认为这部分是问题,但我不知道:(

while w3.eth.getTransactionReceipt(tx_hash) is None:
    print (w3.eth.getTransactionReceipt(tx_hash))

有什么想法吗?

参考 https://github.com/pipermerriam/web3.py

1 个答案:

答案 0 :(得分:0)

我回答了我认为您在问的问题:为什么getLog不会使用setLog返回先前表达式中设置的值?

答案: 到setLog来电时,getLog交易尚未开采。

为了便于阅读,我已经清理了你的代码,并将你的while循环变成了一个超时的函数。我也冒昧地使用web3.py v4预发布版进行更新。

web3.py预发布(allong with eth_tester)可以安装:

$ pip install --pre web3[tester]

import json
import time
from web3 import Web3
from web3.providers.eth_tester import EthereumTesterProvider
from solc import compile_source
from web3.contract import ConciseContract
import time

contract_source_code = '''
pragma solidity ^0.4.11;

contract contract_log {
string name;
string time;
string product;

function contract_log(){
    name='kk';
    }
function setLog(string a, string b, string c) public {
    name = a;
    time = b;
    product = c;
    }


function getLog() constant returns (string, string, string) {
    return (name, time, product);
}

}
'''

def wait_on_tx_receipt(tx_hash):
    start_time = time.time()
    while True:
        if start_time + 60 < time.time():
            raise TimeoutError("Timeout occurred waiting for tx receipt")
        if w3.eth.getTransactionReceipt(tx_hash):
            return w3.eth.getTransactionReceipt(tx_hash)


compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:contract_log']

w3 = Web3(EthereumTesterProvider())

contract = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])

deploy_tx_hash = contract.deploy(transaction={'from': w3.eth.coinbase, 'gas':500000})
contract_address = wait_on_tx_receipt(deploy_tx_hash).contractAddress
contract_instance = w3.eth.contract(abi=contract_interface['abi'], address=contract_address)

tx_hash = contract_instance.functions.setLog(
    'tlatk',
    '12',
    'product').transact({'from':w3.eth.coinbase,'gas':500000})

wait_on_tx_receipt(tx_hash)

print(contract_instance.functions.getLog().call())