-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
meta-txs: optimize volatile sender using calldata
- Loading branch information
1 parent
a576413
commit 6837a45
Showing
12 changed files
with
153 additions
and
59 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
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,49 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
library MemoryHelpers { | ||
|
||
function append(bytes memory self, address addr) internal pure returns (bytes memory) { | ||
// alloc required encoded data size | ||
uint256 dataSize = self.length; | ||
uint256 appendedDataSize = dataSize + 32; | ||
bytes memory appendedData = new bytes(appendedDataSize); | ||
|
||
// copy data | ||
uint256 inputPointer; | ||
uint256 outputPointer; | ||
assembly { | ||
inputPointer := add(self, 0x20) | ||
outputPointer := add(appendedData, 0x20) | ||
} | ||
memcpy(outputPointer, inputPointer, dataSize); | ||
|
||
// append address | ||
assembly { | ||
let signerPointer := add(add(appendedData, 0x20), dataSize) | ||
mstore(signerPointer, addr) | ||
} | ||
|
||
return appendedData; | ||
} | ||
|
||
// From https://github.com/Arachnid/solidity-stringutils/blob/master/src/strings.sol | ||
function memcpy(uint256 dest, uint256 src, uint256 len) private pure { | ||
// Copy word-length chunks while possible | ||
for(; len >= 32; len -= 32) { | ||
assembly { | ||
mstore(dest, mload(src)) | ||
} | ||
dest += 32; | ||
src += 32; | ||
} | ||
|
||
// Copy remaining bytes | ||
uint256 mask = 256 ** (32 - len) - 1; | ||
assembly { | ||
let srcpart := and(mload(src), not(mask)) | ||
let destpart := and(mload(dest), mask) | ||
mstore(dest, or(destpart, srcpart)) | ||
} | ||
} | ||
} |
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
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
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
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,6 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
contract IRelayer { | ||
function relay(address from, address to, uint256 nonce, bytes calldata, bytes signature) external; | ||
} |
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,34 +1,25 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
|
||
import "./IRelayer.sol"; | ||
import "../apps/AragonApp.sol"; | ||
|
||
|
||
interface IRelayedAragonApp { | ||
function exec(address from, bytes calldata) external; | ||
} | ||
contract RelayedAragonApp is AragonApp { | ||
|
||
contract RelayedAragonApp is IRelayedAragonApp, AragonApp { | ||
bytes32 public constant RELAYER_ROLE = keccak256("RELAYER_ROLE"); | ||
function sender() internal view returns (address) { | ||
address relayer = address(_relayer()); | ||
if (msg.sender != relayer) return msg.sender; | ||
|
||
function exec(address from, bytes calldata) external auth(RELAYER_ROLE) { | ||
setVolatileStorageSender(from); | ||
bool success = address(this).call(calldata); | ||
if (!success) revertForwardingError(); | ||
setVolatileStorageSender(address(0)); | ||
address signer = _decodeSigner(); | ||
return signer != address(0) ? signer : relayer; | ||
} | ||
|
||
function sender() internal view returns (address) { | ||
if (msg.sender != address(this)) return msg.sender; | ||
address volatileSender = volatileStorageSender(); | ||
return volatileSender != address(0) ? volatileSender : address(this); | ||
function _decodeSigner() internal returns (address signer) { | ||
bytes memory calldata = msg.data; | ||
assembly { signer := mload(add(calldata, calldatasize)) } | ||
} | ||
|
||
function revertForwardingError() private { | ||
assembly { | ||
let ptr := mload(0x40) | ||
returndatacopy(ptr, 0, returndatasize) | ||
revert(ptr, returndatasize) | ||
} | ||
function _relayer() internal returns (IRelayer) { | ||
return kernel().relayer(); | ||
} | ||
} |
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
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
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
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
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