Skip to content

Commit

Permalink
imp(staking): only allow users to directly call the StakingI contract…
Browse files Browse the repository at this point in the history
…'s address which is 0x0000000000000000000000000000000000000800 to create a new validator
  • Loading branch information
luchenqun committed Nov 13, 2023
1 parent edb1355 commit 4afa195
Showing 1 changed file with 2 additions and 44 deletions.
46 changes: 2 additions & 44 deletions precompiles/staking/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const (
RedelegateAuthz = stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_REDELEGATE
// CancelUnbondingDelegationAuthz defines the authorization type for the staking
CancelUnbondingDelegationAuthz = stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_CANCEL_UNBONDING_DELEGATION
// CreateValidatorAuthz defines the authorization type for the create validator
CreateValidatorAuthz = 5 // TODO: define cosmos sdk
)

// CreateValidator performs create validator.
Expand Down Expand Up @@ -74,62 +72,22 @@ func (p Precompile) CreateValidator(
),
)

var (
// stakeAuthz is the authorization grant for the caller and the delegator address
stakeAuthz *stakingtypes.StakeAuthorization
// expiration is the expiration time of the authorization grant
expiration *time.Time

// isCallerOrigin is true when the contract caller is the same as the origin
isCallerOrigin = contract.CallerAddress == origin
// isCallerDelegator is true when the contract caller is the same as the delegator
isCallerDelegator = contract.CallerAddress == delegatorHexAddr
)

// The provided delegator address should always be equal to the origin address.
// In case the contract caller address is the same as the delegator address provided,
// update the delegator address to be equal to the origin address.
// Otherwise, if the provided delegator address is different from the origin address,
// return an error because is a forbidden operation
if isCallerDelegator {
delegatorHexAddr = origin
} else if origin != delegatorHexAddr {
// We only allow users to directly call the StakingI contract's address which is 0x0000000000000000000000000000000000000800 to create a new validator.
if origin != delegatorHexAddr {
return nil, fmt.Errorf(ErrDifferentOriginFromDelegator, origin.String(), delegatorHexAddr.String())
}

Check warning on line 78 in precompiles/staking/tx.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/tx.go#L62-L78

Added lines #L62 - L78 were not covered by tests

// no need to have authorization when the contract caller is the same as origin (owner of funds)
if !isCallerOrigin {
// Check if the authorization grant exists for the caller and the origin
stakeAuthz, expiration, err = authorization.CheckAuthzAndAllowanceForGranter(ctx, p.AuthzKeeper, contract.CallerAddress, delegatorHexAddr, &msg.Value, DelegateMsg)
if err != nil {
return nil, err
}
}

// Execute the transaction using the message server
msgSrv := stakingkeeper.NewMsgServerImpl(&p.stakingKeeper)
if _, err = msgSrv.CreateValidator(sdk.WrapSDKContext(ctx), msg); err != nil {
return nil, err
}

Check warning on line 84 in precompiles/staking/tx.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/tx.go#L81-L84

Added lines #L81 - L84 were not covered by tests

// Only update the authorization if the contract caller is different from the origin
if !isCallerOrigin {
if err := p.UpdateStakingAuthorization(ctx, contract.CallerAddress, delegatorHexAddr, stakeAuthz, expiration, DelegateMsg, msg); err != nil {
return nil, err
}
}

// Emit the event for the delegate transaction
if err = p.EmitCreateValidatorEvent(ctx, stateDB, msg, delegatorHexAddr); err != nil {
return nil, err
}

Check warning on line 89 in precompiles/staking/tx.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/tx.go#L87-L89

Added lines #L87 - L89 were not covered by tests

// NOTE: This ensures that the changes in the bank keeper are correctly mirrored to the EVM stateDB.
// This prevents the stateDB from overwriting the changed balance in the bank keeper when committing the EVM state.
if isCallerDelegator {
stateDB.(*statedb.StateDB).SubBalance(contract.CallerAddress, msg.Value.Amount.BigInt())
}

return method.Outputs.Pack(true)

Check warning on line 91 in precompiles/staking/tx.go

View check run for this annotation

Codecov / codecov/patch

precompiles/staking/tx.go#L91

Added line #L91 was not covered by tests
}

Expand Down

0 comments on commit 4afa195

Please sign in to comment.