Skip to content

Commit

Permalink
Use base64 in JsonSerializer (#1199)
Browse files Browse the repository at this point in the history
* Base64 Json

* No format

* Json Transaction optimization

* Change to Base64

* Revert some changes

* Revert

* Remove Helper.Base64

* Remove Base64FormattingOptions.None
  • Loading branch information
shargon authored Nov 7, 2019
1 parent 87aed99 commit 1dc262f
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 47 deletions.
4 changes: 2 additions & 2 deletions neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.IO.Json;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void TestToJson()
{
JObject json = contract.ToJson();
json["hash"].AsString().Should().Be("0x820944cfdc70976602d71b0091445eedbc661bc5");
json["script"].AsString().Should().Be("01");
json["script"].AsString().Should().Be("AQ==");
json["manifest"].AsString().Should().Be(manifest.ToJson().AsString());
}
}
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/Network/P2P/Payloads/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void ToJson()

JObject scObj = ((JArray)jObj["witnesses"])[0];
scObj["invocation"].AsString().Should().Be("");
scObj["verification"].AsString().Should().Be("51");
scObj["verification"].AsString().Should().Be("UQ==");

jObj["tx"].Should().NotBeNull();
JArray txObj = (JArray)jObj["tx"];
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ public void ToJson()
((JArray)jObj["attributes"]).Count.Should().Be(0);
((JArray)jObj["cosigners"]).Count.Should().Be(0);
jObj["net_fee"].AsString().Should().Be("0");
jObj["script"].AsString().Should().Be("4220202020202020202020202020202020202020202020202020202020202020");
jObj["script"].AsString().Should().Be("QiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA=");
jObj["sys_fee"].AsString().Should().Be("4200000000");
}
}
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public void ToJson()
JObject json = uut.ToJson();
Assert.IsTrue(json.ContainsProperty("invocation"));
Assert.IsTrue(json.ContainsProperty("verification"));
Assert.AreEqual(json["invocation"].AsString(), "2020");
Assert.AreEqual(json["verification"].AsString(), "202020");
Assert.AreEqual(json["invocation"].AsString(), "ICA=");
Assert.AreEqual(json["verification"].AsString(), "ICAg");
}
}
}
4 changes: 2 additions & 2 deletions neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void TestToJson()
balance.Amount = BigInteger.Zero;
balance.LastUpdatedBlock = 0;
var json = balance.ToJson();
json["asset_hash"].AsString().Should().Be("0000000000000000000000000000000000000000");
json["asset_hash"].AsString().Should().Be("0x0000000000000000000000000000000000000000");
json["amount"].AsNumber().Should().Be(0);
json["last_updated_block"].AsNumber().Should().Be(0);
}
Expand All @@ -54,7 +54,7 @@ public void TestToJson()
public void TestFromJson()
{
var json = new JObject();
json["asset_hash"] = "0000000000000000000000000000000000000000";
json["asset_hash"] = "0x0000000000000000000000000000000000000000";
json["amount"] = "0";
json["last_updated_block"] = "0";
var rpcNep5Balance = RpcNep5Balance.FromJson(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void ParseFromJson_Trust()
[TestMethod]
public void ParseFromJson_Groups()
{
var json = @"{""groups"":[{""pubKey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141""}],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
var json = @"{""groups"":[{""pubKey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand Down
6 changes: 3 additions & 3 deletions neo.UnitTests/SmartContract/UT_ContractParameterContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
Expand Down Expand Up @@ -46,13 +46,13 @@ public void TestToString()
var context = new ContractParametersContext(tx);
context.Add(contract, 0, new byte[] { 0x01 });
string str = context.ToString();
str.Should().Be("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"0000000000f277f1144035a43897f9fa115258eac015adcabe000000000000000000000000000000000000000000000100\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1668747476aa\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}");
str.Should().Be("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"AAAAAADyd\\/EUQDWkOJf5\\u002BhFSWOrAFa3KvgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA==\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}]}}}");
}

