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: add new types and functions #8692

Merged
merged 3 commits into from
Apr 26, 2024
Merged

Conversation

guggero
Copy link
Collaborator

@guggero guggero commented Apr 25, 2024

Extracted from #8683 and #8684.

Will be helpful to have this merged to master so we can reference the commit directly, without needing to use a go.work file (which then would require us to bump to Go 1.22).

Roasbeef and others added 3 commits April 25, 2024 16:48
In this commit, we add a new type alias for a blob type. This type can be used in areas where a byte slice is used to store a TLV value, which may be a fully opaque nested TLV.
This type is useful when one wants to encode an integer as an underlying BigSize record. It wraps any integer, then handles the transformation into and out of the BigSize encoding on disk.
ValOpt returns an Option of the underlying value.
Copy link
Contributor

coderabbitai bot commented Apr 25, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Collaborator

@ProofOfKeags ProofOfKeags left a comment

Choose a reason for hiding this comment

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

I'd recommend making the Blob type more than just an alias since the set of valid Blobs is a subset of the set of all valid byte slices. Therefore lifting it into a new type to provide this assurance is more appropriate.

Comment on lines +20 to +22
// Blob is a type alias for a byte slice. It's used to indicate that a slice of
// bytes is actually an encoded TLV stream.
type Blob = []byte
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there are different semantics here that are being assumed, I'd recommend not making it a simple alias and actually make a newtype (dropping the =)

Copy link
Member

Choose a reason for hiding this comment

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

In this case using the type alias is useful, as then I can put it in a tlv.RecordT as a primitive type. Otherwise, you'd need the record method wrapper.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should just make RecordT a primitive type, no? I just want to stress that type aliases are not useful for anything other than giving the reader a hint on how something is used, it assures no correct api usage the way that a newtype wrapper does.


// BigSizeT is a high-order type that represents a TLV record that encodes an
// integer as a BigSize value in the stream.
type BigSizeT[T constraints.Integer] struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

🫡

// record.
func (o *OptionalRecordT[T, V]) ValOpt() fn.Option[V] {
return fn.MapOption(func(record RecordT[T, V]) V {
return record.Val
Copy link
Collaborator

@ProofOfKeags ProofOfKeags Apr 25, 2024

Choose a reason for hiding this comment

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

Pondering...

This makes me wonder if we should extract out Val as a function. We have the following issue here:

-- we have these
type OptionalRecord t v = Option (Record t v)
valOpt :: Option (Record t v) -> Option v
mapOption :: (a -> b) -> Option a -> Option b

-- if we had this
val :: Record t v -> v

-- then we could do this
valOpt :: OptionalRecord t v -> Option v
valOpt = mapOption val

Porting this go to is tricky since there's no (to my knowledge) easy way for us to refer to a method without yet supplying its receiver argument. So defining something like this wouldn't fix the ergonomics:

func (r RecordT[T, V]) Val() V { ... }

func (o *OptionalRecordT[T, V]) ValOpt() fn.Option[V] {
        return fn.MapOption(RecordT.Val)(*o) // no sir
}

// but we could do this
func Val[T, V any](r Record[T, V]) V { ... }

// and then do this
func (o *OptionalRecordT[T, V] ValOpt() fn.Option[V] {
        return fn.MapOption(Val)(*o) // can do
}

I say all this because if we have to create new names for things every time we want to map a function over some functor, then Map<FUNCTOR> loses a lot of its luster.

If you have ideas and/or opinions here, lmk. This code is fine, it just points to the fact that there's room for improvement on our functional patterns.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🛕

@Roasbeef Roasbeef merged commit e80adcd into lightningnetwork:master Apr 26, 2024
17 of 19 checks passed
@guggero guggero deleted the tlv-types branch April 29, 2024 07:47
@guggero
Copy link
Collaborator Author

guggero commented Apr 29, 2024

Pushed a new tag: tlv/v1.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants