Skip to content

Commit 29b9166

Browse files
authored
chore: expose get_pub_rand_commit_for_height query (#101)
## Description exposes query needed by fp program this query is used by fp program when it determines the voting power of rollup FP, it will fetch pr commit at specific rollup height and then check if that babylon epoch is finalized or not currently the algorithm in fp program was simply fetching last commited fp (this logic is not correct) <!-- Brief description of changes --> ## Checklist - [ ] I have updated the [docs/SPEC.md](https://github.com/babylonlabs-io/rollup-bsn-contracts/blob/main/docs/SPEC.md) file if this change affects the specification - [x] I have updated the schema by running `cargo gen-schema`
1 parent 27ad53c commit 29b9166

File tree

7 files changed

+183
-9
lines changed

7 files changed

+183
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
5252
optimize public key handling by using bytes instead of hex
5353
* [#98](https://github.com/babylonlabs-io/rollup-bsn-contracts/pull/98) feat:
5454
admin handler to update config
55+
* [#101](https://github.com/babylonlabs-io/rollup-bsn-contracts/pull/101) chore:
56+
expose get_pub_rand_commit_for_height query
5557

5658
## v0.1.0
5759

contracts/finality/schema/finality.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,34 @@
502502
},
503503
"additionalProperties": false
504504
},
505+
{
506+
"description": "`PubRandCommitForHeight` returns the public randomness commitment that covers a specific height for a given FP.\n\n`btc_pk_hex` is the BTC public key of the finality provider, in hex format. `height` is the block height to find the covering public randomness commitment for.",
507+
"type": "object",
508+
"required": [
509+
"pub_rand_commit_for_height"
510+
],
511+
"properties": {
512+
"pub_rand_commit_for_height": {
513+
"type": "object",
514+
"required": [
515+
"btc_pk_hex",
516+
"height"
517+
],
518+
"properties": {
519+
"btc_pk_hex": {
520+
"type": "string"
521+
},
522+
"height": {
523+
"type": "integer",
524+
"format": "uint64",
525+
"minimum": 0.0
526+
}
527+
},
528+
"additionalProperties": false
529+
}
530+
},
531+
"additionalProperties": false
532+
},
505533
{
506534
"description": "`ListPubRandCommit` returns a list of public randomness commitments for a given FP.\n\n`btc_pk_hex` is the BTC public key of the finality provider, in hex format. `start_after` is optional pagination parameter - only return commitments with start_height > start_after. `limit` is optional limit on number of results (default 10, max 30). `reverse` is optional flag to reverse the order (default false = ascending by start_height).",
507535
"type": "object",
@@ -976,6 +1004,60 @@
9761004
"additionalProperties": false
9771005
}
9781006
}
1007+
},
1008+
"pub_rand_commit_for_height": {
1009+
"$schema": "http://json-schema.org/draft-07/schema#",
1010+
"title": "Nullable_PubRandCommit",
1011+
"anyOf": [
1012+
{
1013+
"$ref": "#/definitions/PubRandCommit"
1014+
},
1015+
{
1016+
"type": "null"
1017+
}
1018+
],
1019+
"definitions": {
1020+
"PubRandCommit": {
1021+
"description": "`PubRandCommit` is a commitment to a series of public randomness. Currently, the commitment is a root of a Merkle tree that includes a series of public randomness values",
1022+
"type": "object",
1023+
"required": [
1024+
"babylon_epoch",
1025+
"commitment",
1026+
"num_pub_rand",
1027+
"start_height"
1028+
],
1029+
"properties": {
1030+
"babylon_epoch": {
1031+
"description": "The epoch number of Babylon when the commit was submitted",
1032+
"type": "integer",
1033+
"format": "uint64",
1034+
"minimum": 0.0
1035+
},
1036+
"commitment": {
1037+
"description": "Value of the commitment. Currently, it's the root of the Merkle tree constructed by the public randomness",
1038+
"type": "array",
1039+
"items": {
1040+
"type": "integer",
1041+
"format": "uint8",
1042+
"minimum": 0.0
1043+
}
1044+
},
1045+
"num_pub_rand": {
1046+
"description": "The amount of committed public randomness",
1047+
"type": "integer",
1048+
"format": "uint64",
1049+
"minimum": 0.0
1050+
},
1051+
"start_height": {
1052+
"description": "The height of the first commitment",
1053+
"type": "integer",
1054+
"format": "uint64",
1055+
"minimum": 0.0
1056+
}
1057+
},
1058+
"additionalProperties": false
1059+
}
1060+
}
9791061
}
9801062
}
9811063
}

