Skip to content

Commit e80adcd

Browse files
authored
Merge pull request #8692 from guggero/tlv-types
tlv: add new types and functions
2 parents abcad62 + c6dae6c commit e80adcd

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

tlv/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
module github.com/lightningnetwork/lnd/tlv
22

33
require (
4-
github.com/btcsuite/btcd v0.23.3
5-
github.com/btcsuite/btcd/btcec/v2 v2.1.3
4+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af
5+
github.com/btcsuite/btcd/btcec/v2 v2.3.2
66
github.com/davecgh/go-spew v1.1.1
77
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
88
github.com/lightningnetwork/lnd/fn v1.0.4
9-
github.com/stretchr/testify v1.8.2
9+
github.com/stretchr/testify v1.8.4
1010
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b
1111
)
1212

1313
require (
14-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
14+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
1515
github.com/kr/pretty v0.3.0 // indirect
1616
github.com/lightninglabs/neutrino/cache v1.1.2 // indirect
1717
github.com/pmezard/go-difflib v1.0.0 // indirect
1818
github.com/rogpeppe/go-internal v1.9.0 // indirect
19-
golang.org/x/crypto v0.7.0 // indirect
20-
golang.org/x/sys v0.13.0 // indirect
19+
golang.org/x/crypto v0.16.0 // indirect
20+
golang.org/x/sys v0.15.0 // indirect
2121
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2222
gopkg.in/yaml.v3 v3.0.1 // indirect
2323
)

tlv/go.sum

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
github.com/btcsuite/btcd v0.23.3 h1:4KH/JKy9WiCd+iUS9Mu0Zp7Dnj17TGdKrg9xc/FGj24=
2-
github.com/btcsuite/btcd v0.23.3/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
3-
github.com/btcsuite/btcd/btcec/v2 v2.1.3 h1:xM/n3yIhHAhHy04z4i43C8p4ehixJZMsnrVJkgl+MTE=
4-
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
5-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
6-
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
1+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af h1:F60A3wst4/fy9Yr1Vn8MYmFlfn7DNLxp8o8UTvhqgBE=
2+
github.com/btcsuite/btcd v0.24.1-0.20240301210420-1a2b599bf1af/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg=
3+
github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U=
4+
github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
5+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ=
6+
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
77
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
8-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
98
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
109
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1110
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
@@ -28,24 +27,18 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
2827
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
2928
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3029
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
31-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
32-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
33-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
34-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
35-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
36-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
37-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
38-
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
39-
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
30+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
31+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
32+
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
33+
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
4034
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
4135
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
42-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
43-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
36+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
37+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4438
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4539
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4640
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
4741
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
4842
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
49-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5043
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5144
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tlv/record.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ type Type uint64
1717
// slice containing the encoded data.
1818
type TypeMap map[Type][]byte
1919

20+
// Blob is a type alias for a byte slice. It's used to indicate that a slice of
21+
// bytes is actually an encoded TLV stream.
22+
type Blob = []byte
23+
2024
// Encoder is a signature for methods that can encode TLV values. An error
2125
// should be returned if the Encoder cannot support the underlying type of val.
2226
// The provided scratch buffer must be non-nil.

tlv/record_type.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ type OptionalRecordT[T TlvType, V any] struct {
104104
// TlvType returns the type of the record. This is the value used to identify
105105
// this type on the wire. This value is bound to the specified TlvType type
106106
// param.
107-
func (t *OptionalRecordT[T, V]) TlvType() Type {
107+
func (o *OptionalRecordT[T, V]) TlvType() Type {
108108
zeroRecord := ZeroRecordT[T, V]()
109109
return zeroRecord.TlvType()
110110
}
111111

112112
// WhenSomeV executes the given function if the optional record is present.
113113
// This operates on the inner most type, V, which is the value of the record.
114-
func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
115-
t.Option.WhenSome(func(r RecordT[T, V]) {
114+
func (o *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
115+
o.Option.WhenSome(func(r RecordT[T, V]) {
116116
f(r.Val)
117117
})
118118
}
@@ -126,7 +126,7 @@ func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V {
126126
return inner.Val
127127
}
128128

129-
// UnwrapOrErr is used to extract a value from an option, if the option is
129+
// UnwrapOrErrV is used to extract a value from an option, if the option is
130130
// empty, then the specified error is returned directly. This gives the
131131
// underlying value of the record, instead of the record itself.
132132
func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
@@ -141,10 +141,19 @@ func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
141141
}
142142

143143
// Zero returns a zero value of the record type.
144-
func (t *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
144+
func (o *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
145145
return ZeroRecordT[T, V]()
146146
}
147147

148+
// ValOpt returns an Option of the underlying value. This can be used to chain
149+
// other option related methods to avoid needing to first go through the outer
150+
// record.
151+
func (o *OptionalRecordT[T, V]) ValOpt() fn.Option[V] {
152+
return fn.MapOption(func(record RecordT[T, V]) V {
153+
return record.Val
154+
})(o.Option)
155+
}
156+
148157
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
149158
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
150159
return OptionalRecordT[T, V]{
@@ -159,3 +168,30 @@ func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
159168
Val: v,
160169
}
161170
}
171+
172+
// BigSizeT is a high-order type that represents a TLV record that encodes an
173+
// integer as a BigSize value in the stream.
174+
type BigSizeT[T constraints.Integer] struct {
175+
// We'll store the base value in the struct as a uin64, but then expose
176+
// a public method to cast to the specified type.
177+
v uint64
178+
}
179+
180+
// NewBigSizeT creates a new BigSizeT type from a given integer type.
181+
func NewBigSizeT[T constraints.Integer](val T) BigSizeT[T] {
182+
return BigSizeT[T]{
183+
v: uint64(val),
184+
}
185+
}
186+
187+
// Int returns the underlying integer value of the BigSize record.
188+
func (b BigSizeT[T]) Int() T {
189+
return T(b.v)
190+
}
191+
192+
// Record returns the underlying record interface for the record type.
193+
func (b *BigSizeT[T]) Record() Record {
194+
// We use a zero value for the type here as this should be used with
195+
// the higher order RecordT type.
196+
return MakeBigSizeRecord(0, &b.v)
197+
}

0 commit comments

Comments
 (0)