Skip to content

Commit

Permalink
Fix validator out test, remove sdkerrors deprecated usages
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Oct 4, 2024
1 parent 8eff748 commit 746de79
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 154 deletions.
2 changes: 2 additions & 0 deletions integration_tests/happy_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func (s *IntegrationTestSuite) TestHappyPath() {
s.Run("Test orchestrator EthereumTxConfirmation after transaction execution", func() {
val := s.chain.validators[0]
keyring, err := val.keyring()
s.Require().NoError(err)
clientCtx, err := s.chain.clientContext("tcp://localhost:26657", &keyring, "val", val.address())
s.Require().NoError(err)

Expand All @@ -309,6 +310,7 @@ func (s *IntegrationTestSuite) TestHappyPath() {
testDenomERC20 := common.HexToAddress(gravityResponse.Erc20)

initialBalance, err := s.getEthTokenBalanceOf(common.HexToAddress(recipient), testDenomERC20)
s.Require().NoError(err, "error getting initial balance")
// Turn off one orchestrator (let's use the second one)
s.T().Log("Turning off orchestrator1")
err = s.dockerPool.RemoveContainerByName("orchestrator1")
Expand Down
3 changes: 1 addition & 2 deletions integration_tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ var (
stakeAmountCoin = sdk.NewCoin(testDenom, stakeAmount)
gravityContract = common.HexToAddress("0x04C89607413713Ec9775E14b954286519d836FEf")
testERC20contract = common.HexToAddress("0x4C4a2f8c81640e47606d3fd77B353E87Ba015584")
testERC20Denom = fmt.Sprintf("gravity%s", testERC20contract.Hex())
)

type IntegrationTestSuite struct {
Expand Down Expand Up @@ -347,7 +346,7 @@ func (s *IntegrationTestSuite) initGenesis() {
s.Require().NoError(cdc.UnmarshalJSON(appGenState[gravitytypes.ModuleName], &gravityGenState))
gravityGenState.Params.GravityId = "gravitytest"
gravityGenState.Params.BridgeEthereumAddress = gravityContract.String()
gravityGenState.Params.ConfirmedOutgoingTxWindow = 1000000
gravityGenState.Params.ConfirmedOutgoingTxWindow = 300
gravityGenState.Params.TargetEthTxTimeout = 3600000
gravityGenState.Params.AverageBlockTime = 1000
gravityGenState.Params.AverageEthereumBlockTime = 1000
Expand Down
14 changes: 9 additions & 5 deletions integration_tests/validator_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,33 +129,37 @@ func (s *IntegrationTestSuite) TestValidatorOut() {
s.Require().NoError(err)
newQ := stakingtypes.NewQueryClient(clientCtx)

val0, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: sdk.ValAddress(s.chain.validators[0].address()).String()})
val0Address := sdk.ValAddress(s.chain.validators[0].address()).String()
val0, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: val0Address})
if err != nil {
s.T().Logf("error: %s", err)
return false
}
s.Require().False(val0.GetValidator().IsJailed())

val1, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: sdk.ValAddress(s.chain.validators[1].address()).String()})
val1Address := sdk.ValAddress(s.chain.validators[1].address()).String()
val1, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: val1Address})
if err != nil {
s.T().Logf("error: %s", err)
return false
}
s.Require().False(val1.GetValidator().IsJailed())

val2, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: sdk.ValAddress(s.chain.validators[2].address()).String()})
val2Address := sdk.ValAddress(s.chain.validators[2].address()).String()
val2, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: val2Address})
if err != nil {
s.T().Logf("error: %s", err)
return false
}
s.Require().False(val2.GetValidator().IsJailed())

val3, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: sdk.ValAddress(s.chain.validators[3].address()).String()})
val3Address := sdk.ValAddress(s.chain.validators[3].address()).String()
val3, err := newQ.Validator(context.Background(), &stakingtypes.QueryValidatorRequest{ValidatorAddr: val3Address})
if err != nil {
s.T().Logf("error: %s", err)
return false
}
return val3.GetValidator().IsJailed()
}, 5*time.Minute, 5*time.Second, "can't confirm jailing status")
}, 8*time.Minute, 5*time.Second, "can't confirm jailing status")
})
}
5 changes: 3 additions & 2 deletions module/x/gravity/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gravity

import (
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return sdk.WrapServiceResult(ctx, res, err)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
return nil, errors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
}
}
}
Expand All @@ -53,7 +54,7 @@ func NewCommunityPoolEthereumSpendProposalHandler(k keeper.Keeper) govtypes.Hand
case *types.CommunityPoolEthereumSpendProposal:
return k.HandleCommunityPoolEthereumSpendProposal(ctx, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized gravity proposal content type: %T", c)
return errors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized gravity proposal content type: %T", c)
}
}
}
8 changes: 4 additions & 4 deletions module/x/gravity/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sort"
"strconv"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"

