Skip to content

Commit

Permalink
fix: Wrong chain ID in error message when chain not found
Browse files Browse the repository at this point in the history
fix: Chain lookup done even when network prefix in chain ID is wrong
  • Loading branch information
Nikita Nikolashyn committed Jul 25, 2024
1 parent d2f4efb commit c22ab14
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/chains/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (c *Chains) Get(chainID isc.ChainID) (chain.Chain, error) {

ret, exists := c.allChains.Get(chainID)
if !exists {
return nil, interfaces.ErrChainNotFound
return nil, interfaces.NewChainNotFoundError(chainID)
}
return ret.chain, nil
}
Expand Down
8 changes: 7 additions & 1 deletion packages/isc/chainid.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ func ChainIDFromBytes(data []byte) (ret ChainID, err error) {
}

func ChainIDFromString(bech32 string) (ChainID, error) {
_, addr, err := iotago.ParseBech32(bech32)
netPrefix, addr, err := iotago.ParseBech32(bech32)
if err != nil {
return ChainID{}, err
}
if addr.Type() != iotago.AddressAlias {
return ChainID{}, fmt.Errorf("chainID must be an alias address (%s)", bech32)
}

expectedNetPrefix := parameters.L1().Protocol.Bech32HRP
if netPrefix != expectedNetPrefix {
return ChainID{}, fmt.Errorf("invalid network prefix: %s", netPrefix)
}

return ChainIDFromAddress(addr.(*iotago.AliasAddress)), nil
}

Expand Down
4 changes: 2 additions & 2 deletions packages/webapi/controllers/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Controller) getChainInfo(e echo.Context) error {
}

chainInfo, err := c.chainService.GetChainInfoByChainID(chainID, e.QueryParam(params.ParamBlockIndexOrTrieRoot))
if errors.Is(err, interfaces.ErrChainNotFound) {
if errors.As(err, &interfaces.ChainNotFoundError{}) {
return e.NoContent(http.StatusNotFound)
} else if err != nil {
return err
Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *Controller) getChainList(e echo.Context) error {
chainInfo, err := c.chainService.GetChainInfoByChainID(chainID, "")
c.log.Infof("getchaininfo %v", err)

if errors.Is(err, interfaces.ErrChainNotFound) {
if errors.As(err, &interfaces.ChainNotFoundError{}) {
// TODO: Validate this logic here. Is it possible to still get more chain info?
chainList = append(chainList, models.ChainInfoResponse{
IsActive: false,
Expand Down
6 changes: 4 additions & 2 deletions packages/webapi/controllers/corecontracts/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func (c *Controller) Name() string {
}

func (c *Controller) handleViewCallError(err error, chainID isc.ChainID) error {
if errors.Is(err, interfaces.ErrChainNotFound) {
return apierrors.ChainNotFoundError(chainID.String())
var chainNotFound interfaces.ChainNotFoundError
if errors.As(err, &chainNotFound) {
return apierrors.ChainNotFoundError(chainNotFound.ChainID.String())
}

return apierrors.ContractExecutionError(err)
}

Expand Down
31 changes: 31 additions & 0 deletions packages/webapi/interfaces/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package interfaces

import (
"errors"

"github.com/iotaledger/wasp/packages/isc"
)

var (
ErrCantDeleteLastUser = errors.New("you can't delete the last user")
)

func NewChainNotFoundError(chainID isc.ChainID) error {
return ChainNotFoundError{
ChainID: chainID,
}
}

type ChainNotFoundError struct {
ChainID isc.ChainID
}

func (e ChainNotFoundError) Error() string {
errStr := "Chain not found"

if !e.ChainID.Empty() {
errStr = errStr + ": " + e.ChainID.String()
}

return errStr
}
5 changes: 0 additions & 5 deletions packages/webapi/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import (
"github.com/iotaledger/wasp/packages/webapi/models"
)

var (
ErrChainNotFound = errors.New("chain not found")
ErrCantDeleteLastUser = errors.New("you can't delete the last user")
)

type APIController interface {
Name() string
RegisterPublic(publicAPI echoswagger.ApiGroup, mocker Mocker)
Expand Down
2 changes: 1 addition & 1 deletion packages/webapi/services/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (c *ChainService) GetChainInfoByChainID(chainID isc.ChainID, blockIndexOrTr

governanceChainInfo, err := corecontracts.GetChainInfo(ch, blockIndexOrTrieRoot)
if err != nil {
if chainRecord != nil && errors.Is(err, interfaces.ErrChainNotFound) {
if chainRecord != nil && errors.As(err, &interfaces.ChainNotFoundError{}) {
return &dto.ChainInfo{ChainID: chainID, IsActive: false}, nil
}

Expand Down

0 comments on commit c22ab14

Please sign in to comment.