Skip to content

Commit

Permalink
reset and fix ReverifyTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Luchuan committed Nov 18, 2019
1 parent 22c87f2 commit 922c7a4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
15 changes: 12 additions & 3 deletions neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,22 @@ public void Check_BalanceOfTransferAndBurn()
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
NativeContract.GAS.Burn(engine, new UInt160(to), BigInteger.MinusOne));

// Burn more than expected

Assert.ThrowsException<InvalidOperationException>(() =>
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000600000000001)));

// Real burn
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(1)).Should().Be(1);

NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(1));

NativeContract.GAS.BalanceOf(snapshot, to).Should().Be(3000599999999999);

// Burn more than expected
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000600000000000)).Should().Be(3000599999999999);
keyCount.Should().Be(snapshot.Storages.GetChangeSet().Count());

// Burn all

NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000599999999999));

(keyCount - 1).Should().Be(snapshot.Storages.GetChangeSet().Count());

Expand Down
1 change: 0 additions & 1 deletion neo/Ledger/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ internal void InvalidateAllTransactions()
{
if (_unsortedTransactions.TryAdd(item.Tx.Hash, item))
{

verifiedSortedTxPool.Add(item);

if (item.LastBroadcastTimestamp < rebroadcastCutOffTime)
Expand Down
19 changes: 3 additions & 16 deletions neo/SmartContract/Native/Tokens/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,12 @@ internal override bool Initialize(ApplicationEngine engine)
protected override bool OnPersist(ApplicationEngine engine)
{
if (!base.OnPersist(engine)) return false;
BigInteger totalNetworkFee = 0;
BigInteger totalSysFee = 0;
foreach (Transaction tx in engine.Snapshot.PersistingBlock.Transactions)
{
BigInteger burned = Burn(engine, tx.Sender, tx.SystemFee + tx.NetworkFee);
if (burned >= tx.NetworkFee)
{
totalNetworkFee += tx.NetworkFee;
totalSysFee += burned - tx.NetworkFee;
}
else
{
totalNetworkFee += burned;
}
}
Burn(engine, tx.Sender, tx.SystemFee + tx.NetworkFee);
ECPoint[] validators = NEO.GetNextBlockValidators(engine.Snapshot);
UInt160 primary = Contract.CreateSignatureRedeemScript(validators[engine.Snapshot.PersistingBlock.ConsensusData.PrimaryIndex]).ToScriptHash();
Mint(engine, primary, totalNetworkFee);
BigInteger sys_fee = GetSysFeeAmount(engine.Snapshot, engine.Snapshot.PersistingBlock.Index - 1) + totalSysFee;
Mint(engine, primary, engine.Snapshot.PersistingBlock.Transactions.Sum(p => p.NetworkFee));
BigInteger sys_fee = GetSysFeeAmount(engine.Snapshot, engine.Snapshot.PersistingBlock.Index - 1) + engine.Snapshot.PersistingBlock.Transactions.Sum(p => p.SystemFee);
StorageKey key = CreateStorageKey(Prefix_SystemFeeAmount, BitConverter.GetBytes(engine.Snapshot.PersistingBlock.Index));
engine.Snapshot.Storages.Add(key, new StorageItem
{
Expand Down
17 changes: 3 additions & 14 deletions neo/SmartContract/Native/Tokens/Nep5Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,15 @@ internal protected virtual void Mint(ApplicationEngine engine, UInt160 account,
engine.SendNotification(Hash, new StackItem[] { "Transfer", StackItem.Null, account.ToArray(), amount });
}

/// <summary>
/// Burn token, when the account balance less than amount, it will burn all the balance of the account
/// </summary>
/// <param name="engine">The application engine</param>
/// <param name="account">The burning account</param>
/// <param name="amount">The amount to be burned</param>
/// <returns>The actual amount of burned</returns>
internal protected virtual BigInteger Burn(ApplicationEngine engine, UInt160 account, BigInteger amount)
internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount)
{
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (amount.IsZero) return amount;
if (amount.IsZero) return;
StorageKey key = CreateAccountKey(account);
StorageItem storage = engine.Snapshot.Storages.GetAndChange(key);
TState state = new TState();
state.FromByteArray(storage.Value);
if (state.Balance < amount)
{
amount = state.Balance;
}
if (state.Balance < amount) throw new InvalidOperationException();
OnBalanceChanging(engine, account, state, -amount);
if (state.Balance == amount)
{
Expand All @@ -121,7 +111,6 @@ internal protected virtual BigInteger Burn(ApplicationEngine engine, UInt160 acc
totalSupply -= amount;
storage.Value = totalSupply.ToByteArray();
engine.SendNotification(Hash, new StackItem[] { "Transfer", account.ToArray(), StackItem.Null, amount });
return amount;
}

[ContractMethod(0, ContractParameterType.String, Name = "name", SafeMethod = true)]
Expand Down

0 comments on commit 922c7a4

Please sign in to comment.