From 87015e4b7003b80227fa3e41b258687ae76ededf Mon Sep 17 00:00:00 2001 From: erikzhang Date: Sat, 30 Nov 2019 11:44:34 +0800 Subject: [PATCH 1/2] Unify the BigInteger serialization standard with NeoVM --- src/neo/Helper.cs | 5 +++++ src/neo/SmartContract/Native/Tokens/GasToken.cs | 2 +- src/neo/SmartContract/Native/Tokens/NeoToken.cs | 4 ++-- src/neo/SmartContract/Native/Tokens/Nep5Token.cs | 6 +++--- .../SmartContract/Native/Tokens/UT_GasToken.cs | 2 +- .../SmartContract/Native/Tokens/UT_Nep5Token.cs | 3 +-- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/neo/Helper.cs b/src/neo/Helper.cs index 8fa7005f5e..67fe98bc82 100644 --- a/src/neo/Helper.cs +++ b/src/neo/Helper.cs @@ -177,6 +177,11 @@ internal static bool TestBit(this BigInteger i, int index) return (i & (BigInteger.One << index)) > BigInteger.Zero; } + public static byte[] ToByteArrayStandard(this BigInteger i) + { + if (i.IsZero) return Array.Empty(); + return i.ToByteArray(); + } public static string ToHexString(this IEnumerable value) { diff --git a/src/neo/SmartContract/Native/Tokens/GasToken.cs b/src/neo/SmartContract/Native/Tokens/GasToken.cs index be46335940..daa57b6747 100644 --- a/src/neo/SmartContract/Native/Tokens/GasToken.cs +++ b/src/neo/SmartContract/Native/Tokens/GasToken.cs @@ -47,7 +47,7 @@ protected override bool OnPersist(ApplicationEngine engine) StorageKey key = CreateStorageKey(Prefix_SystemFeeAmount, BitConverter.GetBytes(engine.Snapshot.PersistingBlock.Index)); engine.Snapshot.Storages.Add(key, new StorageItem { - Value = sys_fee.ToByteArray(), + Value = sys_fee.ToByteArrayStandard(), IsConstant = true }); return true; diff --git a/src/neo/SmartContract/Native/Tokens/NeoToken.cs b/src/neo/SmartContract/Native/Tokens/NeoToken.cs index e0023b3841..ec60e99ac8 100644 --- a/src/neo/SmartContract/Native/Tokens/NeoToken.cs +++ b/src/neo/SmartContract/Native/Tokens/NeoToken.cs @@ -292,7 +292,7 @@ public static ValidatorState FromByteArray(byte[] data) public byte[] ToByteArray() { - return Votes.ToByteArray(); + return Votes.ToByteArrayStandard(); } } @@ -322,7 +322,7 @@ public byte[] ToByteArray() { w.WriteVarInt(Votes.Length); foreach (BigInteger vote in Votes) - w.WriteVarBytes(vote.ToByteArray()); + w.WriteVarBytes(vote.ToByteArrayStandard()); w.Flush(); return ms.ToArray(); } diff --git a/src/neo/SmartContract/Native/Tokens/Nep5Token.cs b/src/neo/SmartContract/Native/Tokens/Nep5Token.cs index 5728c2d8be..71dfdac9bc 100644 --- a/src/neo/SmartContract/Native/Tokens/Nep5Token.cs +++ b/src/neo/SmartContract/Native/Tokens/Nep5Token.cs @@ -80,11 +80,11 @@ internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, storage.Value = state.ToByteArray(); storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem { - Value = BigInteger.Zero.ToByteArray() + Value = BigInteger.Zero.ToByteArrayStandard() }); BigInteger totalSupply = new BigInteger(storage.Value); totalSupply += amount; - storage.Value = totalSupply.ToByteArray(); + storage.Value = totalSupply.ToByteArrayStandard(); engine.SendNotification(Hash, new StackItem[] { "Transfer", StackItem.Null, account.ToArray(), amount }); } @@ -110,7 +110,7 @@ internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply)); BigInteger totalSupply = new BigInteger(storage.Value); totalSupply -= amount; - storage.Value = totalSupply.ToByteArray(); + storage.Value = totalSupply.ToByteArrayStandard(); engine.SendNotification(Hash, new StackItem[] { "Transfer", account.ToArray(), StackItem.Null, amount }); } diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs index 96beff3cbd..a045785178 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs @@ -172,7 +172,7 @@ public void TestGetSysFeeAmount2() BigInteger sys_fee = new BigInteger(10); snapshot.Storages.Add(storageKey, new StorageItem { - Value = sys_fee.ToByteArray(), + Value = sys_fee.ToByteArrayStandard(), IsConstant = true }); diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs index a3bc7fa531..5e9dbe9617 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs @@ -51,10 +51,9 @@ public void TestTotalSupplyDecimal() BigInteger totalSupply = 100_000_000; totalSupply *= test.Factor; - byte[] value = totalSupply.ToByteArray(); StorageItem item = new StorageItem { - Value = value + Value = totalSupply.ToByteArrayStandard() }; var key = CreateStorageKey(Prefix_TotalSupply); From 6ddfb7c6d88fb16ff74f477546d635cee2f67af4 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Sat, 30 Nov 2019 15:56:35 +0800 Subject: [PATCH 2/2] inline --- src/neo/Helper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/neo/Helper.cs b/src/neo/Helper.cs index 47c444b327..b20c179cbe 100644 --- a/src/neo/Helper.cs +++ b/src/neo/Helper.cs @@ -197,6 +197,7 @@ internal static bool TestBit(this BigInteger i, int index) return (i & (BigInteger.One << index)) > BigInteger.Zero; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] ToByteArrayStandard(this BigInteger i) { if (i.IsZero) return Array.Empty();