Skip to content

Commit 13372e9

Browse files
committed
wip
1 parent 1a48165 commit 13372e9

1 file changed

Lines changed: 25 additions & 48 deletions

File tree

  • cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc

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

Lines changed: 25 additions & 48 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 (`matchesAssetPattern` txOutValueToValue txOutValue) (pat ^. U5c.maybe'asset)
6852

6953
matchesAddressPattern
7054
:: UtxoRpc.AddressPattern
@@ -73,23 +57,22 @@ matchesAddressPattern
7357
matchesAddressPattern pat addr =
7458
exactMatch && paymentMatch && delegationMatch
7559
where
76-
exact = pat ^. U5c.exactAddress
60+
matchesRawField field actual = BS.null field || field == actual
61+
7762
exactMatch = case addr of
78-
AddressInEra ByronAddressInAnyEra a -> BS.null exact || serialiseToRawBytes a == exact
79-
AddressInEra ShelleyAddressInEra{} a -> BS.null exact || serialiseToRawBytes a == exact
80-
payment = pat ^. U5c.paymentPart
63+
AddressInEra ByronAddressInAnyEra a -> matchesRawField (pat ^. U5c.exactAddress) $ serialiseToRawBytes a
64+
AddressInEra ShelleyAddressInEra{} a -> matchesRawField (pat ^. U5c.exactAddress) $ serialiseToRawBytes a
8165
paymentMatch = case addr of
8266
AddressInEra ShelleyAddressInEra{} (ShelleyAddress _ payCred _) ->
83-
BS.null payment || serialisePaymentCredential (fromShelleyPaymentCredential payCred) == payment
84-
_ -> BS.null payment
85-
deleg = pat ^. U5c.delegationPart
67+
matchesRawField (pat ^. U5c.paymentPart) . serialisePaymentCredential $ fromShelleyPaymentCredential payCred
68+
_ -> BS.null $ pat ^. U5c.paymentPart
8669
delegationMatch = case addr of
8770
AddressInEra ShelleyAddressInEra{} (ShelleyAddress _ _ stakeRef) ->
8871
case fromShelleyStakeReference stakeRef of
8972
StakeAddressByValue cred ->
90-
BS.null deleg || serialiseStakeCredential cred == deleg
91-
_ -> BS.null deleg
92-
_ -> BS.null deleg
73+
matchesRawField (pat ^. U5c.delegationPart) $ serialiseStakeCredential cred
74+
_ -> BS.null $ pat ^. U5c.delegationPart
75+
_ -> BS.null $ pat ^. U5c.delegationPart
9376

9477
serialisePaymentCredential :: PaymentCredential -> ByteString
9578
serialisePaymentCredential (PaymentCredentialByKey h) = serialiseToRawBytes h
@@ -119,22 +102,16 @@ matchesAssetPattern pat value =
119102
extractAddressesFromPredicate :: UtxoRpc.UtxoPredicate -> Maybe (Set AddressAny)
120103
extractAddressesFromPredicate p =
121104
case (p ^. U5c.maybe'match, p ^. U5c.not, p ^. U5c.allOf, p ^. U5c.anyOf) of
122-
-- Simple match with exact_address only
123105
(Just pat, [], [], []) -> extractAddressFromPattern pat
124-
-- any_of where each has an exact address
125106
(Nothing, [], [], anyPreds@(_ : _)) ->
126-
foldM (\acc sub -> Set.union acc <$> extractAddressesFromPredicate sub) Set.empty anyPreds
107+
Set.unions <$> traverse extractAddressesFromPredicate anyPreds
127108
_ -> Nothing
128109
where
129110
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
111+
extractAddressFromPattern pat = do
112+
txoPat <- pat ^. U5c.maybe'cardano
113+
addrPat <- txoPat ^. U5c.maybe'address
114+
let exact = addrPat ^. U5c.exactAddress
115+
guard $ not (BS.null exact)
116+
addrAny <- either (const Nothing) Just $ deserialiseFromRawBytes AsAddressAny exact
117+
pure $ Set.singleton addrAny

0 commit comments

Comments
 (0)