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

Make network and rpcport configurable rather than hardcoding them #126

Merged

Conversation

jump-kick
Copy link
Contributor

@jump-kick jump-kick commented Apr 16, 2024

Fixes #56.

  • Add network and rpcport fields to properties class
  • Add validation for the new fields
  • Add unit tests

@theborakompanioni
Copy link
Owner

Looks good!

I think the p2p port should also be exposed right?
At least it was in the (now commented out) section:

List<Integer> hardcodedStandardPorts = ImmutableList.<Integer>builder()
        .addAll(this.properties.getChain() == Chain.mainnet ? Lists.newArrayList(8332, 8333) : Collections.emptyList())
        .addAll(this.properties.getChain() == Chain.testnet ? Lists.newArrayList(18332, 18333) : Collections.emptyList())
        .addAll(this.properties.getChain() == Chain.regtest ? Lists.newArrayList(18443, 18444) : Collections.emptyList())
        .build();

Also, if rpcport is not specified, we should use the default ports of the specific network (regtest is the default):

  • mainnet: 8332 (rpc), 8333 (p2p)
  • testnet: 18332 (rpc), 18333 (p2p)
  • regtest: 18443 (rpc), 18444 (p2p)

What do you think?

@jump-kick
Copy link
Contributor Author

Thanks for the feedback!
Ah, ok, I missed the P2P port. I did add in defaults in the Properties class for RPC port and Network like this:
private String network = DEFAULT_CHAIN.toString();
private Integer rpcport = REGTEST_DEFAULT_RPC_PORT;

And then those defaults are effectively used in setting the exposed ports for the testcontainer:
List<Integer> exposedPorts = ImmutableList.<Integer>builder() .add(this.properties.getRpcport() .get()) .addAll(this.properties.getExposedPorts()) .build();

I could add a P2P port property to the properties class:
private Integer p2pport;

But I think you are also saying you'd like to be able to have logic to determine what the default ports are based on Network? If Regtest (default), then 18443 and 18444. If mainnet, then 8332 and 8333, etc. Is that right? If so, I can certainly do that.

Also, I'm assuming in the case RPC port is provided, but P2P port is blank, I should use the default P2P port for the network.

Then I would probably add a PostConstruct method to check what the Network is and set the ports based on that. But the default would be Regtest.

