@@ -895,16 +895,34 @@ func (a *AssetStore) constraintsToDbFilter(
895895 assetFilter .AssetIDFilter = nil
896896 }
897897
898- // The fn.None option means we don't restrict on script key type
899- // at all.
900- query .ScriptKeyType .WhenSome (func (t asset.ScriptKeyType ) {
901- assetFilter .ScriptKeyType = sqlInt16 (t )
902- })
898+ // Let's figure out the set of script key types we want to
899+ // query for. If the user specified a script key type, then we
900+ // use that to filter the results. If the user didn't specify a
901+ // script key type, then we use the full set of script key
902+ // types, as this might be an internal query that also needs to
903+ // know channel related assets.
904+ assetFilter .ScriptKeyType = scriptKeyTypesForQuery (
905+ false , query .ScriptKeyType ,
906+ )
903907 }
904908
905909 return assetFilter , nil
906910}
907911
912+ // scriptKeyTypesForQuery returns the set of script key types we use in the
913+ // SQL queries based on the passed parameters. If the user specified a script
914+ // key type, then we use that to filter the results. If the user didn't
915+ // specify a script key type, then we use the full set of script key types
916+ // or the set of script key types that excludes channel related assets,
917+ // depending on the filterChannelRelated parameter.
918+ func scriptKeyTypesForQuery (filterChannelRelated bool ,
919+ userSpecified fn.Option [asset.ScriptKeyType ]) []sql.NullInt16 {
920+
921+ return fn .Map (asset .ScriptKeyTypeForDatabaseQuery (
922+ filterChannelRelated , userSpecified ,
923+ ), sqlInt16 )
924+ }
925+
908926// specificAssetFilter maps the given asset parameters to the set of filters
909927// we use in the SQL queries.
910928func (a * AssetStore ) specificAssetFilter (id asset.ID , anchorPoint wire.OutPoint ,
@@ -945,6 +963,17 @@ func fetchAssetsWithWitness(ctx context.Context, q ActiveAssetsStore,
945963 assetFilter QueryAssetFilters ) ([]ConfirmedAsset , assetWitnesses ,
946964 error ) {
947965
966+ // We're using a slice of types to query for the set of script key
967+ // types, which is turned into a `xxx IN (...)` SQL query. But that
968+ // doesn't work for empty slices, as that would result in
969+ // `xxx IN (NULL)` which evaluates to false. So we need to use all
970+ // available types instead.
971+ if len (assetFilter .ScriptKeyType ) == 0 {
972+ assetFilter .ScriptKeyType = fn .Map (
973+ asset .AllScriptKeyTypes , sqlInt16 ,
974+ )
975+ }
976+
948977 // First, we'll fetch all the assets we know of on disk.
949978 dbAssets , err := q .QueryAssets (ctx , assetFilter )
950979 if err != nil {
@@ -1002,21 +1031,7 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
10021031 // channels. The balance of those assets is reported through lnd channel
10031032 // balance. Those assets are identified by the specific script key type
10041033 // for channel keys. We exclude them unless explicitly queried for.
1005- assetBalancesFilter .ExcludeScriptKeyType = sqlInt16 (
1006- asset .ScriptKeyScriptPathChannel ,
1007- )
1008-
1009- // The fn.None option means we don't restrict on script key type at all.
1010- skt .WhenSome (func (t asset.ScriptKeyType ) {
1011- assetBalancesFilter .ScriptKeyType = sqlInt16 (t )
1012-
1013- // If the user explicitly wants to see the channel related asset
1014- // balances, we need to set the exclude type to NULL.
1015- if t == asset .ScriptKeyScriptPathChannel {
1016- nullValue := sql.NullInt16 {}
1017- assetBalancesFilter .ExcludeScriptKeyType = nullValue
1018- }
1019- })
1034+ assetBalancesFilter .ScriptKeyType = scriptKeyTypesForQuery (true , skt )
10201035
10211036 // By default, we only show assets that are not leased.
10221037 if ! includeLeased {
@@ -1094,21 +1109,7 @@ func (a *AssetStore) QueryAssetBalancesByGroup(ctx context.Context,
10941109 // channels. The balance of those assets is reported through lnd channel
10951110 // balance. Those assets are identified by the specific script key type
10961111 // for channel keys. We exclude them unless explicitly queried for.
1097- assetBalancesFilter .ExcludeScriptKeyType = sqlInt16 (
1098- asset .ScriptKeyScriptPathChannel ,
1099- )
1100-
1101- // The fn.None option means we don't restrict on script key type at all.
1102- skt .WhenSome (func (t asset.ScriptKeyType ) {
1103- assetBalancesFilter .ScriptKeyType = sqlInt16 (t )
1104-
1105- // If the user explicitly wants to see the channel related asset
1106- // balances, we need to set the exclude type to NULL.
1107- if t == asset .ScriptKeyScriptPathChannel {
1108- nullValue := sql.NullInt16 {}
1109- assetBalancesFilter .ExcludeScriptKeyType = nullValue
1110- }
1111- })
1112+ assetBalancesFilter .ScriptKeyType = scriptKeyTypesForQuery (true , skt )
11121113
11131114 // By default, we only show assets that are not leased.
11141115 if ! includeLeased {
0 commit comments