From a7ee7bef2a7c83746565f71bdd559922ed354dae Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Tue, 17 Jun 2014 15:25:04 -0700 Subject: [PATCH] transact --- eth/main.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index b8ce5967c..3add014ce 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include @@ -31,6 +33,7 @@ #include #include #include +#include #if ETH_READLINE #include #include @@ -41,6 +44,7 @@ #include "BuildInfo.h" using namespace std; using namespace eth; +using namespace boost::algorithm; using eth::Instruction; using eth::c_instructionInfo; @@ -71,9 +75,9 @@ void interactiveHelp() << " block Gives the current block height." << endl << " balance Gives the current balance." << endl << " peers List the peers that are connected" << endl - << " transact Execute a given transaction. TODO." << endl - << " send Execute a given transaction with current secret. TODO." << endl - << " create Create a new contract with current secret. TODO." << endl + << " transact Execute a given transaction." << endl + << " send Execute a given transaction with current secret." << endl + << " contract Create a new contract with current secret." << endl << " inspect Dumps a contract to /.evm." << endl << " exit Exits the application." << endl; } @@ -156,7 +160,7 @@ string pretty(h160 _a, eth::State _st) } return ns; } - +bytes parse_data(string _args); int main(int argc, char** argv) { unsigned short listenPort = 30303; @@ -399,11 +403,62 @@ int main(int argc, char** argv) else if (cmd == "balance") { ClientGuard g(&c); - cout << "Current balance: " << c.postState().balance(us.address()) << endl; + cout << "Current balance: " << formatBalance(c.postState().balance(us.address())) << " = " << c.postState().balance(us.address()) << " wei" << endl; } else if (cmd == "transact") { - //TODO. + ClientGuard g(&c); + auto const& bc = c.blockChain(); + auto h = bc.currentHash(); + auto blockData = bc.block(h); + BlockInfo info(blockData); + if(iss.peek()!=-1){ + string hexAddr; + u256 amount; + u256 gasPrice; + u256 gas; + string sechex; + string sdata; + + iss >> hexAddr >> amount >> gasPrice >> gas >> sechex >> sdata; + + cnote << "Data:"; + cnote << sdata; + bytes data = parse_data(sdata); + cnote << "Bytes:"; + string sbd = asString(data); + bytes bbd = asBytes(sbd); + stringstream ssbd; + ssbd << bbd; + cnote << ssbd.str(); + int ssize = sechex.length(); + int size = hexAddr.length(); + u256 minGas = (u256)c.state().callGas(data.size(), 0); + if (size < 40) + { + if (size > 0) + cwarn << "Invalid address length: " << size; + } + else if (amount < 0) + cwarn << "Invalid amount: " << amount; + else if (gasPrice < info.minGasPrice) + cwarn << "Minimum gas price is " << info.minGasPrice; + else if (gas < minGas) + cwarn << "Minimum gas amount is " << minGas; + else if (ssize < 40) + { + if (ssize > 0) + cwarn << "Invalid secret length:" << ssize; + } + else + { + Secret secret = h256(fromHex(sechex)); + Address dest = h160(fromHex(hexAddr)); + c.transact(secret, amount, dest, data, gas, gasPrice); + } + } else { + cwarn << "Require parameters: transact ADDRESS AMOUNT GASPRICE GAS SECRET DATA"; + } } else if (cmd == "send") {