How to use postgrest with hivemind

in #tutorial3 days ago

image.png

If you haven't been following the recent development with hivemind. We are actively moving away from the python-based webserver to an sql only implementation where the webserver is postgresql itself. This leads to massive performance improvements. In order to do this we use an awfully named tool: Postgrest (note the T at the end)

This means that if you are a developer on hivemind and want to test an endpoint, you will need to do things a tad differently.

Step 1: install postgrest

Head on the official repo and download the version that corresponds to your needs:

https://github.com/PostgREST/postgrest/releases/tag/v12.0.2

Right now we use 12.0.2, but that number may change. You can always check this dockerfile to see if it's any different.

Once you get it it's a simple binary, so just shove that in /usr/bin and you're good to go:

sudo tar -xf postgrest-v12.0.2-linux-static-x64.tar.xz -C /usr/bin
# check if it was correctly installed
postgrest --version

Step 2: launching the server

Hivemind provides a handy script for that: https://gitlab.syncad.com/hive/hivemind/-/blob/develop/scripts/start_postgrest.sh?ref_type=heads

just execute:

/home/howo/hivemind/scripts/start_postgrest.sh --postgres-url=postgresql://postgres:@localhost:5432/haf_block_log
03/Jun/2025:14:37:42 +0900: Starting PostgREST 12.0.2...
03/Jun/2025:14:37:42 +0900: Attempting to connect to the database...
03/Jun/2025:14:37:42 +0900: Connection successful
03/Jun/2025:14:37:42 +0900: Admin server listening on port 3001
03/Jun/2025:14:37:42 +0900: Listening on port 8080
03/Jun/2025:14:37:42 +0900: Config reloaded
03/Jun/2025:14:37:42 +0900: Listening for notifications on the pgrst channel
03/Jun/2025:14:37:42 +0900: Schema cache loaded

Step 3: querying hivemind

At this point you're good to go ! But you'll notice that if you try to call an endpoint:

curl 'http://localhost:8080' --compressed -X POST  -H 'Content-Type: application/json' --data-raw '{"id":6,"jsonrpc":"2.0","method":"bridge.account_notifications","params":{"account":"howo","limit":1}}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   110    0     2  100   108   1831  98901 --:--:-- --:--:-- --:--:--  107k
{}

You get an empty response, that's because in order to support the hivemind format and route queries to the correct SQL function, there's a router/preprocessor, (you can read about it here: https://gitlab.syncad.com/hive/hivemind/-/blob/develop/hive/db/sql_scripts/postgrest/home.sql

So you should instead call http://localhost:8080/rpc/home :

curl 'http://localhost:8080/rpc/home'  -X POST   -H 'Content-Type: application/json'   --data-raw '{"id":6,"jsonrpc":"2.0","method":"bridge.account_notifications","params":{"account":"gtg","limit":1}}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   381    0   280  100   101   2552    920 --:--:-- --:--:-- --:--:--  3495
{
  "id": "6",
  "result": [
    {
      "id": 2111608,
      "msg": "@liondani voted on your post ($0.27)",
      "url": "@gtg/re-chitty-a-balanced-voting-system-is-impossible-with-current-witness-s-payouts-20160915t141551322z",
      "date": "2016-09-15T18:55:30",
      "type": "vote",
      "score": 50
    }
  ],
  "jsonrpc": "2.0"
}

And voilà !

That's it for this little tutorial, hope you found it useful :)

Sort:  

keep the good work up

ty :)