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

feat: local network process MGMT CLI #2545

Merged
merged 26 commits into from
Mar 14, 2025

Conversation

antazoey
Copy link
Member

@antazoey antazoey commented Mar 10, 2025

What I did

Allows flows like this:

ape networks run --network ::node --background
ape networks list --running
ape networks ping --network ::node
ape networks kill --all

basically Ape now tracks the processes it has started and you can easily kill them all from the command line as well more easily background the processes in the same terminal session and w/o losing the PID

fixes: #2528

Also allows

with networks.parse_network_choice("pid://12345") as running_node":
    ...

For connecting to local nodes processes via PID.
Note: this process must have been started by Ape for this to work currently though.

How I did it

copied this from the dfx tool: https://github.com/dfinity/sdk

How to verify it

tbd

Checklist

  • All changes are completed
  • Change is covered in tests
  • Documentation is complete

@antazoey antazoey marked this pull request as ready for review March 11, 2025 15:32
fubuloubu
fubuloubu previously approved these changes Mar 11, 2025
Copy link
Member

@fubuloubu fubuloubu left a comment

Choose a reason for hiding this comment

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

Hmm, would this better unlock subprocess testing in CI? At least for the integration tests

@antazoey
Copy link
Member Author

Hmm, would this better unlock subprocess testing in CI? At least for the integration tests

Probably, as it gives you more control to start up any networks you need in the beginning.. Also, you can more easily connect to providers via PID, which I want to think of a better way to incorporate that in parse_network_choice() logic, but in any case it would probably be good for silverback back testing so you can always connect to the same running fork node rather than starting up new ones constantly... you can still reset the fork to get a clean state.

@fubuloubu
Copy link
Member

you can still reset the fork to get a clean state.

Why not have this be the default way that ProviderAPI.disconnect works with those types of forks? And then just add atexit to fully shut it down on session end (if not asked to run in background)

@antazoey
Copy link
Member Author

Why not have this be the default way that ProviderAPI.disconnect works with those types of forks? And then just add atexit to fully shut it down on session end (if not asked to run in background)

Maybe when using the .fork() context, but for regular connections, you would lose your state that you probably need. Think testing bridge dApps for example... As soon as you switched to another chain, it would reset.

@antazoey
Copy link
Member Author

antazoey commented Mar 11, 2025

But I agree - when using .fork(), we could incorporate this a bit... I think that is the intended use-case of .fork() so it makes sense

Also, the atexit handler is the what does call the disconnect, not exiting the context.

@fubuloubu
Copy link
Member

fubuloubu commented Mar 12, 2025

But I agree - when using .fork(), we could incorporate this a bit... I think that is the intended use-case of .fork() so it makes sense

Would certainly be a lot faster testing this way vs. how I am doing backtests now w/ anvil in ApeWorX/silverback#174

Currently takes 14 mins to run 2 mins of history, so that needs to come way down

@antazoey
Copy link
Member Author

Currently takes 14 mins to run 2 mins of history, so that needs to come way down

If I was you, I would already be managing the process and just connecting to it and calling reset_fork().
You can connect to it normally and call 1 method each time at the beginning of the simulation...
.fork() is the only method that completely tears down the forked network, regularly connecting won't do that.

@antazoey
Copy link
Member Author

Would certainly be a lot faster testing this way vs. how I am doing backtests now w/ anvil in ApeWorX/silverback#174

In any case, this is not super related to this PR so shouldn't be blocking

@antazoey
Copy link
Member Author

Testing demo:

I made a config like this:

foundry:
  ethereum:
    local:
      uri: http://127.0.0.1:5001

node:
  ethereum:
    local:
      uri: http://127.0.0.1:5000

And then these are some commands w/ output:

(apedev13) ➜  playground ape networks list --running 
Local node(s) not running.
(apedev13) ➜  playground ape networks run --network ::node --background
Making request HTTP. URI: http://127.0.0.1:5000 Method: web3_clientVersion
Starting geth (HTTP=127.0.0.1:5000, IPC=$HOME/.ape/node/geth.ipc).
(apedev13) ➜  playground ape networks run --network ::foundry --background
Starting 'anvil' process.
(apedev13) ➜  playground ape console --network ::node
(apedev13) ➜  playground ape networks list --running
PID    NETWORK                 IPC                       HTTP                   WS                 
33542  ethereum:local:node     $HOME/.ape/node/geth.ipc  http://127.0.0.1:5000  None               
33564  ethereum:local:foundry  None                      http://127.0.0.1:8545  ws://127.0.0.1:8545
(apedev13) ➜  playground ape console --network ::node
INFO:     Connecting to existing Geth node at $HOME/.ape/node/geth.ipc.

In [1]: exit
(apedev13) ➜  playground ape networks list --running 
PID    NETWORK                 IPC                       HTTP                   WS                 
33542  ethereum:local:node     $HOME/.ape/node/geth.ipc  http://127.0.0.1:5000  None               
33564  ethereum:local:foundry  None                      http://127.0.0.1:8545  ws://127.0.0.1:8545
(apedev13) ➜  playground ape console --network ::foundry

In [1]: exit
(apedev13) ➜  playground ape networks list --running    
PID    NETWORK                 IPC                       HTTP                   WS                 
33542  ethereum:local:node     $HOME/.ape/node/geth.ipc  http://127.0.0.1:5000  None               
33564  ethereum:local:foundry  None                      http://127.0.0.1:8545  ws://127.0.0.1:8545
(apedev13) ➜  playground ape networks kill                                
Stopped the following node(s):
        ethereum:local:node - $HOME/.ape/node/geth.ipc
        ethereum:local:foundry - http://127.0.0.1:8545
(apedev13) ➜  playground ape networks list --running
Local node(s) not running.
(apedev13) ➜  playground 

@antazoey antazoey force-pushed the feat/network-mgmt-cli branch from 6575687 to b50503c Compare March 13, 2025 17:01
@antazoey antazoey requested a review from fubuloubu March 13, 2025 17:01
fubuloubu
fubuloubu previously approved these changes Mar 13, 2025
Copy link
Member

@fubuloubu fubuloubu left a comment

Choose a reason for hiding this comment

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

Looks pretty good, just have some notes about killing process interface

@antazoey
Copy link
Member Author

antazoey commented Mar 13, 2025

Looks pretty good, just have some notes about killing process interface

Feedback addressed! thank you

@antazoey antazoey requested a review from fubuloubu March 13, 2025 18:02
@antazoey
Copy link
Member Author

So now you do --all to kill them all, else you have to give it one or more --pid option

Copy link
Contributor

@bitwise-constructs bitwise-constructs left a comment

Choose a reason for hiding this comment

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

super helpful, nice add

fubuloubu
fubuloubu previously approved these changes Mar 13, 2025
@antazoey antazoey dismissed stale reviews from fubuloubu and bitwise-constructs via 1c3ce82 March 13, 2025 23:45
@antazoey antazoey merged commit 0b57ec2 into ApeWorX:main Mar 14, 2025
18 checks passed
@antazoey antazoey deleted the feat/network-mgmt-cli branch March 14, 2025 01:05
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.

Add --background flag to ape networks run
3 participants