[TestMethod]
public void TestParse()
{
var ret = ContractParametersContext.Parse("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"0000000000f277f1144035a43897f9fa115258eac015adcabe000000000000000000000000000000000000000000000100\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1668747476aa\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}");
var ret = ContractParametersContext.Parse("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"hex\":\"AAAAAADyd\\/EUQDWkOJf5\\u002BhFSWOrAFa3KvgAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAA==\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}]}}}");
ret.ScriptHashes[0].ToString().Should().Be("0xbecaad15c0ea585211faf99738a4354014f177f2");
((Transaction)ret.Verifiable).Script.ToHexString().Should().Be(new byte[1].ToHexString());
}
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/SmartContract/UT_JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void Serialize_Array_Bool_Str_Num()

var json = JsonSerializer.Serialize(entry).ToString();

Assert.AreEqual(json, "[true,\"74657374\",123]");
Assert.AreEqual(json, "[true,\"dGVzdA==\",123]");
}

[TestMethod]
Expand Down Expand Up @@ -297,7 +297,7 @@ public void Serialize_Array_OfArray()

var json = JsonSerializer.Serialize(entry).ToString();

Assert.AreEqual(json, "[[true,\"7465737431\",123],[true,\"7465737432\",321]]");
Assert.AreEqual(json, "[[true,\"dGVzdDE=\",123],[true,\"dGVzdDI=\",321]]");
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void System_Blockchain_GetBlock()
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteArray));
Assert.AreEqual(engine.ResultStack.Pop().GetByteArray().ToHexString(),
"5b2235316138373966636161303339626461663437353436306637316334616130383562353964313833616239313764356437363762333865613738323766356266222c332c2230303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030222c2230303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030222c322c312c2230303030303030303030303030303030303030303030303030303030303030303030303030303030222c315d");
"5b22556168355c2f4b6f446d39723064555950636353714346745a30594f726b583164646e7334366e676e3962383d222c332c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c322c312c224141414141414141414141414141414141414141414141414141413d222c315d");
Assert.AreEqual(0, engine.ResultStack.Count);

