我制作了一个简单的智能合约,如下所示
pragma solidity ^0.4.15;
contract ECDS {
Computer[] public computer;
event gotComputers(string _msg);
event addedComputer(string _msg, uint _cores, uint _cache, uint _coresVM, bool _mining);
struct Computer {
uint cores;
uint cache;
uint coresVM;
bool mining;
}
function addComputer(uint _cores, uint _cache, uint _coresVM, bool _mining) returns (bool success){
Computer memory newComputer;
newComputer.cores = _cores;
newComputer.cache = _cache;
newComputer.coresVM = _coresVM;
newComputer.mining = _mining;
computer.push(newComputer);
addedComputer("A computer was added", _cores, _cache, _coresVM, _mining);
return true;
}
function getComputer() constant returns (uint[],uint[],uint[],bool[]){
uint length = computer.length;
uint[] memory coresList = new uint[](length);
uint[] memory cacheList = new uint[](length);
uint[] memory coresVMList = new uint[](length);
bool[] memory miningList = new bool[](length);
for(uint i = 0; i < length; i++){
Computer memory currentComputer;
currentComputer = computer[i];
coresList[i] = currentComputer.cores;
cacheList[i] = currentComputer.cache;
coresVMList[i] = currentComputer.coresVM;
miningList[i] = currentComputer.mining;
}
gotComputers("Got the list of computers");
return (coresList, cacheList, coresVMList, miningList);
}
function availableFreeCores() constant returns (uint[]){
uint length = computer.length;
uint[] memory freeCoresList = new uint[](length);
for(uint i = 0; i < length; i++){
Computer memory currentComputer;
currentComputer = computer[i];
uint freeCores = currentComputer.cores - (currentComputer.cache / 2);
freeCoresList[i] = freeCores;
}
return freeCoresList;
}
}
然后我在 python 程序中成功使用了 call().getComputer(),但是在使用 transact().addComputer(cores, cache, coresVM, True) 时出现错误。Python 如下所示。
from web3 import Web3, HTTPProvider, IPCProvider
import json
contractAdress = '0x2343A6d7c85Ab43f94E50C128A7d3109A3647a1D'
web3 = Web3(HTTPProvider('52.169.42.101:30303'))
with open('factory.json', 'r') as abi_definition:
abi = json.load(abi_definition)
web3 = Web3(IPCProvider())
fContract = web3.eth.contract(contractAdress, abi=abi)
cores = 12
cache = 15
coresVM = 0
mining = True
fContract.transact().addComputer(cores, cache, coresVM, True)
print('Computers',fContract.call().getComputer())
如果我注释掉 transact().addComputer 它会成功运行并返回正确的值。但是将它包含在程序中,我得到的完整错误是:
Traceback (most recent call last):
File "ECDS.py", line 15, in <module>
fContract.transact().addComputer(cores, cache, coresVM, True)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/contract.py", line 821, in transact_with_contract_function
txn_hash = contract.web3.eth.sendTransaction(transact_transaction)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/eth.py", line 211, in sendTransaction
get_buffered_gas_estimate(self.web3, transaction),
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/utils/transactions.py", line 160, in get_buffered_gas_estimate
gas_limit = get_block_gas_limit(web3)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/utils/transactions.py", line 151, in get_block_gas_limit
block = web3.eth.getBlock(block_identifier)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/eth.py", line 127, in getBlock
[block_identifier, full_transactions],
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/manager.py", line 74, in request_blocking
response = self._make_request(method, params)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/manager.py", line 57, in _make_request
return request_func(method, params)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/middleware/attrdict.py", line 20, in middleware
response = make_request(method, params)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/middleware/formatting.py", line 32, in middleware
formatter(response['result']),
File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__ (cytoolz/functoolz.c:3996)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/utils/formatters.py", line 62, in apply_formatter_if
return formatter(value)
File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__ (cytoolz/functoolz.c:3996)
File "/usr/local/lib/python3.5/dist-packages/ethereum_utils-0.5.1-py3.5.egg/eth_utils/functional.py", line 33, in inner
return callback(fn(*args, **kwargs))
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/utils/formatters.py", line 72, in apply_formatters_to_dict
yield key, formatters[key](item)
File "cytoolz/functoolz.pyx", line 232, in cytoolz.functoolz.curry.__call__ (cytoolz/functoolz.c:3996)
File "/usr/local/lib/python3.5/dist-packages/web3-3.16.1-py3.5.egg/web3/middleware/pythonic.py", line 111, in to_hexbytes
result, len(result), num_bytes
ValueError: The value HexBytes('0xd783010703846765746887676f312e392e31856c696e7578000000000000000019905343b07339c85f1797c521d65dc9238743fdc52261c5ee4a4bd39004426e4b06c492df58bfbb037151d294187535732d23b5c768a83f452ceda2d5b6128400') is 97 bytes, but should be 32
我尝试将 Hex 解码为 ascii,我发现它与 geth go 客户端有关,但我还没有找到解决该错误的方法。任何帮助或尝试的建议将不胜感激!