Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public struct TestLog
public byte[] Datas;
public bool Truth;
public Address Address;
public UInt128 Value128;
public UInt256 Value256;
}

[Fact]
Expand All @@ -33,7 +35,9 @@ public void Deserialize_Basic_Log_Success()
Data = 0xAA,
Datas = new byte[] { 0xBB, 0xCC, 0xDD },
Truth = true,
Address = "0x0000000000000000000000000000000000000001".HexToAddress()
Address = "0x0000000000000000000000000000000000000001".HexToAddress(),
Value128 = 123,
Value256 = 456
};

var testBytes = primitiveSerializer.Serialize(testStruct);
Expand All @@ -47,6 +51,8 @@ public void Deserialize_Basic_Log_Success()
Assert.True(testStruct.Datas.SequenceEqual((byte[])deserializedLog.Datas));
Assert.Equal(testStruct.Truth, deserializedLog.Truth);
Assert.Equal(testStruct.Address.ToUint160().ToBase58Address(network), deserializedLog.Address);
Assert.Equal(testStruct.Value128.ToString(), deserializedLog.Value128.ToString());
Assert.Equal(testStruct.Value256.ToString(), deserializedLog.Value256.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public dynamic DeserializeLogData(byte[] bytes, Type type)
{
object fieldValue = this.primitiveSerializer.Deserialize(fieldType, fieldBytes);

if (fieldType == typeof(UInt128) || fieldType == typeof(UInt256))
fieldValue = fieldValue.ToString();

instance[field.Name] = fieldValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ protected override JsonContract CreateContract(Type objectType)
{
contract.Converter = new AddressJsonConverter(this.network);
}
else if (objectType == typeof(UInt128))
{
contract.Converter = new ToStringJsonConverter<UInt128>();
}
else if (objectType == typeof(UInt256))
{
contract.Converter = new ToStringJsonConverter<UInt256>();
}

return contract;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Stratis.Bitcoin.Features.SmartContracts.ReflectionExecutor
{
public class ToStringJsonConverter<T> : JsonConverter
{
public ToStringJsonConverter()
{
}

public override bool CanRead
{
get { return false; }
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
JValue v = JValue.CreateString(value.ToString());
v.WriteTo(writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ public void Address_Json_Outputs_As_Base58String()
Assert.Equal(expectedString, jsonOutput.Replace("\"", ""));
}

[Fact]
public void UInt256_Json_Outputs_As_String()
{
UInt256 testUInt256 = (UInt256)123;
string expectedString = testUInt256.ToString();

string jsonOutput = JsonConvert.SerializeObject(testUInt256, new JsonSerializerSettings
{
ContractResolver = this.resolver
});
Assert.Equal(expectedString, jsonOutput.Replace("\"", ""));
}

[Fact]
public void UInt128_Json_Outputs_As_String()
{
UInt128 testUInt128 = (UInt128)123;
string expectedString = testUInt128.ToString();

string jsonOutput = JsonConvert.SerializeObject(testUInt128, new JsonSerializerSettings
{
ContractResolver = this.resolver
});
Assert.Equal(expectedString, jsonOutput.Replace("\"", ""));
}

[Fact]
public void LocalExecutionResult_Outputs_With_Address()
{
Expand Down