Skip to content

Commit a14038d

Browse files
committed
fix bug on vector of structs typedefs
1 parent 5ecbd6d commit a14038d

6 files changed

Lines changed: 109 additions & 39 deletions

File tree

EosSharp/EosSharp.Core/Providers/AbiSerializationProvider.cs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,16 @@ private void WriteAction(MemoryStream ms, Core.Api.v1.Action action, Abi abi)
678678

679679
private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
680680
{
681+
var uwtype = UnwrapTypeDef(abi, type);
682+
681683
//optional type
682-
if(type.EndsWith("?"))
684+
if (uwtype.EndsWith("?"))
683685
{
684686
WriteByte(ms, value != null ? 1 : 0);
685687
if(value != null)
686688
{
687689
WriteByte(ms, 1);
688-
type.Substring(0, type.Length - 1);
690+
uwtype.Substring(0, uwtype.Length - 1);
689691
}
690692
else
691693
{
@@ -695,10 +697,10 @@ private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
695697
}
696698

697699
// array type
698-
if(type.EndsWith("[]"))
700+
if(uwtype.EndsWith("[]"))
699701
{
700702
var items = (ICollection)value;
701-
var arrayType = type.Substring(0, type.Length - 2);
703+
var arrayType = uwtype.Substring(0, uwtype.Length - 2);
702704

703705
WriteVarUint32(ms, items.Count);
704706
foreach (var item in items)
@@ -715,7 +717,7 @@ private void WriteAbiType(MemoryStream ms, object value, string type, Abi abi)
715717
}
716718
else
717719
{
718-
var abiStruct = GetTypeAbiStruct(abi, type);
720+
var abiStruct = abi.structs.FirstOrDefault(s => s.name == uwtype);
719721
if (abiStruct != null)
720722
{
721723
WriteAbiStruct(ms, value, abiStruct, abi);
@@ -773,6 +775,17 @@ private void WriteAbiStruct(MemoryStream ms, object value, AbiStruct abiStruct,
773775
}
774776
}
775777

778+
private string UnwrapTypeDef(Abi abi, string type)
779+
{
780+
var wtype = abi.types.FirstOrDefault(t => t.new_type_name == type);
781+
if(wtype != null && wtype.type != type)
782+
{
783+
return UnwrapTypeDef(abi, wtype.type);
784+
}
785+
786+
return type;
787+
}
788+
776789
private TSerializer GetTypeSerializerAndCache<TSerializer>(string type, Dictionary<string, TSerializer> typeSerializers, Abi abi)
777790
{
778791
TSerializer nativeSerializer;
@@ -781,11 +794,11 @@ private TSerializer GetTypeSerializerAndCache<TSerializer>(string type, Dictiona
781794
return nativeSerializer;
782795
}
783796

784-
var abiType = abi.types.FirstOrDefault(t => t.new_type_name == type);
797+
var abiTypeDef = abi.types.FirstOrDefault(t => t.new_type_name == type);
785798

786-
if(abiType != null)
799+
if(abiTypeDef != null)
787800
{
788-
var serializer = GetTypeSerializerAndCache(abiType.type, typeSerializers, abi);
801+
var serializer = GetTypeSerializerAndCache(abiTypeDef.type, typeSerializers, abi);
789802

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

797810
return default(TSerializer);
798811
}
799-
800-
private AbiStruct GetTypeAbiStruct(Abi abi, string type)
801-
{
802-
var abiType = abi.types.FirstOrDefault(t => t.new_type_name == type);
803-
804-
if(abiType != null)
805-
{
806-
var abiStruct = abi.structs.FirstOrDefault(s => s.name == abiType.type);
807-
if (abiStruct != null)
808-
{
809-
return abiStruct;
810-
}
811-
else
812-
{
813-
return GetTypeAbiStruct(abi, abiType.type);
814-
}
815-
}
816-
else
817-
{
818-
return abi.structs.FirstOrDefault(s => s.name == type);
819-
}
820-
}
821-
822812
#endregion
823813

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

12311222
//optional type
1232-
if (type.EndsWith("?"))
1223+
if (uwtype.EndsWith("?"))
12331224
{
12341225
var opt = (byte)ReadByte(data, ref readIndex);
12351226

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

12421233
// array type
1243-
if (type.EndsWith("[]"))
1234+
if (uwtype.EndsWith("[]"))
12441235
{
1245-
var arrayType = type.Substring(0, type.Length - 2);
1236+
var arrayType = uwtype.Substring(0, uwtype.Length - 2);
12461237
var size = Convert.ToInt32(ReadVarUint32(data, ref readIndex));
12471238
var items = new List<object>(size);
12481239

@@ -1262,7 +1253,7 @@ private object ReadAbiType(byte[] data, string type, Abi abi, ref int readIndex)
12621253
}
12631254
else
12641255
{
1265-
var abiStruct = GetTypeAbiStruct(abi, type);
1256+
var abiStruct = abi.structs.FirstOrDefault(s => s.name == uwtype);
12661257
if (abiStruct != null)
12671258
{
12681259
value = ReadAbiStruct(data, abiStruct, abi, ref readIndex);

EosSharp/EosSharp.UnitTests.Core/EosTestCasesDef.t4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"GetProducers",
3434
"GetScheduledTransactions",
3535
"CreateTransactionArrayData",
36+
"CreateTransactionActionArrayStructData",
3637
"CreateTransactionAnonymousObjectData",
3738
"CreateTransaction",
3839
"CreateNewAccount"

EosSharp/EosSharp.UnitTests.Core/EosUnitTestCases.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,48 @@ public Task CreateTransactionArrayData()
9090
});
9191
}
9292

93+
public Task CreateTransactionActionArrayStructData()
94+
{
95+
var args = new List<object>()
96+
{
97+
{
98+
new Dictionary<string, object>()
99+
{
100+
{ "air_id", Convert.ToUInt64("8") },
101+
{ "air_place", Convert.ToString("监测点8888") },
102+
{ "air_pm2_5", Convert.ToString("pm2.5数值") },
103+
{ "air_voc", Convert.ToString("voc数值") },
104+
{ "air_carbon", Convert.ToString("碳数值") },
105+
{ "air_nitrogen", Convert.ToString("氮数值") },
106+
{ "air_sulfur", Convert.ToString("硫数值") },
107+
{ "air_longitude", Convert.ToString("经度") },
108+
{ "air_latitude", Convert.ToString("纬度") }
109+
}
110+
}
111+
};
112+
113+
return Eos.CreateTransaction(new Transaction()
114+
{
115+
actions = new List<Core.Api.v1.Action>()
116+
{
117+
new Core.Api.v1.Action()
118+
{
119+
account = "platform",
120+
authorization = new List<PermissionLevel>()
121+
{
122+
new PermissionLevel() {actor = "player1", permission = "active" }
123+
},
124+
name = "airquality",
125+
data = new {
126+
aql = args,
127+
a = args,
128+
b = args
129+
}
130+
}
131+
}
132+
});
133+
}
134+
93135
public Task CreateTransactionAnonymousObjectData()
94136
{
95137
return Eos.CreateTransaction(new Transaction()

EosSharp/EosSharp.UnitTests.Unity3D/EosUnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ public async Task CreateTransactionArrayData()
137137
else
138138
Console.WriteLine("Test CreateTransactionArrayData run failed.");
139139
}
140+
public async Task CreateTransactionActionArrayStructData()
141+
{
142+
bool success = false;
143+
try
144+
{
145+
await EosUnitTestCases.CreateTransactionActionArrayStructData();
146+
success = true;
147+
}
148+
catch (Exception ex)
149+
{
150+
Console.WriteLine(JsonConvert.SerializeObject(ex));
151+
}
152+
153+
if(success)
154+
Console.WriteLine("Test CreateTransactionActionArrayStructData run successfuly.");
155+
else
156+
Console.WriteLine("Test CreateTransactionActionArrayStructData run failed.");
157+
}
140158
public async Task CreateTransactionAnonymousObjectData()
141159
{
142160
bool success = false;
@@ -200,6 +218,7 @@ public async Task TestAll()
200218
await GetProducers();
201219
await GetScheduledTransactions();
202220
await CreateTransactionArrayData();
221+
await CreateTransactionActionArrayStructData();
203222
await CreateTransactionAnonymousObjectData();
204223
await CreateTransaction();
205224
await CreateNewAccount();

EosSharp/EosSharp.UnitTests/EosUnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public async Task CreateTransactionArrayData()
137137
}
138138
[TestMethod]
139139
[TestCategory("Eos Tests")]
140+
public async Task CreateTransactionActionArrayStructData()
141+
{
142+
bool success = false;
143+
try
144+
{
145+
await EosUnitTestCases.CreateTransactionActionArrayStructData();
146+
success = true;
147+
}
148+
catch (Exception ex)
149+
{
150+
Console.WriteLine(JsonConvert.SerializeObject(ex));
151+
}
152+
153+
Assert.IsTrue(success);
154+
}
155+
[TestMethod]
156+
[TestCategory("Eos Tests")]
140157
public async Task CreateTransactionAnonymousObjectData()
141158
{
142159
bool success = false;

EosSharp/EosSharp/EosSharp.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
<Copyright>Copyright 2019</Copyright>
1414
<Product>eos-sharp</Product>
1515
<PackageId>eos-sharp</PackageId>
16-
<AssemblyVersion>2.0.4.0</AssemblyVersion>
17-
<FileVersion>2.0.4.0</FileVersion>
18-
<Version>2.0.4</Version>
19-
<PackageReleaseNotes>Fix bug on specialized Array types</PackageReleaseNotes>
16+
<AssemblyVersion>2.0.5.0</AssemblyVersion>
17+
<FileVersion>2.0.5.0</FileVersion>
18+
<Version>2.0.5</Version>
19+
<PackageReleaseNotes>fix bug on vector of structs typedefs</PackageReleaseNotes>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2121
</PropertyGroup>
2222

0 commit comments

Comments
 (0)