From 0e5cbbf0ba521243bd6b2f56c49ff5fa0939152a Mon Sep 17 00:00:00 2001 From: magnusbechwind Date: Mon, 18 Dec 2023 15:48:42 +0100 Subject: [PATCH] Wrapper for GetWinningBakersEpoch return type. --- v2/getwinningbakersepoch.go | 8 +++---- v2/types.go | 48 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/v2/getwinningbakersepoch.go b/v2/getwinningbakersepoch.go index c6c5e14..74a66ca 100644 --- a/v2/getwinningbakersepoch.go +++ b/v2/getwinningbakersepoch.go @@ -2,8 +2,6 @@ package v2 import ( "context" - - "github.com/Concordium/concordium-go-sdk/v2/pb" ) // GetWinningBakersEpoch retrieves the list of bakers that won the lottery in a particular historical epoch @@ -21,11 +19,11 @@ import ( // - `UNIMPLEMENTED` if the endpoint is disabled on the node. // // This endpoint is only supported for protocol version 6 and onwards. -func (c *Client) GetWinningBakersEpoch(ctx context.Context, req isEpochRequest) (_ pb.Queries_GetWinningBakersEpochClient, err error) { +func (c *Client) GetWinningBakersEpoch(ctx context.Context, req isEpochRequest) (_ WinningBakerStream, err error) { stream, err := c.GrpcClient.GetWinningBakersEpoch(ctx, convertEpochRequest(req)) if err != nil { - return nil, err + return WinningBakerStream{}, err } - return stream, nil + return WinningBakerStream{stream: stream}, nil } diff --git a/v2/types.go b/v2/types.go index ba958e9..cda8fc6 100644 --- a/v2/types.go +++ b/v2/types.go @@ -1163,7 +1163,11 @@ func parseBakerInfo(b *pb.BakerInfo) BakerInfo { electionKey := parseBakerElectionVerifyKey(b.ElectionKey) signatureKey := parseBakerSignatureVerifyKey(b.SignatureKey) aggregationKey := parseBakerAggregationVerifyKey(b.AggregationKey) - return BakerInfo{BakerId: bakerId, ElectionKey: electionKey, SignatureKey: signatureKey, AggregationKey: aggregationKey} + return BakerInfo{ + BakerId: bakerId, + ElectionKey: electionKey, + SignatureKey: signatureKey, + AggregationKey: aggregationKey} } @@ -1257,3 +1261,45 @@ func parseAmountFraction(a *pb.AmountFraction) (AmountFraction, error) { } return res, nil } + +// Return type of GetWinningBakersEpoch. Parses the returned *pb.WinningBaker to WinningBaker when Recv() is called. +type WinningBakerStream struct { + stream pb.Queries_GetWinningBakersEpochClient +} + +// Recv retrieves the next WinningBaker. +func (s *WinningBakerStream) Recv() (WinningBaker, error) { + info, err := s.stream.Recv() + if err != nil { + return WinningBaker{}, err + } + return parseWinningBaker(info), nil +} + +// Details of which baker won the lottery in a given round in consensus version 1. +type WinningBaker struct { + // The round number. + Round Round + // The baker that won the round. + Winner BakerId + // True if the baker produced a block in this round on the finalized chain, and false otherwise. + Present bool +} + +func parseWinningBaker(w *pb.WinningBaker) WinningBaker { + return WinningBaker{ + Round: parseRound(w.Round), + Winner: parseBakerId(w.Winner), + Present: w.Present, + } +} + +// A round. +type Round struct { + Value uint64 +} + +// Parses *pb.Round to Round. +func parseRound(r *pb.Round) Round { + return Round{Value: r.Value} +}