// Clean
Expand Down Expand Up @@ -127,7 +127,7 @@ public void System_ExecutionEngine_GetScriptContainer()
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteArray));
Assert.AreEqual(engine.ResultStack.Pop().GetByteArray().ToHexString(),
@"5b2266613434383036313834373332636138613136363037356430306133643861326531353462333337313333333138303336356561643732373561663132666264222c362c342c2266666666666666666666666666666666666666666666666666666666666666666666666666666666222c332c322c352c223031225d");
@"5b225c75303032426b53415959527a4c4b69685a676464414b50596f754655737a63544d7867445a6572584a3172784c37303d222c362c342c225c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f5c2f383d222c332c322c352c2241513d3d225d");
Assert.AreEqual(0, engine.ResultStack.Count);
}
}
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/Wallets/NEP6/UT_NEP6Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void TestGetKeyWithString()
public void TestToJson()
{
JObject nep6contract = new JObject();
nep6contract["script"] = "2103603f3880eb7aea0ad4500893925e4a42fea48a44ee6f898a10b3c7ce05d2a267ac";
nep6contract["script"] = "IQNgPziA63rqCtRQCJOSXkpC/qSKRO5viYoQs8fOBdKiZ6w=";
JObject parameters = new JObject();
parameters["type"] = 0x00;
parameters["name"] = "Sig";
Expand All @@ -121,7 +121,7 @@ public void TestToJson()
json["isDefault"].ToString().Should().Be("false");
json["lock"].ToString().Should().Be("false");
json["key"].Should().BeNull();
json["contract"]["script"].ToString().Should().Be("\"2103603f3880eb7aea0ad4500893925e4a42fea48a44ee6f898a10b3c7ce05d2a267ac\"");
json["contract"]["script"].ToString().Should().Be("\"IQNgPziA63rqCtRQCJOSXkpC\\/qSKRO5viYoQs8fOBdKiZ6w=\"");
json["extra"].Should().BeNull();

_account.Contract = null;
Expand Down
5 changes: 3 additions & 2 deletions neo.UnitTests/Wallets/NEP6/UT_NEP6Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.Wallets.NEP6;
using System;

namespace Neo.UnitTests.Wallets.NEP6
{
Expand All @@ -19,7 +20,7 @@ public void TestFromNullJson()
[TestMethod]
public void TestFromJson()
{
string json = "{\"script\":\"2103ef891df4c0b7eefb937d21ea0fb88cde8e0d82a7ff11872b5e7047969dafb4eb68747476aa\"," +
string json = "{\"script\":\"IQPviR30wLfu+5N9IeoPuIzejg2Cp/8RhytecEeWna+062h0dHaq\"," +
"\"parameters\":[{\"name\":\"signature\",\"type\":\"Signature\"}],\"deployed\":false}";
JObject @object = JObject.Parse(json);

Expand All @@ -45,7 +46,7 @@ public void TestToJson()

JObject @object = nep6Contract.ToJson();
JString jString = (JString)@object["script"];
jString.Value.Should().Be(nep6Contract.Script.ToHexString());
jString.Value.Should().Be(Convert.ToBase64String(nep6Contract.Script, Base64FormattingOptions.None));

JBoolean jBoolean = (JBoolean)@object["deployed"];
jBoolean.Value.Should().BeFalse();
Expand Down
9 changes: 5 additions & 4 deletions neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Neo.SmartContract.Manifest;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;

namespace Neo.Ledger
Expand Down Expand Up @@ -62,15 +63,15 @@ public JObject ToJson()
{
JObject json = new JObject();
json["hash"] = ScriptHash.ToString();
json["script"] = Script.ToHexString();
json["script"] = Convert.ToBase64String(Script);
json["manifest"] = Manifest.ToJson();
return json;
}

public static ContractState FromJson(JObject json)
{
ContractState contractState = new ContractState();
contractState.Script = json["script"].AsString().HexToBytes();
contractState.Script = Convert.FromBase64String(json["script"].AsString());
contractState.Manifest = ContractManifest.FromJson(json["manifest"]);
return contractState;
}
Expand All @@ -82,8 +83,8 @@ public StackItem ToStackItem()
new StackItem[]
{
new ByteArray(Script),
new Boolean(HasStorage),
new Boolean(Payable),
new VM.Types.Boolean(HasStorage),
new VM.Types.Boolean(Payable),
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public JObject ToJson()
json["valid_until_block"] = ValidUntilBlock;
json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray();
json["cosigners"] = Cosigners.Select(p => p.ToJson()).ToArray();
json["script"] = Script.ToHexString();
json["script"] = Convert.ToBase64String(Script);
json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray();
return json;
}
Expand All @@ -199,7 +199,7 @@ public static Transaction FromJson(JObject json)
tx.ValidUntilBlock = uint.Parse(json["valid_until_block"].AsString());
tx.Attributes = ((JArray)json["attributes"]).Select(p => TransactionAttribute.FromJson(p)).ToArray();
tx.Cosigners = ((JArray)json["cosigners"]).Select(p => Cosigner.FromJson(p)).ToArray();
tx.Script = json["script"].AsString().HexToBytes();
tx.Script = Convert.FromBase64String(json["script"].AsString());
tx.Witnesses = ((JArray)json["witnesses"]).Select(p => Witness.FromJson(p)).ToArray();
return tx;
}
Expand Down
4 changes: 2 additions & 2 deletions neo/Network/P2P/Payloads/TransactionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public JObject ToJson()
{
JObject json = new JObject();
json["usage"] = Usage;
json["data"] = Data.ToHexString();
json["data"] = Convert.ToBase64String(Data);
return json;
}

public static TransactionAttribute FromJson(JObject json)
{
TransactionAttribute transactionAttribute = new TransactionAttribute();
transactionAttribute.Usage = (TransactionAttributeUsage)(byte.Parse(json["usage"].AsString()));
transactionAttribute.Data = json["data"].AsString().HexToBytes();
transactionAttribute.Data = Convert.FromBase64String(json["data"].AsString());
return transactionAttribute;
}
}
Expand Down
10 changes: 5 additions & 5 deletions neo/Network/P2P/Payloads/Witness.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Neo.IO;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
Expand Down Expand Up @@ -44,16 +44,16 @@ void ISerializable.Serialize(BinaryWriter writer)
public JObject ToJson()
{
JObject json = new JObject();
json["invocation"] = InvocationScript.ToHexString();
json["verification"] = VerificationScript.ToHexString();
json["invocation"] = Convert.ToBase64String(InvocationScript);
json["verification"] = Convert.ToBase64String(VerificationScript);
return json;
}

public static Witness FromJson(JObject json)
{
Witness witness = new Witness();
witness.InvocationScript = json["invocation"].AsString().HexToBytes();
witness.VerificationScript = json["verification"].AsString().HexToBytes();
witness.InvocationScript = Convert.FromBase64String(json["invocation"].AsString());
witness.VerificationScript = Convert.FromBase64String(json["verification"].AsString());
return witness;
}
}
Expand Down
2 changes: 1 addition & 1 deletion neo/Network/RPC/Models/RpcNep5Balances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RpcNep5Balance
public JObject ToJson()
{
JObject json = new JObject();
json["asset_hash"] = AssetHash.ToArray().ToHexString();
json["asset_hash"] = AssetHash.ToString();
json["amount"] = Amount.ToString();
json["last_updated_block"] = LastUpdatedBlock.ToString();
return json;
Expand Down
4 changes: 2 additions & 2 deletions neo/SmartContract/ContractParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ContractParameter FromJson(JObject json)
{
case ContractParameterType.Signature:
case ContractParameterType.ByteArray:
parameter.Value = json["value"].AsString().HexToBytes();
parameter.Value = Convert.FromBase64String(json["value"].AsString());
break;
case ContractParameterType.Boolean:
parameter.Value = json["value"].AsBoolean();
Expand Down Expand Up @@ -147,7 +147,7 @@ private static JObject ToJson(ContractParameter parameter, HashSet<ContractParam
{
case ContractParameterType.Signature:
case ContractParameterType.ByteArray:
json["value"] = ((byte[])parameter.Value).ToHexString();
json["value"] = Convert.ToBase64String((byte[])parameter.Value);
break;
case ContractParameterType.Boolean:
json["value"] = (bool)parameter.Value;
Expand Down
12 changes: 6 additions & 6 deletions neo/SmartContract/ContractParametersContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public static ContextItem FromJson(JObject json)
{
return new ContextItem
{
Script = json["script"]?.AsString().HexToBytes(),
Script = Convert.FromBase64String(json["script"]?.AsString()),
Parameters = ((JArray)json["parameters"]).Select(p => ContractParameter.FromJson(p)).ToArray(),
Signatures = json["signatures"]?.Properties.Select(p => new
{
PublicKey = ECPoint.Parse(p.Key, ECCurve.Secp256r1),
Signature = p.Value.AsString().HexToBytes()
Signature = Convert.FromBase64String(p.Value.AsString())
}).ToDictionary(p => p.PublicKey, p => p.Signature)
};
}
Expand All @@ -47,13 +47,13 @@ public JObject ToJson()
{
JObject json = new JObject();
if (Script != null)
json["script"] = Script.ToHexString();
json["script"] = Convert.ToBase64String(Script);
json["parameters"] = new JArray(Parameters.Select(p => p.ToJson()));
if (Signatures != null)
{
json["signatures"] = new JObject();
foreach (var signature in Signatures)
json["signatures"][signature.Key.ToString()] = signature.Value.ToHexString();
json["signatures"][signature.Key.ToString()] = Convert.ToBase64String(signature.Value);
}
return json;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public static ContractParametersContext FromJson(JObject json)
if (!typeof(IVerifiable).IsAssignableFrom(type)) throw new FormatException();

var verifiable = (IVerifiable)Activator.CreateInstance(type);
using (MemoryStream ms = new MemoryStream(json["hex"].AsString().HexToBytes(), false))
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(json["hex"].AsString()), false))
using (BinaryReader reader = new BinaryReader(ms, Encoding.UTF8))
{
verifiable.DeserializeUnsigned(reader);
Expand Down Expand Up @@ -284,7 +284,7 @@ public JObject ToJson()
{
Verifiable.SerializeUnsigned(writer);
writer.Flush();
json["hex"] = ms.ToArray().ToHexString();
json["hex"] = Convert.ToBase64String(ms.ToArray());
}
json["items"] = new JObject();
foreach (var item in ContextItems)
Expand Down
Loading

0 comments on commit 1dc262f

Please sign in to comment.