How to create an inventory message and send it?

0

I am trying to create an inventory message of specific hashes that I select. Assuming I have all the hashes stored in a vector, how do I create the message 'manually'? (i.e stuff like getting total length, generating checksum and adding in all the inventory vector elements to the message and then sending it to all my peers.)

Any help is appreciated, if you can point me at any Bitcoin core (v0.14 or v0.15) source code that handles this kind of stuff that is also welcome.

Nabeel

Posted 2017-10-20T17:45:49.417

Reputation: 37

Answers

1

You can use Peter Todd's python-bitcoinlib, which contains builders for all the network messages.

Simple example, given a list of hashes:

from bitcoin.net import *
from bitcoin.messages import *

m = msg_inv()
for h in hashes:
    assert len(h) == 32
    inv = CInv()
    inv.type = 1  # TX
    inv.hash = h
    m.inv.append(inv)

inv_msg = m.serialize()

This assumes that the list of hashes you have are little endian unhexlified strings.

sr-gi

Posted 2017-10-20T17:45:49.417

Reputation: 2 382

Thank you for this, its a good starting point for what I aim to do. Do you have any suggestions for how to do this on a source code level in Bitcoin core?Nabeel 2017-10-21T23:14:00.247

Not really, sorry. There's a few Core devs on this SE, they may be able to help you though.sr-gi 2017-10-21T23:18:45.923