And then add those in the exposedPorts List:
List<Integer> exposedPorts = ImmutableList.<Integer>builder() .add(this.properties.getRpcport() .add(this.properties.getP2pport()) .addAll(this.properties.getExposedPorts()) .build();

What do you think?

@theborakompanioni
Copy link
Owner

Thanks for the feedback! Ah, ok, I missed the P2P port. I did add in defaults in the Properties class for RPC port and Network like this: private String network = DEFAULT_CHAIN.toString(); private Integer rpcport = REGTEST_DEFAULT_RPC_PORT;

I could add a P2P port property to the properties class: private Integer p2pport;

👍

But I think you are also saying you'd like to be able to have logic to determine what the default ports are based on Network? If Regtest (default), then 18443 and 18444. If mainnet, then 8332 and 8333, etc. Is that right? If so, I can certainly do that.

Yes.

Also, I'm assuming in the case RPC port is provided, but P2P port is blank, I should use the default P2P port for the network.

That is what I was thinking, yes. What do you think?

Then I would probably add a PostConstruct method to check what the Network is and set the ports based on that. But the default would be Regtest.

Currently, most default values of classes with @ConfigurationProperties have their default values applied via getters.
If you want, take a look at ClnContainerProperties, it does already something similar.

I like that more than PostConstruct as it is simple and not using spring automagic. But there are also downsides - what do you prefer?

And then add those in the exposedPorts List: List<Integer> exposedPorts = ImmutableList.<Integer>builder() .add(this.properties.getRpcport() .add(this.properties.getP2pport()) .addAll(this.properties.getExposedPorts()) .build();

What do you think?

Nice.

If network, rpcport and p2pport all return non-null values only, they don't need to return Optional anymore 💪

@jump-kick
Copy link
Contributor Author

Update: I am working this, and sorry for the delay! I'm so close to having it done, but am now having trouble with the unit tests in testcontainer. When running a test with mainnet or testnet configuration, the container times out:

org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (127.0.0.1 ports: [56180] should be listening

Not sure in this case where port 56180 is coming from, since I am setting 8332 and 8333 for Mainnet. I'm new to Testcontainers, so I might be missing something. Will keep you posted

@theborakompanioni
Copy link
Owner

Update: I am working this, and sorry for the delay! I'm so close to having it done, but am now having trouble with the unit tests in testcontainer. When running a test with mainnet or testnet configuration, the container times out:

org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (127.0.0.1 ports: [56180] should be listening

Not sure in this case where port 56180 is coming from, since I am setting 8332 and 8333 for Mainnet. I'm new to Testcontainers, so I might be missing something. Will keep you posted

Hey @jump-kick, take your time! No urgency involved.
Can you tell which test (which module) is failing? Some of them (e.g. electrum testcontainers) can be a bit flaky - thinking about externalizing (move to other repo9 or removing them in the future, as I do not think they are used by anyone.

Do not be afraid of pushing your changes - I'd love to review them 🙌

@jump-kick
Copy link
Contributor Author

jump-kick commented Apr 30, 2024

Ha, thanks for your patience! I just pushed another commit for your review. The failing tests were two new ones I added, 'defaultMainnetValuesTest' and 'defaultTestnetValuesTest' where I set the network to 'mainnet' and 'testnet'. THEN, I noticed the comment in BitcoindTestcontainerAutoConfiguration that says "NOTE: Currently only really supports creating "regtest" container. Defaults do not play nicely with mainnet/testnet -> "networkactive" is disabled." That seemed to be my experience, too, so I tried a way to do the unit tests where the testcontainer is not started; although maybe that defeats the purpose of this project, haha. oh well. See 'defaultMainnetValuesTest' and 'defaultTestnetValuesTest'.

Now I don't have the testcontainer problem where it times out waiting for a port to open, but there is some weird bug where the 'network' property is not being set, and it keeps returning the default 'regtest'. I'm sure it's something obvious and I will kick myself when I realize the problem = ).

@jump-kick
Copy link
Contributor Author

oh yeah, and I like your idea of putting the default logic in the getters, so I went ahead with that.

@theborakompanioni
Copy link
Owner

@jump-kick So nice!

I think I will have time to review it next week. Is it okay to push some changes (e.g. remove logging in property class) or do you favor that I describe them here and you "fix" them yourself? Thank you so much 🙏

@jump-kick
Copy link
Contributor Author

jump-kick commented May 5, 2024 via email

@theborakompanioni
Copy link
Owner

Yes, please feel free to push any changes you have!

PR against your branch: jump-kick#1
Merge, if you think these adaptions are appropriate 🙏

chore: pass port and rpcport arg to bitcoin testcontainer
@theborakompanioni
Copy link
Owner

@jump-kick What do you think.. should p2pport be renamed to port and network to chain?

@jump-kick
Copy link
Contributor Author

jump-kick commented May 29, 2024 via email

@theborakompanioni
Copy link
Owner

Yes, I like that idea! I was thinking I could implement those changes and then add them to the branch/PR and make it official. I could have that done within a couple of days.

Nice! Take your time - there is no urgency involved. Looking forward to your contribution 🧡

@jump-kick jump-kick marked this pull request as ready for review May 30, 2024 19:46
@jump-kick
Copy link
Contributor Author

jump-kick commented May 30, 2024

@theborakompanioni
Copy link
Owner

One quick question, @theborakompanioni - can you confirm this is right?

"main" vs. "mainnet" and "test" vs. "testnet" for the Enum String property?

Yeah, you are right - consistent would be to make them the same values as Bitcoin Core. However, they are named like they are now through-out other modules as well. I'd say let's keep it like it is and maybe think about a rectification of all properties in a follow-up ticket/PR. Okay for you?

I think this should be merged soon.

@jump-kick
Copy link
Contributor Author

Sounds good to me! Thanks

@theborakompanioni
Copy link
Owner

Sounds good to me! Thanks

Nice. Will merge this now into devel. There are still some failing tests and upon inspection, I will rename chain to network again - as it really is different from the Bitcoin Core values ("main" vs "mainnet", etc.). Fixing this in a follow-up PR!

Thank you so much for your contribution @jump-kick 🚀 🚀 🚀

@theborakompanioni theborakompanioni merged commit 8bafde1 into theborakompanioni:master Jun 3, 2024
4 checks passed
@theborakompanioni
Copy link
Owner

Daaamn. Did not look at the target branch and merged into master by mistake. Will fix this as soon as I am back on my workstation. 😬

@theborakompanioni
Copy link
Owner

Daaamn. Did not look at the target branch and merged into master by mistake. Will fix this as soon as I am back on my workstation. 😬

Fixed 🙏

@jump-kick
Copy link
Contributor Author

d'oh! Sorry if I put the wrong branch there! Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants