-
Notifications
You must be signed in to change notification settings - Fork 129
rpc: votegov: Add support for owner and operator address#1717
rpc: votegov: Add support for owner and operator address#1717Bushstar merged 14 commits intoDeFiCh:masterfrom DocteurPing:feat/vote_with_address_for_proposal
Conversation
Summary
Enables support for address support on votegov to vote with an address in addition to the currently accepted masternode ID.
How
- Over the wire, TX still uses masternode ID.
- The RPC API now auto selects the masternode ID based on the address provided.
- The selection order:
- MasternodeID
- OwnerAdress
- OperatorAddress
Limitations
- Throws an error when there is no reasonable choice available (invalid address/masternodeId, address does not own/operate a masternode).
src/masternodes/rpc_proposals.cpp
Outdated
| auto propId = ParseHashV(request.params[0].get_str(), "proposalId"); | ||
| auto mnId = ParseHashV(request.params[1].get_str(), "masternodeId"); | ||
| std::string id = request.params[1].get_str(); | ||
| uint256 mnId = uint256(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you only need as uint256 will default initialise itself as the class has a default constructor.
uint256 mnId;
src/masternodes/rpc_proposals.cpp
Outdated
| auto node = view.GetMasternode(mnId); | ||
| if (!node) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("The masternode %s does not exist", mnId.ToString())); | ||
| if (id.length() == 34) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Address can be 26-35 chars long for P2PKH and 42 for P2WPKH. I'd work on getting the correct mnId before calling GetMasternode.
src/masternodes/rpc_proposals.cpp
Outdated
| if (!node) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("The masternode %s does not exist", mnId.ToString())); | ||
| if (id.length() == 34) { | ||
| CTxDestination dest = DecodeDestination(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to call IsValidDestination on dest to make sure it is valid. You can do this in the else statement.
if (id.length() == 64) {
mnId = ParseHashV(id, "masternodeId");
} else {
CTxDestination dest = DecodeDestination(id);
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
strprintf("The masternode id or address is not valid: %s", id));
}
// Call GetMasternodeIdByOwner and GetMasternodeIdByOperator here.
}
src/masternodes/rpc_proposals.cpp
Outdated
|
const CKeyID ckeyId = dest.index() == PKHashType ? CKeyID(std::get |
||
| auto masterNodeIdByOwner = view.GetMasternodeIdByOwner(ckeyId); | ||
| if (!masterNodeIdByOwner) { | ||
| auto masterNodeIdByOperator = view.GetMasternodeIdByOperator(ckeyId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check that masterNodeIdByOperator is valid or you will get a std::bad_optional_access thrown on masterNodeIdByOperator.value(). I'd structure it like the following:
if (auto masterNodeIdByOwner = view.GetMasternodeIdByOwner(ckeyId)) {
mnId = masterNodeIdByOwner.value();
} else if (auto masterNodeIdByOperator = view.GetMasternodeIdByOperator(ckeyId)) {
mnId = masterNodeIdByOperator.value();
}
src/masternodes/rpc_proposals.cpp
Outdated
| throw JSONRPCError(RPC_INVALID_PARAMETER, | ||
| strprintf("The masternode id or address is not valid: %s", id)); | ||
| } | ||
|
const CKeyID ckeyId = dest.index() == PKHashType ? CKeyID(std::get |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to make sure that the provided address is either P2PKH or a P2WPKH, otherwise if someone passes a different variant then std::get will throw a std::bad_variant_access.
const CKeyID ckeyId = dest.index() == PKHashType ? CKeyID(std::get(dest))
: dest.index() == WitV0KeyHashType ? CKeyID(std::get(dest))
: CKeyID();
src/masternodes/rpc_proposals.cpp
Outdated
| throw JSONRPCError(RPC_INVALID_PARAMETER, | ||
| strprintf("The masternode id or address is not valid: %s", id)); | ||
| } | ||
| const CKeyID ckeyId = dest.index() == PKHashType ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit too hard to read IMO, better to break it down into ifs.