mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Ensure each code snippet in the docs can be extracted for tests
This commit is contained in:
parent
b7fb1bc0a6
commit
23379e1061
@ -198,7 +198,6 @@ In the following example, ``D``, can call ``c.getData()`` to retrieve the value
|
||||
function compute(uint a, uint b) internal returns (uint) { return a+b; }
|
||||
}
|
||||
|
||||
|
||||
contract D {
|
||||
function readData() {
|
||||
C c = new C();
|
||||
@ -209,7 +208,6 @@ In the following example, ``D``, can call ``c.getData()`` to retrieve the value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract E is C {
|
||||
function g() {
|
||||
C c = new C();
|
||||
@ -238,7 +236,6 @@ be done at declaration.
|
||||
uint public data = 42;
|
||||
}
|
||||
|
||||
|
||||
contract Caller {
|
||||
C c = new C();
|
||||
function f() {
|
||||
@ -321,7 +318,6 @@ inheritable properties of contracts and may be overridden by derived contracts.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract mortal is owned {
|
||||
// This contract inherits the "onlyOwner"-modifier from
|
||||
// "owned" and applies it to the "close"-function, which
|
||||
@ -332,7 +328,6 @@ inheritable properties of contracts and may be overridden by derived contracts.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract priced {
|
||||
// Modifiers can receive arguments:
|
||||
modifier costs(uint price) {
|
||||
@ -342,7 +337,6 @@ inheritable properties of contracts and may be overridden by derived contracts.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract Register is priced, owned {
|
||||
mapping (address => bool) registeredAddresses;
|
||||
uint price;
|
||||
@ -570,7 +564,6 @@ Please ensure you test your fallback function thoroughly to ensure the execution
|
||||
function() payable { }
|
||||
}
|
||||
|
||||
|
||||
contract Caller {
|
||||
function callTest(Test test) {
|
||||
test.call(0xabcdef01); // hash does not exist
|
||||
@ -687,12 +680,19 @@ as topics. The event call above can be performed in the same way as
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.10;
|
||||
|
||||
contract C {
|
||||
function f() {
|
||||
bytes32 _id = 0x420042;
|
||||
log3(
|
||||
msg.value,
|
||||
0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20,
|
||||
msg.sender,
|
||||
bytes32(msg.value),
|
||||
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
|
||||
bytes32(msg.sender),
|
||||
_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
where the long hexadecimal number is equal to
|
||||
``keccak256("Deposit(address,hash256,uint256)")``, the signature of the event.
|
||||
@ -734,7 +734,6 @@ Details are given in the following example.
|
||||
address owner;
|
||||
}
|
||||
|
||||
|
||||
// Use "is" to derive from another contract. Derived
|
||||
// contracts can access all non-private members including
|
||||
// internal functions and state variables. These cannot be
|
||||
@ -745,7 +744,6 @@ Details are given in the following example.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// These abstract contracts are only provided to make the
|
||||
// interface known to the compiler. Note the function
|
||||
// without body. If a contract does not implement all
|
||||
@ -754,13 +752,11 @@ Details are given in the following example.
|
||||
function lookup(uint id) returns (address adr);
|
||||
}
|
||||
|
||||
|
||||
contract NameReg {
|
||||
function register(bytes32 name);
|
||||
function unregister();
|
||||
}
|
||||
|
||||
|
||||
// Multiple inheritance is possible. Note that "owned" is
|
||||
// also a base class of "mortal", yet there is only a single
|
||||
// instance of "owned" (as for virtual inheritance in C++).
|
||||
@ -786,7 +782,6 @@ Details are given in the following example.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If a constructor takes an argument, it needs to be
|
||||
// provided in the header (or modifier-invocation-style at
|
||||
// the constructor of the derived contract (see below)).
|
||||
@ -821,12 +816,10 @@ seen in the following example::
|
||||
function kill() { /* do cleanup 1 */ mortal.kill(); }
|
||||
}
|
||||
|
||||
|
||||
contract Base2 is mortal {
|
||||
function kill() { /* do cleanup 2 */ mortal.kill(); }
|
||||
}
|
||||
|
||||
|
||||
contract Final is Base1, Base2 {
|
||||
}
|
||||
|
||||
@ -848,7 +841,6 @@ derived override, but this function will bypass
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract Base1 is mortal {
|
||||
function kill() { /* do cleanup 1 */ super.kill(); }
|
||||
}
|
||||
@ -858,7 +850,6 @@ derived override, but this function will bypass
|
||||
function kill() { /* do cleanup 2 */ super.kill(); }
|
||||
}
|
||||
|
||||
|
||||
contract Final is Base2, Base1 {
|
||||
}
|
||||
|
||||
@ -888,7 +879,6 @@ the base constructors. This can be done in two ways::
|
||||
function Base(uint _x) { x = _x; }
|
||||
}
|
||||
|
||||
|
||||
contract Derived is Base(7) {
|
||||
function Derived(uint _y) Base(_y * _y) {
|
||||
}
|
||||
@ -1081,7 +1071,6 @@ more advanced example to implement a set).
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
Set.Data knownValues;
|
||||
|
||||
@ -1157,7 +1146,6 @@ custom types without the overhead of external function calls:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
using BigInt for BigInt.bigint;
|
||||
|
||||
@ -1250,7 +1238,6 @@ Let us rewrite the set example from the
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
using Set for Set.Data; // this is the crucial change
|
||||
Set.Data knownValues;
|
||||
@ -1276,7 +1263,6 @@ It is also possible to extend elementary types in that way::
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
using Search for uint[];
|
||||
uint[] data;
|
||||
|
@ -363,6 +363,9 @@ As a result, the following code is illegal and cause the compiler to throw an er
|
||||
In addition to this, if a variable is declared, it will be initialized at the beginning of the function to its default value.
|
||||
As a result, the following code is legal, despite being poorly written::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
function foo() returns (uint) {
|
||||
// baz is implicitly initialized as 0
|
||||
uint bar = 5;
|
||||
@ -373,6 +376,7 @@ As a result, the following code is legal, despite being poorly written::
|
||||
}
|
||||
return bar;// returns 5
|
||||
}
|
||||
}
|
||||
|
||||
.. index:: ! exception, ! throw, ! assert, ! require, ! revert
|
||||
|
||||
|
@ -179,7 +179,9 @@ Never use tx.origin for authorization. Let's say you have a wallet contract like
|
||||
}
|
||||
}
|
||||
|
||||
Now someone tricks you into sending ether to the address of this attack wallet::
|
||||
Now someone tricks you into sending ether to the address of this attack wallet:
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.11;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user