Skip to content

Commit

Permalink
Merge pull request #49 from BlockMechanic/segwit
Browse files Browse the repository at this point in the history
Segwit
  • Loading branch information
BlockMechanic authored Mar 28, 2022
2 parents 20aba9c + 23e569c commit 26abb8c
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class CTestNetParams : public CChainParams {
fRequireStandard = false;
m_is_test_chain = true;
m_is_mockable_chain = false;
strSporkKey = "04EA9AF53E4F12CE41F78B666EBDBE96C966ABDD8832979228BD3299E13089F117936EF97B7B9D4644B8B9D2BC7A30029BD7FDDCAC36E40AAC0E03891E493CF197";
strSporkKey = "04fb22e1ca5304b7dd1e36dfdd03b61b82454573a02a86d875d98da840a33e86dc4709baf8aa2c45ccfcf31027451f6831dd080978fc90dbf411bf12fb15b60d7b";
strDevfundAddress = "mr59c3aniaN3qHXej5L8UBsssRZbiUUMnz";
strLegacySignerDummyAddress = "mr59c3aniaN3qHXej5L8UBsssRZbiUUMnz";
checkpointData = {
Expand Down
12 changes: 3 additions & 9 deletions src/crown/legacysigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,13 @@ bool CLegacySigner::SetCollateralAddress(std::string strAddress)

bool CLegacySigner::SetKey(std::string strSecret, CKey& key, CPubKey& pubkey)
{
//auto m_wallet = GetMainWallet();
//EnsureLegacyScriptPubKeyMan(*m_wallet, true);

key = DecodeSecret(strSecret);
if (!key.IsValid())
if (!key.IsValid()){
LogPrintf("CLegacySigner::SetKey - Failed Key is not valid\n");
return false;
}
pubkey = key.GetPubKey();

//auto spk_man = m_wallet->GetScriptPubKeyMan(GetScriptForDestination(PKHash(pubkey.GetID())));

//if(!spk_man)
// return false;

return true;
}

Expand Down
16 changes: 12 additions & 4 deletions src/crown/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ bool CSporkManager::Sign(CSporkMessage& spork)
CKey key2;
CPubKey pubkey2;
std::string errorMessage = "";

if (!legacySigner.SetKey(strMasterPrivKey, key2, pubkey2)) {
std::string privk = strMasterPrivKey;

if (privk == "" && gArgs.IsArgSet("-sporkkey")) // spork priv key
privk = gArgs.GetArg("-sporkkey", "");

if (!legacySigner.SetKey(privk, key2, pubkey2)) {
LogPrintf("CMasternodePayments::Sign - ERROR: Invalid masternodeprivkey: '%s'\n", errorMessage);
return false;
}
Expand Down Expand Up @@ -280,12 +284,16 @@ void CSporkManager::Relay(CSporkMessage& msg)
bool CSporkManager::SetPrivKey(std::string strPrivKey)
{
CSporkMessage msg;
LogPrintf("CSporkManager::SetPrivKey - setting private key %s\n", strPrivKey);

// Test signing successful, proceed
strMasterPrivKey = strPrivKey;

Sign(msg);

if(!Sign(msg)){
LogPrintf("CSporkManager::SetPrivKey - Failed to Sign\n");
return false;
}

if (CheckSignature(msg)) {
LogPrintf("CSporkManager::SetPrivKey - Successfully initialized as spork signer\n");
return true;
Expand Down
8 changes: 7 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ void SetupServerArgs(NodeContext& node)
argsman.AddArg("-systemnode", "Run as systemnode", false, OptionsCategory::RPC);
argsman.AddArg("-systemnodeprivkey", "Systemnode private key", false, OptionsCategory::RPC);
argsman.AddArg("-systemnodeaddr", strprintf(_("Set external address:port to get to this systemnode (example: %s)").translated, "1.2.3.4:12345"), false, OptionsCategory::RPC);

argsman.AddArg("-sporkkey", strprintf(_("Set spork key (example: %s)").translated, "xxxxxxxxxxxxxxxxxxxxxx"), false, OptionsCategory::RPC);

#if HAVE_DECL_DAEMON
argsman.AddArg("-daemon", "Run in the background as a daemon and accept commands", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#else
Expand Down Expand Up @@ -1406,6 +1407,11 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
#if ENABLE_ZMQ
RegisterZMQRPCCommands(tableRPC);
#endif
if (args.IsArgSet("-sporkkey")) // spork priv key
{
if (!sporkManager.SetPrivKey(args.GetArg("-sporkkey", "")))
return InitError(_("Unable to sign spork message, wrong key?"));
}

/* Start the RPC server already. It will be started in "warmup" mode
* and not really process calls already (but it will signify connections
Expand Down
7 changes: 1 addition & 6 deletions src/masternode/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,12 @@ void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFe
CAmount blockValue = GetBlockSubsidy(pindexPrev->nHeight, Params().GetConsensus());
CAmount masternodePayment = GetMasternodePayment(pindexPrev->nHeight+1, blockValue);

if(txNew.nVersion >= TX_ELE_VERSION)
txNew.vpout[0].nValue = blockValue;
else
txNew.vout[0].nValue = blockValue;


if(hasPayment){
if(txNew.nVersion >= TX_ELE_VERSION){
txNew.vpout.resize(2);
txNew.vpout[MN_PMT_SLOT].scriptPubKey = payee;
txNew.vpout[MN_PMT_SLOT].nValue = masternodePayment;
txNew.vpout[MN_PMT_SLOT].nAsset = txNew.vpout[0].nAsset;
txNew.vpout[0].nValue -= masternodePayment;
}else{
txNew.vout.resize(2);
Expand Down
11 changes: 5 additions & 6 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
coinbaseTx.vpout[0].nAsset = Params().GetConsensus().subsidy_asset;
}

pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
pblocktemplate->vTxFees[0] = -nFees;


if (fProofOfStake && ::ChainActive().Height() + 1 >= Params().PoSStartHeight()) {
pblock->nTime = GetAdjustedTime();
CBlockIndex* pindexPrev = ::ChainActive().Tip();
Expand Down Expand Up @@ -218,7 +213,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}

// Masternode and general budget payments
if (IsSporkActive(SPORK_4_ENABLE_MASTERNODE_PAYMENTS)) {
if (IsSporkActive(SPORK_4_ENABLE_MASTERNODE_PAYMENTS) || Params().NetworkIDString() == CBaseChainParams::TESTNET) {
FillBlockPayee(coinbaseTx, nFees);
SNFillBlockPayee(coinbaseTx, nFees);
}
Expand Down Expand Up @@ -252,6 +247,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->payeeSN = coinbaseTx.vout[SN_PMT_SLOT].scriptPubKey;
}

pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
pblocktemplate->vTxFees[0] = -nFees;

LogPrintf("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n %s\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost, pblock->ToString());

// Fill in header
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "unloadwallet", 1, "load_on_startup"},
{ "getnodeaddresses", 0, "count"},
{ "addpeeraddress", 1, "port"},
{ "spork", 1, "value" },
{ "stop", 0, "wait" }
};
// clang-format on
Expand Down
144 changes: 144 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <crown/nodewallet.h>
#include <httpserver.h>
#include <index/blockfilterindex.h>
#include <index/txindex.h>
#include <interfaces/chain.h>
#include <key_io.h>
#include <masternode/masternode-sync.h>
#include <systemnode/systemnode-sync.h>
#include <node/context.h>
#include <outputtype.h>
#include <rpc/blockchain.h>
Expand All @@ -20,6 +23,7 @@
#include <util/ref.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <crown/spork.h>

#include <stdint.h>
#include <tuple>
Expand Down Expand Up @@ -696,6 +700,142 @@ static RPCHelpMan getindexinfo()
};
}

static RPCHelpMan getstakepointers()
{
return RPCHelpMan{"getstakepointers",
"\nReturns current stake pointers\n",
{},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{
HelpExampleCli("getstakepointers", "")
+ HelpExampleRpc("getstakepointers", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
std::vector<StakePointer> vStakePointers;
currentNode.GetRecentStakePointers(vStakePointers);

UniValue ret(UniValue::VARR);

for (auto p : vStakePointers) {
UniValue obj(UniValue::VOBJ);
obj.pushKV("blockhash", p.hashBlock.GetHex());
obj.pushKV("hashpubkey", p.pubKeyProofOfStake.GetID().GetHex());
obj.pushKV("pos", (int64_t)p.nPos);
obj.pushKV("txid", p.txid.GetHex());
ret.push_back(obj);
}

return ret;
},
};
}

static RPCHelpMan mnsync()
{
return RPCHelpMan{"mnsync",
{"\nReturns the sync status, updates to the next step or resets it entirely.\n"},
{
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "The command to issue (status|next|reset)"},
},
RPCResult{RPCResult::Type::STR, "result", "Result"},
RPCExamples{
HelpExampleCli("mnsync", "status")
+ HelpExampleRpc("mnsync", "status")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{

NodeContext& node = EnsureNodeContext(request.context);
std::string strMode = request.params[0].get_str();

if(strMode == "status") {
UniValue objStatus(UniValue::VOBJ);
objStatus.pushKV("IsBlockchainSynced", masternodeSync.IsBlockchainSynced() && systemnodeSync.IsBlockchainSynced());
objStatus.pushKV("IsSynced", masternodeSync.IsSynced() && systemnodeSync.IsSynced());
return objStatus;
}

if(strMode == "next")
{
++masternodeSync.RequestedMasternodeAssets;
++systemnodeSync.RequestedSystemnodeAssets;
return "success";
}

if(strMode == "reset")
{
masternodeSync.Reset();
systemnodeSync.Reset();
return "success";
}
return "failure";
},
};
}

static RPCHelpMan spork()
{
return RPCHelpMan{"spork",
"\nEnable/Disable sporks or show info\n",
{
{"mode", RPCArg::Type::STR, RPCArg::Optional::NO, "spork mode"},
{"value", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "spork mode [<value>]"},
},
RPCResult{
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::BOOL, "isvalid", "If the address is valid or not. If not, this is the only property returned."},
{RPCResult::Type::STR, "address", "The crown address validated"},
{RPCResult::Type::STR_HEX, "scriptPubKey", "The hex-encoded scriptPubKey generated by the address"},
{RPCResult::Type::BOOL, "isscript", "If the key is a script"},
{RPCResult::Type::BOOL, "iswitness", "If the address is a witness address"},
{RPCResult::Type::NUM, "witness_version", /* optional */ true, "The version number of the witness program"},
{RPCResult::Type::STR_HEX, "witness_program", /* optional */ true, "The hex value of the witness program"},
}
},
RPCExamples{
HelpExampleCli("spork", "show") +
HelpExampleRpc("spork", "active")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
UniValue ret(UniValue::VOBJ);

if(request.params.size() == 1 && request.params[0].get_str() == "show"){
for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){
if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown")
ret.pushKV(sporkManager.GetSporkNameByID(nSporkID), GetSporkValue(nSporkID));
}
} else if(request.params.size() == 1 && request.params[0].get_str() == "active"){
for(int nSporkID = SPORK_START; nSporkID <= SPORK_END; nSporkID++){
if(sporkManager.GetSporkNameByID(nSporkID) != "Unknown")
ret.pushKV(sporkManager.GetSporkNameByID(nSporkID), IsSporkActive(nSporkID));
}
} else if (request.params.size() == 2){
std::string name = request.params[0].get_str();
int nSporkID = sporkManager.GetSporkIDByName(name);
if(nSporkID == -1){
return "Invalid spork name";
}

// SPORK VALUE
int64_t nValue = request.params[1].get_int64();

//broadcast new spork
if(sporkManager.UpdateSpork(nSporkID, nValue)){
ExecuteSpork(nSporkID, nValue);
ret.pushKV("result", "success");
} else {
ret.pushKV("result", "failure");
}
}
return ret;

},
};
}

