Json-Rpc

code’:-32602,‘message’:‘無效參數 0:十六進製字元串的長度為奇數’

  • June 19, 2018

首先,我用以下來源寫了一份契約:

pragma solidity ^0.4.8;

contract HelloWorld {
   address creator; string greeting;

   function HelloWorld(string _greeting) public {
       creator = msg.sender;
       greeting = _greeting;
   }

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

   function setGreeting(string _newgreeting) {
       greeting = _newgreeting;
   }

   /**********
    Standard kill() function to recover funds
    **********/
   function kill() {
       if (msg.sender == creator)
           suicide(creator);  // kills this contract and sends remaining funds back to creator
   }
}

然後我用線上編譯器https://ethereum.github.io/browser-solidity編譯它。我從 Web3 部署框中得到了內容,即:

var _greeting = /* var of type string here */ ;
var test_sol_helloworldContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"type":"constructor"}]);
var test_sol_helloworld = test_sol_helloworldContract.new(
_greeting,
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000c57fe5b6040516104cf3803806104cf833981016040528080518201919050505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610080929190610088565b505b5061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b6103938061013c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610051578063a413686214610063578063cfae3217146100bd575bfe5b341561005957fe5b610061610156565b005b341561006b57fe5b6100bb600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101ea565b005b34156100c557fe5b6100cd610205565b604051808060200182810382528381815181526020019150805190602001908083836000831461011c575b80518252602083111561011c576020820191506020810190506020830392506100f8565b505050905090810190601f1680156101485780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101e757600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b80600190805190602001906102009291906102ae565b505b50565b61020d61032e565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a35780601f10610278576101008083540402835291602001916102a3565b820191906000526020600020905b81548152906001019060200180831161028657829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102ef57805160ff191683800117855561031d565b8280016001018555821561031d579182015b8281111561031c578251825591602001919060010190610301565b5b50905061032a9190610342565b5090565b602060405190810160405280600081525090565b61036491905b80821115610360576000816000905550600101610348565b5090565b905600a165627a7a723058208e2dda784b2ed789c298ab5b4bb42dc4fef4c7acd2585c6638dc0190f9c5c3e20029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
    console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

然後我將它複製到geth控制台並探勘它。我收到消息:

Contract mined! address: 0x075ebb29cc7c7d80c4de92077febe53a262524e0 transactionHash: 0xa819e8225eeb7faafd4c2dd239052fba4b2434377c125d31c4e5fb8982fc9228

然後我在geth控制台中輸入得到以下結果:

> web3.sha3('greet()').substr(0, 10)
"0xcfae3217" 

然後執行下面的命令(192.168.241.128是我的geth伺服器IP):

curl 192.168.241.128:8545 -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "0x407d73d8a49eeb85d32cf465507dd71d507100c1", "to": "0x075ebb29cc7c7d80c4de92077febe53a262524e0", "data": "0xcfae321700000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id":1}'

但我收到錯誤消息:

{"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"invalid argument 0: hex string has odd length"}}

我怎樣才能讓它工作?

嘗試執行以下行,其中我刪除了一個額外的 0 字元:

curl 192.168.241.128:8545 -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "0x407d73d8a49eeb85d32cf465507dd71d507100c1", "to": "0x075ebb29cc7c7d80c4de92077febe53a262524e0", "data": "0xcfae32170000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id":1}'

您的原始數據字元串是:

0xcfae321700000000000000000000000000000000000000000000000000000000000000000

這可以分解為以下部分(加上一些編號):

0x
cfae3217
00000000000000000000000000000000000000000000000000000000000000000

12345678901234567890123456789012345678901234567890123456789012345

位置 65 中有一個額外的零導致了您的錯誤。該字元串應包含 64 個十六進製字元。

引用自:https://ethereum.stackexchange.com/questions/13927