Skip to content

Commit 76174df

Browse files
jkawanJenita
andauthored
feat: implement PoolDistrResult (#1256)
Signed-off-by: Jenita <jkawan@blinklabs.io> Co-authored-by: Jenita <jkawan@blinklabs.io>
1 parent 2c86a57 commit 76174df

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

cmd/gouroboros/query.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,31 @@ func testQuery(f *globalFlags) {
315315
os.Exit(1)
316316
}
317317
fmt.Printf("proposed-protocol-params-updates: %v\n", *proposedUpdates)
318+
case "pool-distr":
319+
// GetPoolDistr without specific pool IDs to get all pools distribution
320+
poolDistr, err := o.LocalStateQuery().Client.GetPoolDistr(nil)
321+
if err != nil {
322+
fmt.Printf(
323+
"ERROR: failure querying pool distribution: %s\n",
324+
err,
325+
)
326+
os.Exit(1)
327+
}
328+
fmt.Printf("pool-distr (raw): %#v\n", poolDistr)
329+
330+
// Build a JSON-friendly view: map pool ID (hex) -> entry
331+
jsonResults := make(map[string]any, len(poolDistr.Results))
332+
for poolID, entry := range poolDistr.Results {
333+
// PoolId is [28]byte; represent as hex for debugging
334+
jsonKey := hex.EncodeToString(poolID[:])
335+
jsonResults[jsonKey] = entry
336+
}
337+
jsonData, err := json.Marshal(jsonResults)
338+
if err != nil {
339+
fmt.Printf("pool-distr (JSON marshaling failed): %s\n", err)
340+
} else {
341+
fmt.Printf("pool-distr (JSON): %s\n", string(jsonData))
342+
}
318343
default:
319344
fmt.Printf("ERROR: unknown query: %s\n", queryFlags.flagset.Args()[0])
320345
os.Exit(1)

protocol/localstatequery/client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,18 @@ func (c *Client) GetPoolDistr(poolIds []any) (*PoolDistrResult, error) {
836836
if err != nil {
837837
return nil, err
838838
}
839+
// GetPoolDistr always requires a pool set parameter according to the Haskell implementation
840+
// The query expects (len=2, tag=21) format: [21, Set(poolIds)]
841+
// If no pool IDs specified, use an empty set to query all pools
842+
var params []any
843+
if poolIds == nil {
844+
poolIds = []any{}
845+
}
846+
params = append(params, poolIds)
839847
query := buildShelleyQuery(
840848
currentEra,
841849
QueryTypeShelleyPoolDistr,
842-
// TODO: add args (#870)
850+
params...,
843851
)
844852
var result PoolDistrResult
845853
if err := c.runQuery(query, &result); err != nil {

protocol/localstatequery/queries.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,5 +722,13 @@ type PoolStateResult any
722722
// TODO (#869)
723723
type StakeSnapshotsResult any
724724

725-
// TODO (#870)
726-
type PoolDistrResult any
725+
// PoolDistrResult represents the pool distribution result
726+
// It contains a map of pool IDs to their stake distribution (fraction and VRF hash)
727+
type PoolDistrResult struct {
728+
cbor.StructAsArray
729+
Results map[ledger.PoolId]struct {
730+
cbor.StructAsArray
731+
StakeFraction *cbor.Rat
732+
VrfHash ledger.Blake2b256
733+
}
734+
}

0 commit comments

Comments
 (0)