void RegisterMiscRPCCommands(CRPCTable &t)
{
// clang-format off
Expand All @@ -711,6 +851,10 @@ static const CRPCCommand commands[] =
{ "util", "verifymessage", &verifymessage, {"address","signature","message"} },
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
{ "util", "getindexinfo", &getindexinfo, {"index_name"} },
{ "util", "getstakepointers", &getstakepointers, {} },

{ "crown", "mnsync", &mnsync, {"command"} },
{ "crown", "spork", &spork, {"command"} },

/* Not shown in help */
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
Expand Down
1 change: 1 addition & 0 deletions src/systemnode/systemnode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ void CSystemnodePayments::FillBlockPayee(CMutableTransaction& txNew, int64_t nFe
// [0] is for miner, [1] masternode, [2] systemnode
txNew.vpout[2].scriptPubKey = payee;
txNew.vpout[2].nValue = systemnodePayment;
txNew.vpout[2].nAsset = txNew.vpout[0].nAsset;
txNew.vpout[0].nValue -= systemnodePayment;
}
else{
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,9 @@ void CWallet::AvailableCoins(std::vector<COutput>& vCoins, bool fOnlySafe, const
continue;
}

if (out.nValue == 10000*COIN || out.nValue == 500*COIN)
continue;

std::unique_ptr<SigningProvider> provider = GetSolvingProvider(out.scriptPubKey);

bool solvable = provider ? IsSolvable(*provider, out.scriptPubKey) : false;
Expand Down
1 change: 0 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
bool GetActiveSystemnode(CSystemnode*& activeStakingNode);
uint256 GenerateStakeModifier(const CBlockIndex* prewardBlockIndex) const;
bool CreateCoinStake(const int nHeight, const uint32_t& nBits, const uint32_t& nTime, CMutableTransaction& txCoinStake, uint32_t& nTxNewTime, StakePointer& stakePointer);
bool GetRecentStakePointers(std::vector<StakePointer>& vStakePointers);

void Stake(bool fStake);
};
Expand Down

0 comments on commit 26abb8c

Please sign in to comment.