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

chore: Do not convert int to uint32 in btcstaking lib #135

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

vitsalis
Copy link
Member

@vitsalis vitsalis commented Oct 6, 2024

This PR handles the conversion of ints to uint32s in the btcstaking library. The conversions that were modified were not unsafe as they were the results of the len() call,
but they still made static analyzers complain.

Instead, we opted to convert uint32s to ints. However, this makes the assumption that the program will be executed in 64-bit architectures in order to be safe, which afaik is already a requirement.

@vitsalis vitsalis requested a review from a team as a code owner October 6, 2024 09:43
@vitsalis vitsalis requested review from KonradStaniec and Lazar955 and removed request for a team October 6, 2024 09:43
@vitsalis vitsalis force-pushed the vitsalis/type-conversions-btc-library branch from ed1e4bb to ab85b5a Compare October 6, 2024 11:09
@@ -45,7 +46,11 @@ func newLightChainCtxFromParams(params *chaincfg.Params) *lightChainCtx {
targetTimePerBlock := int64(params.TargetTimePerBlock / time.Second)
adjustmentFactor := params.RetargetAdjustmentFactor

blocksPerRetarget := int32(targetTimespan / targetTimePerBlock)
Copy link
Collaborator

Choose a reason for hiding this comment

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

tbh this is what btcd does with their types regarding those values, and I would leave it like that. I assume they try to match whatever bitcoind is doing. (ref:https://github.com/btcsuite/btcd/blob/master/blockchain/chain.go#L2186)

And in this case it is better to be consistent with btcd and bitcoind, than be consistent with gosec.

Copy link
Member Author

@vitsalis vitsalis Oct 6, 2024

Choose a reason for hiding this comment

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

Aren't we doing the same thing though? No problem with keeping it as it is though, did it just for gosec, but we can add a nosec tag if want to. Reverted.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aren't we doing the same thing though?

Most probably we are doing the same thing, but the probably part would require some investigation to be 100% sure, given that this values are relevant to consensus of BTC headers.

On the other hand, being compatible with btcd relaxes this a bit (I think there are 60 btcd nodes in bitcoin network rn, so if they are conensus compliant we are off the hook) 😅

@vitsalis vitsalis force-pushed the vitsalis/type-conversions-btc-library branch from 63dfbb3 to f619a3e Compare October 6, 2024 11:58
Copy link
Member

@Lazar955 Lazar955 left a comment

Choose a reason for hiding this comment

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

nice work

This PR handles the conversion of ints to uint32s in the btcstaking
library. The conversions that were modified were not unsafe
as they were the results of the `len()` call,
but they still made static analyzers complain.

Instead, we opted to convert uint32s to ints. However,
this makes the assumption that the program will be executed in
64-bit architectures in order to be safe, which afaik is already
a requirement.

Further, the PR includes some other small conversion fixes
that have been found in the codebase -- mostly low hanging fruit.
@vitsalis vitsalis force-pushed the vitsalis/type-conversions-btc-library branch from f619a3e to 39bd50e Compare October 7, 2024 09:04
@vitsalis vitsalis merged commit 64b6897 into main Oct 7, 2024
18 checks passed
@vitsalis vitsalis deleted the vitsalis/type-conversions-btc-library branch October 7, 2024 09:52
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.

3 participants