yann300
10 years ago
97 changed files with 7484 additions and 1481 deletions
@ -0,0 +1,19 @@ |
|||
cmake_policy(SET CMP0015 NEW) |
|||
set(CMAKE_AUTOMOC OFF) |
|||
|
|||
aux_source_directory(. SRC_LIST) |
|||
|
|||
include_directories(BEFORE ..) |
|||
include_directories(${LEVELDB_INCLUDE_DIRS}) |
|||
|
|||
set(EXECUTABLE ethvm) |
|||
|
|||
add_executable(${EXECUTABLE} ${SRC_LIST}) |
|||
|
|||
target_link_libraries(${EXECUTABLE} ethereum) |
|||
|
|||
if (APPLE) |
|||
install(TARGETS ${EXECUTABLE} DESTINATION bin) |
|||
else() |
|||
eth_install_executable(${EXECUTABLE}) |
|||
endif() |
@ -0,0 +1,200 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file main.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
* EVM Execution tool. |
|||
*/ |
|||
#include <fstream> |
|||
#include <iostream> |
|||
#include <boost/algorithm/string.hpp> |
|||
#include <libdevcore/CommonIO.h> |
|||
#include <libdevcore/RLP.h> |
|||
#include <libdevcore/SHA3.h> |
|||
#include <libethereum/State.h> |
|||
#include <libethereum/Executive.h> |
|||
#include <libevm/VM.h> |
|||
#include <libevm/VMFactory.h> |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace eth; |
|||
|
|||
void help() |
|||
{ |
|||
cout |
|||
<< "Usage ethvm <options> [trace|stats|output] (<file>|--)" << endl |
|||
<< "Transaction options:" << endl |
|||
<< " --value <n> Transaction should transfer the <n> wei (default: 0)." << endl |
|||
<< " --gas <n> Transaction should be given <n> gas (default: block gas limit)." << endl |
|||
<< " --gas-price <n> Transaction's gas price' should be <n> (default: 0)." << endl |
|||
<< " --sender <a> Transaction sender should be <a> (default: 0000...0069)." << endl |
|||
<< " --origin <a> Transaction origin should be <a> (default: 0000...0069)." << endl |
|||
#if ETH_EVMJIT || !ETH_TRUE |
|||
<< endl |
|||
<< "VM options:" << endl |
|||
<< " -J,--jit Enable LLVM VM (default: off)." << endl |
|||
<< " --smart Enable smart VM (default: off)." << endl |
|||
#endif |
|||
<< endl |
|||
<< "Options for trace:" << endl |
|||
<< " --flat Minimal whitespace in the JSON." << endl |
|||
<< " --mnemonics Show instruction mnemonics in the trace (non-standard)." << endl |
|||
<< endl |
|||
<< "General options:" << endl |
|||
<< " -V,--version Show the version and exit." << endl |
|||
<< " -h,--help Show this help message and exit." << endl; |
|||
exit(0); |
|||
} |
|||
|
|||
void version() |
|||
{ |
|||
cout << "ethvm version " << dev::Version << endl; |
|||
cout << "By Gav Wood, 2015." << endl; |
|||
cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; |
|||
exit(0); |
|||
} |
|||
|
|||
enum class Mode |
|||
{ |
|||
Trace, |
|||
Statistics, |
|||
OutputOnly |
|||
}; |
|||
|
|||
int main(int argc, char** argv) |
|||
{ |
|||
string incoming = "--"; |
|||
|
|||
Mode mode = Mode::Statistics; |
|||
State state; |
|||
Address sender = Address(69); |
|||
Address origin = Address(69); |
|||
u256 value = 0; |
|||
u256 gas = state.gasLimitRemaining(); |
|||
u256 gasPrice = 0; |
|||
bool styledJson = true; |
|||
StandardTrace st; |
|||
|
|||
for (int i = 1; i < argc; ++i) |
|||
{ |
|||
string arg = argv[i]; |
|||
if (arg == "-h" || arg == "--help") |
|||
help(); |
|||
else if (arg == "-V" || arg == "--version") |
|||
version(); |
|||
#if ETH_EVMJIT |
|||
else if (arg == "-J" || arg == "--jit") |
|||
VMFactory::setKind(VMKind::JIT); |
|||
else if (arg == "--smart") |
|||
VMFactory::setKind(VMKind::Smart); |
|||
#endif |
|||
else if (arg == "--mnemonics") |
|||
st.setShowMnemonics(); |
|||
else if (arg == "--flat") |
|||
styledJson = false; |
|||
else if (arg == "--value" && i + 1 < argc) |
|||
value = u256(argv[++i]); |
|||
else if (arg == "--sender" && i + 1 < argc) |
|||
sender = Address(argv[++i]); |
|||
else if (arg == "--origin" && i + 1 < argc) |
|||
origin = Address(argv[++i]); |
|||
else if (arg == "--gas" && i + 1 < argc) |
|||
gas = u256(argv[++i]); |
|||
else if (arg == "--gas-price" && i + 1 < argc) |
|||
gasPrice = u256(argv[++i]); |
|||
else if (arg == "--value" && i + 1 < argc) |
|||
value = u256(argv[++i]); |
|||
else if (arg == "--value" && i + 1 < argc) |
|||
value = u256(argv[++i]); |
|||
else if (arg == "stats") |
|||
mode = Mode::Statistics; |
|||
else if (arg == "output") |
|||
mode = Mode::OutputOnly; |
|||
else if (arg == "trace") |
|||
mode = Mode::Trace; |
|||
else |
|||
incoming = arg; |
|||
} |
|||
|
|||
bytes code; |
|||
if (incoming == "--" || incoming.empty()) |
|||
for (int i = cin.get(); i != -1; i = cin.get()) |
|||
code.push_back((char)i); |
|||
else |
|||
code = contents(incoming); |
|||
bytes data = fromHex(boost::trim_copy(asString(code))); |
|||
if (data.empty()) |
|||
data = code; |
|||
|
|||
state.addBalance(sender, value); |
|||
Executive executive(state, eth::LastHashes(), 0); |
|||
ExecutionResult res; |
|||
executive.setResultRecipient(res); |
|||
Transaction t = eth::Transaction(value, gasPrice, gas, data, 0); |
|||
t.forceSender(sender); |
|||
|
|||
unordered_map<byte, pair<unsigned, bigint>> counts; |
|||
unsigned total = 0; |
|||
bigint memTotal; |
|||
auto onOp = [&](uint64_t step, Instruction inst, bigint m, bigint gasCost, bigint gas, VM* vm, ExtVMFace const* extVM) { |
|||
if (mode == Mode::Statistics) |
|||
{ |
|||
counts[(byte)inst].first++; |
|||
counts[(byte)inst].second += gasCost; |
|||
total++; |
|||
if (m > 0) |
|||
memTotal = m; |
|||
} |
|||
else if (mode == Mode::Trace) |
|||
st(step, inst, m, gasCost, gas, vm, extVM); |
|||
}; |
|||
|
|||
executive.initialize(t); |
|||
executive.create(sender, value, gasPrice, gas, &data, origin); |
|||
boost::timer timer; |
|||
executive.go(onOp); |
|||
double execTime = timer.elapsed(); |
|||
executive.finalize(); |
|||
bytes output = std::move(res.output); |
|||
|
|||
if (mode == Mode::Statistics) |
|||
{ |
|||
cout << "Gas used: " << res.gasUsed << " (+" << t.gasRequired() << " for transaction, -" << res.gasRefunded << " refunded)" << endl; |
|||
cout << "Output: " << toHex(output) << endl; |
|||
LogEntries logs = executive.logs(); |
|||
cout << logs.size() << " logs" << (logs.empty() ? "." : ":") << endl; |
|||
for (LogEntry const& l: logs) |
|||
{ |
|||
cout << " " << l.address.hex() << ": " << toHex(t.data()) << endl; |
|||
for (h256 const& t: l.topics) |
|||
cout << " " << t.hex() << endl; |
|||
} |
|||
|
|||
cout << total << " operations in " << execTime << " seconds." << endl; |
|||
cout << "Maximum memory usage: " << memTotal * 32 << " bytes" << endl; |
|||
cout << "Expensive operations:" << endl; |
|||
for (auto const& c: {Instruction::SSTORE, Instruction::SLOAD, Instruction::CALL, Instruction::CREATE, Instruction::CALLCODE, Instruction::MSTORE8, Instruction::MSTORE, Instruction::MLOAD, Instruction::SHA3}) |
|||
if (!!counts[(byte)c].first) |
|||
cout << " " << instructionInfo(c).name << " x " << counts[(byte)c].first << " (" << counts[(byte)c].second << " gas)" << endl; |
|||
} |
|||
else if (mode == Mode::Trace) |
|||
cout << st.json(styledJson); |
|||
else if (mode == Mode::OutputOnly) |
|||
cout << toHex(output); |
|||
|
|||
return 0; |
|||
} |
@ -1,4 +1,3 @@ |
|||
3stack:bignumber@2.0.0 |
|||
ethereum:js@0.0.15-rc12 |
|||
meteor@1.1.4 |
|||
underscore@1.0.2 |
|||
ethereum:web3@0.5.0 |
|||
meteor@1.1.6 |
|||
underscore@1.0.3 |
|||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,203 @@ |
|||
<!doctype> |
|||
<html> |
|||
|
|||
<head> |
|||
<script type="text/javascript" src="../dist/web3.js"></script> |
|||
<script type="text/javascript"> |
|||
|
|||
var web3 = require('web3'); |
|||
var BigNumber = require('bignumber.js'); |
|||
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545")); |
|||
var from = web3.eth.coinbase; |
|||
web3.eth.defaultAccount = from; |
|||
|
|||
var nameregAbi = [ |
|||
{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[],"name":"Registrar","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"}, |
|||
{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"}],"name":"PrimaryChanged","type":"event"} |
|||
]; |
|||
|
|||
var depositAbi = [{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"deposit","outputs":[],"type":"function"}]; |
|||
|
|||
var Namereg = web3.eth.contract(nameregAbi); |
|||
var Deposit = web3.eth.contract(depositAbi); |
|||
|
|||
var namereg = web3.eth.namereg; |
|||
var deposit; |
|||
var iban; |
|||
|
|||
function validateNamereg() { |
|||
var address = document.getElementById('namereg').value; |
|||
var ok = /^(0x)?[0-9a-f]{40}$/.test(address) || address === 'default'; |
|||
if (ok) { |
|||
namereg = address === 'default' ? web3.eth.namereg : Namereg.at(address); |
|||
document.getElementById('nameregValidation').innerText = 'ok!'; |
|||
} else { |
|||
document.getElementById('nameregValidation').innerText = 'namereg address is incorrect!'; |
|||
} |
|||
return ok; |
|||
}; |
|||
|
|||
function onNameregKeyUp() { |
|||
updateIBAN(validateNamereg()); |
|||
onExchangeKeyUp(); |
|||
}; |
|||
|
|||
function validateExchange() { |
|||
var exchange = document.getElementById('exchange').value; |
|||
var ok = /^[0-9A-Z]{4}$/.test(exchange); |
|||
if (ok) { |
|||
var address = namereg.addr(exchange); |
|||
deposit = Deposit.at(address); |
|||
document.getElementById('exchangeValidation').innerText = 'ok! address of exchange: ' + address; |
|||
} else { |
|||
document.getElementById('exchangeValidation').innerText = 'exchange id is incorrect'; |
|||
} |
|||
return ok; |
|||
}; |
|||
|
|||
function onExchangeKeyUp() { |
|||
updateIBAN(validateExchange()); |
|||
}; |
|||
|
|||
function validateClient() { |
|||
var client = document.getElementById('client').value; |
|||
var ok = /^[0-9A-Z]{9}$/.test(client); |
|||
if (ok) { |
|||
document.getElementById('clientValidation').innerText = 'ok!'; |
|||
} else { |
|||
document.getElementById('clientValidation').innerText = 'client id is incorrect'; |
|||
} |
|||
return ok; |
|||
}; |
|||
|
|||
function onClientKeyUp() { |
|||
updateIBAN(validateClient()); |
|||
}; |
|||
|
|||
function validateValue() { |
|||
try { |
|||
var value = document.getElementById('value').value; |
|||
var bnValue = new BigNumber(value); |
|||
document.getElementById('valueValidation').innerText = bnValue.toString(10); |
|||
return true; |
|||
} catch (err) { |
|||
document.getElementById('valueValidation').innerText = 'Value is incorrect, cannot parse'; |
|||
return false; |
|||
} |
|||
}; |
|||
|
|||
function onValueKeyUp() { |
|||
validateValue(); |
|||
}; |
|||
|
|||
function validateIBAN() { |
|||
if (!web3.isIBAN(iban)) { |
|||
return document.getElementById('ibanValidation').innerText = ' - IBAN number is incorrect'; |
|||
} |
|||
document.getElementById('ibanValidation').innerText = ' - IBAN number correct'; |
|||
}; |
|||
|
|||
function updateIBAN(ok) { |
|||
var exchangeId = document.getElementById('exchange').value; |
|||
var clientId = document.getElementById('client').value; |
|||
iban = 'XE' + '00' + 'ETH' + exchangeId + clientId; |
|||
document.getElementById('iban').innerText = iban; |
|||
validateIBAN(); |
|||
}; |
|||
|
|||
function transfer() { |
|||
var value = new BigNumber(document.getElementById('value').value); |
|||
var exchange = document.getElementById('exchange').value; |
|||
var client = document.getElementById('client').value; |
|||
deposit.deposit(client, {value: value}); |
|||
displayTransfer("deposited client's " + client + " funds " + value.toString(10) + " to exchange " + exchange); |
|||
}; |
|||
|
|||
function displayTransfer(text) { |
|||
var node = document.createElement('li'); |
|||
var textnode = document.createTextNode(text); |
|||
node.appendChild(textnode); |
|||
document.getElementById('transfers').appendChild(node); |
|||
} |
|||
|
|||
</script> |
|||
</head> |
|||
<body> |
|||
<h1>ICAP transfer</h1> |
|||
<div> |
|||
<h4>namereg address</h4> |
|||
</div> |
|||
<div> |
|||
<text>eg. 0x436474facc88948696b371052a1befb801f003ca or 'default')</text> |
|||
</div> |
|||
<div> |
|||
<input type="text" id="namereg" onkeyup='onNameregKeyUp()' value="default"></input> |
|||
<text id="nameregValidation"></text> |
|||
</div> |
|||
|
|||
<div> |
|||
<h4>exchange identifier</h4> |
|||
</div> |
|||
<div> |
|||
<text>eg. WYWY</text> |
|||
</div> |
|||
<div> |
|||
<input type="text" id="exchange" onkeyup='onExchangeKeyUp()'></input> |
|||
<text id="exchangeValidation"></text> |
|||
</div> |
|||
|
|||
<div> |
|||
<h4>client identifier</h4> |
|||
</div> |
|||
<div> |
|||
<text>eg. GAVOFYORK</text> |
|||
</div> |
|||
<div> |
|||
<input type="text" id="client" onkeyup='onClientKeyUp()'></input> |
|||
<text id="clientValidation"></text> |
|||
</div> |
|||
|
|||
<div> |
|||
<h4>value</h4> |
|||
</div> |
|||
<div> |
|||
<text>eg. 100</text> |
|||
</div> |
|||
<div> |
|||
<input type="text" id="value" onkeyup='onValueKeyUp()'></input> |
|||
<text id="valueValidation"></text> |
|||
</div> |
|||
|
|||
<div> </div> |
|||
<div> |
|||
<text>IBAN: </text> |
|||
<text id="iban"></text> |
|||
<text id="ibanValidation"></text> |
|||
</div> |
|||
<div> </div> |
|||
<div> |
|||
<button id="transfer" type="button" onClick="transfer()">Transfer!</button> |
|||
<text id="transferValidation"></text> |
|||
</div> |
|||
|
|||
<div> |
|||
<h4>transfers</h4> |
|||
</div> |
|||
<div> |
|||
<ul id='transfers'></ul> |
|||
</div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,102 @@ |
|||
<!doctype> |
|||
<html> |
|||
|
|||
<head> |
|||
<script type="text/javascript" src="../dist/web3.js"></script> |
|||
<script type="text/javascript"> |
|||
|
|||
var web3 = require('web3'); |
|||
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545")); |
|||
var from = web3.eth.coinbase; |
|||
web3.eth.defaultAccount = from; |
|||
|
|||
window.onload = function () { |
|||
var filter = web3.eth.namereg.Changed(); |
|||
filter.watch(function (err, event) { |
|||
// live update all fields |
|||
onAddressKeyUp(); |
|||
onNameKeyUp(); |
|||
onRegisterOwnerKeyUp(); |
|||
}); |
|||
}; |
|||
|
|||
function registerOwner() { |
|||
var name = document.getElementById('registerOwner').value; |
|||
web3.eth.namereg.reserve(name); |
|||
document.getElementById('nameAvailability').innerText += ' Registering name in progress, please wait...'; |
|||
}; |
|||
|
|||
function changeAddress() { |
|||
var name = document.getElementById('registerOwner').value; |
|||
var address = document.getElementById('newAddress').value; |
|||
web3.eth.namereg.setAddress(name, address, true); |
|||
document.getElementById('currentAddress').innerText += ' Changing address in progress. Please wait.'; |
|||
}; |
|||
|
|||
function onRegisterOwnerKeyUp() { |
|||
var name = document.getElementById('registerOwner').value; |
|||
var owner = web3.eth.namereg.owner(name) |
|||
document.getElementById('currentAddress').innerText = web3.eth.namereg.addr(name); |
|||
if (owner !== '0x0000000000000000000000000000000000000000') { |
|||
if (owner === from) { |
|||
document.getElementById('nameAvailability').innerText = "This name is already owned by you " + owner; |
|||
} else { |
|||
document.getElementById('nameAvailability').innerText = "This name is not available. It's already registered by " + owner; |
|||
} |
|||
return; |
|||
} |
|||
document.getElementById('nameAvailability').innerText = "This name is available. You can register it."; |
|||
}; |
|||
|
|||
function onAddressKeyUp() { |
|||
var address = document.getElementById('address').value; |
|||
document.getElementById('nameOf').innerText = web3.eth.namereg.name(address); |
|||
}; |
|||
|
|||
function onNameKeyUp() { |
|||
var name = document.getElementById('name').value; |
|||
document.getElementById('addressOf').innerText = web3.eth.namereg.addr(name); |
|||
}; |
|||
|
|||
</script> |
|||
</head> |
|||
<body> |
|||
<i>This example shows only part of namereg functionalities. Namereg contract is available <a href="https://github.com/ethereum/dapp-bin/blob/master/GlobalRegistrar/contract.sol">here</a> |
|||
</i> |
|||
<h1>Namereg</h1> |
|||
<h3>Search for name</h3> |
|||
<div> |
|||
<text>Address: </text> |
|||
<input type="text" id="address" onkeyup='onAddressKeyUp()'></input> |
|||
<text>Name: </text> |
|||
<text id="nameOf"></text> |
|||
</div> |
|||
<h3>Search for address</h3> |
|||
<div> |
|||
<text>Name: </text> |
|||
<input type="text" id="name" onkeyup='onNameKeyUp()'></input> |
|||
<text>Address: </text> |
|||
<text id="addressOf"></text> |
|||
</div> |
|||
<h3>Register name</h3> |
|||
<div> |
|||
<text>Check if name is available: </text> |
|||
<input type="text" id="registerOwner" onkeyup='onRegisterOwnerKeyUp()'></input> |
|||
<text id='nameAvailability'></text> |
|||
</div> |
|||
<div> |
|||
<button id="registerOwnerButton" type="button" onClick="registerOwner()">Register!</button> |
|||
</div> |
|||
<h3></h3> |
|||
<i>If you own the name, you can also change the address it points to</i> |
|||
<div> |
|||
<text>Address: </text> |
|||
<input type="text" id="newAddress"></input> |
|||
<button id="changeAddress" type="button" onClick="changeAddress()">Change address!</button> |
|||
<text>Current address :</text> |
|||
<text id="currentAddress"></text> |
|||
</div> |
|||
|
|||
</body> |
|||
</html> |
|||
|
@ -1,76 +0,0 @@ |
|||
<!doctype> |
|||
<html> |
|||
|
|||
<head> |
|||
<script type="text/javascript" src="../dist/web3.js"></script> |
|||
<script type="text/javascript"> |
|||
|
|||
var web3 = require('web3'); |
|||
web3.setProvider(new web3.providers.HttpProvider()); |
|||
|
|||
// solidity source code |
|||
var source = "" + |
|||
"contract test {\n" + |
|||
" /// @notice Will multiply `a` by 7. \n" + |
|||
" function multiply(uint a) returns(uint d) {\n" + |
|||
" return a * 7;\n" + |
|||
" }\n" + |
|||
"}\n"; |
|||
|
|||
// contract description, this will be autogenerated somehow |
|||
var desc = [{ |
|||
"name": "multiply(uint256)", |
|||
"type": "function", |
|||
"inputs": [ |
|||
{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
var contract; |
|||
|
|||
function createExampleContract() { |
|||
// hide create button |
|||
document.getElementById('create').style.visibility = 'hidden'; |
|||
document.getElementById('source').innerText = source; |
|||
|
|||
// create contract |
|||
var address = web3.eth.sendTransaction({code: web3.eth.solidity(source)}); |
|||
contract = web3.eth.contract(address, desc); |
|||
document.getElementById('call').style.visibility = 'visible'; |
|||
} |
|||
|
|||
function callExampleContract() { |
|||
// this should be generated by ethereum |
|||
var param = parseInt(document.getElementById('value').value); |
|||
|
|||
// transaction does not return any result, cause it's not synchronous and we don't know, |
|||
// when it will be processed |
|||
contract.sendTransaction().multiply(param); |
|||
document.getElementById('result').innerText = 'transaction made'; |
|||
} |
|||
|
|||
</script> |
|||
</head> |
|||
<body> |
|||
<h1>contract</h1> |
|||
<div id="source"></div> |
|||
<div id='create'> |
|||
<button type="button" onClick="createExampleContract();">create example contract</button> |
|||
</div> |
|||
<div id='call' style='visibility: hidden;'> |
|||
<input type="number" id="value"></input> |
|||
<button type="button" onClick="callExampleContract()">Call Contract</button> |
|||
</div> |
|||
<div id="result"></div> |
|||
</body> |
|||
</html> |
|||
|
@ -0,0 +1,39 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** |
|||
* @file sha3.js |
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
var utils = require('./utils'); |
|||
var sha3 = require('crypto-js/sha3'); |
|||
|
|||
module.exports = function (str, isNew) { |
|||
if (str.substr(0, 2) === '0x' && !isNew) { |
|||
console.warn('requirement of using web3.fromAscii before sha3 is deprecated'); |
|||
console.warn('new usage: \'web3.sha3("hello")\''); |
|||
console.warn('see https://github.com/ethereum/web3.js/pull/205'); |
|||
console.warn('if you need to hash hex value, you can do \'sha3("0xfff", true)\''); |
|||
str = utils.toAscii(str); |
|||
} |
|||
|
|||
return sha3(str, { |
|||
outputLength: 256 |
|||
}).toString(); |
|||
}; |
|||
|
@ -1,3 +1,3 @@ |
|||
{ |
|||
"version": "0.4.2" |
|||
"version": "0.5.0" |
|||
} |
|||
|
@ -0,0 +1,108 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** |
|||
* @file icap.js |
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
var utils = require('../utils/utils'); |
|||
|
|||
/** |
|||
* This prototype should be used to extract necessary information from iban address |
|||
* |
|||
* @param {String} iban |
|||
*/ |
|||
var ICAP = function (iban) { |
|||
this._iban = iban; |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to check if icap is correct |
|||
* |
|||
* @method isValid |
|||
* @returns {Boolean} true if it is, otherwise false |
|||
*/ |
|||
ICAP.prototype.isValid = function () { |
|||
return utils.isIBAN(this._iban); |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to check if iban number is direct |
|||
* |
|||
* @method isDirect |
|||
* @returns {Boolean} true if it is, otherwise false |
|||
*/ |
|||
ICAP.prototype.isDirect = function () { |
|||
return this._iban.length === 34; |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to check if iban number if indirect |
|||
* |
|||
* @method isIndirect |
|||
* @returns {Boolean} true if it is, otherwise false |
|||
*/ |
|||
ICAP.prototype.isIndirect = function () { |
|||
return this._iban.length === 20; |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to get iban checksum |
|||
* Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003) |
|||
* |
|||
* @method checksum |
|||
* @returns {String} checksum |
|||
*/ |
|||
ICAP.prototype.checksum = function () { |
|||
return this._iban.substr(2, 2); |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to get institution identifier |
|||
* eg. XREG |
|||
* |
|||
* @method institution |
|||
* @returns {String} institution identifier |
|||
*/ |
|||
ICAP.prototype.institution = function () { |
|||
return this.isIndirect() ? this._iban.substr(7, 4) : ''; |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to get client identifier within institution |
|||
* eg. GAVOFYORK |
|||
* |
|||
* @method client |
|||
* @returns {String} client identifier |
|||
*/ |
|||
ICAP.prototype.client = function () { |
|||
return this.isIndirect() ? this._iban.substr(11) : ''; |
|||
}; |
|||
|
|||
/** |
|||
* Should be called to get client direct address |
|||
* |
|||
* @method address |
|||
* @returns {String} client direct address |
|||
*/ |
|||
ICAP.prototype.address = function () { |
|||
return this.isDirect() ? this._iban.substr(4) : ''; |
|||
}; |
|||
|
|||
module.exports = ICAP; |
|||
|
@ -0,0 +1,46 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** |
|||
* @file namereg.js |
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
var contract = require('./contract'); |
|||
|
|||
var address = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; |
|||
|
|||
var abi = [ |
|||
{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[],"name":"Registrar","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"type":"function"}, |
|||
{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"type":"function"}, |
|||
{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"type":"function"}, |
|||
{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"}, |
|||
{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"}],"name":"PrimaryChanged","type":"event"} |
|||
]; |
|||
|
|||
module.exports = contract(abi).at(address); |
|||
|
@ -0,0 +1,94 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** |
|||
* @file transfer.js |
|||
* @author Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
var web3 = require('../web3'); |
|||
var ICAP = require('./icap'); |
|||
var namereg = require('./namereg'); |
|||
var contract = require('./contract'); |
|||
|
|||
/** |
|||
* Should be used to make ICAP transfer |
|||
* |
|||
* @method transfer |
|||
* @param {String} iban number |
|||
* @param {String} from (address) |
|||
* @param {Value} value to be tranfered |
|||
* @param {Function} callback, callback |
|||
*/ |
|||
var transfer = function (from, iban, value, callback) { |
|||
var icap = new ICAP(iban); |
|||
if (!icap.isValid()) { |
|||
throw new Error('invalid iban address'); |
|||
} |
|||
|
|||
if (icap.isDirect()) { |
|||
return transferToAddress(from, icap.address(), value, callback); |
|||
} |
|||
|
|||
if (!callback) { |
|||
var address = namereg.addr(icap.institution()); |
|||
return deposit(from, address, value, icap.client()); |
|||
} |
|||
|
|||
namereg.addr(icap.insitution(), function (err, address) { |
|||
return deposit(from, address, value, icap.client(), callback); |
|||
}); |
|||
|
|||
}; |
|||
|
|||
/** |
|||
* Should be used to transfer funds to certain address |
|||
* |
|||
* @method transferToAddress |
|||
* @param {String} address |
|||
* @param {String} from (address) |
|||
* @param {Value} value to be tranfered |
|||
* @param {Function} callback, callback |
|||
*/ |
|||
var transferToAddress = function (from, address, value, callback) { |
|||
return web3.eth.sendTransaction({ |
|||
address: address, |
|||
from: from, |
|||
value: value |
|||
}, callback); |
|||
}; |
|||
|
|||
/** |
|||
* Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!) |
|||
* |
|||
* @method deposit |
|||
* @param {String} address |
|||
* @param {String} from (address) |
|||
* @param {Value} value to be tranfered |
|||
* @param {String} client unique identifier |
|||
* @param {Function} callback, callback |
|||
*/ |
|||
var deposit = function (from, address, value, client, callback) { |
|||
var abi = [{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"deposit","outputs":[],"type":"function"}]; |
|||
return contract(abi).at(address).deposit(client, { |
|||
from: from, |
|||
value: value |
|||
}, callback); |
|||
}; |
|||
|
|||
module.exports = transfer; |
|||
|
@ -0,0 +1,17 @@ |
|||
var chai = require('chai'); |
|||
var assert = chai.assert; |
|||
var sha3 = require('../lib/utils/sha3'); |
|||
var web3 = require('../index'); |
|||
|
|||
describe('lib/utils/sha3', function () { |
|||
var test = function (v, e) { |
|||
it('should encode ' + v + ' to ' + e, function () { |
|||
assert.equal(sha3(v), e); |
|||
}); |
|||
}; |
|||
|
|||
test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); |
|||
test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); |
|||
test(web3.fromAscii('test123'), 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); |
|||
}); |
|||
|
@ -0,0 +1,32 @@ |
|||
var chai = require('chai'); |
|||
var utils = require('../lib/utils/utils.js'); |
|||
var assert = chai.assert; |
|||
|
|||
var tests = [ |
|||
{ obj: function () {}, is: false}, |
|||
{ obj: new Function(), is: false}, |
|||
{ obj: 'function', is: false}, |
|||
{ obj: {}, is: false}, |
|||
{ obj: '[]', is: false}, |
|||
{ obj: '[1, 2]', is: false}, |
|||
{ obj: '{}', is: false}, |
|||
{ obj: '{"a": 123, "b" :3,}', is: false}, |
|||
{ obj: '{"c" : 2}', is: false}, |
|||
{ obj: 'XE81ETHXREGGAVOFYORK', is: true}, |
|||
{ obj: 'XE81ETCXREGGAVOFYORK', is: false}, |
|||
{ obj: 'XE81ETHXREGGAVOFYORKD', is: false}, |
|||
{ obj: 'XE81ETHXREGGaVOFYORK', is: false}, |
|||
{ obj: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: true}, |
|||
{ obj: 'XD7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: false} |
|||
]; |
|||
|
|||
describe('lib/utils/utils', function () { |
|||
describe('isIBAN', function () { |
|||
tests.forEach(function (test) { |
|||
it('shoud test if value ' + test.obj + ' is iban: ' + test.is, function () { |
|||
assert.equal(utils.isIBAN(test.obj), test.is); |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
|
@ -0,0 +1,49 @@ |
|||
var chai = require('chai'); |
|||
var assert = chai.assert; |
|||
var web3 = require('../index'); |
|||
var FakeHttpProvider = require('./helpers/FakeHttpProvider'); |
|||
var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2'); |
|||
|
|||
describe('web3.eth.sendIBANTransaction', function () { |
|||
it('should send transaction', function () { |
|||
|
|||
var iban = 'XE81ETHXREGGAVOFYORK'; |
|||
var address = '0x1234567890123456789012345678901234500000'; |
|||
var exAddress = '0x1234567890123456789012345678901234567890' |
|||
|
|||
var provider = new FakeHttpProvider2(); |
|||
web3.setProvider(provider); |
|||
web3.reset(); |
|||
|
|||
provider.injectResultList([{ |
|||
result: exAddress |
|||
}, { |
|||
result: '' |
|||
}]); |
|||
|
|||
var step = 0; |
|||
provider.injectValidation(function (payload) { |
|||
if (step === 0) { |
|||
step++; |
|||
assert.equal(payload.method, 'eth_call'); |
|||
assert.deepEqual(payload.params, [{ |
|||
data: '0x3b3b57de5852454700000000000000000000000000000000000000000000000000000000', |
|||
to: web3.eth.namereg.address |
|||
}, "latest"]); |
|||
|
|||
return; |
|||
} |
|||
assert.equal(payload.method, 'eth_sendTransaction'); |
|||
assert.deepEqual(payload.params, [{ |
|||
data: '0xb214faa54741564f46594f524b0000000000000000000000000000000000000000000000', |
|||
from: address, |
|||
to: exAddress, |
|||
value: payload.params[0].value // don't check this
|
|||
}]); |
|||
}); |
|||
|
|||
web3.eth.sendIBANTransaction(address, iban, 10000); |
|||
|
|||
}); |
|||
}); |
|||
|
@ -1,16 +0,0 @@ |
|||
var BigNumber = require('bignumber.js'); |
|||
var web3 = require('../index'); |
|||
var testMethod = require('./helpers/test.method.js'); |
|||
|
|||
var method = 'sha3'; |
|||
|
|||
var tests = [{ |
|||
args: ['myString'], |
|||
formattedArgs: ['myString'], |
|||
result: '0x319319f831983198319881', |
|||
formattedResult: '0x319319f831983198319881', |
|||
call: 'web3_'+ method |
|||
}]; |
|||
|
|||
testMethod.runTests(null, method, tests); |
|||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,491 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/**
|
|||
* @author Christian <c@ethdev.com> |
|||
* @date 2015 |
|||
* Tests for a (comparatively) complex multisig wallet contract. |
|||
*/ |
|||
|
|||
#include <string> |
|||
#include <tuple> |
|||
#include <boost/test/unit_test.hpp> |
|||
#include <libdevcore/Hash.h> |
|||
#include <test/libsolidity/solidityExecutionFramework.h> |
|||
|
|||
using namespace std; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace solidity |
|||
{ |
|||
namespace test |
|||
{ |
|||
|
|||
static char const* walletCode = R"DELIMITER( |
|||
//sol Wallet
|
|||
// Multi-sig, daily-limited account proxy/wallet.
|
|||
// @authors:
|
|||
// Gav Wood <g@ethdev.com>
|
|||
// inheritable "property" contract that enables methods to be protected by requiring the acquiescence of either a
|
|||
// single, or, crucially, each of a number of, designated owners.
|
|||
// usage:
|
|||
// use modifiers onlyowner (just own owned) or onlymanyowners(hash), whereby the same hash must be provided by
|
|||
// some number (specified in constructor) of the set of owners (specified in the constructor, modifiable) before the
|
|||
// interior is executed.
|
|||
contract multiowned { |
|||
// struct for the status of a pending operation.
|
|||
struct PendingState { |
|||
uint yetNeeded; |
|||
uint ownersDone; |
|||
uint index; |
|||
} |
|||
// this contract only has five types of events: it can accept a confirmation, in which case
|
|||
// we record owner and operation (hash) alongside it.
|
|||
event Confirmation(address owner, bytes32 operation); |
|||
event Revoke(address owner, bytes32 operation); |
|||
// some others are in the case of an owner changing.
|
|||
event OwnerChanged(address oldOwner, address newOwner); |
|||
event OwnerAdded(address newOwner); |
|||
event OwnerRemoved(address oldOwner); |
|||
// the last one is emitted if the required signatures change
|
|||
event RequirementChanged(uint newRequirement); |
|||
// constructor is given number of sigs required to do protected "onlymanyowners" transactions
|
|||
// as well as the selection of addresses capable of confirming them.
|
|||
function multiowned() { |
|||
m_required = 1; |
|||
m_numOwners = 1; |
|||
m_owners[m_numOwners] = uint(msg.sender); |
|||
m_ownerIndex[uint(msg.sender)] = m_numOwners; |
|||
} |
|||
// simple single-sig function modifier.
|
|||
modifier onlyowner { |
|||
if (isOwner(msg.sender)) |
|||
_ |
|||
} |
|||
// multi-sig function modifier: the operation must have an intrinsic hash in order
|
|||
// that later attempts can be realised as the same underlying operation and
|
|||
// thus count as confirmations.
|
|||
modifier onlymanyowners(bytes32 _operation) { |
|||
if (confirmed(_operation)) |
|||
_ |
|||
} |
|||
// Revokes a prior confirmation of the given operation
|
|||
function revoke(bytes32 _operation) external { |
|||
uint ownerIndex = m_ownerIndex[uint(msg.sender)]; |
|||
// make sure they're an owner
|
|||
if (ownerIndex == 0) return; |
|||
uint ownerIndexBit = 2**ownerIndex; |
|||
var pending = m_pending[_operation]; |
|||
if (pending.ownersDone & ownerIndexBit > 0) { |
|||
pending.yetNeeded++; |
|||
pending.ownersDone -= ownerIndexBit; |
|||
Revoke(msg.sender, _operation); |
|||
} |
|||
} |
|||
function confirmed(bytes32 _operation) internal returns (bool) { |
|||
// determine what index the present sender is:
|
|||
uint ownerIndex = m_ownerIndex[uint(msg.sender)]; |
|||
// make sure they're an owner
|
|||
if (ownerIndex == 0) return; |
|||
|
|||
var pending = m_pending[_operation]; |
|||
// if we're not yet working on this operation, switch over and reset the confirmation status.
|
|||
if (pending.yetNeeded == 0) { |
|||
// reset count of confirmations needed.
|
|||
pending.yetNeeded = m_required; |
|||
// reset which owners have confirmed (none) - set our bitmap to 0.
|
|||
pending.ownersDone = 0; |
|||
pending.index = m_pendingIndex.length++; |
|||
m_pendingIndex[pending.index] = _operation; |
|||
} |
|||
// determine the bit to set for this owner.
|
|||
uint ownerIndexBit = 2**ownerIndex; |
|||
// make sure we (the message sender) haven't confirmed this operation previously.
|
|||
if (pending.ownersDone & ownerIndexBit == 0) { |
|||
Confirmation(msg.sender, _operation); |
|||
// ok - check if count is enough to go ahead.
|
|||
if (pending.yetNeeded <= 1) { |
|||
// enough confirmations: reset and run interior.
|
|||
delete m_pendingIndex[m_pending[_operation].index]; |
|||
delete m_pending[_operation]; |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
// not enough: record that this owner in particular confirmed.
|
|||
pending.yetNeeded--; |
|||
pending.ownersDone |= ownerIndexBit; |
|||
} |
|||
} |
|||
} |
|||
// Replaces an owner `_from` with another `_to`.
|
|||
function changeOwner(address _from, address _to) onlymanyowners(sha3(msg.data)) external { |
|||
if (isOwner(_to)) return; |
|||
uint ownerIndex = m_ownerIndex[uint(_from)]; |
|||
if (ownerIndex == 0) return; |
|||
|
|||
clearPending(); |
|||
m_owners[ownerIndex] = uint(_to); |
|||
m_ownerIndex[uint(_from)] = 0; |
|||
m_ownerIndex[uint(_to)] = ownerIndex; |
|||
OwnerChanged(_from, _to); |
|||
} |
|||
function addOwner(address _owner) onlymanyowners(sha3(msg.data)) external { |
|||
if (isOwner(_owner)) return; |
|||
|
|||
clearPending(); |
|||
if (m_numOwners >= c_maxOwners) |
|||
reorganizeOwners(); |
|||
if (m_numOwners >= c_maxOwners) |
|||
return; |
|||
m_numOwners++; |
|||
m_owners[m_numOwners] = uint(_owner); |
|||
m_ownerIndex[uint(_owner)] = m_numOwners; |
|||
OwnerAdded(_owner); |
|||
} |
|||
function removeOwner(address _owner) onlymanyowners(sha3(msg.data)) external { |
|||
uint ownerIndex = m_ownerIndex[uint(_owner)]; |
|||
if (ownerIndex == 0) return; |
|||
if (m_required > m_numOwners - 1) return; |
|||
|
|||
m_owners[ownerIndex] = 0; |
|||
m_ownerIndex[uint(_owner)] = 0; |
|||
clearPending(); |
|||
reorganizeOwners(); //make sure m_numOwner is equal to the number of owners and always points to the optimal free slot
|
|||
OwnerRemoved(_owner); |
|||
} |
|||
function reorganizeOwners() private returns (bool) { |
|||
uint free = 1; |
|||
while (free < m_numOwners) |
|||
{ |
|||
while (free < m_numOwners && m_owners[free] != 0) free++; |
|||
while (m_numOwners > 1 && m_owners[m_numOwners] == 0) m_numOwners--; |
|||
if (free < m_numOwners && m_owners[m_numOwners] != 0 && m_owners[free] == 0) |
|||
{ |
|||
m_owners[free] = m_owners[m_numOwners]; |
|||
m_ownerIndex[m_owners[free]] = free; |
|||
m_owners[m_numOwners] = 0; |
|||
} |
|||
} |
|||
} |
|||
function clearPending() internal { |
|||
uint length = m_pendingIndex.length; |
|||
for (uint i = 0; i < length; ++i) |
|||
if (m_pendingIndex[i] != 0) |
|||
delete m_pending[m_pendingIndex[i]]; |
|||
delete m_pendingIndex; |
|||
} |
|||
function changeRequirement(uint _newRequired) onlymanyowners(sha3(msg.data)) external { |
|||
if (_newRequired > m_numOwners) return; |
|||
m_required = _newRequired; |
|||
clearPending(); |
|||
RequirementChanged(_newRequired); |
|||
} |
|||
function isOwner(address _addr) returns (bool) { |
|||
return m_ownerIndex[uint(_addr)] > 0; |
|||
} |
|||
|
|||
// the number of owners that must confirm the same operation before it is run.
|
|||
uint m_required; |
|||
// pointer used to find a free slot in m_owners
|
|||
uint m_numOwners; |
|||
// list of owners
|
|||
uint[256] m_owners; |
|||
uint constant c_maxOwners = 250; |
|||
// index on the list of owners to allow reverse lookup
|
|||
mapping(uint => uint) m_ownerIndex; |
|||
// the ongoing operations.
|
|||
mapping(bytes32 => PendingState) m_pending; |
|||
bytes32[] m_pendingIndex; |
|||
} |
|||
|
|||
// inheritable "property" contract that enables methods to be protected by placing a linear limit (specifiable)
|
|||
// on a particular resource per calendar day. is multiowned to allow the limit to be altered. resource that method
|
|||
// uses is specified in the modifier.
|
|||
contract daylimit is multiowned { |
|||
// constructor - just records the present day's index.
|
|||
function daylimit() { |
|||
m_lastDay = today(); |
|||
} |
|||
// (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
|
|||
function setDailyLimit(uint _newLimit) onlymanyowners(sha3(msg.data)) external { |
|||
m_dailyLimit = _newLimit; |
|||
} |
|||
// (re)sets the daily limit. needs many of the owners to confirm. doesn't alter the amount already spent today.
|
|||
function resetSpentToday() onlymanyowners(sha3(msg.data)) external { |
|||
m_spentToday = 0; |
|||
} |
|||
// checks to see if there is at least `_value` left from the daily limit today. if there is, subtracts it and
|
|||
// returns true. otherwise just returns false.
|
|||
function underLimit(uint _value) internal onlyowner returns (bool) { |
|||
// reset the spend limit if we're on a different day to last time.
|
|||
if (today() > m_lastDay) { |
|||
m_spentToday = 0; |
|||
m_lastDay = today(); |
|||
} |
|||
// check to see if there's enough left - if so, subtract and return true.
|
|||
if (m_spentToday + _value >= m_spentToday && m_spentToday + _value <= m_dailyLimit) { |
|||
m_spentToday += _value; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
// simple modifier for daily limit.
|
|||
modifier limitedDaily(uint _value) { |
|||
if (underLimit(_value)) |
|||
_ |
|||
} |
|||
// determines today's index.
|
|||
function today() private constant returns (uint) { return now / 1 days; } |
|||
uint m_spentToday; |
|||
uint m_dailyLimit; |
|||
uint m_lastDay; |
|||
} |
|||
// interface contract for multisig proxy contracts; see below for docs.
|
|||
contract multisig { |
|||
event Deposit(address from, uint value); |
|||
event SingleTransact(address owner, uint value, address to, bytes data); |
|||
event MultiTransact(address owner, bytes32 operation, uint value, address to, bytes data); |
|||
event ConfirmationNeeded(bytes32 operation, address initiator, uint value, address to, bytes data); |
|||
function changeOwner(address _from, address _to) external; |
|||
function execute(address _to, uint _value, bytes _data) external returns (bytes32); |
|||
function confirm(bytes32 _h) returns (bool); |
|||
} |
|||
// usage:
|
|||
// bytes32 h = Wallet(w).from(oneOwner).transact(to, value, data);
|
|||
// Wallet(w).from(anotherOwner).confirm(h);
|
|||
contract Wallet is multisig, multiowned, daylimit { |
|||
// Transaction structure to remember details of transaction lest it need be saved for a later call.
|
|||
struct Transaction { |
|||
address to; |
|||
uint value; |
|||
bytes data; |
|||
} |
|||
// logged events:
|
|||
// Funds has arrived into the wallet (record how much).
|
|||
event Deposit(address from, uint value); |
|||
// Single transaction going out of the wallet (record who signed for it, how much, and to whom it's going).
|
|||
event SingleTransact(address owner, uint value, address to, bytes data); |
|||
// constructor - just pass on the owner arra to the multiowned.
|
|||
event Created(); |
|||
function Wallet() { |
|||
Created(); |
|||
} |
|||
// kills the contract sending everything to `_to`.
|
|||
function kill(address _to) onlymanyowners(sha3(msg.data)) external { |
|||
suicide(_to); |
|||
} |
|||
// gets called when no other function matches
|
|||
function() { |
|||
// just being sent some cash?
|
|||
if (msg.value > 0) |
|||
Deposit(msg.sender, msg.value); |
|||
} |
|||
// Outside-visible transact entry point. Executes transacion immediately if below daily spend limit.
|
|||
// If not, goes into multisig process. We provide a hash on return to allow the sender to provide
|
|||
// shortcuts for the other confirmations (allowing them to avoid replicating the _to, _value
|
|||
// and _data arguments). They still get the option of using them if they want, anyways.
|
|||
function execute(address _to, uint _value, bytes _data) onlyowner external returns (bytes32 _r) { |
|||
// first, take the opportunity to check that we're under the daily limit.
|
|||
if (underLimit(_value)) { |
|||
SingleTransact(msg.sender, _value, _to, _data); |
|||
// yes - just execute the call.
|
|||
_to.call.value(_value)(_data); |
|||
return 0; |
|||
} |
|||
// determine our operation hash.
|
|||
_r = sha3(msg.data); |
|||
if (!confirm(_r) && m_txs[_r].to == 0) { |
|||
m_txs[_r].to = _to; |
|||
m_txs[_r].value = _value; |
|||
m_txs[_r].data = _data; |
|||
ConfirmationNeeded(_r, msg.sender, _value, _to, _data); |
|||
} |
|||
} |
|||
// confirm a transaction through just the hash. we use the previous transactions map, m_txs, in order
|
|||
// to determine the body of the transaction from the hash provided.
|
|||
function confirm(bytes32 _h) onlymanyowners(_h) returns (bool) { |
|||
if (m_txs[_h].to != 0) { |
|||
m_txs[_h].to.call.value(m_txs[_h].value)(m_txs[_h].data); |
|||
MultiTransact(msg.sender, _h, m_txs[_h].value, m_txs[_h].to, m_txs[_h].data); |
|||
delete m_txs[_h]; |
|||
return true; |
|||
} |
|||
} |
|||
function clearPending() internal { |
|||
uint length = m_pendingIndex.length; |
|||
for (uint i = 0; i < length; ++i) |
|||
delete m_txs[m_pendingIndex[i]]; |
|||
super.clearPending(); |
|||
} |
|||
// pending transactions we have at present.
|
|||
mapping (bytes32 => Transaction) m_txs; |
|||
} |
|||
)DELIMITER"; |
|||
|
|||
static unique_ptr<bytes> s_compiledWallet; |
|||
|
|||
class WalletTestFramework: public ExecutionFramework |
|||
{ |
|||
protected: |
|||
void deployWallet(u256 const& _value = 0) |
|||
{ |
|||
if (!s_compiledWallet) |
|||
{ |
|||
m_optimize = true; |
|||
m_compiler.reset(false, m_addStandardSources); |
|||
m_compiler.addSource("", walletCode); |
|||
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); |
|||
s_compiledWallet.reset(new bytes(m_compiler.getBytecode("Wallet"))); |
|||
} |
|||
sendMessage(*s_compiledWallet, true, _value); |
|||
BOOST_REQUIRE(!m_output.empty()); |
|||
} |
|||
}; |
|||
|
|||
/// This is a test suite that tests optimised code!
|
|||
BOOST_FIXTURE_TEST_SUITE(SolidityWallet, WalletTestFramework) |
|||
|
|||
BOOST_AUTO_TEST_CASE(creation) |
|||
{ |
|||
deployWallet(200); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(m_sender, h256::AlignRight)) == encodeArgs(true)); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", ~h256(m_sender, h256::AlignRight)) == encodeArgs(false)); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(add_owners) |
|||
{ |
|||
deployWallet(200); |
|||
Address originalOwner = m_sender; |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); |
|||
// now let the new owner add someone
|
|||
m_sender = Address(0x12); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); |
|||
// and check that a non-owner cannot add a new owner
|
|||
m_sender = Address(0x50); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x20)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x20)) == encodeArgs(false)); |
|||
// finally check that all the owners are there
|
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(originalOwner, h256::AlignRight)) == encodeArgs(true)); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(change_owners) |
|||
{ |
|||
deployWallet(200); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(true)); |
|||
BOOST_REQUIRE(callContractFunction("changeOwner(address,address)", h256(0x12), h256(0x13)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12)) == encodeArgs(false)); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x13)) == encodeArgs(true)); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(remove_owner) |
|||
{ |
|||
deployWallet(200); |
|||
// add 10 owners
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
{ |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); |
|||
} |
|||
// check they are there again
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); |
|||
// remove the odd owners
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
if (i % 2 == 1) |
|||
BOOST_REQUIRE(callContractFunction("removeOwner(address)", h256(0x12 + i)) == encodeArgs()); |
|||
// check the result
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(i % 2 == 0)); |
|||
// add them again
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
if (i % 2 == 1) |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12 + i)) == encodeArgs()); |
|||
// check everyone is there
|
|||
for (unsigned i = 0; i < 10; ++i) |
|||
BOOST_REQUIRE(callContractFunction("isOwner(address)", h256(0x12 + i)) == encodeArgs(true)); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(multisig_value_transfer) |
|||
{ |
|||
deployWallet(200); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); |
|||
// 4 owners, set required to 3
|
|||
BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); |
|||
// check that balance is and stays zero at destination address
|
|||
h256 opHash("f916231db11c12e0142dc51f23632bc655de87c63f83fc928c443e90f7aa364a"); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
m_sender = Address(0x12); |
|||
BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
m_sender = Address(0x13); |
|||
BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
m_sender = Address(0x14); |
|||
BOOST_REQUIRE(callContractFunction("execute(address,uint256,bytes)", h256(0x05), 100, 0x60, 0x00) == encodeArgs(opHash)); |
|||
// now it should go through
|
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 100); |
|||
} |
|||
|
|||
BOOST_AUTO_TEST_CASE(daylimit) |
|||
{ |
|||
deployWallet(200); |
|||
BOOST_REQUIRE(callContractFunction("setDailyLimit(uint256)", h256(100)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x12)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x13)) == encodeArgs()); |
|||
BOOST_REQUIRE(callContractFunction("addOwner(address)", h256(0x14)) == encodeArgs()); |
|||
// 4 owners, set required to 3
|
|||
BOOST_REQUIRE(callContractFunction("changeRequirement(uint256)", u256(3)) == encodeArgs()); |
|||
|
|||
// try to send tx over daylimit
|
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
m_sender = Address(0x12); |
|||
BOOST_REQUIRE( |
|||
callContractFunction("execute(address,uint256,bytes)", h256(0x05), 150, 0x60, 0x00) != |
|||
encodeArgs(u256(0)) |
|||
); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
// try to send tx under daylimit by stranger
|
|||
m_sender = Address(0x77); |
|||
BOOST_REQUIRE( |
|||
callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == |
|||
encodeArgs(u256(0)) |
|||
); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 0); |
|||
// now send below limit by owner
|
|||
m_sender = Address(0x12); |
|||
BOOST_REQUIRE( |
|||
callContractFunction("execute(address,uint256,bytes)", h256(0x05), 90, 0x60, 0x00) == |
|||
encodeArgs(u256(0)) |
|||
); |
|||
BOOST_CHECK_EQUAL(m_state.balance(Address(0x05)), 90); |
|||
} |
|||
|
|||
//@todo test data calls
|
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
|||
|
|||
} |
|||
} |
|||
} // end namespaces
|
Loading…
Reference in new issue