native: support native method/event removal

Port a part of https://github.com/neo-project/neo/issues/3210.
A part of #3440.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2024-05-17 14:47:19 +03:00
parent 2d4993a837
commit df1ed68d98
2 changed files with 31 additions and 8 deletions

View file

@ -144,6 +144,7 @@ type Method = func(ic *Context, args []stackitem.Item) stackitem.Item
type MethodAndPrice struct { type MethodAndPrice struct {
HFSpecificMethodAndPrice HFSpecificMethodAndPrice
ActiveFrom *config.Hardfork ActiveFrom *config.Hardfork
ActiveTill *config.Hardfork
} }
// HFSpecificMethodAndPrice is a hardfork-specific native contract method descriptor. // HFSpecificMethodAndPrice is a hardfork-specific native contract method descriptor.
@ -160,6 +161,7 @@ type HFSpecificMethodAndPrice struct {
type Event struct { type Event struct {
HFSpecificEvent HFSpecificEvent
ActiveFrom *config.Hardfork ActiveFrom *config.Hardfork
ActiveTill *config.Hardfork
} }
// HFSpecificEvent is a hardfork-specific native contract event descriptor. // HFSpecificEvent is a hardfork-specific native contract event descriptor.
@ -289,7 +291,8 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) {
w := io.NewBufBinWriter() w := io.NewBufBinWriter()
for i := range c.methods { for i := range c.methods {
m := c.methods[i] m := c.methods[i]
if !(m.ActiveFrom == nil || (hf != config.HFDefault && (*m.ActiveFrom).Cmp(hf) >= 0)) { if !(m.ActiveFrom == nil || (hf != config.HFDefault && (*m.ActiveFrom).Cmp(hf) >= 0)) ||
(m.ActiveTill != nil && (*m.ActiveTill).Cmp(hf) <= 0) {
continue continue
} }
@ -311,7 +314,8 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) {
} }
for i := range c.events { for i := range c.events {
e := c.events[i] e := c.events[i]
if !(e.ActiveFrom == nil || (hf != config.HFDefault && (*e.ActiveFrom).Cmp(hf) >= 0)) { if !(e.ActiveFrom == nil || (hf != config.HFDefault && (*e.ActiveFrom).Cmp(hf) >= 0)) ||
(e.ActiveTill != nil && (*e.ActiveTill).Cmp(hf) <= 0) {
continue continue
} }
@ -369,6 +373,9 @@ func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method) {
if md.ActiveFrom != nil { if md.ActiveFrom != nil {
c.ActiveHFs[*md.ActiveFrom] = struct{}{} c.ActiveHFs[*md.ActiveFrom] = struct{}{}
} }
if md.ActiveTill != nil {
c.ActiveHFs[*md.ActiveTill] = struct{}{}
}
} }
// GetMethodByOffset returns method with the provided offset. // GetMethodByOffset returns method with the provided offset.
@ -410,6 +417,9 @@ func (c *ContractMD) AddEvent(md Event) {
if md.ActiveFrom != nil { if md.ActiveFrom != nil {
c.ActiveHFs[*md.ActiveFrom] = struct{}{} c.ActiveHFs[*md.ActiveFrom] = struct{}{}
} }
if md.ActiveTill != nil {
c.ActiveHFs[*md.ActiveTill] = struct{}{}
}
} }
// Sort sorts interop functions by id. // Sort sorts interop functions by id.

View file

@ -323,7 +323,10 @@ func newDescriptor(name string, ret smartcontract.ParamType, ps ...manifest.Para
} }
} }
func newMethodAndPrice(f interop.Method, cpuFee int64, flags callflag.CallFlag, activeFrom ...config.Hardfork) *interop.MethodAndPrice { // newMethodAndPrice builds method with the provided descriptor and ActiveFrom/ActiveTill hardfork
// values consequently specified via activations. [config.HFDefault] specfied as ActiveFrom is treated
// as active starting from the genesis block.
func newMethodAndPrice(f interop.Method, cpuFee int64, flags callflag.CallFlag, activations ...config.Hardfork) *interop.MethodAndPrice {
md := &interop.MethodAndPrice{ md := &interop.MethodAndPrice{
HFSpecificMethodAndPrice: interop.HFSpecificMethodAndPrice{ HFSpecificMethodAndPrice: interop.HFSpecificMethodAndPrice{
Func: f, Func: f,
@ -331,8 +334,13 @@ func newMethodAndPrice(f interop.Method, cpuFee int64, flags callflag.CallFlag,
RequiredFlags: flags, RequiredFlags: flags,
}, },
} }
if len(activeFrom) != 0 { if len(activations) > 0 {
md.ActiveFrom = &activeFrom[0] if activations[0] != config.HFDefault {
md.ActiveFrom = &activations[0]
}
}
if len(activations) > 1 {
md.ActiveTill = &activations[1]
} }
return md return md
} }
@ -347,14 +355,19 @@ func newEventDescriptor(name string, ps ...manifest.Parameter) *manifest.Event {
} }
} }
func newEvent(desc *manifest.Event, activeFrom ...config.Hardfork) interop.Event { // newEvent builds event with the provided descriptor and ActiveFrom/ActiveTill hardfork
// values consequently specified via activations.
func newEvent(desc *manifest.Event, activations ...config.Hardfork) interop.Event {
md := interop.Event{ md := interop.Event{
HFSpecificEvent: interop.HFSpecificEvent{ HFSpecificEvent: interop.HFSpecificEvent{
MD: desc, MD: desc,
}, },
} }
if len(activeFrom) != 0 { if len(activations) > 0 {
md.ActiveFrom = &activeFrom[0] md.ActiveFrom = &activations[0]
}
if len(activations) > 1 {
md.ActiveTill = &activations[1]
} }
return md return md
} }