Bitcoin ABC  0.24.7
P2P Digital Currency
delegation_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/delegation.h>
7 #include <avalanche/test/util.h>
8 #include <avalanche/validation.h>
9 #include <streams.h>
10 
11 #include <test/util/setup_common.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 #include <streams.h>
16 #include <util/strencodings.h>
17 
18 using namespace avalanche;
19 
20 BOOST_FIXTURE_TEST_SUITE(delegation_tests, TestingSetup)
21 
22 static void CheckDelegation(const Delegation &dg, const ProofRef &p,
23  const CPubKey &expected_pubkey) {
24  DelegationState state;
25  CPubKey pubkey;
26  BOOST_CHECK(dg.verify(state, pubkey));
28  BOOST_CHECK(pubkey == expected_pubkey);
29 
30  BOOST_CHECK(dg.getProofId() == p->getId());
31 }
32 
33 BOOST_AUTO_TEST_CASE(verify_random) {
34  auto key = CKey::MakeCompressedKey();
35 
36  auto p = buildRandomProof(123456, key);
37  DelegationBuilder dgb(*p);
38 
39  {
40  Delegation dg = dgb.build();
41  BOOST_CHECK_EQUAL(dg.getId(), p->getId());
42  CheckDelegation(dg, p, p->getMaster());
43  }
44 
45  auto l1key = CKey::MakeCompressedKey();
46  BOOST_CHECK(!dgb.addLevel(l1key, key.GetPubKey()));
47 
48  dgb.addLevel(key, l1key.GetPubKey());
49  CheckDelegation(dgb.build(), p, l1key.GetPubKey());
50 
51  auto l2key = CKey::MakeCompressedKey();
52  BOOST_CHECK(!dgb.addLevel(key, l2key.GetPubKey()));
53  BOOST_CHECK(!dgb.addLevel(l2key, l2key.GetPubKey()));
54 
55  dgb.addLevel(l1key, l2key.GetPubKey());
56  CheckDelegation(dgb.build(), p, l2key.GetPubKey());
57 }
58 
59 // Proof master priv:
60 // L4J6gEE4wL9ji2EQbzS5dPMTTsw8LRvcMst1Utij4e3X5ccUSdqW
61 // Proof master pub:
62 // 023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3
63 // Stake priv:
64 // KydYrKDNsVnY5uhpLyC4UmazuJvUjNoKJhEEv9f1mdK1D5zcnMSM
65 // Stake pub:
66 // 02449fb5237efe8f647d32e8b64f06c22d1d40368eaca2a71ffc6a13ecc8bce680
67 // Level 1 priv:
68 // KzzLLtiYiyFcTXPWUzywt2yEKk5FxkGbMfKhWgBd4oZdt8t8kk77
69 // Level 1 pub:
70 // 03e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645ef
71 // Level 2 priv:
72 // KwM6hV6hxZt3Kt4NHMtWQGH5T2SwhpyswodUQC2zmSjg6KWFWkQU
73 // Level 2 pub:
74 // 03aac52f4cfca700e7e9824298e0184755112e32f359c832f5f6ad2ef62a2c024a
75 
76 static Proof getProof() {
77  Proof p;
78  CDataStream stream(
79  ParseHex(
80  "0000000000000000ffffffff0000000021023beefdde700a6bc02036335b4df141"
81  "c8bc67bb05a971f5ac2745fd683797dde301bee72758084395310b5a7ccc98a836"
82  "11dff786f0a469d1d66626ba286b0423870000000000108dbe1c000000a4090000"
83  "2102449fb5237efe8f647d32e8b64f06c22d1d40368eaca2a71ffc6a13ecc8bce6"
84  "8069c539018ac799848811fb44a4b987faa71a634970d35976c5e766fb98502432"
85  "aaec53034bd7df23767e7e695203599cf4a6a71569bdf03e90f0f91c8760faae"),
86  SER_NETWORK, 0);
87  stream >> p;
88  BOOST_CHECK_EQUAL(p.getId(),
89  ProofId::fromHex("afc74900c1f28b69e466461fb1e0663352da615"
90  "3be0fcd59280e27f2446391d5"));
92  p.getLimitedId(),
93  LimitedProofId::fromHex("0d45ca55662c483107b45f5c5699e0d8c7778b2"
94  "45c116cb988abba1afa6a1146"));
95  return p;
96 }
97 
98 struct TestVector {
99  std::string name;
100  std::string hex;
101  std::string dgid;
102  std::string pubkey;
104 };
105 
106 BOOST_AUTO_TEST_CASE(deserialization) {
107  Proof p = getProof();
108 
109  std::vector<TestVector> testcases{
110  {"Empty delegation",
111  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
112  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
113  "00",
114  "afc74900c1f28b69e466461fb1e0663352da6153be0fcd59280e27f2446391d5",
115  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3",
117  {"One delegation",
118  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
119  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
120  "012103e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645e"
121  "f7d51"
122  "2ddbea7c88dcf38412b58374856a466e165797a69321c0928a89c64521f7e2e767c93"
123  "de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc",
124  "ffcd49dc98ebdbc90e731a7b0c89939bfe082f15f3aa82aca657176b83669185",
125  "03e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645ef",
127  {"Two delegation",
128  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
129  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
130  "022103e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645e"
131  "f7d512ddbea7c88dcf38412b58374856a466e165797a69321c0928a89c64521f7e2e7"
132  "67c93de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc2103aac52"
133  "f4cfca700e7e9824298e0184755112e32f359c832f5f6ad2ef62a2c024a5cddd0ffe8"
134  "4e12e4bf49e4c0af7c8548e618a24e12495d659f5ba75e114e1526a618aa305b1e69b"
135  "f6ae20b2557999f2e3fec25d5f2271f8b9de0d06ba7344550",
136  "a3f98e6b5ec330219493d109e5c11ed8e302315df4604b5462e9fb80cb0fde89",
137  "03aac52f4cfca700e7e9824298e0184755112e32f359c832f5f6ad2ef62a2c024a",
139  {"Invalid pubkey",
140  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
141  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3012"
142  "103e49f9df53de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645ef7d"
143  "512ddbea7c88dcf38412b58374856a466e165797a69321c0928a89c64521f7e2e767c"
144  "93de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc",
145  "af7e82716489c3cf3f361d449ed815112ff619f7fc34a4803bd958c68d1e2684",
146  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3",
148  {"Invalid signature",
149  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
150  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
151  "012103e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645e"
152  "f7d512ddbea7c88dcf38412c58374856a466e165797a69321c0928a89c64521f7e2e7"
153  "67c93de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc",
154  "ffcd49dc98ebdbc90e731a7b0c89939bfe082f15f3aa82aca657176b83669185",
155  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3",
157  {"Second invalid key",
158  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
159  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
160  "022103e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645e"
161  "f7d512ddbea7c88dcf38412b58374856a466e165797a69321c0928a89c64521f7e2e7"
162  "67c93de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc2103aac52"
163  "f4dfca700e7e9824298e0184755112e32f359c832f5f6ad2ef62a2c024a5cddd0ffe8"
164  "4e12e4bf49e4c0af7c8548e618a24e12495d659f5ba75e114e1526a618aa305b1e69b"
165  "f6ae20b2557999f2e3fec25d5f2271f8b9de0d06ba7344550",
166  "b474512f71a3f5a6e94cc3b958fd658ece0d0632ace58c8c8f9f65c2b9ad5fad",
167  "03e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645ef",
169  {"Second invalid signature",
170  "46116afa1abaab88b96c115c248b77c7d8e099565c5fb40731482c6655ca450d21"
171  "023beefdde700a6bc02036335b4df141c8bc67bb05a971f5ac2745fd683797dde3"
172  "022103e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645e"
173  "f7d512ddbea7c88dcf38412b58374856a466e165797a69321c0928a89c64521f7e2e7"
174  "67c93de645ef5125ec901dcd51347787ca29771e7786bbe402d2d5ead0dc2103aac52"
175  "f4cfca700e7e9824298e0184755112e32f359c832f5f6ad2ef62a2c024a5cddd0ffe8"
176  "4e12e4bf49e4c0af7c8548e618a24e12495d659f5ba75e114e1526a618aa305b1e69b"
177  "f6ae20b2557999f2e3fec25d5f2271f8b9de0d06ba7344551",
178  "a3f98e6b5ec330219493d109e5c11ed8e302315df4604b5462e9fb80cb0fde89",
179  "03e49f9df52de2dea81cf7838b82521b69f2ea360f1c4eed9e6c89b7d0f9e645ef",
181  };
182 
183  for (auto &c : testcases) {
184  CDataStream stream(ParseHex(c.hex), SER_NETWORK, 0);
185  Delegation dg;
186  stream >> dg;
187  BOOST_CHECK_EQUAL(dg.getId(), DelegationId::fromHex(c.dgid));
188 
189  DelegationState state;
190  CPubKey pubkey;
191  BOOST_CHECK_EQUAL(dg.verify(state, pubkey),
192  c.result == DelegationResult::NONE);
193  BOOST_CHECK(state.GetResult() == c.result);
194  BOOST_CHECK(pubkey == CPubKey(ParseHex(c.pubkey)));
195  }
196 }
197 
avalanche::Delegation
Definition: delegation.h:23
ParseHex
std::vector< uint8_t > ParseHex(const char *psz)
Definition: strencodings.cpp:87
delegation.h
TestVector
Definition: delegation_tests.cpp:98
streams.h
avalanche
Definition: avalanche.h:11
CheckDelegation
static void CheckDelegation(const Delegation &dg, const ProofRef &p, const CPubKey &expected_pubkey)
Definition: delegation_tests.cpp:22
avalanche::DelegationBuilder::build
Delegation build() const
Definition: delegationbuilder.cpp:61
delegationbuilder.h
TestVector::pubkey
std::string pubkey
Definition: delegation_tests.cpp:102
TestVector::result
DelegationResult result
Definition: delegation_tests.cpp:103
avalanche::DelegationResult::INVALID_SIGNATURE
@ INVALID_SIGNATURE
avalanche::DelegationId::fromHex
static DelegationId fromHex(const std::string &str)
Definition: delegationid.h:18
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
avalanche::buildRandomProof
ProofRef buildRandomProof(uint32_t score, const CKey &masterKey)
Definition: util.cpp:20
getProof
static Proof getProof()
Definition: delegation_tests.cpp:76
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(verify_random)
Definition: delegation_tests.cpp:33
validation.h
strencodings.h
TestVector::hex
std::string hex
Definition: delegation_tests.cpp:100
avalanche::DelegationState
Definition: validation.h:40
avalanche::DelegationBuilder
Definition: delegationbuilder.h:20
avalanche::LimitedProofId::fromHex
static LimitedProofId fromHex(const std::string &str)
Definition: proofid.h:32
ValidationState::GetResult
Result GetResult() const
Definition: validation.h:121
avalanche::Proof
Definition: proof.h:102
util.h
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
avalanche::Delegation::getId
const DelegationId & getId() const
Definition: delegation.h:52
CKey::MakeCompressedKey
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition: key.cpp:466
TestVector::name
std::string name
Definition: delegation_tests.cpp:99
avalanche::ProofId::fromHex
static ProofId fromHex(const std::string &str)
Definition: proofid.h:21
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163
avalanche::DelegationBuilder::addLevel
bool addLevel(const CKey &delegatorKey, const CPubKey &delegatedPubKey)
Definition: delegationbuilder.cpp:40
avalanche::DelegationResult::NONE
@ NONE
TestVector::dgid
std::string dgid
Definition: delegation_tests.cpp:101
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
avalanche::DelegationResult
DelegationResult
Definition: validation.h:35