"github.com/peggyjv/gravity-bridge/module/v4/x/gravity/types"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (k Keeper) batchTxExecuted(ctx sdk.Context, tokenContract common.Address, n
// If the iterated batches nonce is lower than the one that was just executed, cancel it
btx, ok := otx.(*types.BatchTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for %s", otx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for %s", otx))
}

if (btx.BatchNonce < batchTx.BatchNonce) && (btx.TokenContract == batchTx.TokenContract) {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (k Keeper) GetUnsignedBatchTxs(ctx sdk.Context, val sdk.ValAddress) []*type
if len(sig) == 0 {
batch, ok := cotx.(*types.BatchTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for completed tx %s", cotx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for completed tx %s", cotx))
}
unconfirmed = append(unconfirmed, batch)
}
Expand All @@ -184,7 +184,7 @@ func (k Keeper) GetUnsignedBatchTxs(ctx sdk.Context, val sdk.ValAddress) []*type
if len(sig) == 0 {
batch, ok := otx.(*types.BatchTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for %s", otx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to batch tx for %s", otx))
}
unconfirmed = append(unconfirmed, batch)
}
Expand Down
8 changes: 4 additions & 4 deletions module/x/gravity/keeper/contract_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/hex"
"sort"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/peggyjv/gravity-bridge/module/v4/x/gravity/types"
)

Expand All @@ -17,7 +17,7 @@ func (k Keeper) GetUnsignedContractCallTxs(ctx sdk.Context, val sdk.ValAddress)
if len(sig) == 0 {
call, ok := cotx.(*types.ContractCallTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for completed tx %s", cotx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for completed tx %s", cotx))
}
unconfirmed = append(unconfirmed, call)
}
Expand All @@ -29,7 +29,7 @@ func (k Keeper) GetUnsignedContractCallTxs(ctx sdk.Context, val sdk.ValAddress)
if len(sig) == 0 {
call, ok := otx.(*types.ContractCallTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for %s", otx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to contract call for %s", otx))
}
unconfirmed = append(unconfirmed, call)
}
Expand All @@ -53,7 +53,7 @@ func (k Keeper) contractCallExecuted(ctx sdk.Context, invalidationScope []byte,
// If the iterated contract call's nonce is lower than the one that was just executed, delete it
cctx, ok := otx.(*types.ContractCallTx)
if !ok {
panic(sdkerrors.Wrapf(types.ErrInvalid, "couldn't cast to contract call tx for %s", otx))
panic(errors.Wrapf(types.ErrInvalid, "couldn't cast to contract call tx for %s", otx))
}

if (cctx.InvalidationNonce < completedCallTx.InvalidationNonce) &&
Expand Down
24 changes: 12 additions & 12 deletions module/x/gravity/keeper/ethereum_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package keeper
import (
"math/big"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/common"

Expand All @@ -15,7 +15,7 @@ func (k Keeper) DetectMaliciousSupply(ctx sdk.Context, denom string, amount sdk.
currentSupply := k.bankKeeper.GetSupply(ctx, denom)
newSupply := new(big.Int).Add(currentSupply.Amount.BigInt(), amount.BigInt())
if newSupply.BitLen() > 256 {
return sdkerrors.Wrapf(types.ErrSupplyOverflow, "malicious supply of %s detected", denom)
return errors.Wrapf(types.ErrSupplyOverflow, "malicious supply of %s detected", denom)
}

return nil
Expand All @@ -37,7 +37,7 @@ func (k Keeper) Handle(ctx sdk.Context, eve types.EthereumEvent) (err error) {

// if it is not cosmos originated, mint the coins (aka vouchers)
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
return sdkerrors.Wrapf(err, "mint vouchers coins: %s", coins)
return errors.Wrapf(err, "mint vouchers coins: %s", coins)
}
}

Expand Down Expand Up @@ -79,13 +79,13 @@ func (k Keeper) Handle(ctx sdk.Context, eve types.EthereumEvent) (err error) {
return nil

default:
return sdkerrors.Wrapf(types.ErrInvalid, "event type: %T", event)
return errors.Wrapf(types.ErrInvalid, "event type: %T", event)
}
}

func (k Keeper) verifyERC20DeployedEvent(ctx sdk.Context, event *types.ERC20DeployedEvent) error {
if existingERC20, exists := k.getCosmosOriginatedERC20(ctx, event.CosmosDenom); exists {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"ERC20 token %s already exists for denom %s", existingERC20.Hex(), event.CosmosDenom,
)
Expand All @@ -109,28 +109,28 @@ func (k Keeper) verifyERC20DeployedEvent(ctx sdk.Context, event *types.ERC20Depl
}

if supply := k.bankKeeper.GetSupply(ctx, event.CosmosDenom); supply.IsZero() {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"no supply exists for token %s without metadata", event.CosmosDenom,
)
}

if event.Erc20Name != event.CosmosDenom {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"invalid ERC20 name for token without metadata; got: %s, expected: %s", event.Erc20Name, event.CosmosDenom,
)
}

