Browse Source

bitcoin-cli, rpcrawtransaction: harmonize "{" styling

0.10
Jeff Garzik 11 years ago
parent
commit
3ce7e669e3
  1. 31
      src/bitcoin-cli.cpp
  2. 81
      src/rpcrawtransaction.cpp

31
src/bitcoin-cli.cpp

@ -50,8 +50,7 @@ static bool AppInitRPC(int argc, char* argv[])
// Parameters // Parameters
// //
ParseParameters(argc, argv); ParseParameters(argc, argv);
if (!boost::filesystem::is_directory(GetDataDir(false))) if (!boost::filesystem::is_directory(GetDataDir(false))) {
{
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str()); fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", mapArgs["-datadir"].c_str());
return false; return false;
} }
@ -66,11 +65,9 @@ static bool AppInitRPC(int argc, char* argv[])
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n"); fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
return false; return false;
} }
if (argc<2 || mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version")) if (argc<2 || mapArgs.count("-?") || mapArgs.count("-help") || mapArgs.count("-version")) {
{
std::string strUsage = _("Bitcoin Core RPC client version") + " " + FormatFullVersion() + "\n"; std::string strUsage = _("Bitcoin Core RPC client version") + " " + FormatFullVersion() + "\n";
if (!mapArgs.count("-version")) if (!mapArgs.count("-version")) {
{
strUsage += "\n" + _("Usage:") + "\n" + strUsage += "\n" + _("Usage:") + "\n" +
" bitcoin-cli [options] <command> [params] " + _("Send command to Bitcoin Core") + "\n" + " bitcoin-cli [options] <command> [params] " + _("Send command to Bitcoin Core") + "\n" +
" bitcoin-cli [options] help " + _("List commands") + "\n" + " bitcoin-cli [options] help " + _("List commands") + "\n" +
@ -153,11 +150,9 @@ int CommandLineRPC(int argc, char *argv[])
{ {
string strPrint; string strPrint;
int nRet = 0; int nRet = 0;
try try {
{
// Skip switches // Skip switches
while (argc > 1 && IsSwitchChar(argv[1][0])) while (argc > 1 && IsSwitchChar(argv[1][0])) {
{
argc--; argc--;
argv++; argv++;
} }
@ -178,15 +173,12 @@ int CommandLineRPC(int argc, char *argv[])
const Value& result = find_value(reply, "result"); const Value& result = find_value(reply, "result");
const Value& error = find_value(reply, "error"); const Value& error = find_value(reply, "error");
if (error.type() != null_type) if (error.type() != null_type) {
{
// Error // Error
strPrint = "error: " + write_string(error, false); strPrint = "error: " + write_string(error, false);
int code = find_value(error.get_obj(), "code").get_int(); int code = find_value(error.get_obj(), "code").get_int();
nRet = abs(code); nRet = abs(code);
} } else {
else
{
// Result // Result
if (result.type() == null_type) if (result.type() == null_type)
strPrint = ""; strPrint = "";
@ -208,8 +200,7 @@ int CommandLineRPC(int argc, char *argv[])
throw; throw;
} }
if (strPrint != "") if (strPrint != "") {
{
fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
} }
return nRet; return nRet;
@ -219,8 +210,7 @@ int main(int argc, char* argv[])
{ {
SetupEnvironment(); SetupEnvironment();
try try {
{
if(!AppInitRPC(argc, argv)) if(!AppInitRPC(argc, argv))
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -233,8 +223,7 @@ int main(int argc, char* argv[])
} }
int ret = EXIT_FAILURE; int ret = EXIT_FAILURE;
try try {
{
ret = CommandLineRPC(argc, argv); ret = CommandLineRPC(argc, argv);
} }
catch (std::exception& e) { catch (std::exception& e) {

81
src/rpcrawtransaction.cpp

@ -37,8 +37,7 @@ void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeH
if (fIncludeHex) if (fIncludeHex)
out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
{
out.push_back(Pair("type", GetTxnOutputType(type))); out.push_back(Pair("type", GetTxnOutputType(type)));
return; return;
} }
@ -58,13 +57,11 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
entry.push_back(Pair("version", tx.nVersion)); entry.push_back(Pair("version", tx.nVersion));
entry.push_back(Pair("locktime", (int64_t)tx.nLockTime)); entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
Array vin; Array vin;
BOOST_FOREACH(const CTxIn& txin, tx.vin) BOOST_FOREACH(const CTxIn& txin, tx.vin) {
{
Object in; Object in;
if (tx.IsCoinBase()) if (tx.IsCoinBase())
in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
else else {
{
in.push_back(Pair("txid", txin.prevout.hash.GetHex())); in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
in.push_back(Pair("vout", (int64_t)txin.prevout.n)); in.push_back(Pair("vout", (int64_t)txin.prevout.n));
Object o; Object o;
@ -77,8 +74,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
} }
entry.push_back(Pair("vin", vin)); entry.push_back(Pair("vin", vin));
Array vout; Array vout;
for (unsigned int i = 0; i < tx.vout.size(); i++) for (unsigned int i = 0; i < tx.vout.size(); i++) {
{
const CTxOut& txout = tx.vout[i]; const CTxOut& txout = tx.vout[i];
Object out; Object out;
out.push_back(Pair("value", ValueFromAmount(txout.nValue))); out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
@ -90,15 +86,12 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
} }
entry.push_back(Pair("vout", vout)); entry.push_back(Pair("vout", vout));
if (hashBlock != 0) if (hashBlock != 0) {
{
entry.push_back(Pair("blockhash", hashBlock.GetHex())); entry.push_back(Pair("blockhash", hashBlock.GetHex()));
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock); map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second) if (mi != mapBlockIndex.end() && (*mi).second) {
{
CBlockIndex* pindex = (*mi).second; CBlockIndex* pindex = (*mi).second;
if (chainActive.Contains(pindex)) if (chainActive.Contains(pindex)) {
{
entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight)); entry.push_back(Pair("confirmations", 1 + chainActive.Height() - pindex->nHeight));
entry.push_back(Pair("time", pindex->GetBlockTime())); entry.push_back(Pair("time", pindex->GetBlockTime()));
entry.push_back(Pair("blocktime", pindex->GetBlockTime())); entry.push_back(Pair("blocktime", pindex->GetBlockTime()));
@ -244,11 +237,9 @@ Value listunspent(const Array& params, bool fHelp)
nMaxDepth = params[1].get_int(); nMaxDepth = params[1].get_int();
set<CBitcoinAddress> setAddress; set<CBitcoinAddress> setAddress;
if (params.size() > 2) if (params.size() > 2) {
{
Array inputs = params[2].get_array(); Array inputs = params[2].get_array();
BOOST_FOREACH(Value& input, inputs) BOOST_FOREACH(Value& input, inputs) {
{
CBitcoinAddress address(input.get_str()); CBitcoinAddress address(input.get_str());
if (!address.IsValid()) if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str()); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str());
@ -262,13 +253,11 @@ Value listunspent(const Array& params, bool fHelp)
vector<COutput> vecOutputs; vector<COutput> vecOutputs;
assert(pwalletMain != NULL); assert(pwalletMain != NULL);
pwalletMain->AvailableCoins(vecOutputs, false); pwalletMain->AvailableCoins(vecOutputs, false);
BOOST_FOREACH(const COutput& out, vecOutputs) BOOST_FOREACH(const COutput& out, vecOutputs) {
{
if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth) if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
continue; continue;
if (setAddress.size()) if (setAddress.size()) {
{
CTxDestination address; CTxDestination address;
if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
continue; continue;
@ -283,18 +272,15 @@ Value listunspent(const Array& params, bool fHelp)
entry.push_back(Pair("txid", out.tx->GetHash().GetHex())); entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
entry.push_back(Pair("vout", out.i)); entry.push_back(Pair("vout", out.i));
CTxDestination address; CTxDestination address;
if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
{
entry.push_back(Pair("address", CBitcoinAddress(address).ToString())); entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
if (pwalletMain->mapAddressBook.count(address)) if (pwalletMain->mapAddressBook.count(address))
entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name)); entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name));
} }
entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end()))); entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
if (pk.IsPayToScriptHash()) if (pk.IsPayToScriptHash()) {
{
CTxDestination address; CTxDestination address;
if (ExtractDestination(pk, address)) if (ExtractDestination(pk, address)) {
{
const CScriptID& hash = boost::get<const CScriptID&>(address); const CScriptID& hash = boost::get<const CScriptID&>(address);
CScript redeemScript; CScript redeemScript;
if (pwalletMain->GetCScript(hash, redeemScript)) if (pwalletMain->GetCScript(hash, redeemScript))
@ -351,8 +337,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
CMutableTransaction rawTx; CMutableTransaction rawTx;
BOOST_FOREACH(const Value& input, inputs) BOOST_FOREACH(const Value& input, inputs) {
{
const Object& o = input.get_obj(); const Object& o = input.get_obj();
uint256 txid = ParseHashO(o, "txid"); uint256 txid = ParseHashO(o, "txid");
@ -369,8 +354,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
} }
set<CBitcoinAddress> setAddress; set<CBitcoinAddress> setAddress;
BOOST_FOREACH(const Pair& s, sendTo) BOOST_FOREACH(const Pair& s, sendTo) {
{
CBitcoinAddress address(s.name_); CBitcoinAddress address(s.name_);
if (!address.IsValid()) if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+s.name_);
@ -550,8 +534,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
vector<unsigned char> txData(ParseHexV(params[0], "argument 1")); vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION); CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
vector<CMutableTransaction> txVariants; vector<CMutableTransaction> txVariants;
while (!ssData.empty()) while (!ssData.empty()) {
{
try { try {
CMutableTransaction tx; CMutableTransaction tx;
ssData >> tx; ssData >> tx;
@ -590,12 +573,10 @@ Value signrawtransaction(const Array& params, bool fHelp)
bool fGivenKeys = false; bool fGivenKeys = false;
CBasicKeyStore tempKeystore; CBasicKeyStore tempKeystore;
if (params.size() > 2 && params[2].type() != null_type) if (params.size() > 2 && params[2].type() != null_type) {
{
fGivenKeys = true; fGivenKeys = true;
Array keys = params[2].get_array(); Array keys = params[2].get_array();
BOOST_FOREACH(Value k, keys) BOOST_FOREACH(Value k, keys) {
{
CBitcoinSecret vchSecret; CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(k.get_str()); bool fGood = vchSecret.SetString(k.get_str());
if (!fGood) if (!fGood)
@ -610,11 +591,9 @@ Value signrawtransaction(const Array& params, bool fHelp)
#endif #endif
// Add previous txouts given in the RPC call: // Add previous txouts given in the RPC call:
if (params.size() > 1 && params[1].type() != null_type) if (params.size() > 1 && params[1].type() != null_type) {
{
Array prevTxs = params[1].get_array(); Array prevTxs = params[1].get_array();
BOOST_FOREACH(Value& p, prevTxs) BOOST_FOREACH(Value& p, prevTxs) {
{
if (p.type() != obj_type) if (p.type() != obj_type)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}"); throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
@ -649,12 +628,10 @@ Value signrawtransaction(const Array& params, bool fHelp)
// if redeemScript given and not using the local wallet (private keys // if redeemScript given and not using the local wallet (private keys
// given), add redeemScript to the tempKeystore so it can be signed: // given), add redeemScript to the tempKeystore so it can be signed:
if (fGivenKeys && scriptPubKey.IsPayToScriptHash()) if (fGivenKeys && scriptPubKey.IsPayToScriptHash()) {
{
RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type)); RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type)("redeemScript",str_type));
Value v = find_value(prevOut, "redeemScript"); Value v = find_value(prevOut, "redeemScript");
if (!(v == Value::null)) if (!(v == Value::null)) {
{
vector<unsigned char> rsData(ParseHexV(v, "redeemScript")); vector<unsigned char> rsData(ParseHexV(v, "redeemScript"));
CScript redeemScript(rsData.begin(), rsData.end()); CScript redeemScript(rsData.begin(), rsData.end());
tempKeystore.AddCScript(redeemScript); tempKeystore.AddCScript(redeemScript);
@ -670,8 +647,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
#endif #endif
int nHashType = SIGHASH_ALL; int nHashType = SIGHASH_ALL;
if (params.size() > 3 && params[3].type() != null_type) if (params.size() > 3 && params[3].type() != null_type) {
{
static map<string, int> mapSigHashValues = static map<string, int> mapSigHashValues =
boost::assign::map_list_of boost::assign::map_list_of
(string("ALL"), int(SIGHASH_ALL)) (string("ALL"), int(SIGHASH_ALL))
@ -691,12 +667,10 @@ Value signrawtransaction(const Array& params, bool fHelp)
bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE); bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
// Sign what we can: // Sign what we can:
for (unsigned int i = 0; i < mergedTx.vin.size(); i++) for (unsigned int i = 0; i < mergedTx.vin.size(); i++) {
{
CTxIn& txin = mergedTx.vin[i]; CTxIn& txin = mergedTx.vin[i];
CCoins coins; CCoins coins;
if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n)) if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n)) {
{
fComplete = false; fComplete = false;
continue; continue;
} }
@ -708,8 +682,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
SignSignature(keystore, prevPubKey, mergedTx, i, nHashType); SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
// ... and merge in other signatures: // ... and merge in other signatures:
BOOST_FOREACH(const CMutableTransaction& txv, txVariants) BOOST_FOREACH(const CMutableTransaction& txv, txVariants) {
{
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig); txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
} }
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS, 0)) if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, STANDARD_SCRIPT_VERIFY_FLAGS, 0))

Loading…
Cancel
Save