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 CLI by adding add new click types for fees, amounts, addresses and bytes32 #15718

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

jack60612
Copy link
Contributor

@jack60612 jack60612 commented Jul 9, 2023

Purpose:

  • Adds new TransactionFeeParamType for automatic fee conversion
  • Adds new CliAmount class and AmountParamType for easy conversion between units.
  • Adds new CliAddress class and AddressParamType for easy bech32m address conversion and wallet type tracking
  • Adds new Bytes32ParamType for automatic str to bytes32 conversion.
  • Lastly this pr does more standardization too.

Merge requirements:

Current Behavior:

No Breaking Changes, just lots of backend changes.

New Behavior:

N/A

Testing Notes:

This PR has unit tests for the new types, and actual cli tests are coming in #15718

@jack60612 jack60612 added Changed Required label for PR that categorizes merge commit message as "Changed" for changelog CI CI changes labels Jul 9, 2023
@github-actions github-actions bot added the merge_conflict Branch has conflicts that prevent merge to main label Jul 10, 2023
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@jack60612 jack60612 changed the title Add --mojos argument to the CLI to allow the use of mojo instead of xch, and add lots of new classes as part of the CLI refactor Refactor CLI Classes and add new click types for fees, amounts, addresses and bytes32 Jul 10, 2023
@jack60612 jack60612 changed the title Refactor CLI Classes and add new click types for fees, amounts, addresses and bytes32 Refactor CLI by adding add new click types for fees, amounts, addresses and bytes32 with auto conversion in each class. Jul 10, 2023
@github-actions github-actions bot removed the merge_conflict Branch has conflicts that prevent merge to main label Jul 10, 2023
@github-actions
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

@jack60612 jack60612 changed the title Refactor CLI by adding add new click types for fees, amounts, addresses and bytes32 with auto conversion in each class. Refactor CLI by adding add new click types for fees, amounts, addresses and bytes32 Jul 10, 2023
@jack60612 jack60612 marked this pull request as ready for review July 11, 2023 02:12
@jack60612 jack60612 requested a review from a team as a code owner July 11, 2023 02:12
@jack60612 jack60612 requested a review from arvidn July 11, 2023 02:12
Copy link
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

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

I appreciate this integration with Click. I think the new classes should have unit tests though.

chia/cmds/param_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Show resolved Hide resolved
@wallentx
Copy link
Contributor

Failure. Coverage is below 100%.

Diff Coverage
Diff: origin/main...HEAD, staged and unstaged changes

chia/cmds/coin_funcs.py (0.0%): Missing lines 7,44-45,109,111,143-144,148,184,188-189,220,223
chia/cmds/coins.py (100%)
chia/cmds/data.py (100%)
chia/cmds/data_funcs.py (23.5%): Missing lines 17,24,35,45,55,64,74,83,93,104,113,153,174
chia/cmds/param_types.py (92.9%): Missing lines 30-33,78-81
chia/cmds/plotnft.py (85.7%): Missing lines 84
chia/cmds/plotnft_funcs.py (0.0%): Missing lines 19,29,36,327,344,355,358
chia/cmds/wallet.py (84.8%): Missing lines 585,612,665,1303-1304
chia/cmds/wallet_funcs.py (16.7%): Missing lines 374-379,381-383,652,970,1015,1043-1044,1129,1286,1288,1337,1344,1349
chia/rpc/wallet_rpc_client.py (100%)
tests/cmds/test_types.py (100%)
tests/pools/test_pool_cmdline.py (100%)

Total: 318 lines
Missing: 67 lines
Coverage: 78%

@jack60612
Copy link
Contributor Author

Failure. Coverage is below 100%.

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

chia/cmds/coin_funcs.py (0.0%): Missing lines 7,44-45,109,111,143-144,148,184,188-189,220,223

chia/cmds/coins.py (100%)
chia/cmds/data.py (100%)
chia/cmds/data_funcs.py (23.5%): Missing lines 17,24,35,45,55,64,74,83,93,104,113,153,174
chia/cmds/param_types.py (92.9%): Missing lines 30-33,78-81
chia/cmds/plotnft.py (85.7%): Missing lines 84
chia/cmds/plotnft_funcs.py (0.0%): Missing lines 19,29,36,327,344,355,358
chia/cmds/wallet.py (84.8%): Missing lines 585,612,665,1303-1304
chia/cmds/wallet_funcs.py (16.7%): Missing lines 374-379,381-383,652,970,1015,1043-1044,1129,1286,1288,1337,1344,1349
chia/rpc/wallet_rpc_client.py (100%)
tests/cmds/test_types.py (100%)
tests/pools/test_pool_cmdline.py (100%)

Total: 318 lines

Missing: 67 lines
Coverage: 78%

