|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "cosmossdk.io/math" |
| 6 | + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 9 | +) |
| 10 | + |
| 11 | +type ComputeResult struct { |
| 12 | + Power int64 |
| 13 | + ValidatorPubKey cryptotypes.PubKey |
| 14 | + OperatorAddress string |
| 15 | +} |
| 16 | + |
| 17 | +func (k Keeper) SetComputeValidators(ctx context.Context, computeResults []ComputeResult) ([]types.Validator, error) { |
| 18 | + server := NewMsgServerImpl(&k) |
| 19 | + logger := k.Logger(ctx) |
| 20 | + resultsMap := make(map[string]ComputeResult) |
| 21 | + for _, result := range computeResults { |
| 22 | + resultsMap[result.ValidatorPubKey.String()] = result |
| 23 | + } |
| 24 | + |
| 25 | + currentValidators, err := k.GetAllValidators(ctx) |
| 26 | + if err != nil { |
| 27 | + logger.Error("error getting current validators", "error", err.Error()) |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + validatorsAlreadyExisting := make(map[string]bool) |
| 32 | + for _, validator := range currentValidators { |
| 33 | + conPubKey, err := validator.ConsPubKey() |
| 34 | + if err != nil { |
| 35 | + logger.Error("Error getting cons pubkey", "error", err.Error()) |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + validatorsAlreadyExisting[conPubKey.String()] = true |
| 39 | + } |
| 40 | + |
| 41 | + // Handle validators already in |
| 42 | + for _, validator := range currentValidators { |
| 43 | + conPubKey, err := validator.ConsPubKey() |
| 44 | + if err != nil { |
| 45 | + logger.Error("Error getting cons pubkey", "error", err.Error()) |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + computeResult, inNewResults := resultsMap[conPubKey.String()] |
| 49 | + if inNewResults { |
| 50 | + logger.Info("Updating validator", "operator", validator.GetOperator(), "power", computeResult.Power) |
| 51 | + // update to new power |
| 52 | + validator.Tokens = math.NewInt(computeResult.Power) |
| 53 | + err := k.SetValidator(ctx, validator) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + } else { |
| 58 | + logger.Info("Removing validator", "operator", validator.GetOperator(), "power", computeResult.Power) |
| 59 | + // update to new power |
| 60 | + validator.Tokens = math.NewInt(0) |
| 61 | + err := k.SetValidator(ctx, validator) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Handle validators not in |
| 69 | + for _, computeResult := range computeResults { |
| 70 | + if _, ok := validatorsAlreadyExisting[computeResult.ValidatorPubKey.String()]; !ok { |
| 71 | + logger.Info("Creating validator", "power", computeResult, "operator", computeResult.OperatorAddress) |
| 72 | + _, err := k.createValidator(ctx, computeResult, server) |
| 73 | + if err != nil { |
| 74 | + logger.Error("Error creating validator", "error", err.Error()) |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return k.GetAllValidators(ctx) |
| 80 | +} |
| 81 | + |
| 82 | +func (k Keeper) createValidator(ctx context.Context, computeResult ComputeResult, server types.MsgServer) (*types.Validator, error) { |
| 83 | + logger := k.Logger(ctx) |
| 84 | + s := computeResult.ValidatorPubKey.Address().String() |
| 85 | + newValAddr, err := sdk.ValAddressFromHex(s) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + logger.Error("Error converting pubkey to val address", "error", err.Error()) |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + denom, err := k.BondDenom(ctx) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + createValidatorMsg, err := types.NewMsgCreateValidator( |
| 96 | + newValAddr.String(), |
| 97 | + computeResult.ValidatorPubKey, |
| 98 | + sdk.NewCoin(denom, math.NewInt(computeResult.Power)), |
| 99 | + types.Description{ |
| 100 | + Moniker: computeResult.OperatorAddress, |
| 101 | + Details: "Created after Proof of Compute", |
| 102 | + }, |
| 103 | + |
| 104 | + types.CommissionRates{ |
| 105 | + Rate: math.LegacyMustNewDecFromStr("0.1"), |
| 106 | + MaxRate: math.LegacyMustNewDecFromStr("0.2"), |
| 107 | + MaxChangeRate: math.LegacyMustNewDecFromStr("0.01"), |
| 108 | + }, |
| 109 | + math.NewInt(1), |
| 110 | + ) |
| 111 | + // I think the Go fanatics are off their rocker. This is a lot of boilerplate. |
| 112 | + if err != nil { |
| 113 | + logger.Error("Error creating validator message", "error", err.Error()) |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + _, err = server.CreateValidator(ctx, createValidatorMsg) |
| 117 | + if err != nil { |
| 118 | + logger.Error("Error creating validator", "error", err.Error()) |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + newVal, err := k.GetValidator(ctx, newValAddr) |
| 122 | + if err != nil { |
| 123 | + logger.Error("Error getting created validator", "error", err.Error()) |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + logger.Info("Created validator", "validator", newVal.String()) |
| 127 | + bondedVal, err := k.bondValidator(ctx, newVal) |
| 128 | + if err != nil { |
| 129 | + logger.Error("Error bonding validator", "error", err.Error()) |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + logger.Info("Bonded validator", "validator", bondedVal.String()) |
| 133 | + return &bondedVal, nil |
| 134 | +} |
0 commit comments