Skip to content

Commit

Permalink
native: support native method/event removal
Browse files Browse the repository at this point in the history
Port a part of neo-project/neo#3210.
A part of #3440.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed May 17, 2024
1 parent 2d4993a commit df1ed68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
14 changes: 12 additions & 2 deletions pkg/core/interop/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type Method = func(ic *Context, args []stackitem.Item) stackitem.Item
type MethodAndPrice struct {
HFSpecificMethodAndPrice
ActiveFrom *config.Hardfork
ActiveTill *config.Hardfork
}

// HFSpecificMethodAndPrice is a hardfork-specific native contract method descriptor.
Expand All @@ -160,6 +161,7 @@ type HFSpecificMethodAndPrice struct {
type Event struct {
HFSpecificEvent
ActiveFrom *config.Hardfork
ActiveTill *config.Hardfork
}

// HFSpecificEvent is a hardfork-specific native contract event descriptor.
Expand Down Expand Up @@ -289,7 +291,8 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) {
w := io.NewBufBinWriter()
for i := range c.methods {
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
}

Expand All @@ -311,7 +314,8 @@ func (c *ContractMD) buildHFSpecificMD(hf config.Hardfork) {
}
for i := range c.events {
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
}

Expand Down Expand Up @@ -369,6 +373,9 @@ func (c *ContractMD) AddMethod(md *MethodAndPrice, desc *manifest.Method) {
if md.ActiveFrom != nil {
c.ActiveHFs[*md.ActiveFrom] = struct{}{}
}
if md.ActiveTill != nil {
c.ActiveHFs[*md.ActiveTill] = struct{}{}
}
}

// GetMethodByOffset returns method with the provided offset.
Expand Down Expand Up @@ -410,6 +417,9 @@ func (c *ContractMD) AddEvent(md Event) {
if md.ActiveFrom != nil {
c.ActiveHFs[*md.ActiveFrom] = struct{}{}
}
if md.ActiveTill != nil {
c.ActiveHFs[*md.ActiveTill] = struct{}{}
}
}

// Sort sorts interop functions by id.
Expand Down
25 changes: 19 additions & 6 deletions pkg/core/native/native_nep17.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,24 @@ 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{
HFSpecificMethodAndPrice: interop.HFSpecificMethodAndPrice{
Func: f,
CPUFee: cpuFee,
RequiredFlags: flags,
},
}
if len(activeFrom) != 0 {
md.ActiveFrom = &activeFrom[0]
if len(activations) > 0 {
if activations[0] != config.HFDefault {
md.ActiveFrom = &activations[0]
}
}
if len(activations) > 1 {
md.ActiveTill = &activations[1]
}
return md
}
Expand All @@ -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{
HFSpecificEvent: interop.HFSpecificEvent{
MD: desc,
},
}
if len(activeFrom) != 0 {
md.ActiveFrom = &activeFrom[0]
if len(activations) > 0 {
md.ActiveFrom = &activations[0]
}
if len(activations) > 1 {
md.ActiveTill = &activations[1]
}
return md
}
Expand Down

0 comments on commit df1ed68

Please sign in to comment.