From d69e1111bab38765b228d97c85878bd691e18024 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 26 Feb 2024 16:12:11 -0800 Subject: [PATCH 1/3] build: update to fn/v1.0.4 --- go.mod | 2 +- go.sum | 4 ++-- tlv/go.mod | 5 +++-- tlv/go.sum | 10 ++++++---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 5546c8440..0fcb43124 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( github.com/lightningnetwork/lightning-onion v1.2.1-0.20230823005744-06182b1d7d2f github.com/lightningnetwork/lnd/cert v1.2.2 github.com/lightningnetwork/lnd/clock v1.1.1 - github.com/lightningnetwork/lnd/fn v1.0.2 + github.com/lightningnetwork/lnd/fn v1.0.4 github.com/lightningnetwork/lnd/healthcheck v1.2.3 github.com/lightningnetwork/lnd/kvdb v1.4.4 github.com/lightningnetwork/lnd/queue v1.1.1 diff --git a/go.sum b/go.sum index 0d9d0be78..854d94e10 100644 --- a/go.sum +++ b/go.sum @@ -442,8 +442,8 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U= github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0= github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ= -github.com/lightningnetwork/lnd/fn v1.0.2 h1:6u+DHMvpHj09KH2Uw39fsbjydq9JvG23Rc99i+mhI1A= -github.com/lightningnetwork/lnd/fn v1.0.2/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw= +github.com/lightningnetwork/lnd/fn v1.0.4 h1:n4iGRRoS+XHqNbOrsXIvweps/QfWk+moO7FiYPPFI8k= +github.com/lightningnetwork/lnd/fn v1.0.4/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw= github.com/lightningnetwork/lnd/healthcheck v1.2.3 h1:oqhOOy8WmIEa6RBkYKC0mmYZkhl8T2kGD97n9jpML8o= github.com/lightningnetwork/lnd/healthcheck v1.2.3/go.mod h1:eDxH3dEwV9DeBW/6inrmlVh1qBOFV0AI14EEPnGt9gc= github.com/lightningnetwork/lnd/kvdb v1.4.4 h1:bCv63rVCvzqj1BkagN/EWTov6NDDgYEG/t0z2HepRMk= diff --git a/tlv/go.mod b/tlv/go.mod index 51ee83e25..f61284752 100644 --- a/tlv/go.mod +++ b/tlv/go.mod @@ -5,14 +5,15 @@ require ( github.com/btcsuite/btcd/btcec/v2 v2.1.3 github.com/davecgh/go-spew v1.1.1 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 - github.com/lightningnetwork/lnd/fn v1.0.0 + github.com/lightningnetwork/lnd/fn v1.0.4 github.com/stretchr/testify v1.8.2 - golang.org/x/exp v0.0.0-20231006140011-7918f672742d + golang.org/x/exp v0.0.0-20231226003508-02704c960a9b ) require ( github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect github.com/kr/pretty v0.3.0 // indirect + github.com/lightninglabs/neutrino/cache v1.1.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect golang.org/x/crypto v0.7.0 // indirect diff --git a/tlv/go.sum b/tlv/go.sum index 60d32c136..25b1c26c2 100644 --- a/tlv/go.sum +++ b/tlv/go.sum @@ -19,8 +19,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/lightningnetwork/lnd/fn v1.0.0 h1:I5VG9AD63mOQ89RMQEu7HRI1r68wn8yz539LoylUIKM= -github.com/lightningnetwork/lnd/fn v1.0.0/go.mod h1:XV+0vBXSnh3aUjskJUv58TOpsveiXQ+ac8rEnXZDGFc= +github.com/lightninglabs/neutrino/cache v1.1.2 h1:C9DY/DAPaPxbFC+xNNEI/z1SJY9GS3shmlu5hIQ798g= +github.com/lightninglabs/neutrino/cache v1.1.2/go.mod h1:XJNcgdOw1LQnanGjw8Vj44CvguYA25IMKjWFZczwZuo= +github.com/lightningnetwork/lnd/fn v1.0.4 h1:n4iGRRoS+XHqNbOrsXIvweps/QfWk+moO7FiYPPFI8k= +github.com/lightningnetwork/lnd/fn v1.0.4/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= @@ -35,8 +37,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= -golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4= +golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From afde7162c67188ac12d8713750334d0b8ddb8f35 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 24 Feb 2024 16:41:36 -0800 Subject: [PATCH 2/3] tlv: add UnwrapOrErrV and UnwrapOrFailV These two methods mirror the recently added `fn.Option` methods but allow us to unwrap on level deeper into the underlying value of the Record. --- tlv/record_type.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tlv/record_type.go b/tlv/record_type.go index 248ebc415..ec893a06b 100644 --- a/tlv/record_type.go +++ b/tlv/record_type.go @@ -1,6 +1,8 @@ package tlv import ( + "testing" + "github.com/btcsuite/btcd/btcec/v2" "github.com/lightningnetwork/lnd/fn" "golang.org/x/exp/constraints" @@ -115,6 +117,29 @@ func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) { }) } +// UnwrapOrFailV is used to extract a value from an option within a test +// context. If the option is None, then the test fails. This gives the +// underlying value of the record, rather then the record itself. +func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V { + inner := o.Option.UnwrapOrFail(t) + + return inner.Val +} + +// UnwrapOrErr is used to extract a value from an option, if the option is +// empty, then the specified error is returned directly. This gives the +// underlying value of the record, instead of the record itself. +func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) { + var zero V + + inner, err := o.Option.UnwrapOrErr(err) + if err != nil { + return zero, err + } + + return inner.Val, nil +} + // SomeRecordT creates a new OptionalRecordT type from a given RecordT type. func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] { return OptionalRecordT[T, V]{ From 3dd01cdc78833ef9b9f46a4db1c125f800feff73 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 24 Feb 2024 16:44:15 -0800 Subject: [PATCH 3/3] tlv: add Zero() method for OptionalRecordT Most of the time with wire messages, we'll have the wrapped record, so this is useful when decoding into a blank instance of the record's value. --- tlv/record_type.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tlv/record_type.go b/tlv/record_type.go index ec893a06b..297617d5a 100644 --- a/tlv/record_type.go +++ b/tlv/record_type.go @@ -140,6 +140,11 @@ func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) { return inner.Val, nil } +// Zero returns a zero value of the record type. +func (t *OptionalRecordT[T, V]) Zero() RecordT[T, V] { + return ZeroRecordT[T, V]() +} + // SomeRecordT creates a new OptionalRecordT type from a given RecordT type. func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] { return OptionalRecordT[T, V]{