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: update swap keys for possibly overlapped keys #1365

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/mint, x/slashing) [\#1323](https://github.com/Finschia/finschia-sdk/pull/1323) add missing nil check for params validation
* (x/server) [\#1337](https://github.com/Finschia/finschia-sdk/pull/1337) fix panic when defining minimum gas config as `100stake;100uatom`. Use a `,` delimiter instead of `;`. Fixes the server config getter to use the correct delimiter (backport cosmos/cosmos-sdk#18537)
* (x/fbridge) [\#1361](https://github.com/Finschia/finschia-sdk/pull/1361) Fixes fbridge auth checking bug
* (x/fswap) [\#1365](https://github.com/Finschia/finschia-sdk/pull/1365) fix update swap keys for possibly overlapped keys(`(hello,world) should be different to (hel,loworld)`)

### Removed

Expand Down
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 @@

// 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")...)...)...),
},
}
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")...)...)...),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actualKey := swappedKey(tc.fromDenom, tc.toDenom)
require.Equal(t, tc.expectedKey, actualKey)
})
}
}
Loading