Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify the BigInteger serialization standard with NeoVM #1307

Merged
merged 3 commits into from
Nov 30, 2019
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
7 changes: 7 additions & 0 deletions src/neo/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ 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)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
if (i.IsZero) return Array.Empty<byte>();
return i.ToByteArray();
}

public static string ToHexString(this byte[] value)
{
StringBuilder sb = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/Tokens/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public static ValidatorState FromByteArray(byte[] data)

public byte[] ToByteArray()
{
return Votes.ToByteArray();
return Votes.ToByteArrayStandard();
}
}

Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/Tokens/Nep5Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

Expand All @@ -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 });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down