Skip to content

Commit

Permalink
Filter ledger event for given stake address
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Sep 22, 2023
1 parent d28a3da commit a74045a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ package cardano-node-chairman
ghc-options: -Werror

package cardano-node
ghc-options: -Werror
ghc-options: -Wwarn

package cardano-submit-api
ghc-options: -Werror
Expand Down
5 changes: 3 additions & 2 deletions cardano-node/app/cardano-events.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import System.Environment
import Cardano.Node.LedgerEvent (tailEvent)
import Cardano.Node.LedgerEvent (foldEvent, filterRewards)
import System.IO(stdin)

main :: IO ()
main = getArgs >>= tailEvent . head
main = foldEvent filterRewards mempty stdin
1 change: 1 addition & 0 deletions cardano-node/cardano-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ library
, aeson >= 1.5.6.0
, async
, base16-bytestring
, binary
, bytestring
, cardano-api ^>= 8.2
, cardano-crypto-class
Expand Down
20 changes: 19 additions & 1 deletion cardano-node/src/Cardano/Node/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ module Cardano.Node.LedgerEvent (
, eventCodecVersion
, deserializeEvent
, serializeEvent
, foldEvent
, foldEvent, filterRewards
) where

import Cardano.Prelude hiding (All, Sum)

import Data.Binary.Get(runGetOrFail)
import Cardano.Ledger.Binary (DecCBOR(..), EncCBOR(..), Version, fromCBOR,
serialize', toCBOR, toPlainDecoder)
import Cardano.Ledger.Binary.Coders (Decode(..), Encode (..), encode, (!>),
(<!), decode)
import Cardano.Ledger.Coin (Coin (..), DeltaCoin (..))
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Address as Ledger
import Cardano.Ledger.Core (eraProtVerLow)
import qualified Cardano.Ledger.Credential as Ledger
import Cardano.Ledger.Crypto (Crypto, StandardCrypto)
Expand All @@ -59,6 +61,7 @@ import Codec.CBOR.Read(deserialiseFromBytes)
import Control.State.Transition (Event)
import Data.ByteString.Short(ShortByteString)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Base16 as Hex
import qualified Data.Map.Strict as Map
import Data.SOP.Strict (All, K (..), NS(..), hcmap, hcollapse)
import qualified Data.Set as Set
Expand Down Expand Up @@ -181,6 +184,7 @@ type StakeCred = Ledger.StakeCredential StandardCrypto
-- The `ledger-specs` code defines a `RewardUpdate` type that is parameterised over
-- Shelley/Allegra/Mary. This is a huge pain in the neck for `db-sync` so we define a
-- generic one instead.
-- FIXME: use directly ledger types instead of wrapping them
newtype Rewards = Rewards
{ unRewards :: Map StakeCred (Set Reward)
}
Expand Down Expand Up @@ -511,3 +515,17 @@ foldEvent fn st0 h =
-> IO (LBS.ByteString, a)
unsafeDeserialiseFromBytes decoder =
either (panic . show) pure . deserialiseFromBytes decoder

filterRewards :: Coin -> AnchoredEvent -> IO Coin
filterRewards current = \case
AnchoredEvent{ledgerEvent = LedgerTotalRewards epoch rewardsMap} ->
pure $ maybe current ((<> current) . rewardsToCoin) $ Map.lookup stakeCred rewardsMap
_ -> pure current

where
rewardsToCoin = Set.foldr (<>) mempty . Set.map Ledger.rewardAmount

stakeCred = fromMaybe (panic "should not happen") $ do
case Hex.decode "e04cf4f01890215bd181d1fcd3c9589a2a4a3adbcff1a70b748080fa82" of
Right bytes -> Ledger.getRwdCred <$> Ledger.decodeRewardAcnt bytes
Left err -> panic (show err)

0 comments on commit a74045a

Please sign in to comment.