Skip to content

Commit e0915dc

Browse files
authored
assembly and ast (#95)
* add test and clean * Update snapshots and re-add operations to AST check
1 parent ae15fb9 commit e0915dc

3 files changed

Lines changed: 163 additions & 0 deletions

File tree

tests/Proxy/Proxy.sol

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
pragma solidity ^0.4.24;
2+
3+
/**
4+
* @title Proxy
5+
* @dev Gives the possibility to delegate any call to a foreign implementation.
6+
*/
7+
contract Proxy {
8+
9+
/**
10+
* @dev Tells the address of the implementation where every call will be delegated.
11+
* @return address of the implementation to which it will be delegated
12+
*/
13+
function _implementation() internal view returns (address);
14+
15+
/**
16+
* @dev Fallback function.
17+
* Implemented entirely in `_fallback`.
18+
*/
19+
function _fallback() internal {
20+
_delegate(_implementation());
21+
}
22+
23+
/**
24+
* @dev Fallback function allowing to perform a delegatecall to the given implementation.
25+
* This function will return whatever the implementation call returns
26+
*/
27+
function _delegate(address implementation) internal {
28+
/*solium-disable-next-line security/no-inline-assembly*/
29+
assembly {
30+
// Copy msg.data. We take full control of memory in this inline assembly
31+
// block because it will not return to Solidity code. We overwrite the
32+
// Solidity scratch pad at memory position 0.
33+
calldatacopy(0, 0, calldatasize)
34+
35+
// Call the implementation.
36+
// out and outsize are 0 because we don't know the size yet.
37+
let result := delegatecall(gas, implementation, 0, calldatasize, 0, 0)
38+
39+
// Copy the returned data.
40+
returndatacopy(0, 0, returndatasize)
41+
42+
switch result
43+
// delegatecall returns 0 on error.
44+
case 0 { revert(0, returndatasize) }
45+
default { return(0, returndatasize) }
46+
}
47+
}
48+
49+
function () public payable {
50+
_fallback();
51+
}
52+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Proxy.sol 1`] = `
4+
pragma solidity ^0.4.24;
5+
6+
/**
7+
* @title Proxy
8+
* @dev Gives the possibility to delegate any call to a foreign implementation.
9+
*/
10+
contract Proxy {
11+
12+
/**
13+
* @dev Tells the address of the implementation where every call will be delegated.
14+
* @return address of the implementation to which it will be delegated
15+
*/
16+
function _implementation() internal view returns (address);
17+
18+
/**
19+
* @dev Fallback function.
20+
* Implemented entirely in \`_fallback\`.
21+
*/
22+
function _fallback() internal {
23+
_delegate(_implementation());
24+
}
25+
26+
/**
27+
* @dev Fallback function allowing to perform a delegatecall to the given implementation.
28+
* This function will return whatever the implementation call returns
29+
*/
30+
function _delegate(address implementation) internal {
31+
/*solium-disable-next-line security/no-inline-assembly*/
32+
assembly {
33+
// Copy msg.data. We take full control of memory in this inline assembly
34+
// block because it will not return to Solidity code. We overwrite the
35+
// Solidity scratch pad at memory position 0.
36+
calldatacopy(0, 0, calldatasize)
37+
38+
// Call the implementation.
39+
// out and outsize are 0 because we don't know the size yet.
40+
let result := delegatecall(gas, implementation, 0, calldatasize, 0, 0)
41+
42+
// Copy the returned data.
43+
returndatacopy(0, 0, returndatasize)
44+
45+
switch result
46+
// delegatecall returns 0 on error.
47+
case 0 { revert(0, returndatasize) }
48+
default { return(0, returndatasize) }
49+
}
50+
}
51+
52+
function () public payable {
53+
_fallback();
54+
}
55+
}
56+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57+
pragma solidity ^0.4.24;
58+
59+
/**
60+
* @title Proxy
61+
* @dev Gives the possibility to delegate any call to a foreign implementation.
62+
*/
63+
contract Proxy {
64+
/**
65+
* @dev Tells the address of the implementation where every call will be delegated.
66+
* @return address of the implementation to which it will be delegated
67+
*/
68+
function _implementation() internal view returns (address);
69+
70+
/**
71+
* @dev Fallback function.
72+
* Implemented entirely in \`_fallback\`.
73+
*/
74+
function _fallback() internal {
75+
_delegate(_implementation());
76+
}
77+
78+
/**
79+
* @dev Fallback function allowing to perform a delegatecall to the given implementation.
80+
* This function will return whatever the implementation call returns
81+
*/
82+
function _delegate(address implementation) internal {
83+
/*solium-disable-next-line security/no-inline-assembly*/
84+
assembly {
85+
// Copy msg.data. We take full control of memory in this inline assembly
86+
// block because it will not return to Solidity code. We overwrite the
87+
// Solidity scratch pad at memory position 0.
88+
calldatacopy(0, 0, calldatasize)
89+
// Call the implementation.
90+
// out and outsize are 0 because we don't know the size yet.
91+
let result := delegatecall(gas, implementation, 0, calldatasize, 0, 0)
92+
// Copy the returned data.
93+
returndatacopy(0, 0, returndatasize)
94+
switch result
95+
// delegatecall returns 0 on error.
96+
case 0 {
97+
revert(0, returndatasize)
98+
}
99+
default {
100+
return(0, returndatasize)
101+
}
102+
}
103+
}
104+
105+
function() public payable {
106+
_fallback();
107+
}
108+
}
109+
110+
`;

tests/Proxy/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname);

0 commit comments

Comments
 (0)