Created
June 22, 2016 05:00
-
-
Save PeterBorah/110c331dca7d23236f80e69c83a9d58c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract CircuitBreaker { | |
struct Transfer { uint amount; address to; uint releaseBlock; bool released; bool stopped; } | |
Transfer[] public transfers; | |
address public curator; | |
address public authorizedSender; | |
uint public period; | |
uint public limit; | |
uint public currentPeriodEnd; | |
uint public currentPeriodAmount; | |
event PendingTransfer(uint id, uint amount, address to, uint releaseBlock); | |
function CircuitBreaker(address _curator, address _authorizedSender, uint _period, uint _limit) { | |
curator = _curator; | |
period = _period; | |
limit = _limit; | |
authorizedSender = _authorizedSender; | |
currentPeriodEnd = block.number + period; | |
} | |
function transfer(uint amount, address to) { | |
if (msg.sender == authorizedSender) { | |
updatePeriod(); | |
if (currentPeriodAmount + amount > limit) { | |
uint releaseBlock = block.number + period; | |
PendingTransfer(transfers.length, amount, to, releaseBlock); | |
transfers.push(Transfer(amount, to, releaseBlock, false, false)); | |
} else { | |
currentPeriodAmount += amount; | |
transfers.push(Transfer(amount, to, block.number, true, false)); | |
if(!to.send(amount)) throw; | |
} | |
} | |
} | |
function updatePeriod() { | |
if (currentPeriodEnd < block.number) { | |
currentPeriodEnd = block.number + period; | |
currentPeriodAmount = 0; | |
} | |
} | |
function releasePendingTransfer(uint id) { | |
Transfer transfer = transfers[id]; | |
if (transfer.releaseBlock <= block.number && !transfer.released && !transfer.stopped) { | |
transfer.released = true; | |
if(!transfer.to.send(transfer.amount)) throw; | |
} | |
} | |
function stopTransfer(uint id) { | |
if (msg.sender == curator) { | |
transfers[id].stopped = true; | |
} | |
} | |
} |
Why is updatePeriod() necessarily part of transfer()?
Because whether we can or cannot transfer funds depends on the periodical limit. If there's a new period we don't want to use previous period's currentPeriodAmount
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work. Why is
updatePeriod()
necessarily part oftransfer()
?