Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/delegation_tests.cpp
Show First 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | for (auto &c : testcases) { | ||||
CPubKey pubkey; | CPubKey pubkey; | ||||
BOOST_CHECK_EQUAL(dg.verify(state, p, pubkey), | BOOST_CHECK_EQUAL(dg.verify(state, p, pubkey), | ||||
c.result == DelegationResult::NONE); | c.result == DelegationResult::NONE); | ||||
BOOST_CHECK(state.GetResult() == c.result); | BOOST_CHECK(state.GetResult() == c.result); | ||||
BOOST_CHECK(pubkey == CPubKey(ParseHex(c.pubkey))); | BOOST_CHECK(pubkey == CPubKey(ParseHex(c.pubkey))); | ||||
} | } | ||||
} | } | ||||
static std::pair<Delegation, CKey> AddDelegationLevels(DelegationBuilder &dgb, | |||||
const Proof &p, | |||||
const CKey &master, | |||||
size_t levels) { | |||||
CKey privkey = master; | |||||
for (size_t i = 0; i < levels; i++) { | |||||
CKey k; | |||||
k.MakeNewKey(true); | |||||
BOOST_CHECK(dgb.addLevel(privkey, k.GetPubKey())); | |||||
privkey = k; | |||||
} | |||||
const Delegation dg = dgb.build(); | |||||
CheckDelegation(dg, p, privkey.GetPubKey()); | |||||
return std::pair<Delegation, CKey>(dg, privkey); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(importDelegation) { | |||||
CKey key; | |||||
key.MakeNewKey(true); | |||||
const Proof p = buildRandomProof(123456, key.GetPubKey()); | |||||
std::pair<Delegation, CKey> dgKey5, dgKey10; | |||||
{ | |||||
DelegationBuilder dgb(p); | |||||
dgKey5 = AddDelegationLevels(dgb, p, key, 5); | |||||
dgKey10 = AddDelegationLevels(dgb, p, dgKey5.second, 5); | |||||
BOOST_CHECK(dgKey5.first.getId() != dgKey10.first.getId()); | |||||
} | |||||
{ | |||||
DelegationBuilder dgb(p); | |||||
// Import 5 levels delegation with no pubkey limit | |||||
BOOST_CHECK(dgb.importDelegation(dgKey5.first, CPubKey())); | |||||
BOOST_CHECK(dgb.build().getId() == dgKey5.first.getId()); | |||||
// We cannot import twice | |||||
BOOST_CHECK(!dgb.importDelegation(dgKey5.first, CPubKey())); | |||||
} | |||||
{ | |||||
DelegationBuilder dgb(p); | |||||
// Import the 5 levels delegation with the last pubkey as a limit | |||||
BOOST_CHECK( | |||||
dgb.importDelegation(dgKey5.first, dgKey5.second.GetPubKey())); | |||||
BOOST_CHECK(dgb.build().getId() == dgKey5.first.getId()); | |||||
} | |||||
{ | |||||
DelegationBuilder dgb(p); | |||||
// Import the 10 levels delegation with the last pubkey as a limit | |||||
BOOST_CHECK( | |||||
dgb.importDelegation(dgKey10.first, dgKey10.second.GetPubKey())); | |||||
BOOST_CHECK(dgb.build().getId() == dgKey10.first.getId()); | |||||
} | |||||
{ | |||||
DelegationBuilder dgb(p); | |||||
// Import the 10 levels delegation with the last pubkey from the 5 | |||||
// levels delegation | |||||
BOOST_CHECK( | |||||
dgb.importDelegation(dgKey10.first, dgKey5.second.GetPubKey())); | |||||
const Delegation dg = dgb.build(); | |||||
BOOST_CHECK(dg.getId() == dgKey5.first.getId()); | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |