Skip to content

Commit

Permalink
Add interfaces to BaseType (SubstrateGaming#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apolixit committed Aug 30, 2023
1 parent 4d1b0c6 commit 1cef7dd
Show file tree
Hide file tree
Showing 15 changed files with 1,817 additions and 306 deletions.
41 changes: 41 additions & 0 deletions Substrate.NetApi.Test/TypeConverters/BaseTypesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Substrate.NetApi.Model.Types.Base;
using Substrate.NetApi.Model.Types.Primitive;
using NUnit.Framework;
using Substrate.NetApi.Model.Types.Base.Abstraction;

namespace Substrate.NetApi.Test
{
Expand All @@ -29,6 +30,10 @@ public void BaseTupleTest()
Assert.AreEqual(((U16)t2.Value[1]).Value, ((U16)new BaseTuple<U16, U16>(u16Param, u16Param).Value[1]).Value);
Assert.AreEqual(t2.TypeSize, new BaseTuple<U16, U16>(u16Param, u16Param).TypeSize);

var t2Generic = t2.GetValues();
Assert.AreEqual(2, t2Generic.Length);
Assert.IsTrue(t2Generic.All(x => x is U16));

var t3 = new BaseTuple<U16, U16, U16>();
t3.Create("0x2a002a002a00");
Assert.AreEqual(3, t3.Value.Length);
Expand Down Expand Up @@ -69,6 +74,26 @@ public void BaseTupleCreateTest()
Assert.AreEqual(((U32)tupleOfTwo_1.Value[1]).Value, ((U32)tupleOfTwo_2.Value[1]).Value);
}

/// <summary>
/// Based on <see cref="ValueTests.AccountDataTest"/> and <see cref="ValueTests.AccountInfoTest"/>
/// </summary>
[Test]
public void BaseTupleInheritenceTest()
{
var accountDataWithCharity = new AccountDataWithCharity();
accountDataWithCharity.Create("518fd3f9a8503a4f7e0000000000000000c040b571e8030000000000000000000000c16ff286230000000000000000000000000000000000000000000000000000000000000000000000000000000000");

var accountInfo = new AccountInfo();
accountInfo.Create(Utils.HexToByteArray(
"0500000000000000010000001d58857016a4755a6c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));

IBaseEnumerable tuple = new BaseTuple<AccountDataWithCharity, AccountInfo>(accountDataWithCharity, accountInfo);

Assert.That(tuple.GetValues().Count(), Is.EqualTo(2));
Assert.That(tuple.GetValues()[0], Is.EqualTo(accountDataWithCharity));
Assert.That(tuple.GetValues()[1], Is.EqualTo(accountInfo));
}

[Test]
public void BaseVecTest()
{
Expand All @@ -90,6 +115,11 @@ public void BaseVecTest()
var baseVecCtor_2 = new BaseVec<U16>(
new uint[] { 4, 8, 15, 16, 23, 42, 100 }.Select(x => new U16((ushort)x)).ToArray());
Assert.IsFalse(baseVecCtor.Equals(baseVecCtor_2));

var genericValues = baseVec.GetValues();
Assert.AreEqual(genericValues.Length, baseVec.Value.Length);
Assert.IsInstanceOf<U16>(genericValues.First());
Assert.That(((U16)genericValues.First()).Value, Is.EqualTo(4));
}

[Test]
Expand Down Expand Up @@ -160,6 +190,11 @@ public void BaseBitSeqTest()
Assert.AreEqual(bitSeqTest2[i], baseBitSeq2.Value[i].Value);
}
Assert.AreEqual("0xa04141140000", Utils.Bytes2HexString(baseBitSeq2.Encode()).ToLower());

var baseBitSeqInterfaceResult = baseBitSeq1_1.GetValues();
Assert.That(baseBitSeqInterfaceResult.Count(), Is.EqualTo(baseBitSeq1_1.Value.Count()));
Assert.IsInstanceOf<U8>(baseBitSeqInterfaceResult.First());
Assert.That((U8)baseBitSeqInterfaceResult.First(), Is.EqualTo(baseBitSeq1_1.Value.First()));
}

private byte[] FromBitString(string str)
Expand Down Expand Up @@ -238,6 +273,12 @@ public void BaseEnumTest()

Assert.AreNotEqual(baseEnumFromValue.Bytes, new BaseEnum<PartialBalanceEvents>(PartialBalanceEvents.BalanceSet).Bytes);
Assert.AreNotEqual(baseEnumFromValue.Bytes, new BaseEnum<PartialBalanceEvents>(PartialBalanceEvents.BalanceSet).Value);

var eventEnum = baseEnumFromHex.GetEnum();
var eventData = baseEnumFromHex.GetAssociatedData();

Assert.IsInstanceOf<PartialBalanceEvents>(eventEnum);
Assert.That(((BaseVoid)eventData).Bytes, Is.Null);
}

