-
Notifications
You must be signed in to change notification settings - Fork 13
/
oracle.go
47 lines (40 loc) · 1.69 KB
/
oracle.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
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
fxtypes "github.com/functionx/fx-core/v3/types"
crosschainv1 "github.com/functionx/fx-core/v3/x/crosschain/legacy/v1"
"github.com/functionx/fx-core/v3/x/crosschain/types"
)
func MigrateOracle(ctx sdk.Context, cdc codec.BinaryCodec, storeKey sdk.StoreKey, stakingKeeper StakingKeeper) (types.Oracles, sdk.ValAddress, error) {
validatorsByPower := stakingKeeper.GetBondedValidatorsByPower(ctx)
if len(validatorsByPower) <= 0 {
panic("no found bonded validator")
}
delegateValidator := validatorsByPower[0]
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.OracleKey)
defer iterator.Close()
oracles := types.Oracles{}
for ; iterator.Valid(); iterator.Next() {
var legacyOracle crosschainv1.LegacyOracle
cdc.MustUnmarshal(iterator.Value(), &legacyOracle)
if legacyOracle.DepositAmount.Denom != fxtypes.DefaultDenom {
return nil, nil, sdkerrors.Wrapf(types.ErrInvalid, "delegate denom: %s", legacyOracle.DepositAmount.Denom)
}
oracle := types.Oracle{
OracleAddress: legacyOracle.OracleAddress,
BridgerAddress: legacyOracle.OrchestratorAddress,
ExternalAddress: legacyOracle.ExternalAddress,
DelegateAmount: legacyOracle.DepositAmount.Amount,
StartHeight: legacyOracle.StartHeight,
Online: !legacyOracle.Jailed,
DelegateValidator: delegateValidator.OperatorAddress,
SlashTimes: 0,
}
store.Set(types.GetOracleKey(oracle.GetOracle()), cdc.MustMarshal(&oracle))
oracles = append(oracles, oracle)
}
return oracles, delegateValidator.GetOperator(), nil
}