-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdelegateExample2_withDispatcher.sol
67 lines (56 loc) · 1.71 KB
/
delegateExample2_withDispatcher.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
pragma solidity ^0.4.4;
contract Dispatcher {
mapping(bytes4=>uint32) returnSizes;
int z;
address upgradeContract;
address public dispatcherContract;
function replace(address newUpgradeContract) {
upgradeContract = newUpgradeContract;
upgradeContract.delegatecall(bytes4(sha3("initialize()")));
}
function() {
bytes4 sig;
assembly { sig := calldataload(0) }
var len = returnSizes[sig];
var target = upgradeContract;
assembly {
calldatacopy(mload(0x40), 0x0, calldatasize)
delegatecall(sub(gas, 10000), target, mload(0x40), calldatasize, mload(0x40), len)
return(mload(0x40), len)
}
}
}
contract Upgrade {
mapping(bytes4=>uint32) returnSizes;
int z;
function initialize() {
returnSizes[bytes4(sha3("get()"))] = 32;
}
function plus(int _x, int _y) {
z = _x + _y;
}
function get() returns(int) {
return z;
}
}
contract Main { //
mapping(bytes4=>uint32) public returnSizes;
int public z;
address public upgradeContract;
address public dispatcherContract;
function deployDispatcher() {
dispatcherContract = new Dispatcher();
}
function updateUpgrade(address newUpgradeContract) {
dispatcherContract.delegatecall(bytes4( sha3("replace(address)")), newUpgradeContract);
}
function delegateCall(bytes4 _sig, int _x, int _y) {
dispatcherContract.delegatecall(_sig, _x, _y);
}
function get() constant returns(int output){
dispatcherContract.delegatecall(bytes4( sha3("get()")));
assembly {
output := mload(0x60)
}
}
}