我已成功将我的合约部署到 Kaleido,但我无法弄清楚如何正确验证它。这是部署合约的源代码,我已经验证了最后一条打印语句打印的地址出现在我的 Kaleido 区块链中:
from web3 import Web3
from web3.providers import HTTPProvider
from solc import compile_source
# 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) # Compiled source code
contract_interface = compiled_sol['<stdin>:Greeter']
w3 = Web3(HTTPProvider("https://XXXXX:[email protected]"))
contract_ = w3.eth.contract(
abi=contract_interface['abi'],
bytecode=contract_interface['bin'])
# note: when interacting with kaleido, gasPrice MUST be 0 (I think because of the consensus algorithm I chose)
# and it seems it doesn't matter what account this is sent from
construct_txn = contract_.constructor().buildTransaction({
'from': w3.eth.accounts[0],
'gas': 1728712,
'gasPrice': 0})
txn = w3.eth.sendTransaction(construct_txn)
tx_receipt = w3.eth.getTransactionReceipt(txn)
contract_address = tx_receipt['contractAddress']
print(contract_address)
当我尝试验证我的合约时,我被要求提供源代码、合约名称、编译器版本以及是否使用了优化。
我将以下内容用于请求的源代码
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;
}
}
我用
Greeter
作为合约名称。 solc --version
返回 Version: 0.4.24+commit.e67f0147.Darwin.appleclang
我发现它是在 5 月 16 日提交的:https://github.com/ethereum/solidity/search?q=e67f0147&type=Commits.
我已经为编译器版本 + 启用优化尝试了以下所有组合:{0.4.24、0.4.24-nightly.2018.5.16} x {启用优化、禁用优化},但这些组合均无效。当我尝试 0.4.24-nightly.2018.5.16 时出现以下错误,因为未启用编译器和优化。:
编译结果与位于 0x4c94e89d5ec3125339906109f143673f40868df2 的输入创建字节码不匹配。
编译失败:[“警告:这是一个预发布的编译器版本,请不要在生产中使用它。 ",":6:5: 警告:不推荐将构造函数定义为与合约同名的函数。请改用“constructor(...) { ... }”。 功能迎宾员(){ ^(相关源代码部分从这里开始并跨越多行)。 ",":6:5: 警告:未指定可见性。默认为“公共”。 功能迎宾员(){ ^(相关源代码部分从这里开始并跨越多行)。 ",":14:5: 警告:未指定可见性。默认为“公共”。 函数 greet() 常量返回(字符串){ ^(相关源代码部分从这里开始并跨越多行)。 “] .
你不应该需要编译器参数来验证合约。只有在极少数情况下,您才需要了解它们。我目前使用 0.4.21 编译器,并且能够仅使用合约 Solidity 源代码来验证我的合约。
我可能会建议在合约编译指示中指定您正在使用的特定编译器,而不是 ^0.4.0.
我在尝试验证时也使用了 Geth PoA 环境,并且必须对您的代码进行一些修改才能使其正常工作。您使用的是 Quorum 环境吗?哪个协议?
这是我从自述文件中的快速入门示例中获取的代码:
import json
import web3
from web3 import Web3, 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.21;
contract Greeter {
string public greeting;
function Greeter() public {
greeting = 'Hello';
}
function setGreeting(string _greeting) public {
greeting = _greeting;
}
function greet() public returns (string) {
return greeting;
}
}
'''
compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:Greeter']
# web3.py instance
w3 = Web3(HTTPProvider("https://YYY:[email protected]"))
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
# set pre-funded account as sender
w3.eth.defaultAccount = w3.eth.accounts[0]
# Instantiate and deploy contract
Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
# Submit the transaction that deploys the contract
tx_hash = Greeter.constructor().transact()
# Wait for the transaction to be mined, and get the transaction receipt
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
print(tx_receipt.contractAddress)
# Create the contract instance with the newly-deployed address
greeter = w3.eth.contract(
address=tx_receipt.contractAddress,
abi=contract_interface['abi'],
)
# Display the default greeting from the contract
print('Default contract greeting: {}'.format(
greeter.functions.greet().call()
))
print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact()
# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)
# Display the new greeting value
print('Updated contract greeting: {}'.format(
greeter.functions.greet().call()
))
# When issuing a lot of reads, try this more concise reader:
reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"
如果你真的需要知道如何找到编译时参数,它们通常作为
kwargs
提供给编译函数。这是可以发挥作用的源代码行:https://github.com/ethereum/py-solc/blob/c595d84d9f0ef5f5da0a5d79e7d5fcabecfe5f06/solc/main.py#L106