params: compare V2 between different versions, close XFN-45 (#1807)

This commit is contained in:
Daniel Liu 2025-11-27 15:59:35 +08:00 committed by benjamin202410
parent 732ee2775d
commit 9c2b34973f

View file

@ -19,6 +19,7 @@ package params
import ( import (
"fmt" "fmt"
"math/big" "math/big"
"slices"
"strings" "strings"
"sync" "sync"
@ -506,44 +507,20 @@ func V2Equal(a, b *V2) bool {
log.Info("[V2Equal] Nil check", "a", a, "b", b, "equal", a == b) log.Info("[V2Equal] Nil check", "a", a, "b", b, "equal", a == b)
return a == b return a == b
} }
if a.SwitchEpoch != b.SwitchEpoch || !configNumEqual(a.SwitchBlock, b.SwitchBlock) || !slices.Equal(a.configIndex, b.configIndex) {
if a.SwitchEpoch != b.SwitchEpoch {
log.Info("[V2Equal] SwitchEpoch mismatch", "a.SwitchEpoch", a.SwitchEpoch, "b.SwitchEpoch", b.SwitchEpoch)
return false return false
} }
// compare AllConfigs according to smaller map
if !configNumEqual(a.SwitchBlock, b.SwitchBlock) { smallerMap, largerMap := a.AllConfigs, b.AllConfigs
aBlock := "<nil>" if len(smallerMap) > len(largerMap) {
bBlock := "<nil>" smallerMap, largerMap = largerMap, smallerMap
if a.SwitchBlock != nil {
aBlock = a.SwitchBlock.String()
} }
if b.SwitchBlock != nil { for key, cfg1 := range smallerMap {
bBlock = b.SwitchBlock.String() cfg2, ok := largerMap[key]
} if !ok || !V2ConfigEqual(cfg1, cfg2) {
log.Info("[V2Equal] SwitchBlock mismatch", "a.SwitchBlock", aBlock, "b.SwitchBlock", bBlock)
return false return false
} }
}
// if !maps.EqualFunc(a.AllConfigs, b.AllConfigs, V2ConfigEqual) {
// log.Info("[V2Equal] AllConfigs mismatch", "a.AllConfigs.len", len(a.AllConfigs), "b.AllConfigs.len", len(b.AllConfigs))
// // Log details about which configs differ
// for key, aConfig := range a.AllConfigs {
// bConfig, exists := b.AllConfigs[key]
// if !exists {
// log.Info("[V2Equal] AllConfigs key missing in b", "key", key)
// } else if !V2ConfigEqual(aConfig, bConfig) {
// log.Info("[V2Equal] AllConfigs value mismatch at key", "key", key)
// }
// }
// for key := range b.AllConfigs {
// if _, exists := a.AllConfigs[key]; !exists {
// log.Info("[V2Equal] AllConfigs key missing in a", "key", key)
// }
// }
// return false
// }
return true return true
} }
@ -552,20 +529,7 @@ func V2ConfigEqual(a, b *V2Config) bool {
log.Info("[V2ConfigEqual] Nil check", "a", a, "b", b, "equal", a == b) log.Info("[V2ConfigEqual] Nil check", "a", a, "b", b, "equal", a == b)
return a == b return a == b
} }
return a.MaxMasternodes == b.MaxMasternodes && a.SwitchRound == b.SwitchRound && a.CertThreshold == b.CertThreshold
equal := *a == *b
if !equal {
log.Info("[V2ConfigEqual] Config mismatch",
"a.MaxMasternodes", a.MaxMasternodes, "b.MaxMasternodes", b.MaxMasternodes,
"a.SwitchRound", a.SwitchRound, "b.SwitchRound", b.SwitchRound,
"a.MinePeriod", a.MinePeriod, "b.MinePeriod", b.MinePeriod,
"a.TimeoutSyncThreshold", a.TimeoutSyncThreshold, "b.TimeoutSyncThreshold", b.TimeoutSyncThreshold,
"a.TimeoutPeriod", a.TimeoutPeriod, "b.TimeoutPeriod", b.TimeoutPeriod,
"a.CertThreshold", a.CertThreshold, "b.CertThreshold", b.CertThreshold,
"a.ExpTimeoutConfig.Base", a.ExpTimeoutConfig.Base, "b.ExpTimeoutConfig.Base", b.ExpTimeoutConfig.Base,
"a.ExpTimeoutConfig.MaxExponent", a.ExpTimeoutConfig.MaxExponent, "b.ExpTimeoutConfig.MaxExponent", b.ExpTimeoutConfig.MaxExponent)
}
return equal
} }
func (c *XDPoSConfig) String() string { func (c *XDPoSConfig) String() string {