forked from drivechain-project/mainchain-old
-
Notifications
You must be signed in to change notification settings - Fork 5
/
transaction_criticaldata_tests.cpp
193 lines (148 loc) · 5.9 KB
/
transaction_criticaldata_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2017-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// TODO cleanup includes
#include "chainparams.h"
#include "consensus/validation.h"
#include "core_io.h"
#include "miner.h"
#include "random.h"
#include "script/sigcache.h"
#include "script/standard.h"
#include "uint256.h"
#include "utilstrencodings.h"
#include "validation.h"
#include "test/test_skydoge.h"
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(transaction_criticaldata_tests, TestChain100Setup)
BOOST_AUTO_TEST_CASE(criticaldata_serialization)
{
CMutableTransaction mtx;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.nLockTime = 21;
mtx.vin[0].prevout.SetNull();
mtx.vin[0].scriptSig = CScript();
CScript script;
script << OP_RETURN;
mtx.vout[0] = CTxOut(50 * CENT, script);
mtx.criticalData.hashCritical = GetRandHash();
// Get the transaction's serialization
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
mtx.Serialize(ss);
// Deserialize
CTransaction txDeserialized(deserialize, ss);
// Check that CTransaction was properly deserialized
BOOST_CHECK(txDeserialized.GetHash() == mtx.GetHash());
}
BOOST_AUTO_TEST_CASE(criticaldata_valid)
{
// Test in block with a valid data & commit
BOOST_CHECK(chainActive.Height() == 100);
// Generate a block
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
// Checking that we can make blocks normally
BOOST_CHECK(chainActive.Height() == 101);
// Create transaction with critical data
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[0].GetHash();
mtx.vin[0].prevout.n = 0;
mtx.vout[0].scriptPubKey = CScript() << OP_0;
mtx.vout[0].nValue = 50 * CENT;
// We set the lock time to the current height. Critical Data transactions
// have a validation rule to confirm the transactions goes into the block
// at height tx.nLockTime + 1. We don't want it to be spendable before or
// after the locktime.
mtx.nLockTime = 101;
// Add critical data
mtx.criticalData.hashCritical = GetRandHash();
// Sign
const CTransaction txToSign(mtx);
std::vector<unsigned char> vchSig;
uint256 hash = SignatureHash(GetScriptForRawPubKey(coinbaseKey.GetPubKey()), txToSign, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
vchSig.push_back((unsigned char)SIGHASH_ALL);
mtx.vin[0].scriptSig << vchSig;
TestMemPoolEntryHelper entry;
mempool.addUnchecked(mtx.GetHash(), entry.Fee(10000).FromTx(mtx));
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()), false, false);
BOOST_CHECK(chainActive.Height() == 102);
}
BOOST_AUTO_TEST_CASE(criticaldata_invalid_locktime)
{
// TODO
/*
// Test in block with a valid data & commit but invalid locktime
BOOST_CHECK(chainActive.Height() == 100);
// Generate a block
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
// Checking that we can make blocks normally
BOOST_CHECK(chainActive.Height() == 101);
// Create transaction with critical data
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[0].GetHash();
mtx.vin[0].prevout.n = 0;
mtx.vout[0].scriptPubKey = CScript() << OP_0;
mtx.vout[0].nValue = 50 * CENT;
// Set locktime to the block we would like critical data to be commited in
mtx.nLockTime = 2600;
// Add critical data
mtx.criticalData.hashCritical = GetRandHash();
// Sign
const CTransaction txToSign(mtx);
std::vector<unsigned char> vchSig;
uint256 hash = SignatureHash(GetScriptForRawPubKey(coinbaseKey.GetPubKey()), txToSign, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
vchSig.push_back((unsigned char)SIGHASH_ALL);
mtx.vin[0].scriptSig << vchSig;
TestMemPoolEntryHelper entry;
mempool.addUnchecked(mtx.GetHash(), entry.Fee(10000).FromTx(mtx));
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
// Block should have been rejected, blockheight should be unchanged
BOOST_CHECK(chainActive.Height() == 101);
*/
}
BOOST_AUTO_TEST_CASE(criticaldata_invalid_no_commit)
{
// TODO
/*
// Test in block with a valid data but no commit
BOOST_CHECK(chainActive.Height() == 100);
// Generate a block
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
// Checking that we can make blocks normally
BOOST_CHECK(chainActive.Height() == 101);
// Create transaction with critical data
CMutableTransaction mtx;
mtx.nVersion = 2;
mtx.vin.resize(1);
mtx.vout.resize(1);
mtx.vin[0].prevout.hash = coinbaseTxns[0].GetHash();
mtx.vin[0].prevout.n = 0;
mtx.vout[0].scriptPubKey = CScript() << OP_0;
mtx.vout[0].nValue = 50 * CENT;
// Set locktime to the block we would like critical data to be commited in
mtx.nLockTime = 102;
// Add critical data
mtx.criticalData.hashCritical = GetRandHash();
// Sign
const CTransaction txToSign(mtx);
std::vector<unsigned char> vchSig;
uint256 hash = SignatureHash(GetScriptForRawPubKey(coinbaseKey.GetPubKey()), txToSign, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
vchSig.push_back((unsigned char)SIGHASH_ALL);
mtx.vin[0].scriptSig << vchSig;
TestMemPoolEntryHelper entry;
mempool.addUnchecked(mtx.GetHash(), entry.Fee(10000).FromTx(mtx));
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
// Block should have been rejected, blockheight should be unchanged
BOOST_CHECK(chainActive.Height() == 101);
*/
}
BOOST_AUTO_TEST_SUITE_END()