Skip to content
Mike Dallas edited this page Mar 12, 2019 · 1 revision

Grin 1.1.0 introduces event hooks for the following events:

  • New Transaction received
  • New Header received
  • New Block received
  • New Block accepted

Webhooks

You can configure webhooks on the events mentioned above by adding the following to your grin-server.toml configuration file:

[server.webhook_config]
nthreads = 4
timeout = 10
block_accepted_url = "http://127.0.0.1:8080/acceptedblock"
tx_received_url = "http://127.0.0.1:8080/tx"
header_received_url = "http://127.0.0.1:8080/header"
block_received_url = "http://127.0.0.1:8080/block"

nthreads: The number of worker threads that will be assigned to making the http requests.

timeout: The timeout of the http request in seconds.

block_accepted_url: The url where a POST request will be sent when a new block is accepted by our node.

tx_received_url: The url where a POST request will be sent when a new transaction is received by a peer.

header_received_url: The url where a POST request will be sent when a new header is received by a peer.

block_received_url: The url where a POST request will be sent when a new block is received by a peer.

The json payload that will be sent to those urls contains the block/tx/header serialized as json plus some metadata (Block status/Peer address etc)

Custom Event Callbacks

If you want to add your own custom callbacks you will have to edit the servers/src/common/hooks.rs file.

Say you want to implement an event logger that prints information to stdout on new network events (header/block/tx received).

First you have to implement the NetEvents trait (or the ChainEvents trait for chain events):


struct MyAwesomeLogger;

impl NetEvents for MyAwesomeLogger {
	fn on_transaction_received(&self, tx: &core::Transaction) {
                // Add here what you want to happen when a new transaction is received.
                println!("Received transaction: {}", tx.hash()); 		
	}

	fn on_block_received(&self, block: &core::Block, addr: &PeerAddr) {
                // Add here what you want to happen when a new block is received.
                println!("Received block: {}", block.hash());
	}

	fn on_header_received(&self, header: &core::BlockHeader, addr: &PeerAddr) {
                // Add here what you want to happen when a new header is received.
                println!("Received header: {}", header.hash());
	}
}

Then, add your newly created hook to the list of hooks to be initialized.

/// Returns the list of event hooks that will be initialized for network events
pub fn init_net_hooks(config: &ServerConfig) -> Vec<Box<dyn NetEvents + Send + Sync>> {
	let mut list: Vec<Box<NetEvents + Send + Sync>> = Vec::new();
	list.push(Box::new(EventLogger));
	list.push(Box::new(WebHook::from_config(&config.webhook_config)));
        // Add your new logger here.
        list.push(Box::new(MyAwesomeLogger));

	list
}

Close and save the file and then recompile with cargo build --release

Now if you run the newly created binary (with the TUI disabled) these messages will start popping in your console:

Received header: 53e721c24f34
Received header: 06ea5a5521f3
Received header: 174c7bc1dd82
Received header: 3d5b9e280cc1
Received block: 0abacecc9c2b
Received header: 4ddffdec0a1c
Received block: 22cecab8baa4
Clone this wiki locally