Skip to content

Instantly share code, notes, and snippets.

@jimmysong
Last active March 22, 2017 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimmysong/4c72d098255b217b4c8c to your computer and use it in GitHub Desktop.
Save jimmysong/4c72d098255b217b4c8c to your computer and use it in GitHub Desktop.
How to test bitcoin alerts

How to test bitcoin alerts

Motivation

Programming for bitcoin can be a bit of a pain at times. There's a lot of esoteric knowledge required and not everything is documented that well, though with some of the newer documentation projects like the bitcoin developer documentation, it's getting easier.

One thing I ran into recently is the existence of bitcoin alerts. These are alerts that are sent by the core developers onto the entire bitcoin network to warn of serious security events. They don't come up very often (there's only been 8 in the entire history of bitcoin as of this writing), but if your application is built on top of it, you'd better handle it.

The problem, though, is that the alerts are actually signed by a key controlled by the core developers. You can't just create new alerts for testnet, for example, as every other bitcoin client, including your own, will ignore them. So how do you test them?

It's not easy, and you'll have to patch bitcoind in order to do it, but it can be done. You will need two places where you can run the bitcoin binary.

Shortcut

If you don't feel like doing all of the below and would rather just get some source code and build, you can use my branch and skip to step 5.

Overview

  1. Clone bitcoin
  2. Add sendalert.cpp
  3. Patch the alert functions
  4. Patch the Makefile and init.cpp
  5. Build the binary
  6. Run the binary in two different places that connect to each other.
  7. Wait.

Clone bitcoin

The very first thing you'll need to do is to fork the bitcoin repository. On github, you can just click "Fork" or you can clone the repository and make all the changes locally. Either way, you're going to need your own copy of the bitcoin source code.

Add sendalert.cpp

This is a modified version of the gist found here. You will need to put this file into the src directory.

Optionally, you can edit the actual alert properties like the comment text, status bar text, what alerts to actually now ignore, etc. You can see the full alert specification here.

Patch the alert functions

In src/alert.cpp, make these functions return true:

  • IsInEffect
  • AppliesToMe
  • CheckSignature

Patch the Makefile and init.cpp

Near the top of init.cpp, you need to put this code:

extern void ThreadSendAlert();

Near the end of the file right before:

return !fRequestShutdown;

put in this line:

threadGroup.create_thread(boost::bind(ThreadSendAlert));

You also need to add sendalert.cpp into Makefile.am by adding sendalert.cpp to the list of libbitcoin_server_a_SOURCES. This can be done after this line:

txmempool.cpp \

and adding this line after:

sendalert.cpp \

Build the binary

Follow the instructions for unix or mac.

Run the binary in two different places

You'll need two machines, though it may be possible to do with just one using different ports, I haven't been able to figure this out. You will need the ip addresses of each machine. Say you have machine A and B. You want machine B to receive an alert.

From machine A you'll need to run:

./bitcoind -printtoconsole -sendalert -testnet -addnode=<ip of machine B>

From machine B you'll need to run:

./bitcoind -printtoconsole -sendalert -testnet -addnode=<ip of machine A>

Make sure they're started relatively closely (within 5 seconds of each other).

Wait

About 1 minute after you start, you should see an alert in the console of machine B that looks like this:

ThreadSendAlert:
hash=14c7c1217b54b953a81b08e0be16b9a56a9365240169d808424d5e7dd5690769
vchMsg=010000003271cd5400000000c2ffdb5400000000150400002a000000021004000012040000721101007311010002102f5361746f7368693a302e31302e302f0f2f5361746f7368693a302e392e332f88130000177465737420636f6d6d656e742031343232373530303032165445535420414c4552543a203134323337303430303210746573742072657365727665642e2e2e
vchSig=304402204cf39bec79eb10ad056f7a1ca48cfda3d612444a22eead543f345ae5f9c483df02204fc13370a17c639b796061ed39f294dd145363b3b953b9b4475da3b40bcd3f0e
ThreadSendAlert() : Sending alert
attempting relay
ThreadSendAlert() : Sent alert to x.x.x.x:18333
attempting relay
ThreadSendAlert() : Sent alert to x.x.x.x:18333
attempting relay
ThreadSendAlert() : Sent alert to x.x.x.x:18333
attempting relay
ThreadSendAlert() : Sent alert to x.x.x.x:18333
attempting relay
ThreadSendAlert() : Sent alert to x.x.x.x:18333
ThreadSendAlert() : Alert sent to 5 nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment