Value HexBytes(*hex string*) 为 97 字节,但在使用 Contract.transact() 时应为 32
Posted
技术标签:
【中文标题】Value HexBytes(*hex string*) 为 97 字节,但在使用 Contract.transact() 时应为 32【英文标题】:The Value HexBytes(*hex string*) is 97 bytes but should be 32 when using Contract.transact() 【发布时间】:2018-04-18 04:42:57 【问题描述】:我做了一个简单的智能合约如下
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 客户端有关,但我还没有找到修复错误的方法。任何帮助或建议尝试将不胜感激!
【问题讨论】:
你在哪个网络,主网?您从源代码安装了 web3.py;你在哪个承诺上? (处于 alpha 状态)你能打印出web3.manager.request_blocking('eth_getBlockByNumber', ['latest', False])
的结果吗?
我在 rinkeby 网络上。我正在进行 10 月 30 日的最新提交。我做了一个小python程序只是为了尝试.request_blocking并得到'Request for id:eth_getBlockByNumber not found'
我在 Ubuntu 上使用以太坊钱包部署了合约,并安装了 geth。我正在考虑做一个干净的虚拟机并尝试重新从头开始构建所有内容,而不是使用以太坊钱包来查看是否可以解决问题
'Request for id:eth_getBlockByNumber not found'
是一个非常奇怪的错误,可能来自连接到损坏的客户端。我注意到您在上面创建了两次web3
。您是否可能连接到与您打算不同的客户端? IPCProvider
不带参数尝试连接到 go-ethereum (geth) 客户端——尽管一些备用客户端会创建一个 IPC 连接文件,geth 通常会将其放置在其中以保持兼容。重新安装好运吗?
全新重新安装似乎已经做到了。我用 ropsten 网络而不是 rinkeby 做了一个新的虚拟机,它工作正常。仍然有另一个 VM,并且已经玩过几次,但仍然没有任何工作。倾向于它是一种安装,但在某种程度上,但谁知道呢。
【参考方案1】:
这似乎是节点安装或启动时出现的错误,其中某些东西运行不正确。我最终重新安装了所有相关组件,现在它似乎可以正常工作了!
【讨论】:
以上是关于Value HexBytes(*hex string*) 为 97 字节,但在使用 Contract.transact() 时应为 32的主要内容,如果未能解决你的问题,请参考以下文章
org.json.JSONException: Value okhttp3.internal.http.RealResponseBody@da68fa1 of type java.lang.Strin