简体   繁体   English

Kaleido VM处于只读模式

[英]Kaleido vm in read-only mode

I've successfully deployed the following contract on Kaleido: 我已经在Kaleido上成功部署了以下合同:

pragma solidity ^0.4.0;

contract Greeter {
    string public greeting;

    function Greeter() {
        greeting = 'Hello';
    }

    function setGreeting(string _greeting) public {
        greeting = _greeting;
    }

    function greet() constant returns (string) {
        return greeting;
    }
}

I try to interact with the contract like so: 我尝试像这样与合同进行交互:

from web3 import Web3
from web3.providers import HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract

# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.0;

contract Greeter {
    string public greeting;

    function Greeter() {
        greeting = 'Hello';
    }

    function setGreeting(string _greeting) public {
        greeting = _greeting;
    }

    function greet() constant returns (string) {
        return greeting;
    }
}
'''

compiled_sol = compile_source(contract_source_code) 
contract_interface = compiled_sol[':Greeter']

w3 = Web3(HTTPProvider("https://user:password@u0telyzine-u0od4ny83j-rpc.us-east-2.kaleido.io"))

# address from previous deployment
contract_address = Web3.toChecksumAddress("0x4c94e89d5ec3125339906109f143673f40868df2")

greeter = w3.eth.contract(
    address=contract_address,
    abi=contract_interface['abi'],
)

print('Default contract greeting: {}'.format(
    greeter.functions.greet().call()
))

# --- this hangs ---
print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact({ 'from': w3.eth.accounts[0], 'gas': 100000})

w3.eth.waitForTransactionReceipt(tx_hash)

print('Updated contract greeting: {}'.format(
    greeter.functions.greet().call()
))

reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"

However, when I try to submit a transaction which calls setGreeting the transaction hangs. 但是,当我尝试提交调用setGreeting的事务时,事务挂起。 Viewing the Kaleido logs, I see VM in read-only mode. Mutating opcode prohibited 查看Kaleido日志,我看到VM in read-only mode. Mutating opcode prohibited VM in read-only mode. Mutating opcode prohibited . VM in read-only mode. Mutating opcode prohibited Also, when I visit the block explorer for my node, the transactions don't load while the blocks do. 另外,当我访问节点的区块浏览器时,事务不会加载,而区块却会加载。

交易未加载

What can I do about this read only mode? 该只读模式我该怎么办?

moghadasian, Moghadasian,

The "VM in read-only mode" is because you are using call to interact with your Smart Contract method. “处于只读模式的VM”是因为您正在使用call与Smart Contract方法进行交互。 So it's just calling your method in a read-only mode. 因此,它只是以只读模式调用您的方法。 You would use this to call methods on contracts that query data - without having to submit a transaction to the chain. 您可以使用它来调用查询数据合同的方法,而不必向链提交事务。

[edit] - the above advice is generally helpful for "VM in read-only mode", but if you're trying out python web3, you pobably want the other answer with a full working example: https://stackoverflow.com/a/51155413/4972840 [/edit] [编辑]-上述建议通常对“处于只读模式的VM”很有帮助,但是如果您尝试使用python web3,则可能需要其他答案以及完整的工作示例: https ://stackoverflow.com/ a / 51155413/4972840 [/ edit]

Regards, Peter 问候,彼得

moghadasian 莫加达斯人

I could not recreate your "VM in read-only mode" when submitting a transaction - that worked successfully. 提交事务时,我无法重新创建“处于只读模式的VM”-成功运行。 However, I had to do a bit of investigation to get web3/python connecting to Kaleido - so I'm adding a separate answer to help others trying to get going. 但是,我必须做一些调查才能使web3 / python连接到Kaleido-因此,我添加了一个单独的答案来帮助其他尝试入门的人。

Configuring HTTPS authentication to Kaleido from Python web3 从Python Web3配置对Kaleido的HTTPS身份验证

On my Mac, with a default pip3 installation of web3, I found the only way to configure the Python Session with auth was to use a $HOME/.netrc file such as: 在我的Mac上,默认安装了web3的pip3,发现使用auth配置Python会话的唯一方法是使用$HOME/.netrc文件,例如:

machine u0oaXXXXXX-u0c4XXXXXX-rpc.us-east-2.kaleido.io
login u0d0bxXXXX
password jA-pJdIrcRaIx7XXXXXXXXXXXXXXXXXXXXXXXXX

Configure web3 for Geth/PoA 将Web3配置为Geth / PoA

My chain was using Geth/PoA, so I had to follow the instructions here, to install the required middleware: http://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority 我的连锁店使用的是Geth / PoA,因此我必须按照此处的说明安装所需的中间件: http ://web3py.readthedocs.io/en/stable/middleware.html#geth-style-proof-of-authority

Updated example including deployment of contract 更新的示例包括合同的部署

Here is the python3 that successfully deployed and reported Updated contract greeting: Nihao . 这是成功部署并报告Updated contract greeting: Nihao的python3 Updated contract greeting: Nihao You will need to change your HTTPProvider to the HTTPS RPC URL of your node, but without the authentication headers. 您将需要将HTTPProvider更改为节点的HTTPS RPC URL,但没有身份验证标头。

from web3 import Web3
from web3.providers import HTTPProvider
from solc import compile_source
from web3.contract import ConciseContract
from web3.middleware import geth_poa_middleware

# Solidity source code
contract_source_code = '''
pragma solidity ^0.4.0;