this only tests the new types class, and the missing lines in in param funcs are expected, as the mojos part isn't user available yet.

Copy link
Contributor

@arvidn arvidn 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. Could you expand a bit on why you need the isinstance in the convert functions? it looks risky. If it's just for the default value, would it work yo change the type to the default value to str to look just like what would be passed on the command line?

chia/cmds/coin_funcs.py Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
@arvidn
Copy link
Contributor

arvidn commented Jul 26, 2023

It would make sense to hold off landing this until we have test coverage, right?

Diff: origin/main...HEAD, staged and unstaged changes
-------------
chia/cmds/coin_funcs.py (0.0%): Missing lines 7,44-45,109,111,143-144,148,184,188-189,220,223
chia/cmds/coins.py (100%)
chia/cmds/data.py (100%)
chia/cmds/data_funcs.py (23.5%): Missing lines 17,24,35,45,55,64,74,83,93,104,113,153,174
chia/cmds/param_types.py (92.9%): Missing lines 30-33,78-81
chia/cmds/plotnft.py (85.7%): Missing lines 84
chia/cmds/plotnft_funcs.py (0.0%): Missing lines 19,29,36,327,344,355,358
chia/cmds/wallet.py (84.8%): Missing lines 585,612,665,1303-1304
chia/cmds/wallet_funcs.py (16.7%): Missing lines 374-379,381-383,652,970,1015,1043-1044,1129,1286,1288,1337,1344,1349
chia/rpc/wallet_rpc_client.py (100%)
tests/cmds/test_types.py (100%)
tests/pools/test_pool_cmdline.py (100%)

@jack60612
Copy link
Contributor Author

It would make sense to hold off landing this until we have test coverage, right?

Diff: origin/main...HEAD, staged and unstaged changes
-------------
chia/cmds/coin_funcs.py (0.0%): Missing lines 7,44-45,109,111,143-144,148,184,188-189,220,223
chia/cmds/coins.py (100%)
chia/cmds/data.py (100%)
chia/cmds/data_funcs.py (23.5%): Missing lines 17,24,35,45,55,64,74,83,93,104,113,153,174
chia/cmds/param_types.py (92.9%): Missing lines 30-33,78-81
chia/cmds/plotnft.py (85.7%): Missing lines 84
chia/cmds/plotnft_funcs.py (0.0%): Missing lines 19,29,36,327,344,355,358
chia/cmds/wallet.py (84.8%): Missing lines 585,612,665,1303-1304
chia/cmds/wallet_funcs.py (16.7%): Missing lines 374-379,381-383,652,970,1015,1043-1044,1129,1286,1288,1337,1344,1349
chia/rpc/wallet_rpc_client.py (100%)
tests/cmds/test_types.py (100%)
tests/pools/test_pool_cmdline.py (100%)

In my mind this increases overall coverage by centralizing where everything is converted, but we can hold it off if you want those other tests first.

@github-actions github-actions bot added the merge_conflict Branch has conflicts that prevent merge to main label Jul 27, 2023
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

tests/cmds/test_click_types.py Outdated Show resolved Hide resolved
tests/cmds/test_click_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
Copy link
Contributor

@altendky altendky left a comment

Choose a reason for hiding this comment

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

I didn't repeat a detailed review yet. Let's address the existing open issues then go back over the whole thing.

Co-authored-by: Kyle Altendorf <sda@fstab.net>
Copy link
Contributor

File Coverage Missing Lines
chia/cmds/coin_funcs.py 75.0% lines 106, 108
chia/cmds/data_funcs.py 0.0% lines 45, 58, 70, 81, 92, 102, 113, 124, 135, 147, 157, 215, 243
chia/cmds/param_types.py 93.3% lines 33-34, 74, 80-81, 126-128, 165, 202
chia/cmds/plotnft.py 90.9% lines 79
chia/cmds/plotnft_funcs.py 0.0% lines 19, 29, 36, 340, 357, 368, 371
chia/cmds/wallet_funcs.py 82.1% lines 295, 299, 301-302, 662, 1398, 1552
Total Missing Coverage
484 lines 40 lines 91%

Copy link
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

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

looks reasonable. except it looks like that block of code in validate_uint64() could be removed

chia/cmds/param_types.py Show resolved Hide resolved
altendky
altendky previously approved these changes Jan 4, 2024
Copy link
Contributor

@altendky altendky left a comment

Choose a reason for hiding this comment

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

Looks like a couple still open conversations to finish with.

chia/cmds/param_types.py Outdated Show resolved Hide resolved
chia/cmds/param_types.py Outdated Show resolved Hide resolved
@altendky altendky dismissed their stale review January 4, 2024 13:37

just meant to comment, not approve. sorry

Copy link
Contributor

@altendky altendky left a comment

Choose a reason for hiding this comment

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

previous approval was meant to be a comment

