Browser solidity で、下記の様なContractを書きました。
contract answer{
mapping(address => mapping(string => bool)) voters;
struct qList {
uint count; //The number of respondents
mapping(address => mapping(uint => uint)) answer;
}
mapping(string => qList) questionnaires;
function vote(string ID, uint qNum, uint ans) returns (bool) {
if(voters[msg.sender][ID]) throw;
voters[msg.sender][ID] = true;
questionnaires[ID].count += 1;
questionnaires[ID].answer[msg.sender][qNum] = ans;
return true;
}
function getNumResult(string ID) constant returns (uint res) {
return questionnaires[ID].count;
}
}
アンケート(ID)の質問(qNum)に対して回答する(ans)という、シンプルな内容です。
contract自体には特に問題ないのですが、browser solidity上でvoteを行った後に、”Waiting for transaction to be mined...”と表示がずっと出て、マイニングが終わらないという現象が起こりました。
原因は非常に単純で、gasが足りない為に起こった現象でした。
というわけで、CLI上でコンパイルして動かしてみた結果が以下の通り
> var answerSource='contract Answer { mapping(address => mapping(string => bool)) voters; struct qList { uint count; /* The number of respondents */ mapping(address => mapping(uint => uint)) answer; } mapping(string => qList) questionnaires; function vote(string ID, uint qNum, uint ans) returns (bool) { if(voters[msg.sender][ID]) throw; voters[msg.sender][ID] = true; questionnaires[ID].count += 1; questionnaires[ID].answer[msg.sender][qNum] = ans; return true; } function getNumResult(string ID) constant returns (uint res) { return questionnaires[ID].count; }}'
undefined
> var answerCompiled = web3.eth.compile.solidity(answerSource);
undefined
> var answerContract = web3.eth.contract(answerCompiled.Answer.info.abiDefinition);
undefined
> var answer = answerContract.new({
from:web3.eth.accounts[0],
data: answerCompiled.Answer.code, gas: 2000000},
function(e, contract) {
if (!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: "
+ contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
})
すると以下の様に無事minedとなりました。
Contract mined! Address: 0xe51ac93e4c28206f0f0296e5b6d66daf0a917bc3
アンケートidOneの質問1への回答として、1を選択したとしましょう。
> answer.vote("idOne", 1, 1, eth.accounts[0], {
from:web3.eth.accounts[0],
data: answerCompiled.Answer.code,
gas: 1000000
});
"0xfcbf47472733c0922552aa617fc7cb1226c346edc022fbe09ea521dfb75f7699"
しばらくすると、minedとなります。下記の様に、無事voteが機能していることがわかります。
> answer.getNumResult("idOne")
1
と、以上の様に、正しいcontractであるにも関わらず、browser compiler上ではマイニングされない問題が起こりましたら、CLI上でgasを設定すれば無事マイニングされるというお話でした。
browser solidity上でgasの設定ができればいいのですが、やり方を未だ見つけられておらず。見つけたら追記したいと思います。