Merge pull request #1574 from nspcc-dev/onpayment

native: call `onPayment` only during `transfer`
This commit is contained in:
Roman Khimov 2020-12-01 11:07:20 +03:00 committed by GitHub
commit 0cf8dae1c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 29 deletions

View file

@ -75,7 +75,7 @@ func (g *GAS) Initialize(ic *interop.Context) error {
if err != nil { if err != nil {
return err return err
} }
g.mint(ic, h, big.NewInt(initialGAS*GASFactor)) g.mint(ic, h, big.NewInt(initialGAS*GASFactor), false)
return nil return nil
} }
@ -94,7 +94,7 @@ func (g *GAS) OnPersist(ic *interop.Context) error {
for _, tx := range ic.Block.Transactions { for _, tx := range ic.Block.Transactions {
netFee += tx.NetworkFee netFee += tx.NetworkFee
} }
g.mint(ic, primary, big.NewInt(int64(netFee))) g.mint(ic, primary, big.NewInt(int64(netFee)), false)
return nil return nil
} }

View file

@ -189,7 +189,7 @@ func (n *NEO) Initialize(ic *interop.Context) error {
if err != nil { if err != nil {
return err return err
} }
n.mint(ic, h, big.NewInt(NEOTotalSupply)) n.mint(ic, h, big.NewInt(NEOTotalSupply), false)
var index uint32 = 0 var index uint32 = 0
value := big.NewInt(5 * GASFactor) value := big.NewInt(5 * GASFactor)
@ -294,7 +294,7 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
committeeSize := len(ic.Chain.GetConfig().StandbyCommittee) committeeSize := len(ic.Chain.GetConfig().StandbyCommittee)
index := int(ic.Block.Index) % committeeSize index := int(ic.Block.Index) % committeeSize
committeeReward := new(big.Int).Mul(gas, big.NewInt(committeeRewardRatio)) committeeReward := new(big.Int).Mul(gas, big.NewInt(committeeRewardRatio))
n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big.NewInt(100))) n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big.NewInt(100)), false)
if ShouldUpdateCommittee(ic.Block.Index, ic.Chain) { if ShouldUpdateCommittee(ic.Block.Index, ic.Chain) {
var voterReward = big.NewInt(voterRewardRatio) var voterReward = big.NewInt(voterRewardRatio)
@ -410,7 +410,7 @@ func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOB
return err return err
} }
acc.BalanceHeight = ic.Block.Index acc.BalanceHeight = ic.Block.Index
n.GAS.mint(ic, h, gen) n.GAS.mint(ic, h, gen, true)
return nil return nil
} }

View file

@ -139,9 +139,10 @@ func addrToStackItem(u *util.Uint160) stackitem.Item {
return stackitem.NewByteArray(u.BytesBE()) return stackitem.NewByteArray(u.BytesBE())
} }
func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint160, amount *big.Int, data stackitem.Item) { func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint160, amount *big.Int,
data stackitem.Item, callOnPayment bool) {
c.emitTransfer(ic, from, to, amount) c.emitTransfer(ic, from, to, amount)
if to == nil { if to == nil || !callOnPayment {
return return
} }
cs, err := ic.DAO.GetContractState(*to) cs, err := ic.DAO.GetContractState(*to)
@ -230,7 +231,7 @@ func (c *nep17TokenNative) TransferInternal(ic *interop.Context, from, to util.U
} }
} }
c.postTransfer(ic, &from, &to, amount, data) c.postTransfer(ic, &from, &to, amount, data, true)
return nil return nil
} }
@ -244,12 +245,12 @@ func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item)
return stackitem.NewBigInteger(&balance) return stackitem.NewBigInteger(&balance)
} }
func (c *nep17TokenNative) mint(ic *interop.Context, h util.Uint160, amount *big.Int) { func (c *nep17TokenNative) mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) {
if amount.Sign() == 0 { if amount.Sign() == 0 {
return return
} }
c.addTokens(ic, h, amount) c.addTokens(ic, h, amount)
c.postTransfer(ic, nil, &h, amount, stackitem.Null{}) c.postTransfer(ic, nil, &h, amount, stackitem.Null{}, callOnPayment)
} }
func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big.Int) { func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big.Int) {
@ -257,7 +258,7 @@ func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big
return return
} }
c.addTokens(ic, h, new(big.Int).Neg(amount)) c.addTokens(ic, h, new(big.Int).Neg(amount))
c.postTransfer(ic, &h, nil, amount, stackitem.Null{}) c.postTransfer(ic, &h, nil, amount, stackitem.Null{}, false)
} }
func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount *big.Int) { func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount *big.Int) {

View file

@ -136,7 +136,7 @@ func (n *Notary) OnPersist(ic *interop.Context) error {
} }
singleReward := calculateNotaryReward(nFees, len(notaries)) singleReward := calculateNotaryReward(nFees, len(notaries))
for _, notary := range notaries { for _, notary := range notaries {
n.GAS.mint(ic, notary.GetScriptHash(), singleReward) n.GAS.mint(ic, notary.GetScriptHash(), singleReward, false)
} }
return nil return nil
} }

