Skip to content

Commit

Permalink
lots of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Nov 15, 2018
1 parent 9d8662f commit 992d54b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void SetupChainParamsBaseOptions()

gArgs.AddArg("-anyonecanspendaremine", "Consider ANYONECANSPEND outputs as yours.", false, OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-fedpegscript", "The script for the federated peg.", false, OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-parentpubkeyprefix", strprintf("The byte prefix, in decimal, of the parent chain's base58 pubkey address. (default: %d)", 111));
gArgs.AddArg("-parentscriptprefix", strprintf("The byte prefix, in decimal, of the parent chain's base58 script address. (default: %d)", 196));
gArgs.AddArg("-parentpubkeyprefix", strprintf("The byte prefix, in decimal, of the parent chain's base58 pubkey address. (default: %d)", 111), false, OptionsCategory::ELEMENTS);
gArgs.AddArg("-parentscriptprefix", strprintf("The byte prefix, in decimal, of the parent chain's base58 script address. (default: %d)", 196), false, OptionsCategory::ELEMENTS);
gArgs.AddArg("-parent_bech32_hrp", strprintf("The human-readable part of the parent chain's bech32 encoding. (default: %s)", "bc"), false, OptionsCategory::ELEMENTS);
}

Expand Down
6 changes: 3 additions & 3 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class DestinationEncoder : public boost::static_visitor<std::string>
}

std::string operator()(const CNoDestination& no) const { return {}; }
std::string operator()(const NullData& nd) const { return {}; }
};

CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, const bool for_parent)
Expand All @@ -89,7 +90,6 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
return CKeyID(hash);
}
{
// Script-hash-addresses have version 5 (or 196 testnet).
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
CChainParams::Base58Type type_sh = for_parent ? CChainParams::PARENT_SCRIPT_ADDRESS : CChainParams::SCRIPT_ADDRESS;
Expand Down Expand Up @@ -232,15 +232,15 @@ bool IsValidDestinationString(const std::string& str, const CChainParams& params

bool IsValidDestinationString(const std::string& str)
{
return IsValidDestinationString(str, Params());
return IsValidDestination(DecodeDestination(str, Params(), true));
}

//
// ELEMENTS

std::string EncodeParentDestination(const CTxDestination& dest)
{
return boost::apply_visitor(DestinationEncoder(Params()), dest, true);
return boost::apply_visitor(DestinationEncoder(Params(), true), dest);
}

CTxDestination DecodeParentDestination(const std::string& str)
Expand Down
3 changes: 2 additions & 1 deletion src/mainchainrpc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <chainparamsbase.h>
#include <mainchainrpc.h>

#include <chainparamsbase.h>
#include <util.h>
#include <utilstrencodings.h>
#include <rpc/protocol.h>
Expand Down
13 changes: 7 additions & 6 deletions src/mainchainrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#ifndef BITCOIN_MAINCHAINRPC_H
#define BITCOIN_MAINCHAINRPC_H

#include "rpc/client.h"
#include "rpc/protocol.h"
#include "uint256.h"
#include <rpc/client.h>
#include <rpc/protocol.h>
#include <uint256.h>

#include <string>
#include <stdexcept>

//#include <univalue.h>
#include "univalue/include/univalue.h"
#include <univalue.h>
//TODO(stevenroose) remove if not needed
//#include <univalue/include/univalue.h>

static const bool DEFAULT_NAMED=false;
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
Expand All @@ -40,7 +41,7 @@ UniValue CallMainChainRPC(const std::string& strMethod, const UniValue& params);
// of confirmations.
// For validating merkle blocks, you can provide the nbTxs parameter to verify if
// it equals the number of transactions in the block.
bool IsConfirmedBitcoinBlock(const uint256& hash, int nMinConfirmationDepth, int nbTxs);
bool IsConfirmedBitcoinBlock(const uint256& hash, const int nMinConfirmationDepth, const int nbTxs);

#endif // BITCOIN_MAINCHAINRPC_H

8 changes: 8 additions & 0 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue>
obj.pushKV("witness_program", HexStr(id.program, id.program + id.length));
return obj;
}

UniValue operator()(const NullData& id) const
{
UniValue obj(UniValue::VOBJ);
obj.pushKV("isscript", false);
obj.pushKV("iswitness", false);
return obj;
}
};

