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

Support prefix notation for compound conditions #226

Merged
merged 8 commits into from
Jun 19, 2023

Conversation

derekpierre
Copy link
Member

@derekpierre derekpierre commented Jun 15, 2023

Type of PR:

  • Bugfix
  • Feature
  • Documentation
  • Refactor
  • Other

Required reviews:

How many reviews does the PR author need?

  • 1
  • 2
  • 3

What this does:
Implements prefix notation for compound conditions in nucpyher-ts.

Related to:

Issues fixed/closed:

  • Fixes #...

Fixes #224 .

Why it's needed:

Explain how this PR fits in the greater context of the NuCypher Network.
E.g., if this PR address a nucypher/productdev issue, let reviewers know!

Notes for reviewers:

What should reviewers focus on?
Is there a particular commit/function/section of your PR that requires more attention from reviewers?

  • ConditionSet should be renamed, and will be when we introduce versioning in a follow-up PR.

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2023

Codecov Report

Merging #226 (e810f42) into tdec-epic (c0d33d5) will increase coverage by 1.34%.
The diff coverage is 100.00%.

@@              Coverage Diff              @@
##           tdec-epic     #226      +/-   ##
=============================================
+ Coverage      82.35%   83.70%   +1.34%     
=============================================
  Files             37       37              
  Lines            958      945      -13     
  Branches         117      116       -1     
=============================================
+ Hits             789      791       +2     
+ Misses           162      147      -15     
  Partials           7        7              
Impacted Files Coverage Δ
src/conditions/base/condition.ts 88.88% <ø> (-0.59%) ⬇️
src/characters/pre-recipient.ts 84.05% <100.00%> (+0.47%) ⬆️
src/conditions/base/contract.ts 88.88% <100.00%> (+0.65%) ⬆️
src/conditions/base/rpc.ts 100.00% <100.00%> (ø)
src/conditions/base/time.ts 100.00% <100.00%> (ø)
src/conditions/compound-condition.ts 100.00% <100.00%> (ø)
src/conditions/condition-set.ts 100.00% <100.00%> (+22.44%) ⬆️
src/conditions/context/context.ts 97.82% <100.00%> (+0.09%) ⬆️
src/conditions/index.ts 100.00% <100.00%> (ø)

@derekpierre derekpierre changed the title [WIP] Support prefix notation for compound conditions Support prefix notation for compound conditions Jun 16, 2023
@derekpierre derekpierre marked this pull request as ready for review June 16, 2023 17:55

export const STANDARD_CONTRACT_TYPES = ['ERC20', 'ERC721'];

const functionAbiVariable = Joi.object({
internalType: Joi.string().required(),
internalType: Joi.string(), // TODO is this needed?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think yes, working on a related change here: #228


describe('accepts either standardContractType or functionAbi but not both or none', () => {
const standardContractType = 'ERC20';
const functionAbi = {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a testFunctionAbi in testVariables.ts if we want to reuse it.

Comment on lines +62 to +69
it.each([
erc721BalanceCondition,
contractConditionNoAbi,
contractConditionWithAbi,
rpcCondition,
timeCondition,
compoundCondition,
])('serializes to and from json', async (condition) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Exquisite

Comment on lines +80 to +104
// no "operator" nor "method" value
{
randoKey: 'randoValue',
otherKey: 'otherValue',
},
// invalid "method" and no "contractAddress"
{
method: 'doWhatIWant',
returnValueTest: {
index: 0,
comparator: '>',
value: '100',
},
chain: 5,
},
// condition with wrong method "method" and no contract address
{
...testTimeConditionObj,
method: 'doWhatIWant',
},
// rpc condition (no contract address) with disallowed method
{
...testRpcConditionObj,
method: 'isPolicyActive',
},
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using permutations here to exhaustively test for every possible incorrect variant? Nothing from with the current implementation, I'm just interested in your thoughts.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm intrigued - could you elaborate a bit more on what you are thinking? Just want to make sure I understand.

The goal for this test was to test the ConditionSet.fromObj(...) branching logic for invalid data.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could generate a set of all possible permutations of the form <field>: <value>, where both field and value may contain valid and invalid values. From this set we remove permutations we know to be correct. Others should fail the validation, and so the test cases would be successful.

I think without a property-based testing library this is tiresome to implement. I did consider this type of testing in other places on different occasions.

Copy link
Member Author

Choose a reason for hiding this comment

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

This could be good in a variety of areas for hardening testing. I guess one thing, based on your comment, is effort-to-benefit ratio, but if there is a tool in typescript to help with this that could change the equation and be pretty useful.

Assuming we specify the field validation correctly 😅 ., the JOI schema definitions help us in this general area; but hardening of tests is a good thing.

In this case, it's less about the validation by the Joi schema (actual condition), and more about being able to figure out what potential type of condition it is and then having that specific condition type perform further validation. The logic for determining the type of condition is pretty specific, so randomizing field values may provide marginal benefit in this case, but could provide larger benefit in other areas of the code. Good thing to keep in mind moving forward 👍 .

@derekpierre derekpierre merged commit e39109f into nucypher:tdec-epic Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

4 participants