Skip to content

Instantly share code, notes, and snippets.

@pipermerriam pipermerriam/naive.js
Last active Nov 25, 2017

Embed
What would you like to do?
var n_source = eth.compile.solidity("contract naive { mapping (bytes32 => int) intRegister; function registerInt(bytes32 key, int value) external { intRegister[key] = value; } function returnInt(bytes32 key, int value) internal { msg.sender.call(bytes4(sha3(\"registerInt(bytes32,int)\")), key, value); } function makeKey(address callee, bytes32 funcName, bytes32 argsHash) internal returns (bytes32 key) { return sha3(block.blockhash(block.number), callee, funcName, argsHash); } } contract Sub is naive { function add(bytes32 key, int a, int b) external returns (int sum) { sum = a + b; returnInt(key, sum); return sum; } } contract Master is naive { function delegated(address to, int a, int b) external returns (int sum) { bytes32 argsHash = sha3(a, b); bytes32 key = makeKey(to, \"add\", argsHash); to.call(bytes4(sha3(\"add(bytes32,int,int)\")), key, a, b); return intRegister[key]; } }");
var n_master = n_source.Master;
var n_masterContract = eth.contract(n_master.info.abiDefinition);
var m_addr = "0x45b9bb86b94aeff2d99632b43332cee84ff4d3ca";
var m = n_masterContract.at(m_addr);
var n_sub = n_source.Sub;
var n_subContract = eth.contract(n_sub.info.abiDefinition);
var s_addr = "0xa72f859f9c5d6bfcfe3b157049dd234e3fa5674c";
var s = n_subContract.at(s_addr);
/*
* call with following should return 7. Instead it prints "invalid address"
* > m.delegated.sendTransaction(s_addr, 3, 4)
*/
contract naive {
mapping (bytes32 => int) public intRegister;
event IntRegistered(bytes32 key, int value);
event IntReturned(bytes32 key, int value);
event KeyGenerated(bytes32 key);
function registerInt(bytes32 key, int value) external {
IntRegistered(key, value);
intRegister[key] = value;
}
function returnInt(bytes32 key, int value) internal {
IntReturned(key, value);
msg.sender.call(bytes4(sha3("registerInt(bytes32,int)")), key, value);
}
function makeKey(address callee, bytes32 funcName, bytes32 argsHash) internal returns (bytes32 key) {
key = sha3(block.blockhash(block.number), callee, funcName, argsHash);
KeyGenerated(key);
return key;
}
}
contract Sub is naive {
event Added(bytes32 key, int a, int b, int sum);
function add(bytes32 key, int a, int b) external returns (int sum) {
sum = a + b;
returnInt(key, sum);
Added(key, a, b, sum);
return sum;
}
}
contract Master is naive {
event Delegated(address to, int a, int b, int result);
function delegated(address to, int a, int b) external returns (int sum) {
bytes32 argsHash = sha3(a, b);
bytes32 key = makeKey(to, "add", argsHash);
to.call(bytes4(sha3("add(bytes32,int,int)")), key, a, b);
Delegated(to, a, b, intRegister[key]);
return intRegister[key];
}
}
contract Sub {
event Added(int a, int b, int sum);
event IntReturned(address to, int value);
event CallSucceeded(address callee, bytes32 name, bool success);
function add(address from, int a, int b) public {
int sum = a + b;
IntReturned(msg.sender, sum);
bool s = from.call(bytes4(sha3("register(int256)")), sum);
CallSucceeded(from, "register", s);
Added(a, b, sum);
}
}
contract Master {
event CallSucceeded(address callee, bytes32 name, bool success);
int public retVal;
event IntRegistered(address from, int value);
function register(int value) public {
IntRegistered(msg.sender, value);
retVal = value;
}
event Delegated(address to, int a, int b, int result);
function delegated(address to, int a, int b) public {
bytes32 argsHash = sha3(a, b);
bool s = to.call(bytes4(sha3("add(address,int256,int256)")), address(this), a, b);
CallSucceeded(to, "add", s);
Delegated(to, a, b, retVal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.