44 "bytes"
55 "crypto/sha256"
66 "encoding/hex"
7+ "reflect"
78 "testing"
89
910 "github.com/btcsuite/btcd/blockchain"
@@ -18,6 +19,7 @@ import (
1819 "github.com/lightningnetwork/lnd/keychain"
1920 "github.com/lightningnetwork/lnd/tlv"
2021 "github.com/stretchr/testify/require"
22+ "pgregory.net/rapid"
2123)
2224
2325var (
@@ -514,6 +516,79 @@ func TestAssetEncoding(t *testing.T) {
514516 test .WriteTestVectors (t , generatedTestVectorName , testVectors )
515517}
516518
519+ // TestAltLeafEncoding runs a property test for AltLeaf validation, encoding,
520+ // and decoding.
521+ func TestAltLeafEncoding (t * testing.T ) {
522+ t .Run ("alt leaf encode/decode" , rapid .MakeCheck (testAltLeafEncoding ))
523+ }
524+
525+ // testAltLeafEncoding tests the AltLeaf validation logic, and that a valid
526+ // AltLeaf can be encoded and decoded correctly.
527+ func testAltLeafEncoding (t * rapid.T ) {
528+ protoLeaf := AltLeafGen (t ).Draw (t , "alt_leaf" )
529+ validAltLeafErr := protoLeaf .ValidateAltLeaf ()
530+
531+ // If validation passes, the asset must follow all alt leaf constraints.
532+ asserts := []AssetAssert {
533+ AssetVersionAssert (V0 ),
534+ AssetGenesisAssert (EmptyGenesis ),
535+ AssetAmountAssert (0 ),
536+ AssetLockTimeAssert (0 ),
537+ AssetRelativeLockTimeAssert (0 ),
538+ AssetHasSplitRootAssert (false ),
539+ AssetGroupKeyAssert (nil ),
540+ AssetHasScriptKeyAssert (true ),
541+ }
542+ assertErr := CheckAssetAsserts (& protoLeaf , asserts ... )
543+
544+ // If the validation method and these assertions behave differently,
545+ // either the test or the validation method is incorrect.
546+ switch {
547+ case validAltLeafErr == nil && assertErr != nil :
548+ t .Error (assertErr )
549+
550+ case validAltLeafErr != nil && assertErr == nil :
551+ t .Error (validAltLeafErr )
552+
553+ default :
554+ }
555+
556+ // Don't test encoding for invalid alt leaves.
557+ if validAltLeafErr != nil {
558+ return
559+ }
560+
561+ // If the alt leaf is valid, check that it can be encoded without error,
562+ // and decoded to an identical alt leaf.
563+ // fmt.Println("valid leaf")
564+ var buf bytes.Buffer
565+ if err := protoLeaf .EncodeAltLeaf (& buf ); err != nil {
566+ t .Error (err )
567+ }
568+
569+ var decodedLeaf Asset
570+ altLeafBytes := bytes .NewReader (buf .Bytes ())
571+ if err := decodedLeaf .DecodeAltLeaf (altLeafBytes ); err != nil {
572+ t .Error (err )
573+ }
574+
575+ if ! protoLeaf .DeepEqual (& decodedLeaf ) {
576+ t .Errorf ("decoded leaf %v does not match input %v" , decodedLeaf ,
577+ protoLeaf )
578+ }
579+
580+ // Asset.DeepEqual does not inspect UnknownOddTypes, so check for their
581+ // equality separately.
582+ if ! reflect .DeepEqual (
583+ protoLeaf .UnknownOddTypes , decodedLeaf .UnknownOddTypes ,
584+ ) {
585+
586+ t .Errorf ("decoded leaf unknown types %v does not match input " +
587+ "%v" , decodedLeaf .UnknownOddTypes ,
588+ protoLeaf .UnknownOddTypes )
589+ }
590+ }
591+
517592// TestTapLeafEncoding asserts that we can properly encode and decode tapLeafs
518593// through their TLV serialization, and that invalid tapLeafs are rejected.
519594func TestTapLeafEncoding (t * testing.T ) {
0 commit comments