Skip to content

Commit

Permalink
Merge pull request #58 from RemarkableTools/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
axenteoctavian authored Nov 26, 2023
2 parents fcd05b0 + da4fe44 commit 685282d
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/Mx.NET.SDK.Core/Domain/Abi/Abi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Variant
{
public string Name { get; set; }
public int Discriminant { get; set; }
public Field[] Fields { get; set; }
}

public class Endpoint
Expand Down
7 changes: 5 additions & 2 deletions src/Mx.NET.SDK.Core/Domain/Abi/AbiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ private TypeValue GetTypeValue(string type)
return TypeValue.EnumValue(typeFromStruct.Type,
typeFromStruct.Variants?
.ToList()
.Select(c => new FieldDefinition(c.Name, "", GetTypeValue(TypeValue.FromRustType("Enum").RustType)))
.Select(v => new VariantDefinition(v.Name, "", v.Discriminant, v.Fields?
.ToList()
.Select(f=> new FieldDefinition(f.Name, "", GetTypeValue(f.Type)))
.ToArray()))
.ToArray());
}
else if (typeFromStruct.Type == "struct")
{
return TypeValue.StructValue(typeFromStruct.Type,
typeFromStruct.Fields?
.ToList()
.Select(c => new FieldDefinition(c.Name, "", GetTypeValue(c.Type)))
.Select(f => new FieldDefinition(f.Name, "", GetTypeValue(f.Type)))
.ToArray());

}
Expand Down
65 changes: 55 additions & 10 deletions src/Mx.NET.SDK.Core/Domain/Codec/EnumBinaryCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using Mx.NET.SDK.Core.Domain.Values;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mx.NET.SDK.Core.Domain.Codec
{
public class EnumBinaryCodec : IBinaryCodec
public class EnumBinaryCodec : IBinaryCodec
{
private readonly BinaryCodec _binaryCodec;
public string Type => TypeValue.BinaryTypes.Enum;
Expand All @@ -18,15 +17,33 @@ public EnumBinaryCodec(BinaryCodec binaryCodec)

public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
{
var fieldDefinitions = type.GetFieldDefinitions();
var fields = new List<EnumField>();
var originalBuffer = data;
var offset = 0;

var (value, bytesLength) = _binaryCodec.DecodeNested(data, fieldDefinitions[0].Type);
var (discriminant, lengthOfDiscriminant) = ReadDiscriminant(data);
offset += lengthOfDiscriminant;
data = originalBuffer.Slice(offset);

var index = int.Parse(value.ToString());
var enumValue = new EnumValue(type, new EnumField(fieldDefinitions[index].Name, value));
return (enumValue, 1);
var index = int.Parse(discriminant.ToString());
var variantDefinitions = type.GetVariantDefinitions();
var fieldDefinitions = variantDefinitions[index].Fields ?? new FieldDefinition[0];
var fields = new List<Field>();

foreach (var fieldDefinition in fieldDefinitions)
{
var (value, fieldsLength) = _binaryCodec.DecodeNested(data, fieldDefinition.Type);
fields.Add(new Field(fieldDefinition.Name, value));
offset += fieldsLength;
data = originalBuffer.Slice(offset);
}

var enumValue = new EnumValue(type, variantDefinitions[index], fields.ToArray());
return (enumValue, offset);
}

private (IBinaryType Value, int BytesLength) ReadDiscriminant(byte[] data)
{
return _binaryCodec.DecodeNested(data, TypeValue.U8TypeValue);
}

public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
Expand All @@ -38,12 +55,40 @@ public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
public byte[] EncodeNested(IBinaryType value)
{
var enumValue = value.ValueOf<EnumValue>();
return Encoding.ASCII.GetBytes(enumValue.Variant.Discriminant.ToString());
var buffer = new List<byte[]>();

var discriminant = new NumericValue(TypeValue.U8TypeValue, enumValue.Discriminant);
var discriminantBuffer = _binaryCodec.EncodeNested(discriminant);

buffer.Add(discriminantBuffer);
foreach (var field in enumValue.Fields)
{
var fieldBuffer = _binaryCodec.EncodeNested(field.Value);
buffer.Add(fieldBuffer);
}

var data = buffer.SelectMany(s => s);
return data.ToArray();
}

public byte[] EncodeTopLevel(IBinaryType value)
{
return EncodeNested(value);
var enumValue = value.ValueOf<EnumValue>();
var hasFields = enumValue.Fields?.Length > 0;
var buffer = new List<byte[]>();

var discriminant = new NumericValue(TypeValue.U8TypeValue, enumValue.Discriminant);
var discriminantBuffer = hasFields ? _binaryCodec.EncodeNested(discriminant) : _binaryCodec.EncodeTopLevel(discriminant);
buffer.Add(discriminantBuffer);

foreach (var field in enumValue.Fields)
{
var fieldBuffer = _binaryCodec.EncodeNested(field.Value);
buffer.Add(fieldBuffer);
}

var data = buffer.SelectMany(s => s);
return data.ToArray();
}
}
}
40 changes: 40 additions & 0 deletions src/Mx.NET.SDK.Core/Domain/Codec/StringBinaryCodec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Mx.NET.SDK.Core.Domain.Values;

