Compiling Solidity Code with Geth and Testrpc – Fixing Invalid JSON RPC Response Error

bugcompilationgo-ethereumsoliditytestrpc

I'm attempting to compile the following contract using geth console attached to a testrpc instance:

contract UnitCounter {
    mapping (address => uint256) public UnitsFrom;
    uint256 public TotalUnits;

    function submitUnits(uint256 Units) {
        UnitsFrom[msg.sender] = Units;
        TotalUnits += Units;
    }
}

After running testrpc and connecting:

testrpc -a 1000

I attached a geth cosole

geth attach rpc:http://localhost:8545

and did the following:

> eth.getCompilers()
["solidity"]
> co=eth.compile.solidity("contract UnitCounter {mapping (address => uint256) public UnitsFrom;uint256 public TotalUnits;function SubmitUnits(uint256 Units) {UnitsFrom[msg.sender] = Units;TotalUnits += Units;}}")

Which gives this error:

Error: Invalid JSON RPC response: {"error":{"code":-32603,"json: cannot unmarshal array into Go value of type string":"json: cannot unmarshal array into Go value of type string"},"id":11,"version":"2.0"}
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at <anonymous>:1:4  

What does this error mean?
What steps should I take to debug?

Best Answer

You can use tcpdump to intercept communication between geth and testrpc.

This will dump all packets going through loopback interface lo from/to port 8545:

$ sudo tcpdump -A -i lo port 8545

Now you can run eth.compile.solidity(..) command in geth console and get JSON-RPC request and response:

request

{
  "jsonrpc": "2.0",
  "id": 14,
  "method": "eth_compileSolidity",
  "params": [
    "contract UnitCounter {mapping (address => uint256) public UnitsFrom;uint256 public TotalUnits;function SubmitUnits(uint256 Units) {UnitsFrom[msg.sender] = Units;TotalUnits += Units;}}"
  ]
}

response

{
  "id": 14,
  "jsonrpc": "2.0",
  "error": {
    "message": [
      ":1:1: Warning: Source file does not specify required compiler version!Consider adding \"pragma solidity ^0.4.4\ncontract UnitCounter {mapping (address => uint256) public UnitsFrom;uint256 public TotalUnits;function SubmitUnits(uint256 Units) {UnitsFrom[msg.sender] = Units;TotalUnits += Units;}}\n^-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^\n"
    ],
    "code": -32000
  }
}

Form here you can see that:

  • actual error is missing pragma solidity ^0.4.4; in your code
  • testrpc returns malformed response (error.message must be a string but array is provided, see JSON-RPC spec)
    • this is fixed in provider-engine@8.1.18
Related Topic