Skip to content

Commit

Permalink
Add reverse mapping for pointers (sei-protocol#1634)
Browse files Browse the repository at this point in the history
* Add reverse key mapping for pointers

* Fix unit test failures

* Make some methods private
  • Loading branch information
yzang2019 authored May 7, 2024
1 parent 5330af1 commit 0ecbfe4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 19 deletions.
98 changes: 84 additions & 14 deletions x/evm/keeper/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ import (
)

func (k *Keeper) SetERC20NativePointer(ctx sdk.Context, token string, addr common.Address) error {
return k.SetPointerInfo(ctx, types.PointerERC20NativeKey(token), addr[:], native.CurrentVersion)
err := k.setPointerInfo(ctx, types.PointerERC20NativeKey(token), addr[:], native.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(token), native.CurrentVersion)
}

func (k *Keeper) SetERC20NativePointerWithVersion(ctx sdk.Context, token string, addr common.Address, version uint16) error {
return k.SetPointerInfo(ctx, types.PointerERC20NativeKey(token), addr[:], version)
err := k.setPointerInfo(ctx, types.PointerERC20NativeKey(token), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(token), version)
}

func (k *Keeper) GetERC20NativePointer(ctx sdk.Context, token string) (addr common.Address, version uint16, exists bool) {
Expand All @@ -31,16 +39,37 @@ func (k *Keeper) GetERC20NativePointer(ctx sdk.Context, token string) (addr comm
return
}

func (k *Keeper) GetERC20NativeByPointer(ctx sdk.Context, addr common.Address) (token string, version uint16, exists bool) {
tokenBz, version, exists := k.GetPointerInfo(ctx, types.PointerReverseRegistryKey(addr))
if exists {
token = string(tokenBz)
}
return
}

func (k *Keeper) DeleteERC20NativePointer(ctx sdk.Context, token string, version uint16) {
k.DeletePointerInfo(ctx, types.PointerERC20NativeKey(token), version)
addr, _, exists := k.GetERC20NativePointer(ctx, token)
if exists {
k.deletePointerInfo(ctx, types.PointerERC20NativeKey(token), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}

}

func (k *Keeper) SetERC20CW20Pointer(ctx sdk.Context, cw20Address string, addr common.Address) error {
return k.SetPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), addr[:], cw20.CurrentVersion)
err := k.setPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), addr[:], cw20.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw20Address), cw20.CurrentVersion)
}

func (k *Keeper) SetERC20CW20PointerWithVersion(ctx sdk.Context, cw20Address string, addr common.Address, version uint16) error {
return k.SetPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), addr[:], version)
err := k.setPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw20Address), version)
}

func (k *Keeper) GetERC20CW20Pointer(ctx sdk.Context, cw20Address string) (addr common.Address, version uint16, exists bool) {
Expand All @@ -51,16 +80,37 @@ func (k *Keeper) GetERC20CW20Pointer(ctx sdk.Context, cw20Address string) (addr
return
}

func (k *Keeper) GetERC20CW20ByPointer(ctx sdk.Context, addr common.Address) (cw20Address string, version uint16, exists bool) {
cw20AddressBz, version, exists := k.GetPointerInfo(ctx, types.PointerReverseRegistryKey(addr))
if exists {
cw20Address = string(cw20AddressBz)
}
return
}

func (k *Keeper) DeleteERC20CW20Pointer(ctx sdk.Context, cw20Address string, version uint16) {
k.DeletePointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), version)
addr, _, exists := k.GetERC20CW20Pointer(ctx, cw20Address)
if exists {
k.deletePointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}

}

func (k *Keeper) SetERC721CW721Pointer(ctx sdk.Context, cw721Address string, addr common.Address) error {
return k.SetPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), addr[:], cw721.CurrentVersion)
err := k.setPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), addr[:], cw721.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw721Address), cw721.CurrentVersion)
}

func (k *Keeper) SetERC721CW721PointerWithVersion(ctx sdk.Context, cw721Address string, addr common.Address, version uint16) error {
return k.SetPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), addr[:], version)
err := k.setPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw721Address), version)
}

func (k *Keeper) GetERC721CW721Pointer(ctx sdk.Context, cw721Address string) (addr common.Address, version uint16, exists bool) {
Expand All @@ -71,12 +121,28 @@ func (k *Keeper) GetERC721CW721Pointer(ctx sdk.Context, cw721Address string) (ad
return
}

func (k *Keeper) GetERC721CW721ByPointer(ctx sdk.Context, addr common.Address) (cw721Address string, version uint16, exists bool) {
cw721AddressBz, version, exists := k.GetPointerInfo(ctx, types.PointerReverseRegistryKey(addr))
if exists {
cw721Address = string(cw721AddressBz)
}
return
}

func (k *Keeper) DeleteERC721CW721Pointer(ctx sdk.Context, cw721Address string, version uint16) {
k.DeletePointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), version)
addr, _, exists := k.GetERC721CW721Pointer(ctx, cw721Address)
if exists {
k.deletePointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}
}

func (k *Keeper) SetCW20ERC20Pointer(ctx sdk.Context, erc20Address common.Address, addr string) error {
return k.SetPointerInfo(ctx, types.PointerCW20ERC20Key(erc20Address), []byte(addr), erc20.CurrentVersion)
err := k.setPointerInfo(ctx, types.PointerCW20ERC20Key(erc20Address), []byte(addr), erc20.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(common.BytesToAddress([]byte(addr))), erc20Address[:], erc20.CurrentVersion)
}