contract Greeter {
    string public greeting;

    function Greeter() {
        greeting = 'Hello';
    }

    function setGreeting(string _greeting) public {
        greeting = _greeting;
    }

    function greet() constant returns (string) {
        return greeting;
    }
}
'''

compiled_sol = compile_source(contract_source_code) 
contract_interface = compiled_sol['<stdin>:Greeter']

w3 = Web3(HTTPProvider("https://u0oaXXXXXX-u0c4XXXXXX-rpc.us-east-2.kaleido.io"))
w3.middleware_stack.inject(geth_poa_middleware, layer=0)

Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Greeter.constructor().transact({ 'from': w3.eth.accounts[0], 'gas': 1000000})
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
print('Deployed greeter contract: {}'.format(tx_receipt.contractAddress))

# address from previous deployment
contract_address = Web3.toChecksumAddress(tx_receipt.contractAddress)

greeter = w3.eth.contract(
    address=contract_address,
    abi=contract_interface['abi'],
)

print('Default contract greeting: {}'.format(
    greeter.functions.greet().call()
))

print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact({ 'from': w3.eth.accounts[0], 'gas': 100000})

w3.eth.waitForTransactionReceipt(tx_hash)

print('Updated contract greeting: {}'.format(
    greeter.functions.greet().call()
))

reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只读函数迭代成本 - Read-only Functions Iteration Cost 类型错误:成员“长度”是只读的,不能用于调整数组大小 - TypeError: Member "length" is read-only and cannot be used to resize arrays 在视图/只读区块链 function 上调用 ethers web3js 时出现气体限制错误 - I'm getting a gas limit error when calling ethers web3js on a view/ read-only blockchain function 使用 Kaleido 的以太坊账户私钥 - Private key of Ethereum account using Kaleido 仅读取钱包地址的权限 - Permission to only read Wallet Address 错误:返回错误:处理事务时 VM 异常:还原只有所有者可以调用此 function - Error: Returned error: VM Exception while processing transaction: revert only owner can call this function 返回错误:VM Exception while processing transaction: revert only owner can call this function - Returned error: VM Exception while processing transaction: revert only owner can call this function 处理事务时VM异常:缺少气体 - VM Exception while processing transaction: out of gas 松露错误:错误:处理事务时VM异常:恢复 - Truffle error: Error: VM Exception while processing transaction: revert 在松露控制台中使用OpenZeppelin ERC721 Mint时,“处理事务时发生VM异常:还原” - “VM Exception while processing transaction: revert” when using OpenZeppelin ERC721 mint in truffle console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM