Bitcoin ABC  0.22.13
P2P Digital Currency
proof_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <avalanche/proof.h>
6 
8 #include <avalanche/test/util.h>
9 #include <avalanche/validation.h>
10 #include <coins.h>
11 #include <script/standard.h>
12 #include <streams.h>
13 #include <util/strencodings.h>
14 
15 #include <test/util/setup_common.h>
16 
17 #include <boost/test/unit_test.hpp>
18 
19 using namespace avalanche;
20 
21 BOOST_FIXTURE_TEST_SUITE(proof_tests, TestingSetup)
22 
23 BOOST_AUTO_TEST_CASE(proof_random) {
24  for (int i = 0; i < 1000; i++) {
25  const uint32_t score = InsecureRand32();
26  const Proof p = buildRandomProof(score);
27  BOOST_CHECK_EQUAL(p.getScore(), score);
28 
30  BOOST_CHECK(p.verify(state));
31  BOOST_CHECK(state.GetResult() == ProofValidationResult::NONE);
32  }
33 }
34 
35 BOOST_AUTO_TEST_CASE(proofbuilder) {
36  CKey key;
37 
38  // Master key.
39  key.MakeNewKey(true);
40  const CPubKey master = key.GetPubKey();
41 
42  const uint64_t sequence = InsecureRandBits(64);
43  const int64_t expiration = InsecureRandBits(64);
44 
45  ProofBuilder pb(sequence, expiration, master);
46 
47  for (int i = 0; i < 3; i++) {
48  key.MakeNewKey(true);
49  pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()),
50  int64_t(InsecureRand32()) * COIN / 100, InsecureRand32(),
51  InsecureRandBool(), key);
52  }
53 
54  Proof p = pb.build();
55 
57  BOOST_CHECK(p.verify(state));
58 
59  BOOST_CHECK_EQUAL(p.getSequence(), sequence);
60  BOOST_CHECK_EQUAL(p.getExpirationTime(), expiration);
61  BOOST_CHECK(p.getMaster() == master);
62 
64 }
65 
66 struct TestVector {
67  std::string name;
68  std::string hex;
70  uint32_t score;
72 };
73 
74 BOOST_AUTO_TEST_CASE(deserialization) {
75  std::vector<TestVector> testcases{
76  {"No utxo staked",
77  "96527eae083f1f24625f049d9e54bb9a2102a93d98bf42ab90cfc0bf9e7c634ed76a7"
78  "3e95b02cacfd357b64e4fb6c92e92dd00",
79  ProofId::fromHex("df721b6e2a857ce8abac63d8d5eca35f3bdb0293b6e8295942c7"
80  "6274c5418c0c"),
82  {"1 utxo staked",
83  "a6d66db9fe9378fdd37a0ad2c01c2acd2103648144bb6a0c1d09b0f04d0df6d55f914"
84  "fd81efc65f23a718b68b7c9e42bd5430145a4d07798547464daa53acefb7c97c0c415"
85  "ed8e81e549ff56a0ef6f847fcc9ca855b36200fe38dce5060000e707d7274104fb662"
86  "6e21dbd1cc9feeecdefc9213fdce2b51ac4bb44e1f8dc6f14c2052f5dd7bfaeb2267a"
87  "97ca2bec6e0dd4acf50a66204bde1ebb5d6c551684cff2f939920f7fbb2efd860d6d5"
88  "926bf425eb47b78bf6979cdcd67eb705e2c9a4d45a0930ba25463178a3fb99cb28c8b"
89  "77d8fcf68c54ebfadf08b9a446c251a0088301c50d53",
90  ProofId::fromHex("048235ee870030f11c287d898dd3ec184f9b38cf4fb274334966"
91  "c6aad83b769d"),
93  {"2 utxo staked",
94  "872379ab64f55b4166ca0e79639999ec4104a66861de557a54eefc0375264cc17c3a3"
95  "50ccabca6fd9c91883e899ab55bb140517aa56c5b4041908e7027a786b99f66488a04"
96  "135ce5fe189a99a7bc541ddfe602fabf3ad5b875840e7813a66d5ea8a1288a49b6222"
97  "b59fcbe6249f94e5927f9f4b884b0b040a534b3ba040000d045d5d021036830e697b0"
98  "ee89866da798a8945bd85b352545ec1bcace7e04909ea54c134f16d5fe4e972b7acd2"
99  "9ebfc2b7b11c26974b84e5f21a45bbe8372472f59e5dfdea7e9e5857c6aebe5dbc5e6"
100  "46dfbf4e7cee380afaddb15d06153bf1755b9ef00a616d4c8c3c3a662b5eddc192656"
101  "4a488e3e68e334291078001480f7fa5144ef3a606a41e85c0218dd377090000e41099"
102  "912102ebfcea8e1864c1273c41e0d7c1e9097be5c491bbbf5fe31161d8e5589b9d6b5"
103  "b12f3b963c7fc7614d56d83af907e5cb18ac2f4c3e70a8c4253995f6bc002ec5e3504"
104  "91c965cba4dbc11c210979217f1ac3ece7a748f5b2fcf5cced40a5d4c40e",
105  ProofId::fromHex("7319126f0d4efc188440dd50105ea30d792687b65e9cdde6c4d6"
106  "08ed226cba00"),
108  {"3 utxo staked",
109  "525e2aa04af0e2457c66ac9e7f66257f210252db8e3ceea6fca44a7696e82f7b77e5a"
110  "4025e60ac60271b174e91ffbb6ce01f039ce8d3b77938e49ce3bc9824e90b72c65542"
111  "2fb502f137e03a4499e5223d10096fe541eb80316ce3c80800000285f59341044fd7e"
112  "95de7c7bb30e7f60434a3e1a414a9e5d9c383c7b27396b1b84355a32e2996ecb98dc2"
113  "0143089932fa1b905a60fc3cfefeea193c91d1405f7c03de494fa4de065c067d64606"
114  "0e9270281c316d5c4c01d7e43d009151a72bf647794ce1727cbefaeb19719f916cd4d"
115  "d176c376a4da72431b61736d4a3e01c25ba057eac0af8f2988b78d1b75e02281fac56"
116  "2357a06353bf7f214c883e65add05b3a616300cb99cc963c0f4ded8c00e0000b03aab"
117  "f82103172b4f1890594508ab1e0cc5e9728b8a249660da4df724762a8fd888e8ece1b"
118  "d6fe923ed17ad0fd77a90d31e3877de1a8cdb4e95bcf2cdd6fb9768f86789f253b432"
119  "f3b5058b2d1892e90882529055fdedf8ae5d3280b2404a65321cf7f7229202db300ff"
120  "2897e33259a81dfc4bf296c3a156dc8dbfa074c602dd2250cc531b346fc28bb80ddf2"
121  "9ddc020000a713a5bb21035615635d449988a4eea03fd317e44481bde2e34a2489f3f"
122  "b24f0b0ea1cbfc4030bec095a8d3e9c2a233778535dc1fcc07755149b23ea8c17931a"
123  "37e9377eaa2f45bafb8d0bb9ba1700aab88fd6a53ea3e83d95ad2d84e7abe828f5570"
124  "91b185e",
125  ProofId::fromHex("3b3204993240ab338324310ecadc5f234da1dac0627029cef63e"
126  "1169a049d18f"),
128  {"4 utxo staked",
129  "eef33172651f752ac255c85a4e1374992102c12b37ff6139157865fc4c3a9d7ad999b"
130  "686ade45d453545d04e76f6e14793b404295de5ebf9fbbbb65fc1d9a71587c5284cff"
131  "b2e834addefe090b8200435668c8f02c0b0100689a117e0e000007d801582102a682d"
132  "9d12d53b0eb37a3af2838510f079041905a75f82b6a3fb5558728d781fbf868d1d968"
133  "27b273f5a79f84ebe23add967a98f472fb80323439d0a65d546fc3745806f6d7f3381"
134  "24a7a2573864e97a26246644a7d7b05d97943dfcdb4b694df631e6dc5f87e28c1fe3e"
135  "5ba021f38c471638769041db81ffcf8c9887d078419f97fe2a2c408fee822c0600001"
136  "689b9cd2102e38d0adddcd7c88e3c87b8babcae10647e2862fb719839fc8890e42aeb"
137  "929b85e1a3f14e2cdc65e2b1396f2dae41b047958cdc7e4d2f6fa051065829e26797c"
138  "5b882e45bb9fea32c0b0e0ad90f8ac1e5d8d0b16a9b74d77614b7fd99e56b6091aca3"
139  "67f8f7a68d1b654e51dd00733bd191dc9bea2ba750e063b05d962aaf9c4d2088ce4f0"
140  "03e73e253040000a5eebe222103a20dd85b66b44b22fdd17a93762194c9bedb442c7f"
141  "fd7f08a9f82c42a8c1d9a0443f700cb8a40c8cf7f840b5137b6d019efed961771d095"
142  "88b0eb3c5e1672bb95b06ca7e2068e564001aa75b8c37bb6601117c286b6b0c9728d1"
143  "e928ab02e3b67e9422b484ef2624ce5de974b5bd616874ec39d03d32ed0bf114759b7"
144  "0bf5dcef51534b100515301140f0000950121754104d66dba1569164a134111961133"
145  "4bad5e2d398823f1454ceecb9c4266fd3ba4b969ac4d4f6c4b3975d19c2f7dcbbca09"
146  "6af5395780a2d3c42505146c095bc861fab15238fb8aa1fb82c7ad28b0ee5d1335348"
147  "76dc7887490c7c6e61103b2cd221f1991826a73fecf08e0b5a0a7d357a5431eee032d"
148  "14a348c80ca1833d68b3d7b",
149  ProofId::fromHex("62f256d73411fe69bf0db6083248e4efb75be9788850851468ac"
150  "c913c5c14360"),
152  {"Properly signed 1 UTXO proof",
153  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
154  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
155  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
156  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
157  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
158  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
159  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
160  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
161  ProofId::fromHex("17717dbf1cee175925be12321bb04eda7bc861a28b04d764c2d5"
162  "5eabf01cabd0"),
163  444638638, ProofValidationResult::NONE},
164  {"Duplicated UTXO",
165  "60f8332a3ff3430a4f3c9010160cc63e2102f2df6361a5744521d16753f9efb5ace68"
166  "d43121e90ff67290540b54341cbab0e02d1e26c2287948bc6ab2b55945c591b8ba3ff"
167  "a237f5d9164d30a4f10145a61f788e639b1480731e2aead30500bf84628721029ed70"
168  "5bdc5adf0444c57c5ec2ea929c1adc0cd5b9cef4647d85dcb7fc400dc3afd21b29571"
169  "9c45b90fda493bbc74c8ae1baf28e124cc23bb1987f9932d2df5a0b3455e6c815767c"
170  "be8ccdd04e0e2d33d5f2f092667ef081acdaafde869d58daed1e26c2287948bc6ab2b"
171  "55945c591b8ba3ffa237f5d9164d30a4f10145a61f788e639b1480731e2aead30500b"
172  "f84628721029ed705bdc5adf0444c57c5ec2ea929c1adc0cd5b9cef4647d85dcb7fc4"
173  "00dc3afd21b295719c45b90fda493bbc74c8ae1baf28e124cc23bb1987f9932d2df5a"
174  "0b3455e6c815767cbe8ccdd04e0e2d33d5f2f092667ef081acdaafde869d58dae",
175  ProofId::fromHex("b0c2c07cbc766df0a934bcb252063ad5d7d5167b080369f6feca"
176  "a3aabae29785"),
178  {"Properly signed 3 UTXO proof",
179  "c96492f9e27a25d7e8404581c7be874e210282eb68a587dc586673f439fe317fa0b76"
180  "aceefa9f99820eadaa70d3b19c0cfe10305d427b706705a5d4b6a368a231d6db62aba"
181  "cf8c29bc32b61e7f65a0a6976aa8b86b687bc0260e821e4f0200b9d3bf6d2102914b1"
182  "aade61c7b5d38b18fb8c02ec4a9411858a43969927e5a2bf371e83ea20aa2a8ac89e9"
183  "fb52edcb227de3c3bd51519c0afcd5c3c134f09e53a9dd4315d4cde560c54b5700c2e"
184  "7b4f15c18461a3c093b790e93ef54640c051fff6798f18e2c18f54247f6390791706a"
185  "f36fac782302479898b5273f9e51a92cb1fb5af43deeb6c8c269403dbd48c9530a001"
186  "34398c441040a9c4426531e19916a0250536684073fbedce86d73e883ee1107e7984e"
187  "7c721b2be965c63bcbdbd23dcabba6de7ae6715872c803a257e53b686ea69217228c0"
188  "6ee1b88c4c7aa55adb1804a13e9d222b8d327c4de466fcd9e71ec20582f8d0760830a"
189  "67e020688e73c91519bd4b74358e65e9ddc95065bcf1bee0d11767e10f30f0d4ce48c"
190  "4ca9d0714b1fa51920270f8575e0af610f07b4e602a018ecdbb649b64fff614c0026e"
191  "9fc8e0030092533d424104973b212564dfb8ff1aa4bab13389ed170ea6cbb06fd9a5b"
192  "5b3e8c00d1a7d4bca7d237748ad793fc043b12f73a28caabb00927c5aea81e323d3ba"
193  "047a9661adbb0728ccfb41ab345299d7469ff5ec259b98fc946c730b5100cc97284ea"
194  "6101f7e43337987bbfe655754939709c72c7ba69a9dc08558733781ee9da34e3de7bd"
195  "a7",
196  ProofId::fromHex("5948d32ee26882a2d9f96c18e9e51ad13a79234b627a020ee7db"
197  "733684cb05a7"),
198  353426051, ProofValidationResult::NONE},
199  {"Changing sequence affect ProofId",
200  "d87587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
201  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
202  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
203  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
204  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
205  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
206  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
207  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
208  ProofId::fromHex("3fdcb56110cfe2f394689be3dd28dedee674f79a567c8391dbd9"
209  "3900c9845ca8"),
211  {"Changing expiration affect ProofId",
212  "d97587e6c882615797011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
213  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
214  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
215  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
216  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
217  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
218  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
219  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
220  ProofId::fromHex("a9ffbb2b37526e05a1f72b43314f872c381281ce8d3d96a5bf56"
221  "47db057a64dc"),
223  {"Changing the master key affect ProofId",
224  "d97587e6c882615796011ec8f9a7b1c64104e556ba897297fd6a655bc2579d26814e8"
225  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
226  "bd546a614b0e05433528b19587 "
227  "0169a79ff23e1d58c64afad42ad81cffe53967e16be"
228  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
229  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
230  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
231  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
232  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
233  ProofId::fromHex("c237e5436f2d21e4ae990422bfb8ec97df52a9f6db5c20c40f54"
234  "64e590dcda39"),
236  {"Changing the TxId affect the ProofId",
237  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
238  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
239  "bd546a614b0e05433528b195870179a79ff23e1d58c64afad42ad81cffe53967e16be"
240  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
241  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
242  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
243  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
244  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
245  ProofId::fromHex("9d372d7580917985824c632c919ce8bf89fc6f45c07ee60b2564"
246  "6aea23390ec6"),
248  {"Changing the outpoint index change the ProofId",
249  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
250  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
251  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
252  "b692fc5776bb442c79c5d91df00cf21804712806594010038e168a34104d9d84ebf65"
253  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
254  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
255  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
256  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
257  ProofId::fromHex("1a98c5f7684508665fbc946c64c9486e3f7c91b2f42d299320c5"
258  "56e68e4028fe"),
260  {"Changing the amount changes the ProofId",
261  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
262  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
263  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
264  "b692fc5776bb442c79c5d91de00cf21814712806594010038e168a34104d9d84ebf65"
265  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
266  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
267  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
268  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
269  ProofId::fromHex("528fa9654e845dfe30ed03966240ab615a8d374d86256e1e4892"
270  "83eb0a0be44b"),
272  {"Changing the height changes the ProofId",
273  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
274  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
275  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
276  "b692fc5776bb442c79c5d91de00cf21804712806594010028e168a34104d9d84ebf65"
277  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
278  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
279  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
280  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
281  ProofId::fromHex("a932294f46f106b2b38e57a4b1e4b5849730aba2124662f680a6"
282  "1c8cc429adb0"),
284  {"Changing the pubkey changes the ProofId",
285  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
286  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
287  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
288  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d8d84ebf65"
289  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
290  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
291  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
292  "7c0276a293ee5b3f5a130fc5f9b217585cae23e",
293  ProofId::fromHex("336c9a502b22ba2edcd78298c1ce6cc5e8b5e94cdc60b1047d1f"
294  "96810d5d17a3"),
296  {"Changing the signatue does NOT change the ProofId",
297  "d97587e6c882615796011ec8f9a7b1c64104e556ba887297fd6a655bc2579d26814e8"
298  "54c902448ec3ce3c6fe965e3420aa3f0e9611d3c0b8a73b6329741e2e726fd17f5e8a"
299  "bd546a614b0e05433528b195870169a79ff23e1d58c64afad42ad81cffe53967e16be"
300  "b692fc5776bb442c79c5d91de00cf21804712806594010038e168a34104d9d84ebf65"
301  "22cf24c6fd4addedd068632a4db06c3cd6e40031c72d416e9eefbd90037383d8da9e9"
302  "213a4818a02f108ac1656141ecfbfdde0aeb8620eb210c08b2ce587d9fec0799f9725"
303  "5d2bc5a077c7b4e8ca8a68d6a0377abf0aa2473f97b37779431062dad50ef5fd21cf1"
304  "7c0276a293ee5b3f5a130fc5f9b217585cae23f",
305  ProofId::fromHex("17717dbf1cee175925be12321bb04eda7bc861a28b04d764c2d5"
306  "5eabf01cabd0"),
308  {"1 utxo staked but zero coins",
309  "a6d66db9fe9378fdd37a0ad2c01c2acd2103648144bb6a0c1d09b0f04d0df6d55f914"
310  "fd81efc65f23a718b68b7c9e42bd5430145a4d07798547464daa53acefb7c97c0c415"
311  "ed8e81e549ff56a0ef6f847fcc9ca855b3620000000000000000e707d7274104fb662"
312  "6e21dbd1cc9feeecdefc9213fdce2b51ac4bb44e1f8dc6f14c2052f5dd7bfaeb2267a"
313  "97ca2bec6e0dd4acf50a66204bde1ebb5d6c551684cff2f939920f7fbb2efd860d6d5"
314  "926bf425eb47b78bf6979cdcd67eb705e2c9a4d45a0930ba25463178a3fb99cb28c8b"
315  "77d8fcf68c54ebfadf08b9a446c251a0088301c50d53",
316  ProofId::fromHex("32c852db61880e9e2d4de3edba2daf3f5449e00bf1bc5b18595f"
317  "80113d5b0722"),
319  };
320 
321  for (auto &c : testcases) {
322  CDataStream stream(ParseHex(c.hex), SER_NETWORK, 0);
323  Proof p;
324  stream >> p;
325  BOOST_CHECK_EQUAL(p.getId(), c.proofid);
326  BOOST_CHECK_EQUAL(p.getScore(), c.score);
327 
328  ProofValidationState state;
329  BOOST_CHECK_EQUAL(p.verify(state),
330  c.result == ProofValidationResult::NONE);
331  BOOST_CHECK(state.GetResult() == c.result);
332  }
333 }
334 
336  CCoinsView coinsDummy;
337  CCoinsViewCache coins(&coinsDummy);
338 
339  CKey key;
340  key.MakeNewKey(true);
341  const CPubKey pubkey = key.GetPubKey();
342 
343  const Amount value = 12345 * SATOSHI;
344  const uint32_t height = 10;
345 
346  COutPoint pkh_outpoint(TxId(InsecureRand256()), InsecureRand32());
347  CTxOut pkh_output(value, GetScriptForRawPubKey(pubkey));
348  coins.AddCoin(pkh_outpoint, Coin(pkh_output, height, false), false);
349 
350  COutPoint nonstd_outpoint(TxId(InsecureRand256()), InsecureRand32());
351  CTxOut nonstd_output(value, CScript() << OP_TRUE);
352  coins.AddCoin(nonstd_outpoint, Coin(nonstd_output, height, false), false);
353 
354  COutPoint p2sh_outpoint(TxId(InsecureRand256()), InsecureRand32());
355  CTxOut p2sh_output(value,
356  GetScriptForDestination(ScriptHash(InsecureRand160())));
357  coins.AddCoin(p2sh_outpoint, Coin(p2sh_output, height, false), false);
358 
359  const auto runCheck = [&](const ProofValidationResult result,
360  const COutPoint &o, const Amount v,
361  const uint32_t h, const bool is_coinbase,
362  const CKey &k) {
363  // Generate a proof that match the UTXO.
364  ProofBuilder pb(0, 0, pubkey);
365  pb.addUTXO(o, v, h, is_coinbase, k);
366  Proof p = pb.build();
367 
368  ProofValidationState state;
369  BOOST_CHECK(p.verify(state));
370  BOOST_CHECK(p.verify(state, coins) ==
371  (result == ProofValidationResult::NONE));
372  BOOST_CHECK(state.GetResult() == result);
373  };
374 
375  // Valid proof
376  runCheck(ProofValidationResult::NONE, pkh_outpoint, value, height, false,
377  key);
378 
379  // Coinbase mismatch
380  runCheck(ProofValidationResult::COINBASE_MISMATCH, pkh_outpoint, value,
381  height, true, key);
382 
383  // Height mismatch
384  runCheck(ProofValidationResult::HEIGHT_MISMATCH, pkh_outpoint, value,
385  height + 1, false, key);
386 
387  // Amount mismatch
388  runCheck(ProofValidationResult::AMOUNT_MISMATCH, pkh_outpoint,
389  value + 1 * SATOSHI, height, false, key);
390 
391  // Invalid outpoints
393  COutPoint(pkh_outpoint.GetTxId(), pkh_outpoint.GetN() + 1), value,
394  height, false, key);
396  COutPoint(TxId(InsecureRand256()), pkh_outpoint.GetN()), value,
397  height, false, key);
398 
399  // Non standard script
400  runCheck(ProofValidationResult::NON_STANDARD_DESTINATION, nonstd_outpoint,
401  value, height, false, key);
402 
403  // Non PKHhash destination
404  runCheck(ProofValidationResult::DESTINATION_NOT_SUPPORTED, p2sh_outpoint,
405  value, height, false, key);
406 
407  // Mismatching key
408  {
409  CKey altkey;
410  altkey.MakeNewKey(true);
411  runCheck(ProofValidationResult::DESTINATION_MISMATCH, pkh_outpoint,
412  value, height, false, altkey);
413  }
414 
415  // No stake
416  {
417  Proof p = ProofBuilder(0, 0, pubkey).build();
418 
419  ProofValidationState state;
420  BOOST_CHECK(!p.verify(state, coins));
422  }
423 
424  // Dust thresold
425  {
426  ProofBuilder pb(0, 0, pubkey);
427  pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key);
428  Proof p = pb.build();
429 
430  ProofValidationState state;
431  BOOST_CHECK(!p.verify(state, coins));
433  }
434 
435  // Duplicated input
436  {
437  ProofBuilder pb(0, 0, pubkey);
438  pb.addUTXO(pkh_outpoint, value, height, false, key);
439  pb.addUTXO(pkh_outpoint, value, height, false, key);
440  Proof p = pb.build();
441 
442  ProofValidationState state;
443  BOOST_CHECK(!p.verify(state, coins));
444  BOOST_CHECK(state.GetResult() ==
446  }
447 }
448 
uint32_t GetN() const
Definition: transaction.h:44
ProofValidationResult result
Definition: proof_tests.cpp:71
static constexpr Amount zero()
Definition: amount.h:35
uint32_t score
Definition: proof_tests.cpp:70
const CPubKey & getMaster() const
Definition: proof.h:113
A UTXO entry.
Definition: coins.h:27
int64_t getExpirationTime() const
Definition: proof.h:112
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
uint256 GetRandHash() noexcept
Definition: random.cpp:659
BOOST_AUTO_TEST_CASE(proof_random)
Definition: proof_tests.cpp:23
Definition: amount.h:17
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:253
const TxId & GetTxId() const
Definition: transaction.h:43
uint32_t getScore() const
Definition: proof.cpp:41
static constexpr Amount SATOSHI
Definition: amount.h:151
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:196
Proof buildRandomProof(uint32_t score, const CPubKey &master)
Definition: util.cpp:18
static constexpr Amount COIN
Definition: amount.h:153
Abstract view on the open txout dataset.
Definition: coins.h:175
ProofId proofid
Definition: proof_tests.cpp:69
const char * name
Definition: rest.cpp:43
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:103
An encapsulated public key.
Definition: pubkey.h:31
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
Result GetResult() const
Definition: validation.h:119
An output of a transaction.
Definition: transaction.h:141
bool verify(ProofValidationState &state) const
Definition: proof.cpp:52
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:246
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
ProofValidationResult
Definition: validation.h:12
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
A TxId is the identifier of a transaction.
Definition: txid.h:14
std::vector< uint8_t > ParseHex(const char *psz)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
static ProofId fromHex(const std::string &str)
Definition: proofid.h:18
An encapsulated secp256k1 private key.
Definition: key.h:25
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
Definition: script.h:56
uint64_t getSequence() const
Definition: proof.h:111
#define BOOST_CHECK(expr)
Definition: object.cpp:17