Skip to content

Commit

Permalink
SCB: Removing the ContractStateTransition event.
Browse files Browse the repository at this point in the history
It's a confusing duplicate of `ContractStateTransition`.
  • Loading branch information
Kris Jenkins authored and krisajenkins committed Aug 3, 2020
1 parent 0bbe232 commit f6dd2d5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
1 change: 0 additions & 1 deletion plutus-scb-client/src/View/Pretty.purs
Expand Up @@ -43,7 +43,6 @@ eventWithPrefix prefix content =

instance prettyUserEvent :: Pretty t => Pretty (UserEvent t) where
pretty (InstallContract contract) = span_ [ text $ "Install", nbsp, pretty contract ]
pretty (ContractStateTransition instanceState) = pretty instanceState

instance prettyContractExe :: Pretty ContractExe where
pretty ((ContractExe { contractPath })) = text contractPath
Expand Down
7 changes: 1 addition & 6 deletions plutus-scb/src/Plutus/SCB/Command.hs
Expand Up @@ -20,15 +20,13 @@ module Plutus.SCB.Command
( installCommand
, saveBalancedTx
, saveBalancedTxResult
, saveContractState
-- * Commands related to updating the contract state
, sendContractEvent
) where

import Eventful (Aggregate (Aggregate), aggregateCommandHandler, aggregateProjection)
import qualified Ledger
import Plutus.SCB.Events (ChainEvent (ContractEvent, UserEvent), ContractInstanceState,
UserEvent (ContractStateTransition, InstallContract))
import Plutus.SCB.Events (ChainEvent (ContractEvent, UserEvent), UserEvent (InstallContract))
import qualified Plutus.SCB.Events as Events
import Plutus.SCB.Query (nullProjection)

Expand All @@ -55,8 +53,5 @@ saveBalancedTx = sendEvents (return . Events.WalletEvent . Events.BalancedTx)
saveBalancedTxResult :: forall t. Aggregate () (ChainEvent t) Ledger.Tx
saveBalancedTxResult = sendEvents (return . Events.NodeEvent . Events.SubmittedTx)

saveContractState :: forall t. Aggregate () (ChainEvent t) (ContractInstanceState t)
saveContractState = sendEvents (return . UserEvent . ContractStateTransition)

sendContractEvent :: forall t. Aggregate () (ChainEvent t) (Events.Contract.ContractEvent t)
sendContractEvent = sendEvents (return . ContractEvent)
5 changes: 2 additions & 3 deletions plutus-scb/src/Plutus/SCB/Core/ContractInstance.hs
Expand Up @@ -58,7 +58,7 @@ import Wallet.Effects (ChainIndexEffe
import Wallet.Emulator.LogMessages (TxBalanceMsg)

import Plutus.SCB.Command (saveBalancedTx, saveBalancedTxResult,
saveContractState, sendContractEvent)
sendContractEvent)
import Plutus.SCB.Effects.Contract (ContractCommand (..), ContractEffect)
import qualified Plutus.SCB.Effects.Contract as Contract
import Plutus.SCB.Effects.EventLog (EventLogEffect, runCommand, runGlobalQuery)
Expand All @@ -71,7 +71,7 @@ import Plutus.SCB.Events.Contract (ContractEvent
unContractHandlersResponse)
import qualified Plutus.SCB.Events.Contract as Events.Contract
import qualified Plutus.SCB.Query as Query
import Plutus.SCB.Types (SCBError (..), Source (ContractEventSource, NodeEventSource, UserEventSource, WalletEventSource))
import Plutus.SCB.Types (SCBError (..), Source (ContractEventSource, NodeEventSource, WalletEventSource))
import Plutus.SCB.Utils (tshow)

import qualified Plutus.SCB.Core.Projections as Projections
Expand Down Expand Up @@ -142,7 +142,6 @@ sendContractStateMessages is = do
void
$ runCommand (sendContractEvent @t) ContractEventSource
$ ContractInstanceStateUpdateEvent is
void $ runCommand (saveContractState @t) UserEventSource is

