Skip to content

Commit

Permalink
Merge pull request #38 from make-software/bugfix/gh-issue-37
Browse files Browse the repository at this point in the history
Fix for GH issue #37
  • Loading branch information
mrkara authored Sep 6, 2023
2 parents 3754779 + 21741df commit 62b63f0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ All notable changes to this project will be documented in this file. The format
[comment]: <> (Fixed: any bug fixes)
[comment]: <> (Security: in case of vulnerabilities)

## [2.1.0]

### Fixed
* Breaking change! the type for the `Proposer` property in the body block has changed from `PublicKey` to `Proposer`.
This new type permits to parse correctly the value `"00"` used for system blocks in Casper network starting from v1.5.

## [2.0.0]

### Added
Expand Down Expand Up @@ -56,6 +62,7 @@ All notable changes to this project will be documented in this file. The format
### Added
* Initial release of Casper .NET SDK.

[2.1.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v2.1.0
[2.0.0]: https://github.com/make-software/casper-net-sdk/releases/tag/v2.0.0
[1.1.2]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.1.2
[1.1.1]: https://github.com/make-software/casper-net-sdk/releases/tag/v1.1.1
Expand Down
19 changes: 19 additions & 0 deletions Casper.Network.SDK.Test/NctlGetXTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ public async Task GetBlockTest()
}
}

[Test]
public async Task GetSystemBlockProposerTest()
{
try
{
var response = await _client.GetBlock(0);
var result = response.Parse();
Assert.IsNotNull(result.Block.Hash);
Assert.IsTrue(result.Block.Body.Proposer.isSystem);
}
catch (RpcClientException e)
{
Assert.IsNotNull(e.RpcError);
Assert.IsNotNull(e.RpcError.Message);
Assert.AreNotEqual(0, e.RpcError.Code);
Assert.IsNotNull(e.RpcError.Data);
}
}

[Test]
public async Task GetAuctionInfoTest()
{
Expand Down
6 changes: 3 additions & 3 deletions Casper.Network.SDK/Casper.Network.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<PackageVersion>2.0.0</PackageVersion>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0</FileVersion>
<PackageVersion>2.1.0</PackageVersion>
<Title>Casper.Network.SDK</Title>
<Authors>make-software</Authors>
<PackageProjectUrl>https://github.com/make-software/casper-net-sdk</PackageProjectUrl>
Expand Down
63 changes: 60 additions & 3 deletions Casper.Network.SDK/Types/Block.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Casper.Network.SDK.Types
Expand Down Expand Up @@ -69,6 +71,61 @@ public class BlockHeader
public string Timestamp { get; init; }
}

/// <summary>
/// Validator that proposed the block
/// </summary>
public class Proposer
{
/// <summary>
/// The block was proposed by the system, not a validator
/// </summary>
public bool isSystem { get; set; }

/// <summary>
/// Validator's public key
/// </summary>
public PublicKey PublicKey { get; set; }

/// <summary>
/// Json converter class to serialize/deserialize a Proposer to/from Json
/// </summary>
public class ProposerConverter : JsonConverter<Proposer>
{
public override Proposer Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
try
{
var pkhex = reader.GetString();
if (pkhex == "00")
return new Proposer()
{
isSystem = true,
};
return new Proposer()
{
isSystem = false,
PublicKey = PublicKey.FromHexString(pkhex),
};
}
catch (Exception e)
{
throw new JsonException(e.Message);
}
}

public override void Write(
Utf8JsonWriter writer,
Proposer proposer,
JsonSerializerOptions options)
{
writer.WriteStringValue(proposer.isSystem ? "00" : proposer.PublicKey.ToAccountHex());
}
}
}

/// <summary>
/// A block body
/// </summary>
Expand All @@ -84,8 +141,8 @@ public class BlockBody
/// Public key of the validator that proposed the block
/// </summary>
[JsonPropertyName("proposer")]
[JsonConverter(typeof(PublicKey.PublicKeyConverter))]
public PublicKey Proposer { get; init; }
[JsonConverter(typeof(Proposer.ProposerConverter))]
public Proposer Proposer { get; init; }

/// <summary>
/// List of Transfer hashes included in the block
Expand Down Expand Up @@ -143,4 +200,4 @@ public class Block
[JsonPropertyName("proofs")]
public List<BlockProof> Proofs { get; init; }
}
}
}

0 comments on commit 62b63f0

Please sign in to comment.