Skip to content

Commit

Permalink
native: perform initialisation for the set of hardforks
Browse files Browse the repository at this point in the history
Close #3433.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed May 17, 2024
1 parent 2d4993a commit c90f678
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions pkg/core/native/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,11 @@ func (m *Management) OnPersist(ic *interop.Context) error {
var cache *ManagementCache
for _, native := range ic.Natives {
var (
activeIn = native.ActiveIn()
isDeploy bool
isUpdate bool
latestHF config.Hardfork
activeIn = native.ActiveIn()
isDeploy bool
isUpdate bool
latestHF config.Hardfork
currentActiveHFs []config.Hardfork
)
activeHFs := native.Metadata().ActiveHFs
isDeploy = activeIn == nil && ic.Block.Index == 0 ||
Expand All @@ -611,16 +612,18 @@ func (m *Management) OnPersist(ic *interop.Context) error {
isUpdate = true
activation := hf // avoid loop variable pointer exporting.
activeIn = &activation // reuse ActiveIn variable for the initialization hardfork.
// Break immediately since native Initialize should be called only for the first hardfork in a raw
// Break immediately since native Initialize should be called starting from the first hardfork in a raw
// (if there are multiple hardforks with the same enabling height).
break
}
}
}
// Search for the latest active hardfork to properly construct manifest.
// Search for the latest active hardfork to properly construct manifest and
// initialize natives for the range of active hardforks.
for _, hf := range config.Hardforks {
if _, ok := activeHFs[hf]; ok && ic.IsHardforkActivation(hf) {
latestHF = hf
currentActiveHFs = append(currentActiveHFs, hf)
}
}
if !(isDeploy || isUpdate) {
Expand Down Expand Up @@ -654,9 +657,21 @@ func (m *Management) OnPersist(ic *interop.Context) error {
if err != nil {
return fmt.Errorf("failed to put contract state: %w", err)
}
if err := native.Initialize(ic, activeIn, hfSpecificMD); err != nil {
return fmt.Errorf("initializing %s native contract at HF %d: %w", md.Name, activeIn, err)

// Deploy hardfork (contract's ActiveIn) is not a part of contract's active hardforks and
// allowed to be nil, this, a special initialization call for it.
if isDeploy {
if err := native.Initialize(ic, activeIn, hfSpecificMD); err != nil {
return fmt.Errorf("initializing %s native contract at HF %v: %w", md.Name, activeIn, err)

Check warning on line 665 in pkg/core/native/management.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/native/management.go#L665

Added line #L665 was not covered by tests
}
}
// The rest of activating hardforks also require initialization.
for _, hf := range currentActiveHFs {
if err := native.Initialize(ic, &hf, hfSpecificMD); err != nil {
return fmt.Errorf("initializing %s native contract at HF %d: %w", md.Name, activeIn, err)

Check warning on line 671 in pkg/core/native/management.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/native/management.go#L671

Added line #L671 was not covered by tests
}
}

if cache == nil {
cache = ic.DAO.GetRWCache(m.ID).(*ManagementCache)
}
Expand Down

0 comments on commit c90f678

Please sign in to comment.