Skip to content

Commit

Permalink
fix bug on vector of structs typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcs85 committed Apr 1, 2019
1 parent 5ecbd6d commit a14038d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 39 deletions.
61 changes: 26 additions & 35 deletions EosSharp/EosSharp.Core/Providers/AbiSerializationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,16 @@ private void WriteAction(MemoryStream ms, Core.Api.v1.Action action, Abi abi)

private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
{
var uwtype = UnwrapTypeDef(abi, type);

//optional type
if(type.EndsWith("?"))
if (uwtype.EndsWith("?"))
{
WriteByte(ms, value != null ? 1 : 0);
if(value != null)
{
WriteByte(ms, 1);
type.Substring(0, type.Length - 1);
uwtype.Substring(0, uwtype.Length - 1);
}
else
{
Expand All @@ -695,10 +697,10 @@ private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
}

// array type
if(type.EndsWith("[]"))
if(uwtype.EndsWith("[]"))
{
var items = (ICollection)value;
var arrayType = type.Substring(0, type.Length - 2);
var arrayType = uwtype.Substring(0, uwtype.Length - 2);

WriteVarUint32(ms, items.Count);
foreach (var item in items)
Expand All @@ -715,7 +717,7 @@ private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
}
else
{
var abiStruct = GetTypeAbiStruct(abi, type);
var abiStruct = abi.structs.FirstOrDefault(s => s.name == uwtype);
if (abiStruct != null)
{
WriteAbiStruct(ms, value, abiStruct, abi);
Expand Down Expand Up @@ -773,6 +775,17 @@ private void WriteAbiStruct(MemoryStream ms, object value, AbiStruct abiStruct,
}
}

private string UnwrapTypeDef(Abi abi, string type)
{
var wtype = abi.types.FirstOrDefault(t => t.new_type_name == type);
if(wtype != null && wtype.type != type)
{
return UnwrapTypeDef(abi, wtype.type);
}

return type;
}

private TSerializer GetTypeSerializerAndCache<TSerializer>(string type, Dictionary<string, TSerializer> typeSerializers, Abi abi)
{
TSerializer nativeSerializer;
Expand All @@ -781,11 +794,11 @@ private TSerializer GetTypeSerializerAndCache<TSerializer>(string type, Dictiona
return nativeSerializer;
}

var abiType = abi.types.FirstOrDefault(t => t.new_type_name == type);
var abiTypeDef = abi.types.FirstOrDefault(t => t.new_type_name == type);

if(abiType != null)
if(abiTypeDef != null)
{
var serializer = GetTypeSerializerAndCache(abiType.type, typeSerializers, abi);
var serializer = GetTypeSerializerAndCache(abiTypeDef.type, typeSerializers, abi);

if(serializer != null)
{
Expand All @@ -796,29 +809,6 @@ private TSerializer GetTypeSerializerAndCache<TSerializer>(string type, Dictiona

return default(TSerializer);
}

private AbiStruct GetTypeAbiStruct(Abi abi, string type)
{
var abiType = abi.types.FirstOrDefault(t => t.new_type_name == type);

if(abiType != null)
{
var abiStruct = abi.structs.FirstOrDefault(s => s.name == abiType.type);
if (abiStruct != null)
{
return abiStruct;
}
else
{
return GetTypeAbiStruct(abi, abiType.type);
}
}
else
{
return abi.structs.FirstOrDefault(s => s.name == type);
}
}

#endregion

#region Reader Functions
Expand Down Expand Up @@ -1227,9 +1217,10 @@ private List<AbiTable> ReadAbiTableList(byte[] data, ref int readIndex)
private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)
{
object value = null;
var uwtype = UnwrapTypeDef(abi, type);

//optional type
if (type.EndsWith("?"))
if (uwtype.EndsWith("?"))
{
var opt = (byte)ReadByte(data, ref readIndex);

Expand All @@ -1240,9 +1231,9 @@ private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)
}

// array type
if (type.EndsWith("[]"))
if (uwtype.EndsWith("[]"))
{
var arrayType = type.Substring(0, type.Length - 2);
var arrayType = uwtype.Substring(0, uwtype.Length - 2);
var size = Convert.ToInt32(ReadVarUint32(data, ref readIndex));
var items = new List<object>(size);

Expand All @@ -1262,7 +1253,7 @@ private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)
}
else
{
var abiStruct = GetTypeAbiStruct(abi, type);
var abiStruct = abi.structs.FirstOrDefault(s => s.name == uwtype);
if (abiStruct != null)
{
value = ReadAbiStruct(data, abiStruct, abi, ref readIndex);
Expand Down
1 change: 1 addition & 0 deletions EosSharp/EosSharp.UnitTests.Core/EosTestCasesDef.t4
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"GetProducers",
"GetScheduledTransactions",
"CreateTransactionArrayData",
"CreateTransactionActionArrayStructData",
"CreateTransactionAnonymousObjectData",
"CreateTransaction",
"CreateNewAccount"
Expand Down
42 changes: 42 additions & 0 deletions EosSharp/EosSharp.UnitTests.Core/EosUnitTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,48 @@ public Task CreateTransactionArrayData()
});
}

public Task CreateTransactionActionArrayStructData()
{
var args = new List<object>()
{
{
new Dictionary<string, object>()
{
{ "air_id", Convert.ToUInt64("8") },
{ "air_place", Convert.ToString("监测点8888") },
{ "air_pm2_5", Convert.ToString("pm2.5数值") },
{ "air_voc", Convert.ToString("voc数值") },
{ "air_carbon", Convert.ToString("碳数值") },
{ "air_nitrogen", Convert.ToString("氮数值") },
{ "air_sulfur", Convert.ToString("硫数值") },
{ "air_longitude", Convert.ToString("经度") },
{ "air_latitude", Convert.ToString("纬度") }
}
}
};

return Eos.CreateTransaction(new Transaction()
{
actions = new List<Core.Api.v1.Action>()
{
new Core.Api.v1.Action()
{
account = "platform",
authorization = new List<PermissionLevel>()
{
new PermissionLevel() {actor = "player1", permission = "active" }
},
name = "airquality",
data = new {
aql = args,
a = args,
b = args
}
}
}
});
}

public Task CreateTransactionAnonymousObjectData()
{
return Eos.CreateTransaction(new Transaction()
Expand Down
19 changes: 19 additions & 0 deletions EosSharp/EosSharp.UnitTests.Unity3D/EosUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ public async Task CreateTransactionArrayData()
else
Console.WriteLine("Test CreateTransactionArrayData run failed.");
}
public async Task CreateTransactionActionArrayStructData()
{
bool success = false;
try
{
await EosUnitTestCases.CreateTransactionActionArrayStructData();
success = true;
}
catch (Exception ex)
{
Console.WriteLine(JsonConvert.SerializeObject(ex));
}

if(success)
Console.WriteLine("Test CreateTransactionActionArrayStructData run successfuly.");
else
Console.WriteLine("Test CreateTransactionActionArrayStructData run failed.");
}
public async Task CreateTransactionAnonymousObjectData()
{
bool success = false;
Expand Down Expand Up @@ -200,6 +218,7 @@ public async Task TestAll()
await GetProducers();
await GetScheduledTransactions();
await CreateTransactionArrayData();
await CreateTransactionActionArrayStructData();
await CreateTransactionAnonymousObjectData();
await CreateTransaction();
await CreateNewAccount();
Expand Down
17 changes: 17 additions & 0 deletions EosSharp/EosSharp.UnitTests/EosUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ public async Task CreateTransactionArrayData()
}
[TestMethod]
[TestCategory("Eos Tests")]
public async Task CreateTransactionActionArrayStructData()
{
bool success = false;
try
{
await EosUnitTestCases.CreateTransactionActionArrayStructData();
success = true;
}
catch (Exception ex)
{
Console.WriteLine(JsonConvert.SerializeObject(ex));
}

Assert.IsTrue(success);
}
[TestMethod]
[TestCategory("Eos Tests")]
public async Task CreateTransactionAnonymousObjectData()
{
bool success = false;
Expand Down
8 changes: 4 additions & 4 deletions EosSharp/EosSharp/EosSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<Copyright>Copyright 2019</Copyright>
<Product>eos-sharp</Product>
<PackageId>eos-sharp</PackageId>
<AssemblyVersion>2.0.4.0</AssemblyVersion>
<FileVersion>2.0.4.0</FileVersion>
<Version>2.0.4</Version>
<PackageReleaseNotes>Fix bug on specialized Array types</PackageReleaseNotes>
<AssemblyVersion>2.0.5.0</AssemblyVersion>
<FileVersion>2.0.5.0</FileVersion>
<Version>2.0.5</Version>
<PackageReleaseNotes>fix bug on vector of structs typedefs</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup>

Expand Down

0 comments on commit a14038d

Please sign in to comment.