Skip to content

Commit

Permalink
Merge branch 'feat/genesis-operator'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Apr 5, 2024
2 parents 7059f78 + ace29eb commit 83401f9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require (
github.com/rs/cors v1.9.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/zondax/hid v0.9.1 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.23.0 // indirect
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,8 @@ golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
9 changes: 5 additions & 4 deletions x/assets/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,13 @@ func (gs GenesisState) Validate() error {
assetID, info,
)
}
// check that the withdrawable amount is not greater than the total deposit amount.
// since withdrawable amount should be less than or equal to the amount deposited.
if info.WithdrawableAmount.GT(info.TotalDepositAmount) {
// check that the withdrawable amount and the deposited amount are equal.
// this is because this module's genesis only sets up free deposits.
// the delegation module bonds them, thereby altering the withdrawable amount.
if !info.WithdrawableAmount.Equal(info.TotalDepositAmount) {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"withdrawable amount exceeds total deposit amount for %s: %+v",
"withdrawable amount is not equal to total deposit amount for %s: %+v",
assetID, info,
)
}
Expand Down
5 changes: 4 additions & 1 deletion x/assets/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
AssetID: assetID,
Info: types.StakerAssetInfo{
TotalDepositAmount: math.NewInt(100),
WithdrawableAmount: math.NewInt(0),
WithdrawableAmount: math.NewInt(100),
WaitUnbondingAmount: math.NewInt(0),
},
},
Expand Down Expand Up @@ -493,9 +493,12 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
malleate: func(gs *types.GenesisState) {
gs.Deposits[0].Deposits[0].Info.TotalDepositAmount =
stakingInfo.AssetBasicInfo.TotalSupply.Add(math.NewInt(1))
gs.Deposits[0].Deposits[0].Info.WithdrawableAmount =
stakingInfo.AssetBasicInfo.TotalSupply.Add(math.NewInt(1))
},
unmalleate: func(gs *types.GenesisState) {
gs.Deposits[0].Deposits[0].Info.TotalDepositAmount = math.NewInt(100)
gs.Deposits[0].Deposits[0].Info.WithdrawableAmount = math.NewInt(100)
},
},
{
Expand Down
33 changes: 24 additions & 9 deletions x/operator/keeper/consensus_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,26 @@ func (k *Keeper) SetOperatorConsKeyForChainID(
chainID string,
// should be tm-ed25519
consKey *tmprotocrypto.PublicKey,
) error {
return k.setOperatorConsKeyForChainID(ctx, opAccAddr, chainID, consKey, false)
}

// setOperatorConsKeyForChainID is the private version of SetOperatorConsKeyForChainID.
// it is used with a boolean flag to indicate that the call is from genesis.
// if so, operator freeze status is not checked and hooks are not called.
func (k *Keeper) setOperatorConsKeyForChainID(
ctx sdk.Context,
opAccAddr sdk.AccAddress,
chainID string,
consKey *tmprotocrypto.PublicKey,
genesis bool,
) error {
// check if we are an operator
if !k.IsOperator(ctx, opAccAddr) {
return delegationtypes.ErrOperatorNotExist
}
// check for slashing
if k.slashKeeper.IsOperatorFrozen(ctx, opAccAddr) {
if !genesis && k.slashKeeper.IsOperatorFrozen(ctx, opAccAddr) {
return delegationtypes.ErrOperatorIsFrozen
}
// check if the chain id is valid
Expand Down Expand Up @@ -102,21 +115,23 @@ func (k *Keeper) SetOperatorConsKeyForChainID(
}
}
}
k.setOperatorConsKeyForChainID(ctx, opAccAddr, consAddr, chainID, bz)
if found {
if !alreadyRecorded {
k.Hooks().AfterOperatorKeyReplacement(ctx, opAccAddr, prevKey, consKey, chainID)
k.setOperatorConsKeyForChainIDUnchecked(ctx, opAccAddr, consAddr, chainID, bz)
if !genesis {
if found {
if !alreadyRecorded {
k.Hooks().AfterOperatorKeyReplacement(ctx, opAccAddr, prevKey, consKey, chainID)
}
} else {
k.Hooks().AfterOperatorOptIn(ctx, opAccAddr, chainID, consKey)
}
} else {
k.Hooks().AfterOperatorOptIn(ctx, opAccAddr, chainID, consKey)
}
return nil
}

// setOperatorConsKeyForChainID is the internal private version. It performs
// setOperatorConsKeyForChainIDUnchecked is the internal private version. It performs
// no error checking of the input. The caller must do the error checking
// and then call this function.
func (k Keeper) setOperatorConsKeyForChainID(
func (k Keeper) setOperatorConsKeyForChainIDUnchecked(
ctx sdk.Context, opAccAddr sdk.AccAddress, consAddr sdk.ConsAddress,
chainID string, bz []byte,
) {
Expand Down
4 changes: 2 additions & 2 deletions x/operator/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) []abci.Va
// #nosec G703 // already validated
key, _ := types.HexStringToPubKey(detail.ConsensusKey)
// then set pub key
if err := k.SetOperatorConsKeyForChainID(
ctx, operatorAddr, detail.ChainID, key,
if err := k.setOperatorConsKeyForChainID(
ctx, operatorAddr, detail.ChainID, key, true,
); err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/operator/keeper/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k *Keeper) SetOperatorInfo(ctx sdk.Context, addr string, info *operatortyp

// key := common.HexToAddress(incentive.Contract)
bz := k.cdc.MustMarshal(info)

// TODO: check about client chain registration for earnings addresses provided?
store.Set(opAccAddr, bz)
return nil
}
Expand Down

0 comments on commit 83401f9

Please sign in to comment.