@@ -4,39 +4,46 @@ import (
4
4
"encoding/base64"
5
5
"fmt"
6
6
"log"
7
+ "time"
7
8
8
9
sdk "github.com/cosmos/cosmos-sdk/types"
9
10
"github.com/cosmos/cosmos-sdk/types/module"
10
11
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
11
12
12
13
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
13
14
14
- sdkerrors "cosmossdk.io/errors"
15
15
"cosmossdk.io/math"
16
16
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
17
+ "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
17
18
18
19
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
19
20
20
21
"github.com/ODIN-PROTOCOL/odin-core/app/keepers"
21
22
"github.com/ODIN-PROTOCOL/odin-core/app/upgrades"
22
- errortypes "github.com/cosmos/cosmos-sdk/types/errors"
23
23
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
24
24
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
25
25
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
26
+ distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
27
+ slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
28
+ slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
26
29
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
27
30
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
28
31
)
29
32
30
- // Old addresses
31
- const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk"
32
- const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx"
33
+ const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396"
34
+ const DefiantLabAccAddress = "odin16dcwlyrwx8duucsr363zqqsf2prc5gv52uv6zk" // Prod
35
+ // const DefiantLabAccAddress = "odin1t6hn2c9hrc33fa5slh9wtvv4ew2qhygl0rmc4q"
33
36
34
- // New addresses
35
- const DefiantLabOldAccAddress = "odin1dnmz4yzv73lr3lmauuaa0wpwn8zm8s20fyv396 "
37
+ //const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
38
+ const OdinMainnet3OldAccAddress = "odin1s0p07h5n4v2nqh0jr2gprq5cphv2mgs9twppcx "
36
39
const OdinMainnet3NewAccAddress = "odin1hgdq6yekx3hpz5mhph660el664pc02a4npxdas"
37
40
38
41
// PubKeys
39
- const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s="
42
+ const OdinMainnet3ValPubKey = "FQf4cxaS5XNv+mFEi6dtDQDOLUWVWfEyh8SqljsJz1s=" // Prod
43
+ //const OdinMainnet3ValPubKey = "f7pqqa+1Rkl+5j13R6iBnnKAR7bhNrOV8Cc0RfpSzjs=" // Test
44
+
45
+ const DefiantLabPubKey = "Aw22yXnDmYKzQ1CeHh6A+PD1043vsbSBH5FmuAWIlkS7" // Prod
46
+ // const DefiantLabPubKey = "A8gI+6AHMv9Tg37JyrxSP16hUH76Umr4krXfIEqOQJMo" // Test
40
47
41
48
42
49
func getBalance (
@@ -60,6 +67,30 @@ func getBalance(
60
67
}
61
68
}
62
69
70
+ func CreateNewAccount (ctx sdk.Context , authKeeper keeper.AccountKeeper , address sdk.AccAddress , secpPubKey []byte ) error {
71
+ // Check if the account already exists
72
+ account := authKeeper .GetAccount (ctx , address )
73
+ if account != nil {
74
+ return fmt .Errorf ("account %s already exists" , address .String ())
75
+ }
76
+
77
+ // Optionally, set any initial values or parameters for the new account
78
+ // For example, you might want to set an initial balance using the bank module
79
+ var pubkey cryptotypes.PubKey = & secp256k1.PubKey {Key : secpPubKey }
80
+
81
+ // Create a new account with the address and public key
82
+ newAccount := authKeeper .NewAccountWithAddress (ctx , address )
83
+ if newAccount == nil {
84
+ return fmt .Errorf ("failed to create new account for address %s" , address .String ())
85
+ }
86
+
87
+ newAccount .SetPubKey (pubkey )
88
+ log .Printf ("New account created %v: %v" , address .String (), pubkey .String ())
89
+
90
+ // Save the new account to the state
91
+ authKeeper .SetAccount (ctx , newAccount )
92
+ return nil
93
+ }
63
94
64
95
func getDelegations (
65
96
ctx sdk.Context ,
@@ -70,8 +101,34 @@ func getDelegations(
70
101
return delegations
71
102
}
72
103
104
+ func InitializeValidatorSigningInfo (ctx sdk.Context , slashingKeeper slashingkeeper.Keeper , stakingKeeper stakingkeeper.Keeper , consAddr sdk.ConsAddress ) error {
105
+ // Check if signing info already exists to avoid overwriting it
106
+ _ , found := slashingKeeper .GetValidatorSigningInfo (ctx , consAddr )
107
+ if ! found {
108
+ startHeight := ctx .BlockHeight ()
109
+ signingInfo := slashingtypes .NewValidatorSigningInfo (consAddr , startHeight , 0 , time .Unix (0 , 0 ), false , 0 )
110
+ slashingKeeper .SetValidatorSigningInfo (ctx , consAddr , signingInfo )
111
+ }
112
+ return nil
113
+ }
114
+
115
+
116
+ func InitializeValidatorDistributionInfo (ctx sdk.Context , keepers * keepers.AppKeepers , validatorAddr sdk.ValAddress ) {
117
+ // Initialize distribution information for the validator
118
+ // set initial historical rewards (period 0) with reference count of 1
119
+ keepers .DistrKeeper .SetValidatorHistoricalRewards (ctx , validatorAddr , 0 , distrtypes .NewValidatorHistoricalRewards (sdk.DecCoins {}, 1 ))
120
+
121
+ // set current rewards (starting at period 1)
122
+ keepers .DistrKeeper .SetValidatorCurrentRewards (ctx , validatorAddr , distrtypes .NewValidatorCurrentRewards (sdk.DecCoins {}, 1 ))
73
123
74
- func createValidator (ctx sdk.Context , sk stakingkeeper.Keeper , dk distributionkeeper.Keeper , address string , pubKey cryptotypes.PubKey , description stakingtypes.Description , comission stakingtypes.Commission ) (stakingtypes.Validator , error ){
124
+ // set accumulated commission
125
+ keepers .DistrKeeper .SetValidatorAccumulatedCommission (ctx , validatorAddr , distrtypes .InitialValidatorAccumulatedCommission ())
126
+
127
+ // set outstanding rewards
128
+ keepers .DistrKeeper .SetValidatorOutstandingRewards (ctx , validatorAddr , distrtypes.ValidatorOutstandingRewards {Rewards : sdk.DecCoins {}})
129
+ }
130
+
131
+ func createValidator (ctx sdk.Context , keeppers * keepers.AppKeepers , address string , pubKey cryptotypes.PubKey , description stakingtypes.Description , comission stakingtypes.Commission ) (stakingtypes.Validator , error ){
75
132
76
133
valAddr := sdk .ValAddress (address )
77
134
minSelfDelegation := sdk .OneInt ()
@@ -90,14 +147,27 @@ func createValidator(ctx sdk.Context, sk stakingkeeper.Keeper, dk distributionke
90
147
validator .Commission = comission
91
148
92
149
// Update validators in the store
93
- sk .SetValidator (ctx , validator )
150
+ keeppers .StakingKeeper .SetValidator (ctx , validator )
151
+
152
+ consAddr := sdk .ConsAddress (pubKey .Address ())
153
+ valconsAddr , err := validator .GetConsAddr ()
154
+ if err != nil {
155
+ log .Printf ("Error when converting validator consensus address to string: %s" , err )
156
+ return stakingtypes.Validator {}, err
157
+ }
158
+
159
+ log .Printf ("Created validator %v (%v:%v)" , valAddr .String (), consAddr .String (), valconsAddr )
160
+
161
+ keeppers .StakingKeeper .SetValidatorByConsAddr (ctx , validator )
162
+ InitializeValidatorSigningInfo (ctx , keeppers .SlashingKeeper , * keeppers .StakingKeeper , consAddr )
163
+ InitializeValidatorDistributionInfo (ctx , keeppers , valAddr )
94
164
95
- err = sk .Hooks ().AfterValidatorCreated (ctx , valAddr )
165
+ err = keeppers . StakingKeeper .Hooks ().AfterValidatorCreated (ctx , valAddr )
96
166
if err != nil {
97
167
return stakingtypes.Validator {}, err
98
168
}
99
169
100
- err = dk .Hooks ().AfterValidatorCreated (ctx , valAddr )
170
+ err = keeppers . DistrKeeper .Hooks ().AfterValidatorCreated (ctx , valAddr )
101
171
if err != nil {
102
172
return stakingtypes.Validator {}, err
103
173
}
@@ -178,13 +248,10 @@ func moveValidatorDelegations(ctx sdk.Context, k stakingkeeper.Keeper, d distrib
178
248
179
249
k .AddValidatorTokensAndShares (ctx , newVal , tokens .TruncateInt ())
180
250
k .RemoveValidatorTokensAndShares (ctx , oldVal , cumOldValShares )
181
-
182
- k .SetValidatorByConsAddr (ctx , oldVal )
183
- k .SetValidatorByConsAddr (ctx , newVal )
184
-
185
251
return nil
186
252
}
187
253
254
+
188
255
func moveDelegations (ctx sdk.Context , keepers * keepers.AppKeepers , oldAddress sdk.AccAddress , newVal stakingtypes.Validator ) error {
189
256
for _ , delegation := range getDelegations (ctx , * keepers .StakingKeeper , oldAddress ) {
190
257
log .Printf ("Moving delegation from %v to %v" , delegation .DelegatorAddress , newVal .OperatorAddress )
@@ -201,13 +268,13 @@ func moveDelegations(ctx sdk.Context, keepers *keepers.AppKeepers, oldAddress sd
201
268
log .Printf ("Error when running hook after adding delegation %v to %v" , delegation .GetDelegatorAddr (), newVal .GetOperator ())
202
269
return err
203
270
}
204
- keepers .StakingKeeper .SetDelegation (ctx , newDelegation )
205
-
271
+
206
272
err = keepers .DistrKeeper .Hooks ().BeforeDelegationCreated (ctx , delegation .GetDelegatorAddr (), newVal .GetOperator ())
207
273
if err != nil {
208
274
log .Printf ("Error when running hook after addig delegation %v to %v" , delegation .GetDelegatorAddr (), newVal .GetOperator ())
209
275
return err
210
276
}
277
+ keepers .StakingKeeper .SetDelegation (ctx , newDelegation )
211
278
}
212
279
return nil
213
280
}
@@ -262,40 +329,15 @@ func sendCoins(
262
329
}
263
330
264
331
265
- func SelfDelegate (ctx sdk.Context , stakingKeeper stakingkeeper.Keeper , bankKeeper bankkeeper.Keeper , delegatorAddr sdk.AccAddress , validator stakingtypes.Validator , amount sdk.Coins ) error {
266
- // Delegate tokens to the validator
267
- for _ , balance := range amount {
268
-
269
- // Ensure the delegator (validator account) has enough balance for the delegation
270
- if ! bankKeeper .HasBalance (ctx , delegatorAddr , balance ) {
271
- return sdkerrors .Wrapf (errortypes .ErrInsufficientFunds , "not enough balance to self-delegate to validator: %s" , validator .OperatorAddress )
272
- }
273
-
274
- // Send coins from the delegator's account to the module account (staking module account) as part of delegation
275
- err := bankKeeper .SendCoinsFromAccountToModule (ctx , delegatorAddr , stakingtypes .NotBondedPoolName , amount )
276
- if err != nil {
277
- return err
278
- }
279
-
280
- _ , err = stakingKeeper .Delegate (ctx , delegatorAddr , balance .Amount , stakingtypes .Unbonded , validator , false )
281
- if err != nil {
282
- return err
283
- }
284
- }
285
- return nil
286
- }
287
-
288
-
289
332
func fixDefiantLabs (ctx sdk.Context , keepers * keepers.AppKeepers ) (error ) {
290
-
291
333
292
334
// Fixing self delegation
293
335
DefiantLabsValAddress , err := addrToValAddr (DefiantLabAccAddress )
294
336
if err != nil {
295
337
return err
296
338
}
297
339
298
- DefiantLabsVal , found := keepers .StakingKeeper .GetValidator (ctx , DefiantLabsValAddress )
340
+ DanVal , found := keepers .StakingKeeper .GetValidator (ctx , DefiantLabsValAddress )
299
341
if ! found {
300
342
log .Printf ("Validator with %v has not been found" , DefiantLabsValAddress )
301
343
return err
@@ -313,22 +355,33 @@ func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
313
355
return err
314
356
}
315
357
316
- // Setting withdrawal address
358
+ PubKeyBytes , err := base64 .StdEncoding .DecodeString (DefiantLabPubKey )
359
+ if err != nil {
360
+ log .Printf ("Error whend decoding public key from string %v" , err )
361
+ return err
362
+ }
363
+
364
+ err = CreateNewAccount (ctx , keepers .AccountKeeper , DefiantLabsAcc , PubKeyBytes )
365
+ if err != nil {
366
+ log .Printf ("Error when creating new account for %v: %s" , DefiantLabsAcc , err )
367
+ }
368
+
317
369
keepers .DistrKeeper .SetWithdrawAddr (ctx , DefiantLabsAcc , DefiantLabsAcc )
370
+
371
+ // sending balances
372
+ ctx .Logger ().Info (fmt .Sprintf ("Sending tokens from %s to %s" , DefiantLabOldAccAddress , DefiantLabAccAddress ))
318
373
319
- // Moving DefiantLabs self-delegation
320
- err = moveSelfDelegation (ctx , keepers , DefiantLabsOldAcc , DefiantLabsAcc , DefiantLabsValAddress )
374
+ balance , err := getBalance (ctx , * keepers .StakingKeeper , keepers .AccountKeeper , keepers .BankKeeper , DefiantLabsOldAcc )
321
375
if err != nil {
322
- log .Printf ("Error when moving self delegation %s" , err )
376
+ log .Printf ("Error when retrieving balance for address %s: %s" , DefiantLabOldAccAddress , err )
323
377
return err
324
378
}
325
-
379
+ sendCoins (ctx , keepers .BankKeeper , DefiantLabsOldAcc , DefiantLabsAcc , balance )
380
+
326
381
// Moving delegations
327
- moveDelegations (ctx , keepers , DefiantLabsOldAcc , DefiantLabsVal )
382
+ moveSelfDelegation (ctx , keepers , DefiantLabsOldAcc , DefiantLabsAcc , DefiantLabsValAddress )
383
+ moveDelegations (ctx , keepers , DefiantLabsOldAcc , DanVal )
328
384
329
- DefiantLabsVal .Jailed = false
330
- keepers .StakingKeeper .SetValidator (ctx , DefiantLabsVal )
331
-
332
385
return nil
333
386
}
334
387
@@ -382,7 +435,7 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
382
435
return err
383
436
}
384
437
385
- Odin3Val , err := createValidator (ctx , * keepers . StakingKeeper , keepers . DistrKeeper , string (Odin3ValAddr ), & Odin3PubKey , Odin3OldVal .Description , Odin3OldVal .Commission )
438
+ Odin3Val , err := createValidator (ctx , keepers , string (Odin3ValAddr ), & Odin3PubKey , Odin3OldVal .Description , Odin3OldVal .Commission )
386
439
if err != nil {
387
440
return err
388
441
}
@@ -393,13 +446,22 @@ func fixMainnet3(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
393
446
if err != nil {
394
447
return err
395
448
}
449
+
450
+ moveSelfDelegation (ctx , keepers , OldMainnet3Addr , NewMainnet3Addr , Odin3ValAddr )
396
451
moveDelegations (ctx , keepers , sdk .AccAddress (OdinMainnet3OldAccAddress ), Odin3Val )
397
452
398
453
Odin3Val .UpdateStatus (stakingtypes .Bonded )
399
454
400
455
// rewards and comission
401
456
withdrawRewardsAndCommission (ctx , * keepers .StakingKeeper , keepers .DistrKeeper , Odin3OldValAddress , Odin3ValAddr )
402
- Odin3OldVal .UpdateStatus (stakingtypes .Unbonded )
457
+ ctx .Logger ().Info (fmt .Sprintf ("Sending tokens from %s to %s" , OldMainnet3Addr , NewMainnet3Addr ))
458
+ balance , err = getBalance (ctx , * keepers .StakingKeeper , keepers .AccountKeeper , keepers .BankKeeper , OldMainnet3Addr )
459
+ if err != nil {
460
+ log .Printf ("Error when retrieving balance for address %s: %s" , OldMainnet3Addr , err )
461
+ return err
462
+ }
463
+ // sending balances
464
+ sendCoins (ctx , keepers .BankKeeper , OldMainnet3Addr , NewMainnet3Addr , balance )
403
465
404
466
// Showing all validator powers
405
467
log .Printf ("Validator power after update:" )
0 commit comments