sendContractMessage ::
forall t effs.
Expand Down
4 changes: 4 additions & 0 deletions plutus-scb/src/Plutus/SCB/Events/Contract.hs
Expand Up @@ -15,6 +15,7 @@ module Plutus.SCB.Events.Contract(
, ContractHandlersResponse(..)
, ContractResponse(..)
, PartiallyDecodedResponse(..)
, hasActiveRequests
, ContractInstanceState(..)
-- * Prisms
-- ** ContractRequest
Expand Down Expand Up @@ -221,3 +222,6 @@ instance Pretty t => Pretty (ContractEvent t) where
makePrisms ''ContractSCBRequest
makePrisms ''ContractResponse
makePrisms ''ContractEvent

hasActiveRequests :: ContractInstanceState t -> Bool
hasActiveRequests = not . null . hooks . csCurrentState
15 changes: 6 additions & 9 deletions plutus-scb/src/Plutus/SCB/Events/User.hs
@@ -1,25 +1,22 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StrictData #-}

module Plutus.SCB.Events.User where

import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson (FromJSON, ToJSON)
import Data.Text.Prettyprint.Doc
import GHC.Generics (Generic)
import Plutus.SCB.Events.Contract (ContractInstanceState)
import GHC.Generics (Generic)

-- | Users can install contracts and transition them to a new state.
-- Contracts are identified by values of 't'.
data UserEvent t
= InstallContract !t
| ContractStateTransition !(ContractInstanceState t)
newtype UserEvent t
= InstallContract t
deriving (Show, Eq, Generic)
deriving anyclass (FromJSON, ToJSON)
deriving newtype (FromJSON, ToJSON)

instance Pretty t => Pretty (UserEvent t) where
pretty = \case
InstallContract t -> "InstallContract:" <+> pretty t
ContractStateTransition s -> "ContractStateTransition:" <+> pretty s
23 changes: 12 additions & 11 deletions plutus-scb/src/Plutus/SCB/Query.hs
Expand Up @@ -54,7 +54,7 @@ import Ledger (Address, Tx, TxId, TxO
txOutRefId, txOutTxOut, txOutTxTx)
import Ledger.Index (UtxoIndex (UtxoIndex))
import Plutus.SCB.Events (ChainEvent (..), NodeEvent (SubmittedTx),
UserEvent (ContractStateTransition, InstallContract))
UserEvent (InstallContract), hasActiveRequests)
import Plutus.SCB.Events.Contract (ContractEvent (..), ContractInstanceId,
ContractInstanceState (..), ContractResponse (..),
IterationID)
Expand Down Expand Up @@ -184,16 +184,17 @@ contractStates IteratedContractState{icsContractIterations=ContractIterationStat

-- | IDs of active contracts by contract type
activeContractsProjection ::
forall t key position. Ord t =>
Projection (Map t (Set ContractInstanceId)) (StreamEvent key position (ChainEvent t))
forall t key position. Ord t
=> Projection (Map t (Set ContractInstanceId)) (StreamEvent key position (ChainEvent t))
activeContractsProjection =
let projectionEventHandler m (StreamEvent _ _ (UserEvent (ContractStateTransition state))) =
Map.insertWith (<>) (csContractDefinition state) (Set.singleton (csContract state)) m
projectionEventHandler m _ = m
in Projection
{ projectionSeed = Map.empty
, projectionEventHandler
}
let projectionEventHandler m = \case
(StreamEvent _ _ (ContractEvent (ContractInstanceStateUpdateEvent state))) ->
let key = csContractDefinition state
in if hasActiveRequests state
then Map.insertWith (<>) key (Set.singleton (csContract state)) m
else Map.delete key m
_ -> m
in Projection {projectionSeed = Map.empty, projectionEventHandler}

-- | Transactions submitted to the node.
txHistoryProjection ::
Expand All @@ -212,7 +213,7 @@ activeContractHistoryProjection ::
activeContractHistoryProjection cid =
projectionMapMaybe contractPaths monoidProjection
where
contractPaths (StreamEvent _ _ (UserEvent (ContractStateTransition state))) =
contractPaths (StreamEvent _ _ (ContractEvent (ContractInstanceStateUpdateEvent state))) =
if csContract state == cid
then Just [state]
else Nothing
Expand Down

0 comments on commit f6dd2d5

Please sign in to comment.