From a630100cecfa86b9d4d028b8cc093865a4392e5c Mon Sep 17 00:00:00 2001 From: yutianwu Date: Mon, 9 Jan 2023 15:23:21 +0800 Subject: [PATCH] refactor name of function ProcessClaim to CheckClaim --- x/oracle/keeper/keeper.go | 13 +++++++++---- x/oracle/keeper/keeper_test.go | 10 +++++----- x/oracle/keeper/msg_server.go | 2 +- x/oracle/types/errors.go | 3 ++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index 051ec7c189..167ca820b0 100644 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -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 @@ -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")) diff --git a/x/oracle/keeper/keeper_test.go b/x/oracle/keeper/keeper_test.go index c54ad6fc54..c668fca5a6 100644 --- a/x/oracle/keeper/keeper_test.go +++ b/x/oracle/keeper/keeper_test.go @@ -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") @@ -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") @@ -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") @@ -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") } diff --git a/x/oracle/keeper/msg_server.go b/x/oracle/keeper/msg_server.go index c05292ec1e..3e72d46553 100644 --- a/x/oracle/keeper/msg_server.go +++ b/x/oracle/keeper/msg_server.go @@ -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 } diff --git a/x/oracle/types/errors.go b/x/oracle/types/errors.go index 71aa61c235..30ac76fe9f 100644 --- a/x/oracle/types/errors.go +++ b/x/oracle/types/errors.go @@ -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") )