-
Notifications
You must be signed in to change notification settings - Fork 39
/
pubkey_replacement.go
120 lines (92 loc) · 3.79 KB
/
pubkey_replacement.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package legacy
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
cryptocodec "github.com/cometbft/cometbft/crypto/encoding"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
)
type replacementConfigs []replacementConfig
func (r *replacementConfigs) isReplacedValidator(validatorAddress string) (int, replacementConfig) {
for i, replacement := range *r {
if replacement.ValidatorAddress == validatorAddress {
return i, replacement
}
}
return -1, replacementConfig{}
}
type replacementConfig struct {
ValidatorName string `json:"validator_name"`
ValidatorAddress string `json:"validator_address"`
ConsensusPubkey string `json:"stargate_consensus_public_key"`
}
func loadKeydataFromFile(clientCtx client.Context, replacementrJSON string, genDoc *tmtypes.GenesisDoc) *tmtypes.GenesisDoc {
jsonReplacementBlob, err := os.ReadFile(replacementrJSON)
if err != nil {
log.Fatal(errors.Wrapf(err, "failed to read replacement keys from file %s", replacementrJSON))
}
var replacementKeys replacementConfigs
err = json.Unmarshal(jsonReplacementBlob, &replacementKeys)
if err != nil {
log.Fatal(errors.Wrap(err, "Could not unmarshal replacement keys "))
}
var state types.AppMap
if err := json.Unmarshal(genDoc.AppState, &state); err != nil {
log.Fatal(errors.Wrap(err, "failed to JSON unmarshal initial genesis state"))
}
var stakingGenesis staking.GenesisState
var slashingGenesis slashing.GenesisState
clientCtx.Codec.MustUnmarshalJSON(state[staking.ModuleName], &stakingGenesis)
clientCtx.Codec.MustUnmarshalJSON(state[slashing.ModuleName], &slashingGenesis)
for i, val := range stakingGenesis.Validators {
idx, replacement := replacementKeys.isReplacedValidator(val.OperatorAddress)
if idx != -1 {
toReplaceValConsAddress, _ := val.GetConsAddr()
var consPubKey cryptotypes.PubKey
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(replacement.ConsensusPubkey), &consPubKey); err != nil {
log.Fatal(fmt.Errorf("failed to decode key:%s %w", replacement.ConsensusPubkey, err))
}
val.ConsensusPubkey, err = codectypes.NewAnyWithValue(consPubKey)
if err != nil {
log.Fatal(fmt.Errorf("failed to decode key:%s %w", consPubKey, err))
}
replaceValConsAddress, _ := val.GetConsAddr()
protoReplaceValConsPubKey, _ := val.TmConsPublicKey()
replaceValConsPubKey, _ := cryptocodec.PubKeyFromProto(protoReplaceValConsPubKey)
for i, signingInfo := range slashingGenesis.SigningInfos {
if signingInfo.Address == toReplaceValConsAddress.String() {
slashingGenesis.SigningInfos[i].Address = replaceValConsAddress.String()
slashingGenesis.SigningInfos[i].ValidatorSigningInfo.Address = replaceValConsAddress.String()
}
}
for i, missedInfo := range slashingGenesis.MissedBlocks {
if missedInfo.Address == toReplaceValConsAddress.String() {
slashingGenesis.MissedBlocks[i].Address = replaceValConsAddress.String()
}
}
for tmIdx, tmval := range genDoc.Validators {
if bytes.Equal(tmval.Address.Bytes(), toReplaceValConsAddress.Bytes()) {
genDoc.Validators[tmIdx].Address = replaceValConsAddress.Bytes()
genDoc.Validators[tmIdx].PubKey = replaceValConsPubKey
}
}
stakingGenesis.Validators[i] = val
}
}
state[staking.ModuleName] = clientCtx.Codec.MustMarshalJSON(&stakingGenesis)
state[slashing.ModuleName] = clientCtx.Codec.MustMarshalJSON(&slashingGenesis)
genDoc.AppState, err = json.Marshal(state)
if err != nil {
log.Fatal("Could not marshal App State")
}
return genDoc
}