Skip to content

Commit

Permalink
Updated the code to generate blocks for the given address. (#646)
Browse files Browse the repository at this point in the history
* Updated the code to generate blocks for the given address.
* Updated code and updated tests.
* Refactored code.
* Added getCKeyIDFromDestination().

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
surangap and prasannavl authored Aug 4, 2021
1 parent 557c3d8 commit e23fb88
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,11 @@ bool IsValidDestinationString(const std::string& str)
{
return IsValidDestinationString(str, Params());
}

CKeyID getCKeyIDFromDestination(const CTxDestination& dest) {
switch (dest.which()) {
case PKHashType : return CKeyID(*boost::get<PKHash>(&dest));
case WitV0KeyHashType : return CKeyID(*boost::get<WitnessV0KeyHash>(&dest));
default : return CKeyID();
}
}
2 changes: 2 additions & 0 deletions src/key_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
bool IsValidDestinationString(const std::string& str);
bool IsValidDestinationString(const std::string& str, const CChainParams& params);

CKeyID getCKeyIDFromDestination(const CTxDestination& dest);

#endif // DEFI_KEY_IO_H
22 changes: 16 additions & 6 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,23 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
}

auto myIDs = pcustomcsview->AmIOperator();
if (!myIDs) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: I am not masternode operator");
CKeyID passedID = getCKeyIDFromDestination(destination);

auto myAllMNs = pcustomcsview->GetOperatorsMulti();
if (myAllMNs.empty()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: I am not masternode operator");
}

CScript coinbase_script = GetScriptForDestination(destination);
CKeyID operatorID;
auto mnForPassedID = pcustomcsview->GetMasternodeIdByOperator(passedID);
// check mnForPassedID is in myAllMNs
if (mnForPassedID && myAllMNs.count(std::make_pair(passedID, *mnForPassedID))) {
operatorID = passedID;
} else {
operatorID = myAllMNs.begin()->first;
}

CScript coinbase_script = GetScriptForDestination(destination);
CKey minterKey;
{
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
Expand All @@ -205,7 +215,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)

bool found =false;
for (auto&& wallet : wallets) {
if (wallet->GetKey(myIDs->first, minterKey)) {
if (wallet->GetKey(operatorID, minterKey)) {
found = true;
break;
}
Expand All @@ -214,7 +224,7 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: masternode operator private key not found");

}
return generateBlocks(coinbase_script, minterKey, myIDs->first, nGenerate, nMaxTries);
return generateBlocks(coinbase_script, minterKey, operatorID, nGenerate, nMaxTries);
}

// Returns the mining information of all local masternodes
Expand Down
12 changes: 9 additions & 3 deletions test/functional/rpc_getmininginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from test_framework.test_framework import DefiTestFramework
from test_framework.util import (
assert_equal,
assert_greater_than,
connect_nodes_bi,
)

Expand All @@ -32,16 +33,17 @@ def run_test(self):
operators = [node0_keys.operatorAuthAddress, node1_keys.operatorAuthAddress]

self.log.info("Restart nodes...")
self.restart_node(0, ['-gen', '-masternode_operator=' + operators[0]])
self.restart_node(1, ['-gen', '-rewardaddress=' + operators[1]] +
self.restart_node(0, ['-gen', '-dummypos=0', '-masternode_operator=' + operators[0]])
self.restart_node(1, ['-gen', '-dummypos=0', '-rewardaddress=' + operators[1]] +
['-masternode_operator=' + x for x in operators])

connect_nodes_bi(self.nodes, 0, 1)

self.log.info("Mining blocks ...")
self.nodes[0].generate(10)
self.sync_all()
self.nodes[1].generate(50)
self.nodes[1].generate(25)
self.nodes[1].generatetoaddress(25, node0_keys.operatorAuthAddress)
self.sync_all()

# getmininginfo() on node[0], should only return one master node in the response array
Expand All @@ -50,17 +52,21 @@ def run_test(self):
assert_equal(resp0['masternodes'][0]['state'], "ENABLED")
assert_equal(resp0['masternodes'][0]['generate'], True)
assert_equal(resp0['masternodes'][0]['lastblockcreationattempt'] != "0", True)
assert_greater_than(resp0['masternodes'][0]['mintedblocks'], 0)

# getmininginfo() on node[1], should return two master nodes in the response array
resp1 = self.nodes[1].getmininginfo()
assert_equal(len(resp1['masternodes']), 2)
assert_equal(resp1['masternodes'][0]['state'], "ENABLED")
assert_equal(resp1['masternodes'][0]['generate'], True)
assert_equal(resp1['masternodes'][0]['lastblockcreationattempt'] != "0", True)
assert_greater_than(resp1['masternodes'][0]['mintedblocks'], 0)


assert_equal(resp1['masternodes'][1]['state'], "ENABLED")
assert_equal(resp1['masternodes'][1]['generate'], True)
assert_equal(resp1['masternodes'][1]['lastblockcreationattempt'] != "0", True)
assert_greater_than(resp1['masternodes'][1]['mintedblocks'], 0)

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

0 comments on commit e23fb88

Please sign in to comment.