diff --git a/src/avalanche/delegationbuilder.cpp b/src/avalanche/delegationbuilder.cpp --- a/src/avalanche/delegationbuilder.cpp +++ b/src/avalanche/delegationbuilder.cpp @@ -37,8 +37,10 @@ } bool DelegationBuilder::addLevel(const CKey &key, const CPubKey &master) { - // Ensures that the private key provided is the one we need. - if (levels.back().pubkey != key.GetPubKey()) { + // Ensures that the private key provided is the one we need and we're not + // delegating to the same public key. + const CPubKey ¤tPubKey = levels.back().pubkey; + if (currentPubKey != key.GetPubKey() || currentPubKey == master) { return false; } diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -251,8 +251,9 @@ // Generate the delegation to the session key. DelegationBuilder dgb(peerData->proof); - if (sessionKey.GetPubKey() != peerData->proof.getMaster()) { - dgb.addLevel(masterKey, sessionKey.GetPubKey()); + if (!dgb.addLevel(masterKey, sessionKey.GetPubKey())) { + error = _("failed to add a level of delegation"); + return nullptr; } peerData->delegation = dgb.build(); } diff --git a/test/functional/abc_rpc_avalancheproof.py b/test/functional/abc_rpc_avalancheproof.py --- a/test/functional/abc_rpc_avalancheproof.py +++ b/test/functional/abc_rpc_avalancheproof.py @@ -209,6 +209,25 @@ expected_msg="Error: the avalanche master key is invalid", ) + # Wrong key + node.assert_start_raises_init_error( + self.extra_args[0] + [ + "-avaproof={}".format(proof), + "-avamasterkey=cN55daf1HotwBAgAKWVgDcoppmUNDtQSfb7XLutTLeAgVc3u8hik", + ], + expected_msg="Error: failed to add a level of delegation", + ) + + # Delegating to self + node.assert_start_raises_init_error( + self.extra_args[0] + [ + "-avaproof={}".format(proof), + "-avamasterkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", + "-avasessionkey=cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN", + ], + expected_msg="Error: failed to add a level of delegation", + ) + def check_proof_init_error(proof, message): node.assert_start_raises_init_error( self.extra_args[0] + [