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

Bqeth testing and usage script #289

Closed
wants to merge 127 commits into from

Conversation

theref
Copy link
Contributor

@theref theref commented Jul 9, 2024

cygnusv and others added 30 commits April 15, 2024 21:35
wip: change of plans was a mistakerino
Co-authored-by: Manuel Montenegro <manuel@nucypher.com>
Co-authored-by: David Núñez <david@nucypher.com>
Copy link
Member

@manumonti manumonti left a comment

Choose a reason for hiding this comment

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

LGTM! 👌

scripts/bqeth_subscription.py Outdated Show resolved Hide resolved
scripts/bqeth_subscription.py Show resolved Hide resolved
scripts/bqeth_subscription.py Outdated Show resolved Hide resolved

# Load environment variables
load_dotenv()
PROVIDER_URL = os.environ.get("PROVIDER_URL")
Copy link
Member

Choose a reason for hiding this comment

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

This can be a CLI parameter instead of an env-var. This may alleviate some potential issues when changing providers between mainnet and testnet while testing. Also, if you want to keep the env var functionality, click provides some out-of-the-box functionality for this:

https://click.palletsprojects.com/en/8.1.x/options/#values-from-environment-variables
https://click.palletsprojects.com/en/8.1.x/options/#dynamic-defaults-for-prompts

scripts/bqeth_subscription.py Outdated Show resolved Hide resolved
scripts/bqeth_subscription.py Outdated Show resolved Hide resolved
Comment on lines +179 to +181
tx_hash = context.subscription_contract.functions.payForEncryptorSlots(extra_slots).transact(
{"from": context.account.address}
)
Copy link
Member

@KPrasch KPrasch Aug 5, 2024

Choose a reason for hiding this comment

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

  1. Does this function also imply the transfer of ERC20 fees to the subscription contract?
  2. If so - we may need to include ERC20 approval here also?
  3. Perhaps this is related to how you handle MAX_NODES above as a means of "pre-approving" an ERC20 amount?

def initiate_ritual(context, num_nodes, duration, random_seed, num_rituals):
"""Initiate a ritual."""
if NUCYPHER_DOMAIN == "mainnet":
porter_request = f"https://porter.nucypher.community/bucket_sampling?quantity={num_nodes}&random_seed={random_seed}"
Copy link
Member

Choose a reason for hiding this comment

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

Instead of URL-encoding in a string template, requests does have the ability to accept a dictionary of params.

Comment on lines 212 to 214
click.echo(f"Account address that will sign the transaction: {context.account.address}")
click.echo(f"Initiating ritual with {num_nodes} providers for {duration} seconds.")
click.echo(f"Endpoint used: {PROVIDER_URL}")
Copy link
Member

Choose a reason for hiding this comment

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

These can be combined into a single expression.

