权威证明问题中的 Go-ethereum 私有网络:调用合约方法但没有响应

Posted

技术标签:

【中文标题】权威证明问题中的 Go-ethereum 私有网络:调用合约方法但没有响应【英文标题】:Go-ethereum private network in Proof-of-Authority problem: call contract method but nothing response 【发布时间】:2019-12-24 07:04:36 【问题描述】:

当我创建一个具有 POA 共识的 2 节点私有网络时,当我发送一个简单的交易时它工作正常。 但是当我使用 Truffle 部署一个简单的合约 SimpleStorage.sol 时,我想使用 myetherwallet 调用 get() 方法,但它返回 0 ,而不是 100。

SimpleStorage的详细信息如下:

pragma solidity >=0.4.17;

contract SimpleStorage 
    uint storedData = 100;

    function set(uint x) public 
        storedData = x;
    

    function get() public view returns (uint) 
        return storedData;
    

系统信息
Geth Version: 1.8.23-stable
Git Commit: c942700427557e3ff6de3aaf6b916e2f056c1ec2
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.11.5
Operating System: darwin (MacOs)
GOPATH=
GOROOT=/Users/travis/.gimme/versions/go1.11.5.darwin.amd64

Truffle v5.0.7 (core: 5.0.7)
Solidity v0.5.0 (solc-js)
Node v9.10.0
重现行为 Genesis.json

  "config": 
    "chainId": 1515,
    "homesteadBlock": 1,
    "eip150Block": 2,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 3,
    "eip158Block": 3,
    "byzantiumBlock": 4,
    "clique": 
      "period": 2,
      "epoch": 30000
    
  ,
  "nonce": "0x0",
  "timestamp": "0x5d5769ad",
  "extraData": "0x00000000000000000000000000000000000000000000000000000000000000003b50d35ed4032c984992ad0d757ba65338523919fc0f1c754a2dfa18640ce2a0950aea20d1d206940000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x7FFFFFFFFFFFF",
  "difficulty": "0x1",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": 
    "3b50d35ed4032c984992ad0d757ba65338523919": 
      "balance": "0x200000000000000000000000000000000000000000000000000000000000000"
    ,
    "fc0f1c754a2dfa18640ce2a0950aea20d1d20694": 
      "balance": "0x200000000000000000000000000000000000000000000000000000000000000"
    
  ,
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"

Geth 启动命令
geth --datadir node1/ --syncmode 'full' --port 30312 --rpc --rpcaddr 0.0.0.0 --rpcport 8502 --rpccorsdomain "*" --ws --wsaddr 0.0.0.0 --wsport 8602 --wsorigins "*" --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3 --wsapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3 --bootnodes 'enode://4765bd12afddce5bb5a2ea55e58ffcdbade132a593bddd9f2723e18460b407039bf07e3fa851b12b0b20c8e0b4c2d3518c9578f201b4efe6ab4d9243e28cccaa@127.0.0.1:30310' --networkid 1515 --gasprice '1' -unlock '0x3b50d35ed4032c984992ad0d757ba65338523919' --password node1/password.txt --mine --targetgaslimit 2251799813685247
松露设置

truffle-config.js


module.exports = 
  networks: 
    geth: 
      host: "127.0.0.1",
      port: 8501,
      from: "0xfc0f1c754a2dfa18640ce2a0950aea20d1d20694",
      network_id: "*",
      gasPrice: "0x47B7600",
      gas: "0x47B7600"
    
  ,

  // Set default mocha options here, use special reporters etc.
  mocha: 
    // timeout: 100000
  ,
  // Configure your compilers
  compilers: 
    solc: 
      // version: "0.5.1",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings:           // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: 
      //    enabled: false,
      //    runs: 200
      //  ,
      //  evmVersion: "byzantium"
      // 
    
  

迁移脚本

truffle migrate --network geth --reset
预期行为

当调用get()方法时,它应该返回100

实际行为

它返回了0

我发现还有人和我遇到了类似的问题: One of ethereum solidity methods is not working properly got error Returned values aren't valid, did it run Out of Gas

谁能帮我解决这个问题?谢谢。

【问题讨论】:

【参考方案1】:

我已经弄清楚为什么我无法从变量 storedData 中检索值...

当我将storedData 设置为公开时,它可以正常工作。

(但需要注意的是,如果使用 ganache-cli,即使它不是公共变量,我也可以获得storedData...)

pragma solidity >=0.4.17;

contract SimpleStorage 
    // uint storedData = 100;
    uint public storedData = 100; // set it to be public

    function set(uint x) public 
        storedData = x;
    

    function get() public view returns (uint) 
        return storedData;
    

【讨论】:

以上是关于权威证明问题中的 Go-ethereum 私有网络:调用合约方法但没有响应的主要内容,如果未能解决你的问题,请参考以下文章

区块链,使用 Go-Ethereum 搭建以太坊私有链

使用 Go-Ethereum 1.7.2搭建以太坊私有链

CentOS 7 部署以太坊私有链 go1.13.2 + go-ethereum1.9.12

创建Substrate 私有网络

如何搭建以太坊私有链

如何搭建以太坊私有链