namespace Mx.NET.SDK.Core.Domain.Codec
{
public class StringBinaryCodec : IBinaryCodec
{
private readonly BytesBinaryCodec _bytesBinaryCodec;

public StringBinaryCodec()
{
_bytesBinaryCodec = new BytesBinaryCodec();
}
public string Type => TypeValue.BinaryTypes.String;

public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
{
var (value, bytesLength) = _bytesBinaryCodec.DecodeNested(data, type);
return (StringValue.FromUtf8(value.ValueOf<StringValue>().ToString()), bytesLength);
}

public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
{
return new StringValue(data, type);
}

public byte[] EncodeNested(IBinaryType value)
{
var stringValueObject = value.ValueOf<StringValue>();
var buffer = BytesValue.FromUtf8(stringValueObject.ToString());

return _bytesBinaryCodec.EncodeNested(buffer);
}

public byte[] EncodeTopLevel(IBinaryType value)
{
var bytes = value.ValueOf<StringValue>();
return bytes.Buffer;
}
}
}
17 changes: 8 additions & 9 deletions src/Mx.NET.SDK.Core/Domain/Codec/StructBinaryCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public StructBinaryCodec(BinaryCodec binaryCodec)
public (IBinaryType Value, int BytesLength) DecodeNested(byte[] data, TypeValue type)
{
var fieldDefinitions = type.GetFieldDefinitions();
var fields = new List<StructField>();
var fields = new List<Field>();
var originalBuffer = data;
var offset = 0;

foreach (var fieldDefinition in fieldDefinitions)
{
var (value, bytesLength) = _binaryCodec.DecodeNested(data, fieldDefinition.Type);
fields.Add(new StructField(fieldDefinition.Name, value));
offset += bytesLength;
var (value, fieldsLength) = _binaryCodec.DecodeNested(data, fieldDefinition.Type);
fields.Add(new Field(fieldDefinition.Name, value));
offset += fieldsLength;
data = originalBuffer.Slice(offset);
}

Expand All @@ -43,16 +43,15 @@ public IBinaryType DecodeTopLevel(byte[] data, TypeValue type)
public byte[] EncodeNested(IBinaryType value)
{
var structValue = value.ValueOf<StructValue>();
var buffers = new List<byte[]>();
var fields = structValue.Fields;
var fieldsBuffer = new List<byte[]>();

foreach (var field in fields)
foreach (var field in structValue.Fields)
{
var fieldBuffer = _binaryCodec.EncodeNested(field.Value);
buffers.Add(fieldBuffer);
fieldsBuffer.Add(fieldBuffer);
}

var data = buffers.SelectMany(s => s);
var data = fieldsBuffer.SelectMany(s => s);
return data.ToArray();
}

Expand Down
3 changes: 1 addition & 2 deletions src/Mx.NET.SDK.Core/Domain/Values/BytesValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace Mx.NET.SDK.Core.Domain.Values
{
public class BytesValue : BaseBinaryValue
{
public BytesValue(byte[] data, TypeValue type)
: base(type)
public BytesValue(byte[] data, TypeValue type) : base(type)
{
Buffer = data;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Mx.NET.SDK.Core/Domain/Values/ESDTIdentifierValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ namespace Mx.NET.SDK.Core.Domain.Values
{
public class ESDTIdentifierValue : BaseBinaryValue
{
public string Value { get; }
public byte[] Buffer { get; }

public ESDTIdentifierValue(byte[] data, TypeValue type) : base(type)
{
Buffer = data;
Value = Encoding.UTF8.GetString(data);
}

public string Value { get; }

public byte[] Buffer { get; }

public static ESDTIdentifierValue From(byte[] data)
{
return new ESDTIdentifierValue(data, TypeValue.TokenIdentifierValue);
Expand Down
19 changes: 0 additions & 19 deletions src/Mx.NET.SDK.Core/Domain/Values/EnumField.cs

This file was deleted.

15 changes: 10 additions & 5 deletions src/Mx.NET.SDK.Core/Domain/Values/EnumValue.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using Mx.NET.SDK.Core.Domain.Helper;
using System.Linq;

namespace Mx.NET.SDK.Core.Domain.Values
{
public class EnumValue : BaseBinaryValue
{
public EnumField Variant { get; }
public string Name { get; }
public int Discriminant { get; }
public Field[] Fields { get; }

public EnumValue(TypeValue enumType, EnumField variant) : base(enumType)
public EnumValue(TypeValue enumType, VariantDefinition variant, Field[] fields) : base(enumType)
{
Variant = variant;
Name = variant.Name;
Discriminant = variant.Discriminant;
Fields = fields;
}

public override string ToString()
{
return Variant.Name;
return Discriminant.ToString();
}

public override T ToObject<T>()
Expand All @@ -23,7 +28,7 @@ public override T ToObject<T>()

public override string ToJson()
{
return JsonUnqtWrapper.Serialize(Variant.Discriminant.ToJson());
return JsonUnqtWrapper.Serialize((Name, Fields?.Select(f => f.Value.ToJson())));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Mx.NET.SDK.Core.Domain.Values
{
public class StructField
public class Field
{
public IBinaryType Value { get; }
public string Name { get; }
public IBinaryType Value { get; }

public StructField(string name, IBinaryType value)
public Field(string name, IBinaryType value)
{
Value = value;
Name = name;
Value = value;
}

public override string ToString()
Expand Down
47 changes: 47 additions & 0 deletions src/Mx.NET.SDK.Core/Domain/Values/StringValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Text;
using Mx.NET.SDK.Core.Domain.Helper;

namespace Mx.NET.SDK.Core.Domain.Values
{
public class StringValue : BaseBinaryValue
{
public string Value { get; }
public byte[] Buffer { get; }

public StringValue(byte[] data, TypeValue type) : base(type)
{
Buffer = data;
Value = Encoding.UTF8.GetString(data);
}

public static StringValue FromUtf8(string utf8String)
{
return new StringValue(Encoding.UTF8.GetBytes(utf8String), TypeValue.StringValue);
}

public static StringValue FromHex(string hexString)
{
return new StringValue(Converter.FromHexString(hexString), TypeValue.StringValue);
}

public static StringValue FromBuffer(byte[] data)
{
return new StringValue(data, TypeValue.StringValue);
}

public int GetLength()
{
return Value.Length;
}

public override string ToString()
{
return Value;
}

public override string ToJson()
{
return ToString();
}
}
}
6 changes: 3 additions & 3 deletions src/Mx.NET.SDK.Core/Domain/Values/StructValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Mx.NET.SDK.Core.Domain.Values
{
public class StructValue : BaseBinaryValue
{
public StructField[] Fields { get; }
public Field[] Fields { get; }

public StructValue(TypeValue structType, StructField[] fields) : base(structType)
public StructValue(TypeValue structType, Field[] fields) : base(structType)
{
Fields = fields;
CheckTyping();
}

public StructField GetStructField(string name)
public Field GetStructField(string name)
{
var field = Fields.SingleOrDefault(f => f.Name == name);
return field;
Expand Down
Loading

0 comments on commit 685282d

Please sign in to comment.