Skip to content

Commit

Permalink
Merge pull request #58 from Concordium/ss/update-to-p6
Browse files Browse the repository at this point in the history
Updated SDK to align with Protocol 6
  • Loading branch information
Søren Schwartz authored Aug 2, 2023
2 parents b9d4002 + 51c8d1a commit d422449
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 21 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
## Unreleased changes

## 4.0.0
- The SDK requires node version 6 or later.
- Breaking changes
- `ConsensusInfo`
- `SlotDuration` is now a nullable field, only present in protocols 1-5.
- Bugfix: `BlockLastArrivedTime` was wrongly mapped from `BlockLastReceivedTime`.
- `BlockInfo`
- `BlockSlot` is now a nullable field, and only present in protocols 1-5
- Added
- `ConsensusInfo`
- a new field `ConcordiumBftDetails` is added that is present if protocol version is 6 or higher
- `BlockInfo`
- new fields `Round` and `Epoch` that are present in protocol 6 or higher.
- `BakerPoolStatus`
- a new field`BakerPoolPendingChange` is added which is present if any change is pending on baker pool.

## 3.0.0
- Added
- Add optional cancellation token parameter to all client calls.
Expand Down
2 changes: 1 addition & 1 deletion concordium-grpc-api
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Concordium.Sdk.Helpers;

internal static class TimestampExtensions
internal static class DateAndTimeExtensions
{
internal static DateTimeOffset ToDateTimeOffset(this Timestamp timestamp) => DateTimeOffset.FromUnixTimeMilliseconds((long)timestamp.Value);

internal static DateTimeOffset ToDateTimeOffset(this TransactionTime seconds) => DateTimeOffset.FromUnixTimeSeconds((long)seconds.Value);

internal static TimeSpan ToTimeSpan(this Duration duration) => TimeSpan.FromMilliseconds(duration.Value);
}
45 changes: 45 additions & 0 deletions src/Types/BakerPoolPendingChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Concordium.Grpc.V2;
using Concordium.Sdk.Exceptions;
using Concordium.Sdk.Helpers;

namespace Concordium.Sdk.Types;

/// <summary>
/// Pending change in the baker's stake.
/// </summary>
public abstract record BakerPoolPendingChange
{
internal static BakerPoolPendingChange? From(PoolPendingChange? pendingChange)
{
if (pendingChange is null)
{
return null;
}

return pendingChange.ChangeCase switch
{
PoolPendingChange.ChangeOneofCase.Reduce =>
new BakerPoolReduceStakePending(CcdAmount.From(pendingChange.Reduce.ReducedEquityCapital), pendingChange.Reduce.EffectiveTime.ToDateTimeOffset()),
PoolPendingChange.ChangeOneofCase.Remove =>
new BakerPoolRemovePending(pendingChange.Remove.EffectiveTime.ToDateTimeOffset()),
PoolPendingChange.ChangeOneofCase.None => null,
_ => throw new MissingEnumException<PoolPendingChange.ChangeOneofCase>(pendingChange.ChangeCase)
};
}
}

/// <summary>
/// The baker will be removed at the given time.
/// </summary>
/// <param name="EffectiveTime">Time when the baker will be removed.</param>
public sealed record BakerPoolRemovePending(DateTimeOffset EffectiveTime) : BakerPoolPendingChange;
/// <summary>
/// The stake is being reduced. The new stake will take affect at the given time.
/// </summary>
/// <param name="NewStake">New stake which will take effect.</param>
/// <param name="EffectiveTime">Time when the baker will be removed.</param>
public sealed record BakerPoolReduceStakePending(CcdAmount NewStake, DateTimeOffset EffectiveTime) : BakerPoolPendingChange;