@arvidn
Copy link
Contributor

arvidn commented Jan 6, 2024

the cmd test on windows failed twice in a row (with what looks like a time out). I would expect all of these tests to be 100% deterministic, and not be flaky.

@arvidn
Copy link
Contributor

arvidn commented Jan 6, 2024

Copy link
Contributor

@altendky altendky left a comment

Choose a reason for hiding this comment

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

alrighty, this covers the param types and the test file for them. i'll try to get to the rest of the files, but expect them to be a lot less interesting.

try:
d_value = Decimal(value)
except InvalidOperation:
fail_func("Value must be a valid uint64 number", param, ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

uint64 isn't the proper description here. Should also have the actual failure probably, as noted elsewhere.

) -> Decimal:
try:
d_value = Decimal(value)
except InvalidOperation: # won't ever be value error because of the fee limit check
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment doesn't seem applicable.

Suggested change
except InvalidOperation: # won't ever be value error because of the fee limit check
except InvalidOperation:

fail_func("Value must be decimal dotted value in XCH (e.g. 0.00005)", param, ctx)
if d_value.is_signed():
fail_func("Value can not be negative", param, ctx)
if not d_value.is_zero() and d_value < Decimal("0.000000000001"):
Copy link
Contributor

Choose a reason for hiding this comment

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

This still allows, for example, 0.0000000000015 which isn't valid. Unless there is a specific feature for this, we might have to multiply by whatever-our-xch-mojo-conversion-constant is then .to_integral() and compare.

Alternatively, we delete this conversion function and just multiply by the xch-mojo conversion when needed and then use the validate-uint64. The caveat being that ... iirc, balances can be uint128 but coin amounts can only be uint64. So maybe there's a relevant detail there.

A Click parameter type for transaction fees, which can be specified in XCH or mojos.
"""

name: str = uint64.__name__ # output type
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically these are all ClassVars such as name: ClassVar[str]. Probably the same across all these classes.

A Click parameter type for transaction fees, which can be specified in XCH or mojos.
"""

name: str = uint64.__name__ # output type
Copy link
Contributor

Choose a reason for hiding this comment

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

Aside from the hinting, I'm not sure this is right. I did suggest uint64.__name__ instead of "uint64" since it seemed to be the intent to be tied to the Python type we convert to. But, I'm not entirely sure that we want that as the name.

https://click.palletsprojects.com/en/8.1.x/api/#click.ParamType.name

the descriptive name of this type

Maybe we should figure out where this is used so we can make sure we have useful text there. Or maybe uint64 still ends up sufficiently useful depending.

Or, we can just keep this in mind for later consideration.

Comment on lines +171 to +172
from chia.util.config import load_config
from chia.util.default_root import DEFAULT_ROOT_PATH
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a need for these to not be global scope imports?

Comment on lines +180 to +185
if hrp == expected_prefix:
addr_type = AddressType.XCH
else:
self.fail(f"Unexpected Address Prefix: {hrp}, are you sure its for the right network?", param, ctx)
else:
addr_type = AddressType(hrp)
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to close this after reading.

It feels like this pattern belongs on the AddressType class. The 'to hrp' helper already takes a config so depending on that wouldn't be an issue. It should probably also provide the xch/txch options in some way. An idea for another time.

# TODO: Test MOJO Logic When Implemented

# Test Decimal / XCH
assert TransactionFeeParamType().convert("0.5", None, None) == uint64(int(0.5 * units["chia"]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert TransactionFeeParamType().convert("0.5", None, None) == uint64(int(0.5 * units["chia"]))
assert TransactionFeeParamType().convert("0.5", None, None) == uint64(Decimal("0.5") * units["chia"])

AmountParamType().convert("-0.6", None, None)
# Test Type Failures
with pytest.raises(BadParameter):
AmountParamType().convert(float(0.01), None, None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
AmountParamType().convert(float(0.01), None, None)
AmountParamType().convert(0.01, None, None)

AmountParamType().convert(float(0.01), None, None)

# Test CliAmount Class
assert decimal_cli_amount.convert_amount(units["chia"]) == uint64(int(5.25 * units["chia"]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert decimal_cli_amount.convert_amount(units["chia"]) == uint64(int(5.25 * units["chia"]))
assert decimal_cli_amount.convert_amount(units["chia"]) == uint64(Decimal("5.25") * units["chia"])

@github-actions github-actions bot added the merge_conflict Branch has conflicts that prevent merge to main label Jan 16, 2024
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

1 similar comment
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@wjblanke
Copy link
Contributor

Lets discuss this again with #16802

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changed Required label for PR that categorizes merge commit message as "Changed" for changelog CI CI changes coverage-diff merge_conflict Branch has conflicts that prevent merge to main
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Combining coins with fee
6 participants