View file

@ -121,13 +121,6 @@ func newOracle() *Oracle {
md = newMethodAndPrice(o.verify, 100_0000, smartcontract.NoneFlag) md = newMethodAndPrice(o.verify, 100_0000, smartcontract.NoneFlag)
o.AddMethod(md, desc, false) o.AddMethod(md, desc, false)
desc = newDescriptor("onPayment", smartcontract.VoidType,
manifest.NewParameter("from", smartcontract.Hash160Type),
manifest.NewParameter("amount", smartcontract.IntegerType),
manifest.NewParameter("data", smartcontract.AnyType))
md = newMethodAndPrice(o.onPayment, 0, smartcontract.NoneFlag)
o.AddMethod(md, desc, false)
pp := chainOnPersist(postPersistBase, o.PostPersist) pp := chainOnPersist(postPersistBase, o.PostPersist)
desc = newDescriptor("postPersist", smartcontract.VoidType) desc = newDescriptor("postPersist", smartcontract.VoidType)
md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates) md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates)
@ -200,7 +193,7 @@ func (o *Oracle) PostPersist(ic *interop.Context) error {
} }
} }
for i := range reward { for i := range reward {
o.GAS.mint(ic, nodes[i].GetScriptHash(), &reward[i]) o.GAS.mint(ic, nodes[i].GetScriptHash(), &reward[i], false)
} }
return nil return nil
} }
@ -307,7 +300,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
return ErrNotEnoughGas return ErrNotEnoughGas
} }
callingHash := ic.VM.GetCallingScriptHash() callingHash := ic.VM.GetCallingScriptHash()
o.GAS.mint(ic, o.Hash, gas) o.GAS.mint(ic, o.Hash, gas, false)
si := ic.DAO.GetStorageItem(o.ContractID, prefixRequestID) si := ic.DAO.GetStorageItem(o.ContractID, prefixRequestID)
id := binary.LittleEndian.Uint64(si.Value) + 1 id := binary.LittleEndian.Uint64(si.Value) + 1
binary.LittleEndian.PutUint64(si.Value, id) binary.LittleEndian.PutUint64(si.Value, id)
@ -410,14 +403,6 @@ func (o *Oracle) verify(ic *interop.Context, _ []stackitem.Item) stackitem.Item
return stackitem.NewBool(ic.Tx.HasAttribute(transaction.OracleResponseT)) return stackitem.NewBool(ic.Tx.HasAttribute(transaction.OracleResponseT))
} }
func (o *Oracle) onPayment(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
// FIXME when calling native transfer directly, context is not provided.
if h := ic.VM.GetCallingScriptHash(); h != o.Hash && h != o.GAS.Hash {
panic("only GAS can be accepted")
}
return stackitem.Null{}
}
func (o *Oracle) getOriginalTxID(d dao.DAO, tx *transaction.Transaction) util.Uint256 { func (o *Oracle) getOriginalTxID(d dao.DAO, tx *transaction.Transaction) util.Uint256 {
for i := range tx.Attributes { for i := range tx.Attributes {
if tx.Attributes[i].Type == transaction.OracleResponseT { if tx.Attributes[i].Type == transaction.OracleResponseT {