首页 > 解决方案 > 向以太坊智能合约发送数据,交易未被验证(区块未形成)

问题描述

我根据智能合约中的说明编写了一个可靠的智能合约,用于从树莓派接收数据,并部署在 testrpc 中。我使用相同的帐户来部署智能合约以及覆盆子地址。

`pragma solidity ^0.5.4;

contract StoreIntegerValue{
    address owner;
    int sensorData;

    constructor (int _sensorData) public  {
        sensorData = _sensorData;
        owner = msg.sender;
    }

    function setSensorData(int _sensorData) public {
        require(msg.sender == owner);
        sensorData = _sensorData;
    }

    function getSensorData() public returns (int) {
        require(msg.sender == owner);
        return sensorData;
    }
}` 

我希望使用 web3.py 将传感器数据从树莓派发送到智能合约。我尝试设置使用 web3.py 调用智能合约的阈值限制。

from web3 import Web3, HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract
from random import randint
import time
import json

#actual web3 provider (for more info: http://web3py.readthedocs.io/en/stable/providers.html)
w3 = Web3(HTTPProvider('http://127.0.0.1:8545'))

#abi can be generated form the command line with solc or online with Remix IDE
abi = json.loads('''[
    {
        "constant": false,
        "inputs": [],
        "name": "getSensorData",
        "outputs": [
            {
                "name": "",
                "type": "int256"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_sensorData",
                "type": "int256"
            }
        ],
        "name": "setSensorData",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "_sensorData",
                "type": "int256"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    }
]

''')


address = Web3.toChecksumAddress("0xc9ab3c7431dd3298b26546ffc2526d9b45469cf0")

StoreIntegerValue = w3.eth.contract(
    address, abi=abi, ContractFactoryClass=ConciseContract)

#Replace with real account address for raspi
raspi = 0x26b3bff595e86d4c318f34f2e92195f44eaa327b

#Example function to submit data to the block chain
def submitSensorData(data):
    #note that data must be an integer,
    StoreIntegerValue.setSensorData(int(data), transact={'from': raspi})

def stopwatch(seconds):
    TempCount=0
    Count=0
    start=time.time()
    time.clock()
    elapsed=0
    while elapsed < seconds:
        elapsed = time.time()-start
        Temp= randint(15,25)
        print("temp values: ", Temp)
        if(Temp>20):
            TempCount=TempCount + 1
            Count = TempCount
            TempCount = 0
            time.sleep(2)
    return Count #, int(time.time())

k= stopwatch(30)
if(k>3):
    submitSensorData(k)

代码 web3 和 solidity 都运行没有任何错误,但是由于交易,我在 testrpc 中看不到任何新的块形成。是否有兴趣知道是否按照代码发送数据?如果正在发送,为什么没有块形成?我的代码中是否有任何语义错误?请帮助我解决问题或解决问题的任何线索。

标签: ethereumsoliditysmartcontractsweb3

解决方案


推荐阅读