Skip to content

Commit

Permalink
Validation: Remove CheckBlockSignature now unneeded enableP2PKH flag.
Browse files Browse the repository at this point in the history
It was only needed for the activation period, now that it's fully enforced, can be removed.
The activation time isn't needed, v5 enforcement checkpoint is ensuring that the chain cannot reorg to a time in which P2PKH stakes weren't enabled.

Github-Pull: #2295
Rebased-From: 5aa3600
  • Loading branch information
furszy committed Apr 9, 2021
1 parent 7f244b1 commit a78dfbe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 34 deletions.
9 changes: 1 addition & 8 deletions src/blocksignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool SignBlock(CBlock& block, const CKeyStore& keystore)
return SignBlockWithKey(block, key);
}

bool CheckBlockSignature(const CBlock& block, const bool enableP2PKH)
bool CheckBlockSignature(const CBlock& block)
{
if (block.IsProofOfWork())
return block.vchBlockSig.empty();
Expand All @@ -64,13 +64,6 @@ bool CheckBlockSignature(const CBlock& block, const bool enableP2PKH)
if (!Solver(txout.scriptPubKey, whichType, vSolutions))
return false;

if (!enableP2PKH) {
// Before v5 activation, P2PKH was always failing.
if (whichType == TX_PUBKEYHASH) {
return false;
}
}

if (whichType == TX_PUBKEY) {
valtype& vchPubKey = vSolutions[0];
pubkey = CPubKey(vchPubKey);
Expand Down
2 changes: 1 addition & 1 deletion src/blocksignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

bool SignBlockWithKey(CBlock& block, const CKey& key);
bool SignBlock(CBlock& block, const CKeyStore& keystore);
bool CheckBlockSignature(const CBlock& block, const bool enableP2PKH);
bool CheckBlockSignature(const CBlock& block);

#endif //PIVX_BLOCKSIGNATURE_H
27 changes: 7 additions & 20 deletions src/test/main_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ CBlock CreateDummyBlockWithSignature(CKey stakingKey, BlockSignatureType type, b
return block;
}

bool TestBlockSignaturePreEnforcementV5(const CBlock& block)
bool TestBlockSignature(const CBlock& block)
{
return CheckBlockSignature(block, false);
}

bool TestBlockSignaturePostEnforcementV5(const CBlock& block)
{
return CheckBlockSignature(block, true);
return CheckBlockSignature(block);
}

BOOST_AUTO_TEST_CASE(block_signature_test)
Expand All @@ -89,27 +84,19 @@ BOOST_AUTO_TEST_CASE(block_signature_test)
stakingKey.MakeNewKey(true);
bool useInputP2PK = i % 2 == 0;

// Test P2PK block signature pre enforcement.
// Test P2PK block signature
CBlock block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PK, useInputP2PK);
BOOST_CHECK(TestBlockSignaturePreEnforcementV5(block));

// Test P2PK block signature post enforcement
block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PK, useInputP2PK);
BOOST_CHECK(TestBlockSignaturePostEnforcementV5(block));

// Test P2PKH block signature pre enforcement ---> must fail.
block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PKH, useInputP2PK);
BOOST_CHECK(!TestBlockSignaturePreEnforcementV5(block));
BOOST_CHECK(TestBlockSignature(block));

// Test P2PKH block signature post enforcement
// Test P2PKH block signature
block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PKH, useInputP2PK);
if (useInputP2PK) {
// If it's using a P2PK scriptsig as input and a P2PKH output
// The block doesn't contain the public key to verify the sig anywhere.
// Must fail.
BOOST_CHECK(!TestBlockSignaturePostEnforcementV5(block));
BOOST_CHECK(!TestBlockSignature(block));
} else {
BOOST_CHECK(TestBlockSignaturePostEnforcementV5(block));
BOOST_CHECK(TestBlockSignature(block));
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3263,15 +3263,12 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_pt
// Preliminary checks
int64_t nStartTime = GetTimeMillis();
const Consensus::Params& consensus = Params().GetConsensus();
int newHeight = 0;

// check block
bool checked = CheckBlock(*pblock, state);

// For now, we need the tip to know whether p2pkh block signatures are accepted or not.
// After 5.0, this can be removed and replaced by the enforcement block time.
const int newHeight = chainActive.Height() + 1;
const bool enableP2PKH = consensus.NetworkUpgradeActive(newHeight, Consensus::UPGRADE_V5_0);
if (!CheckBlockSignature(*pblock, enableP2PKH))
if (!CheckBlockSignature(*pblock))
return error("%s : bad proof-of-stake block signature", __func__);

if (pblock->GetHash() != consensus.hashGenesisBlock && pfrom != NULL) {
Expand All @@ -3298,6 +3295,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_pt
if (!ret) {
return error("%s : AcceptBlock FAILED", __func__);
}
newHeight = pindex->nHeight;
}

if (!ActivateBestChain(state, pblock, checked))
Expand Down

0 comments on commit a78dfbe

Please sign in to comment.