diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4017,6 +4017,15 @@ SchnorrSig sig; verifier >> sig; + if (!pubkey.VerifySchnorr(g_avalanche->buildRemoteSighash(&pfrom), + sig)) { + Misbehaving(pfrom, 100, "invalid-avahello-signature"); + return; + } + LogPrint(BCLog::NET, + "Successfully checked AVAHELLO signature for peer=%d\n", + pfrom.GetId()); + return; } if (msg_type == NetMsgType::AVAPOLL) { diff --git a/test/functional/abc_p2p_avalanche.py b/test/functional/abc_p2p_avalanche.py --- a/test/functional/abc_p2p_avalanche.py +++ b/test/functional/abc_p2p_avalanche.py @@ -4,17 +4,23 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the resolution of forks via avalanche.""" import random +import struct from test_framework.avatools import create_coinbase_stakes from test_framework.key import ( + bytes_to_wif, ECKey, ECPubKey, ) from test_framework.mininode import P2PInterface, mininode_lock from test_framework.messages import ( + AvalancheDelegation, AvalancheResponse, AvalancheVote, CInv, + FromHex, + hash256, + msg_avahello, msg_avapoll, msg_tcpavaresponse, NODE_AVALANCHE, @@ -110,6 +116,22 @@ with mininode_lock: return self.avahello + def _getLocalSigHash(self, delegation_id: bytes) -> bytes: + b = delegation_id + b += struct.pack(" P2PInterface)") + interface = get_node() + avahello = interface.wait_for_avahello().hello avakey.set(bytes.fromhex(node.getavalanchekey())) assert avakey.verify_schnorr( - avahello.sig, avahello.get_sighash(poll_node)) + avahello.sig, avahello.get_sighash(interface)) + + self.log.info("Test the avahello signature (P2PInterface -> node)") + stakes = create_coinbase_stakes(node, [blockhashes[1]], addrkey0.key) + interface_proof_hex = node.buildavalancheproof( + proof_sequence, proof_expiration, pubkey.get_bytes().hex(), + stakes) + # delegate + delegated_key = ECKey() + delegated_key.generate() + interface_delegation_hex = node.delegateavalancheproof( + interface_proof_hex, + bytes_to_wif(privkey.get_bytes()), + delegated_key.get_pubkey().get_bytes().hex(), + None) + + with self.nodes[0].assert_debug_log( + ["Successfully checked AVAHELLO signature for peer=0"]): + interface.send_avahello(interface_delegation_hex, delegated_key) + + self.log.info("Test a wrong avahello signature") + interface = get_node() + wrong_key = ECKey() + wrong_key.generate() + with self.nodes[0].assert_debug_log( + ["Misbehaving", + "peer=1 (0 -> 100) BAN THRESHOLD EXCEEDED: invalid-avahello-signature"]): + interface.send_avahello(interface_delegation_hex, wrong_key) if __name__ == '__main__':