Skip to content

Commit

Permalink
Wrapper for GetWinningBakersEpoch return type.
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusbechwind committed Dec 18, 2023
1 parent 876cc77 commit 0e5cbbf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
8 changes: 3 additions & 5 deletions v2/getwinningbakersepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
48 changes: 47 additions & 1 deletion v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

}

Expand Down Expand Up @@ -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}
}

0 comments on commit 0e5cbbf

Please sign in to comment.