func (k *Keeper) GetCW20ERC20Pointer(ctx sdk.Context, erc20Address common.Address) (addr sdk.AccAddress, version uint16, exists bool) {
Expand All @@ -88,7 +154,11 @@ func (k *Keeper) GetCW20ERC20Pointer(ctx sdk.Context, erc20Address common.Addres
}

func (k *Keeper) SetCW721ERC721Pointer(ctx sdk.Context, erc721Address common.Address, addr string) error {
return k.SetPointerInfo(ctx, types.PointerCW721ERC721Key(erc721Address), []byte(addr), erc721.CurrentVersion)
err := k.setPointerInfo(ctx, types.PointerCW721ERC721Key(erc721Address), []byte(addr), erc721.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(common.BytesToAddress([]byte(addr))), erc721Address[:], erc721.CurrentVersion)
}

func (k *Keeper) GetCW721ERC721Pointer(ctx sdk.Context, erc721Address common.Address) (addr sdk.AccAddress, version uint16, exists bool) {
Expand All @@ -112,10 +182,10 @@ func (k *Keeper) GetPointerInfo(ctx sdk.Context, pref []byte) (addr []byte, vers
return
}

func (k *Keeper) SetPointerInfo(ctx sdk.Context, pref []byte, addr []byte, version uint16) error {
func (k *Keeper) setPointerInfo(ctx sdk.Context, pref []byte, addr []byte, version uint16) error {
existingAddr, existingVersion, exists := k.GetPointerInfo(ctx, pref)
if exists && existingVersion >= version {
return fmt.Errorf("pointer at %s with version %d exists when trying to set pointer for version %d", string(existingAddr), existingVersion, version)
return fmt.Errorf("pointer at %X with version %d exists when trying to set pointer for version %d", string(existingAddr), existingVersion, version)
}
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), pref)
versionBz := make([]byte, 2)
Expand All @@ -124,7 +194,7 @@ func (k *Keeper) SetPointerInfo(ctx sdk.Context, pref []byte, addr []byte, versi
return nil
}

func (k *Keeper) DeletePointerInfo(ctx sdk.Context, pref []byte, version uint16) {
func (k *Keeper) deletePointerInfo(ctx sdk.Context, pref []byte, version uint16) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), pref)
versionBz := make([]byte, 2)
binary.BigEndian.PutUint16(versionBz, version)
Expand Down
28 changes: 25 additions & 3 deletions x/evm/keeper/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@ import (
"github.com/stretchr/testify/require"
)

func TestSetPointer(t *testing.T) {
func TestERC20NativePointer(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
_, pointer := testkeeper.MockAddressPair()
cw20, _ := testkeeper.MockAddressPair()
cw721, _ := testkeeper.MockAddressPair()
require.Nil(t, k.SetERC20NativePointer(ctx, "test", pointer))
require.NotNil(t, k.SetERC20NativePointer(ctx, "test", pointer)) // already set
addr, _, _ := k.GetERC20NativePointer(ctx, "test")
require.Equal(t, pointer, addr)
token, _, _ := k.GetERC20NativeByPointer(ctx, addr)
require.Equal(t, "test", token)
}

func TestSetERC20CW20Pointer(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
_, pointer := testkeeper.MockAddressPair()
cw20, _ := testkeeper.MockAddressPair()
require.Nil(t, k.SetERC20CW20Pointer(ctx, cw20.String(), pointer))
require.NotNil(t, k.SetERC20CW20Pointer(ctx, cw20.String(), pointer)) // already set
addr, _, _ := k.GetERC20CW20Pointer(ctx, cw20.String())
require.Equal(t, pointer, addr)
cw20Addr, _, _ := k.GetERC20CW20ByPointer(ctx, addr)
require.Equal(t, cw20.String(), cw20Addr)
}

func TestERC721CW721Pointer(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
_, pointer := testkeeper.MockAddressPair()
cw721, _ := testkeeper.MockAddressPair()
require.Nil(t, k.SetERC721CW721Pointer(ctx, cw721.String(), pointer))
require.NotNil(t, k.SetERC721CW721Pointer(ctx, cw721.String(), pointer)) // already set
addr, _, _ := k.GetERC721CW721Pointer(ctx, cw721.String())
require.Equal(t, pointer, addr)
cw721Addr, _, _ := k.GetERC721CW721ByPointer(ctx, addr)
require.Equal(t, cw721.String(), cw721Addr)
}
9 changes: 7 additions & 2 deletions x/evm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ var (
ReplayedHeight = []byte{0x13}
ReplayInitialHeight = []byte{0x14}

PointerRegistryPrefix = []byte{0x15}
PointerCWCodePrefix = []byte{0x16}
PointerRegistryPrefix = []byte{0x15}
PointerCWCodePrefix = []byte{0x16}
PointerReverseRegistryPrefix = []byte{0x17}
)

var (
Expand Down Expand Up @@ -120,3 +121,7 @@ func PointerCW721ERC721Key(erc721Addr common.Address) []byte {
erc721Addr[:]...,
)
}

func PointerReverseRegistryKey(addr common.Address) []byte {
return append(PointerReverseRegistryPrefix, addr[:]...)
}

0 comments on commit 0ecbfe4

Please sign in to comment.