UniValue DescribeAddress(const CTxDestination& dest)
Expand Down
5 changes: 3 additions & 2 deletions src/script/ismine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <script/ismine.h>

#include <chainparams.h>
#include <key.h>
#include <keystore.h>
#include <script/script.h>
Expand Down Expand Up @@ -137,7 +138,6 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
}
break;
}

case TX_MULTISIG:
{
// Never treat bare multisig outputs as ours (they can still be made watchonly-though)
Expand Down Expand Up @@ -165,7 +165,8 @@ IsMineResult IsMineInner(const CKeyStore& keystore, const CScript& scriptPubKey,
}
case TX_TRUE:
if (Params().anyonecanspend_aremine)
return ISMINE_SPENDABLE;
ret = std::max(ret, IsMineResult::SPENDABLE);
break;
}

if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
Expand Down
10 changes: 10 additions & 0 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ class CScriptVisitor : public boost::static_visitor<bool>
*script << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
return true;
}

bool operator()(const NullData& id) const
{
script->clear();
*script << OP_RETURN;
for (const auto& push : id.null_data) {
*script << push;
}
return true;
}
};
} // namespace

Expand Down
17 changes: 16 additions & 1 deletion src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ struct WitnessUnknown
}
};

class NullData
{
public:
std::vector<std::vector<unsigned char>> null_data;
friend bool operator==(const NullData &a, const NullData &b) { return true; }
friend bool operator<(const NullData &a, const NullData &b) { return true; }

NullData& operator<<(std::vector<unsigned char> b)
{
null_data.push_back(b);
return *this;
}
};

/**
* A txout script template with a specific destination. It is either:
* * CNoDestination: no destination set
Expand All @@ -119,9 +133,10 @@ struct WitnessUnknown
* * WitnessV0ScriptHash: TX_WITNESS_V0_SCRIPTHASH destination (P2WSH)
* * WitnessV0KeyHash: TX_WITNESS_V0_KEYHASH destination (P2WPKH)
* * WitnessUnknown: TX_WITNESS_UNKNOWN destination (P2W???)
* * NullData: TX_NULL_DATA destination (OP_RETURN)
* A CTxDestination is the internal data type encoded in a bitcoin address
*/
typedef boost::variant<CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown> CTxDestination;
typedef boost::variant<CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown, NullData> CTxDestination;

/** Check whether a CTxDestination is a CNoDestination. */
bool IsValidDestination(const CTxDestination& dest);
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ void InitScriptExecutionCache() {
*
* Non-static (and re-declared) in src/test/txvalidationcache_tests.cpp
*/
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::set<std::pair<uint256, COutPoint>>& setPeginsSpent, std::vector<CScriptCheck> *pvChecks)
{
if (!tx.IsCoinBase())
{
Expand Down Expand Up @@ -2533,7 +2533,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
{
std::vector<CScriptCheck> vChecks;
bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : nullptr))
if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, fCacheResults, txdata[i], setPeginsSpent == NULL ? setPeginsSpentDummy : *setPeginsSpent, nScriptCheckThreads ? &vChecks : nullptr))
return error("ConnectBlock(): CheckInputs on %s failed with %s",
tx.GetHash().ToString(), FormatStateMessage(state));
control.Add(vChecks);
Expand Down
26 changes: 16 additions & 10 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ static UniValue getaddressesbyaccount(const JSONRPCRequest& request)
return ret;
}

