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

refactor(core-state): wallet repository search #3911

Closed
wants to merge 11 commits into from

Conversation

rainydio
Copy link
Contributor

@rainydio rainydio commented Jul 23, 2020

Summary

Work in progress, fixes #3591. More changes and description to follow.

  • Wallet.getAttributes method is replaced with Wallet.attributes property.
  • /wallets endpoints for both raw and transformed API responses return only 5 properties (address, publicKey, balance, nonce, attributes)
  • Other properties were removed (there was TODO mentioning that those should be removed in v3).
  • Search criteria syntax now allows precise filtering of any attribute value.

It's now possible to extract any information from wallet repository through /wallets/search endpoint.


Syntax for /wallets/search endpoint closely resembles one used in /transactions/search with a small difference that it extends deeper than a single level. It's possible to filter down to a particular range of values in attribute, something that is not possible with transaction asset due to postgres's @> operator limitations.

Additionally GET endpoints also allow building criteria object by using dot notation (e.g. /wallets?attributes.delegate.resigned=false). There is no request schema anymore, it requires knowing precise layout of attributes which isn't available.

String properties

String property criteria may include % wildcard, similar to SQL's LIKE:

const criteria = {
  attributes: {
    delegate: {
      username: "genesis_%"
    }
  }
}

Boolean properties

Boolean values are additionally compared to "true" and "false" string literals, because criteria built from query string will contain string values only.

const criteria = {
  attributes: {
    delegate: {
      resigned: "false"
    }
  }
}

Numeric properties

Similarly when comparing numeric value (number, BigInt, Utils.BigNumber) criteria may be an object with either or both from and to properties and may also use string to specify number.

const criteria = {
  balance: { from: "1000000" }
}

Properties as AND expression

This very natural syntax:

const criteria = {
  balance: { from: "1000" },
  attributes: {
    vote: "genesis_1"
  }
}
Wildcard property

To match any object property special wildcard * criteria property can be used:

const criteria = {
  attributes: {
    locks: {
      "*": {
        recipientId: "AReY3W6nTv3utiG2em5nefKEsGQeqEVPN4"
      }
    }
  }
}

Arrays as OR expression

Here is an actual code that is used to find wallet by either address, publicKey, or attributes.delegate.username:

private findWallet(id: string): Contracts.State.Wallet | undefined {
  const addressCriteria = { address: id };
  const publicKeyCriteria = { publicKey: id };
  const delegateUsernameCriteria = { attributes: { delegate: { username: id } } };

  return this.walletRepository.findOneByCriteria([
    addressCriteria,
    publicKeyCriteria,
    delegateUsernameCriteria
  ]);
}

Another example is from tests. Previously there was addresses criteria which is now replaced by sending array in address criteria:

const response = await api.request("POST", "wallets/search", {
  address: [address, address2],
});

Same syntax can be used for any attribute:

const criteria = {
  attributes: {
    delegate: {
      username: ["genesis_1", "genesis_2"]
    }
  }
}

I expect this syntax to be good enough for most applications to the point that no-one should bother implementing their own endpoints.

@codecov
Copy link

codecov bot commented Jul 23, 2020

Codecov Report

Merging #3911 into develop will increase coverage by 72.02%.
The diff coverage is 64.32%.

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #3911       +/-   ##
============================================
+ Coverage    13.69%   85.72%   +72.02%     
============================================
  Files          628      628               
  Lines        14531    14693      +162     
  Branches      1728     1784       +56     
============================================
+ Hits          1990    12595    +10605     
+ Misses       12426     1896    -10530     
- Partials       115      202       +87     
Flag Coverage Δ
#functional 6.70% <0.00%> (-0.08%) ⬇️
#integration 9.87% <17.83%> (+0.04%) ⬆️
#unit 83.70% <63.78%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/core-api/src/routes/wallets.ts 100.00% <ø> (ø)
...ckages/core-state/src/wallets/wallet-repository.ts 88.38% <12.50%> (+88.38%) ⬆️
packages/core-kernel/src/utils/search.ts 70.15% <57.89%> (+70.15%) ⬆️
packages/core-api/src/controllers/controller.ts 92.10% <70.00%> (+13.53%) ⬆️
packages/core-api/src/controllers/delegates.ts 100.00% <100.00%> (+10.81%) ⬆️
packages/core-api/src/controllers/wallets.ts 100.00% <100.00%> (+25.64%) ⬆️
packages/core-api/src/resources/wallet.ts 100.00% <100.00%> (+40.00%) ⬆️
packages/core-manager/src/watcher-wallet.ts 100.00% <100.00%> (+100.00%) ⬆️
...kages/core-state/src/wallets/utils/get-property.ts 100.00% <100.00%> (+100.00%) ⬆️
packages/core-state/src/wallets/wallet.ts 100.00% <100.00%> (+100.00%) ⬆️
... and 519 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 95d6b2c...83e89b3. Read the comment docs.

@rainydio rainydio closed this Aug 5, 2020
@ghost ghost removed the Status: In Progress label Aug 5, 2020
@faustbrian faustbrian deleted the refactor/core-state/wallet-repository-search branch September 22, 2021 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant