/
address_map.go
69 lines (57 loc) · 2.39 KB
/
address_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/nephirim/blackfury/x/interchainstaking/types"
)
// GetRemoteAddressMap retrieves a remote address using a local address.
func (k *Keeper) GetRemoteAddressMap(ctx sdk.Context, localAddress []byte, chainID string) ([]byte, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetRemoteAddressKey(localAddress, chainID)
value := store.Get(key)
return value, value != nil
}
// IterateUserMappedAccounts iterates over all the user mapped accounts.
func (k Keeper) IterateUserMappedAccounts(ctx sdk.Context, localAddress []byte, fn func(index int64, chainID string, remoteAddressBytes []byte) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetRemoteAddressPrefix(localAddress))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
value := iterator.Value()
key := iterator.Key()
chainIDBytes := key[len(types.GetRemoteAddressPrefix(localAddress)):]
stop := fn(i, string(chainIDBytes), value)
if stop {
break
}
i++
}
}
// SetRemoteAddressMap sets a remote address using a local address as a map.
func (k *Keeper) SetRemoteAddressMap(ctx sdk.Context, localAddress, remoteAddress []byte, chainID string) {
store := ctx.KVStore(k.storeKey)
key := types.GetRemoteAddressKey(localAddress, chainID)
store.Set(key, remoteAddress)
}
// GetLocalAddressMap retrieves a local address using a remote address.
func (k *Keeper) GetLocalAddressMap(ctx sdk.Context, remoteAddress []byte, chainID string) ([]byte, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetLocalAddressKey(remoteAddress, chainID)
value := store.Get(key)
return value, value != nil
}
// SetLocalAddressMap sets a local address using a remote address as a map.
func (k *Keeper) SetLocalAddressMap(ctx sdk.Context, localAddress, remoteAddress []byte, chainID string) {
store := ctx.KVStore(k.storeKey)
key := types.GetLocalAddressKey(remoteAddress, chainID)
store.Set(key, localAddress)
}
// SetAddressMapPair sets forward and reverse maps for localAddress => remoteAddress and remoteAddress => localAddress.
func (k *Keeper) SetAddressMapPair(ctx sdk.Context, localAddress, remoteAddress []byte, chainID string) {
k.SetLocalAddressMap(ctx, localAddress, remoteAddress, chainID)
k.SetRemoteAddressMap(ctx, localAddress, remoteAddress, chainID)
}