【发布时间】:2018-06-16 11:10:51
【问题描述】:
我刚开始学习 Solidity,并且对我为练习/娱乐而创建的智能合约有一些疑问。 如果我的任何概念不准确,请告诉我,感谢所有建议和建议。
说明:
这个智能合约的概念很简单,谁给合约发送更多的以太币谁就赢了,它会和你之前的人配对(如果没有人在你之前,你就是player_one),它会重置2人玩完后(可以再次玩)
代码:
contract zero_one {
address public player_one;
address public player_two;
uint public player_one_amount;
uint public player_two_amount;
function zero_one() public{
reset();
}
function play() public payable{
//Scenario #1 Already have two player in game, dont accpet new player. do I even need this check at all? since smart contract execute serially i should never face this condition?
if(player_one != address(0) && player_two != address(0)) throw;
//Scenario #2 First player enter the game
else if(player_one == address(0) && player_two == address(0)){
player_one=msg.sender;
player_one_amount = msg.value;
}
//Scenario #3 Second player join in, execute the game
else{
player_two = msg.sender;
player_two_amount = msg.value;
//check the amount send from player_one and player two, whoever has the bigger amount win and get their money
if(player_two_amount>player_one_amount){
player_one.transfer(player_one_amount+player_two_amount);
reset();
}
else if(player_two_amount<player_one_amount){
player_two.transfer(player_one_amount+player_two_amount);
reset();
}
else{
//return fund back to both player
player_one.transfer(player_one_amount);
player_two.transfer(player_two_amount);
reset();
}
}
}
function reset() internal{
player_one = address(0);
player_two = address(0);
player_one_amount = 0;
player_two_amount = 0;
}
}
问题
将以太币发回给用户时,我需要计算多少 气要走?还是智能合约会自动扣除 我要发送的气体量
将 reset() 设置为 interal 是否正确,因为我只希望它是 在智能合约中调用,不应被其他任何人调用
场景#1 会发生吗?因为据我了解 智能合约不必担心竞态条件和它 永远不应该处于那种状态?
- 添加可播放内容是否正确? (因为用户将通过此调用发送以太币)
使用 throw 是一种不好的做法吗? (混音警告)
transfer vs send,为什么使用transfer更好?
我将此智能合约编码为一种实践。我已经可以看到,如果 有人想玩系统,他可以等待某人成为 player_one 并检查 player_one 发送的金额(在区块被开采后),然后发送比这更大的金额。无论如何,这个漏洞可以被阻止吗?还有其他我没有发现的安全/缺陷吗?
谢谢!
【问题讨论】:
标签: ethereum solidity smartcontracts