From 922c7a49d6f183e7038efaa7cecb69c598976605 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Mon, 18 Nov 2019 15:30:37 +0800 Subject: [PATCH] reset and fix ReverifyTransactions --- .../Native/Tokens/UT_GasToken.cs | 15 ++++++++++++--- neo/Ledger/MemoryPool.cs | 1 - neo/SmartContract/Native/Tokens/GasToken.cs | 19 +++---------------- neo/SmartContract/Native/Tokens/Nep5Token.cs | 17 +++-------------- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs b/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs index d14285e67e..7e811d3eda 100644 --- a/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs +++ b/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs @@ -104,13 +104,22 @@ public void Check_BalanceOfTransferAndBurn() Assert.ThrowsException(() => NativeContract.GAS.Burn(engine, new UInt160(to), BigInteger.MinusOne)); + // Burn more than expected + + Assert.ThrowsException(() => + 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()); diff --git a/neo/Ledger/MemoryPool.cs b/neo/Ledger/MemoryPool.cs index 65f4054956..78df9b97f6 100644 --- a/neo/Ledger/MemoryPool.cs +++ b/neo/Ledger/MemoryPool.cs @@ -441,7 +441,6 @@ internal void InvalidateAllTransactions() { if (_unsortedTransactions.TryAdd(item.Tx.Hash, item)) { - verifiedSortedTxPool.Add(item); if (item.LastBroadcastTimestamp < rebroadcastCutOffTime) diff --git a/neo/SmartContract/Native/Tokens/GasToken.cs b/neo/SmartContract/Native/Tokens/GasToken.cs index 11dcfee9a9..7ef522dab7 100644 --- a/neo/SmartContract/Native/Tokens/GasToken.cs +++ b/neo/SmartContract/Native/Tokens/GasToken.cs @@ -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 { diff --git a/neo/SmartContract/Native/Tokens/Nep5Token.cs b/neo/SmartContract/Native/Tokens/Nep5Token.cs index 5a506e50e8..a95885a665 100644 --- a/neo/SmartContract/Native/Tokens/Nep5Token.cs +++ b/neo/SmartContract/Native/Tokens/Nep5Token.cs @@ -87,25 +87,15 @@ internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, engine.SendNotification(Hash, new StackItem[] { "Transfer", StackItem.Null, account.ToArray(), amount }); } - /// - /// Burn token, when the account balance less than amount, it will burn all the balance of the account - /// - /// The application engine - /// The burning account - /// The amount to be burned - /// The actual amount of burned - 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) { @@ -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)]