在以太坊专用网络(geth)中,我确实有非常简单的合约(在 Solidity 中)。
版本 1:
contract T {
string log;
function getLastLog() constant returns (string lastLog) { return log; }
function T() { log = "[call end]: T()\n"; }
struct TData {
uint amount;
}
mapping (address => uint) balance;
mapping (address => TData) mystructmap;
function setBalance(address _user, uint _balance) {
log = "[call start]: setBalance()\n";
balance[_user] = _balance;
mystructmap[_user] = TData({amount: 42});
log = "[call end]: setBalance()\n";
}
function getBalance(address _user) constant returns (uint _balance) {
return balance[_user];
}
function get42(address _user) constant returns (uint _fourtytwo) {
return mystructmap[_user].amount;
}
}
我确实部署了合同,然后像这样调用它(来自web3.js):
- 合同.getLog()
- contract.setBalance(valid_address, 55)
- 合同.getLog()
- contract.getBalance(地址)
- contract.get42(地址)
我得到输出结果:
- [通话结束]:T()
- [通话结束]:setBalance()
- 55
- 42
现在我只需在 TData 结构中添加一个新字段:
版本 2:
contract T {
string log;
function getLastLog() constant returns (string lastLog) { return log; }
function T() { log = "[call end]: T()\n"; }
struct TData {
uint somedata;
uint amount;
}
mapping (address => uint) balance;
mapping (address => TData) mystructmap;
function setBalance(address _user, uint _balance) {
log = "[call start]: setBalance()\n";
balance[_user] = _balance;
mystructmap[_user] = TData({somedata: 11, amount: 42});
log = "[call end]: setBalance()\n";
}
function getBalance(address _user) external constant returns (uint _balance) {
return balance[_user];
}
function get42(address _user) external constant returns (uint _fourtytwo) {
return mystructmap[_user].amount;
}
}
我执行与上述相同的调用:
- 合同.getLog()
- contract.setBalance(valid_address, 55)
- 合同.getLog()
- contract.getBalance(地址)
- contract.get42(地址)
但现在我得到:
- [通话结束]:T()
- [通话结束]:T()
- 0
- 0
似乎“ setBalance() ”函数未执行(或在某处退出)并且存储中的状态未更改。
请帮忙!
谢谢。