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
This commit is contained in:
Anna Shaleva 2020-11-19 18:40:36 +03:00
parent 97069a05d5
commit 2fee69f26f
6 changed files with 22 additions and 11 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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),