Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from aragon/metro-forwarder
[Byzantium] Implement support for dynamic returns in delegatecall proxies
- Loading branch information
Showing
3 changed files
with
46 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
pragma solidity 0.4.15; | ||
|
||
import "./AppStorage.sol"; | ||
import "../common/DelegateProxy.sol"; | ||
|
||
contract AppProxy is AppStorage { | ||
contract AppProxy is AppStorage, DelegateProxy { | ||
function AppProxy(Kernel _kernel, bytes32 _appId) { | ||
kernel = _kernel; | ||
appId = _appId; | ||
} | ||
|
||
function () payable public { | ||
uint32 len = 320; // 10 return size | ||
address target = kernel.getAppCode(appId); | ||
require(target > 0); // if app code hasn't been set yet, don't call | ||
assembly { | ||
calldatacopy(0x0, 0x0, calldatasize) | ||
let result := delegatecall(sub(gas, 10000), target, 0x0, calldatasize, 0, len) | ||
switch result case 0 { invalid() } | ||
return (0, len) | ||
} | ||
delegatedFwd(target, msg.data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
pragma solidity 0.4.15; | ||
|
||
contract DelegateProxy { | ||
bool constant IS_BYZANTIUM = false; | ||
|
||
// TODO: Remove pre-byzantium logic after tools have new opcodes | ||
|
||
/** | ||
* @dev Performs a delegatecall and returns whatever the delegatecall returned (entire context execution will return!) | ||
* @param _dst Destination address to perform the delegatecall | ||
* @param _calldata Calldata for the delegatecall | ||
*/ | ||
function delegatedFwd(address _dst, bytes _calldata) internal { | ||
uint useByzantiumOpcodes = IS_BYZANTIUM ? 1 : 0; | ||
assembly { | ||
switch extcodesize(_dst) case 0 { revert(0, 0) } | ||
|
||
switch useByzantiumOpcodes | ||
case 0 { | ||
// This code block will be removed | ||
let len := 4096 | ||
let result := delegatecall(sub(gas, 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, len) | ||
switch result case 0 { invalid() } | ||
return (0, len) | ||
} | ||
default { | ||
let result := delegatecall(sub(gas, 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, 0) | ||
let size := returndatasize | ||
|
||
let ptr := mload(0x40) | ||
returndatacopy(ptr, 0, size) | ||
|
||
// revert instead of invalid() bc if the underlying call failed with invalid() it already wasted gas. | ||
// if the call returned error data, forward it | ||
switch result case 0 { revert(ptr, size) } | ||
default { return(ptr, size) } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters