Skip to content

Commit

Permalink
Unify the BigInteger serialization standard with NeoVM (neo-project#1307
Browse files Browse the repository at this point in the history
)
  • Loading branch information
erikzhang authored and Luchuan committed Jan 10, 2020
1 parent 12ec257 commit ae9a5fd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
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)
{
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

0 comments on commit ae9a5fd

Please sign in to comment.