Skip to content

Commit

Permalink
encode uint instead of convert to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarom Swisa authored and Yarom Swisa committed Apr 3, 2024
1 parent 8a92426 commit d8a16e9
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions x/pairing/types/epoch_cu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/binary"
"fmt"
"strconv"
"strings"
Expand All @@ -13,27 +14,34 @@ const (
ProviderConsumerEpochCuPrefix = "ProviderConsumerEpochCu/"
)

func EncodeBlock(block uint64) []byte {
encodedKey := make([]byte, 8)
binary.BigEndian.PutUint64(encodedKey[0:8], block)
return encodedKey
}

func DecodeBlock(encodedKey []byte) uint64 {
return binary.BigEndian.Uint64(encodedKey[0:8])
}

func UniqueEpochSessionKey(epoch uint64, provider string, chainID string, project string, sessionID uint64) []byte {
return []byte(strings.Join([]string{strconv.FormatUint(epoch, 10), provider, chainID, project, strconv.FormatUint(sessionID, 10)}, " "))
return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, chainID, project, strconv.FormatUint(sessionID, 10)}, " "))
}

func ProviderEpochCuKey(epoch uint64, provider string, chainID string) []byte {
return []byte(strings.Join([]string{strconv.FormatUint(epoch, 10), provider, chainID}, " "))
return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, chainID}, " "))
}

func ProviderConsumerEpochCuKey(epoch uint64, provider string, project string, chainID string) []byte {
return []byte(strings.Join([]string{strconv.FormatUint(epoch, 10), provider, project, chainID}, " "))
return []byte(strings.Join([]string{string(EncodeBlock(epoch)), provider, project, chainID}, " "))
}

func DecodeUniqueEpochSessionKey(key string) (epoch uint64, provider string, chainID string, project string, sessionID uint64, err error) {
split := strings.Split(key, " ")
if len(split) != 5 {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key)
}
epoch, err = strconv.ParseUint(split[0], 10, 64)
if err != nil {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad epoch. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
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)
Expand All @@ -46,10 +54,7 @@ func DecodeProviderEpochCuKey(key string) (epoch uint64, provider string, chainI
if len(split) != 3 {
return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key)
}
epoch, err = strconv.ParseUint(split[0], 10, 64)
if err != nil {
return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad epoch. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
return epoch, split[1], split[2], nil
}

Expand All @@ -58,10 +63,7 @@ func DecodeProviderConsumerEpochCuKey(key string) (epoch uint64, provider string
if len(split) != 4 {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key)
}
epoch, err = strconv.ParseUint(split[0], 10, 64)
if err != nil {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad epoch. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
return epoch, split[1], split[2], split[3], nil
}

Expand Down

0 comments on commit d8a16e9

Please sign in to comment.