Skip to content

Commit

Permalink
Applied PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert committed Sep 28, 2021
1 parent 6b89da8 commit b375b70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
27 changes: 14 additions & 13 deletions marlowe-dashboard-client/src/Capability/PlutusApps/MarloweApp.purs
Expand Up @@ -29,6 +29,7 @@ import Data.Lens (Lens', toArrayOf, traversed, view)
import Data.Lens.Record (prop)
import Data.Map (Map)
import Data.Symbol (SProxy(..))
import Data.Traversable (for)
import Data.Tuple.Nested ((/\), type (/\))
import Data.UUID (UUID, genUUID)
import Effect (Effect)
Expand Down Expand Up @@ -87,6 +88,10 @@ createEndpointMutex = do
requests <- EAVar.new mempty
pure { create, applyInputs, redeem, requests }

-- This is the amount of requests we store in the request queue
maxRequests :: Int
maxRequests = 15

invokeMutexedEndpoint ::
forall payload m env.
Encode payload =>
Expand Down Expand Up @@ -123,7 +128,7 @@ invokeMutexedEndpoint plutusAppId reqId endpointName _endpointMutex payload = do
newRequestMutex <- AVar.empty
let
-- We add new requests to the begining of the array and remove them from the end.
newRequests = take 15 $ (reqId /\ newRequestMutex) : requests
newRequests = take maxRequests $ (reqId /\ newRequestMutex) : requests
AVar.put newRequests requestMutex
pure result

Expand All @@ -148,16 +153,14 @@ onNewObservableState lastResult = case lastResult of
requestMutex <- asks $ view (_marloweAppEndpointMutex <<< _requests)
-- This read is blocking but does not take the mutex.
requests <- liftAff $ AVar.read requestMutex
case findReqId reqId requests of
Nothing -> pure Nothing
Just reqMutex -> do
liftAff $ AVar.put lastResult reqMutex
pure $ Just lastResult
for (findReqId reqId requests) \reqMutex -> do
liftAff $ AVar.put lastResult reqMutex
pure lastResult

findReqId :: UUID -> Array (UUID /\ AVar LastResult) -> Maybe (AVar LastResult)
findReqId reqId = findMap (\(reqId' /\ reqMutex) -> if reqId == reqId' then Just reqMutex else Nothing)

-- TODO: This function is not used yet, but is intended to be used to be able to the refactor
-- TODO: This function is not used yet, but is intended to be used to be able to do the refactor
-- mentioned in MainFrame.State :: NewObservableState
waitForResponse ::
forall env m.
Expand All @@ -169,12 +172,10 @@ waitForResponse reqId = do
requestMutex <- asks $ view (_marloweAppEndpointMutex <<< _requests)
-- This read is blocking but does not take the mutex.
requests <- liftAff $ AVar.read requestMutex
case findReqId reqId requests of
Nothing -> pure Nothing
Just reqMutex -> do
-- TODO: We could add a timer so we don't wait forever
lastResult <- liftAff $ AVar.read reqMutex
pure $ Just lastResult
for (findReqId reqId requests) \reqMutex -> do
-- TODO: We could add a timer so we don't wait forever
lastResult <- liftAff $ AVar.read reqMutex
pure lastResult

-- Plutus contracts have endpoints that can be available or not. We get notified by the
-- websocket message NewActiveEndpoints when the status change, and we use this function
Expand Down
Expand Up @@ -20,14 +20,11 @@ import Marlowe.Semantics (Input, MarloweData, Slot, TransactionError)
import Plutus.Contract.StateMachine (InvalidTransition, SMContractError)
import Wallet.Types (ContractError)

-- TODO: We should change this for a Tuple of endpoint name and request id.
type EndpointName
= String

-- The Plutus contract state keeps track of the result of the last action. This is needed because
-- the PAB needs to return inmediatly and the result might take a while to compute.
-- Right now we are only allowing one endpoint to be called at a time, but we could later extend this
-- to use a RequestId to map between the request and the response.
data LastResult
= OK UUID EndpointName
| SomeError UUID EndpointName MarloweError
Expand Down

0 comments on commit b375b70

Please sign in to comment.