Nethereum

乙太坊結構體大小

  • June 26, 2017

我有這個簡單的契約

pragma solidity ^0.4.10;

contract test {

 int _multiplier;
 struct MyCustomData {
   bool exists;
   string Name1;
 }
 mapping(string => MyCustomData) myData;

 event Multiplied(int indexed a, address indexed sender, int result );

 function test(int multiplier) {
   _multiplier = multiplier;
 }

 function multiply(int a) returns (int r) {

   myData["a"] = MyCustomData(true, "aa1");

   r = a * _multiplier;
   Multiplied(a, msg.sender, r);
   return r;
 }
}

當我部署並執行以下程式碼時,一切看起來都不錯。

public async Task ContractTest()
{

 var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
 var password = "password";

 var abi = @"[{'constant':false,'inputs':[{'name':'a','type':'int256'}],...";

 var byteCode = "0x6060604052341...";

 var multiplier = 7;

 var web3 = new Web3();

 var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(senderAddress, password, new HexBigInteger(120));
 Assert.IsTrue(unlockResult);

 var transactionHash = await web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), multiplier);
 var receipt = await MineAndGetReceiptAsync(web3, transactionHash);

 var contractAddress = receipt.ContractAddress;

 var contract = web3.Eth.GetContract(abi, contractAddress);

 var multiplyFunction = contract.GetFunction("multiply");

 var multiplyEvent = contract.GetEvent("Multiplied");

 var filterAll = await multiplyEvent.CreateFilterAsync();
 var filter7 = await multiplyEvent.CreateFilterAsync(7);

 var gas = await multiplyFunction.EstimateGasAsync(69) ;

 transactionHash = await multiplyFunction.SendTransactionAsync(senderAddress, 7);
 transactionHash = await multiplyFunction.SendTransactionAsync(senderAddress, 8);

 receipt = await MineAndGetReceiptAsync(web3, transactionHash);

 var debuginfo = await web3.DebugGeth.TraceTransaction.SendRequestAsync(transactionHash,
       new TraceTransactionOptions { DisableMemory = true, DisableStorage = true, DisableStack = true });


 var log = await multiplyEvent.GetFilterChanges<MultipliedEvent>(filterAll);
 var log7 = await multiplyEvent.GetFilterChanges<MultipliedEvent>(filter7);

 Assert.AreEqual(2, log.Count);
 Assert.AreEqual(1, log7.Count);
 Assert.AreEqual(7, log7[0].Event.MultiplicationInput);
 Assert.AreEqual(49, log7[0].Event.Result);
}

public async Task<TransactionReceipt> MineAndGetReceiptAsync(Web3 web3, string transactionHash)

{

 var miningResult = await web3.Miner.Start.SendRequestAsync(200);
 Assert.IsTrue(miningResult);

 var receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash);

 while (receipt == null)
 {
   Thread.Sleep(1000);
   receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash);
 }

 miningResult = await web3.Miner.Stop.SendRequestAsync();
 Assert.IsTrue(miningResult);
 return receipt;
}

但是當我嘗試將結構更改為

struct MyCustomData {
   bool exists;
   string Name1;
   string Name2;
   string Name3;
   string Name4;
   string Name5;
}

測試失敗:消息:Assert.AreEqual 失敗。預期:<2>。實際:<0>。

所以看起來方法 multiply 沒有執行。我猜這是gas的問題,但我試圖在交易中添加gas;transactionHash = await multiplyFunction.SendTransactionAsync(senderAddress, new HexBigInteger(200000), new HexBigInteger(120), 7);

但沒有任何改變。

老實說,我現在不知道該怎麼辦。我想我什麼都試過了…… Nethereum 是否已經準備好投入生產?

星期五上午 12:15 任何幫助將不勝感激謝謝

經過幾天的嘗試,我終於找到了問題所在。函式方法multiply的聲明缺少應付定義。

function multiply(int a) payable returns (int r) { ... }

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