From a442e1530d7a24643a6be7657fa901fe42b85340 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 5 Feb 2021 16:09:51 +0300 Subject: [PATCH 1/2] native: rename onPayment into onNEP17Payment Follow neo-project/neo#2287. --- cli/testdata/verify.go | 2 +- cli/testdata/verify.manifest.json | 2 +- pkg/core/interop_system_test.go | 2 +- pkg/core/native/native_nep17.go | 2 +- pkg/core/native/notary.go | 2 +- pkg/smartcontract/manifest/manifest.go | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cli/testdata/verify.go b/cli/testdata/verify.go index 6a3ed7b6a..4abc927d5 100644 --- a/cli/testdata/verify.go +++ b/cli/testdata/verify.go @@ -4,5 +4,5 @@ func Verify() bool { return true } -func OnPayment(from []byte, amount int, data interface{}) { +func OnNEP17Payment(from []byte, amount int, data interface{}) { } diff --git a/cli/testdata/verify.manifest.json b/cli/testdata/verify.manifest.json index 103a744a8..47bf7b91a 100755 --- a/cli/testdata/verify.manifest.json +++ b/cli/testdata/verify.manifest.json @@ -1 +1 @@ -{"name":"verify","abi":{"methods":[{"name":"verify","offset":0,"parameters":[],"returntype":"Boolean","safe":false},{"name":"onPayment","offset":5,"parameters":[{"name":"from","type":"ByteArray"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"verify","abi":{"methods":[{"name":"verify","offset":0,"parameters":[],"returntype":"Boolean","safe":false},{"name":"onNEP17Payment","offset":5,"parameters":[{"name":"from","type":"ByteArray"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 2ada38325..574a7d3ad 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -456,7 +456,7 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) { ReturnType: smartcontract.VoidType, }, { - Name: "onPayment", + Name: manifest.MethodOnNEP17Payment, Offset: onPaymentOff, Parameters: []manifest.Parameter{ manifest.NewParameter("from", smartcontract.Hash160Type), diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 7ba55dd6c..ca092bd8b 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -143,7 +143,7 @@ func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint stackitem.NewBigInteger(amount), data, } - if err := contract.CallFromNative(ic, c.Hash, cs, manifest.MethodOnPayment, args, false); err != nil { + if err := contract.CallFromNative(ic, c.Hash, cs, manifest.MethodOnNEP17Payment, args, false); err != nil { panic(err) } } diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index 0a27a5dec..d891acd05 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -55,7 +55,7 @@ var maxNotValidBeforeDeltaKey = []byte{10} func newNotary() *Notary { n := &Notary{ContractMD: *interop.NewContractMD(nativenames.Notary, notaryContractID)} - desc := newDescriptor("onPayment", smartcontract.VoidType, + desc := newDescriptor("onNEP17Payment", smartcontract.VoidType, manifest.NewParameter("from", smartcontract.Hash160Type), manifest.NewParameter("amount", smartcontract.IntegerType), manifest.NewParameter("data", smartcontract.AnyType)) diff --git a/pkg/smartcontract/manifest/manifest.go b/pkg/smartcontract/manifest/manifest.go index e65175e85..dc9a07640 100644 --- a/pkg/smartcontract/manifest/manifest.go +++ b/pkg/smartcontract/manifest/manifest.go @@ -21,8 +21,8 @@ const ( // MethodVerify is a name for default verification method. MethodVerify = "verify" - // MethodOnPayment is name of the method which is called when contract receives funds. - MethodOnPayment = "onPayment" + // MethodOnNEP17Payment is name of the method which is called when contract receives NEP-17 tokens. + MethodOnNEP17Payment = "onNEP17Payment" // NEP10StandardName represents the name of NEP10 smartcontract standard. NEP10StandardName = "NEP-10" From cd9b34416e2cf13b9d570f11653d3b3626f7b153 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 5 Feb 2021 16:18:08 +0300 Subject: [PATCH 2/2] native: call onNEP11Transfer for NEP-11 transfers See neo-project/neo#2287. --- pkg/core/interop_system_test.go | 21 +++++++++++++++++++-- pkg/core/native/nonfungible.go | 21 +++++++++++++++++++++ pkg/core/native_name_service_test.go | 10 ++++++++++ pkg/smartcontract/manifest/manifest.go | 3 +++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 574a7d3ad..27d8247ba 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -340,13 +340,20 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) { emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext) emit.Syscall(w.BinWriter, interopnames.SystemStorageGet) emit.Opcodes(w.BinWriter, opcode.RET) - onPaymentOff := w.Len() + onNEP17PaymentOff := w.Len() emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetCallingScriptHash) emit.Int(w.BinWriter, 4) emit.Opcodes(w.BinWriter, opcode.PACK) emit.String(w.BinWriter, "LastPayment") emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify) emit.Opcodes(w.BinWriter, opcode.RET) + onNEP11PaymentOff := w.Len() + emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetCallingScriptHash) + emit.Int(w.BinWriter, 4) + emit.Opcodes(w.BinWriter, opcode.PACK) + emit.String(w.BinWriter, "LostPayment") + emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify) + emit.Opcodes(w.BinWriter, opcode.RET) update3Off := w.Len() emit.Int(w.BinWriter, 3) emit.Opcodes(w.BinWriter, opcode.JMP, 2+1) @@ -455,9 +462,19 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) { }, ReturnType: smartcontract.VoidType, }, + { + Name: manifest.MethodOnNEP11Payment, + Offset: onNEP11PaymentOff, + Parameters: []manifest.Parameter{ + manifest.NewParameter("from", smartcontract.Hash160Type), + manifest.NewParameter("amount", smartcontract.IntegerType), + manifest.NewParameter("tokenid", smartcontract.ByteArrayType), + }, + ReturnType: smartcontract.VoidType, + }, { Name: manifest.MethodOnNEP17Payment, - Offset: onPaymentOff, + Offset: onNEP17PaymentOff, Parameters: []manifest.Parameter{ manifest.NewParameter("from", smartcontract.Hash160Type), manifest.NewParameter("amount", smartcontract.IntegerType), diff --git a/pkg/core/native/nonfungible.go b/pkg/core/native/nonfungible.go index 36826506f..902287d92 100644 --- a/pkg/core/native/nonfungible.go +++ b/pkg/core/native/nonfungible.go @@ -8,6 +8,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/dao" "github.com/nspcc-dev/neo-go/pkg/core/interop" + "github.com/nspcc-dev/neo-go/pkg/core/interop/contract" "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage" "github.com/nspcc-dev/neo-go/pkg/core/state" @@ -277,6 +278,26 @@ func (n *nonfungible) postTransfer(ic *interop.Context, from, to *util.Uint160, }), } ic.Notifications = append(ic.Notifications, ne) + if to == nil { + return + } + cs, err := ic.GetContract(*to) + if err != nil { + return + } + + fromArg := stackitem.Item(stackitem.Null{}) + if from != nil { + fromArg = stackitem.NewByteArray((*from).BytesBE()) + } + args := []stackitem.Item{ + fromArg, + stackitem.NewBigInteger(intOne), + stackitem.NewByteArray(tokenID), + } + if err := contract.CallFromNative(ic, n.Hash, cs, manifest.MethodOnNEP11Payment, args, false); err != nil { + panic(err) + } } func (n *nonfungible) burn(ic *interop.Context, tokenID []byte) { diff --git a/pkg/core/native_name_service_test.go b/pkg/core/native_name_service_test.go index 2042ee55a..1ffd822e4 100644 --- a/pkg/core/native_name_service_test.go +++ b/pkg/core/native_name_service_test.go @@ -287,6 +287,16 @@ func TestTransfer(t *testing.T) { testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "totalSupply", 1) testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "ownerOf", to.Contract.ScriptHash().BytesBE(), []byte("neo.com")) + cs, cs2 := getTestContractState(bc) // cs2 doesn't have OnNEP11Transfer + require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs)) + require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs2)) + testNameServiceInvokeAux(t, bc, defaultRegisterSysfee, to, "transfer", + nil, cs2.Hash.BytesBE(), []byte("neo.com")) + testNameServiceInvokeAux(t, bc, defaultRegisterSysfee, to, "transfer", + true, cs.Hash.BytesBE(), []byte("neo.com")) + testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "totalSupply", 1) + testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "ownerOf", + cs.Hash.BytesBE(), []byte("neo.com")) } func TestTokensOf(t *testing.T) { diff --git a/pkg/smartcontract/manifest/manifest.go b/pkg/smartcontract/manifest/manifest.go index dc9a07640..b622ab33d 100644 --- a/pkg/smartcontract/manifest/manifest.go +++ b/pkg/smartcontract/manifest/manifest.go @@ -24,6 +24,9 @@ const ( // MethodOnNEP17Payment is name of the method which is called when contract receives NEP-17 tokens. MethodOnNEP17Payment = "onNEP17Payment" + // MethodOnNEP11Payment is the name of the method which is called when contract receives NEP-11 tokens. + MethodOnNEP11Payment = "onNEP11Payment" + // NEP10StandardName represents the name of NEP10 smartcontract standard. NEP10StandardName = "NEP-10" // NEP17StandardName represents the name of NEP17 smartcontract standard.