static CTransactionRef SendMoney(CWallet * const pwallet, CScript scriptPubKey, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue, std::string fromAccount)
static CTransactionRef SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue, std::string fromAccount)
{
CAmount curBalance = pwallet->GetBalance();

Expand All @@ -484,6 +484,9 @@ static CTransactionRef SendMoney(CWallet * const pwallet, CScript scriptPubKey,
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
}

// Parse Bitcoin address
CScript scriptPubKey = GetScriptForDestination(address);

// Create and send the transaction
CReserveKey reservekey(pwallet);
CAmount nFeeRequired;
Expand Down Expand Up @@ -591,8 +594,7 @@ static UniValue sendtoaddress(const JSONRPCRequest& request)

EnsureWalletIsUnlocked(pwallet);

CScript scriptPubKey = GetScriptForDestination(dest);
CTransactionRef tx = SendMoney(pwallet, scriptPubKey, nAmount, fSubtractFeeFromAmount, coin_control, std::move(mapValue), {} /* fromAccount */);
CTransactionRef tx = SendMoney(pwallet, dest, nAmount, fSubtractFeeFromAmount, coin_control, std::move(mapValue), {} /* fromAccount */);
return tx->GetHash().GetHex();
}

Expand Down Expand Up @@ -1110,8 +1112,7 @@ static UniValue sendfrom(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");

CCoinControl no_coin_control; // This is a deprecated API
CScript scriptPubKey = GetScriptForDestination(dest);
CTransactionRef tx = SendMoney(pwallet, scriptPubKey, nAmount, false, no_coin_control, std::move(mapValue), std::move(strAccount));
CTransactionRef tx = SendMoney(pwallet, dest, nAmount, false, no_coin_control, std::move(mapValue), std::move(strAccount));
return tx->GetHash().GetHex();
}

Expand Down Expand Up @@ -4117,6 +4118,7 @@ class DescribeWalletAddressVisitor : public boost::static_visitor<UniValue>
}

UniValue operator()(const WitnessUnknown& id) const { return UniValue(UniValue::VOBJ); }
UniValue operator()(const NullData& id) const { return NullUniValue; }
};

static UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& dest)
Expand Down Expand Up @@ -4874,16 +4876,19 @@ UniValue sendtomainchain(const JSONRPCRequest& request)
uint256 genesisBlockHash = Params().ParentGenesisBlockHash();

// Asset type is implicit, no need to add to script
CScript scriptPubKey;
scriptPubKey << OP_RETURN;
scriptPubKey << std::vector<unsigned char>(genesisBlockHash.begin(), genesisBlockHash.end());
scriptPubKey << std::vector<unsigned char>(scriptPubKeyMainchain.begin(), scriptPubKeyMainchain.end());
NullData nulldata;
nulldata << std::vector<unsigned char>(genesisBlockHash.begin(), genesisBlockHash.end());
nulldata << std::vector<unsigned char>(scriptPubKeyMainchain.begin(), scriptPubKeyMainchain.end());
//TODO(stevenroose) remove
//nulldata.null_data.push_back(std::vector<unsigned char>(genesisBlockHash.begin(), genesisBlockHash.end()));
//nulldata.null_data.push_back(std::vector<unsigned char>(scriptPubKeyMainchain.begin(), scriptPubKeyMainchain.end()));
CTxDestination address(nulldata);

EnsureWalletIsUnlocked(pwallet);

mapValue_t mapValue;
CCoinControl no_coin_control; // This is a deprecated API
CTransactionRef tx = SendMoney(pwallet, scriptPubKey, nAmount, subtract_fee, no_coin_control, std::move(mapValue), {});
CTransactionRef tx = SendMoney(pwallet, address, nAmount, subtract_fee, no_coin_control, std::move(mapValue), {});

//TODO(rebase) CT/CA
// v this line was in elements-0.14 instead of the line above here
Expand Down Expand Up @@ -5200,6 +5205,7 @@ extern UniValue importprunedfunds(const JSONRPCRequest& request);
extern UniValue removeprunedfunds(const JSONRPCRequest& request);
extern UniValue importmulti(const JSONRPCRequest& request);
extern UniValue rescanblockchain(const JSONRPCRequest& request);
extern UniValue sendrawtransaction(const JSONRPCRequest& request);

static const CRPCCommand commands[] =
{ // category name actor (function) argNames
Expand Down

0 comments on commit 992d54b

Please sign in to comment.