From c22ab1425f5e97c987759c0679534a509c4197c7 Mon Sep 17 00:00:00 2001 From: Nikita Nikolashyn Date: Thu, 25 Jul 2024 13:36:00 +0300 Subject: [PATCH 1/9] fix: Wrong chain ID in error message when chain not found fix: Chain lookup done even when network prefix in chain ID is wrong --- packages/chains/chains.go | 2 +- packages/isc/chainid.go | 8 ++++- packages/webapi/controllers/chain/chain.go | 4 +-- .../controllers/corecontracts/controller.go | 6 ++-- packages/webapi/interfaces/errors.go | 31 +++++++++++++++++++ packages/webapi/interfaces/interfaces.go | 5 --- packages/webapi/services/chain.go | 2 +- 7 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 packages/webapi/interfaces/errors.go diff --git a/packages/chains/chains.go b/packages/chains/chains.go index 3bbfa85a79..fc11db7ee7 100644 --- a/packages/chains/chains.go +++ b/packages/chains/chains.go @@ -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 } diff --git a/packages/isc/chainid.go b/packages/isc/chainid.go index b3805782a4..c769f40175 100644 --- a/packages/isc/chainid.go +++ b/packages/isc/chainid.go @@ -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 } diff --git a/packages/webapi/controllers/chain/chain.go b/packages/webapi/controllers/chain/chain.go index b01f7f967a..1325d4a7fb 100644 --- a/packages/webapi/controllers/chain/chain.go +++ b/packages/webapi/controllers/chain/chain.go @@ -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 @@ -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, diff --git a/packages/webapi/controllers/corecontracts/controller.go b/packages/webapi/controllers/corecontracts/controller.go index 936bc4393b..8f96f2229e 100644 --- a/packages/webapi/controllers/corecontracts/controller.go +++ b/packages/webapi/controllers/corecontracts/controller.go @@ -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) } diff --git a/packages/webapi/interfaces/errors.go b/packages/webapi/interfaces/errors.go new file mode 100644 index 0000000000..c136acf3cc --- /dev/null +++ b/packages/webapi/interfaces/errors.go @@ -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 +} diff --git a/packages/webapi/interfaces/interfaces.go b/packages/webapi/interfaces/interfaces.go index ef7f5f2335..b5b6a3f965 100644 --- a/packages/webapi/interfaces/interfaces.go +++ b/packages/webapi/interfaces/interfaces.go @@ -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) diff --git a/packages/webapi/services/chain.go b/packages/webapi/services/chain.go index c8b4d2b774..3d8c83995e 100644 --- a/packages/webapi/services/chain.go +++ b/packages/webapi/services/chain.go @@ -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 } From 5f8c5ee6fc10e7f87944891c0092b3ad0bfd5cf1 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Thu, 25 Jul 2024 15:35:15 +0300 Subject: [PATCH 2/9] Add tests --- packages/isc/chainid_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/isc/chainid_test.go b/packages/isc/chainid_test.go index 0a318ad3c6..fce7e74e42 100644 --- a/packages/isc/chainid_test.go +++ b/packages/isc/chainid_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/iotaledger/wasp/packages/util/rwutil" + "github.com/stretchr/testify/require" ) func TestChainIDSerialization(t *testing.T) { @@ -12,3 +13,10 @@ func TestChainIDSerialization(t *testing.T) { rwutil.BytesTest(t, chainID, ChainIDFromBytes) rwutil.StringTest(t, chainID, ChainIDFromString) } + +func TestIncorrectPrefix(t *testing.T) { + chainID := "rms1prxunz807j39nmhzy3gre4hwdlzvdjyrkfn59d27x6xh426y8ajt205mh9g" + _, err := ChainIDFromString(chainID) + + require.ErrorContains(t, err, "invalid network prefix: rms", "expected error for incorrect prefix") +} From a9ea9aa1bdc19cf2a1cc41d9b0747129651d0561 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Thu, 25 Jul 2024 15:55:53 +0300 Subject: [PATCH 3/9] Revert "Add tests" This reverts commit 5f8c5ee6fc10e7f87944891c0092b3ad0bfd5cf1. --- packages/isc/chainid_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/isc/chainid_test.go b/packages/isc/chainid_test.go index fce7e74e42..0a318ad3c6 100644 --- a/packages/isc/chainid_test.go +++ b/packages/isc/chainid_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/iotaledger/wasp/packages/util/rwutil" - "github.com/stretchr/testify/require" ) func TestChainIDSerialization(t *testing.T) { @@ -13,10 +12,3 @@ func TestChainIDSerialization(t *testing.T) { rwutil.BytesTest(t, chainID, ChainIDFromBytes) rwutil.StringTest(t, chainID, ChainIDFromString) } - -func TestIncorrectPrefix(t *testing.T) { - chainID := "rms1prxunz807j39nmhzy3gre4hwdlzvdjyrkfn59d27x6xh426y8ajt205mh9g" - _, err := ChainIDFromString(chainID) - - require.ErrorContains(t, err, "invalid network prefix: rms", "expected error for incorrect prefix") -} From 2bd07e7c3aff4b9ddeac02a836faef20ed7f67c2 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Thu, 25 Jul 2024 15:56:04 +0300 Subject: [PATCH 4/9] Revert "fix: Wrong chain ID in error message when chain not found" This reverts commit c22ab1425f5e97c987759c0679534a509c4197c7. --- packages/chains/chains.go | 2 +- packages/isc/chainid.go | 8 +---- packages/webapi/controllers/chain/chain.go | 4 +-- .../controllers/corecontracts/controller.go | 6 ++-- packages/webapi/interfaces/errors.go | 31 ------------------- packages/webapi/interfaces/interfaces.go | 5 +++ packages/webapi/services/chain.go | 2 +- 7 files changed, 12 insertions(+), 46 deletions(-) delete mode 100644 packages/webapi/interfaces/errors.go diff --git a/packages/chains/chains.go b/packages/chains/chains.go index fc11db7ee7..3bbfa85a79 100644 --- a/packages/chains/chains.go +++ b/packages/chains/chains.go @@ -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.NewChainNotFoundError(chainID) + return nil, interfaces.ErrChainNotFound } return ret.chain, nil } diff --git a/packages/isc/chainid.go b/packages/isc/chainid.go index c769f40175..b3805782a4 100644 --- a/packages/isc/chainid.go +++ b/packages/isc/chainid.go @@ -45,19 +45,13 @@ func ChainIDFromBytes(data []byte) (ret ChainID, err error) { } func ChainIDFromString(bech32 string) (ChainID, error) { - netPrefix, addr, err := iotago.ParseBech32(bech32) + _, 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 } diff --git a/packages/webapi/controllers/chain/chain.go b/packages/webapi/controllers/chain/chain.go index 1325d4a7fb..b01f7f967a 100644 --- a/packages/webapi/controllers/chain/chain.go +++ b/packages/webapi/controllers/chain/chain.go @@ -65,7 +65,7 @@ func (c *Controller) getChainInfo(e echo.Context) error { } chainInfo, err := c.chainService.GetChainInfoByChainID(chainID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) - if errors.As(err, &interfaces.ChainNotFoundError{}) { + if errors.Is(err, interfaces.ErrChainNotFound) { return e.NoContent(http.StatusNotFound) } else if err != nil { return err @@ -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.As(err, &interfaces.ChainNotFoundError{}) { + if errors.Is(err, interfaces.ErrChainNotFound) { // TODO: Validate this logic here. Is it possible to still get more chain info? chainList = append(chainList, models.ChainInfoResponse{ IsActive: false, diff --git a/packages/webapi/controllers/corecontracts/controller.go b/packages/webapi/controllers/corecontracts/controller.go index 8f96f2229e..936bc4393b 100644 --- a/packages/webapi/controllers/corecontracts/controller.go +++ b/packages/webapi/controllers/corecontracts/controller.go @@ -27,11 +27,9 @@ func (c *Controller) Name() string { } func (c *Controller) handleViewCallError(err error, chainID isc.ChainID) error { - var chainNotFound interfaces.ChainNotFoundError - if errors.As(err, &chainNotFound) { - return apierrors.ChainNotFoundError(chainNotFound.ChainID.String()) + if errors.Is(err, interfaces.ErrChainNotFound) { + return apierrors.ChainNotFoundError(chainID.String()) } - return apierrors.ContractExecutionError(err) } diff --git a/packages/webapi/interfaces/errors.go b/packages/webapi/interfaces/errors.go deleted file mode 100644 index c136acf3cc..0000000000 --- a/packages/webapi/interfaces/errors.go +++ /dev/null @@ -1,31 +0,0 @@ -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 -} diff --git a/packages/webapi/interfaces/interfaces.go b/packages/webapi/interfaces/interfaces.go index b5b6a3f965..ef7f5f2335 100644 --- a/packages/webapi/interfaces/interfaces.go +++ b/packages/webapi/interfaces/interfaces.go @@ -17,6 +17,11 @@ 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) diff --git a/packages/webapi/services/chain.go b/packages/webapi/services/chain.go index 3d8c83995e..c8b4d2b774 100644 --- a/packages/webapi/services/chain.go +++ b/packages/webapi/services/chain.go @@ -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.As(err, &interfaces.ChainNotFoundError{}) { + if chainRecord != nil && errors.Is(err, interfaces.ErrChainNotFound) { return &dto.ChainInfo{ChainID: chainID, IsActive: false}, nil } From 057a00230d8b0c3aa448e84b62c9c69d161dd1c7 Mon Sep 17 00:00:00 2001 From: Nikita Nikolashyn Date: Thu, 25 Jul 2024 16:14:22 +0300 Subject: [PATCH 5/9] fix: Chain lookup done even when network prefix in chain ID is wrong --- packages/isc/chainid.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/isc/chainid.go b/packages/isc/chainid.go index b3805782a4..3bb9dfccf4 100644 --- a/packages/isc/chainid.go +++ b/packages/isc/chainid.go @@ -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 } @@ -121,6 +127,10 @@ func (id ChainID) ShortString() string { // String human-readable form (bech32) func (id ChainID) String() string { + if id.Empty() { + return "" + } + return id.AsAddress().Bech32(parameters.L1().Protocol.Bech32HRP) } From 203bc509d3375370e111553186187c62adfbc539 Mon Sep 17 00:00:00 2001 From: Nikita Nikolashyn Date: Thu, 25 Jul 2024 16:17:16 +0300 Subject: [PATCH 6/9] chore: Added test which was rolled back --- packages/isc/chainid_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/isc/chainid_test.go b/packages/isc/chainid_test.go index 0a318ad3c6..78aef2fa96 100644 --- a/packages/isc/chainid_test.go +++ b/packages/isc/chainid_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/iotaledger/wasp/packages/util/rwutil" + "github.com/stretchr/testify/require" ) func TestChainIDSerialization(t *testing.T) { @@ -12,3 +13,10 @@ func TestChainIDSerialization(t *testing.T) { rwutil.BytesTest(t, chainID, ChainIDFromBytes) rwutil.StringTest(t, chainID, ChainIDFromString) } + +func TestIncorrectPrefix(t *testing.T) { + chainID := "rms1prxunz807j39nmhzy3gre4hwdlzvdjyrkfn59d27x6xh426y8ajt205mh9g" + _, err := ChainIDFromString(chainID) + + require.ErrorContains(t, err, "invalid network prefix: rms") +} From d69a1872a22684e3618f60a98832e18049457268 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Thu, 25 Jul 2024 16:25:34 +0300 Subject: [PATCH 7/9] Remove chainID from 'chain not found' error --- packages/webapi/apierrors/errors.go | 4 +- packages/webapi/controllers/chain/chain.go | 2 +- .../controllerutils/controllerutils.go | 2 +- .../controllers/corecontracts/accounts.go | 48 +++++++++---------- .../webapi/controllers/corecontracts/blob.go | 12 ++--- .../controllers/corecontracts/blocklog.go | 46 +++++++++--------- .../controllers/corecontracts/controller.go | 4 +- .../controllers/corecontracts/errors.go | 6 +-- .../controllers/corecontracts/governance.go | 18 +++---- .../webapi/controllers/requests/offledger.go | 2 +- 10 files changed, 72 insertions(+), 72 deletions(-) diff --git a/packages/webapi/apierrors/errors.go b/packages/webapi/apierrors/errors.go index f69bb9c00b..d850dda38c 100644 --- a/packages/webapi/apierrors/errors.go +++ b/packages/webapi/apierrors/errors.go @@ -7,8 +7,8 @@ import ( "strings" ) -func ChainNotFoundError(chainID string) *HTTPError { - return NewHTTPError(http.StatusNotFound, fmt.Sprintf("Chain ID: %v not found", chainID), nil) +func ChainNotFoundError() *HTTPError { + return NewHTTPError(http.StatusNotFound, "Chain ID not found", nil) } func UserNotFoundError(username string) *HTTPError { diff --git a/packages/webapi/controllers/chain/chain.go b/packages/webapi/controllers/chain/chain.go index b01f7f967a..d947ae48e8 100644 --- a/packages/webapi/controllers/chain/chain.go +++ b/packages/webapi/controllers/chain/chain.go @@ -34,7 +34,7 @@ func (c *Controller) getCommitteeInfo(e echo.Context) error { chain, err := c.chainService.GetChainInfoByChainID(chainID, "") if err != nil { - return apierrors.ChainNotFoundError(chainID.String()) + return apierrors.ChainNotFoundError() } chainNodeInfo, err := c.committeeService.GetCommitteeInfo(chainID) diff --git a/packages/webapi/controllers/controllerutils/controllerutils.go b/packages/webapi/controllers/controllerutils/controllerutils.go index 9292962278..51cda7119e 100644 --- a/packages/webapi/controllers/controllerutils/controllerutils.go +++ b/packages/webapi/controllers/controllerutils/controllerutils.go @@ -23,7 +23,7 @@ func ChainIDFromParams(c echo.Context, cs interfaces.ChainService) (isc.ChainID, } if !cs.HasChain(chainID) { - return isc.ChainID{}, apierrors.ChainNotFoundError(chainID.String()) + return isc.ChainID{}, apierrors.ChainNotFoundError() } // set chainID to be used by the prometheus metrics c.Set(EchoContextKeyChainID, chainID) diff --git a/packages/webapi/controllers/corecontracts/accounts.go b/packages/webapi/controllers/corecontracts/accounts.go index f284e92836..f97e31e608 100644 --- a/packages/webapi/controllers/corecontracts/accounts.go +++ b/packages/webapi/controllers/corecontracts/accounts.go @@ -15,14 +15,14 @@ import ( ) func (c *Controller) getTotalAssets(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } assets, err := corecontracts.GetTotalAssets(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } assetsResponse := &models.AssetsResponse{ @@ -34,9 +34,9 @@ func (c *Controller) getTotalAssets(e echo.Context) error { } func (c *Controller) getAccountBalance(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } agentID, err := params.DecodeAgentID(e) @@ -46,7 +46,7 @@ func (c *Controller) getAccountBalance(e echo.Context) error { assets, err := corecontracts.GetAccountBalance(ch, agentID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } assetsResponse := &models.AssetsResponse{ @@ -58,9 +58,9 @@ func (c *Controller) getAccountBalance(e echo.Context) error { } func (c *Controller) getAccountNFTs(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } agentID, err := params.DecodeAgentID(e) @@ -70,7 +70,7 @@ func (c *Controller) getAccountNFTs(e echo.Context) error { nfts, err := corecontracts.GetAccountNFTs(ch, agentID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } nftsResponse := &models.AccountNFTsResponse{ @@ -85,9 +85,9 @@ func (c *Controller) getAccountNFTs(e echo.Context) error { } func (c *Controller) getAccountFoundries(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } agentID, err := params.DecodeAgentID(e) if err != nil { @@ -96,7 +96,7 @@ func (c *Controller) getAccountFoundries(e echo.Context) error { foundries, err := corecontracts.GetAccountFoundries(ch, agentID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } return e.JSON(http.StatusOK, &models.AccountFoundriesResponse{ @@ -105,9 +105,9 @@ func (c *Controller) getAccountFoundries(e echo.Context) error { } func (c *Controller) getAccountNonce(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } agentID, err := params.DecodeAgentID(e) @@ -117,7 +117,7 @@ func (c *Controller) getAccountNonce(e echo.Context) error { nonce, err := corecontracts.GetAccountNonce(ch, agentID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } nonceResponse := &models.AccountNonceResponse{ @@ -128,9 +128,9 @@ func (c *Controller) getAccountNonce(e echo.Context) error { } func (c *Controller) getNFTData(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } nftID, err := params.DecodeNFTID(e) @@ -140,7 +140,7 @@ func (c *Controller) getNFTData(e echo.Context) error { nftData, err := corecontracts.GetNFTData(ch, *nftID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } nftDataResponse := isc.NFTToJSONObject(nftData) @@ -149,14 +149,14 @@ func (c *Controller) getNFTData(e echo.Context) error { } func (c *Controller) getNativeTokenIDRegistry(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } registries, err := corecontracts.GetNativeTokenIDRegistry(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } nativeTokenIDRegistryResponse := &models.NativeTokenIDRegistryResponse{ @@ -171,9 +171,9 @@ func (c *Controller) getNativeTokenIDRegistry(e echo.Context) error { } func (c *Controller) getFoundryOutput(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } serialNumber, err := params.DecodeUInt(e, "serialNumber") @@ -183,7 +183,7 @@ func (c *Controller) getFoundryOutput(e echo.Context) error { foundryOutput, err := corecontracts.GetFoundryOutput(ch, uint32(serialNumber), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } foundryOutputID, err := foundryOutput.ID() diff --git a/packages/webapi/controllers/corecontracts/blob.go b/packages/webapi/controllers/corecontracts/blob.go index 37d2ccc693..0d40ef812f 100644 --- a/packages/webapi/controllers/corecontracts/blob.go +++ b/packages/webapi/controllers/corecontracts/blob.go @@ -26,9 +26,9 @@ type BlobValueResponse struct { } func (c *Controller) getBlobValue(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } blobHash, err := params.DecodeBlobHash(e) @@ -40,7 +40,7 @@ func (c *Controller) getBlobValue(e echo.Context) error { blobValueBytes, err := corecontracts.GetBlobValue(ch, *blobHash, fieldKey, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } blobValueResponse := &BlobValueResponse{ @@ -57,9 +57,9 @@ type BlobInfoResponse struct { func (c *Controller) getBlobInfo(e echo.Context) error { fmt.Println("GET BLOB INFO") - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } blobHash, err := params.DecodeBlobHash(e) @@ -69,7 +69,7 @@ func (c *Controller) getBlobInfo(e echo.Context) error { blobInfo, ok, err := corecontracts.GetBlobInfo(ch, *blobHash, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } fmt.Printf("GET BLOB INFO: ok:%v, err:%v", ok, err) diff --git a/packages/webapi/controllers/corecontracts/blocklog.go b/packages/webapi/controllers/corecontracts/blocklog.go index 41e1469868..11f119525a 100644 --- a/packages/webapi/controllers/corecontracts/blocklog.go +++ b/packages/webapi/controllers/corecontracts/blocklog.go @@ -20,13 +20,13 @@ import ( ) func (c *Controller) getControlAddresses(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } controlAddresses, err := corecontracts.GetControlAddresses(ch) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } controlAddressesResponse := &models.ControlAddressesResponse{ @@ -39,9 +39,9 @@ func (c *Controller) getControlAddresses(e echo.Context) error { } func (c *Controller) getBlockInfo(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } var blockInfo *blocklog.BlockInfo blockIndex := e.Param(params.ParamBlockIndex) @@ -58,7 +58,7 @@ func (c *Controller) getBlockInfo(e echo.Context) error { blockInfo, err = corecontracts.GetBlockInfo(ch, uint32(blockIndexNum), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) } if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } blockInfoResponse := models.MapBlockInfoResponse(blockInfo) @@ -67,9 +67,9 @@ func (c *Controller) getBlockInfo(e echo.Context) error { } func (c *Controller) getRequestIDsForBlock(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } var requestIDs []isc.RequestID blockIndex := e.Param(params.ParamBlockIndex) @@ -87,7 +87,7 @@ func (c *Controller) getRequestIDsForBlock(e echo.Context) error { } if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } requestIDsResponse := &models.RequestIDsResponse{ @@ -132,9 +132,9 @@ func (c *Controller) getRequestReceipt(e echo.Context) error { } func (c *Controller) getRequestReceiptsForBlock(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } var blocklogReceipts []*blocklog.RequestReceipt blockIndex := e.Param(params.ParamBlockIndex) @@ -143,7 +143,7 @@ func (c *Controller) getRequestReceiptsForBlock(e echo.Context) error { var blockInfo *blocklog.BlockInfo blockInfo, err = corecontracts.GetLatestBlockInfo(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } blocklogReceipts, err = corecontracts.GetRequestReceiptsForBlock(ch, blockInfo.BlockIndex(), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) @@ -157,7 +157,7 @@ func (c *Controller) getRequestReceiptsForBlock(e echo.Context) error { blocklogReceipts, err = corecontracts.GetRequestReceiptsForBlock(ch, uint32(blockIndexNum), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) } if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } receiptsResponse := make([]*models.ReceiptResponse, len(blocklogReceipts)) @@ -177,7 +177,7 @@ func (c *Controller) getRequestReceiptsForBlock(e echo.Context) error { func (c *Controller) getIsRequestProcessed(e echo.Context) error { ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } requestID, err := params.DecodeRequestID(e) if err != nil { @@ -186,7 +186,7 @@ func (c *Controller) getIsRequestProcessed(e echo.Context) error { requestProcessed, err := corecontracts.IsRequestProcessed(ch, requestID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } requestProcessedResponse := models.RequestProcessedResponse{ @@ -209,9 +209,9 @@ func eventsResponse(e echo.Context, events []*isc.Event) error { } func (c *Controller) getBlockEvents(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } var events []*isc.Event blockIndex := e.Param(params.ParamBlockIndex) @@ -224,26 +224,26 @@ func (c *Controller) getBlockEvents(e echo.Context) error { events, err = corecontracts.GetEventsForBlock(ch, uint32(blockIndexNum), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } } else { blockInfo, err := corecontracts.GetLatestBlockInfo(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } events, err = corecontracts.GetEventsForBlock(ch, blockInfo.BlockIndex(), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } } return eventsResponse(e, events) } func (c *Controller) getRequestEvents(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } requestID, err := params.DecodeRequestID(e) if err != nil { @@ -252,7 +252,7 @@ func (c *Controller) getRequestEvents(e echo.Context) error { events, err := corecontracts.GetEventsForRequest(ch, requestID, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } return eventsResponse(e, events) } diff --git a/packages/webapi/controllers/corecontracts/controller.go b/packages/webapi/controllers/corecontracts/controller.go index 936bc4393b..c948e8287c 100644 --- a/packages/webapi/controllers/corecontracts/controller.go +++ b/packages/webapi/controllers/corecontracts/controller.go @@ -26,9 +26,9 @@ func (c *Controller) Name() string { return "corecontracts" } -func (c *Controller) handleViewCallError(err error, chainID isc.ChainID) error { +func (c *Controller) handleViewCallError(err error) error { if errors.Is(err, interfaces.ErrChainNotFound) { - return apierrors.ChainNotFoundError(chainID.String()) + return apierrors.ChainNotFoundError() } return apierrors.ContractExecutionError(err) } diff --git a/packages/webapi/controllers/corecontracts/errors.go b/packages/webapi/controllers/corecontracts/errors.go index 097f7f09e9..4b96b7e7ae 100644 --- a/packages/webapi/controllers/corecontracts/errors.go +++ b/packages/webapi/controllers/corecontracts/errors.go @@ -15,9 +15,9 @@ type ErrorMessageFormatResponse struct { } func (c *Controller) getErrorMessageFormat(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } contractHname, err := params.DecodeHNameFromHNameHexString(e, "contractHname") if err != nil { @@ -31,7 +31,7 @@ func (c *Controller) getErrorMessageFormat(e echo.Context) error { messageFormat, err := corecontracts.ErrorMessageFormat(ch, contractHname, uint16(errorID), e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } errorMessageFormatResponse := &ErrorMessageFormatResponse{ diff --git a/packages/webapi/controllers/corecontracts/governance.go b/packages/webapi/controllers/corecontracts/governance.go index 7cfd76898b..70a7ba27bd 100644 --- a/packages/webapi/controllers/corecontracts/governance.go +++ b/packages/webapi/controllers/corecontracts/governance.go @@ -31,14 +31,14 @@ func MapGovChainInfoResponse(chainInfo *isc.ChainInfo) models.GovChainInfoRespon } func (c *Controller) getChainInfo(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } chainInfo, err := corecontracts.GetChainInfo(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } chainInfoResponse := MapGovChainInfoResponse(chainInfo) @@ -47,14 +47,14 @@ func (c *Controller) getChainInfo(e echo.Context) error { } func (c *Controller) getChainOwner(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } chainOwner, err := corecontracts.GetChainOwner(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } chainOwnerResponse := models.GovChainOwnerResponse{ @@ -65,14 +65,14 @@ func (c *Controller) getChainOwner(e echo.Context) error { } func (c *Controller) getAllowedStateControllerAddresses(e echo.Context) error { - ch, chainID, err := controllerutils.ChainFromParams(e, c.chainService) + ch, _, err := controllerutils.ChainFromParams(e, c.chainService) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } addresses, err := corecontracts.GetAllowedStateControllerAddresses(ch, e.QueryParam(params.ParamBlockIndexOrTrieRoot)) if err != nil { - return c.handleViewCallError(err, chainID) + return c.handleViewCallError(err) } encodedAddresses := make([]string, len(addresses)) diff --git a/packages/webapi/controllers/requests/offledger.go b/packages/webapi/controllers/requests/offledger.go index 2ac95440fd..58523088ac 100644 --- a/packages/webapi/controllers/requests/offledger.go +++ b/packages/webapi/controllers/requests/offledger.go @@ -28,7 +28,7 @@ func (c *Controller) handleOffLedgerRequest(e echo.Context) error { e.Set(controllerutils.EchoContextKeyChainID, chainID) if !c.chainService.HasChain(chainID) { - return apierrors.ChainNotFoundError(chainID.String()) + return apierrors.ChainNotFoundError() } requestDecoded, err := iotago.DecodeHex(request.Request) From 0beef3d20e95b23cffbc3ba77bf176a83d60782e Mon Sep 17 00:00:00 2001 From: Nikita Nikolashyn Date: Thu, 25 Jul 2024 16:34:59 +0300 Subject: [PATCH 8/9] chore: Fixed lint --- packages/isc/chainid_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/isc/chainid_test.go b/packages/isc/chainid_test.go index 78aef2fa96..d39fc72df4 100644 --- a/packages/isc/chainid_test.go +++ b/packages/isc/chainid_test.go @@ -3,8 +3,9 @@ package isc import ( "testing" - "github.com/iotaledger/wasp/packages/util/rwutil" "github.com/stretchr/testify/require" + + "github.com/iotaledger/wasp/packages/util/rwutil" ) func TestChainIDSerialization(t *testing.T) { From 7cf41a5ee757299b6a3bc5c673c115b4cdfbc8f5 Mon Sep 17 00:00:00 2001 From: Nikita Nikolashyn Date: Thu, 25 Jul 2024 16:40:19 +0300 Subject: [PATCH 9/9] fix: Removed accidentally pushed debug code --- packages/isc/chainid.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/isc/chainid.go b/packages/isc/chainid.go index 3bb9dfccf4..c769f40175 100644 --- a/packages/isc/chainid.go +++ b/packages/isc/chainid.go @@ -127,10 +127,6 @@ func (id ChainID) ShortString() string { // String human-readable form (bech32) func (id ChainID) String() string { - if id.Empty() { - return "" - } - return id.AsAddress().Bech32(parameters.L1().Protocol.Bech32HRP) }