[Test]
Expand Down
9 changes: 8 additions & 1 deletion Substrate.NetApi.Test/TypeConverters/TypeEncodingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ public void ExtEnumCreateTest()
Assert.That(vecExtEnumTypeFromCreateValue.Bytes, Is.EqualTo(vecExtEnumTypeFromCreateByteArray.Bytes));
Assert.That(vecExtEnumTypeFromCreateValue.Value, Is.EqualTo(vecExtEnumTypeFromCreateByteArray.Value));


Assert.That(vecExtEnumTypeFromCreateValue.Bytes, Is.EqualTo(vecExtEnumTypeFromCreateHex.Bytes));
Assert.That(vecExtEnumTypeFromCreateValue.Value, Is.EqualTo(vecExtEnumTypeFromCreateHex.Value));

var eventEnum = vecExtEnumTypeFromCreateValue.GetEnum();
Assert.That(eventEnum, Is.EqualTo(PhaseState.None));
Assert.That(eventEnum, Is.Not.EqualTo(Substrate.NetApi.Test.BaseTypesTest.PartialBalanceEvents.Endowed));

var eventData = vecExtEnumTypeFromCreateValue.GetAssociatedData();
Assert.IsInstanceOf<U8>(eventData);
Assert.That(((U8)eventData).Value, Is.EqualTo(1));
}

[Test]
Expand Down
64 changes: 62 additions & 2 deletions Substrate.NetApi.Test/Values/ValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,55 @@ public class AccountData : BaseType
{
public override string TypeName() => "AccountData<T::Balance>";

public override byte[] Encode() => null;
public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(Free.Encode());
result.AddRange(Reserved.Encode());
result.AddRange(MiscFrozen.Encode());
result.AddRange(FeeFrozen.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
var start = p;

Free = new Balance();
Free.Decode(byteArray, ref p);

Reserved = new Balance();
Reserved.Decode(byteArray, ref p);

MiscFrozen = new Balance();
MiscFrozen.Decode(byteArray, ref p);

FeeFrozen = new Balance();
FeeFrozen.Decode(byteArray, ref p);

TypeSize = p - start;
}

public Balance Free { get; private set; }
public Balance Reserved { get; private set; }
public Balance MiscFrozen { get; private set; }
public Balance FeeFrozen { get; private set; }
}

public class AccountDataWithCharity : AccountData
{
public override string TypeName() => "AccountDataNewVersion<T::Balance>";

public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(Free.Encode());
result.AddRange(Reserved.Encode());
result.AddRange(MiscFrozen.Encode());
result.AddRange(FeeFrozen.Encode());
result.AddRange(Charity.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
Expand All @@ -53,20 +101,32 @@ public override void Decode(byte[] byteArray, ref int p)
FeeFrozen = new Balance();
FeeFrozen.Decode(byteArray, ref p);

Charity = new Balance();
Charity.Decode(byteArray, ref p);

TypeSize = p - start;
}

public Balance Free { get; private set; }
public Balance Reserved { get; private set; }
public Balance MiscFrozen { get; private set; }
public Balance FeeFrozen { get; private set; }
public Balance Charity { get; private set; }
}

