-
Notifications
You must be signed in to change notification settings - Fork 49
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
Tpos signature hf #154
Tpos signature hf #154
Changes from 16 commits
60e7bdb
a314026
a6bcdf5
e4e5350
ffa6720
f6ab52e
5ee0562
9bc66b8
36b2e9d
7a902f9
70e6b6f
753df43
71a7fd8
a830381
d96b9fc
c2959fb
e2e0ff2
1775be7
7c61da6
9944895
307d51f
7247ba9
55478d1
7b54eb7
31297af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,19 @@ static CPubKey::InputScriptType GetScriptTypeFromDestination(const CTxDestinatio | |
return CPubKey::InputScriptType::SPENDUNKNOWN; | ||
} | ||
|
||
CBlockSigner::CBlockSigner(CBlock &block, const CKeyStore *keystore, const TPoSContract &contract) : | ||
CBlockSigner::CBlockSigner(CBlock &block, const CKeyStore *keystore, const TPoSContract &contract, int chainHeight) : | ||
refBlock(block), | ||
refKeystore(keystore), | ||
refContract(contract) | ||
refContract(contract), | ||
nChainHeight(chainHeight) | ||
{ | ||
|
||
} | ||
|
||
bool CBlockSigner::SignBlock() | ||
{ | ||
CKey keySecret; | ||
CPubKey::InputScriptType scriptType; | ||
CPubKey::InputScriptType scriptType { CPubKey::InputScriptType::SPENDUNKNOWN }; | ||
|
||
if(refBlock.IsProofOfStake()) | ||
{ | ||
|
@@ -45,15 +46,18 @@ bool CBlockSigner::SignBlock() | |
|
||
if(refBlock.IsTPoSBlock()) | ||
{ | ||
CKeyID merchantKeyID; | ||
if(!refContract.merchantAddress.GetKeyID(merchantKeyID)) | ||
if(!refContract.scriptMerchantAddress.IsPayToPublicKeyHash()) { | ||
return error("CBlockSigner::SignBlock() : merchant address is not P2PKH, critical error, can't accept."); | ||
} | ||
|
||
CTxDestination dest; | ||
ExtractDestination(refContract.scriptMerchantAddress, dest); | ||
|
||
if(merchantKeyID != activeMerchantnode.pubKeyMerchantnode.GetID()) | ||
if(dest != CTxDestination(activeMerchantnode.pubKeyMerchantnode.GetID())) { | ||
return error("CBlockSigner::SignBlock() : contract address is different from merchantnode address, won't sign."); | ||
} | ||
|
||
scriptType = CPubKey::InputScriptType::SPENDP2PKH; | ||
|
||
keySecret = activeMerchantnode.keyMerchantnode; | ||
} | ||
else | ||
|
@@ -69,8 +73,13 @@ bool CBlockSigner::SignBlock() | |
scriptType = GetScriptTypeFromDestination(destination); | ||
} | ||
} | ||
//? | ||
return CHashSigner::SignHash(refBlock.IsTPoSBlock() ? refBlock.GetTPoSHash() : refBlock.GetHash(), keySecret, scriptType, refBlock.vchBlockSig); | ||
|
||
const auto &hash = refBlock.IsTPoSBlock() ? refBlock.GetTPoSHash() : refBlock.GetHash(); | ||
if (nChainHeight >= Params().GetConsensus().nTPoSSignatureUpgradeHFHeight) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to create a common function that has tests to check this, instead of keep writing the same check everywhere. Or, even better, just write a helper thing that handles signing contracts, and verifying signatures. |
||
return CMessageSigner::SignMessage(std::string(hash.begin(), hash.end()), refBlock.vchBlockSig, keySecret, scriptType); | ||
} else { | ||
return CHashSigner::SignHash(hash, keySecret, scriptType, refBlock.vchBlockSig); | ||
} | ||
} | ||
|
||
bool CBlockSigner::CheckBlockSignature() const | ||
|
@@ -85,24 +94,26 @@ bool CBlockSigner::CheckBlockSignature() const | |
|
||
CTxDestination destination; | ||
|
||
if(!ExtractDestination(txout.scriptPubKey, destination)) | ||
{ | ||
if(!ExtractDestination(txout.scriptPubKey, destination)) { | ||
return error("CBlockSigner::CheckBlockSignature() : failed to extract destination from script: %s", txout.scriptPubKey.ToString()); | ||
} | ||
|
||
auto hashMessage = refBlock.IsTPoSBlock() ? refBlock.GetTPoSHash() : refBlock.GetHash(); | ||
if(refBlock.IsProofOfStake()) | ||
{ | ||
if(refBlock.IsTPoSBlock()) | ||
{ | ||
destination = refContract.merchantAddress.Get(); | ||
if(refBlock.IsProofOfStake()) { | ||
if(refBlock.IsTPoSBlock()) { | ||
if (!ExtractDestination(refContract.scriptMerchantAddress, destination)) { | ||
return error("CBlockSigner::CheckBlockSignature() : failed to extract destination from script: %s", refContract.scriptMerchantAddress.ToString()); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
else { | ||
return true; | ||
} | ||
|
||
std::string strError; | ||
return CHashSigner::VerifyHash(hashMessage, destination, refBlock.vchBlockSig, strError); | ||
if (nChainHeight >= Params().GetConsensus().nTPoSSignatureUpgradeHFHeight) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same duplication stated above. |
||
return CMessageSigner::VerifyMessage(destination, refBlock.vchBlockSig, std::string(hashMessage.begin(), hashMessage.end()), strError); | ||
} else { | ||
return CHashSigner::VerifyHash(hashMessage, destination, refBlock.vchBlockSig, strError); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ class CMainParams : public CChainParams { | |
consensus.nPosTargetSpacing = 1 * 60; // XSN: 1 minutes | ||
consensus.nPosTargetTimespan = 60 * 40; | ||
consensus.nPoSUpdgradeHFHeight = 898488; // 4 December 2019 | ||
consensus.nTPoSSignatureUpgradeHFHeight = 1264447; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's working commenting what's this for, and why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will set the date later |
||
consensus.nMerchantnodeMinimumConfirmations = 1; | ||
consensus.nMasternodeMinimumConfirmations = 15; | ||
consensus.nStakeMinAge = 60 * 60; | ||
|
@@ -110,6 +111,7 @@ class CMainParams : public CChainParams { | |
consensus.nMaxBlockSpacingFixDeploymentHeight = 674980; // apprx 28 of June | ||
consensus.fPowAllowMinDifficultyBlocks = false; | ||
consensus.fPowNoRetargeting = false; | ||
consensus.fPoSNoRetargeting = false; | ||
consensus.nRuleChangeActivationThreshold = 1080; // 75% of 2016 | ||
consensus.nMinerConfirmationWindow = 1440; // nPowTargetTimespan / nPowTargetSpacing | ||
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28; | ||
|
@@ -235,6 +237,7 @@ class CTestNetParams : public CChainParams { | |
consensus.nMasternodeMinimumConfirmations = 1; | ||
consensus.nMerchantnodeMinimumConfirmations = 1; | ||
consensus.fPowNoRetargeting = false; | ||
consensus.fPoSNoRetargeting = false; | ||
consensus.nPowKGWHeight = 4001; // nPowKGWHeight >= nPowDGWHeight means "no KGW" | ||
consensus.nPowDGWHeight = 4001; | ||
consensus.nMaxBlockSpacingFixDeploymentHeight = 0; | ||
|
@@ -289,11 +292,6 @@ class CTestNetParams : public CChainParams { | |
|
||
vFixedSeeds.clear(); | ||
vSeeds.clear(); | ||
// nodes with support for servicebits filtering should be at the top | ||
// vSeeds.emplace_back("testnet-seed.xsn.jonasschnelli.ch"); | ||
// vSeeds.emplace_back("seed.txsn.petertodd.org"); | ||
// vSeeds.emplace_back("seed.testnet.xsn.sprovoost.nl"); | ||
// vSeeds.emplace_back("testnet-seed.bluematt.me"); // Just a static list of stable node(s), only supports x9 | ||
|
||
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,140); | ||
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,19); | ||
|
@@ -360,13 +358,16 @@ class CRegTestParams : public CChainParams { | |
consensus.nPowTargetSpacing = 1 * 60; // XSN: 1 minutes | ||
consensus.nPosTargetSpacing = 1 * 60; // PoSW: 1 minutes | ||
consensus.nPosTargetTimespan = 60 * 40; | ||
consensus.nPoSUpdgradeHFHeight = 0; | ||
consensus.nTPoSSignatureUpgradeHFHeight = 80; | ||
consensus.fPowAllowMinDifficultyBlocks = true; | ||
consensus.fPowNoRetargeting = true; | ||
consensus.fPoSNoRetargeting = true; | ||
consensus.nPowKGWHeight = 4001; // nPowKGWHeight >= nPowDGWHeight means "no KGW" | ||
consensus.nPowDGWHeight = 4001; | ||
consensus.nLastPoWBlock = 75; | ||
consensus.nMaxBlockSpacingFixDeploymentHeight = 0; | ||
consensus.nStakeMinAge = 60 * 60; | ||
consensus.nStakeMinAge = 60; | ||
consensus.nStakeMaxAge = 60 * 60 * 24; // one day | ||
consensus.nCoinbaseMaturity = 20; | ||
consensus.nFirstBlocksEmpty = 0; | ||
|
@@ -422,13 +423,13 @@ class CRegTestParams : public CChainParams { | |
0 | ||
}; | ||
|
||
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,140); | ||
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,19); | ||
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239); | ||
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; | ||
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; | ||
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,76); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these changes actually needed? It was useful for testing but is it ok to keep the change? Also, I'd add the details to the README for using the regtest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, should be ok, doesn't look too bad |
||
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,16); | ||
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,204); | ||
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E}; | ||
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4}; | ||
|
||
bech32_hrp = "bcrt"; | ||
bech32_hrp = "xc"; | ||
|
||
/* enable fallback fee on regtest */ | ||
m_fallback_fee_enabled = true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1115,8 +1115,11 @@ void AdjustMasternodePayment(CMutableTransaction &tx, const CTxOut &txoutMastern | |
if(tposContract.IsValid()) // here we have 3 outputs, first as stake reward, second as tpos reward, third as MN reward | ||
{ | ||
masternodePayment /= 100; // to calculate percentage | ||
tx.vout[i - 1].nValue -= masternodePayment * tposContract.stakePercentage; // adjust reward for owner. | ||
tx.vout[i].nValue -= masternodePayment * (100 - tposContract.stakePercentage); // adjust reward for merchant | ||
tx.vout[i - 1].nValue -= masternodePayment * (100 - tposContract.nOperatorReward); // adjust reward for owner. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd create a common function for this, and make sure it has proper tests, a mistake would be very bad for us There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true |
||
// it might be that last vout is masternode, since operator reward was 0 | ||
if (tx.vout[i].scriptPubKey == tposContract.scriptMerchantAddress) { | ||
tx.vout[i].nValue -= masternodePayment * tposContract.nOperatorReward; // adjust reward for merchant | ||
} | ||
} | ||
else // here we have 2 outputs, first as stake reward, second as MN reward | ||
{ | ||
|
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.
Would you mind elaborating why is this required?
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.
Currently we accept merchant addresses only in P2PKH format, so we avoid signing blocks that would get rejected