Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Gracefully handle CardanoTokenRegistry errors during getAsset request #412

Merged
merged 1 commit into from
Sep 13, 2022

Conversation

ivaylo-andonov
Copy link
Contributor

@ivaylo-andonov ivaylo-andonov commented Aug 30, 2022

Context

AssetInfo.tokenMetatdata is stored in an off-chain registry, which cannot be depended on for returning a match for the given assetIds, nor it's service availability. Currently we’re rethrowing request errors as a Provider error, which is not ideal since it then couples our own service health, as opposed to the desired behaviour of omitting the metadata from the response.
cardano-js-sdk/CardanoTokenRegistry.ts at 8fe1cdd7683a7802bebd081616abead292488953 · input-output-hk/cardano-js-sdk

This issue does raise an important question of how can a client differentiate between a response that included a 404 from the registry vs a 5xx with our current shape of from AssetInfo.tokenMetadata

Proposed Solution

Approach A)
Introduce the suggested approach from the task description with minor diffs as follows:

interface TokenMetadataSource {
  type: MetadataSourceType; \\ enum to support: 'registry', 'on-chain' or 'referenced'  
  requestStatus: 'success' | 'fail'; 
}

export interface AssetInfo<Source> {
  ...
  tokenMetadata?: { data?: TokenMetadata | null, source?: Source } ;
  ...
}

// Provider interface use AssetInfo<unknown>
// cardano-services use AssetInfo<TokenMetadataSource>

Notes:

  • cached: boolean is committed because it's not so related to the Source scope, however, it could be added easily with correct values within CardanoTokenRegistry easily
  • source? property is nullable to keep the compatibility with the other providers which implement sameAssetProvider such as blockfrost. Not sure what source.type in blockfrost should be since the tokenMetadata is fetched as part of the whole blockfrost.assetsById() response.
  • in cardano-services is used AssetInfo<TokenMetadataSource>, everywhere else is AssetInfo<unknown>
  • token metadata responses are adjusted based on the interface changes within the tests (wrapper into data obj and added source obj in cardano-services)

Approach B) Omitting the metadata from the response in case of 5xx error thrown by the external source (in our current situation - token metadata server)

Following the CIP-0035 comment, the second approach could be simply to avoid the AssetInfo interface changes and to keep it as it is:
tokenMetadata?: TokenMetadata | null; with the only difference is to shallow the ProviderError in catch block and to omit tokenMetadata in case of network error by the external token server by returning simply null.

The downside here is losing the details of the source/origin of that data.

Important Changes Introduced

@ivaylo-andonov ivaylo-andonov self-assigned this Aug 30, 2022
@ivaylo-andonov ivaylo-andonov force-pushed the feat/handle-get-asset-request-errors branch from 818365f to d08353b Compare August 30, 2022 19:34
@ivaylo-andonov ivaylo-andonov requested a review from rhyslbw August 30, 2022 19:34
Copy link
Member

@rhyslbw rhyslbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far @IvayloAndonov, just a minor change request

packages/cardano-services/src/Asset/types.ts Outdated Show resolved Hide resolved
@ivaylo-andonov ivaylo-andonov marked this pull request as ready for review August 31, 2022 07:05
@ivaylo-andonov ivaylo-andonov requested a review from rhyslbw August 31, 2022 07:05
iadmytro
iadmytro previously approved these changes Sep 5, 2022
Copy link
Member

@rhyslbw rhyslbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given there's currently only a single source for this metadata, let's revert the breaking AssetInfo type change given tokenMetadata === undefined can be used to infer the success of the registry request.

@ivaylo-andonov ivaylo-andonov force-pushed the feat/handle-get-asset-request-errors branch 2 times, most recently from a3c34af to 039e0f0 Compare September 8, 2022 14:17
@ivaylo-andonov
Copy link
Contributor Author

Rebased with master and resolved conflicts.

Revert the breaking AssetInfo type change: 039e0f0 (it eliminates most of the previously introduced changes)

Copy link
Member

@rhyslbw rhyslbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DbSyncAssetProvider test is missing the assertion that demonstrates the assetInfo.tokenMetadata value is undefined if the asset is not found in the token registry.

rhyslbw
rhyslbw previously approved these changes Sep 12, 2022
mkazlauskas
mkazlauskas previously approved these changes Sep 12, 2022
Copy link
Member

@mkazlauskas mkazlauskas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 🚀

@ivaylo-andonov ivaylo-andonov dismissed stale reviews from mkazlauskas and rhyslbw via 938bf9c September 12, 2022 12:23
@ivaylo-andonov ivaylo-andonov force-pushed the feat/handle-get-asset-request-errors branch from 09b85b3 to 938bf9c Compare September 12, 2022 12:23
@ivaylo-andonov
Copy link
Contributor Author

Rebased with master and squashed commits.

@rhyslbw rhyslbw self-requested a review September 12, 2022 12:26
- lift up error handing into `DbSyncAssetProvider
- return `undefined` if there is an error while fetch from token metadata server
@ivaylo-andonov ivaylo-andonov force-pushed the feat/handle-get-asset-request-errors branch from 938bf9c to 32a9b1f Compare September 12, 2022 12:43
@ivaylo-andonov ivaylo-andonov changed the title refactor: Gracefully handle CardanoTokenRegistry errors during getAsset request fix: Gracefully handle CardanoTokenRegistry errors during getAsset request Sep 12, 2022
@rhyslbw rhyslbw added fix and removed refactor labels Sep 12, 2022
@rhyslbw rhyslbw merged commit f3461b6 into master Sep 13, 2022
@rhyslbw rhyslbw deleted the feat/handle-get-asset-request-errors branch September 13, 2022 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants