From 3085710e9b6a0fd726ba14ca421f035112aed883 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 30 Nov 2020 13:05:42 +0300 Subject: [PATCH] native: call `onPayment` only during `transfer` OnPayment method should be called during GAS distribution and NEO transfer. --- pkg/core/native/native_gas.go | 4 ++-- pkg/core/native/native_neo.go | 6 +++--- pkg/core/native/native_nep17.go | 13 +++++++------ pkg/core/native/notary.go | 2 +- pkg/core/native/oracle.go | 19 ++----------------- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 867e8aa76..8965f0236 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -75,7 +75,7 @@ func (g *GAS) Initialize(ic *interop.Context) error { if err != nil { return err } - g.mint(ic, h, big.NewInt(initialGAS*GASFactor)) + g.mint(ic, h, big.NewInt(initialGAS*GASFactor), false) return nil } @@ -94,7 +94,7 @@ func (g *GAS) OnPersist(ic *interop.Context) error { for _, tx := range ic.Block.Transactions { netFee += tx.NetworkFee } - g.mint(ic, primary, big.NewInt(int64(netFee))) + g.mint(ic, primary, big.NewInt(int64(netFee)), false) return nil } diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 5bcc23e06..d211accca 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -189,7 +189,7 @@ func (n *NEO) Initialize(ic *interop.Context) error { if err != nil { return err } - n.mint(ic, h, big.NewInt(NEOTotalSupply)) + n.mint(ic, h, big.NewInt(NEOTotalSupply), false) var index uint32 = 0 value := big.NewInt(5 * GASFactor) @@ -294,7 +294,7 @@ func (n *NEO) PostPersist(ic *interop.Context) error { committeeSize := len(ic.Chain.GetConfig().StandbyCommittee) index := int(ic.Block.Index) % committeeSize 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) { var voterReward = big.NewInt(voterRewardRatio) @@ -410,7 +410,7 @@ func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOB return err } acc.BalanceHeight = ic.Block.Index - n.GAS.mint(ic, h, gen) + n.GAS.mint(ic, h, gen, true) return nil } diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index c8ddc6a72..981d85e03 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -139,9 +139,10 @@ func addrToStackItem(u *util.Uint160) stackitem.Item { 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) - if to == nil { + if to == nil || !callOnPayment { return } 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 } @@ -244,12 +245,12 @@ func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) 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 { return } 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) { @@ -257,7 +258,7 @@ func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big return } 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) { diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index 9282c5211..f20010bca 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -136,7 +136,7 @@ func (n *Notary) OnPersist(ic *interop.Context) error { } singleReward := calculateNotaryReward(nFees, len(notaries)) for _, notary := range notaries { - n.GAS.mint(ic, notary.GetScriptHash(), singleReward) + n.GAS.mint(ic, notary.GetScriptHash(), singleReward, false) } return nil } diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index 81ed06f5f..6621881f2 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -121,13 +121,6 @@ func newOracle() *Oracle { md = newMethodAndPrice(o.verify, 100_0000, smartcontract.NoneFlag) 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) desc = newDescriptor("postPersist", smartcontract.VoidType) md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates) @@ -200,7 +193,7 @@ func (o *Oracle) PostPersist(ic *interop.Context) error { } } 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 } @@ -307,7 +300,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string return ErrNotEnoughGas } 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) id := binary.LittleEndian.Uint64(si.Value) + 1 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)) } -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 { for i := range tx.Attributes { if tx.Attributes[i].Type == transaction.OracleResponseT {