Skip to content

Commit

Permalink
refactor name of function ProcessClaim to CheckClaim
Browse files Browse the repository at this point in the history
  • Loading branch information
yutianwu committed Jan 9, 2023
1 parent e4c61b7 commit a630100
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
13 changes: 9 additions & 4 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,21 @@ func (k Keeper) GetRelayerParam(ctx sdk.Context) (uint64, uint64) {
}

func (k Keeper) IsValidatorInturn(ctx sdk.Context, validators []stakingtypes.Validator, claim *types.MsgClaim) (bool, error) {
fromAddress, err := sdk.AccAddressFromHexUnsafe(claim.FromAddress)
if err != nil {
return false, sdkerrors.Wrapf(types.ErrInvalidAddress, fmt.Sprintf("from address (%s) is invalid", claim.FromAddress))
}

var validatorIndex int64 = -1
for index, validator := range validators {
if validator.RelayerAddress == claim.FromAddress {
if validator.RelayerAddress == fromAddress.String() {
validatorIndex = int64(index)
break
}
}

if validatorIndex < 0 {
return false, sdkerrors.Wrapf(types.ErrNotValidator, fmt.Sprintf("sender is not validator"))
return false, sdkerrors.Wrapf(types.ErrNotRelayer, fmt.Sprintf("sender(%s) is not a relayer", fromAddress.String()))
}

// check inturn validator index
Expand All @@ -105,8 +110,8 @@ func (k Keeper) IsValidatorInturn(ctx sdk.Context, validators []stakingtypes.Val
return uint64(validatorIndex) == (inturnValidatorIndex+backoffIndex)%uint64(len(validators)), nil
}

// ProcessClaim checks the bls signature
func (k Keeper) ProcessClaim(ctx sdk.Context, claim *types.MsgClaim) error {
// CheckClaim checks the bls signature
func (k Keeper) CheckClaim(ctx sdk.Context, claim *types.MsgClaim) error {
historicalInfo, ok := k.StakingKeeper.GetHistoricalInfo(ctx, ctx.BlockHeight())
if !ok {
return sdkerrors.Wrapf(types.ErrValidatorSet, fmt.Sprintf("get historical validators failed"))
Expand Down
10 changes: 5 additions & 5 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func (s *TestSuite) TestProcessClaim() {
msgClaim.AggSignature = blsSig

s.ctx = s.ctx.WithBlockTime(time.Unix(int64(msgClaim.Timestamp), 0))
err := s.app.OracleKeeper.ProcessClaim(s.ctx, &msgClaim)
err := s.app.OracleKeeper.CheckClaim(s.ctx, &msgClaim)
s.Require().Nil(err, "error should be nil")

// not in turn
s.ctx = s.ctx.WithBlockTime(time.Unix(int64(msgClaim.Timestamp)+6, 0))
err = s.app.OracleKeeper.ProcessClaim(s.ctx, &msgClaim)
err = s.app.OracleKeeper.CheckClaim(s.ctx, &msgClaim)
s.Require().NotNil(err, "error should not be nil")
s.Require().Contains(err.Error(), "validator is not in turn")

Expand All @@ -121,7 +121,7 @@ func (s *TestSuite) TestProcessClaim() {
}
msgClaim.VoteAddressSet = wrongValBitSet.Bytes()
s.ctx = s.ctx.WithBlockTime(time.Unix(int64(msgClaim.Timestamp), 0))
err = s.app.OracleKeeper.ProcessClaim(s.ctx, &msgClaim)
err = s.app.OracleKeeper.CheckClaim(s.ctx, &msgClaim)
s.Require().NotNil(err, "error should not be nil")
s.Require().Contains(err.Error(), "number of validator set is larger than validators")

Expand All @@ -131,7 +131,7 @@ func (s *TestSuite) TestProcessClaim() {
wrongValBitSet.Set(uint(validatorMap[newValidators[1].RelayerAddress]))
msgClaim.VoteAddressSet = wrongValBitSet.Bytes()
s.ctx = s.ctx.WithBlockTime(time.Unix(int64(msgClaim.Timestamp), 0))
err = s.app.OracleKeeper.ProcessClaim(s.ctx, &msgClaim)
err = s.app.OracleKeeper.CheckClaim(s.ctx, &msgClaim)
s.Require().NotNil(err, "error should not be nil")
s.Require().Contains(err.Error(), "not enough validators voted")

Expand All @@ -140,7 +140,7 @@ func (s *TestSuite) TestProcessClaim() {
msgClaim.AggSignature = bytes.Repeat([]byte{2}, 96)

s.ctx = s.ctx.WithBlockTime(time.Unix(int64(msgClaim.Timestamp), 0))
err = s.app.OracleKeeper.ProcessClaim(s.ctx, &msgClaim)
err = s.app.OracleKeeper.CheckClaim(s.ctx, &msgClaim)
s.Require().NotNil(err, "error should not be nil")
s.Require().Contains(err.Error(), "BLS signature converts failed")
}
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k msgServer) Claim(goCtx context.Context, req *types.MsgClaim) (*types.Msg
return nil, sdkerrors.Wrapf(types.ErrInvalidReceiveSequence, fmt.Sprintf("current sequence of channel %d is %d", types.RelayPackagesChannelId, sequence))
}

err := k.oracleKeeper.ProcessClaim(ctx, req)
err := k.oracleKeeper.CheckClaim(ctx, req)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion x/oracle/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ var (
ErrBlsPubKey = sdkerrors.Register(ModuleName, 9, "public key is invalid")
ErrBlsVotesNotEnough = sdkerrors.Register(ModuleName, 10, "bls votes is not enough")
ErrInvalidBlsSignature = sdkerrors.Register(ModuleName, 11, "bls signature is invalid")
ErrNotValidator = sdkerrors.Register(ModuleName, 12, "sender is not validator")
ErrNotRelayer = sdkerrors.Register(ModuleName, 12, "sender is not a relayer")
ErrValidatorNotInTurn = sdkerrors.Register(ModuleName, 13, "validator is not in turn")
ErrInvalidDestChainId = sdkerrors.Register(ModuleName, 14, "dest chain id is invalid")
ErrInvalidSrcChainId = sdkerrors.Register(ModuleName, 15, "src chain id is invalid")
ErrInvalidAddress = sdkerrors.Register(ModuleName, 16, "address is invalid")
)

0 comments on commit a630100

Please sign in to comment.