tx_hash = context.coordinator_contract.functions.initiateRitual(
context.subscription_contract.address,
nodes,
context.account.address, # TODO: parametrize this
Copy link
Member

Choose a reason for hiding this comment

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

This is the authority address of the ritual. For production use-cases this will not be the same as the initiator so we need to parameterize this.

@KPrasch KPrasch requested a review from vzotova August 5, 2024 10:00
@click.option("--random-seed", help="Random seed integer for sampling.")
@click.option("--num-rituals", default=1, help="Number of rituals to initiate.", type=int)
@setup_context
def initiate_ritual(context, num_nodes, duration, random_seed, num_rituals):
Copy link
Member

Choose a reason for hiding this comment

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

This function has a sufficiently different scope than subscription payment/management and will be executed by a different permissioned role. IMO this command can be removed from this PR entirely. In fact, there is already a ritual initiation script in this same directory.

- Use typed syntax instead of named tuple
- CHAIN is now derived from domain
- domain is a cli argument
- dotenv overrides each time the script is used
- MAX NODES is removed
def build_constants(domain, provider_url, private_key):
DOMAIN_TO_CHAIN = {"lynx": "80002", "tapir": "80002", "mainnet": "137"}
chain = DOMAIN_TO_CHAIN[domain]
PORTER_ENDPOINT = f"https://porter-{domain}.nucypher.community"
Copy link
Member

@KPrasch KPrasch Aug 7, 2024

Choose a reason for hiding this comment

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

  1. Will this work on mainnet given the url is porter.nucypher.io (without hyphen)?
  2. Let's use the .io domain instead of the .community domain (community is planed for deprecation) https://porter.nucypher.io
  3. Consider using the PORTER_SAMPLING_ENDPOINTS constant defined in `deployments/constants.py



@cli.command()
@click.option("--num-nodes", default=2, help="Number of nodes to use for the ritual.")
Copy link
Member

Choose a reason for hiding this comment

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

IMO this can be required without a default since each ritual is intentionally created for a specific purpose.

Suggested change
@click.option("--num-nodes", default=2, help="Number of nodes to use for the ritual.")
@click.option("--num-nodes", required=True, help="Number of nodes to use for the ritual.")

[
u["checksum_address"]
for u in requests.get(
f"{context.constants['PORTER_ENDPOINT']}/get_ursulas?quantity={num_nodes}"
Copy link
Member

Choose a reason for hiding this comment

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

Do you still intend to use the bucket sampling endpoint for mainnet - or is this script only meant for testnet now?

Comment on lines +203 to +210
nodes = list(sorted(
[
u["checksum_address"]
for u in requests.get(
f"{context.constants['PORTER_ENDPOINT']}/get_ursulas?quantity={num_nodes}"
).json()["result"]["ursulas"]
]
))
Copy link
Member

Choose a reason for hiding this comment

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

This comprehension has grown beyond readability and will be more maintainable if it's broken up into a few variables instead.

Comment on lines +211 to +216
start_of_subscription = context.subscription_contract.functions.startOfSubscription().call()
duration = (
context.subscription_contract.functions.subscriptionPeriodDuration().call()
+ context.subscription_contract.functions.yellowPeriodDuration().call()
+ context.subscription_contract.functions.redPeriodDuration().call()
)
Copy link
Member

Choose a reason for hiding this comment

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

Ah - This is very interesting functionality! Just making sure I understand correctly it looks like you are correlating a ritual's duration with the specific temporal rules of a subscription?

Copy link
Member

Choose a reason for hiding this comment

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

@theref Can you make a commit into PR #297 to include these calculations?

Copy link
Member

Choose a reason for hiding this comment

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

#297 is merged now but it will still be good to include these changes in initiate_ritual.py @theref Do you want to take care of that?

scripts/bqeth_subscription.py Show resolved Hide resolved


def build_constants(domain, provider_url, private_key):
DOMAIN_TO_CHAIN = {"lynx": "80002", "tapir": "80002", "mainnet": "137"}
Copy link
Member

Choose a reason for hiding this comment

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

There are string constants for these domain strings in deployments/constants.py

Suggested change
DOMAIN_TO_CHAIN = {"lynx": "80002", "tapir": "80002", "mainnet": "137"}
DOMAIN_TO_CHAIN = {LYNX: "80002", TAPIR: "80002", MAINNET: "137"}

Comment on lines +70 to +71
"ERC20_CONTRACT_ADDRESS": registry[chain][f"{domain.title()}RitualToken"]["address"],
"ERC20_CONTRACT_ABI": registry[chain][f"{domain.title()}RitualToken"]["abi"],
Copy link
Member

@KPrasch KPrasch Aug 7, 2024

Choose a reason for hiding this comment

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

Is this script intended only for testnets (absolutely fine if it is given the name of the PR is "testing")?

@theref
Copy link
Contributor Author

theref commented Aug 7, 2024

@KPrasch do we still need this with #299 ?

@KPrasch
Copy link
Member

KPrasch commented Aug 7, 2024

@KPrasch do we still need this with #299 ?

Do you mean this entire PR? This script broke the ice for testnet fee model deployments but I'm in favor of closing this PR as long as you feel that all of the useful bits have been included elsewhere. Also, I want to acknowledge that #299 is based off your initial work here (I made sure to include you as a commit author).

That being said (almost) all of the functionality implemented in this PR has been implemented in #299 and #297. The main difference is that this PR has fewer dependencies (#299 heavily depends on eth-ape abstractions). Personally, I do not view that as a major issue. There are a few interesting bits of logic here, particularly with regard to ritual duration calculation that I'd like to understand better so we may possibly include the same changes in #297. (see #289 (comment))

Additional thoughts?

@KPrasch KPrasch deleted the branch nucypher:epic-subscription August 15, 2024 13:28
@KPrasch KPrasch closed this Aug 15, 2024
@KPrasch
Copy link
Member

KPrasch commented Aug 15, 2024

This branch was closed by automation after deleting the epic-subscription branch. I suggest making a new PR into main for any additional follow-ups.

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.

7 participants