Go-Ethereum

無法讓這份契約生效

  • October 29, 2016
pragma solidity ^0.4.0;

contract DataStore {

  struct DataModel
  {
      uint id;
      bytes32 string1;
      bytes32 string2;
      bytes32 string3;
      uint actorId;
      bytes32 string4;
      bytes32 string5;
      bytes32 string6;
      bytes32 string7;
   }

   mapping(bytes32 => DataModel) dataMapping;
   DataModel datamodel;
   address owner;

   function DataStore()
   {
       owner = msg.sender;
   }

   function storeData(bytes32 hash, uint id, bytes32 string1, bytes32    string2, bytes32 string3, uint actorId, bytes32 string4, bytes32 string5, bytes32 string6, bytes32 string7) returns (bool response)
   {
       if(msg.sender != owner)
       {
           return false;
       }else
       {
           datamodel.id = id;
           datamodel.string1 = string1;
           datamodel.string2 = string2;
           datamodel.string3 = string3;
           datamodel.actorId = actorId;
           datamodel.string4 = string4;
           datamodel.string5 = string5;
           datamodel.string6 = string6;
           datamodel.string7 = string7;

           dataMapping[hash] = datamodel;

           return true;
       }
   }

   function queryData(bytes32 hash) returns (uint, bytes32, bytes32, bytes32, uint, bytes32, bytes32, bytes32, bytes32)
   {
       if(msg.sender != owner)
       {
           throw;
       }

       DataModel model = dataMapping[hash];
       return (model.id, model.string1, model.string2, model.string3, model.actorId, model.string4, model.string5, model.string6, model.string7);
   }
}
  1. 我已經部署了合約並收到了地址。
  2. 為了儲存數據,我寫:DataStore.storeData.sendTransaction("hash", 1, "instrument", "ccy", "er", 64, "st", "end", "ts", "tz", {from: eth.accounts[0]}
  3. 我收到:

I1026 18:36:14.659905 ETH / api.go:1185]的Tx(0xcfd97f6c50c4ecca05e8df6939c17b2d9da01107064ccecf943024dabb8998b2)到:0x21f3801411da8b93346bb6e2c216159d1e21ff53“0xcfd97f6c50c4ecca05e8df6939c17b2d9da01107064ccecf943024dabb8998b2”

  1. 我開始使用miner.start()並讓一些塊被探勘。然後我停止使用miner.stop()
  2. 然後我使用:檢查儲存 eth.getStorageAt("0x21f3801411da8b93346bb6e2c216159d1e21ff53"),我得到:

“0x000000000000000000000000000000000000000000000000000000000000000000”

  1. 如果我嘗試:DataStore.queryData("hash"),我得到:

無效地址

以下程式碼有效:

pragma solidity ^0.4.0;

contract DataStore{
   struct DataModel {
       uint id;
       bytes32 string1;
       bytes32 string2;
       bytes32 string3;
       uint actorId;
       bytes32 string4;
       bytes32 string5;
       bytes32 string6;
       bytes32 string7;
   }

   mapping(bytes32 => DataModel) dataMapping;
   address owner;

   function DataStore() {
       owner = msg.sender;
   }

   function storeData(bytes32 hash, uint id, bytes32 string1, bytes32 string2, 
     bytes32 string3, uint actorId, bytes32 string4, bytes32 string5, 
     bytes32 string6, bytes32 string7) returns (bool response) {
       if (msg.sender != owner) {
           return false;
       } else {
           DataModel memory datamodel;
           datamodel.id = id;
           datamodel.string1 = string1;
           datamodel.string2 = string2;
           datamodel.string3 = string3;
           datamodel.actorId = actorId;
           datamodel.string4 = string4;
           datamodel.string5 = string5;
           datamodel.string6 = string6;
           datamodel.string7 = string7;
           dataMapping[hash] = datamodel;
           return true;
       }
   }

   function queryData(bytes32 hash) constant returns (uint, bytes32, bytes32, 
     bytes32, uint, bytes32, bytes32, bytes32, bytes32) {
       if (msg.sender != owner) {
           throw;
       }
       DataModel memory model = dataMapping[hash];
       return (model.id, model.string1, model.string2, model.string3, 
         model.actorId, model.string4, model.string5, model.string6, 
         model.string7);
   }
}

這是 Browser Solidity 螢幕,顯示storeData(...)queryData(...)方法按預期工作:

在此處輸入圖像描述

我所做的更改是:

“記憶體”,用於保存臨時值。它在(外部)函式呼叫之間被刪除並且使用成本更低。

  • queryData(...)中,我聲明了該方法constant,因為它不修改任何數據。來自Solidity - 常見問題

標記為常數的函式與未標記的函式有什麼區別?

常量函式可以執行某些操作並返回一個值,但不能更改狀態**(編譯器尚未強制執行此操作)**。換句話說,常量函式不能保存或更新合約或更廣泛的區塊鏈中的任何變數。這些函式是使用 geth 或任何其他 web3.js 環境中的 c.someFunction(…) 呼叫的。

“非常量”函式(那些缺少常量說明符)必須使用 c.someMethod.sendTransaction({from:eth.accounts

$$ x $$, 氣體: 1000000}); 也就是說,因為他們可以改變狀態,所以他們必須支付 gas 費用才能完成工作。

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