Skip to content

Commit

Permalink
feat(halo/evmstaking2): unit tests for unhappy paths (#2591)
Browse files Browse the repository at this point in the history
More unit tests for unhappy paths.

issue: #2525

---------

Co-authored-by: Khalil Claybon <[email protected]>
  • Loading branch information
chmllr and kc1116 authored Dec 3, 2024
1 parent cd99b79 commit 3b2f469
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 84 deletions.
40 changes: 26 additions & 14 deletions halo/evmstaking2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,21 @@ func (k Keeper) parseAndDeliver(ctx context.Context, elog *evmenginetypes.EVMEve

switch ethlog.Topics[0] {
case createValidatorEvent.ID:
ev, err := k.contract.ParseCreateValidator(ethlog)
delegate, err := k.contract.ParseCreateValidator(ethlog)
if err != nil {
return errors.Wrap(err, "parse create validator")
}

if err := k.deliverCreateValidator(ctx, ev); err != nil {
if err := k.deliverCreateValidator(ctx, delegate); err != nil {
return errors.Wrap(err, "create validator")
}
case delegateEvent.ID:
ev, err := k.contract.ParseDelegate(ethlog)
delegate, err := k.contract.ParseDelegate(ethlog)
if err != nil {
return errors.Wrap(err, "parse delegate")
}

if err := k.deliverDelegate(ctx, ev); err != nil {
if err := k.deliverDelegate(ctx, delegate); err != nil {
return errors.Wrap(err, "delegate")
}
default:
Expand All @@ -229,8 +229,8 @@ func (k Keeper) parseAndDeliver(ctx context.Context, elog *evmenginetypes.EVMEve
//
// NOTE: if we error, the deposit is lost (on EVM). consider recovery methods.
func (k Keeper) deliverDelegate(ctx context.Context, ev *bindings.StakingDelegate) error {
if ev.Delegator != ev.Validator {
return errors.New("only self delegation")
if err := verifyStakingDelegate(ev); err != nil {
return err
}

delAddr := sdk.AccAddress(ev.Delegator.Bytes())
Expand Down Expand Up @@ -280,16 +280,16 @@ func (k Keeper) createAccIfNone(ctx context.Context, addr sdk.AccAddress) {
// - Create a new validator with the depositor's account.
//
// NOTE: if we error, the deposit is lost (on EVM). consider recovery methods.
func (k Keeper) deliverCreateValidator(ctx context.Context, ev *bindings.StakingCreateValidator) error {
pubkey, err := k1util.PubKeyBytesToCosmos(ev.Pubkey)
func (k Keeper) deliverCreateValidator(ctx context.Context, createValidator *bindings.StakingCreateValidator) error {
pubkey, err := k1util.PubKeyBytesToCosmos(createValidator.Pubkey)
if err != nil {
return errors.Wrap(err, "pubkey to cosmos")
}

accAddr := sdk.AccAddress(ev.Validator.Bytes())
valAddr := sdk.ValAddress(ev.Validator.Bytes())
accAddr := sdk.AccAddress(createValidator.Validator.Bytes())
valAddr := sdk.ValAddress(createValidator.Validator.Bytes())

amountCoin, amountCoins := omniToBondCoin(ev.Deposit)
amountCoin, amountCoins := omniToBondCoin(createValidator.Deposit)

if _, err := k.sKeeper.GetValidator(ctx, valAddr); err == nil {
return errors.New("validator already exists")
Expand All @@ -306,14 +306,14 @@ func (k Keeper) deliverCreateValidator(ctx context.Context, ev *bindings.Staking
}

log.Info(ctx, "EVM staking deposit detected, adding new validator",
"depositor", ev.Validator.Hex(),
"amount", ev.Deposit.String())
"depositor", createValidator.Validator.Hex(),
"amount", createValidator.Deposit.String())

msg, err := stypes.NewMsgCreateValidator(
valAddr.String(),
pubkey,
amountCoin,
stypes.Description{Moniker: ev.Validator.Hex()},
stypes.Description{Moniker: createValidator.Validator.Hex()},
stypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
math.NewInt(1)) // Stub out minimum self delegation for now, just use 1.
if err != nil {
Expand All @@ -327,3 +327,15 @@ func (k Keeper) deliverCreateValidator(ctx context.Context, ev *bindings.Staking

return nil
}

func verifyStakingDelegate(delegate *bindings.StakingDelegate) error {
if delegate.Delegator != delegate.Validator {
return errors.New("only self delegation")
}

if delegate.Amount == nil {
return errors.New("stake amount missing")
}

return nil
}
Loading

0 comments on commit 3b2f469

Please sign in to comment.