Skip to content

Commit

Permalink
Avoid call onPayment on specific cases (neo-project#2098)
Browse files Browse the repository at this point in the history
* Avoid call onPayment

* dotnet format

* Remove arg from Burn

* change to true

* Fix UT
  • Loading branch information
shargon authored and cloud8little committed Jan 24, 2021
1 parent 07a3a8f commit c528f82
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected override void PostPersist(ApplicationEngine engine)
{
foreach (var (account, gas) in nodes)
{
if (gas.Sign > 0) GAS.Mint(engine, account, gas);
if (gas.Sign > 0) GAS.Mint(engine, account, gas, false);
}
}
}
Expand All @@ -186,7 +186,7 @@ private void Request(ApplicationEngine engine, string url, string filter, string

//Mint gas for the response
engine.AddGas(gasForResponse);
GAS.Mint(engine, Hash, gasForResponse);
GAS.Mint(engine, Hash, gasForResponse, false);

//Increase the request id
StorageItem item_id = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_RequestId));
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/Tokens/GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal GasToken()
internal override void Initialize(ApplicationEngine engine)
{
UInt160 account = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators);
Mint(engine, account, 30_000_000 * Factor);
Mint(engine, account, 30_000_000 * Factor, false);
}

protected override void OnPersist(ApplicationEngine engine)
Expand All @@ -32,7 +32,7 @@ protected override void OnPersist(ApplicationEngine engine)
}
ECPoint[] validators = NEO.GetNextBlockValidators(engine.Snapshot);
UInt160 primary = Contract.CreateSignatureRedeemScript(validators[engine.Snapshot.PersistingBlock.ConsensusData.PrimaryIndex]).ToScriptHash();
Mint(engine, primary, totalNetworkFee);
Mint(engine, primary, totalNetworkFee, false);
}
}
}
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/Tokens/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void DistributeGas(ApplicationEngine engine, UInt160 account, NeoAccount

BigInteger gas = CalculateBonus(engine.Snapshot, state.VoteTo, state.Balance, state.BalanceHeight, engine.Snapshot.PersistingBlock.Index);
state.BalanceHeight = engine.Snapshot.PersistingBlock.Index;
GAS.Mint(engine, account, gas);
GAS.Mint(engine, account, gas, true);
}

private BigInteger CalculateBonus(StoreView snapshot, ECPoint vote, BigInteger value, uint start, uint end)
Expand Down Expand Up @@ -127,7 +127,7 @@ internal override void Initialize(ApplicationEngine engine)
// Initialize economic parameters

engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_GasPerBlock).AddBigEndian(0u), new StorageItem(5 * GAS.Factor));
Mint(engine, Blockchain.GetConsensusAddress(Blockchain.StandbyValidators), TotalAmount);
Mint(engine, Blockchain.GetConsensusAddress(Blockchain.StandbyValidators), TotalAmount, false);
}

protected override void OnPersist(ApplicationEngine engine)
Expand Down Expand Up @@ -157,7 +157,7 @@ protected override void PostPersist(ApplicationEngine engine)
var committee = GetCommitteeFromCache(engine.Snapshot);
var pubkey = committee.ElementAt(index).PublicKey;
var account = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash();
GAS.Mint(engine, account, gasPerBlock * CommitteeRewardRatio / 100);
GAS.Mint(engine, account, gasPerBlock * CommitteeRewardRatio / 100, false);

// Record the cumulative reward of the voters of committee

Expand Down
12 changes: 6 additions & 6 deletions src/neo/SmartContract/Native/Tokens/Nep17Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected Nep17Token()
Manifest.Abi.Events = events.ToArray();
}

internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount)
internal protected virtual void Mint(ApplicationEngine engine, UInt160 account, BigInteger amount, bool callOnPayment)
{
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (amount.IsZero) return;
Expand All @@ -67,7 +67,7 @@ internal protected virtual void Mint(ApplicationEngine engine, UInt160 account,
state.Balance += amount;
storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero));
storage.Add(amount);
PostTransfer(engine, null, account, amount, StackItem.Null);
PostTransfer(engine, null, account, amount, StackItem.Null, callOnPayment);
}

internal protected virtual void Burn(ApplicationEngine engine, UInt160 account, BigInteger amount)
Expand All @@ -85,7 +85,7 @@ internal protected virtual void Burn(ApplicationEngine engine, UInt160 account,
state.Balance -= amount;
storage = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply));
storage.Add(-amount);
PostTransfer(engine, account, null, amount, StackItem.Null);
PostTransfer(engine, account, null, amount, StackItem.Null, false);
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
Expand Down Expand Up @@ -143,15 +143,15 @@ protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160
state_to.Balance += amount;
}
}
PostTransfer(engine, from, to, amount, data);
PostTransfer(engine, from, to, amount, data, true);
return true;
}

protected virtual void OnBalanceChanging(ApplicationEngine engine, UInt160 account, TState state, BigInteger amount)
{
}

private void PostTransfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data)
private void PostTransfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data, bool callOnPayment)
{
// Send notification

Expand All @@ -160,7 +160,7 @@ private void PostTransfer(ApplicationEngine engine, UInt160 from, UInt160 to, Bi

// Check if it's a wallet or smart contract

if (to is null || engine.Snapshot.Contracts.TryGet(to) is null) return;
if (!callOnPayment || to is null || engine.Snapshot.Contracts.TryGet(to) is null) return;

// Call onPayment method (NEP-17)

Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered()
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, senderAccount);
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, long.MaxValue);
NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
NativeContract.GAS.Mint(engine, UInt160.Zero, 70);
NativeContract.GAS.Mint(engine, UInt160.Zero, 70, true);

long txFee = 1;
AddTransactionsWithBalanceVerify(70, txFee, snapshot);
Expand All @@ -243,7 +243,7 @@ public void BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered()

ApplicationEngine applicationEngine = ApplicationEngine.Create(TriggerType.All, block, snapshot, (long)balance);
NativeContract.GAS.Burn(applicationEngine, sender, NativeContract.GAS.BalanceOf(snapshot, sender));
NativeContract.GAS.Mint(applicationEngine, sender, txFee * 30); // Set the balance to meet 30 txs only
NativeContract.GAS.Mint(applicationEngine, sender, txFee * 30, true); // Set the balance to meet 30 txs only

// Persist block and reverify all the txs in mempool, but half of the txs will be discarded
_unit.UpdatePoolForBlockPersisted(block, snapshot);
Expand All @@ -252,7 +252,7 @@ public void BlockPersistAndReverificationWillAbandonTxAsBalanceTransfered()

// Revert the balance
NativeContract.GAS.Burn(applicationEngine, sender, txFee * 30);
NativeContract.GAS.Mint(applicationEngine, sender, balance);
NativeContract.GAS.Mint(applicationEngine, sender, balance, true);
}

private void VerifyTransactionsSortedDescending(IEnumerable<Transaction> transactions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void TestDuplicateOracle()
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
NativeContract.GAS.Mint(engine, UInt160.Zero, 8);
NativeContract.GAS.Mint(engine, UInt160.Zero, 8, false);

// Test
TransactionVerificationContext verificationContext = new TransactionVerificationContext();
Expand All @@ -77,7 +77,7 @@ public void TestTransactionSenderFee()
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
NativeContract.GAS.Mint(engine, UInt160.Zero, 8);
NativeContract.GAS.Mint(engine, UInt160.Zero, 8, true);

TransactionVerificationContext verificationContext = new TransactionVerificationContext();
var tx = CreateTransactionWithFee(1, 2);
Expand Down

0 comments on commit c528f82

Please sign in to comment.