if event.Erc20Symbol != "" {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"expected empty ERC20 symbol for token without metadata; got: %s", event.Erc20Symbol,
)
}

if event.Erc20Decimals != 0 {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"expected zero ERC20 decimals for token without metadata; got: %d", event.Erc20Decimals,
)
Expand All @@ -141,14 +141,14 @@ func (k Keeper) verifyERC20DeployedEvent(ctx sdk.Context, event *types.ERC20Depl

func verifyERC20Token(metadata banktypes.Metadata, event *types.ERC20DeployedEvent) error {
if event.Erc20Name != metadata.Display {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"ERC20 name %s does not match the denom display %s", event.Erc20Name, metadata.Display,
)
}

if event.Erc20Symbol != metadata.Display {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"ERC20 symbol %s does not match denom display %s", event.Erc20Symbol, metadata.Display,
)
Expand Down Expand Up @@ -179,7 +179,7 @@ func verifyERC20Token(metadata banktypes.Metadata, event *types.ERC20DeployedEve
}

if uint64(decimals) != event.Erc20Decimals {
return sdkerrors.Wrapf(
return errors.Wrapf(
types.ErrInvalidERC20Event,
"ERC20 decimals %d does not match denom decimals %d", event.Erc20Decimals, decimals,
)
Expand Down
4 changes: 2 additions & 2 deletions module/x/gravity/keeper/ethereum_event_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"strconv"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"

"github.com/peggyjv/gravity-bridge/module/v4/x/gravity/types"
Expand All @@ -25,7 +25,7 @@ func (k Keeper) recordEventVote(
lastEventNonce := k.getLastEventNonceByValidator(ctx, val)
expectedNonce := lastEventNonce + 1
if event.GetEventNonce() != expectedNonce {
return nil, sdkerrors.Wrapf(types.ErrInvalid,
return nil, errors.Wrapf(types.ErrInvalid,
"non contiguous event nonce expected %v observed %v for validator %v",
expectedNonce,
event.GetEventNonce(),
Expand Down
20 changes: 17 additions & 3 deletions module/x/gravity/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {
outgoingTxs = append(outgoingTxs, ota)
sstx, _ := otx.(*types.SignerSetTx)
k.iterateEthereumSignatures(ctx, sstx.GetStoreIndex(), func(val sdk.ValAddress, sig []byte) bool {
siga, _ := types.PackConfirmation(&types.SignerSetTxConfirmation{sstx.Nonce, k.GetValidatorEthereumAddress(ctx, val).Hex(), sig})
siga, _ := types.PackConfirmation(&types.SignerSetTxConfirmation{
SignerSetNonce: sstx.Nonce,
EthereumSigner: k.GetValidatorEthereumAddress(ctx, val).Hex(),
Signature: sig,
})
ethereumTxConfirmations = append(ethereumTxConfirmations, siga)
return false
})
Expand All @@ -139,7 +143,12 @@ func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {
outgoingTxs = append(outgoingTxs, ota)
btx, _ := otx.(*types.BatchTx)
k.iterateEthereumSignatures(ctx, btx.GetStoreIndex(), func(val sdk.ValAddress, sig []byte) bool {
siga, _ := types.PackConfirmation(&types.BatchTxConfirmation{btx.TokenContract, btx.BatchNonce, k.GetValidatorEthereumAddress(ctx, val).Hex(), sig})
siga, _ := types.PackConfirmation(&types.BatchTxConfirmation{
TokenContract: btx.TokenContract,
BatchNonce: btx.BatchNonce,
EthereumSigner: k.GetValidatorEthereumAddress(ctx, val).Hex(),
Signature: sig,
})
ethereumTxConfirmations = append(ethereumTxConfirmations, siga)
return false
})
Expand All @@ -152,7 +161,12 @@ func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {
outgoingTxs = append(outgoingTxs, ota)
btx, _ := otx.(*types.ContractCallTx)
k.iterateEthereumSignatures(ctx, btx.GetStoreIndex(), func(val sdk.ValAddress, sig []byte) bool {
siga, _ := types.PackConfirmation(&types.ContractCallTxConfirmation{btx.InvalidationScope, btx.InvalidationNonce, k.GetValidatorEthereumAddress(ctx, val).Hex(), sig})
siga, _ := types.PackConfirmation(&types.ContractCallTxConfirmation{
InvalidationScope: btx.InvalidationScope,
InvalidationNonce: btx.InvalidationNonce,
EthereumSigner: k.GetValidatorEthereumAddress(ctx, val).Hex(),
Signature: sig,
})
ethereumTxConfirmations = append(ethereumTxConfirmations, siga)
return false
})
Expand Down
Loading

0 comments on commit 746de79

Please sign in to comment.