Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CNS-epoch-payments-keys-fix #1354

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions x/pairing/types/epoch_cu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,56 @@ 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) {
split := strings.Split(key, " ")
if len(split) != 5 {
if len(key) < 8 {
return 0, "", "", "", 0, fmt.Errorf("invalid UniqueEpochSession key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
sessionID, err = strconv.ParseUint(split[4], 10, 64)

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[: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)
}
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) {
split := strings.Split(key, " ")
if len(split) != 3 {
if len(key) < 8 {
return 0, "", "", fmt.Errorf("invalid ProviderEpochCu key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
return epoch, split[1], split[2], nil
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[:8]))
return epoch, split[0], split[1], 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) < 8 {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key)
}
split := strings.Split(key[8:], " ")
if len(split) != 3 {
return 0, "", "", "", fmt.Errorf("invalid ProviderConsumerEpochCu key: bad structure. key: %s", key)
}
epoch = DecodeBlock([]byte(split[0]))
return epoch, split[1], split[2], split[3], nil
epoch = DecodeBlock([]byte(key[:8]))
return epoch, split[0], split[1], split[2], nil
}

func UniqueEpochSessionKeyPrefix() []byte {
Expand Down
Loading