Skip to content

Commit 14cdaa3

Browse files
committed
wip
1 parent 1a48165 commit 14cdaa3

File tree

1 file changed

+22
-42
lines changed
  • cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc

1 file changed

+22
-42
lines changed

cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Predicate.hs

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
1717

1818
import RIO hiding (toList)
1919

20+
import Control.Monad (guard)
2021
import Data.ByteString qualified as BS
2122
import Data.Set (Set)
2223
import Data.Set qualified as Set
@@ -29,42 +30,25 @@ matchesUtxoPredicate
2930
-> TxOut CtxUTxO era
3031
-> Bool
3132
matchesUtxoPredicate p txOut =
32-
matchField && notField && allOfField && anyOfField
33-
where
34-
matchField = case p ^. U5c.maybe'match of
35-
Nothing -> True
36-
Just pat -> matchesAnyUtxoPattern pat txOut
37-
notField = all (\sub -> Prelude.not $ matchesUtxoPredicate sub txOut) (p ^. U5c.not)
38-
allOfField = case p ^. U5c.allOf of
39-
[] -> True
40-
ps -> all (`matchesUtxoPredicate` txOut) ps
41-
anyOfField = case p ^. U5c.anyOf of
42-
[] -> True
43-
ps -> any (`matchesUtxoPredicate` txOut) ps
33+
all (`matchesAnyUtxoPattern` txOut) (p ^. U5c.maybe'match)
34+
&& not (any (`matchesUtxoPredicate` txOut) (p ^. U5c.not))
35+
&& all (`matchesUtxoPredicate` txOut) (p ^. U5c.allOf)
36+
&& (null (p ^. U5c.anyOf) || any (`matchesUtxoPredicate` txOut) (p ^. U5c.anyOf))
4437

4538
matchesAnyUtxoPattern
4639
:: UtxoRpc.AnyUtxoPattern
4740
-> TxOut CtxUTxO era
4841
-> Bool
4942
matchesAnyUtxoPattern pat txOut =
50-
case pat ^. U5c.maybe'utxoPattern of
51-
Just (UtxoRpc.AnyUtxoPattern'Cardano txOutputPattern) ->
52-
matchesTxOutputPattern txOutputPattern txOut
53-
Nothing -> True
43+
all (`matchesTxOutputPattern` txOut) (pat ^. U5c.maybe'cardano)
5444

5545
matchesTxOutputPattern
5646
:: UtxoRpc.TxOutputPattern
5747
-> TxOut CtxUTxO era
5848
-> Bool
5949
matchesTxOutputPattern pat (TxOut addrInEra txOutValue _datum _script) =
60-
addressMatches && assetMatches
61-
where
62-
addressMatches = case pat ^. U5c.maybe'address of
63-
Nothing -> True
64-
Just addrPat -> matchesAddressPattern addrPat addrInEra
65-
assetMatches = case pat ^. U5c.maybe'asset of
66-
Nothing -> True
67-
Just assetPat -> matchesAssetPattern assetPat (txOutValueToValue txOutValue)
50+
all (`matchesAddressPattern` addrInEra) (pat ^. U5c.maybe'address)
51+
&& all (\assetPat -> matchesAssetPattern assetPat (txOutValueToValue txOutValue)) (pat ^. U5c.maybe'asset)
6852

6953
matchesAddressPattern
7054
:: UtxoRpc.AddressPattern
@@ -73,21 +57,23 @@ matchesAddressPattern
7357
matchesAddressPattern pat addr =
7458
exactMatch && paymentMatch && delegationMatch
7559
where
60+
matchesRawField field actual = BS.null field || field == actual
61+
7662
exact = pat ^. U5c.exactAddress
7763
exactMatch = case addr of
78-
AddressInEra ByronAddressInAnyEra a -> BS.null exact || serialiseToRawBytes a == exact
79-
AddressInEra ShelleyAddressInEra{} a -> BS.null exact || serialiseToRawBytes a == exact
64+
AddressInEra ByronAddressInAnyEra a -> matchesRawField exact (serialiseToRawBytes a)
65+
AddressInEra ShelleyAddressInEra{} a -> matchesRawField exact (serialiseToRawBytes a)
8066
payment = pat ^. U5c.paymentPart
8167
paymentMatch = case addr of
8268
AddressInEra ShelleyAddressInEra{} (ShelleyAddress _ payCred _) ->
83-
BS.null payment || serialisePaymentCredential (fromShelleyPaymentCredential payCred) == payment
69+
matchesRawField payment (serialisePaymentCredential (fromShelleyPaymentCredential payCred))
8470
_ -> BS.null payment
8571
deleg = pat ^. U5c.delegationPart
8672
delegationMatch = case addr of
8773
AddressInEra ShelleyAddressInEra{} (ShelleyAddress _ _ stakeRef) ->
8874
case fromShelleyStakeReference stakeRef of
8975
StakeAddressByValue cred ->
90-
BS.null deleg || serialiseStakeCredential cred == deleg
76+
matchesRawField deleg (serialiseStakeCredential cred)
9177
_ -> BS.null deleg
9278
_ -> BS.null deleg
9379

@@ -119,22 +105,16 @@ matchesAssetPattern pat value =
119105
extractAddressesFromPredicate :: UtxoRpc.UtxoPredicate -> Maybe (Set AddressAny)
120106
extractAddressesFromPredicate p =
121107
case (p ^. U5c.maybe'match, p ^. U5c.not, p ^. U5c.allOf, p ^. U5c.anyOf) of
122-
-- Simple match with exact_address only
123108
(Just pat, [], [], []) -> extractAddressFromPattern pat
124-
-- any_of where each has an exact address
125109
(Nothing, [], [], anyPreds@(_ : _)) ->
126-
foldM (\acc sub -> Set.union acc <$> extractAddressesFromPredicate sub) Set.empty anyPreds
110+
Set.unions <$> traverse extractAddressesFromPredicate anyPreds
127111
_ -> Nothing
128112
where
129113
extractAddressFromPattern :: UtxoRpc.AnyUtxoPattern -> Maybe (Set AddressAny)
130-
extractAddressFromPattern pat =
131-
case pat ^. U5c.maybe'utxoPattern of
132-
Just (UtxoRpc.AnyUtxoPattern'Cardano txoPat) -> do
133-
addrPat <- txoPat ^. U5c.maybe'address
134-
let exact = addrPat ^. U5c.exactAddress
135-
if BS.null exact
136-
then Nothing
137-
else do
138-
addrAny <- either (const Nothing) Just $ deserialiseFromRawBytes AsAddressAny exact
139-
Just (Set.singleton addrAny)
140-
Nothing -> Nothing
114+
extractAddressFromPattern pat = do
115+
txoPat <- pat ^. U5c.maybe'cardano
116+
addrPat <- txoPat ^. U5c.maybe'address
117+
let exact = addrPat ^. U5c.exactAddress
118+
guard $ not (BS.null exact)
119+
addrAny <- either (const Nothing) Just $ deserialiseFromRawBytes AsAddressAny exact
120+
pure $ Set.singleton addrAny

0 commit comments

Comments
 (0)