Skip to content

Commit

Permalink
Merge pull request #6104 from multiversx/fix-backwards-compatibility-…
Browse files Browse the repository at this point in the history
…problem-2024.04.03

Fixed backwards compatibility problem
  • Loading branch information
gabi-vuls authored Apr 12, 2024
2 parents ea7bf27 + 1658254 commit c332095
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
42 changes: 27 additions & 15 deletions epochStart/metachain/legacySystemSCs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/marshal"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/common/errChan"
vInfo "github.com/multiversx/mx-chain-go/common/validatorInfo"
Expand All @@ -24,7 +26,6 @@ import (
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/vm"
"github.com/multiversx/mx-chain-go/vm/systemSmartContracts"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

type legacySystemSCProcessor struct {
Expand Down Expand Up @@ -288,12 +289,11 @@ func (s *legacySystemSCProcessor) unStakeNodesWithNotEnoughFunds(
continue
}

stakingV4Enabled := s.enableEpochsHandler.IsFlagEnabled(common.StakingV4StartedFlag)
validatorLeaving := validatorInfo.ShallowClone()
validatorLeaving.SetListAndIndex(string(common.LeavingList), validatorLeaving.GetIndex(), s.enableEpochsHandler.IsFlagEnabled(common.StakingV4StartedFlag))
err = validatorsInfoMap.Replace(validatorInfo, validatorLeaving)
if err != nil {
return 0, err
}
validatorLeaving.SetListAndIndex(string(common.LeavingList), validatorLeaving.GetIndex(), stakingV4Enabled)
err = s.replaceValidators(validatorInfo, validatorLeaving, validatorsInfoMap)
log.LogIfError(err)
}

err = s.updateDelegationContracts(mapOwnersKeys)
Expand All @@ -302,7 +302,9 @@ func (s *legacySystemSCProcessor) unStakeNodesWithNotEnoughFunds(
}

nodesToStakeFromQueue := uint32(len(nodesToUnStake))
nodesToStakeFromQueue -= nodesUnStakedFromAdditionalQueue
if s.enableEpochsHandler.IsFlagEnabled(common.CorrectLastUnJailedFlag) {
nodesToStakeFromQueue -= nodesUnStakedFromAdditionalQueue
}

log.Debug("stake nodes from waiting list", "num", nodesToStakeFromQueue)
return nodesToStakeFromQueue, nil
Expand Down Expand Up @@ -720,10 +722,8 @@ func (s *legacySystemSCProcessor) stakingToValidatorStatistics(
}

if !isNew {
err = validatorsInfoMap.Delete(jailedValidator)
if err != nil {
return nil, err
}
// the new validator is deleted from the staking queue, not the jailed validator
validatorsInfoMap.DeleteByKey(blsPubKey, account.GetShardId())
}

account.SetListAndIndex(jailedValidator.GetShardId(), string(common.NewList), uint32(stakingData.StakedNonce), s.enableEpochsHandler.IsFlagEnabled(common.StakingV4StartedFlag))
Expand Down Expand Up @@ -752,14 +752,26 @@ func (s *legacySystemSCProcessor) stakingToValidatorStatistics(
}

newValidatorInfo := s.validatorInfoCreator.PeerAccountToValidatorInfo(account)
err = validatorsInfoMap.Replace(jailedValidator, newValidatorInfo)
if err != nil {
return nil, err
}
err = s.replaceValidators(jailedValidator, newValidatorInfo, validatorsInfoMap)
log.LogIfError(err)

return blsPubKey, nil
}

func (s *legacySystemSCProcessor) replaceValidators(
old state.ValidatorInfoHandler,
new state.ValidatorInfoHandler,
validatorsInfoMap state.ShardValidatorsInfoMapHandler,
) error {
stakingV4Enabled := s.enableEpochsHandler.IsFlagEnabled(common.StakingV4StartedFlag)
if stakingV4Enabled {
return validatorsInfoMap.Replace(old, new)
}

_ = validatorsInfoMap.ReplaceValidatorByKey(old.GetPublicKey(), new, old.GetShardId())
return nil
}

func isValidator(validator state.ValidatorInfoHandler) bool {
return validator.GetList() == string(common.WaitingList) || validator.GetList() == string(common.EligibleList)
}
Expand Down
5 changes: 4 additions & 1 deletion state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-go/common"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"

