Skip to content

Commit

Permalink
fix: update swap keys for possibly overlapped keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed May 8, 2024
1 parent 5e2cd7f commit 8ea4861
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
27 changes: 22 additions & 5 deletions x/fswap/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ var (

// swapKey key(prefix + fromDenom + toDenom)
func swapKey(fromDenom, toDenom string) []byte {
key := append(swapPrefix, fromDenom...)
return append(key, toDenom...)
denoms := combineDenoms(fromDenom, toDenom)
return append(swapPrefix, denoms...)
}

// swappedKey key(prefix + fromDenom + toDenom)
// swappedKey key(prefix + (lengthPrefixed+)fromDenom + (lengthPrefixed+)toDenom)
func swappedKey(fromDenom, toDenom string) []byte {
key := append(swappedKeyPrefix, fromDenom...)
return append(key, toDenom...)
denoms := combineDenoms(fromDenom, toDenom)
return append(swappedKeyPrefix, denoms...)
}

func combineDenoms(fromDenom, toDenom string) []byte {
lengthPrefixedFromDenom := lengthPrefix([]byte(fromDenom))
lengthPrefixedToDenom := lengthPrefix([]byte(toDenom))
return append(lengthPrefixedFromDenom, lengthPrefixedToDenom...)
}

// lengthPrefix prefixes the address bytes with its length, this is used
// for example for variable-length components in store keys.
func lengthPrefix(bz []byte) []byte {
bzLen := len(bz)
if bzLen == 0 {
return bz

Check warning on line 32 in x/fswap/keeper/keys.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/keys.go#L32

Added line #L32 was not covered by tests
}

return append([]byte{byte(bzLen)}, bz...)
}
53 changes: 53 additions & 0 deletions x/fswap/keeper/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package keeper

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSwapKey(t *testing.T) {
tests := []struct {
name string
fromDenom string
toDenom string
expectedKey []byte
}{
{
name: "swapKey",
fromDenom: "cony",
toDenom: "peb",
expectedKey: []byte{0x1, 0x4, 0x63, 0x6f, 0x6e, 0x79, 0x3, 0x70, 0x65, 0x62},
//expectedKey: append(swapPrefix, append(append([]byte{byte(len("cony"))}, []byte("cony")...), append([]byte{byte(len("peb"))}, []byte("peb")...)...)...),

Check failure on line 21 in x/fswap/keeper/keys_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actualKey := swapKey(tc.fromDenom, tc.toDenom)
require.Equal(t, tc.expectedKey, actualKey)
})
}
}

func TestSwappedKey(t *testing.T) {
tests := []struct {
name string
fromDenom string
toDenom string
expectedKey []byte
}{
{
name: "swappedKey",
fromDenom: "cony",
toDenom: "peb",
expectedKey: []byte{0x3, 0x4, 0x63, 0x6f, 0x6e, 0x79, 0x3, 0x70, 0x65, 0x62},
//expectedKey: append(swappedKeyPrefix, append(append([]byte{byte(len("cony"))}, []byte("cony")...), append([]byte{byte(len("peb"))}, []byte("peb")...)...)...),

Check failure on line 44 in x/fswap/keeper/keys_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actualKey := swappedKey(tc.fromDenom, tc.toDenom)
require.Equal(t, tc.expectedKey, actualKey)
})
}
}

0 comments on commit 8ea4861

Please sign in to comment.