Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event callbacks for Network and Chain Events #2598

Merged
merged 12 commits into from
Feb 26, 2019

Conversation

mcdallas
Copy link
Contributor

@mcdallas mcdallas commented Feb 19, 2019

This allows for hooks on the following events:

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

How to use:

for webhooks, add the urls to your config (http only)

[server.webhook_config]
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"

Each time an event triggers, a POST request will be made to the urls configured.

To add your own custom callbacks edit servers/src/common/hooks.rs, implements the corresponding trait and add your hook to the init function

TODO:

  • https support
  • configurable timeout
  • more logging

Closes #2321

@bladedoyle
Copy link
Contributor

See also: #2538
See also: #2321

@mcdallas mcdallas marked this pull request as ready for review February 20, 2019 23:10
fn parse_url(value: &Option<String>) -> Option<hyper::Uri> {
match value {
Some(url) => {
let uri: hyper::Uri = url.parse().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no unwrap

Some(url) => {
let uri: hyper::Uri = url.parse().unwrap();
let scheme = uri.scheme_part().map(|s| s.as_str());
if scheme != Some("http") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about https?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For TLS an extra crate is needed https://hyper.rs/guides/client/configuration/ . Not sure if it is worth it as I expect this to be used mostly with internal apis and the info it broadcasts is public.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that a consumer service could be https-only. Btw grin api client supports it already, so the crate is already here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then, will add https support

servers/src/common/hooks.rs Show resolved Hide resolved
servers/src/common/hooks.rs Outdated Show resolved Hide resolved
servers/src/common/hooks.rs Outdated Show resolved Hide resolved
servers/src/common/hooks.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@rentenmark rentenmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the caller should be aware of the backpressure it is creating, if the client is taking too long to process the requests it could result in a DOS for the client. It is best practice, and it is not easy to implement but I thought it was worth mentioning. I don't expect that it will be implemented in this first cut.

});

let handle = self.runtime.executor();
handle.spawn(future);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is non-blocking?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes tokio is async. By default it spawns 1 worker thread per virtual core per reactor.

let future = self
.client
.request(req)
.map(|_res| {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a trace or debug showing result? could be useful for debugging

HeaderValue::from_static("application/json"),
);

let future = self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configurable timeout?

@0xmichalis
Copy link
Contributor

@mcdallas do you plan to add docs in this PR, separate PR, or no plans to add docs? A HOWTO in the wiki would be nice.

@mcdallas
Copy link
Contributor Author

@Kargakis yes I will add a section in the wiki when/if it gets merged

@ignopeverell
Copy link
Contributor

How should we consider this? Being a new feature, it seems complete enough to merge and then improve upon. But if the improvements are forthcoming, then it's likely better to wait.

@mcdallas
Copy link
Contributor Author

Rebased on latest. @ignopeverell I am on the road and probably wont be able to finish it this week. If you want to merge I can send the rest next week on a separate PR

@ignopeverell ignopeverell merged commit d560a36 into mimblewimble:milestone/1.1.0 Feb 26, 2019
@mcdallas
Copy link
Contributor Author

@Kargakis I documented the new feature here: https://github.com/mimblewimble/docs/wiki/Event-hooks

@0xmichalis
Copy link
Contributor

@mcdallas thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants