Skip to content

Commit

Permalink
Block v13 checks and corrections to sidestake contract validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 1, 2023
1 parent 522f453 commit c2ca64c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
8 changes: 2 additions & 6 deletions src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,11 @@ void SideStakeRegistry::Revert(const ContractContext& ctx)

bool SideStakeRegistry::Validate(const Contract& contract, const CTransaction& tx, int &DoS) const
{
if (contract.m_version < 1) {
return true;
}

const auto payload = contract.SharePayloadAs<SideStakePayload>();

if (contract.m_version >= 3 && payload->m_version < 2) {
if (contract.m_version < 3) {
DoS = 25;
error("%s: Legacy SideStake entry contract in contract v3", __func__);
error("%s: Sidestake entries only valid in contract v3 and above", __func__);
return false;
}

Expand Down
43 changes: 24 additions & 19 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2437,30 +2437,35 @@ UniValue addkey(const UniValue& params, bool fHelp)
break;
case GRC::ContractType::SIDESTAKE:
{
GRC::CBitcoinAddressForStorage sidestake_address;
if (!sidestake_address.SetString(params[2].get_str())) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address specified for the sidestake is invalid.");
}
if (block_v13_enabled) {
GRC::CBitcoinAddressForStorage sidestake_address;
if (!sidestake_address.SetString(params[2].get_str())) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Address specified for the sidestake is invalid.");
}

double allocation = 0.0;
if (!ParseDouble(params[3].get_str(), &allocation)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid allocation specified.");
}
double allocation = 0.0;
if (!ParseDouble(params[3].get_str(), &allocation)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid allocation specified.");
}

allocation /= 100.0;
allocation /= 100.0;

if (allocation > 1.0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Allocation specified is greater than 100.0%.");
if (allocation > 1.0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Allocation specified is greater than 100.0%.");
}

contract = GRC::MakeContract<GRC::SideStakePayload>(
contract_version, // Contract version number (3+)
action, // Contract action
uint32_t {1}, // Contract payload version number
sidestake_address, // Sidestake address
allocation, // Sidestake allocation
GRC::SideStakeStatus::MANDATORY // sidestake status
);
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Sidestake contracts are not valid for block version less than v13.");
}

contract = GRC::MakeContract<GRC::SideStakePayload>(
contract_version, // Contract version number
action, // Contract action
uint32_t {1}, // Contract payload version number
sidestake_address, // Sidestake address
allocation, // Sidestake allocation
GRC::SideStakeStatus::MANDATORY // sidestake status
);
break;
}
case GRC::ContractType::BEACON:
Expand Down

0 comments on commit c2ca64c

Please sign in to comment.