public class AccountInfo : BaseType
{
public override string TypeName() => "AccountInfo<T::Index, T::AccountData>";

public override byte[] Encode() => null;
public override byte[] Encode()
{
var result = new List<byte>();
result.AddRange(Nonce.Encode());
result.AddRange(Consumers.Encode());
result.AddRange(Providers.Encode());
result.AddRange(AccountData.Encode());
return result.ToArray();
}

public override void Decode(byte[] byteArray, ref int p)
{
Expand Down
13 changes: 13 additions & 0 deletions Substrate.NetApi/Model/Types/Base/Abstraction/IBaseBitSeq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Model.Types.Base.Abstraction
{
public interface IBaseBitSeq : IType
{
IType[] GetValues();
byte[] Reverse(byte[] b);
byte Reverse(byte b);
}
}
11 changes: 11 additions & 0 deletions Substrate.NetApi/Model/Types/Base/Abstraction/IBaseCom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Model.Types.Base.Abstraction
{
public interface IBaseCom : IType
{
CompactInteger Value { get; }
}
}
12 changes: 12 additions & 0 deletions Substrate.NetApi/Model/Types/Base/Abstraction/IBaseEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Model.Types.Base.Abstraction
{
public interface IBaseEnum : IType
{
Enum GetEnum();
IType GetAssociatedData();
}
}
14 changes: 14 additions & 0 deletions Substrate.NetApi/Model/Types/Base/Abstraction/IBaseEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Model.Types.Base.Abstraction
{
public interface IBaseEnumerable : IType
{
/// <summary>
/// List of each item
/// </summary>
IType[] GetValues();
}
}
11 changes: 11 additions & 0 deletions Substrate.NetApi/Model/Types/Base/Abstraction/IBaseValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Substrate.NetApi.Model.Types.Base.Abstraction
{
public interface IBaseValue : IType
{
IType GetValue();
}
}
5 changes: 4 additions & 1 deletion Substrate.NetApi/Model/Types/Base/BaseBitSeq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Substrate.NetApi.Model.Types.Base.Abstraction;

namespace Substrate.NetApi.Model.Types.Base
{
Expand All @@ -11,7 +12,7 @@ namespace Substrate.NetApi.Model.Types.Base
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
public class BaseBitSeq<T1, T2> : IType
public class BaseBitSeq<T1, T2> : IBaseBitSeq
where T1 : IType, new()
where T2 : IType, new()
{
Expand Down Expand Up @@ -60,6 +61,8 @@ public void Decode(byte[] byteArray, ref int p)

public virtual T1[] Value { get; internal set; }

public IType[] GetValues() => Value.Select(x => (IType)x).ToArray();

public void Create(T1[] list)
{
Value = list;
Expand Down
5 changes: 3 additions & 2 deletions Substrate.NetApi/Model/Types/Base/BaseCom.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Substrate.NetApi.Model.Types.Base.Abstraction;
using System;

namespace Substrate.NetApi.Model.Types.Base
{
public class BaseCom<T> : BaseType where T : IType, new()
public class BaseCom<T> : BaseType, IBaseCom where T : IType, new()
{
public BaseCom()
{ }
Expand Down
13 changes: 12 additions & 1 deletion Substrate.NetApi/Model/Types/Base/BaseEnum.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Substrate.NetApi.Model.Types.Base.Abstraction;

namespace Substrate.NetApi.Model.Types.Base
{
public class BaseEnum<T> : IType where T : System.Enum
public class BaseEnum<T> : IBaseEnum where T : System.Enum
{
public BaseEnum()
{ }
Expand Down Expand Up @@ -54,6 +55,16 @@ public void Create(byte[] byteArray)

public override string ToString() => JsonConvert.SerializeObject(Value);

public Enum GetEnum()
{
return (Enum)Enum.Parse(typeof(T), Bytes[0].ToString(), true);
}

public IType GetAssociatedData()
{
return new BaseVoid();
}

[JsonConverter(typeof(StringEnumConverter))]
public T Value { get; internal set; }
}
Expand Down
Loading

0 comments on commit 1cef7dd

Please sign in to comment.