10 changes: 6 additions & 4 deletions src/Types/BakerPoolStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Concordium.Sdk.Types;
/// if the pool is not a baker in the payday (e.g., because they just
/// registered and a new payday has not started yet).
/// </param>
/// <param name="BakerStakePendingChange">Any pending change to the baker's stake.</param>
/// <param name="AllPoolTotalCapital">Total capital staked across all pools.</param>
public sealed record BakerPoolStatus(
BakerId BakerId,
Expand All @@ -32,7 +33,8 @@ public sealed record BakerPoolStatus(
CcdAmount DelegatedCapitalCap,
BakerPoolInfo PoolInfo,
CurrentPaydayBakerPoolStatus? CurrentPaydayStatus,
CcdAmount AllPoolTotalCapital)
CcdAmount AllPoolTotalCapital,
BakerPoolPendingChange? BakerStakePendingChange)
{
internal static BakerPoolStatus From(Grpc.V2.PoolInfoResponse poolInfoResponse) =>
new(
Expand All @@ -43,7 +45,7 @@ internal static BakerPoolStatus From(Grpc.V2.PoolInfoResponse poolInfoResponse)
CcdAmount.From(poolInfoResponse.DelegatedCapitalCap),
BakerPoolInfo.From(poolInfoResponse.PoolInfo)!,
CurrentPaydayBakerPoolStatus.From(poolInfoResponse.CurrentPaydayInfo),
CcdAmount.From(poolInfoResponse.AllPoolTotalCapital));
CcdAmount.From(poolInfoResponse.AllPoolTotalCapital),
BakerPoolPendingChange.From(poolInfoResponse.EquityPendingChange)
);
}


19 changes: 14 additions & 5 deletions src/Types/BlockInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ namespace Concordium.Sdk.Types;
/// Time when the block was added to the node's tree. This is a subjective
/// (i.e., node specific) value.
/// </param>
/// <param name="BlockSlot">Slot number of the slot the block is in.</param>
/// <param name="BlockSlot">
/// Slot number of the slot the block is in.
/// Present in protocol versions 1-5.
/// </param>
/// <param name="BlockSlotTime">
/// Slot time of the slot the block is in. In contrast to
/// <see cref="BlockArriveTime"/> this is an objective value, all nodes
Expand All @@ -44,6 +47,8 @@ namespace Concordium.Sdk.Types;
/// <param name="TransactionSize">Size of all the transactions in the block in bytes.</param>
/// <param name="BlockStateHash">Hash of the block state at the end of the given block.</param>
/// <param name="ProtocolVersion">Protocol version to which the block belongs.</param>
/// <param name="Round">The round of the block. Present from protocol version 6.</param>
/// <param name="Epoch">The epoch of the block. Present from protocol version 6.</param>
public sealed record BlockInfo(
BlockHash BlockHash,
BlockHash BlockParent,
Expand All @@ -53,15 +58,17 @@ public sealed record BlockInfo(
ulong EraBlockHeight,
DateTimeOffset BlockReceiveTime,
DateTimeOffset BlockArriveTime,
ulong BlockSlot,
ulong? BlockSlot,
DateTimeOffset BlockSlotTime,
BakerId? BlockBaker,
bool Finalized,
uint TransactionCount,
EnergyAmount TransactionEnergyCost,
uint TransactionSize,
StateHash BlockStateHash,
ProtocolVersion ProtocolVersion
ProtocolVersion ProtocolVersion,
Round? Round,
Epoch? Epoch
)
{
internal static BlockInfo From(Grpc.V2.BlockInfo blockInfo) =>
Expand All @@ -74,14 +81,16 @@ internal static BlockInfo From(Grpc.V2.BlockInfo blockInfo) =>
EraBlockHeight: blockInfo.EraBlockHeight.Value,
BlockReceiveTime: blockInfo.ReceiveTime.ToDateTimeOffset(),
BlockArriveTime: blockInfo.ArriveTime.ToDateTimeOffset(),
BlockSlot: blockInfo.SlotNumber.Value,
BlockSlot: blockInfo.SlotNumber?.Value,
BlockSlotTime: blockInfo.SlotTime.ToDateTimeOffset(),
BlockBaker: blockInfo.Baker != null ? BakerId.From(blockInfo.Baker) : null,
Finalized: blockInfo.Finalized,
TransactionCount: blockInfo.TransactionsSize,
TransactionEnergyCost: new EnergyAmount(blockInfo.TransactionsEnergyCost.Value),
TransactionSize: blockInfo.TransactionsSize,
BlockStateHash: new StateHash(blockInfo.StateHash.Value),
ProtocolVersion: blockInfo.ProtocolVersion.Into()
ProtocolVersion: blockInfo.ProtocolVersion.Into(),
Round: blockInfo.Round != null ? Types.Round.From(blockInfo.Round) : null,
Epoch: blockInfo.Epoch != null ? Types.Epoch.From(blockInfo.Epoch) : null
);
}
31 changes: 31 additions & 0 deletions src/Types/ConcordiumBftDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Concordium.Sdk.Helpers;

