From 2fee69f26f75b3dbbf5ea9538f442736b857adf2 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 19 Nov 2020 18:40:36 +0300 Subject: [PATCH] core: add missing onPersist and postPersist methods to natives Although not every contract is persisted (see https://github.com/neo-project/neo/blob/master/src/neo/Ledger/Blockchain.cs#L94) we have to add `onPersist` and `postPersist` methods to every native contract in order to display them in manifest for users and follow C# behaviour. In C# there are `onPersist` and `postPersist` methods in base native contract class, see https://github.com/neo-project/neo/blob/master/src/neo/SmartContract/Native/NativeContract.cs#L141 and https://github.com/neo-project/neo/blob/master/src/neo/SmartContract/Native/NativeContract.cs#L148 --- pkg/core/native/contract.go | 7 +++++++ pkg/core/native/designate.go | 8 ++++++++ pkg/core/native/native_gas.go | 2 +- pkg/core/native/native_neo.go | 2 +- pkg/core/native/native_nep17.go | 10 +--------- pkg/core/native/oracle.go | 4 ++++ 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/core/native/contract.go b/pkg/core/native/contract.go index b34c2578c..007ff8a1e 100644 --- a/pkg/core/native/contract.go +++ b/pkg/core/native/contract.go @@ -133,3 +133,10 @@ func postPersistBase(ic *interop.Context) error { } return nil } + +func onPersistBase(ic *interop.Context) error { + if ic.Trigger != trigger.OnPersist { + return errors.New("onPersist must be trigered by system") + } + return nil +} diff --git a/pkg/core/native/designate.go b/pkg/core/native/designate.go index 1f426ec5c..5eeb2e15a 100644 --- a/pkg/core/native/designate.go +++ b/pkg/core/native/designate.go @@ -91,6 +91,14 @@ func newDesignate(p2pSigExtensionsEnabled bool) *Designate { md = newMethodAndPrice(nameMethod(designateName), 0, smartcontract.NoneFlag) s.AddMethod(md, desc, true) + desc = newDescriptor("onPersist", smartcontract.VoidType) + md = newMethodAndPrice(getOnPersistWrapper(onPersistBase), 0, smartcontract.AllowModifyStates) + s.AddMethod(md, desc, false) + + desc = newDescriptor("postPersist", smartcontract.VoidType) + md = newMethodAndPrice(getOnPersistWrapper(postPersistBase), 0, smartcontract.AllowModifyStates) + s.AddMethod(md, desc, false) + return s } diff --git a/pkg/core/native/native_gas.go b/pkg/core/native/native_gas.go index 3a7dde827..867e8aa76 100644 --- a/pkg/core/native/native_gas.go +++ b/pkg/core/native/native_gas.go @@ -31,7 +31,7 @@ func newGAS() *GAS { nep17.symbol = "gas" nep17.decimals = 8 nep17.factor = GASFactor - nep17.onPersist = chainOnPersist(nep17.OnPersist, g.OnPersist) + nep17.onPersist = chainOnPersist(onPersistBase, g.OnPersist) nep17.incBalance = g.increaseBalance nep17.ContractID = gasContractID diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 76004775b..45bd97f8b 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -97,7 +97,7 @@ func newNEO() *NEO { nep17.symbol = "neo" nep17.decimals = 0 nep17.factor = 1 - nep17.onPersist = chainOnPersist(nep17.OnPersist, n.OnPersist) + nep17.onPersist = chainOnPersist(onPersistBase, n.OnPersist) nep17.postPersist = chainOnPersist(nep17.postPersist, n.PostPersist) nep17.incBalance = n.increaseBalance nep17.ContractID = neoContractID diff --git a/pkg/core/native/native_nep17.go b/pkg/core/native/native_nep17.go index 84216e81c..5ebf99a1e 100644 --- a/pkg/core/native/native_nep17.go +++ b/pkg/core/native/native_nep17.go @@ -13,7 +13,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" - "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -85,7 +84,7 @@ func newNEP17Native(name string) *nep17TokenNative { n.AddMethod(md, desc, false) desc = newDescriptor("onPersist", smartcontract.VoidType) - md = newMethodAndPrice(getOnPersistWrapper(n.OnPersist), 0, smartcontract.AllowModifyStates) + md = newMethodAndPrice(getOnPersistWrapper(onPersistBase), 0, smartcontract.AllowModifyStates) n.AddMethod(md, desc, false) desc = newDescriptor("postPersist", smartcontract.VoidType) @@ -287,13 +286,6 @@ func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount } } -func (c *nep17TokenNative) OnPersist(ic *interop.Context) error { - if ic.Trigger != trigger.OnPersist { - return errors.New("onPersist must be triggerred by system") - } - return nil -} - func newDescriptor(name string, ret smartcontract.ParamType, ps ...manifest.Parameter) *manifest.Method { return &manifest.Method{ Name: name, diff --git a/pkg/core/native/oracle.go b/pkg/core/native/oracle.go index d94f6a693..d90f8d508 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -137,6 +137,10 @@ func newOracle() *Oracle { md = newMethodAndPrice(getOnPersistWrapper(pp), 0, smartcontract.AllowModifyStates) o.AddMethod(md, desc, false) + desc = newDescriptor("onPersist", smartcontract.VoidType) + md = newMethodAndPrice(getOnPersistWrapper(onPersistBase), 0, smartcontract.AllowModifyStates) + o.AddMethod(md, desc, false) + o.AddEvent("OracleRequest", manifest.NewParameter("Id", smartcontract.IntegerType), manifest.NewParameter("RequestContract", smartcontract.Hash160Type), manifest.NewParameter("Url", smartcontract.StringType),