Solidity v0.5.0 重大更新¶
这部分Solidity v0.5.0 版本一写主要的不兼容的更新,了解更新背后的原因,以及影响了那些代码,可以查看 更新日志.
备注
Contracts compiled with Solidity v0.5.0 can still interface with contracts and even libraries compiled with older versions without recompiling or redeploying them. Changing the interfaces to include data locations and visibility and mutability specifiers suffices. See the Interoperability With Older Contracts section below.
语义变化¶
This section lists the changes that are semantic-only, thus potentially hiding new and different behavior in existing code.
Signed right shift now uses proper arithmetic shift, i.e. rounding towards negative infinity, instead of rounding towards zero. Signed and unsigned shift will have dedicated opcodes in Constantinople, and are emulated by Solidity for the moment.
The
continue
statement in ado...while
loop now jumps to the condition, which is the common behavior in such cases. It used to jump to the loop body. Thus, if the condition is false, the loop terminates.The functions
.call()
,.delegatecall()
and.staticcall()
do not pad anymore when given a singlebytes
parameter.Pure and view functions are now called using the opcode
STATICCALL
instead ofCALL
if the EVM version is Byzantium or later. This disallows state changes on the EVM level.The ABI encoder now properly pads byte arrays and strings from calldata (
msg.data
and external function parameters) when used in external function calls and inabi.encode
. For unpadded encoding, useabi.encodePacked
.The ABI decoder reverts in the beginning of functions and in
abi.decode()
if passed calldata is too short or points out of bounds. Note that dirty higher order bits are still simply ignored.Forward all available gas with external function calls starting from Tangerine Whistle.
语义及语法更改¶
This section highlights changes that affect syntax and semantics.
The functions
.call()
,.delegatecall()
,staticcall()
,keccak256()
,sha256()
andripemd160()
now accept only a singlebytes
argument. Moreover, the argument is not padded. This was changed to make more explicit and clear how the arguments are concatenated. Change every.call()
(and family) to a.call("")
and every.call(signature, a, b, c)
to use.call(abi.encodeWithSignature(signature, a, b, c))
(the last one only works for value types). Change everykeccak256(a, b, c)
tokeccak256(abi.encodePacked(a, b, c))
. Even though it is not a breaking change, it is suggested that developers changex.call(bytes4(keccak256("f(uint256)")), a, b)
tox.call(abi.encodeWithSignature("f(uint256)", a, b))
.Functions
.call()
,.delegatecall()
and.staticcall()
now return(bool, bytes memory)
to provide access to the return data. Changebool success = otherContract.call("f")
to(bool success, bytes memory data) = otherContract.call("f")
.Solidity now implements C99-style scoping rules for function local variables, that is, variables can only be used after they have been declared and only in the same or nested scopes. Variables declared in the initialization block of a
for
loop are valid at any point inside the loop.
准确性要求¶
This section lists changes where the code now needs to be more explicit. For most of the topics the compiler will provide suggestions.
Explicit function visibility is now mandatory. Add
public
to every function and constructor, andexternal
to every fallback or interface function that does not specify its visibility already.Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change
uint[] x = z
touint[] storage x = z
, andfunction f(uint[][] x)
tofunction f(uint[][] memory x)
wherememory
is the data location and might be replaced bystorage
orcalldata
accordingly. Note thatexternal
functions require parameters with a data location ofcalldata
.Contract types do not include
address
members anymore in order to separate the namespaces. Therefore, it is now necessary to explicitly convert values of contract type to addresses before using anaddress
member. Example: ifc
is a contract, changec.transfer(...)
toaddress(c).transfer(...)
, andc.balance
toaddress(c).balance
.Explicit conversions between unrelated contract types are now disallowed. You can only convert from a contract type to one of its base or ancestor types. If you are sure that a contract is compatible with the contract type you want to convert to, although it does not inherit from it, you can work around this by converting to
address
first. Example: ifA
andB
are contract types,B
does not inherit fromA
andb
is a contract of typeB
, you can still convertb
to typeA
usingA(address(b))
. Note that you still need to watch out for matching payable fallback functions, as explained below.The
address
type was split intoaddress
andaddress payable
, where onlyaddress payable
provides thetransfer
function. Anaddress payable
can be directly converted to anaddress
, but the other way around is not allowed. Convertingaddress
toaddress payable
is possible via conversion throughuint160
. Ifc
is a contract,address(c)
results inaddress payable
only ifc
has a payable fallback function. If you use the withdraw pattern, you most likely do not have to change your code becausetransfer
is only used onmsg.sender
instead of stored addresses andmsg.sender
is anaddress payable
.Conversions between
bytesX
anduintY
of different size are now disallowed due tobytesX
padding on the right anduintY
padding on the left which may cause unexpected conversion results. The size must now be adjusted within the type before the conversion. For example, you can convert abytes4
(4 bytes) to auint64
(8 bytes) by first converting thebytes4
variable tobytes8
and then touint64
. You get the opposite padding when converting throughuint32
. Before v0.5.0 any conversion betweenbytesX
anduintY
would go throughuint8X
. For exampleuint8(bytes3(0x291807))
would be converted touint8(uint24(bytes3(0x291807)))
(the result is0x07
).Using
msg.value
in non-payable functions (or introducing it via a modifier) is disallowed as a security feature. Turn the function intopayable
or create a new internal function for the program logic that usesmsg.value
.For clarity reasons, the command line interface now requires
-
if the standard input is used as source.
弃用元素¶
This section lists changes that deprecate prior features or syntax. Note that
many of these changes were already enabled in the experimental mode
v0.5.0
.
弃用命令行及 JSON 接口¶
The command line option
--formal
(used to generate Why3 output for further formal verification) was deprecated and is now removed. A new formal verification module, the SMTChecker, is enabled viapragma experimental SMTChecker;
.The command line option
--julia
was renamed to--yul
due to the renaming of the intermediate languageJulia
toYul
.The
--clone-bin
and--combined-json clone-bin
command line options were removed.Remappings with empty prefix are disallowed.
The JSON AST fields
constant
andpayable
were removed. The information is now present in thestateMutability
field.The JSON AST field
isConstructor
of theFunctionDefinition
node was replaced by a field calledkind
which can have the value"constructor"
,"fallback"
or"function"
.In unlinked binary hex files, library address placeholders are now the first 36 hex characters of the keccak256 hash of the fully qualified library name, surrounded by
$...$
. Previously, just the fully qualified library name was used. This reduces the chances of collisions, especially when long paths are used. Binary files now also contain a list of mappings from these placeholders to the fully qualified names.
构造函数变更¶
Constructors must now be defined using the
constructor
keyword.Calling base constructors without parentheses is now disallowed.
Specifying base constructor arguments multiple times in the same inheritance hierarchy is now disallowed.
Calling a constructor with arguments but with wrong argument count is now disallowed. If you only want to specify an inheritance relation without giving arguments, do not provide parentheses at all.
弃用函数¶
Function
callcode
is now disallowed (in favor ofdelegatecall
). It is still possible to use it via inline assembly.suicide
is now disallowed (in favor ofselfdestruct
).sha3
is now disallowed (in favor ofkeccak256
).throw
is now disallowed (in favor ofrevert
,require
andassert
).
弃用类型转换¶
Explicit and implicit conversions from decimal literals to
bytesXX
types is now disallowed.Explicit and implicit conversions from hex literals to
bytesXX
types of different size is now disallowed.
弃用字面量及后缀¶
The unit denomination
years
is now disallowed due to complications and confusions about leap years.Trailing dots that are not followed by a number are now disallowed.
Combining hex numbers with unit denominations (e.g.
0x1e wei
) is now disallowed.The prefix
0X
for hex numbers is disallowed, only0x
is possible.
弃用变量¶
Declaring empty structs is now disallowed for clarity.
The
var
keyword is now disallowed to favor explicitness.Assignments between tuples with different number of components is now disallowed.
Values for constants that are not compile-time constants are disallowed.
Multi-variable declarations with mismatching number of values are now disallowed.
Uninitialized storage variables are now disallowed.
Empty tuple components are now disallowed.
Detecting cyclic dependencies in variables and structs is limited in recursion to 256.
Fixed-size arrays with a length of zero are now disallowed.
弃用语法¶
Using
constant
as function state mutability modifier is now disallowed.Boolean expressions cannot use arithmetic operations.
The unary
+
operator is now disallowed.Literals cannot anymore be used with
abi.encodePacked
without prior conversion to an explicit type.Empty return statements for functions with one or more return values are now disallowed.
The “loose assembly” syntax is now disallowed entirely, that is, jump labels, jumps and non-functional instructions cannot be used anymore. Use the new
while
,switch
andif
constructs instead.Functions without implementation cannot use modifiers anymore.
Function types with named return values are now disallowed.
Single statement variable declarations inside if/while/for bodies that are not blocks are now disallowed.
New keywords:
calldata
andconstructor
.New reserved keywords:
alias
,apply
,auto
,copyof
,define
,immutable
,implements
,macro
,mutable
,override
,partial
,promise
,reference
,sealed
,sizeof
,supports
,typedef
andunchecked
.
和老合约进行交互¶
It is still possible to interface with contracts written for Solidity versions prior to v0.5.0 (or the other way around) by defining interfaces for them. Consider you have the following pre-0.5.0 contract already deployed:
// This will not compile with the current version of the compiler
pragma solidity ^0.4.25;
contract OldContract {
function someOldFunction(uint8 a) {
//...
}
function anotherOldFunction() constant returns (bool) {
//...
}
// ...
}
This will no longer compile with Solidity v0.5.0. However, you can define a compatible interface for it:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
interface OldContract {
function someOldFunction(uint8 a) external;
function anotherOldFunction() external returns (bool);
}
Note that we did not declare anotherOldFunction
to be view
, despite it being declared constant
in the original
contract. This is due to the fact that starting with Solidity v0.5.0 staticcall
is used to call view
functions.
Prior to v0.5.0 the constant
keyword was not enforced, so calling a function declared constant
with staticcall
may still revert, since the constant
function may still attempt to modify storage. Consequently, when defining an
interface for older contracts, you should only use view
in place of constant
in case you are absolutely sure that
the function will work with staticcall
.
Given the interface defined above, you can now easily use the already deployed pre-0.5.0 contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
interface OldContract {
function someOldFunction(uint8 a) external;
function anotherOldFunction() external returns (bool);
}
contract NewContract {
function doSomething(OldContract a) public returns (bool) {
a.someOldFunction(0x42);
return a.anotherOldFunction();
}
}
Similarly, pre-0.5.0 libraries can be used by defining the functions of the library without implementation and supplying the address of the pre-0.5.0 library during linking (see 使用命令行编译器 for how to use the commandline compiler for linking):
// This will not compile after 0.6.0
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.5.0;
library OldLibrary {
function someFunction(uint8 a) public returns(bool);
}
contract NewContract {
function f(uint8 a) public returns (bool) {
return OldLibrary.someFunction(a);
}
}
举例¶
The following example shows a contract and its updated version for Solidity v0.5.0 with some of the changes listed in this section.
Old version:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.4.25;
// This will not compile after 0.5.0
contract OtherContract {
uint x;
function f(uint y) external {
x = y;
}
function() payable external {}
}
contract Old {
OtherContract other;
uint myNumber;
// Function mutability not provided, not an error.
function someInteger() internal returns (uint) { return 2; }
// Function visibility not provided, not an error.
// Function mutability not provided, not an error.
function f(uint x) returns (bytes) {
// Var is fine in this version.
var z = someInteger();
x += z;
// Throw is fine in this version.
if (x > 100)
throw;
bytes memory b = new bytes(x);
y = -3 >> 1;
// y == -1 (wrong, should be -2)
do {
x += 1;
if (x > 10) continue;
// 'Continue' causes an infinite loop.
} while (x < 11);
// Call returns only a Bool.
bool success = address(other).call("f");
if (!success)
revert();
else {
// Local variables could be declared after their use.
int y;
}
return b;
}
// No need for an explicit data location for 'arr'
function g(uint[] arr, bytes8 x, OtherContract otherContract) public {
otherContract.transfer(1 ether);
// Since uint32 (4 bytes) is smaller than bytes8 (8 bytes),
// the first 4 bytes of x will be lost. This might lead to
// unexpected behavior since bytesX are right padded.
uint32 y = uint32(x);
myNumber += y + msg.value;
}
}
New version:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.5.0;
// This will not compile after 0.6.0
contract OtherContract {
uint x;
function f(uint y) external {
x = y;
}
function() payable external {}
}
contract New {
OtherContract other;
uint myNumber;
// Function mutability must be specified.
function someInteger() internal pure returns (uint) { return 2; }
// Function visibility must be specified.
// Function mutability must be specified.
function f(uint x) public returns (bytes memory) {
// The type must now be explicitly given.
uint z = someInteger();
x += z;
// Throw is now disallowed.
require(x <= 100);
int y = -3 >> 1;
require(y == -2);
do {
x += 1;
if (x > 10) continue;
// 'Continue' jumps to the condition below.
} while (x < 11);
// Call returns (bool, bytes).
// Data location must be specified.
(bool success, bytes memory data) = address(other).call("f");
if (!success)
revert();
return data;
}
using AddressMakePayable for address;
// Data location for 'arr' must be specified
function g(uint[] memory /* arr */, bytes8 x, OtherContract otherContract, address unknownContract) public payable {
// 'otherContract.transfer' is not provided.
// Since the code of 'OtherContract' is known and has the fallback
// function, address(otherContract) has type 'address payable'.
address(otherContract).transfer(1 ether);
// 'unknownContract.transfer' is not provided.
// 'address(unknownContract).transfer' is not provided
// since 'address(unknownContract)' is not 'address payable'.
// If the function takes an 'address' which you want to send
// funds to, you can convert it to 'address payable' via 'uint160'.
// Note: This is not recommended and the explicit type
// 'address payable' should be used whenever possible.
// To increase clarity, we suggest the use of a library for
// the conversion (provided after the contract in this example).
address payable addr = unknownContract.makePayable();
require(addr.send(1 ether));
// Since uint32 (4 bytes) is smaller than bytes8 (8 bytes),
// the conversion is not allowed.
// We need to convert to a common size first:
bytes4 x4 = bytes4(x); // Padding happens on the right
uint32 y = uint32(x4); // Conversion is consistent
// 'msg.value' cannot be used in a 'non-payable' function.
// We need to make the function payable
myNumber += y + msg.value;
}
}
// We can define a library for explicitly converting ``address``
// to ``address payable`` as a workaround.
library AddressMakePayable {
function makePayable(address x) internal pure returns (address payable) {
return address(uint160(x));
}
}