2017-05-30 14 views
1

合同代碼:爲什麼映射的值(bytes32 => uint8)在事務調用之間似乎會改變?

pragma solidity ^0.4.10; 

contract Test { 
    mapping (bytes32 => uint8) private dict; 

    function Test() {} 

    function Set(bytes32 key, uint8 val) returns (uint8) { 
     dict[key] = val; 
     return dict[key]; 
    } 

    function Get(bytes32 key) returns (uint8) { 
     return dict[key]; 
    } 

} 

和我上testrpc運行:

contract_file = 'test/test.sol' 
contract_name = ':Test' 

Solc = require('solc') 
Web3 = require('web3') 

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 
source_code = fs.readFileSync(contract_file).toString() 

compiledContract = Solc.compile(source_code) 
abi = compiledContract.contracts[contract_name].interface 
bytecode = compiledContract.contracts[contract_name].bytecode; 
ContractClass = web3.eth.contract(JSON.parse(abi)) 

contract_init_data = { 
    data: bytecode, 
    from: web3.eth.accounts[0], 
    gas: 1000000, 
} 

deployed_contract = ContractClass.new(contract_init_data) 

deployed_contract.Set.call("akey", 5) 
deployed_contract.Get.call("akey") 

奇怪的是,這是在輸出I在節點終端得到:

> deployed_contract.Set.call("akey", 5) 
{ [String: '5'] s: 1, e: 0, c: [ 5 ] } 
> deployed_contract.Get.call("akey") 
{ [String: '0'] s: 1, e: 0, c: [ 0 ] } 

該結果是本很長的調試會話的結果......發生了什麼事情?這似乎明顯喜歡的東西在這裏打破,但我也跟着a tutorial它做的非常類似的東西,這似乎工作...

也:

> Solc.version() 
'0.4.11+commit.68ef5810.Emscripten.clang' 

回答

2

試試這個deployed_contract.Set("akey", 5)沒有.CALL

因爲。調用您的setter方法

執行消息調用事務,該事務直接在節點的 VM中執行,但從未開採區塊鏈。

doc

地圖不會改變的價值。我敢打賭0是沒有設置任何內容時的默認值

順便嘗試使用online compiler您將很快看到問題出在合同上還是與web3進行交互的方式。

相關問題