Skip to content

Commit 190c37e

Browse files
authored
Merge pull request #1195 from input-output-hk/stake_pool_get
Add stake pool details getter to REST
2 parents 5b342f3 + 9eeee34 commit 190c37e

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

doc/jcli/rest.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,36 @@ YAML printed on success
477477
# timestamp of last time gossip was received from node if ever (optional)
478478
lastGossipReceived: "2019-10-14T00:45:59.419496188+00:00"
479479
```
480+
481+
## Get stake pool details
482+
483+
Fetches stake pool details
484+
485+
```
486+
jcli rest v0 stake-pool get <pool-id> <options>
487+
```
488+
489+
<pool-id> - hex-encoded pool ID
490+
491+
The options are
492+
493+
- -h <node_addr> - see [conventions](#conventions)
494+
- --debug - see [conventions](#conventions)
495+
- --output-format <format> - see [conventions](#conventions)
496+
497+
YAML printed on success
498+
499+
```yaml
500+
---
501+
tax: # pool reward
502+
fixed: 5 # what get subtracted as fixed value
503+
ratio: # ratio of tax after fixed amount is subtracted. Expressed as numerator/denominator
504+
numerator: 1
505+
denominator: 10000
506+
max: 100 # limit of tax (optional)
507+
total_stake: 2000000000000 # total stake pool value
508+
# bech32-encoded stake pool KES key
509+
kesPublicKey: kes25519-12-pk1q7susucqwje0lpetqzjgzncgcrjzx7e2guh900qszdjskkeyqpusf3p39r
510+
# bech32-encoded stake pool VRF key
511+
vrfPublicKey: vrf_pk1rcm4qm3q9dtwq22x9a4avnan7a3k987zvepuxwekzj3uyu6a8v0s6sdy0l
512+
```

doc/openapi.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,68 @@ paths:
662662
type: string
663663
pattern: '[0-9a-fA-F]+'
664664
example: [ "5cf03f333f37eb7b987dbc9017b8a928287a3d77d086cd93cd9ad05bcba7e60f" ]
665+
/api/v0/stake_pool:
666+
get:
667+
description: Gets stake pool details
668+
responses:
669+
200:
670+
description: Success
671+
content:
672+
application/json:
673+
schema:
674+
description: Stake pool details
675+
type: object
676+
required: [tax, total_stake, kesPublicKey, vrfPublicKey]
677+
properies:
678+
tax:
679+
description: Pool reward
680+
type: object
681+
required: [fixed, ratio, max]
682+
properties:
683+
fixed:
684+
description: What get subtracted as fixed value
685+
type: integer
686+
minimum: 0
687+
ratio:
688+
description: Ratio of tax after fixed amount is subtracted. Expressed as numerator/denominator
689+
type: object
690+
required: [numerator, denominator]
691+
properties:
692+
numerator:
693+
type: integer
694+
minimum: 0
695+
denominator:
696+
type: integer
697+
minimum: 1
698+
max:
699+
description: Limit of tax
700+
type: integer
701+
minimum: 1
702+
nullable: true
703+
total_stake:
704+
description: Total stake pool value
705+
type: integer
706+
minimum: 0
707+
vrfPublicKey:
708+
description: Bech32-encoded stake pool KES key
709+
type: string
710+
vrfPublicKey:
711+
description: Bech32-encoded stake pool VRF key
712+
type: string
713+
example: |
714+
{
715+
"tax": {
716+
"fixed": 5,
717+
"ratio": {
718+
"numerator": 1
719+
"denominator": 10000,
720+
}
721+
"max": 100,
722+
},
723+
"total_stake": 2000000000000,
724+
"kesPublicKey": "kes25519-12-pk1q7susucqwje0lpetqzjgzncgcrjzx7e2guh900qszdjskkeyqpusf3p39r",
725+
"vrfPublicKey": "vrf_pk1rcm4qm3q9dtwq22x9a4avnan7a3k987zvepuxwekzj3uyu6a8v0s6sdy0l"
726+
}
665727
/api/v0/stake:
666728
get:
667729
description: Gets stake distribution

jcli/src/jcli_app/rest/v0/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod node;
77
mod settings;
88
mod shutdown;
99
mod stake;
10+
mod stake_pool;
1011
mod stake_pools;
1112
mod tip;
1213
mod utxo;
@@ -33,6 +34,8 @@ pub enum V0 {
3334
Settings(settings::Settings),
3435
/// Stake information
3536
Stake(stake::Stake),
37+
/// Stake pool operations
38+
StakePool(stake_pool::StakePool),
3639
/// Stake pools operations
3740
StakePools(stake_pools::StakePools),
3841
/// Shutdown node
@@ -54,6 +57,7 @@ impl V0 {
5457
V0::Node(node) => node.exec(),
5558
V0::Settings(settings) => settings.exec(),
5659
V0::Stake(stake) => stake.exec(),
60+
V0::StakePool(stake_pool) => stake_pool.exec(),
5761
V0::StakePools(stake_pools) => stake_pools.exec(),
5862
V0::Shutdown(shutdown) => shutdown.exec(),
5963
V0::Tip(tip) => tip.exec(),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::jcli_app::rest::Error;
2+
use crate::jcli_app::utils::{DebugFlag, HostAddr, OutputFormat, RestApiSender};
3+
use structopt::StructOpt;
4+
5+
#[derive(StructOpt)]
6+
#[structopt(rename_all = "kebab-case")]
7+
pub enum StakePool {
8+
/// Get stake pool details
9+
Get {
10+
/// hex-encoded pool ID
11+
pool_id: String,
12+
#[structopt(flatten)]
13+
addr: HostAddr,
14+
#[structopt(flatten)]
15+
debug: DebugFlag,
16+
#[structopt(flatten)]
17+
output_format: OutputFormat,
18+
},
19+
}
20+
21+
impl StakePool {
22+
pub fn exec(self) -> Result<(), Error> {
23+
let StakePool::Get {
24+
pool_id,
25+
addr,
26+
debug,
27+
output_format,
28+
} = self;
29+
let url = addr
30+
.with_segments(&["v0", "stake_pool", &pool_id])?
31+
.into_url();
32+
let builder = reqwest::Client::new().get(url);
33+
let response = RestApiSender::new(builder, &debug).send()?;
34+
response.ok_response()?;
35+
let status = response.body().json_value()?;
36+
let formatted = output_format.format_json(status)?;
37+
println!("{}", formatted);
38+
Ok(())
39+
}
40+
}

jormungandr/src/rest/v0/handlers.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use chain_impl_mockchain::value::{Value, ValueError};
1414
use chain_storage::error::Error as StorageError;
1515

1616
use crate::blockchain::Ref;
17+
use crate::chain_crypto::bech32::Bech32;
1718
use crate::intercom::{self, NetworkMsg, TransactionMsg};
1819
use crate::secure::NodeSecret;
1920
use bytes::{Bytes, IntoBuf};
@@ -420,3 +421,38 @@ pub fn get_utxo(context: State<Context>, path_params: Path<(String, u8)>) -> Act
420421
})
421422
})
422423
}
424+
425+
pub fn get_stake_pool(context: State<Context>, pool_id_hex: Path<String>) -> ActixFuture!() {
426+
pool_id_hex
427+
.parse()
428+
.map_err(ErrorBadRequest)
429+
.into_future()
430+
.and_then(move |pool_id| {
431+
chain_tip_fut(&context).and_then(move |blockchain_tip| {
432+
let ledger = blockchain_tip.ledger();
433+
let pool_reg = ledger.delegation().lookup(&pool_id).ok_or_else(|| {
434+
ErrorNotFound(format!("Stake pool '{}' not found", pool_id_hex))
435+
})?;
436+
let total_stake: u64 = ledger
437+
.get_stake_distribution()
438+
.to_pools
439+
.get(&pool_id)
440+
.map(|pool| pool.total.total_stake.into())
441+
.unwrap_or(0);
442+
let tax = &pool_reg.rewards;
443+
Ok(Json(json!({
444+
"kesPublicKey": pool_reg.keys.kes_public_key.to_bech32_str(),
445+
"vrfPublicKey": pool_reg.keys.vrf_public_key.to_bech32_str(),
446+
"total_stake": total_stake,
447+
"tax": {
448+
"fixed": tax.fixed.0,
449+
"ratio": {
450+
"numerator": tax.ratio.numerator,
451+
"denominator": tax.ratio.denominator,
452+
},
453+
"max": tax.max_limit,
454+
}
455+
})))
456+
})
457+
})
458+
}

jormungandr/src/rest/v0/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub fn resources() -> Vec<(
3939
("/stake_pools", &|r| {
4040
r.get().with_async(handlers::get_stake_pools)
4141
}),
42+
("/stake_pool/{pool_id}", &|r| {
43+
r.get().with_async(handlers::get_stake_pool)
44+
}),
4245
("/shutdown", &|r| r.get().with(handlers::get_shutdown)),
4346
("/message", &|r| r.post().with(handlers::post_message)),
4447
("/node/stats", &|r| {

0 commit comments

Comments
 (0)