You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/contracts.rst
+8-35Lines changed: 8 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -679,7 +679,7 @@ Such contracts cannot be compiled (even if they contain implemented functions al
679
679
680
680
If a contract inherits from an abstract contract and does not implement all non-implemented functions by overriding, it will itself be abstract.
681
681
682
-
.. index:: ! library, callcode
682
+
.. index:: ! library, callcode, delegatecall
683
683
684
684
.. _libraries:
685
685
@@ -688,7 +688,8 @@ Libraries
688
688
************
689
689
690
690
Libraries are similar to contracts, but their purpose is that they are deployed
691
-
only once at a specific address and their code is reused using the `CALLCODE`
691
+
only once at a specific address and their code is reused using the `DELEGATECALL`
692
+
(`CALLCODE` until homestead)
692
693
feature of the EVM. This means that if library functions are called, their code
693
694
is executed in the context of the calling contract, i.e. `this` points to the
694
695
calling contract and especially the storage from the calling contract can be
@@ -755,12 +756,12 @@ reference parameters, can have multiple storage reference
755
756
parameters and in any position.
756
757
757
758
The calls to `Set.contains`, `Set.insert` and `Set.remove`
758
-
are all compiled as calls (`CALLCODE`s) to an external
759
+
are all compiled as calls (`DELEGATECALL`s) to an external
759
760
contract/library. If you use libraries, take care that an
760
-
actual external function call is performed, so `msg.sender`
761
-
does not point to the original sender anymore but to the the
762
-
calling contract and also `msg.value` contains the funds
763
-
sent during the call to the library function.
761
+
actual external function call is performed.
762
+
`msg.sender`, `msg.value` and `this` will retain their values
763
+
in this call, though (prior to Homestead, `msg.sender` and
764
+
`msg.value` changed, though).
764
765
765
766
As the compiler cannot know where the library will be
766
767
deployed at, these addresses have to be filled into the
@@ -780,34 +781,6 @@ Restrictions for libraries in comparison to contracts:
780
781
781
782
(these might be lifted at a later point)
782
783
783
-
Common pitfalls for libraries
784
-
=============================
785
-
786
-
.. index:: msg;sender
787
-
788
-
The value of `msg.sender`
789
-
-------------------------
790
-
791
-
The value for `msg.sender` will be that of the contract which is calling the library function.
792
-
793
-
For example, if A calls contract B which internally calls library C, then within the function call of library C, `msg.sender` will be the address of contract B.
794
-
795
-
The reason for this is that the expression `LibraryName.functionName()`
796
-
performs an external function call using `CALLCODE`, which maps to a real EVM
797
-
call just like `otherContract.functionName()` or `this.functionName()`. This
798
-
call extends the call depth by one (limited to 1024), stores the caller (the
799
-
current contract) as `msg.sender`, and then executes the library contract's
800
-
code against the current contracts storage. This execution occurs in a
801
-
completely new memory context meaning that memory types will be copied and
802
-
cannot be passed by reference.
803
-
804
-
Transferring Ether
805
-
-------------------------
806
-
807
-
It is *in principle* possible to transfer ether using
808
-
`LibraryName.functionName.value(x)()`, but as `CALLCODE` is used, the Ether
Copy file name to clipboardExpand all lines: docs/control-structures.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,7 +145,7 @@ Assigning *to* a state variable always creates an independent copy. On the other
145
145
Exceptions
146
146
==========
147
147
148
-
There are some cases where exceptions are thrown automatically (see below). You can use the `throw` instruction to throw an exception manually. The effect of an exception is that the currently executing call is stopped and reverted (i.e. all changes to the state and balances are undone) and the exception is also "bubbled up" through Solidity function calls (exceptions are `send` and the low-level functions `call` and `callcode`, those return `false` in case of an exception).
148
+
There are some cases where exceptions are thrown automatically (see below). You can use the `throw` instruction to throw an exception manually. The effect of an exception is that the currently executing call is stopped and reverted (i.e. all changes to the state and balances are undone) and the exception is also "bubbled up" through Solidity function calls (exceptions are `send` and the low-level functions `call`, `delegatecall` and `callcode`, those return `false` in case of an exception).
@@ -82,7 +82,7 @@ and to send Ether (in units of wei) to an address using the `send` function:
82
82
.. note::
83
83
If `x` is a contract address, its code (more specifically: its fallback function, if present) will be executed together with the `send` call (this is a limitation of the EVM and cannot be prevented). If that execution runs out of gas or fails in any way, the Ether transfer will be reverted. In this case, `send` returns `false`.
84
84
85
-
* `call`and `callcode`
85
+
* `call`, `callcode` and `delegatecall`
86
86
87
87
Furthermore, to interface with contracts that do not adhere to the ABI,
88
88
the function `call` is provided which takes an arbitrary number of arguments of any type. These arguments are padded to 32 bytes and concatenated. One exception is the case where the first argument is encoded to exactly four bytes. In this case, it is not padded to allow the use of function signatures here.
@@ -95,9 +95,9 @@ the function `call` is provided which takes an arbitrary number of arguments of
95
95
96
96
`call` returns a boolean indicating whether the invoked function terminated (`true`) or caused an EVM exception (`false`). It is not possible to access the actual data returned (for this we would need to know the encoding and size in advance).
97
97
98
-
In a similar way, the function `callcode` can be used: The difference is that only the code of the given address is used, all other aspects (storage, balance, ...) are taken from the current contract. The purpose of `callcode` is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is suitable for callcode to be used.
98
+
In a similar way, the function `delegatecall` can be used: The difference is that only the code of the given address is used, all other aspects (storage, balance, ...) are taken from the current contract. The purpose of `delegatecall` is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is suitable for delegatecall to be used. Prior to homestead, only a limited variant called `callcode` was available that did not provide access to the original `msg.sender` and `msg.value` values.
99
99
100
-
Both `call` and `callcode` are very low-level functions and should only be used as a *last resort* as they break the type-safety of Solidity.
100
+
All three functions `call`, `delegatecall` and `callcode` are very low-level functions and should only be used as a *last resort* as they break the type-safety of Solidity.
101
101
102
102
.. note::
103
103
All contracts inherit the members of address, so it is possible to query the balance of the
0 commit comments