@@ -51,7 +51,9 @@ import Control.Monad.Except
51
51
import Data.Functor.Identity
52
52
import Data.Kind (Type )
53
53
import Data.Map.Strict (Map )
54
+ import qualified Data.Map.Strict as Map
54
55
import Data.Set (Set )
56
+ import qualified Data.Set as Set
55
57
import Data.Type.Equality ((:~:) (Refl ), apply )
56
58
import GHC.Generics (Generic )
57
59
@@ -289,6 +291,8 @@ newtype NonMyopicMemberRewards c = NonMyopicMemberRewards {
289
291
deriving stock (Show )
290
292
deriving newtype (Eq )
291
293
294
+ type Delegations c = Map (SL. Credential 'SL.Staking c ) (SL. KeyHash 'SL.StakePool c )
295
+
292
296
instance Crypto c => Serialise (NonMyopicMemberRewards c ) where
293
297
encode = toCBOR . unNonMyopicMemberRewards
294
298
decode = NonMyopicMemberRewards <$> fromCBOR
@@ -336,6 +340,10 @@ instance TPraosCrypto c => QueryLedger (ShelleyBlock c) where
336
340
:: Query (ShelleyBlock c ) result
337
341
-> Query (ShelleyBlock c ) (Serialised result )
338
342
343
+ GetFilteredDelegationsAndRewardAccounts
344
+ :: Set (SL. Credential 'SL.Staking c )
345
+ -> Query (ShelleyBlock c ) (Delegations c , SL. RewardAccounts c )
346
+
339
347
340
348
answerQuery cfg query st = case query of
341
349
GetLedgerTip -> ledgerTip st
@@ -350,6 +358,11 @@ instance TPraosCrypto c => QueryLedger (ShelleyBlock c) where
350
358
GetCurrentLedgerState -> getCurrentLedgerState $ shelleyState st
351
359
GetCBOR query' -> mkSerialised (encodeShelleyResult query' ) $
352
360
answerQuery cfg query' st
361
+ GetFilteredDelegationsAndRewardAccounts creds ->
362
+ getFilteredDelegationsAndRewardAccounts
363
+ (shelleyLedgerGlobals cfg )
364
+ (shelleyState st )
365
+ creds
353
366
where
354
367
globals = shelleyLedgerGlobals cfg
355
368
@@ -399,21 +412,30 @@ instance TPraosCrypto c => QueryLedger (ShelleyBlock c) where
399
412
= apply Refl <$> eqQuery q q'
400
413
eqQuery (GetCBOR _ ) _
401
414
= Nothing
415
+ eqQuery (GetFilteredDelegationsAndRewardAccounts creds )
416
+ (GetFilteredDelegationsAndRewardAccounts creds' )
417
+ | creds == creds'
418
+ = Just Refl
419
+ | otherwise
420
+ = Nothing
421
+ eqQuery (GetFilteredDelegationsAndRewardAccounts _ ) _
422
+ = Nothing
402
423
403
424
deriving instance Eq (Query (ShelleyBlock c ) result )
404
425
deriving instance Show (Query (ShelleyBlock c ) result )
405
426
406
427
instance Crypto c => ShowQuery (Query (ShelleyBlock c )) where
407
- showResult GetLedgerTip = show
408
- showResult GetEpochNo = show
409
- showResult (GetNonMyopicMemberRewards {}) = show
410
- showResult GetCurrentPParams = show
411
- showResult GetProposedPParamsUpdates = show
412
- showResult GetStakeDistribution = show
413
- showResult (GetFilteredUTxO {}) = show
414
- showResult GetUTxO = show
415
- showResult GetCurrentLedgerState = show
416
- showResult (GetCBOR {}) = show
428
+ showResult GetLedgerTip = show
429
+ showResult GetEpochNo = show
430
+ showResult (GetNonMyopicMemberRewards {}) = show
431
+ showResult GetCurrentPParams = show
432
+ showResult GetProposedPParamsUpdates = show
433
+ showResult GetStakeDistribution = show
434
+ showResult (GetFilteredUTxO {}) = show
435
+ showResult GetUTxO = show
436
+ showResult GetCurrentLedgerState = show
437
+ showResult (GetCBOR {}) = show
438
+ showResult (GetFilteredDelegationsAndRewardAccounts {}) = show
417
439
418
440
{- ------------------------------------------------------------------------------
419
441
ValidateEnvelope
@@ -454,6 +476,22 @@ getCurrentLedgerState =
454
476
nukeUtxOSet :: SL. UTxOState c -> SL. UTxOState c
455
477
nukeUtxOSet us = us { SL. _utxo = SL. UTxO mempty }
456
478
479
+ getDState :: SL. ShelleyState c -> SL. DState c
480
+ getDState = SL. _dstate . SL. _delegationState . SL. esLState . SL. nesEs
481
+
482
+ getFilteredDelegationsAndRewardAccounts :: SL. Globals
483
+ -> SL. ShelleyState c
484
+ -> Set (SL. Credential 'SL.Staking c )
485
+ -> (Delegations c , SL. RewardAccounts c )
486
+ getFilteredDelegationsAndRewardAccounts globals ss creds =
487
+ (filteredDelegations, filteredRwdAcnts)
488
+ where
489
+ network = SL. networkId globals
490
+ rwdAcnts = Set. map (SL. RewardAcnt network) creds
491
+ dstate = getDState ss
492
+ filteredDelegations = Map. restrictKeys (SL. _delegations dstate) creds
493
+ filteredRwdAcnts = Map. restrictKeys (SL. _rewards dstate) rwdAcnts
494
+
457
495
{- ------------------------------------------------------------------------------
458
496
Serialisation
459
497
-------------------------------------------------------------------------------}
@@ -525,53 +563,58 @@ encodeShelleyQuery query = case query of
525
563
CBOR. encodeListLen 1 <> CBOR. encodeWord8 8
526
564
GetCBOR query' ->
527
565
CBOR. encodeListLen 2 <> CBOR. encodeWord8 9 <> encodeShelleyQuery query'
566
+ GetFilteredDelegationsAndRewardAccounts creds ->
567
+ CBOR. encodeListLen 2 <> CBOR. encodeWord8 10 <> toCBOR creds
528
568
529
569
decodeShelleyQuery :: Crypto c => Decoder s (Some (Query (ShelleyBlock c )))
530
570
decodeShelleyQuery = do
531
571
len <- CBOR. decodeListLen
532
572
tag <- CBOR. decodeWord8
533
573
case (len, tag) of
534
- (1 , 0 ) -> return $ Some GetLedgerTip
535
- (1 , 1 ) -> return $ Some GetEpochNo
536
- (2 , 2 ) -> Some . GetNonMyopicMemberRewards <$> fromCBOR
537
- (1 , 3 ) -> return $ Some GetCurrentPParams
538
- (1 , 4 ) -> return $ Some GetProposedPParamsUpdates
539
- (1 , 5 ) -> return $ Some GetStakeDistribution
540
- (2 , 6 ) -> Some . GetFilteredUTxO <$> fromCBOR
541
- (1 , 7 ) -> return $ Some GetUTxO
542
- (1 , 8 ) -> return $ Some GetCurrentLedgerState
543
- (2 , 9 ) -> (\ (Some q) -> Some (GetCBOR q)) <$> decodeShelleyQuery
544
- _ -> fail $
574
+ (1 , 0 ) -> return $ Some GetLedgerTip
575
+ (1 , 1 ) -> return $ Some GetEpochNo
576
+ (2 , 2 ) -> Some . GetNonMyopicMemberRewards <$> fromCBOR
577
+ (1 , 3 ) -> return $ Some GetCurrentPParams
578
+ (1 , 4 ) -> return $ Some GetProposedPParamsUpdates
579
+ (1 , 5 ) -> return $ Some GetStakeDistribution
580
+ (2 , 6 ) -> Some . GetFilteredUTxO <$> fromCBOR
581
+ (1 , 7 ) -> return $ Some GetUTxO
582
+ (1 , 8 ) -> return $ Some GetCurrentLedgerState
583
+ (2 , 9 ) -> (\ (Some q) -> Some (GetCBOR q)) <$> decodeShelleyQuery
584
+ (2 , 10 ) -> Some . GetFilteredDelegationsAndRewardAccounts <$> fromCBOR
585
+ _ -> fail $
545
586
" decodeShelleyQuery: invalid (len, tag): (" <>
546
587
show len <> " , " <> show tag <> " )"
547
588
548
589
encodeShelleyResult
549
590
:: Crypto c
550
591
=> Query (ShelleyBlock c ) result -> result -> Encoding
551
592
encodeShelleyResult query = case query of
552
- GetLedgerTip -> encodePoint encode
553
- GetEpochNo -> encode
554
- GetNonMyopicMemberRewards {} -> encode
555
- GetCurrentPParams -> toCBOR
556
- GetProposedPParamsUpdates -> toCBOR
557
- GetStakeDistribution -> toCBOR
558
- GetFilteredUTxO {} -> toCBOR
559
- GetUTxO -> toCBOR
560
- GetCurrentLedgerState -> toCBOR
561
- GetCBOR {} -> encode
593
+ GetLedgerTip -> encodePoint encode
594
+ GetEpochNo -> encode
595
+ GetNonMyopicMemberRewards {} -> encode
596
+ GetCurrentPParams -> toCBOR
597
+ GetProposedPParamsUpdates -> toCBOR
598
+ GetStakeDistribution -> toCBOR
599
+ GetFilteredUTxO {} -> toCBOR
600
+ GetUTxO -> toCBOR
601
+ GetCurrentLedgerState -> toCBOR
602
+ GetCBOR {} -> encode
603
+ GetFilteredDelegationsAndRewardAccounts {} -> toCBOR
562
604
563
605
decodeShelleyResult
564
606
:: Crypto c
565
607
=> Query (ShelleyBlock c ) result
566
608
-> forall s . Decoder s result
567
609
decodeShelleyResult query = case query of
568
- GetLedgerTip -> decodePoint decode
569
- GetEpochNo -> decode
570
- GetNonMyopicMemberRewards {} -> decode
571
- GetCurrentPParams -> fromCBOR
572
- GetProposedPParamsUpdates -> fromCBOR
573
- GetStakeDistribution -> fromCBOR
574
- GetFilteredUTxO {} -> fromCBOR
575
- GetUTxO -> fromCBOR
576
- GetCurrentLedgerState -> fromCBOR
577
- GetCBOR {} -> decode
610
+ GetLedgerTip -> decodePoint decode
611
+ GetEpochNo -> decode
612
+ GetNonMyopicMemberRewards {} -> decode
613
+ GetCurrentPParams -> fromCBOR
614
+ GetProposedPParamsUpdates -> fromCBOR
615
+ GetStakeDistribution -> fromCBOR
616
+ GetFilteredUTxO {} -> fromCBOR
617
+ GetUTxO -> fromCBOR
618
+ GetCurrentLedgerState -> fromCBOR
619
+ GetCBOR {} -> decode
620
+ GetFilteredDelegationsAndRewardAccounts {} -> fromCBOR
0 commit comments