PIVX Core  5.6.99
P2P Digital Currency
main_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin Core developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "test/test_pivx.h"
8 
9 #include "blocksignature.h"
10 #include "net.h"
11 #include "primitives/transaction.h"
12 #include "script/sign.h"
13 #include "validation.h"
14 
15 #include <boost/test/unit_test.hpp>
16 
18 
22  P2CS
23 };
24 
26 {
27  switch(type){
28  case P2PK:
29  return CScript() << pubKey << OP_CHECKSIG;
30  default:
31  return GetScriptForDestination(pubKey.GetID());
32  }
33 }
34 
35 std::vector<unsigned char> CreateDummyScriptSigWithKey(CPubKey pubKey)
36 {
37  std::vector<unsigned char> vchSig;
38  const CScript scriptCode;
39  DummySignatureCreator(nullptr).CreateSig(vchSig, pubKey.GetID(), scriptCode, SIGVERSION_BASE);
40  return vchSig;
41 }
42 
44 {
45  CScript script = CScript() << CreateDummyScriptSigWithKey(pubKey);
46  if (!isP2PK)
47  script << ToByteVector(pubKey);
48  return script;
49 }
50 
51 CBlock CreateDummyBlockWithSignature(CKey stakingKey, BlockSignatureType type, bool useInputP2PK)
52 {
53  CMutableTransaction txCoinStake;
54  // Dummy input
55  CTxIn input(uint256(), 0);
56  // P2PKH input
57  input.scriptSig = GetDummyScriptSigByType(stakingKey.GetPubKey(), useInputP2PK);
58  // Add dummy input
59  txCoinStake.vin.emplace_back(input);
60  // Empty first output
61  txCoinStake.vout.emplace_back(0, CScript());
62  // P2PK staking output
63  CScript scriptPubKey = GetScriptForType(stakingKey.GetPubKey(), type);
64  txCoinStake.vout.emplace_back(0, scriptPubKey);
65 
66  // Now the block.
67  CBlock block;
68  block.vtx.emplace_back(std::make_shared<const CTransaction>(CTransaction())); // dummy first tx
69  block.vtx.emplace_back(std::make_shared<const CTransaction>(txCoinStake));
70  SignBlockWithKey(block, stakingKey);
71 
72  return block;
73 }
74 
75 bool TestBlockSignature(const CBlock& block)
76 {
77  return CheckBlockSignature(block);
78 }
79 
80 BOOST_AUTO_TEST_CASE(block_signature_test)
81 {
82  for (int i = 0; i < 20; ++i) {
83  CKey stakingKey;
84  stakingKey.MakeNewKey(true);
85  bool useInputP2PK = i % 2 == 0;
86 
87  // Test P2PK block signature
88  CBlock block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PK, useInputP2PK);
90 
91  // Test P2PKH block signature
92  block = CreateDummyBlockWithSignature(stakingKey, BlockSignatureType::P2PKH, useInputP2PK);
93  if (useInputP2PK) {
94  // If it's using a P2PK scriptsig as input and a P2PKH output
95  // The block doesn't contain the public key to verify the sig anywhere.
96  // Must fail.
98  } else {
100  }
101  }
102 }
103 
104 CAmount nMoneySupplyPoWEnd = 43199500 * COIN;
105 
106 BOOST_AUTO_TEST_CASE(subsidy_limit_test)
107 {
108  CAmount nSum = 0;
109  for (int nHeight = 0; nHeight < 1; nHeight += 1) {
110  /* premine in block 1 (60,001 PIV) */
111  CAmount nSubsidy = GetBlockValue(nHeight + 1);
112  BOOST_CHECK(nSubsidy <= 60001 * COIN);
113  nSum += nSubsidy;
114  }
115 
116  for (int nHeight = 1; nHeight < 86400; nHeight += 1) {
117  /* PoW Phase One */
118  CAmount nSubsidy = GetBlockValue(nHeight + 1);
119  BOOST_CHECK(nSubsidy <= 250 * COIN);
120  nSum += nSubsidy;
121  }
122 
123  for (int nHeight = 86400; nHeight < 151200; nHeight += 1) {
124  /* PoW Phase Two */
125  CAmount nSubsidy = GetBlockValue(nHeight + 1);
126  BOOST_CHECK(nSubsidy <= 225 * COIN);
127  nSum += nSubsidy;
128  }
129 
130  for (int nHeight = 151200; nHeight < 259200; nHeight += 1) {
131  /* PoW Phase Two */
132  CAmount nSubsidy = GetBlockValue(nHeight + 1);
133  BOOST_CHECK(nSubsidy <= 45 * COIN);
134  BOOST_CHECK(Params().GetConsensus().MoneyRange(nSubsidy));
135  nSum += nSubsidy;
136  BOOST_CHECK(nSum > 0 && nSum <= nMoneySupplyPoWEnd);
137  }
138  BOOST_CHECK(nSum == 4109975100000000ULL);
139 }
140 
141 bool ReturnFalse() { return false; }
142 bool ReturnTrue() { return true; }
143 
144 BOOST_AUTO_TEST_CASE(test_combiner_all)
145 {
146  boost::signals2::signal<bool(), CombinerAll> Test;
147  BOOST_CHECK(Test());
148  Test.connect(&ReturnFalse);
149  BOOST_CHECK(!Test());
150  Test.connect(&ReturnTrue);
151  BOOST_CHECK(!Test());
152  Test.disconnect(&ReturnFalse);
153  BOOST_CHECK(Test());
154  Test.disconnect(&ReturnTrue);
155  BOOST_CHECK(Test());
156 }
157 
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
bool CheckBlockSignature(const CBlock &block)
bool SignBlockWithKey(CBlock &block, const CKey &key)
const CChainParams & Params()
Return the currently selected parameters.
Definition: block.h:80
std::vector< CTransactionRef > vtx
Definition: block.h:83
An encapsulated private key.
Definition: key.h:30
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:158
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:186
An encapsulated public key.
Definition: pubkey.h:44
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:167
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
An input of a transaction.
Definition: transaction.h:94
CScript scriptSig
Definition: transaction.h:97
A signature creator that just produces 72-byte empty signatyres.
Definition: sign.h:94
bool CreateSig(std::vector< unsigned char > &vchSig, const CKeyID &keyid, const CScript &scriptCode, SigVersion sigversion) const
Create a singular (non-script) signature.
Definition: sign.cpp:367
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
CScript GetScriptForType(CPubKey pubKey, BlockSignatureType type)
Definition: main_tests.cpp:25
BOOST_AUTO_TEST_CASE(block_signature_test)
Definition: main_tests.cpp:80
CBlock CreateDummyBlockWithSignature(CKey stakingKey, BlockSignatureType type, bool useInputP2PK)
Definition: main_tests.cpp:51
bool ReturnTrue()
Definition: main_tests.cpp:142
bool ReturnFalse()
Definition: main_tests.cpp:141
CScript GetDummyScriptSigByType(CPubKey pubKey, bool isP2PK)
Definition: main_tests.cpp:43
CAmount nMoneySupplyPoWEnd
Definition: main_tests.cpp:104
BlockSignatureType
Definition: main_tests.cpp:19
@ P2PK
Definition: main_tests.cpp:20
@ P2PKH
Definition: main_tests.cpp:21
@ P2CS
Definition: main_tests.cpp:22
std::vector< unsigned char > CreateDummyScriptSigWithKey(CPubKey pubKey)
Definition: main_tests.cpp:35
bool TestBlockSignature(const CBlock &block)
Definition: main_tests.cpp:75
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:43
@ OP_CHECKSIG
Definition: script.h:166
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
Definition: standard.cpp:278
A mutable version of CTransaction.
Definition: transaction.h:409
std::vector< CTxOut > vout
Definition: transaction.h:411
std::vector< CTxIn > vin
Definition: transaction.h:410
@ SIGVERSION_BASE
Definition: transaction.h:27
CAmount GetBlockValue(int nHeight)
Definition: validation.cpp:816