I'm going to try something new with this post: Sharing small specific howtos to help other developers integrate code with Steem. Today we'll look at processing blocks as they come in. We can actually get event notifications for new blocks using the `set_block_applied_callback` function, which is defined in `database_api.hpp` as follows [1]: void set_block_applied_callback( std::function<void(const variant& block_header)> cb ); This is confusing -- it takes a single parameter which is a callback function. While you can create an `std::function` in C++, how's it possible to pass this function over the network or use this API from non-C++ languages? The answer is that instead of accepting actual functions, the steemd server's API's accept a number representing a function. This number is then passed by server-pushed callback notifications, and the client is responsible for routing the notification to the proper client-side callback function. Because notifications require server push, we have to use a websocket connection. Here's a simple example with the `wscat` utility which lets us interact with websockets in the terminal: $ wscat -c ws://127.0.0.1:8090/ > {"jsonrpc": "2.0", "method": "call", "params": ["database_api","set_block_applied_callback",[1234]], "id": 1} < {"id":1,"result":null} < {"method":"notice","params":[1234,[{"previous":"0033cb1b...","timestamp":"2016-07-21T17:40:36","witness":"xeldal",...}]]} < {"method":"notice","params":[1234,[{"previous":"0033cb1c...","timestamp":"2016-07-21T17:40:39","witness":"blocktrades",...}]]} < {"method":"notice","params":[1234,[{"previous":"0033cb1d...","timestamp":"2016-07-21T17:40:42","witness":"bhuz",...}]]} This example should be very enlightening. We called `set_block_applied_callback` to establish a subscription, and the server started sending us notifications. The number the client passes as the callback can be any value, it is simply echoed by the server with each notification. If we subscribe to multiple callbacks on the same websocket connection, the client needs some way to route each notification to its corresponding subscription -- so it should simply assign diffferent callback ID's for each subscription. Now we want to call `get_block` to get the full block data, for that we need a block number ("block height" in Bitcoin terminology). In Steem, the first 4 bytes (8 hex digits) of the block ID represents the block number, so we can simply take the first 8 characters of the `previous` member, convert to integer and add 1 to get the block number which can be passed to the `get_block` API call: > {"jsonrpc": "2.0", "method": "call", "params": ["database_api","get_block",[3394332]], "id": 1} < {"id":1,"result":{"previous":"0033cb1b171069d351390acdc91b962a2b5bb6db","timestamp":"2016-07-21T17:40:36","witness":"xeldal",...}} [1] Even if you're not a C++ coder, looking at the `*_api.hpp` files in the Steem source tree is very helpful because it will tell you what API methods are available and what parameters they take.
author | theoretical |
---|---|
permlink | dev-snippet-how-to-subscribe-to-blocks |
category | steem |
json_metadata | {"tags":["steem"]} |
created | 2016-07-21 18:16:06 |
last_update | 2016-07-21 18:16:06 |
depth | 0 |
children | 5 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 33.178 HBD |
curator_payout_value | 5.724 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,076 |
author_reputation | 30,164,760,525,645 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 253,945 |
net_rshares | 9,451,449,822,342 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
theoretical | 0 | 214,350,094,953 | 100% | ||
wang | 0 | 3,500,490,423,736 | 100% | ||
clayop | 0 | 5,347,867,897,932 | 100% | ||
bonapartist | 0 | 0 | 100% | ||
yefet | 0 | 21,390,171,756 | 100% | ||
joelinux | 0 | 17,669,486,112 | 100% | ||
paco-steem | 0 | 461,697,266 | 100% | ||
spaninv | 0 | 4,872,903,721 | 100% | ||
coar | 0 | 896,399,358 | 100% | ||
stiletto | 0 | 353,805,817 | 100% | ||
wingz | 0 | 123,588,010,736 | 100% | ||
will-zewe | 0 | 163,094,973,533 | 100% | ||
tee-em | 0 | 862,806,958 | 100% | ||
ausbitbank | 0 | 12,556,289,909 | 100% | ||
egabragsiyrallih | 0 | 1,366,261,845 | 100% | ||
aaseb | 0 | 3,408,601,544 | 100% | ||
str11ngfello | 0 | 12,548,467,705 | 100% | ||
jearson | 0 | 233,185,934 | 100% | ||
luisucv34 | 0 | 17,213,722 | 100% | ||
q00p | 0 | 150,901,497 | 0% | ||
irina | 0 | 255,168,110 | 100% | ||
bitcoiner | 0 | 111,932,087 | 100% | ||
mookosoo | 0 | 2,470,629,745 | 100% | ||
qonq99 | 0 | 7,113,004 | 100% | ||
maurizio | 0 | 13,968,746 | 100% | ||
krabgat | 0 | 522,277,317 | 100% | ||
fatboy | 0 | 21,190,452,560 | 100% | ||
yarly10 | 0 | 135,316,554 | 100% | ||
kukuy | 0 | 38,301,863 | 100% | ||
manofsteem | 0 | 97,310,931 | 100% | ||
nigroll | 0 | 86,776,665 | 100% | ||
dmilash | 0 | 79,996,637 | 100% | ||
nicoleta | 0 | 111,105,149 | 100% | ||
spinner | 0 | 84,247,299 | 100% | ||
weenis | 0 | 6,268,424 | 100% | ||
dez1337 | 0 | 0 | 100% | ||
john-ag | 0 | 59,363,217 | 100% |
very interesting ,i hope more people will use it. Thank you
author | nicoleta |
---|---|
permlink | re-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160721t182101081z |
category | steem |
json_metadata | {"tags":["steem"]} |
created | 2016-07-21 18:21:00 |
last_update | 2016-07-21 18:21:00 |
depth | 1 |
children | 0 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 59 |
author_reputation | 137,518,217,509 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 254,097 |
net_rshares | 2,542,037,823 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
mookosoo | 0 | 2,422,186,024 | 100% | ||
nicoleta | 0 | 108,883,046 | 100% | ||
alexander1 | 0 | 10,968,753 | 100% |
Hey, this is not in regards to this post, but i wanted to mention that i wrote about you in my latest post. Also, please fact check it because I am not sure that I described your role as mathematician of voting rewards algorithm..........let me know if i need to correct anything. https://steemit.com/vacation/@stellabelle/christmas-in-july-our-vacation-was-funded-entirely-by-steem-dollars
author | stellabelle |
---|---|
permlink | re-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160722t144039103z |
category | steem |
json_metadata | {"tags":["steem"],"links":["https://steemit.com/vacation/@stellabelle/christmas-in-july-our-vacation-was-funded-entirely-by-steem-dollars"]} |
created | 2016-07-22 14:40:39 |
last_update | 2016-07-22 14:40:39 |
depth | 1 |
children | 0 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 390 |
author_reputation | 516,061,669,130,124 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 274,806 |
net_rshares | 0 |
Lol great post)))
author | tatyanasvitsa |
---|---|
permlink | re-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160722t182332732z |
category | steem |
json_metadata | {"tags":["steem"]} |
created | 2016-07-21 18:18:12 |
last_update | 2016-07-21 18:18:12 |
depth | 1 |
children | 0 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 17 |
author_reputation | 37,999,020,213 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 254,000 |
net_rshares | 0 |
 Rock On @theoretical!
author | weenis |
---|---|
permlink | dev-snippet-how-to-subscribe-to-blocks |
category | steem |
json_metadata | "" |
created | 2016-07-21 18:16:42 |
last_update | 2016-07-21 18:16:42 |
depth | 1 |
children | 1 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 70 |
author_reputation | -4,781,861,673,917 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 253,962 |
net_rshares | 0 |
hello bot
author | coar |
---|---|
permlink | re-weenis-dev-snippet-how-to-subscribe-to-blocks-20160721t183511389z |
category | steem |
json_metadata | {"tags":["steem"]} |
created | 2016-07-21 18:35:12 |
last_update | 2016-07-21 18:35:12 |
depth | 2 |
children | 0 |
last_payout | 2016-08-23 12:26:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 9 |
author_reputation | 4,231,602,317,552 |
root_title | "Dev snippet: How to subscribe to blocks" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 254,475 |
net_rshares | 0 |