Skip to content

Commit

Permalink
Burn sys_fee (neo-project#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 3f142cb commit 47303bb
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 74 deletions.
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public int Size
}

/// <summary>
/// Distributed to NEO holders.
/// Fee to be burned.
/// </summary>
public long SystemFee
{
Expand Down
30 changes: 0 additions & 30 deletions src/neo/SmartContract/Native/Tokens/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
using Neo.Cryptography.ECC;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Linq;
using System.Numerics;
using VMArray = Neo.VM.Types.Array;

namespace Neo.SmartContract.Native.Tokens
{
Expand All @@ -21,8 +16,6 @@ public sealed class GasToken : Nep5Token<Nep5AccountState>
public override string Symbol => "gas";
public override byte Decimals => 8;

private const byte Prefix_SystemFeeAmount = 15;

internal GasToken()
{
}
Expand All @@ -44,30 +37,7 @@ protected override bool OnPersist(ApplicationEngine engine)
ECPoint[] validators = NEO.GetNextBlockValidators(engine.Snapshot);
UInt160 primary = Contract.CreateSignatureRedeemScript(validators[engine.Snapshot.PersistingBlock.ConsensusData.PrimaryIndex]).ToScriptHash();
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
{
Value = sys_fee.ToByteArrayStandard(),
IsConstant = true
});
return true;
}

[ContractMethod(0_01000000, ContractParameterType.Integer, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "index" }, SafeMethod = true)]
private StackItem GetSysFeeAmount(ApplicationEngine engine, VMArray args)
{
uint index = (uint)args[0].GetBigInteger();
return GetSysFeeAmount(engine.Snapshot, index);
}

public BigInteger GetSysFeeAmount(StoreView snapshot, uint index)
{
if (index == 0) return Blockchain.GenesisBlock.Transactions.Sum(p => p.SystemFee);
StorageKey key = CreateStorageKey(Prefix_SystemFeeAmount, BitConverter.GetBytes(index));
StorageItem storage = snapshot.Storages.TryGet(key);
if (storage is null) return BigInteger.Zero;
return new BigInteger(storage.Value);
}
}
}
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ private BigInteger CalculateBonus(StoreView snapshot, BigInteger value, uint sta
}
amount += (iend - istart) * Blockchain.GenerationAmount[ustart];
}
amount += (GAS.GetSysFeeAmount(snapshot, end - 1) - (start == 0 ? 0 : GAS.GetSysFeeAmount(snapshot, start - 1))) / GAS.Factor;
return value * amount * GAS.Factor / TotalAmount;
}

Expand Down
42 changes: 0 additions & 42 deletions tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Neo.SmartContract.Native;
using Neo.UnitTests.Extensions;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.Linq;
using System.Numerics;
Expand Down Expand Up @@ -138,46 +137,5 @@ public void Check_BadScript()

NativeContract.GAS.Invoke(engine).Should().BeFalse();
}

[TestMethod]
public void TestGetSysFeeAmount1()
{
using (ApplicationEngine engine = NativeContract.GAS.TestCall("getSysFeeAmount", 2u))
{
engine.ResultStack.Peek().GetBigInteger().Should().Be(new BigInteger(0));
engine.ResultStack.Peek().GetType().Should().Be(typeof(Integer));
}

using (ApplicationEngine engine = NativeContract.GAS.TestCall("getSysFeeAmount", 0u))
{
engine.ResultStack.Peek().GetBigInteger().Should().Be(new BigInteger(0));
}
}

[TestMethod]
public void TestGetSysFeeAmount2()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
NativeContract.GAS.GetSysFeeAmount(snapshot, 0).Should().Be(new BigInteger(0));
NativeContract.GAS.GetSysFeeAmount(snapshot, 1).Should().Be(new BigInteger(0));

byte[] key = BitConverter.GetBytes(1);
StorageKey storageKey = new StorageKey
{
Id = NativeContract.GAS.Id,
Key = new byte[sizeof(byte) + key.Length]
};
storageKey.Key[0] = 15;
key.CopyTo(storageKey.Key.AsSpan(1));

BigInteger sys_fee = new BigInteger(10);
snapshot.Storages.Add(storageKey, new StorageItem
{
Value = sys_fee.ToByteArrayStandard(),
IsConstant = true
});

NativeContract.GAS.GetSysFeeAmount(snapshot, 1).Should().Be(sys_fee);
}
}
}

0 comments on commit 47303bb

Please sign in to comment.