Skip to content

Commit

Permalink
Merge pull request #275 from Concordium/getBakersRewardPeriod
Browse files Browse the repository at this point in the history
Get bakers reward period
  • Loading branch information
magnusbechwind authored Nov 8, 2023
2 parents 0c8975e + 3e79a8e commit 7a83d7e
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased changes
- Added support for GRPC V2 `GetBakerRewardPeriodInfo` for getting all the bakers in the reward period of a block. Only available when querying a node with version at least 6.1.
- Added support for GRPC V2 `GetBlockCertificates` for retrieving certificates for a block supporting ConcordiumBF, i.e. a node with at least version 6.1.
- Extended `CurrentPaydayStatus` with `CommissionRates` that apply for the current reward period. Requires at least node version 6.1.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.concordium.sdk.examples;

import com.concordium.sdk.ClientV2;
import com.concordium.sdk.Connection;
import com.concordium.sdk.requests.BlockQuery;
import com.concordium.sdk.responses.bakersrewardperiod.BakerRewardPeriodInfo;
import lombok.val;
import picocli.CommandLine;

import java.net.URL;
import java.util.concurrent.Callable;

/**
* Calls {@link ClientV2#getBakersRewardPeriod(BlockQuery)}
* retrieving a list of {@link BakerRewardPeriodInfo} about the bakers in the reward period of the block.
* Prints the result to the terminal.
*/
@CommandLine.Command(name = "GetBakersRewardPeriod", mixinStandardHelpOptions = true)
public class GetBakersRewardPeriod implements Callable<Integer> {

@CommandLine.Option(
names = {"--endpoint"},
description = "GRPC interface of the node.",
defaultValue = "http://localhost:20002")
private String endpoint;

@Override
public Integer call() throws Exception {
URL endpointUrl = new URL(this.endpoint);
Connection connection = Connection.newBuilder()
.host(endpointUrl.getHost())
.port(endpointUrl.getPort())
.build();

ClientV2 client = ClientV2.from(connection);


val bakerInfo = client.getBakersRewardPeriod(BlockQuery.BEST);
bakerInfo.forEach(System.out::println);
return 0;
}

public static void main(String[] args) {
int exitCode = new CommandLine(new GetBakersRewardPeriod()).execute(args);
System.exit(exitCode);
}
}
24 changes: 22 additions & 2 deletions concordium-sdk/src/main/java/com/concordium/sdk/ClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.concordium.sdk.responses.DelegatorInfo;
import com.concordium.sdk.responses.DelegatorRewardPeriodInfo;
import com.concordium.sdk.responses.*;
import com.concordium.sdk.responses.bakersrewardperiod.BakerRewardPeriodInfo;
import com.concordium.sdk.responses.accountinfo.AccountInfo;
import com.concordium.sdk.responses.blockcertificates.BlockCertificates;
import com.concordium.sdk.responses.blockinfo.BlockInfo;
Expand Down Expand Up @@ -85,8 +86,9 @@ public static ClientV2 from(final Connection connection) throws ClientInitializa

