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

tlv: Added bool to primitive #8057

Merged
merged 2 commits into from
Oct 16, 2023
Merged

Conversation

Chinwendu20
Copy link
Contributor

@Chinwendu20 Chinwendu20 commented Oct 4, 2023

Change Description

This pull request adds bool to the primitive type in tlv as per @guggero's comment in this PR:
https://github.com/lightningnetwork/lnd/pull/7787/files#r1317199032
to persist new data structure in the DB using tlv. While doing that I noticed that one of the data types in the LocalCloseInfo type is boolean so that motivated that change.

I know we could just change the bool type to an existing integer primitive type and indicate true as 1 and false as 0 but
since bool is part of the primitive data types in golang, it would make sense to include it here.
Made a comment about it in the PR that introduced tlv to lnd 3 weeks ago: #3061 (comment)
to get your thoughts and perspective, yet to receive a reply.

If the concept is acked, I will go ahead to add the release notes to make the CI happy.
I would be happy to close this PR if this makes no sense.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!
Did a quick pass and approved the CI run.

Just a reminder to everyone that these are changes in a submodule, so they will only be accessible in the main lnd code once we merge the PR and push a new tag for the submodule.

tlv/record.go Show resolved Hide resolved
tlv/primitive.go Outdated Show resolved Hide resolved
@Chinwendu20
Copy link
Contributor Author

Thanks @guggero for the review, as per your comment on it being accessible only after there is a tagged release for lnd, would that mean my next PR on persisting channel force close issue on DB which depends on this change would have to wait?
Also why can't we just use the local tlv package in development, why wait for release?

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Two more requests, otherwise looks good!

Also why can't we just use the local tlv package in development, why wait for release?

I didn't mean we have to wait for a release of lnd itself. I just have to push a new tag for the tlv package after this PR was merged. Like this for example: https://github.com/lightningnetwork/lnd/releases/tag/tlv%2Fv1.1.1

In the other PR you can temporarily use a replace directive in the go.mod file:

replace github.com/lightningnetwork/lnd/tlv => ./tlv

That will cause the linter to fail but should allow you access to the changed code. And when the tag is pushed after merging this PR, you simply remove the replace and instead update the entry for github.com/lightningnetwork/lnd/tlv to the new version (will likely be v1.1.2).

@@ -134,6 +134,11 @@ None
is updated](https://github.com/lightningnetwork/lnd/pull/7788) so that LND can
take advantage of the latest filter fetching performance improvements.

### Tlv
Copy link
Collaborator

Choose a reason for hiding this comment

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

The v0.17.0-beta version has already been released, please move this to the file for 0.18.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, that's only in the staging branch. We'll need to wait for this to be merged: #8059.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we change the base branch to 0-18-staging ?

Copy link
Collaborator

@guggero guggero Oct 9, 2023

Choose a reason for hiding this comment

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

The staging branch is about to be merged into master, so I think we can just wait a moment and then re-base on master.

Or if you want to update this PR without waiting, you can change the base to 0-18-staging-rebased instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be updated now, the 0.18 staging branch was merged into master.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, done

tlv/primitive_test.go Show resolved Hide resolved
tlv/primitive.go Outdated
@@ -362,3 +362,29 @@ func SizeBigSize(val interface{}) SizeFunc {
return size
}
}

// EBool encodes a boolean. An error is returned if val is not a boolean.
func EBool(w io.Writer, val interface{}, buf *[8]byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we move this primitive type to the types above in the file where all the integers are defined (first the Encoding and followed by the Encoding functions), and also include a EBoolT for custom-like boolean types or is this not really 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.

Thanks for the review. I noticed that the new functions I added in this file should be above the SizeBigSize function, is that what you are referring to? as for EBoolT, never thought about that if you feel that there will be a good use case fir that, I will include it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes either before SízeBigSize but I even recommend to sort it anywhere after the func DUint64 and before func EBytes32 because to me a bool is way more equal to the basic type like the integers rather than bytes (arrays) and custom types which follow after them.
Yeah I mean I would add EBoolT because if you know you are encoding a boolean you could save a heap allocation as well so better have it and stay inline with the other primitive golang types, but I have not strong feeling about it.

@Chinwendu20
Copy link
Contributor Author

Thanks @guggero really appreciate your explanation. What I want to know is why we cannot make the replace directive for internal packages like tlv permanently incorporated in the codebase without having it raise lint errors as that would be great for development but I guess this arrangement is best so that there can be separate PRs for respective changes made to each internal submodule.

@guggero
Copy link
Collaborator

guggero commented Oct 6, 2023

Thanks @guggero really appreciate your explanation. What I want to know is why we cannot make the replace directive for internal packages like tlv permanently incorporated in the codebase without having it raise lint errors as that would be great for development but I guess this arrangement is best so that there can be separate PRs for respective changes made to each internal submodule.

The problem with using a replace directive permanently (which we used to have a couple of years back) is that those are ignored if you pull in lnd as a library in another Go project. So we tended to forget to push new tags for those submodules and update their versions in go.mod and then whenever we bumped the version of lnd in Loop/Pool/Faraday we ran into issues as the modules were outdated.

@Chinwendu20 Chinwendu20 force-pushed the tlv-bool branch 2 times, most recently from 120e552 to 76b8755 Compare October 7, 2023 09:02
@Chinwendu20
Copy link
Contributor Author

Thanks @guggero really appreciate your explanation. What I want to know is why we cannot make the replace directive for internal packages like tlv permanently incorporated in the codebase without having it raise lint errors as that would be great for development but I guess this arrangement is best so that there can be separate PRs for respective changes made to each internal submodule.

The problem with using a replace directive permanently (which we used to have a couple of years back) is that those are ignored if you pull in lnd as a library in another Go project. So we tended to forget to push new tags for those submodules and update their versions in go.mod and then whenever we bumped the version of lnd in Loop/Pool/Faraday we ran into issues as the modules were outdated.

Thank you

Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Nice PR, LGTM

Copy link
Collaborator

@morehouse morehouse left a comment

Choose a reason for hiding this comment

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

For testing completeness, perhaps we should extend the new TLV fuzz tests in tlv/fuzz_test.go for the new bool primitive.

if _, err := io.ReadFull(r, buf[:1]); err != nil {
return err
}
*i = buf[0] != 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should return an error if val is not 0 or 1... If that happens when decoding from the DB it's a strong indication that there's a bug or data corruption somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review, pushed new changes with respect to this.

tlv/fuzz_test.go Outdated
@@ -97,6 +97,13 @@ func FuzzVarBytes(f *testing.F) {
})
}

func FuzzBool(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
var val []byte
Copy link
Collaborator

Choose a reason for hiding this comment

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

val needs to be a bool, otherwise this won't go past the first if in EBool. The rest looks good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks all done

Signed-off-by: Ononiwu Maureen <[email protected]>
Signed-off-by: Ononiwu Maureen <[email protected]>
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM 🎉

Copy link
Collaborator

@morehouse morehouse left a comment

Choose a reason for hiding this comment

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

LGTM

@guggero guggero merged commit a6f044b into lightningnetwork:master Oct 16, 2023
21 of 25 checks passed
@guggero
Copy link
Collaborator

guggero commented Oct 16, 2023

@Chinwendu20 I pushed a new tag tor/v1.1.3 which you can use in the dependent version (meaning you can bump the version of github.com/lightningnetwork/lnd/tor to v1.1.3 in go.mod of your other PR.

@saubyk saubyk added this to the v0.18.0 milestone Oct 16, 2023
@Chinwendu20
Copy link
Contributor Author

Woohoo! thanks!

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.

5 participants