@@ -3,7 +3,6 @@ package tapdb
33import (
44 "context"
55 "database/sql"
6- "fmt"
76 "math/rand"
87 "testing"
98 "time"
@@ -660,40 +659,12 @@ func randScriptKey(t *testing.T) asset.ScriptKey {
660659 return scriptKey
661660}
662661
663- // insertScriptKeyWithNull is a helper function that inserts a script key with a
664- // a NULL value for declared known. We use this so we can insert a NULL vs an
665- // actual value. It is identical to the InsertScriptKey.
666- func insertScriptKeyWithNull (ctx context.Context ,
667- key asset.ScriptKey ) func (AddrBook ) error {
668-
669- return func (q AddrBook ) error {
670- internalKeyID , err := insertInternalKey (
671- ctx , q , key .RawKey ,
672- )
673- if err != nil {
674- return fmt .Errorf ("error inserting internal key: %w" ,
675- err )
676- }
677-
678- _ , err = q .UpsertScriptKey (ctx , NewScriptKey {
679- InternalKeyID : internalKeyID ,
680- TweakedScriptKey : key .PubKey .SerializeCompressed (),
681- Tweak : key .Tweak ,
682- DeclaredKnown : sql.NullBool {
683- Valid : false ,
684- },
685- })
686- return err
687- }
688- }
689-
690662func assertKeyKnowledge (t * testing.T , ctx context.Context ,
691- addrBook * TapAddressBook , scriptKey asset.ScriptKey , known bool ,
663+ addrBook * TapAddressBook , scriptKey asset.ScriptKey ,
692664 keyType asset.ScriptKeyType ) {
693665
694666 dbScriptKey , err := addrBook .FetchScriptKey (ctx , scriptKey .PubKey )
695667 require .NoError (t , err )
696- require .Equal (t , known , dbScriptKey .DeclaredKnown )
697668 require .Equal (t , keyType , dbScriptKey .Type )
698669}
699670
@@ -705,87 +676,6 @@ func assertTweak(t *testing.T, ctx context.Context, addrBook *TapAddressBook,
705676 require .Equal (t , tweak , dbScriptKey .Tweak )
706677}
707678
708- // TestScriptKeyKnownUpsert tests that we can insert a script key, then insert
709- // it again declared as known.
710- func TestScriptKeyKnownUpsert (t * testing.T ) {
711- t .Parallel ()
712-
713- // First, make a new addr book instance we'll use in the test below.
714- testClock := clock .NewTestClock (time .Now ())
715- addrBook , _ := newAddrBook (t , testClock )
716-
717- ctx := context .Background ()
718-
719- // In this test, we insert the known field as false, and make sure we
720- // can flip it back to true.
721- t .Run ("false_to_true" , func (t * testing.T ) {
722- known := false
723- scriptKey := randScriptKey (t )
724-
725- // We'll insert a random script key into the database. We won't
726- // declare it as known though.
727- err := addrBook .InsertScriptKey (
728- ctx , scriptKey , known , asset .ScriptKeyBip86 ,
729- )
730- require .NoError (t , err )
731-
732- // We'll fetch the script key and confirm that it's not known.
733- assertKeyKnowledge (
734- t , ctx , addrBook , scriptKey , known ,
735- asset .ScriptKeyBip86 ,
736- )
737-
738- known = true
739-
740- // We'll now insert it again, but this time declare it as known.
741- err = addrBook .InsertScriptKey (
742- ctx , scriptKey , known , asset .ScriptKeyBip86 ,
743- )
744- require .NoError (t , err )
745-
746- // We'll fetch the script key and confirm that it's known.
747- assertKeyKnowledge (
748- t , ctx , addrBook , scriptKey , known ,
749- asset .ScriptKeyBip86 ,
750- )
751- })
752-
753- // In this test, we insert a NULL value, and make sure that it can still
754- // be set to true.
755- t .Run ("null_to_true" , func (t * testing.T ) {
756- known := false
757- scriptKey := randScriptKey (t )
758-
759- // We'll lift the internal routine of InsertScriptKey so we can
760- // insert an actual NULL here.
761- err := addrBook .db .ExecTx (
762- ctx , & AddrBookTxOptions {},
763- insertScriptKeyWithNull (ctx , scriptKey ),
764- )
765- require .NoError (t , err )
766-
767- // We'll fetch the script key and confirm that it's not known.
768- assertKeyKnowledge (
769- t , ctx , addrBook , scriptKey , known ,
770- asset .ScriptKeyUnknown ,
771- )
772-
773- known = true
774-
775- // We'll now insert it again, but this time declare it as known.
776- err = addrBook .InsertScriptKey (
777- ctx , scriptKey , known , asset .ScriptKeyBip86 ,
778- )
779- require .NoError (t , err )
780-
781- // We'll fetch the script key and confirm that it's known.
782- assertKeyKnowledge (
783- t , ctx , addrBook , scriptKey , known ,
784- asset .ScriptKeyBip86 ,
785- )
786- })
787- }
788-
789679// TestScriptKeyTweakUpsert tests that we can insert a script key, then insert
790680// it again when we know the tweak for it.
791681func TestScriptKeyTweakUpsert (t * testing.T ) {
@@ -800,40 +690,35 @@ func TestScriptKeyTweakUpsert(t *testing.T) {
800690 // In this test, we insert the tweak as NULL, and make sure we overwrite
801691 // it with an actual value again later.
802692 t .Run ("null_to_value" , func (t * testing.T ) {
803- known := false
804693 scriptKey := randScriptKey (t )
805694 scriptKey .Tweak = nil
806695
807696 // We'll insert a random script key into the database. We won't
808697 // declare it as known though, and it doesn't have the tweak.
809698 err := addrBook .InsertScriptKey (
810- ctx , scriptKey , known ,
811- asset .ScriptKeyScriptPathExternal ,
699+ ctx , scriptKey , asset .ScriptKeyUnknown ,
812700 )
813701 require .NoError (t , err )
814702
815703 // We'll fetch the script key and confirm that it's not known.
816704 assertKeyKnowledge (
817- t , ctx , addrBook , scriptKey , known ,
818- asset .ScriptKeyScriptPathExternal ,
705+ t , ctx , addrBook , scriptKey , asset .ScriptKeyUnknown ,
819706 )
820707 assertTweak (t , ctx , addrBook , scriptKey , nil )
821708
822- known = true
823709 randTweak := test .RandBytes (32 )
824710 scriptKey .Tweak = randTweak
825711
826712 // We'll now insert it again, but this time declare it as known
827713 // and also know the tweak.
828714 err = addrBook .InsertScriptKey (
829- ctx , scriptKey , known ,
830- asset .ScriptKeyScriptPathExternal ,
715+ ctx , scriptKey , asset .ScriptKeyScriptPathExternal ,
831716 )
832717 require .NoError (t , err )
833718
834719 // We'll fetch the script key and confirm that it's known.
835720 assertKeyKnowledge (
836- t , ctx , addrBook , scriptKey , known ,
721+ t , ctx , addrBook , scriptKey ,
837722 asset .ScriptKeyScriptPathExternal ,
838723 )
839724 assertTweak (t , ctx , addrBook , scriptKey , randTweak )
@@ -854,35 +739,32 @@ func TestScriptKeyTypeUpsert(t *testing.T) {
854739 // In this test, we insert the type as unknown, and make sure we
855740 // overwrite it with an actual value again later.
856741 t .Run ("null_to_value" , func (t * testing.T ) {
857- known := true
858742 scriptKey := randScriptKey (t )
859743 scriptKey .Tweak = nil
860744
861745 // We'll insert a random script key into the database. It is
862746 // declared as known, but doesn't have a known type.
863747 err := addrBook .InsertScriptKey (
864- ctx , scriptKey , known , asset .ScriptKeyUnknown ,
748+ ctx , scriptKey , asset .ScriptKeyUnknown ,
865749 )
866750 require .NoError (t , err )
867751
868752 // We'll fetch the script key and confirm that it's not known.
869753 assertKeyKnowledge (
870- t , ctx , addrBook , scriptKey , known ,
871- asset .ScriptKeyUnknown ,
754+ t , ctx , addrBook , scriptKey , asset .ScriptKeyUnknown ,
872755 )
873756 assertTweak (t , ctx , addrBook , scriptKey , nil )
874757
875758 // We'll now insert it again, but this time declare it as known
876759 // and also know the tweak.
877760 err = addrBook .InsertScriptKey (
878- ctx , scriptKey , known , asset .ScriptKeyBip86 ,
761+ ctx , scriptKey , asset .ScriptKeyBip86 ,
879762 )
880763 require .NoError (t , err )
881764
882765 // We'll fetch the script key and confirm that it's known.
883766 assertKeyKnowledge (
884- t , ctx , addrBook , scriptKey , known ,
885- asset .ScriptKeyBip86 ,
767+ t , ctx , addrBook , scriptKey , asset .ScriptKeyBip86 ,
886768 )
887769 })
888770}
0 commit comments