"github.com/multiversx/mx-chain-go/common"
)

// AccountFactory creates an account of different types
Expand Down Expand Up @@ -292,7 +293,9 @@ type ShardValidatorsInfoMapHandler interface {

Add(validator ValidatorInfoHandler) error
Delete(validator ValidatorInfoHandler) error
DeleteByKey(blsKey []byte, shardID uint32)
Replace(old ValidatorInfoHandler, new ValidatorInfoHandler) error
ReplaceValidatorByKey(oldBlsKey []byte, new ValidatorInfoHandler, shardID uint32) bool
SetValidatorsInShard(shardID uint32, validators []ValidatorInfoHandler) error
}

Expand Down
35 changes: 24 additions & 11 deletions state/validatorsInfoMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,9 @@ func (vi *shardValidatorsInfoMap) Replace(old ValidatorInfoHandler, new Validato
"with new validator", hex.EncodeToString(new.GetPublicKey()), "shard", new.GetShardId(), "list", new.GetList(),
)

vi.mutex.Lock()
defer vi.mutex.Unlock()

for idx, validator := range vi.valInfoMap[shardID] {
if bytes.Equal(validator.GetPublicKey(), old.GetPublicKey()) {
vi.valInfoMap[shardID][idx] = new
return nil
}
replaced := vi.ReplaceValidatorByKey(old.GetPublicKey(), new, shardID)
if replaced {
return nil
}

return fmt.Errorf("old %w: %s when trying to replace it with %s",
Expand All @@ -123,6 +118,20 @@ func (vi *shardValidatorsInfoMap) Replace(old ValidatorInfoHandler, new Validato
)
}

// ReplaceValidatorByKey will replace an existing ValidatorInfoHandler with a new one, based on the provided blsKey for the old record.
func (vi *shardValidatorsInfoMap) ReplaceValidatorByKey(oldBlsKey []byte, new ValidatorInfoHandler, shardID uint32) bool {
vi.mutex.Lock()
defer vi.mutex.Unlock()

for idx, validator := range vi.valInfoMap[shardID] {
if bytes.Equal(validator.GetPublicKey(), oldBlsKey) {
vi.valInfoMap[shardID][idx] = new
return true
}
}
return false
}

// SetValidatorsInShard resets all validators saved in a specific shard with the provided []ValidatorInfoHandler.
// Before setting them, it checks that provided validators have the same shardID as the one provided.
func (vi *shardValidatorsInfoMap) SetValidatorsInShard(shardID uint32, validators []ValidatorInfoHandler) error {
Expand Down Expand Up @@ -160,18 +169,22 @@ func (vi *shardValidatorsInfoMap) Delete(validator ValidatorInfoHandler) error {
}

shardID := validator.GetShardId()
vi.DeleteByKey(validator.GetPublicKey(), shardID)
return nil
}

// DeleteByKey will delete the provided blsKey from the internally stored map, if found.
func (vi *shardValidatorsInfoMap) DeleteByKey(blsKey []byte, shardID uint32) {
vi.mutex.Lock()
defer vi.mutex.Unlock()

for index, validatorInfo := range vi.valInfoMap[shardID] {
if bytes.Equal(validatorInfo.GetPublicKey(), validator.GetPublicKey()) {
if bytes.Equal(validatorInfo.GetPublicKey(), blsKey) {
length := len(vi.valInfoMap[shardID])
vi.valInfoMap[shardID][index] = vi.valInfoMap[shardID][length-1]
vi.valInfoMap[shardID][length-1] = nil
vi.valInfoMap[shardID] = vi.valInfoMap[shardID][:length-1]
break
}
}

return nil
}
4 changes: 0 additions & 4 deletions vm/systemSmartContracts/eei.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,13 @@ func (host *vmContext) GetStorageFromAddress(address []byte, key []byte) []byte
if value, isInMap := storageAdrMap[string(key)]; isInMap {
return value
}
} else {
storageAdrMap = make(map[string][]byte)
}

data, _, err := host.blockChainHook.GetStorageData(address, key)
if err != nil {
return nil
}

storageAdrMap[string(key)] = data

return data
}

Expand Down

0 comments on commit c332095

Please sign in to comment.