Skip to content

Commit

Permalink
Merge pull request #56 from Concordium/ss/fix-issues-after-grpc-update
Browse files Browse the repository at this point in the history
Made to lower invariant and fixed minor bugs after gRPC update
  • Loading branch information
Søren Schwartz authored Jul 27, 2023
2 parents b1abdfd + e098305 commit b9d4002
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Unreleased changes

## 3.0.0
- Added
- Add optional cancellation token parameter to all client calls.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Currently, helpers for working with transactions of the [`Transfer`](http://deve
The SDK is published on [nuget.org](https://www.nuget.org/packages/ConcordiumNetSdk). Depending on your setup, it can be added to your project as a dependency by running either

```powershell
PM> Install-Package Concordium.SDK -Version 2.0
PM> Install-Package Concordium.SDK -Version 3.0
```
or

Expand Down
4 changes: 2 additions & 2 deletions src/Concordium.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageVersion>2.0.0</PackageVersion>
<PackageVersion>3.0.0</PackageVersion>
<Description>
A .NET integration library written in C# which adds support for
constructing and sending various transactions, as well as querying
Expand All @@ -16,7 +16,7 @@
<PackageTags>concordium;concordium-net-sdk;blockchain;sdk;</PackageTags>
<Company>Concordium</Company>
<PackageId>ConcordiumNetSdk</PackageId>
<Version>2.0.0</Version>
<Version>3.0.0</Version>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MPL-2.0</PackageLicenseExpression>
Expand Down
2 changes: 1 addition & 1 deletion src/Types/AccountBakerPendingChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract record AccountBakerPendingChange
return stakeBakerPendingChange.ChangeCase switch
{
StakePendingChange.ChangeOneofCase.Reduce =>
new AccountBakerReduceStakePending(CcdAmount.From(stakeBakerPendingChange.Reduce.NewStake), stakeBakerPendingChange.Remove.ToDateTimeOffset()),
new AccountBakerReduceStakePending(CcdAmount.From(stakeBakerPendingChange.Reduce.NewStake), stakeBakerPendingChange.Reduce.EffectiveTime.ToDateTimeOffset()),
StakePendingChange.ChangeOneofCase.Remove =>
new AccountBakerRemovePending(stakeBakerPendingChange.Remove.ToDateTimeOffset())
,
Expand Down
6 changes: 3 additions & 3 deletions src/Types/AccountStakingInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ internal static class AccountStakingInfo
/// The account is a baker.
/// </summary>
public sealed record AccountBaker(
BakerId BakerId,
AccountBakerPendingChange? PendingChange,
bool RestakeEarnings,
CcdAmount StakedAmount,
BakerInfo BakerInfo,
AccountBakerPendingChange? PendingChange,
BakerPoolInfo? BakerPoolInfo
) : IAccountStakingInfo
{
internal static AccountBaker From(Grpc.V2.AccountStakingInfo.Types.Baker stakeBaker) =>
new(
BakerId: BakerId.From(stakeBaker.BakerInfo.BakerId),
BakerInfo: BakerInfo.From(stakeBaker.BakerInfo),
PendingChange: AccountBakerPendingChange.From(stakeBaker.PendingChange),
RestakeEarnings: stakeBaker.RestakeEarnings,
StakedAmount: CcdAmount.From(stakeBaker.StakedAmount),
Expand Down
2 changes: 1 addition & 1 deletion src/Types/AccountTransactionEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public sealed record DataRegistered(byte[] Data) : IAccountTransactionEffects
/// <summary>
/// Returns hex representation of data.
/// </summary>
public string ToHex() => Convert.ToHexString(this.Data);
public string ToHexString() => Convert.ToHexString(this.Data).ToLowerInvariant();
}

/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Types/BakerInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Concordium.Sdk.Types;

/// <summary>
/// Information about a baker.
/// </summary>
/// <param name="BakerId">Account index of the account controlling the baker.</param>
/// <param name="BakerElectionVerifyKey">
/// Baker's public key used to check whether they won the lottery or not.
/// </param>
/// <param name="BakerSignatureVerifyKey"></param>
/// <param name="BakerAggregationVerifyKey">
/// 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.
/// </param>
public sealed record BakerInfo(BakerId BakerId,
byte[] BakerElectionVerifyKey,
byte[] BakerSignatureVerifyKey,
byte[] BakerAggregationVerifyKey)
{
internal static BakerInfo From(Grpc.V2.BakerInfo bakerInfo) =>
new(
BakerId.From(bakerInfo.BakerId),
bakerInfo.ElectionKey.Value.ToByteArray(),
bakerInfo.SignatureKey.Value.ToByteArray(),
bakerInfo.AggregationKey.Value.ToByteArray()
);
}
12 changes: 9 additions & 3 deletions src/Types/BakerPoolInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ public sealed record BakerPoolInfo(
BakerPoolOpenStatus OpenStatus,
string MetadataUrl)
{
internal static BakerPoolInfo From(Grpc.V2.BakerPoolInfo poolInfo) =>
new
(
internal static BakerPoolInfo? From(Grpc.V2.BakerPoolInfo? poolInfo)
{
if (poolInfo is null)
{
return null;
}

return new BakerPoolInfo(
CommissionRates: CommissionRates.From(poolInfo.CommissionRates),
OpenStatus: BakerPoolOpenStatus.OpenForAll,
MetadataUrl: poolInfo.Url
);
}
}
2 changes: 1 addition & 1 deletion src/Types/BakerPoolStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static BakerPoolStatus From(Grpc.V2.PoolInfoResponse poolInfoResponse)
CcdAmount.From(poolInfoResponse.EquityCapital),
CcdAmount.From(poolInfoResponse.DelegatedCapital),
CcdAmount.From(poolInfoResponse.DelegatedCapitalCap),
BakerPoolInfo.From(poolInfoResponse.PoolInfo),
BakerPoolInfo.From(poolInfoResponse.PoolInfo)!,
CurrentPaydayBakerPoolStatus.From(poolInfoResponse.CurrentPaydayInfo),
CcdAmount.From(poolInfoResponse.AllPoolTotalCapital));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Types/ContractEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public sealed record ContractEvent(byte[] Bytes)
/// <summary>
/// Return hex representation.
/// </summary>
public string ToHex() => Convert.ToHexString(this.Bytes);
public string ToHexString() => Convert.ToHexString(this.Bytes).ToLowerInvariant();
}
2 changes: 1 addition & 1 deletion src/Types/CredentialRegistrationID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed record CredentialRegistrationId(byte[] Id) : IAccountIdentifier
/// Return hex string representation.
/// </summary>
/// <returns></returns>
public string ToHex() => Convert.ToHexString(this.Id);
public string ToHexString() => Convert.ToHexString(this.Id).ToLowerInvariant();

internal static CredentialRegistrationId From(Grpc.V2.CredentialRegistrationId id) => new(id.Value.ToByteArray());

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public sealed record Parameter(byte[] Param)
/// <summary>
/// Convert parameters to hex string.
/// </summary>
public string ToHexString() => Convert.ToHexString(this.Param);
public string ToHexString() => Convert.ToHexString(this.Param).ToLowerInvariant();
}
4 changes: 2 additions & 2 deletions src/Types/RejectReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public sealed record DuplicateCredIds(IList<byte[]> CredIds) : IRejectReason
/// <summary>
/// Return keys in hex representations.
/// </summary>
public IEnumerable<string> ToHexStrings() => this.CredIds.Select(Convert.ToHexString);
public IEnumerable<string> ToHexStrings() => this.CredIds.Select(k => Convert.ToHexString(k).ToLowerInvariant());
}

/// <summary>
Expand All @@ -295,7 +295,7 @@ public sealed record NonExistentCredIds(IList<byte[]> CredIds) : IRejectReason
/// <summary>
/// Return keys in hex representations.
/// </summary>
public IEnumerable<string> ToHexStrings() => this.CredIds.Select(Convert.ToHexString);
public IEnumerable<string> ToHexStrings() => this.CredIds.Select(k => Convert.ToHexString(k).ToLowerInvariant());
}
/// <summary>
/// Attempt to remove the first credential
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Sha256Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ internal Sha256Hash(ByteString byteString) : base(byteString.ToByteArray()) { }
/// <summary>
/// Return hex representation of data.
/// </summary>
public string ToHex() => Convert.ToHexString(this.AsSpan());
public string ToHexString() => Convert.ToHexString(this.AsSpan()).ToLowerInvariant();
}
27 changes: 16 additions & 11 deletions src/Types/TransactionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ public enum TransactionType : byte
public static class TransactionTypeFactory
{
/// <summary>
/// Get transaction enum from transaction type. The transaction needs to be succeeded hence type
/// <see cref="None"/> will throw an exception.
/// Get transaction enum from transaction type.
/// </summary>
/// <exception cref="MissingTypeException{IAccountTransactionEffects}"></exception>
public static TransactionType From(IAccountTransactionEffects effects) =>
effects switch
public static bool TryFrom(IAccountTransactionEffects effects, out TransactionType? transactionType)
{
transactionType = effects switch
{
AccountTransfer => TransactionType.Transfer,
AccountTransfer accountTransactionEffects => accountTransactionEffects.Memo == null
? TransactionType.Transfer
: TransactionType.TransferWithMemo,
BakerAdded => TransactionType.AddBaker,
BakerConfigured => TransactionType.ConfigureBaker,
BakerKeysUpdated => TransactionType.UpdateBakerKeys,
Expand All @@ -120,18 +122,21 @@ public static TransactionType From(IAccountTransactionEffects effects) =>
DataRegistered => TransactionType.RegisterData,
DelegationConfigured => TransactionType.ConfigureDelegation,
EncryptedAmountTransferred encryptedAmountTransferred =>
encryptedAmountTransferred.Memo == null ?
TransactionType.EncryptedAmountTransfer :
TransactionType.EncryptedAmountTransferWithMemo,
encryptedAmountTransferred.Memo == null
? TransactionType.EncryptedAmountTransfer
: TransactionType.EncryptedAmountTransferWithMemo,
ModuleDeployed => TransactionType.DeployModule,
TransferredToEncrypted => TransactionType.TransferToEncrypted,
TransferredToPublic => TransactionType.TransferToPublic,
TransferredWithSchedule transferredWithSchedule =>
transferredWithSchedule.Memo == null ?
TransactionType.TransferWithSchedule :
TransactionType.TransferWithScheduleAndMemo,
transferredWithSchedule.Memo == null
? TransactionType.TransferWithSchedule
: TransactionType.TransferWithScheduleAndMemo,
None none => none.TransactionType,
_ => throw new MissingTypeException<IAccountTransactionEffects>(effects)
};
return transactionType != null;
}

internal static TransactionType Into(this Grpc.V2.TransactionType transactionType) =>
transactionType switch
Expand Down
30 changes: 30 additions & 0 deletions tests/IntegrationTests/Client/GetAccountInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Concordium.Sdk.Types;
using FluentAssertions;
using Xunit.Abstractions;

namespace Concordium.Sdk.Tests.IntegrationTests.Client;

[Trait("Category", "IntegrationTests")]
public class GetAccountInfo : Tests
{
public GetAccountInfo(ITestOutputHelper output) : base(output)
{ }

[Fact]
public async Task GivenBakerZero_AtGenesisBlock_WhenGetAccountInfo_ThenReturnBakerZeroId()
{
// Arrange
var block = BlockHash.From("4221332d34e1694168c2a0c0b3fd0f273809612cb13d000d5c2e00e85f50f796");
var accountAddress = AccountAddress.From("48XGRnvQoG92T1AwETvW5pnJ1aRSPMKsWtGdKhTqyiNZzMk3Qn");

// Act
var accountInfoAsync = await this.Client.GetAccountInfoAsync(accountAddress, new Given(block));

// Assert
accountInfoAsync.Response.AccountStakingInfo.Should().NotBeNull();
accountInfoAsync.Response.AccountStakingInfo!.Should().BeOfType<AccountBaker>();
var baker = accountInfoAsync.Response.AccountStakingInfo! as AccountBaker;
baker!.BakerInfo.BakerId.Id.Index.Should().Be(0);
}

}

0 comments on commit b9d4002

Please sign in to comment.