Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(halo/evmstaking2): unit tests for unhappy paths #2591

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading