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

Simplified and improved kotlin forwarding service #3400

Conversation

ItoroD
Copy link
Contributor

@ItoroD ItoroD commented May 18, 2024

  • One main difference to note in the kotlin version of forwarding service is that in kotlin its not the best best practice to use multiple constructors (secondary constructors). Hence default values have been used for some member variables of config class.
  • With this we can initialize config as if it had multiple constructors

Copy link
Member

@msgilligan msgilligan left a comment

Choose a reason for hiding this comment

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

Overall, this looks great!

I am being exceptionally picky about the ForwardingService because for many it will be their first impression of bitcoinj (and maybe Kotlin or modern Java) and also because if we try really hard and can't eliminate any unwieldy code then we need to look at improving the bitcoinj API itself to fix them.

I really appreciate your work and your willingness to keep fine-tuning this!

@ItoroD ItoroD force-pushed the Simplified-and-improved-kotlin-code-for-forwarding-service branch from 0b91ab2 to 6853d4f Compare May 18, 2024 20:41
@ItoroD ItoroD requested a review from msgilligan May 18, 2024 20:49
Copy link
Member

@msgilligan msgilligan left a comment

Choose a reason for hiding this comment

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

Getting close! This is looking good!

@@ -24,63 +24,58 @@ import org.bitcoinj.wallet.Wallet
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener
import java.io.Closeable
import java.io.File
import java.util.Objects.*
import java.util.*
Copy link
Member

Choose a reason for hiding this comment

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

We generally try to avoid using wildcard imports...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will look out those

constructor(forwardingAddress: Address) : this(
forwardingAddress,
forwardingAddress.network() as BitcoinNetwork
)
Copy link
Member

Choose a reason for hiding this comment

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

Is this constructor necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I noticed you do the address check in a second constructor that has (address, network) as parameters. So this constructor for when only address is passed, calls the other constructor that checks address before address is set on member variables.

since forwardingAddress.network() as BitcoinNetwork is now being done in the constructor, i should also take it out of the default set value for network

@ItoroD ItoroD force-pushed the Simplified-and-improved-kotlin-code-for-forwarding-service branch from 6853d4f to fcb51db Compare May 18, 2024 21:49
@ItoroD ItoroD requested a review from msgilligan May 18, 2024 21:59
@ItoroD
Copy link
Contributor Author

ItoroD commented May 18, 2024

Overall, this looks great!

I am being exceptionally picky about the ForwardingService because for many it will be their first impression of bitcoinj (and maybe Kotlin or modern Java) and also because if we try really hard and can't eliminate any unwieldy code then we need to look at improving the bitcoinj API itself to fix them.

I really appreciate your work and your willingness to keep fine-tuning this!

Thanks! @msgilligan

Copy link
Member

@msgilligan msgilligan left a comment

Choose a reason for hiding this comment

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

Approved.

Copy link
Member

@schildbach schildbach left a comment

Choose a reason for hiding this comment

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

I wonder can we do anything about these warnings as well?

w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:81 Parameter 'prevBalance' is never used
w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:100 Parameter 'newBalance' is never used

Obviously we can't change the method signature, but is it possible to declare the parameters as "intentionally not being used"?

@schildbach
Copy link
Member

One nitpick about the commit message: we use present tense to describe the semantics of the commit, rather than past tense. So "simplify coin listener" rather than "simplified…".

@ItoroD
Copy link
Contributor Author

ItoroD commented May 29, 2024

One nitpick about the commit message: we use present tense to describe the semantics of the commit, rather than past tense. So "simplify coin listener" rather than "simplified…".

Noted. Thanks

@ItoroD
Copy link
Contributor Author

ItoroD commented May 29, 2024

I wonder can we do anything about these warnings as well?

w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:81 Parameter 'prevBalance' is never used
w: file:///bitcoinj/examples-kotlin/src/main/kotlin/org/bitcoinj/ForwardingService.kt:94:100 Parameter 'newBalance' is never used

Obviously we can't change the method signature, but is it possible to declare the parameters as "intentionally not being used"?

We can use the "Suppress" annotation like so

@Suppress("UNUSED_PARAMETER")
private fun coinForwardingListener(wallet: Wallet, incomingTx: Transaction, prevBalance: Coin, newBalance: Coin)

@msgilligan
Copy link
Member

Obviously we can't change the method signature, but is it possible to declare the parameters as "intentionally not being used"?

Can't they be replaced with _ or prefixed with _?

Note that this convention is now in Java 22 and later: https://openjdk.org/jeps/456

@ItoroD
Copy link
Contributor Author

ItoroD commented May 29, 2024

Obviously we can't change the method signature, but is it possible to declare the parameters as "intentionally not being used"?

Can't they be replaced with _ or prefixed with _?

Note that this convention is now in Java 22 and later: https://openjdk.org/jeps/456

Yes the underscore exists in kotlin but it cannot be used on parameter of a function. Its used in special cases like parameter of a lamda function.

Also in java 22 I doubt you can use the underscore in a method param. Its used in other cases in java 22 as well. (not in method params) to the best of my knowledge

@schildbach
Copy link
Member

@msgilligan Are you fine with the added @Suppress? Or should we do without and live with the warnings?

@msgilligan
Copy link
Member

@msgilligan Are you fine with the added @Suppress? Or should we do without and live with the warnings?

I'm fine with it for now. I spent some time looking at alternatives and while doing so discovered that the neither the Kotlin nor the Java example correctly removes the listener. The problem is that when creating the method references now objects are created. So the listener passed in to remove does not match the one stored in the list...

@schildbach
Copy link
Member

Merged.

@schildbach schildbach closed this Jun 1, 2024
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.

None yet

3 participants