Skip to content

Commit

Permalink
chore: use errors.New to replace fmt.Errorf with no parameters
Browse files Browse the repository at this point in the history
Signed-off-by: RiceChuan <[email protected]>
  • Loading branch information
RiceChuan committed Dec 12, 2024
1 parent e56f8c0 commit 2350424
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
43 changes: 22 additions & 21 deletions x/evm/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evm

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -21,7 +22,7 @@ import (
func handleTokenSent(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus) error {
e := event.GetEvent().(*types.Event_TokenSent).TokenSent
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

sourceChain := funcs.MustOk(n.GetChain(ctx, event.Chain))
Expand Down Expand Up @@ -69,7 +70,7 @@ func handleTokenSent(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n
func handleContractCall(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus, multisig types.MultisigKeeper) error {
e := event.GetContractCall()
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

destinationChain := funcs.MustOk(n.GetChain(ctx, e.DestinationChain))
Expand All @@ -86,7 +87,7 @@ func handleContractCall(ctx sdk.Context, event types.Event, bk types.BaseKeeper,
func handleContractCallToEVM(ctx sdk.Context, bk types.BaseKeeper, multisig types.MultisigKeeper, n types.Nexus, destinationChain nexus.ChainName, event types.Event) {
e := event.GetContractCall()
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

destinationCk := funcs.Must(bk.ForChain(ctx, destinationChain))
Expand Down Expand Up @@ -119,7 +120,7 @@ func handleContractCallToEVM(ctx sdk.Context, bk types.BaseKeeper, multisig type
func handleContractCallWithToken(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus, multisig types.MultisigKeeper) error {
e := event.GetContractCallWithToken()
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

sourceChain := funcs.MustOk(n.GetChain(ctx, event.Chain))
Expand Down Expand Up @@ -149,7 +150,7 @@ func handleContractCallWithToken(ctx sdk.Context, event types.Event, bk types.Ba
func handleContractCallWithTokenToEVM(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus, multisig types.MultisigKeeper, sourceChain, destinationChain nexus.ChainName, asset string) error {
e := event.GetContractCallWithToken()
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

destinationCk := funcs.Must(bk.ForChain(ctx, destinationChain))
Expand Down Expand Up @@ -228,7 +229,7 @@ func setMessageToNexus(ctx sdk.Context, n types.Nexus, event types.Event, asset

case *types.Event_ContractCallWithToken:
if asset == nil {
return fmt.Errorf("expect asset for ContractCallWithToken")
return errors.New("expect asset for ContractCallWithToken")
}

sender := nexus.CrossChainAddress{
Expand Down Expand Up @@ -264,7 +265,7 @@ func setMessageToNexus(ctx sdk.Context, n types.Nexus, event types.Event, asset
func handleConfirmDeposit(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus) error {
e := event.GetEvent().(*types.Event_Transfer).Transfer
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

chain := funcs.MustOk(n.GetChain(ctx, event.Chain))
Expand Down Expand Up @@ -336,7 +337,7 @@ func handleConfirmDeposit(ctx sdk.Context, event types.Event, bk types.BaseKeepe
func handleTokenDeployed(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus) error {
e := event.GetEvent().(*types.Event_TokenDeployed).TokenDeployed
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

chain := funcs.MustOk(n.GetChain(ctx, event.Chain))
Expand Down Expand Up @@ -380,7 +381,7 @@ func handleTokenDeployed(ctx sdk.Context, event types.Event, bk types.BaseKeeper
func handleMultisigTransferKey(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n types.Nexus, multisig types.MultisigKeeper) error {
e := event.GetEvent().(*types.Event_MultisigOperatorshipTransferred).MultisigOperatorshipTransferred
if e == nil {
panic(fmt.Errorf("event is nil"))
panic(errors.New("event is nil"))
}

chain := funcs.MustOk(n.GetChain(ctx, event.Chain))
Expand Down Expand Up @@ -409,17 +410,17 @@ func handleMultisigTransferKey(ctx sdk.Context, event types.Event, bk types.Base
for i, newAddress := range newAddresses {
newAddressHex := newAddress.Hex()
if addressSeen[newAddressHex] {
return fmt.Errorf("duplicate address in new addresses")
return errors.New("duplicate address in new addresses")
}
addressSeen[newAddressHex] = true

expectedWeight, ok := expectedAddressWeights[newAddressHex]
if !ok {
return fmt.Errorf("new addresses do not match")
return errors.New("new addresses do not match")
}

if !expectedWeight.Equal(newWeights[i]) {
return fmt.Errorf("new weights do not match")
return errors.New("new weights do not match")
}
}

Expand Down Expand Up @@ -473,12 +474,12 @@ func validateEvent(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n ty
// skip if destination chain is not registered
destinationChain, ok := n.GetChain(ctx, destinationChainName)
if !ok {
return fmt.Errorf("destination chain not found")
return errors.New("destination chain not found")
}

// skip if destination chain is not activated
if !n.IsChainActivated(ctx, destinationChain) {
return fmt.Errorf("destination chain de-activated")
return errors.New("destination chain de-activated")
}

// skip further destination chain keeper checks if it is not an evm chain
Expand All @@ -487,17 +488,17 @@ func validateEvent(ctx sdk.Context, event types.Event, bk types.BaseKeeper, n ty
}

if len(contractAddress) != 0 && !common.IsHexAddress(contractAddress) {
return fmt.Errorf("invalid contract address")
return errors.New("invalid contract address")
}

destinationCk, err := bk.ForChain(ctx, destinationChainName)
if err != nil {
return fmt.Errorf("destination chain not EVM-compatible")
return errors.New("destination chain not EVM-compatible")
}

// skip if destination chain has not got gateway set yet
if _, ok := destinationCk.GetGatewayAddress(ctx); !ok {
return fmt.Errorf("destination chain gateway not deployed yet")
return errors.New("destination chain gateway not deployed yet")
}

return nil
Expand Down Expand Up @@ -575,19 +576,19 @@ func validateMessage(ctx sdk.Context, ck types.ChainKeeper, n types.Nexus, m typ
// TODO refactor to do these checks earlier so we don't fail in the end blocker
_, ok := m.GetCurrentKeyID(ctx, chain.Name)
if !ok {
return fmt.Errorf("current key not set")
return errors.New("current key not set")
}

if !n.IsChainActivated(ctx, chain) {
return fmt.Errorf("destination chain de-activated")
return errors.New("destination chain de-activated")
}

if _, ok := ck.GetGatewayAddress(ctx); !ok {
return fmt.Errorf("destination chain gateway not deployed yet")
return errors.New("destination chain gateway not deployed yet")
}

if !common.IsHexAddress(msg.GetDestinationAddress()) {
return fmt.Errorf("invalid contract address")
return errors.New("invalid contract address")
}

switch msg.Type() {
Expand Down
12 changes: 6 additions & 6 deletions x/evm/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestHandleGeneralMessage(t *testing.T) {
return func() {
destinationCk.EnqueueCommandFunc = func(ctx sdk.Context, cmd types.Command) error {
if !isSuccessful {
return fmt.Errorf("enqueue error")
return errors.New("enqueue error")
}

return nil
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestHandleGeneralMessages(t *testing.T) {
When("rate limit is triggered", func() {
n.SetMessageFailedFunc = func(ctx sdk.Context, id string) error { return nil }
n.RateLimitTransferFunc = func(ctx sdk.Context, chain nexus.ChainName, asset sdk.Coin, direction nexus.TransferDirection) error {
return fmt.Errorf("rate limit triggered")
return errors.New("rate limit triggered")
}
}).
Then("should handle", func(t *testing.T) {
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestHandleContractCall(t *testing.T) {
return func() {
destinationCk.EnqueueCommandFunc = func(ctx sdk.Context, cmd types.Command) error {
if !isSuccessful {
return fmt.Errorf("enqueue error")
return errors.New("enqueue error")
}

return nil
Expand All @@ -560,7 +560,7 @@ func TestHandleContractCall(t *testing.T) {
return func() {
n.SetNewMessageFunc = func(sdk.Context, nexus.GeneralMessage) error {
if !isSuccessful {
return fmt.Errorf("set general message error")
return errors.New("set general message error")
}

return nil
Expand Down Expand Up @@ -779,7 +779,7 @@ func TestHandleTokenSent(t *testing.T) {
whenTokensAreConfirmed.
When("failed to enqueue the transfer", func() {
n.EnqueueTransferFunc = func(ctx sdk.Context, senderChain nexus.Chain, recipient nexus.CrossChainAddress, asset sdk.Coin) (nexus.TransferID, error) {
return 0, fmt.Errorf("err")
return 0, errors.New("err")
}
}).
Then("should fail", assertFail).
Expand Down Expand Up @@ -1272,7 +1272,7 @@ func TestHandleConfirmDeposit(t *testing.T) {
return func() {
n.EnqueueForTransferFunc = func(sdk.Context, nexus.CrossChainAddress, sdk.Coin) (nexus.TransferID, error) {
if !succeed {
return 0, fmt.Errorf("err")
return 0, errors.New("err")
}
return 0, nil
}
Expand Down
9 changes: 5 additions & 4 deletions x/evm/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2350424

Please sign in to comment.