我使用web3.py读取大量数据,但我看不出这里出了什么问题。 getValue()和getValue2(),两者都可以从remix.ethereum.org调用而没有错误,但是当我使用我发布的python代码时,我只能读出getValue2(),函数getValue()会抛出错误它看起来像是在气体限制下运行。但由于该函数没有抛出remix.ethereum调用的错误,我真的不明白为什么会出现这样的气体错误:
合同合同:
pragma solidity ^0.4.19;
contract TestContract {
uint [1000000] val;
uint [200000] val2;
function TestContract(){
}
function getValue() external view returns(uint [1000000]){
return val;
}
function getValue2() external view returns(uint [200000]){
return val2;
}
}
Python代码:
import json
import web3
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
# web3
w3 = Web3(HTTPProvider('https://ropsten.infura.io'))
# Instantiate and deploy contract
contractAddress = '0x37c587c2174bd9248f203947d7272bf1b8f91fa9'
with open('testfactory.json', 'r') as abi_definition:
abi = json.load(abi_definition)
contract_instance = w3.eth.contract(contractAddress, abi=abi,ContractFactoryClass=ConciseContract)
arr2=contract_instance.getValue2() #works fine
print(len(arr2))
arr=contract_instance.getValue() #throws an error, posted below
print(len(arr))
testfactory.json:
[
{
"constant": true,
"inputs": [],
"name": "getValue2",
"outputs": [
{
"name": "",
"type": "uint256[200000]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getValue",
"outputs": [
{
"name": "",
"type": "uint256[1000000]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
]
pyton中的错误:
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\web3\contract.py", line 844, in call_contract_function
output_data = decode_abi(output_types, return_data)
File "C:\Python36\lib\site-packages\eth_abi\abi.py", line 109, in decode_abi
return decoder(stream)
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 102, in __call__
return self.decode(stream)
File "C:\Python36\lib\site-packages\eth_utils\functional.py", line 22, in inner
return callback(fn(*args, **kwargs))
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 140, in decode
yield decoder(stream)
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 102, in __call__
return self.decode(stream)
File "C:\Python36\lib\site-packages\eth_utils\functional.py", line 22, in inner
return callback(fn(*args, **kwargs))
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 198, in decode
yield cls.item_decoder(stream)
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 102, in __call__
return self.decode(stream)
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 165, in decode
raw_data = cls.read_data_from_stream(stream)
File "C:\Python36\lib\site-packages\eth_abi\decoding.py", line 247, in read_data_from_stream
len(data),
eth_abi.exceptions.InsufficientDataBytes: Tried to read 32 bytes. Only got 0 bytes
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/Sebi/PycharmProjects/web3/test.py", line 18, in <module>
arr=contract_instance.getValue()
File "C:\Python36\lib\site-packages\web3\contract.py", line 805, in __call__
return self.__prepared_function(**kwargs)(*args)
File "C:\Python36\lib\site-packages\web3\contract.py", line 866, in call_contract_function
raise_from(BadFunctionCallOutput(msg), e)
File "C:\Python36\lib\site-packages\web3\utils\exception_py3.py", line 2, in raise_from
raise my_exception from other_exception
web3.exceptions.BadFunctionCallOutput: Could not decode contract function call getValue return data 0x for output_types ['uint256[1000000]']
有什么建议我可以做什么? web3.py v4.0中是否有错误?