Skip to content

Commit

Permalink
Update common-patterns.rst
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Wisniewski authored and chriseth committed May 3, 2017
1 parent 00933b9 commit 2b4b86a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 99 deletions.
22 changes: 9 additions & 13 deletions docs/common-patterns.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ become the new richest.

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract WithdrawalContract {
address public richest;
Expand Down Expand Up @@ -70,7 +70,7 @@ This is as opposed to the more intuitive sending pattern:

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract SendContract {
address public richest;
Expand All @@ -86,9 +86,7 @@ This is as opposed to the more intuitive sending pattern:
// Check if call succeeds to prevent an attacker
// from trapping the previous person's funds in
// this contract through a callstack attack
if (!richest.send(msg.value)) {
throw;
}
richest.transfer(msg.value);
richest = msg.sender;
mostSent = msg.value;
return true;
Expand Down Expand Up @@ -135,7 +133,7 @@ restrictions highly readable.

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract AccessRestriction {
// These will be assigned at the construction
Expand All @@ -152,8 +150,7 @@ restrictions highly readable.
// a certain address.
modifier onlyBy(address _account)
{
if (msg.sender != _account)
throw;
require(msg.sender == _account);
// Do not forget the "_;"! It will
// be replaced by the actual function
// body when the modifier is used.
Expand All @@ -169,7 +166,7 @@ restrictions highly readable.
}

modifier onlyAfter(uint _time) {
if (now < _time) throw;
require(now >= _time);
_;
}

Expand All @@ -190,8 +187,7 @@ restrictions highly readable.
// This was dangerous before Solidity version 0.4.0,
// where it was possible to skip the part after `_;`.
modifier costs(uint _amount) {
if (msg.value < _amount)
throw;
require(msg.value >= _amount);
_;
if (msg.value > _amount)
msg.sender.send(msg.value - _amount);
Expand Down Expand Up @@ -276,7 +272,7 @@ function finishes.

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract StateMachine {
enum Stages {
Expand All @@ -293,7 +289,7 @@ function finishes.
uint public creationTime = now;

modifier atStage(Stages _stage) {
if (stage != _stage) throw;
require(stage == _stage);
_;
}

Expand Down
19 changes: 8 additions & 11 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ inheritable properties of contracts and may be overridden by derived contracts.

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract owned {
function owned() { owner = msg.sender; }
Expand All @@ -341,8 +341,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
// function is executed and otherwise, an exception is
// thrown.
modifier onlyOwner {
if (msg.sender != owner)
throw;
require(msg.sender == owner);
_;
}
}
Expand Down Expand Up @@ -390,7 +389,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
contract Mutex {
bool locked;
modifier noReentrancy() {
if (locked) throw;
require(!locked);
locked = true;
_;
locked = false;
Expand All @@ -401,7 +400,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
/// The `return 7` statement assigns 7 to the return value but still
/// executes the statement `locked = false` in the modifier.
function f() noReentrancy returns (uint) {
if (!msg.sender.call()) throw;
require(msg.sender.call());
return 7;
}
}
Expand Down Expand Up @@ -989,7 +988,7 @@ more advanced example to implement a set).

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

library Set {
// We define a new struct datatype that will be used to
Expand Down Expand Up @@ -1035,8 +1034,7 @@ more advanced example to implement a set).
// The library functions can be called without a
// specific instance of the library, since the
// "instance" will be the current contract.
if (!Set.insert(knownValues, value))
throw;
assert(Set.insert(knownValues, value));
}
// In this contract, we can also directly access knownValues.flags, if we want.
}
Expand Down Expand Up @@ -1166,7 +1164,7 @@ available without having to add further code.
Let us rewrite the set example from the
:ref:`libraries` in this way::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

// This is the same code as before, just without comments
library Set {
Expand Down Expand Up @@ -1207,8 +1205,7 @@ Let us rewrite the set example from the
// corresponding member functions.
// The following function call is identical to
// Set.insert(knownValues, value)
if (!knownValues.insert(value))
throw;
require(knownValues.insert(value));
}
}

Expand Down
3 changes: 1 addition & 2 deletions docs/frequently-asked-questions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,7 @@ What does the following strange check do in the Custom Token contract?

::

if (balanceOf[_to] + _value < balanceOf[_to])
throw;
require(balanceOf[_to] + _value >= balanceOf[_to]);

Integers in Solidity (and most other machine-related programming languages) are restricted to a certain range.
For ``uint256``, this is ``0`` up to ``2**256 - 1``. If the result of some operation on those numbers
Expand Down
11 changes: 5 additions & 6 deletions docs/security-considerations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ outlined further below:

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

contract Fund {
/// Mapping of ether shares of the contract.
Expand All @@ -88,8 +88,7 @@ outlined further below:
function withdraw() {
var share = shares[msg.sender];
shares[msg.sender] = 0;
if (!msg.sender.send(share))
throw;
require(msg.sender.send(share));
}
}

Expand Down Expand Up @@ -162,7 +161,7 @@ Never use tx.origin for authorization. Let's say you have a wallet contract like

::

pragma solidity ^0.4.0;
pragma solidity ^0.4.11;

// THIS CONTRACT CONTAINS A BUG - DO NOT USE
contract TxUserWallet {
Expand All @@ -173,8 +172,8 @@ Never use tx.origin for authorization. Let's say you have a wallet contract like
}

function transfer(address dest, uint amount) {
if (tx.origin != owner) { throw; }
if (!dest.call.value(amount)()) throw;
require(tx.origin == owner);
require(dest.call.value(amount)());
}
}

Expand Down
Loading

0 comments on commit 2b4b86a

Please sign in to comment.