Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do features preactivation at any height in 'blockchainHeightAction'. #1308

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions itests/config/genesis_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,12 @@ func newBlockchainConfig(additionalArgsPath ...string) (*config, []AccountInfo,
if err != nil {
return nil, nil, err
}
cfg.PreactivatedFeaturesAtHeights = make([]settings.FeatureActionHeight, len(preactivatedFeatures))
for i, f := range preactivatedFeatures {
cfg.PreactivatedFeaturesAtHeights[i] = settings.FeatureActionHeight{ID: f.Feature, Height: f.Height}

featsAtHeights := make(map[uint64][]int16)
for _, f := range preactivatedFeatures {
featsAtHeights[f.Height] = append(featsAtHeights[f.Height], f.Feature)
}
cfg.PreactivatedFeaturesAtHeights = featsAtHeights

return &config{
BlockchainSettings: &cfg,
Expand Down
12 changes: 6 additions & 6 deletions pkg/settings/blockchain_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type FeatureActionHeight struct {

type FunctionalitySettings struct {
// Features.
FeaturesVotingPeriod uint64 `json:"features_voting_period"`
VotesForFeatureActivation uint64 `json:"votes_for_feature_activation"`
PreactivatedFeatures []int16 `json:"preactivated_features"`
PreactivatedFeaturesAtHeights []FeatureActionHeight `json:"preactivated_features_at_heights"`
DoubleFeaturesPeriodsAfterHeight uint64 `json:"double_features_periods_after_height"`
SponsorshipSingleActivationPeriod bool `json:"sponsorship_single_activation_period"`
FeaturesVotingPeriod uint64 `json:"features_voting_period"`
VotesForFeatureActivation uint64 `json:"votes_for_feature_activation"`
PreactivatedFeatures []int16 `json:"preactivated_features"`
PreactivatedFeaturesAtHeights map[uint64][]int16 `json:"preactivated_features_at_heights"`
DoubleFeaturesPeriodsAfterHeight uint64 `json:"double_features_periods_after_height"`
SponsorshipSingleActivationPeriod bool `json:"sponsorship_single_activation_period"`

// Heights when some rules change.
GenerationBalanceDepthFrom50To1000AfterHeight uint64 `json:"generation_balance_depth_from_50_to_1000_after_height"`
Expand Down
55 changes: 31 additions & 24 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,22 @@ func newStateManager(dataDir string, amend bool, params StateParams, settings *s
// 0 state height means that no blocks are found in state, so blockchain history is empty and we have to add genesis
if height == 0 {
// Assign unique block number for this block ID, add this number to the list of valid blocks
if err := state.stateDB.addBlock(settings.Genesis.BlockID()); err != nil {
genesisBlockID := settings.Genesis.BlockID()
if err := state.stateDB.addBlock(genesisBlockID); err != nil {
return nil, err
}
if err := state.addGenesisBlock(); err != nil {
return nil, errors.Wrap(err, "failed to apply/save genesis")
}
// We apply pre-activated features after genesis block, so they aren't active in genesis itself
if err := state.applyPreActivatedFeatures(settings.PreactivatedFeatures, settings.PreactivatedFeaturesAtHeights,
settings.Genesis.BlockID()); err != nil {
if err := state.applyPreActivatedFeatures(settings.PreactivatedFeatures, genesisBlockID, 1); err != nil {
return nil, errors.Wrap(err, "failed to apply pre-activated features")
}
// Flush all changes to the database.
if err := state.flush(); err != nil {
return nil, errors.Wrap(err, "failed to flush genesis block changes")
}
state.reset() // Reset the current state
}

// check the correct blockchain is being loaded
Expand Down Expand Up @@ -625,33 +630,29 @@ func (s *stateManager) addGenesisBlock() error {
return nil
}

func (s *stateManager) applyPreActivatedFeatures(
features []int16, featuresActivationHeights []settings.FeatureActionHeight, blockID proto.BlockID,
) error {
func (s *stateManager) applyPreActivatedFeatures(features []int16, blockID proto.BlockID, height proto.Height) error {
for _, featureID := range features {
approvalRequest := &approvedFeaturesRecord{1}
if err := s.stor.features.approveFeature(featureID, approvalRequest, blockID); err != nil {
return err
featuresIsApproved, apprErr := s.stor.features.newestIsApproved(featureID)
if apprErr != nil {
return apprErr
}
activationRequest := &activatedFeaturesRecord{1}
if err := s.stor.features.activateFeature(featureID, activationRequest, blockID); err != nil {
return err
if !featuresIsApproved {
approvalRequest := &approvedFeaturesRecord{height}
if err := s.stor.features.approveFeature(featureID, approvalRequest, blockID); err != nil {
return err
}
}
}
for _, fah := range featuresActivationHeights {
approvalRequest := &approvedFeaturesRecord{approvalHeight: fah.Height}
if err := s.stor.features.approveFeature(fah.ID, approvalRequest, blockID); err != nil {
return err
featuresIsActivated, actErr := s.stor.features.newestIsActivated(featureID)
if actErr != nil {
return actErr
}
activationRequest := &activatedFeaturesRecord{activationHeight: fah.Height}
if err := s.stor.features.activateFeature(fah.ID, activationRequest, blockID); err != nil {
return err
if !featuresIsActivated {
activationRequest := &activatedFeaturesRecord{height}
if err := s.stor.features.activateFeature(featureID, activationRequest, blockID); err != nil {
return err
}
}
}
if err := s.flush(); err != nil {
return err
}
s.reset()
return nil
}

Expand Down Expand Up @@ -1345,6 +1346,12 @@ func (s *stateManager) blockchainHeightAction(blockchainHeight uint64, lastBlock
return rvErr
}
}
// Apply pre-activated features at the preconfigured height
nextBlockHeight := blockchainHeight + 1
feats := s.settings.PreactivatedFeaturesAtHeights[nextBlockHeight]
if fErr := s.applyPreActivatedFeatures(feats, nextBlock, nextBlockHeight); fErr != nil {
return fErr
}

needToRecalculate, err := s.needToRecalculateVotesAfterCappedRewardActivationInVotingPeriod(blockchainHeight)
if err != nil {
Expand Down
Loading