Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom reward address to createmasternode RPC #1941

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
node.creationHeight = height;
node.operatorType = obj.operatorType;
node.operatorAuthAddress = obj.operatorAuthAddress;
if (height >= static_cast<uint32_t>(consensus.NextNetworkUpgradeHeight)) {
node.rewardAddressType = obj.rewardType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CMasternodesView::CreateMasternode add checks that the type is either NoDestType, PKHashType, ScriptHashType or WitV0KeyHashType, so 0, 1, 2 or 4.

node.rewardAddress = obj.rewardAddress;
}

// Set masternode version2 after FC for new serialisation
if (height >= static_cast<uint32_t>(consensus.FortCanningHeight))
Expand Down
6 changes: 6 additions & 0 deletions src/masternodes/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ struct CCreateMasterNodeMessage {
char operatorType;
CKeyID operatorAuthAddress;
uint16_t timelock{0};
char rewardType;
CKeyID rewardAddress;

ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
Expand All @@ -255,6 +257,10 @@ struct CCreateMasterNodeMessage {
if (!s.eof()) {
READWRITE(timelock);
}
if (!s.eof()) {
READWRITE(rewardType);
READWRITE(rewardAddress);
}
}
};

Expand Down
18 changes: 18 additions & 0 deletions src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ UniValue createmasternode(const JSONRPCRequest& request)
"specify either FIVEYEARTIMELOCK or TENYEARTIMELOCK to create a masternode that cannot be resigned for\n"
"five or ten years and will have 1.5x or 2.0 the staking power respectively. Be aware that this means\n"
"that you cannot spend the collateral used to create a masternode for whatever period is specified."},
{"rewardAddress", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Masternode`s reward address"},
},
RPCResult{
"\"hash\" (string) The hex-encoded hash of broadcasted transaction\n"
Expand All @@ -133,15 +134,23 @@ UniValue createmasternode(const JSONRPCRequest& request)

std::string ownerAddress = request.params[0].getValStr();
std::string operatorAddress = request.params.size() > 1 && !request.params[1].getValStr().empty() ? request.params[1].getValStr() : ownerAddress;
std::string rewardAddress = !request.params[4].getValStr().empty() ? request.params[4].getValStr() : ownerAddress;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave empty if no reward is set, so just get an empty string here, dest will be 0 which is the toggle as to whether to use the reward address or not.

CTxDestination ownerDest = DecodeDestination(ownerAddress); // type will be checked on apply/create
CTxDestination operatorDest = DecodeDestination(operatorAddress);
CTxDestination rewardDest = DecodeDestination(rewardAddress);

bool eunosPaya;
{
LOCK(cs_main);
eunosPaya = ::ChainActive().Tip()->nHeight >= Params().GetConsensus().EunosPayaHeight;
}

bool nextNetworkUpdate;
{
LOCK(cs_main);
nextNetworkUpdate = ::ChainActive().Tip()->nHeight >= Params().GetConsensus().NextNetworkUpgradeHeight;
}

// Get timelock if any
uint16_t timelock{0};
if (!request.params[3].isNull()) {
Expand All @@ -165,7 +174,12 @@ UniValue createmasternode(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Address (%s) is not owned by the wallet", EncodeDestination(ownerDest)));
}

if (rewardDest.index() != 1 && rewardDest.index() != 4) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this check and let it happen on the consensus side. Reward will include type 2 as well on the next hard fork due to PR 1664.

throw JSONRPCError(RPC_INVALID_PARAMETER, "rewardAddress (" + rewardAddress + ") does not refer to a P2PKH or P2WPKH address");
}

CKeyID const operatorAuthKey = operatorDest.index() == 1 ? CKeyID(std::get<PKHash>(operatorDest)) : CKeyID(std::get<WitnessV0KeyHash>(operatorDest));
CKeyID const rewardAuthKey = rewardDest.index() == 1 ? CKeyID(std::get<PKHash>(rewardDest)) : CKeyID(std::get<WitnessV0KeyHash>(rewardDest));

CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::CreateMasternode)
Expand All @@ -175,6 +189,10 @@ UniValue createmasternode(const JSONRPCRequest& request)
metadata << timelock;
}

if (nextNetworkUpdate) {
metadata << static_cast<char>(rewardDest.index()) << rewardAuthKey;
}

CScript scriptMeta;
scriptMeta << OP_RETURN << ToByteVector(metadata);

Expand Down
64 changes: 64 additions & 0 deletions test/functional/rpc_mn_create_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""Test the masternodes create RPC.

- verify MN creation with custom reward address and resign
"""

from test_framework.test_framework import DefiTestFramework

from test_framework.authproxy import JSONRPCException
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)


class MasternodesRpcCreateRewardTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=50', '-dakotaheight=120',
'-eunosheight=120', '-eunospayaheight=120', '-fortcanningheight=120', '-fortcanninghillheight=122',
'-grandcentralheight=189', "-nextnetworkupgradeheight=120"],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=50', '-dakotaheight=120',
'-eunosheight=120', '-eunospayaheight=120', '-fortcanningheight=120', '-fortcanninghillheight=122',
'-grandcentralheight=189', '-txindex=1', "-nextnetworkupgradeheight=120"]]

def run_test(self):
assert_equal(len(self.nodes[0].listmasternodes()), 8)
self.nodes[0].generate(100)
self.sync_blocks()

collateral0 = self.nodes[0].getnewaddress("", "legacy")

# Fail to create: Insufficient funds (not matured coins)
try:
idnode0 = self.nodes[0].createmasternode(
collateral0
)
except JSONRPCException as e:
errorString = e.error['message']
assert ("Insufficient funds" in errorString)

# Create node0
self.nodes[0].generate(20)
collateral1 = self.nodes[1].getnewaddress("", "legacy")
reward = self.nodes[1].getnewaddress("reward", "legacy")
assert_raises_rpc_error(-8, "Address ({}) is not owned by the wallet".format(collateral1),
self.nodes[0].createmasternode, collateral1)

# Fail to create: Wrong reward address
try:
self.nodes[0].createmasternode(collateral0, '', [], 'TENYEARTIMELOCK', "test")
except JSONRPCException as e:
errorString = e.error['message']
assert ("does not refer to a P2PKH or P2WPKH address" in errorString)

idnode0 = self.nodes[0].createmasternode(collateral0, '', [], 'TENYEARTIMELOCK', reward)
self.nodes[0].generate(1)
assert_equal(self.nodes[0].listmasternodes()[idnode0]['rewardAddress'], reward)


if __name__ == '__main__':
MasternodesRpcCreateRewardTest().main()