From e74e60f12205222d6ea1d67632176bfa55191a47 Mon Sep 17 00:00:00 2001 From: Yaroms Date: Fri, 5 Apr 2024 09:04:00 +0300 Subject: [PATCH 1/4] fix the key decoding --- x/pairing/types/epoch_cu.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/x/pairing/types/epoch_cu.go b/x/pairing/types/epoch_cu.go index 48cae050c6..97a9f08ee4 100644 --- a/x/pairing/types/epoch_cu.go +++ b/x/pairing/types/epoch_cu.go @@ -37,11 +37,15 @@ func ProviderConsumerEpochCuKey(epoch uint64, provider string, project string, c } func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, chainID string, project string, sessionID uint64, err error) { - split := strings.Split(key, " ") - if len(split) != 5 { + if len(key) < 9 { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(split[0])) + + split := strings.Split(key[9:], " ") + if len(split) != 4 { + return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key) + } + epoch = DecodeBlock([]byte(key[0:8])) sessionID, err = strconv.ParseUint(split[4], 10, 64) if err != nil { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad session ID. key: %s", key) @@ -50,20 +54,26 @@ func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, cha } func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainID string, err error) { - split := strings.Split(key, " ") - if len(split) != 3 { + if len(key) < 9 { return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(split[0])) + split := strings.Split(key[9:], " ") + if len(split) != 2 { + return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key) + } + epoch = DecodeBlock([]byte(key[0:8])) return epoch, split[1], split[2], nil } func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string, project string, chainID string, err error) { - split := strings.Split(key, " ") - if len(split) != 4 { + if len(key) < 9 { + return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key) + } + split := strings.Split(key[9:], " ") + if len(split) != 3 { return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(split[0])) + epoch = DecodeBlock([]byte(key[0:8])) return epoch, split[1], split[2], split[3], nil } From e9ab415940393232b3f3e02a2f55c5f282927068 Mon Sep 17 00:00:00 2001 From: Yaroms Date: Fri, 5 Apr 2024 09:17:18 +0300 Subject: [PATCH 2/4] fix --- x/pairing/types/epoch_cu.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/pairing/types/epoch_cu.go b/x/pairing/types/epoch_cu.go index 97a9f08ee4..917dad4141 100644 --- a/x/pairing/types/epoch_cu.go +++ b/x/pairing/types/epoch_cu.go @@ -50,7 +50,7 @@ func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, cha if err != nil { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad session ID. key: %s", key) } - return epoch, split[1], split[2], split[3], sessionID, nil + return epoch, split[0], split[1], split[2], sessionID, nil } func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainID string, err error) { @@ -62,7 +62,7 @@ func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainI return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key) } epoch = DecodeBlock([]byte(key[0:8])) - return epoch, split[1], split[2], nil + return epoch, split[0], split[1], nil } func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string, project string, chainID string, err error) { @@ -74,7 +74,7 @@ func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key) } epoch = DecodeBlock([]byte(key[0:8])) - return epoch, split[1], split[2], split[3], nil + return epoch, split[0], split[1], split[2], nil } func UniqueEpochSessionKeyPrefix() []byte { From 6ba339dead72cf7f56872261cf4098d1fd2abf31 Mon Sep 17 00:00:00 2001 From: Yaroms Date: Fri, 5 Apr 2024 09:33:30 +0300 Subject: [PATCH 3/4] fix --- x/pairing/types/epoch_cu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/pairing/types/epoch_cu.go b/x/pairing/types/epoch_cu.go index 917dad4141..a1174febd5 100644 --- a/x/pairing/types/epoch_cu.go +++ b/x/pairing/types/epoch_cu.go @@ -46,7 +46,7 @@ func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, cha return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key) } epoch = DecodeBlock([]byte(key[0:8])) - sessionID, err = strconv.ParseUint(split[4], 10, 64) + sessionID, err = strconv.ParseUint(split[3], 10, 64) if err != nil { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad session ID. key: %s", key) } From bffd4d5b988a8a3db7115a052843da17b5265e73 Mon Sep 17 00:00:00 2001 From: Yarom Swisa Date: Sun, 7 Apr 2024 17:34:23 +0300 Subject: [PATCH 4/4] make it more explicit --- x/pairing/types/epoch_cu.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/x/pairing/types/epoch_cu.go b/x/pairing/types/epoch_cu.go index a1174febd5..f9442a7939 100644 --- a/x/pairing/types/epoch_cu.go +++ b/x/pairing/types/epoch_cu.go @@ -25,27 +25,27 @@ func DecodeBlock(encodedKey []byte) uint64 { } func UniqueEpochSessionKey(epoch uint64, provider string, chainID string, project string, sessionID uint64) []byte { - return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, chainID, project, strconv.FormatUint(sessionID, 10)}, " ")) + return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, chainID, project, strconv.FormatUint(sessionID, 10)}, " "))...) } func ProviderEpochCuKey(epoch uint64, provider string, chainID string) []byte { - return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, chainID}, " ")) + return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, chainID}, " "))...) } func ProviderConsumerEpochCuKey(epoch uint64, provider string, project string, chainID string) []byte { - return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, project, chainID}, " ")) + return append(EncodeBlock(epoch), []byte(strings.Join([]string{provider, project, chainID}, " "))...) } func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, chainID string, project string, sessionID uint64, err error) { - if len(key) < 9 { + if len(key) < 8 { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key) } - split := strings.Split(key[9:], " ") + split := strings.Split(key[8:], " ") if len(split) != 4 { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(key[0:8])) + epoch = DecodeBlock([]byte(key[:8])) sessionID, err = strconv.ParseUint(split[3], 10, 64) if err != nil { return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad session ID. key: %s", key) @@ -54,26 +54,26 @@ func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, cha } func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainID string, err error) { - if len(key) < 9 { + if len(key) < 8 { return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key) } - split := strings.Split(key[9:], " ") + split := strings.Split(key[8:], " ") if len(split) != 2 { return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(key[0:8])) + epoch = DecodeBlock([]byte(key[:8])) return epoch, split[0], split[1], nil } func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string, project string, chainID string, err error) { - if len(key) < 9 { + if len(key) < 8 { return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key) } - split := strings.Split(key[9:], " ") + split := strings.Split(key[8:], " ") if len(split) != 3 { return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key) } - epoch = DecodeBlock([]byte(key[0:8])) + epoch = DecodeBlock([]byte(key[:8])) return epoch, split[0], split[1], split[2], nil }