From 0e08ca17f962f7fab0690e78d7d1299691612550 Mon Sep 17 00:00:00 2001 From: Jiri Date: Fri, 18 Oct 2024 12:17:51 +0200 Subject: [PATCH 1/2] fix --- app/ordered_map.go | 9 +++++++++ app/upgrade_cudos.go | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/ordered_map.go b/app/ordered_map.go index b70fe631..d4ff0916 100644 --- a/app/ordered_map.go +++ b/app/ordered_map.go @@ -127,6 +127,15 @@ func (om *OrderedMap[K, V]) Keys() []K { return om.keys } +func (om *OrderedMap[K, V]) SafeKeys() []K { + // Create a new slice with the same length + clonedSlice := make([]K, len(om.keys)) + + // Copy the elements + copy(clonedSlice, om.keys) + return clonedSlice +} + // PrintOrdered prints the map in current order func (om *OrderedMap[K, V]) PrintOrdered() { for _, key := range om.keys { diff --git a/app/upgrade_cudos.go b/app/upgrade_cudos.go index 780eedc0..403dce1e 100644 --- a/app/upgrade_cudos.go +++ b/app/upgrade_cudos.go @@ -2032,9 +2032,11 @@ func DoGenesisAccountMovements(genesisData *GenesisData, cudosCfg *CudosMergeCon // Handle delegations movement remainingAmountToMove := sdk.NewIntFromBigInt(accountMovement.Amount.BigInt()) + if sourceDelegations, exists := genesisData.Delegations.Get(accountMovement.SourceAddress); exists { - for i := range sourceDelegations.Iterate() { - validatorAddr, delegatedAmount := i.Key, i.Value + // We iterate and delete from source array at the same time + for _, validatorAddr := range sourceDelegations.SafeKeys() { + delegatedAmount := sourceDelegations.MustGet(validatorAddr) if delegatedAmount.GTE(remainingAmountToMove) { // Split delegation From e613b691a56f70f24820d8c65fbc061535c04c18 Mon Sep 17 00:00:00 2001 From: Jiri Date: Fri, 18 Oct 2024 12:27:38 +0200 Subject: [PATCH 2/2] Better fix --- app/ordered_map.go | 23 +++++++++-------------- app/upgrade_cudos.go | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/ordered_map.go b/app/ordered_map.go index d4ff0916..cc600df2 100644 --- a/app/ordered_map.go +++ b/app/ordered_map.go @@ -112,13 +112,17 @@ func (om *OrderedMap[K, V]) Has(key K) bool { func (om *OrderedMap[K, V]) Delete(key K) { if _, exists := om.values[key]; exists { delete(om.values, key) - // Remove key from slice - for i, k := range om.keys { - if k == key { - om.keys = append(om.keys[:i], om.keys[i+1:]...) - break + // Create a new slice to avoid modifying the original reference + newKeys := make([]K, 0, len(om.keys)-1) + + // Remove the key from the keys slice + for _, k := range om.keys { + if k != key { + newKeys = append(newKeys, k) } } + + om.keys = newKeys // Assign the newly created slice to om.keys } } @@ -127,15 +131,6 @@ func (om *OrderedMap[K, V]) Keys() []K { return om.keys } -func (om *OrderedMap[K, V]) SafeKeys() []K { - // Create a new slice with the same length - clonedSlice := make([]K, len(om.keys)) - - // Copy the elements - copy(clonedSlice, om.keys) - return clonedSlice -} - // PrintOrdered prints the map in current order func (om *OrderedMap[K, V]) PrintOrdered() { for _, key := range om.keys { diff --git a/app/upgrade_cudos.go b/app/upgrade_cudos.go index 403dce1e..9db82c92 100644 --- a/app/upgrade_cudos.go +++ b/app/upgrade_cudos.go @@ -2035,7 +2035,7 @@ func DoGenesisAccountMovements(genesisData *GenesisData, cudosCfg *CudosMergeCon if sourceDelegations, exists := genesisData.Delegations.Get(accountMovement.SourceAddress); exists { // We iterate and delete from source array at the same time - for _, validatorAddr := range sourceDelegations.SafeKeys() { + for _, validatorAddr := range sourceDelegations.Keys() { delegatedAmount := sourceDelegations.MustGet(validatorAddr) if delegatedAmount.GTE(remainingAmountToMove) {