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 all 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
3 changes: 3 additions & 0 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ Res CMasternodesView::CreateMasternode(const uint256 &nodeId, const CMasternode
return Res::Err(
"bad owner and|or operator address (should be P2PKH or P2WPKH only) or node with those addresses exists");
}
if (node.rewardAddressType != 0 && node.rewardAddressType != 1 && node.rewardAddressType != 2 && node.rewardAddressType != 4) {
return Res::Err("bad reward address");
}

WriteBy<ID>(nodeId, node);
WriteBy<Owner>(node.ownerAuthAddress, nodeId);
Expand Down
6 changes: 6 additions & 0 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,12 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
node.creationHeight = height;
node.operatorType = obj.operatorType;
node.operatorAuthAddress = obj.operatorAuthAddress;
if (height >= static_cast<uint32_t>(consensus.NextNetworkUpgradeHeight)) {
if (obj.rewardType == NoDestType || obj.rewardType == PKHashType || obj.rewardType == ScriptHashType || obj.rewardType == WitV0KeyHashType) {
node.rewardAddressType = obj.rewardType;
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 @@ -251,6 +251,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 @@ -262,6 +264,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 @@ -107,6 +107,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 @@ -131,15 +132,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();
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 @@ -163,7 +172,12 @@ UniValue createmasternode(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Address (%s) is not owned by the wallet", EncodeDestination(ownerDest)));
}

if (!rewardAddress.empty() && rewardDest.index() != 1 && rewardDest.index() != 2 && rewardDest.index() != 4) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "rewardAddress (" + rewardAddress + ") does not refer to a P2PKH, BECH32 or P2WPKH address");
}

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

CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::CreateMasternode)
Expand All @@ -173,6 +187,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
61 changes: 61 additions & 0 deletions test/functional/rpc_mn_create_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/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.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)
assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[0].createmasternode, collateral0)

# 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 format
assert_raises_rpc_error(-8, "does not refer to a P2PKH, BECH32 or P2WPKH address", self.nodes[0].createmasternode, collateral0, '', [], 'TENYEARTIMELOCK', self.nodes[0].getnewaddress("", "eth"))

# Fail to create: Invalid reward address
assert_raises_rpc_error(-8, "does not refer to a P2PKH, BECH32 or P2WPKH address", self.nodes[0].createmasternode, collateral0, '', [], 'TENYEARTIMELOCK', "test")

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

# Should create: masternode without reward address
idnode1 = self.nodes[0].createmasternode(self.nodes[0].getnewaddress("", "legacy"))
self.nodes[0].generate(1)
assert_equal(self.nodes[0].listmasternodes()[idnode1]['rewardAddress'], '')


if __name__ == '__main__':
MasternodesRpcCreateRewardTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
'rpc_getchaintips.py',
'rpc_misc.py',
'rpc_mn_basic.py',
'rpc_mn_create_reward.py',
'feature_smart_contracts.py',
'feature_reject_customtxs.py',
'feature_initdist.py',
Expand Down
Loading