Skip to content

Commit

Permalink
block_info RPC, minor changes to blocks RPC (#1086)
Browse files Browse the repository at this point in the history
* Add block_info RPC

* Styling changes on blocks rpc

* Add block_info docs

* revert time column name change
  • Loading branch information
dostrelith678 committed Sep 14, 2021
1 parent dfa72c2 commit a3f0e91
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 22 deletions.
36 changes: 36 additions & 0 deletions docs/Build/grestsvcs.json
Expand Up @@ -574,6 +574,42 @@
}
}
},
"/rpc/block_info": {
"post": {
"tags": [
"Block Queries"
],
"summary": "Get detailed information about a specific block",
"produces": [
"application/json",
"application/vnd.pgrst.object+json"
],
"parameters": [
{
"required": true,
"schema": {
"required": [
"_block_hash"
],
"type": "object",
"properties": {
"_block_hash": {
"format": "text",
"type": "string"
}
}
},
"in": "body",
"name": "args"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/rpc/account_addresses": {
"post": {
"tags": [
Expand Down
56 changes: 56 additions & 0 deletions files/grest/rpc/blocks/block_info.sql
@@ -0,0 +1,56 @@
DROP FUNCTION IF EXISTS grest.block_info (_block_hash text);

CREATE FUNCTION grest.block_info (_block_hash text)
RETURNS TABLE (
HASH text,
EPOCH uinteger,
ABS_SLOT uinteger,
EPOCH_SLOT uinteger,
HEIGHT uinteger,
BLOCK_TIME timestamp,
TX_COUNT bigint,
VRF_KEY varchar,
OP_CERT_COUNTER word63type,
POOL varchar,
PARENT_HASH text,
CHILD_HASH text)
LANGUAGE PLPGSQL
AS $$
BEGIN
RETURN QUERY
SELECT
_block_hash AS HASH,
b.EPOCH_NO AS EPOCH,
b.SLOT_NO AS ABS_SLOT,
b.EPOCH_SLOT_NO AS EPOCH_SLOT,
b.BLOCK_NO AS HEIGHT,
b.TIME AS BLOCK_TIME,
b.TX_COUNT,
b.VRF_KEY,
b.OP_CERT_COUNTER,
ph.VIEW AS POOL,
(
SELECT
ENCODE(tB.HASH::bytea, 'hex')
FROM
block tB
WHERE
id = b.id - 1) AS PARENT_HASH,
(
SELECT
ENCODE(tB.HASH::bytea, 'hex')
FROM
block tB
WHERE
id = b.id + 1) AS CHILD_HASH
FROM
BLOCK B
LEFT JOIN SLOT_LEADER SL ON SL.ID = B.SLOT_LEADER_ID
LEFT JOIN POOL_HASH PH ON PH.ID = SL.POOL_HASH_ID
WHERE
ENCODE(B.HASH::bytea, 'hex') = _block_hash;
END;
$$;

COMMENT ON FUNCTION grest.block_info IS 'Get detailed information about a specific block';

39 changes: 17 additions & 22 deletions files/grest/rpc/blocks/blocks.sql
Expand Up @@ -2,39 +2,33 @@ DROP FUNCTION IF EXISTS grest.blocks ();

CREATE FUNCTION grest.blocks ()
RETURNS TABLE (
hash text,
epoch uinteger,
abs_slot uinteger,
epoch_slot uinteger,
block_no uinteger,
block_time timestamp,
tx_count bigint,
vrf_key varchar,
op_cert_counter word63type,
pool varchar,
parent_hash text)
HASH text,
EPOCH uinteger,
ABS_SLOT uinteger,
EPOCH_SLOT uinteger,
HEIGHT uinteger,
BLOCK_TIME timestamp,
TX_COUNT bigint,
VRF_KEY varchar,
OP_CERT_COUNTER word63type,
POOL varchar,
PARENT_HASH text)
LANGUAGE PLPGSQL
AS $$
BEGIN
RETURN QUERY
SELECT
ENCODE(B.HASH::bytea, 'hex') AS BLOCK_HASH,
ENCODE(B.HASH::bytea, 'hex') AS HASH,
b.EPOCH_NO AS EPOCH,
b.SLOT_NO AS ABS_SLOT,
b.EPOCH_SLOT_NO AS EPOCH_SLOT,
b.BLOCK_NO,
b.TIME,
b.BLOCK_NO AS HEIGHT,
b.TIME AS BLOCK_TIME,
b.TX_COUNT,
b.VRF_KEY,
b.OP_CERT_COUNTER,
ph.VIEW,
(
SELECT
ENCODE(tB.HASH::bytea, 'hex')
FROM
block tB
WHERE
id = b.id - 1) AS parent_hash
ph.VIEW AS POOL,
LAG(ENCODE(b.HASH::bytea, 'hex')) OVER (ORDER BY b.ID) AS PARENT_HASH
FROM
BLOCK B
LEFT JOIN SLOT_LEADER SL ON SL.ID = B.SLOT_LEADER_ID
Expand All @@ -45,3 +39,4 @@ END;
$$;

COMMENT ON FUNCTION grest.blocks IS 'Get detailed information about all blocks (paginated - latest first)';

0 comments on commit a3f0e91

Please sign in to comment.