Skip to content

Commit

Permalink
Feat next reelase 0913 (#68)
Browse files Browse the repository at this point in the history
* added unstable watch event

* Added description
improved error handling
  • Loading branch information
darkfriend77 authored Dec 24, 2023
1 parent e9e8c90 commit c79a4fe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
17 changes: 17 additions & 0 deletions Substrate.NetApi/Model/Rpc/TransactionEventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@ public sealed class TransactionEventInfo
{
public TransactionEvent TransactionEvent { get; set; }

/// <summary>
/// Is the total number of individual peers this transaction has been broadcasted to.
/// </summary>
public uint? NumPeers { get; set; }

/// <summary>
/// Is a string containing the hexadecimal-encoded hash of the header of the block. index is an integer indicating the
/// 0-based index of this transaction within the body of this block.
/// </summary>
public Hash Hash { get; set; }

public uint? Index { get; set; }

/// <summary>
/// If the broadcasted field is true, then this transaction has been sent to other peers and might still be included in the
/// chain in the future. No guarantee is offered that the transaction will be included in the chain even if broadcasted is
/// true. However, if broadcasted is false, then it is guaranteed that this transaction will not be included, unless it has
/// been submitted in parallel on a different node.
/// </summary>
public bool? Broadcasted { get; set; }

/// <summary>
/// Is a human-readable error message indicating what happened. This string isn't meant to be shown to end users, but is for
/// developers to understand the problem.
/// </summary>
public string Error { get; set; }
}
}
78 changes: 40 additions & 38 deletions Substrate.NetApi/TypeConverters/ExtrinsicStatusJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Substrate.NetApi.Model.Rpc;
using Substrate.NetApi.Model.Types.Base;
using System;
Expand Down Expand Up @@ -30,50 +31,51 @@ public override TransactionEventInfo ReadJson(JsonReader reader, Type objectType
var eventName = jObject["event"]?.ToString();
if (Enum.TryParse(eventName, true, out TransactionEvent transactionEvent))
{
transactionEventStatus.TransactionEvent = transactionEvent;

switch (transactionEvent)
try
{
case TransactionEvent.Validated:
break;

case TransactionEvent.Broadcasted:
transactionEventStatus.NumPeers = uint.Parse(jObject["numPeers"].ToString());
break;

case TransactionEvent.BestChainBlockIncluded:
var bestChainBlock = jObject["block"];
if (bestChainBlock != null)
{
transactionEventStatus.Hash = new Hash(jObject["block"]["hash"].ToString());
transactionEventStatus.Index = uint.Parse(jObject["block"]["index"].ToString());
}
break;

case TransactionEvent.Finalized:
transactionEventStatus.Hash = new Hash(jObject["block"]["hash"].ToString());
transactionEventStatus.Index = uint.Parse(jObject["block"]["index"].ToString());
break;
transactionEventStatus.TransactionEvent = transactionEvent;

case TransactionEvent.Error:
transactionEventStatus.Error = jObject["error"].ToString();
break;

case TransactionEvent.Invalid:
transactionEventStatus.Error = jObject["error"].ToString();
break;
switch (transactionEvent)
{
case TransactionEvent.Validated:
break;

case TransactionEvent.Broadcasted:
transactionEventStatus.NumPeers = uint.Parse(jObject["numPeers"].ToString());
break;

case TransactionEvent.BestChainBlockIncluded:
case TransactionEvent.Finalized:
transactionEventStatus.Hash = null;
transactionEventStatus.Index = null;
if (jObject["block"] != null)
{
transactionEventStatus.Hash = new Hash(jObject["block"]["hash"].ToString());
transactionEventStatus.Index = uint.Parse(jObject["block"]["index"].ToString());
}
break;

case TransactionEvent.Dropped:
// TODO, check if this works broadcassted boolean
//transactionEventStatus.Broadcasted = bool.Parse(jObject["broadcasted"].ToString());
transactionEventStatus.Error = jObject["error"].ToString();
break;
case TransactionEvent.Error:
transactionEventStatus.Error = jObject["error"].ToString();
break;

default:
throw new NotImplementedException(
$"Unimplemented state {transactionEvent} with value '{reader.Value}'.");
case TransactionEvent.Invalid:
transactionEventStatus.Error = jObject["error"].ToString();
break;

case TransactionEvent.Dropped:
transactionEventStatus.Broadcasted = bool.Parse(jObject["broadcasted"].ToString());
transactionEventStatus.Error = jObject["error"].ToString();
break;

default:
throw new NotImplementedException(
$"Unimplemented state {transactionEvent} with value '{reader.Value}'.");
}
}
catch (Exception ex)
{
Log.Warning("TransactionEventInfo[{eventEnum}]: JObject: {jobject} - {error}", jObject.ToString(), transactionEvent, ex);
}
}

Expand Down

0 comments on commit c79a4fe

Please sign in to comment.