Skip to content

Commit

Permalink
Merge pull request #14 from neo-project/master
Browse files Browse the repository at this point in the history
Merge from master
  • Loading branch information
Qiao-Jin authored Mar 23, 2020
2 parents 0e2a04b + d71a470 commit bc2678c
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 85 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
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ partial class ApplicationEngine
[OpCode.CALL_L] = 22000,
[OpCode.CALLA] = 22000,
[OpCode.THROW] = 30,
[OpCode.THROWIF] = 30,
[OpCode.THROWIFNOT] = 30,
[OpCode.ABORT] = 30,
[OpCode.ASSERT] = 30,
[OpCode.RET] = 0,
[OpCode.SYSCALL] = 0,
[OpCode.DEPTH] = 60,
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
2 changes: 1 addition & 1 deletion src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public Transaction MakeTransaction(TransferOutput[] outputs, UInt160 from = null
foreach (var (account, value) in balances_used)
{
sb.EmitAppCall(output.AssetId, "transfer", account, output.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
}
}
if (assetId.Equals(NativeContract.GAS.Hash))
Expand Down
2 changes: 1 addition & 1 deletion src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.1" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00209" />
<PackageReference Include="Neo.VM" Version="3.0.0-CI00210" />
</ItemGroup>

</Project>
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void FeeIsSignatureContract_TestScope_Global()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -417,7 +417,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -511,7 +511,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -606,7 +606,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -666,7 +666,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -763,7 +763,7 @@ public void FeeIsSignatureContract_TestScope_NoScopeFAULT()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

Expand Down Expand Up @@ -1023,7 +1023,7 @@ public void FeeIsSignatureContract_TestScope_Global_Default()
// self-transfer of 1e-8 GAS
System.Numerics.BigInteger value = (new BigDecimal(1, 8)).Value;
sb.EmitAppCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value);
sb.Emit(OpCode.THROWIFNOT);
sb.Emit(OpCode.ASSERT);
script = sb.ToArray();
}

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 bc2678c

Please sign in to comment.