/**
* Construct a new client
* @param timeout The timeout in milliseconds.
* @param channel the underlying grpc channel.
*
* @param timeout The timeout in milliseconds.
* @param channel the underlying grpc channel.
* @param credentials Optionally extra headers.
*/
ClientV2(final int timeout, final ManagedChannel channel, final Optional<Credentials> credentials) {
Expand Down Expand Up @@ -776,6 +778,24 @@ public InvokeInstanceResult invokeInstance(InvokeInstanceRequest request) {
return InvokeInstanceResult.parse(grpcResponse);
}

/**
* Get all bakers in the reward period of a block.
* This endpoint is only supported for protocol version 6 and onwards.
* If the protocol does not support the endpoint then an 'UNIMPLEMENTED' exception is thrown
*
* @param input The block to query.
*
* @return {@link ImmutableList} with the {@link BakerRewardPeriodInfo} of all the bakers in the block.
*/
public ImmutableList<BakerRewardPeriodInfo> getBakersRewardPeriod(BlockQuery input) {
val response = this.server().getBakersRewardPeriod(to(input));
val periodInfos = new ImmutableList.Builder<BakerRewardPeriodInfo>();
response.forEachRemaining(
info -> periodInfos.add(BakerRewardPeriodInfo.from(info))
);
return periodInfos.build();
}

/**
* Retrieves the {@link BlockCertificates} for a given block.
* Note that, if the block being pointed to is not a product of ConcordiumBFT, i.e. created before protocol version 6, then a INVALID_ARGUMENT exception will be thrown.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.concordium.sdk.responses.bakersrewardperiod;

import com.concordium.sdk.crypto.bls.BLSPublicKey;
import com.concordium.sdk.crypto.ed25519.ED25519PublicKey;
import com.concordium.sdk.crypto.vrf.VRFPublicKey;
import com.concordium.sdk.responses.BakerId;
import com.concordium.sdk.responses.nodeinfo.NodeInfo;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Information about a baker.
*/
@Builder
@Getter
@EqualsAndHashCode
@ToString
public class BakerInfo {

/**
* Id of the baker.
*/
private BakerId bakerId;
/**
* Baker's public key used to check whether they won the lottery or not.
*/
private VRFPublicKey bakerElectionVerifyKey;
/**
* Baker's public key used to check that they are indeed the ones who produced the block.
*/
private ED25519PublicKey bakerSignatureVerifyKey;
/**
* Baker's public key used to check signatures on finalization records.
* This is only used if the baker has sufficient stake to participate in finalization.
*/
private BLSPublicKey bakerAggregationVerifyKey;

/**
* Parses {@link com.concordium.grpc.v2.BakerInfo} to {@link BakerInfo}.
*
* @param bakerInfo {@link com.concordium.grpc.v2.BakerInfo} returned by the GRPC v2 API.
* @return Parsed {@link BakerRewardPeriodInfo}.
*/
public static BakerInfo from(com.concordium.grpc.v2.BakerInfo bakerInfo) {
return BakerInfo.builder()
.bakerId(BakerId.from(bakerInfo.getBakerId()))
.bakerElectionVerifyKey(VRFPublicKey.from(bakerInfo.getElectionKey().getValue().toByteArray()))
.bakerSignatureVerifyKey(ED25519PublicKey.from(bakerInfo.getSignatureKey().getValue().toByteArray()))
.bakerAggregationVerifyKey(BLSPublicKey.from(bakerInfo.getAggregationKey().getValue().toByteArray()))
.build();
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.concordium.sdk.responses.bakersrewardperiod;

import com.concordium.sdk.responses.accountinfo.CommissionRates;
import com.concordium.sdk.transactions.CCDAmount;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

/**
* Information about a particular baker with respect to the current reward period.
*/
@Builder
@ToString
@Getter
@EqualsAndHashCode
public class BakerRewardPeriodInfo {

/**
* The baker id and public keys for the baker.
*/
private BakerInfo baker;
/**
* The effective stake of the baker for the consensus protocol.
* The returned amount accounts for delegation, capital bounds and leverage bounds.
*/
private CCDAmount effectiveStake;
/**
* The effective commission rate for the baker that applies for the reward period.
*/
private CommissionRates commissionRates;
/**
* The amount staked by the baker itself.
*/
private CCDAmount equityCapital;
/**
* The total amount of capital delegated to this baker pool.
*/
private CCDAmount delegatedCapital;
/**
* Whether the baker is a finalizer or not.
*/
private boolean isFinalizer;

/**
* Parses {@link com.concordium.grpc.v2.BakerRewardPeriodInfo} to {@link BakerRewardPeriodInfo}.
*
* @param info {@link com.concordium.grpc.v2.BakerRewardPeriodInfo} returned by the GRPC v2 API.
* @return Parsed {@link BakerRewardPeriodInfo}.
*/
public static BakerRewardPeriodInfo from(com.concordium.grpc.v2.BakerRewardPeriodInfo info) {
return BakerRewardPeriodInfo.builder()
.baker(BakerInfo.from(info.getBaker()))
.effectiveStake(CCDAmount.from(info.getEffectiveStake()))
.commissionRates(CommissionRates.from(info.getCommissionRates()))
.equityCapital(CCDAmount.from(info.getEquityCapital()))
.delegatedCapital(CCDAmount.from(info.getDelegatedCapital()))
.isFinalizer(info.getIsFinalizer())
.build();

}

}
Loading

0 comments on commit 7a83d7e

Please sign in to comment.