contracts/finality/schema/raw/query.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@
7373
},
7474
"additionalProperties": false
7575
},
76+
{
77+
"description": "`PubRandCommitForHeight` returns the public randomness commitment that covers a specific height for a given FP.\n\n`btc_pk_hex` is the BTC public key of the finality provider, in hex format. `height` is the block height to find the covering public randomness commitment for.",
78+
"type": "object",
79+
"required": [
80+
"pub_rand_commit_for_height"
81+
],
82+
"properties": {
83+
"pub_rand_commit_for_height": {
84+
"type": "object",
85+
"required": [
86+
"btc_pk_hex",
87+
"height"
88+
],
89+
"properties": {
90+
"btc_pk_hex": {
91+
"type": "string"
92+
},
93+
"height": {
94+
"type": "integer",
95+
"format": "uint64",
96+
"minimum": 0.0
97+
}
98+
},
99+
"additionalProperties": false
100+
}
101+
},
102+
"additionalProperties": false
103+
},
76104
{
77105
"description": "`ListPubRandCommit` returns a list of public randomness commitments for a given FP.\n\n`btc_pk_hex` is the BTC public key of the finality provider, in hex format. `start_after` is optional pagination parameter - only return commitments with start_height > start_after. `limit` is optional limit on number of results (default 10, max 30). `reverse` is optional flag to reverse the order (default false = ascending by start_height).",
78106
"type": "object",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Nullable_PubRandCommit",
4+
"anyOf": [
5+
{
6+
"$ref": "#/definitions/PubRandCommit"
7+
},
8+
{
9+
"type": "null"
10+
}
11+
],
12+
"definitions": {
13+
"PubRandCommit": {
14+
"description": "`PubRandCommit` is a commitment to a series of public randomness. Currently, the commitment is a root of a Merkle tree that includes a series of public randomness values",
15+
"type": "object",
16+
"required": [
17+
"babylon_epoch",
18+
"commitment",
19+
"num_pub_rand",
20+
"start_height"
21+
],
22+
"properties": {
23+
"babylon_epoch": {
24+
"description": "The epoch number of Babylon when the commit was submitted",
25+
"type": "integer",
26+
"format": "uint64",
27+
"minimum": 0.0
28+
},
29+
"commitment": {
30+
"description": "Value of the commitment. Currently, it's the root of the Merkle tree constructed by the public randomness",
31+
"type": "array",
32+
"items": {
33+
"type": "integer",
34+
"format": "uint8",
35+
"minimum": 0.0
36+
}
37+
},
38+
"num_pub_rand": {
39+
"description": "The amount of committed public randomness",
40+
"type": "integer",
41+
"format": "uint64",
42+
"minimum": 0.0
43+
},
44+
"start_height": {
45+
"description": "The height of the first commitment",
46+
"type": "integer",
47+
"format": "uint64",
48+
"minimum": 0.0
49+
}
50+
},
51+
"additionalProperties": false
52+
}
53+
}
54+
}

contracts/finality/src/contract.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::state::config::{get_config, set_config, Config, RateLimitingConfig, A
1717
use crate::state::finality::get_highest_voted_height;
1818
use crate::state::pruning::handle_prune_data;
1919
use crate::state::public_randomness::{
20-
get_first_pub_rand_commit, get_last_pub_rand_commit, list_pub_rand_commit,
20+
get_first_pub_rand_commit, get_last_pub_rand_commit, get_pub_rand_commit_for_height,
21+
list_pub_rand_commit,
2122
};
2223

2324
pub fn instantiate(
@@ -88,6 +89,9 @@ pub fn query(
8889
QueryMsg::LastPubRandCommit { btc_pk_hex } => Ok(to_json_binary(
8990
&get_last_pub_rand_commit(deps.storage, &hex::decode(&btc_pk_hex)?)?,
9091
)?),
92+
QueryMsg::PubRandCommitForHeight { btc_pk_hex, height } => Ok(to_json_binary(
93+
&get_pub_rand_commit_for_height(deps.storage, &hex::decode(&btc_pk_hex)?, height)?,
94+
)?),
9195
QueryMsg::ListPubRandCommit {
9296
btc_pk_hex,
9397
start_after,

contracts/finality/src/msg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ pub enum QueryMsg {
7272
/// `btc_pk_hex` is the BTC public key of the finality provider, in hex format.
7373
#[returns(Option<PubRandCommit>)]
7474
LastPubRandCommit { btc_pk_hex: String },
75+
/// `PubRandCommitForHeight` returns the public randomness commitment that covers a specific height for a given FP.
76+
///
77+
/// `btc_pk_hex` is the BTC public key of the finality provider, in hex format.
78+
/// `height` is the block height to find the covering public randomness commitment for.
79+
#[returns(Option<PubRandCommit>)]
80+
PubRandCommitForHeight { btc_pk_hex: String, height: u64 },
7581
/// `ListPubRandCommit` returns a list of public randomness commitments for a given FP.
7682
///
7783
/// `btc_pk_hex` is the BTC public key of the finality provider, in hex format.

contracts/finality/src/state/public_randomness.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ impl PubRandCommit {
6262
}
6363
}
6464

65-
fn get_pub_rand_commit_for_height(
65+
pub fn get_pub_rand_commit_for_height(
6666
storage: &dyn Storage,
6767
fp_btc_pk: &[u8],
6868
height: u64,
69-
) -> Result<PubRandCommit, ContractError> {
69+
) -> Result<Option<PubRandCommit>, ContractError> {
7070
let end_at = Some(Bound::inclusive(height));
7171
let res = PUB_RAND_COMMITS
7272
.prefix(fp_btc_pk)
@@ -84,12 +84,9 @@ fn get_pub_rand_commit_for_height(
8484
})
8585
.collect::<StdResult<Vec<_>>>()?;
8686
if res.is_empty() {
87-
Err(ContractError::MissingPubRandCommit(
88-
hex::encode(fp_btc_pk),
89-
height,
90-
))
87+
Ok(None)
9188
} else {
92-
Ok(res[0].clone())
89+
Ok(Some(res[0].clone()))
9390
}
9491
}
9592

@@ -102,7 +99,8 @@ pub fn get_timestamped_pub_rand_commit_for_height(
10299
fp_btc_pk: &[u8],
103100
height: u64,
104101
) -> Result<PubRandCommit, ContractError> {
105-
let pr_commit = get_pub_rand_commit_for_height(deps.storage, fp_btc_pk, height)?;
102+
let pr_commit = get_pub_rand_commit_for_height(deps.storage, fp_btc_pk, height)?
103+
.ok_or_else(|| ContractError::MissingPubRandCommit(hex::encode(fp_btc_pk), height))?;
106104

107105
// Ensure the finality provider's corresponding randomness commitment is already finalised by
108106
// BTC timestamping

0 commit comments

Comments
 (0)