namespace Concordium.Sdk.Types;

/// <summary>
/// Parameters pertaining to the Concordium BFT consensus.
/// </summary>
/// <param name="CurrentTimeoutDuration">The current duration to wait before a round times out.</param>
/// <param name="CurrentRound">The current round.</param>
/// <param name="CurrentEpoch">The current epoch.</param>
/// <param name="TriggerBlockTime">
/// The first block in the epoc with a timestamp equal to or later than this timestamp, is considered
/// to be the trigger block for the epoch transition.
/// </param>
public sealed record ConcordiumBftDetails(TimeSpan CurrentTimeoutDuration, Round CurrentRound, Epoch CurrentEpoch, DateTimeOffset TriggerBlockTime)
{
internal static ConcordiumBftDetails? From(Grpc.V2.ConsensusInfo info)
{
if (info.CurrentTimeoutDuration == null || info.CurrentRound == null ||
info.CurrentEpoch == null || info.TriggerBlockTime == null)
{
return null;
}

return new ConcordiumBftDetails(
info.CurrentTimeoutDuration.ToTimeSpan(),
Round.From(info.CurrentRound),
Epoch.From(info.CurrentEpoch),
info.TriggerBlockTime.ToDateTimeOffset());
}
}
27 changes: 17 additions & 10 deletions src/Types/ConsensusInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ namespace Concordium.Sdk.Types;
/// <see cref="GenesisBlock"/>.
/// </param>
/// <param name="CurrentEraGenesisTime">Time when the current era started.</param>
/// <param name="SlotDuration">Duration of a slot.</param>
/// <param name="SlotDuration">
/// Duration of a slot.
/// Present only in protocol versions 1-5.
/// </param>
/// <param name="EpochDuration">Duration of an epoch.</param>
/// <param name="GenesisIndex">
/// The number of chain restarts via a protocol update. An effected
Expand Down Expand Up @@ -99,6 +102,10 @@ namespace Concordium.Sdk.Types;
/// finalizations. Will be `None` if there are no finalizations yet
/// since the node start.
/// </param>
/// <param name="ConcordiumBftDetails">
/// Parameters that apply from protocol 6 onward. This is present if and
/// only if the protocol version is <see cref="ProtocolVersion.P6"/> or later.
/// </param>
public sealed record ConsensusInfo(
BlockHash BestBlock,
BlockHash GenesisBlock,
Expand All @@ -109,7 +116,7 @@ public sealed record ConsensusInfo(
ProtocolVersion ProtocolVersion,
BlockHash CurrentEraGenesisBlock,
DateTimeOffset CurrentEraGenesisTime,
TimeSpan SlotDuration,
TimeSpan? SlotDuration,
TimeSpan EpochDuration,
uint GenesisIndex,
uint BlocksReceivedCount,
Expand All @@ -129,12 +136,12 @@ public sealed record ConsensusInfo(
ulong FinalizationCount,
DateTimeOffset? LastFinalizedTime,
double? FinalizationPeriodEma,
double? FinalizationPeriodEmsd
double? FinalizationPeriodEmsd,
ConcordiumBftDetails? ConcordiumBftDetails
)
{
internal static ConsensusInfo From(Grpc.V2.ConsensusInfo consensusInfo) =>
new
(
new(
BestBlock: BlockHash.From(consensusInfo.BestBlock),
GenesisBlock: BlockHash.From(consensusInfo.GenesisBlock),
GenesisTime: consensusInfo.GenesisTime.ToDateTimeOffset(),
Expand All @@ -144,8 +151,8 @@ internal static ConsensusInfo From(Grpc.V2.ConsensusInfo consensusInfo) =>
ProtocolVersion: consensusInfo.ProtocolVersion.Into(),
CurrentEraGenesisBlock: BlockHash.From(consensusInfo.CurrentEraGenesisBlock),
CurrentEraGenesisTime: consensusInfo.CurrentEraGenesisTime.ToDateTimeOffset(),
SlotDuration: TimeSpan.FromMilliseconds(consensusInfo.SlotDuration.Value),
EpochDuration: TimeSpan.FromMilliseconds(consensusInfo.EpochDuration.Value),
SlotDuration: consensusInfo.SlotDuration?.ToTimeSpan(),
EpochDuration: consensusInfo.EpochDuration.ToTimeSpan(),
GenesisIndex: consensusInfo.GenesisIndex.Value,
BlocksReceivedCount: consensusInfo.BlocksReceivedCount,
BlockLastReceivedTime: consensusInfo.BlockLastReceivedTime?.ToDateTimeOffset(),
Expand All @@ -154,7 +161,7 @@ internal static ConsensusInfo From(Grpc.V2.ConsensusInfo consensusInfo) =>
BlockReceivePeriodEma: consensusInfo.HasBlockReceivePeriodEma ? consensusInfo.BlockReceivePeriodEma : null,
BlockReceivePeriodEmsd: consensusInfo.HasBlockReceivePeriodEmsd ? consensusInfo.BlockReceivePeriodEmsd : null,
BlocksVerifiedCount: consensusInfo.BlocksVerifiedCount,
BlockLastArrivedTime: consensusInfo.BlockLastReceivedTime?.ToDateTimeOffset(),
BlockLastArrivedTime: consensusInfo.BlockLastArrivedTime?.ToDateTimeOffset(),
BlockArriveLatencyEma: consensusInfo.BlockArriveLatencyEma,
BlockArriveLatencyEmsd: consensusInfo.BlockArriveLatencyEmsd,
BlockArrivePeriodEma: consensusInfo.HasBlockArrivePeriodEma ? consensusInfo.BlockArrivePeriodEma : null,
Expand All @@ -164,6 +171,6 @@ internal static ConsensusInfo From(Grpc.V2.ConsensusInfo consensusInfo) =>
FinalizationCount: consensusInfo.FinalizationCount,
LastFinalizedTime: consensusInfo.LastFinalizedTime?.ToDateTimeOffset(),
FinalizationPeriodEma: consensusInfo.HasFinalizationPeriodEma ? consensusInfo.FinalizationPeriodEma : null,
FinalizationPeriodEmsd: consensusInfo.HasFinalizationPeriodEmsd ? consensusInfo.FinalizationPeriodEmsd : null
);
FinalizationPeriodEmsd: consensusInfo.HasFinalizationPeriodEmsd ? consensusInfo.FinalizationPeriodEmsd : null,
ConcordiumBftDetails: ConcordiumBftDetails.From(consensusInfo));
}
10 changes: 10 additions & 0 deletions src/Types/Round.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Concordium.Sdk.Types;

/// <summary>
/// Round number. Applies to protocol 6 and onward.
/// </summary>
/// <param name="RoundNumber"></param>
public readonly record struct Round(ulong RoundNumber)
{
internal static Round From(Grpc.V2.Round round) => new(round.Value);
}

0 comments on commit d422449

Please sign in to comment.