Solidity

返回字節646464從一個函式在 TestRPC 中工作而不是在 Geth

  • November 9, 2017

我有一個返回字節的智能合約

$$ 64 $$大批。在 testrpc 中,我得到了正確的數組,但在 Geth 中,我得到了所有“0x”的數組。 這是智能合約程式碼:

pragma solidity ^0.4.18;

contract DataStore
{
   function stringToBytes32(string memory source) returns (bytes32 result) {
       assembly {
           result := mload(add(source, 32))
       }
   }

   function DataStore()
   {
   }

   function str_to_bytes(string str) constant returns (byte[64]){
       bytes memory b = bytes(str);
       byte[64] final_str;
       for(uint i; i<b.length; i++){
           final_str[i] = b[i];
       }

       return final_str;
   }

   function getValue() public returns (byte[64])
   {
       return str_to_bytes("sasdasdasdasdasdasd");
   }
}

getValue()函式的輸出在 Geth 中不正確,但在 TestRPC 中是正確的。

知道為什麼會這樣嗎?

str_to_bytes功能更改byte[64] final_str;byte[64] memory final_str;,它也適用於 Quorum。

我懷疑您的 Geth 節點沒有完全同步,您指向錯誤的地址,或者合約沒有正確部署到 Geth 指向的網路。

我已將契約部署到Rinkeby,當我呼叫getValue函式時,我看到了預期的輸出:

字節1

$$ 64 $$:0x73,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x61,0x73,0x64,0x00,0x73,0x64,0x00,0x73,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

對程式碼的一條評論,我建議您將getValue函式修改為常量或視圖,以防止必須探勘事務才能查看函式的輸出:


function getValue() public view returns (byte[64])
   {
       return str_to_bytes("sasdasdasdasdasdasd");
   }

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