Skip to content

Commit

Permalink
Implement waiting using the indexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
raduom committed Aug 8, 2022
1 parent 3ef0896 commit 7d99801
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
19 changes: 17 additions & 2 deletions plutus-pab/src/Plutus/PAB/Core.hs
Expand Up @@ -87,6 +87,7 @@ import Control.Applicative (Alternative ((<|>)))
import Control.Concurrent.STM (STM)
import Control.Concurrent.STM qualified as STM
import Control.Lens (view)
import Control.Lens.Operators
import Control.Monad (forM, guard, void)
import Control.Monad.Freer (Eff, LastMember, Member, interpret, reinterpret, runM, send, subsume, type (~>))
import Control.Monad.Freer.Error (Error, runError, throwError)
Expand All @@ -96,11 +97,14 @@ import Control.Monad.Freer.Reader (Reader (Ask), ask, asks, runReader)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson qualified as JSON
import Data.Foldable (traverse_)
import Data.IORef (readIORef)
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Proxy (Proxy (Proxy))
import Data.Set (Set)
import Data.Text (Text)
import Index.VSplit qualified as Ix
import Index.VSqlite qualified as Ix
import Ledger (Address (addressCredential), Params, TxOutRef)
import Ledger.Address (PaymentPubKeyHash)
import Ledger.Tx (CardanoTx, ciTxOutValue)
Expand Down Expand Up @@ -510,8 +514,19 @@ waitForState extract instanceId = do
-- | Wait for the transaction to be confirmed on the blockchain.
waitForTxStatusChange :: forall t env. TxId -> PABAction t env TxStatus
waitForTxStatusChange t = do
env <- asks @(PABEnvironment t env) blockchainEnv
liftIO $ STM.atomically $ Instances.waitForTxStatusChange Unknown t env
env <- asks @(PABEnvironment t env) blockchainEnv
case Instances.beTxChanges env of
Left ix -> liftIO . STM.atomically $
Instances.waitForTxStatusChange Unknown t ix $
Instances.beLastSyncedBlockNo env
Right ixRef -> liftIO $ do
ix <- readIORef ixRef
blockNumber <- STM.readTVarIO $ Instances.beLastSyncedBlockNo env
events <- Ix.getEvents (ix ^. Ix.storage)
(ix ^. Ix.query) ix t events >>= \case
Nothing -> undefined
Just ts -> undefined
undefined

-- | Wait for the transaction output to be confirmed on the blockchain.
waitForTxOutStatusChange :: forall t env. TxOutRef -> PABAction t env TxOutStatus
Expand Down
6 changes: 5 additions & 1 deletion plutus-pab/src/Plutus/PAB/Core/ContractInstance.hs
Expand Up @@ -198,7 +198,11 @@ processTxStatusChangeRequestsSTM =
where
handler txId = do
env <- ask
pure (AwaitTxStatusChangeResp txId <$> InstanceState.waitForTxStatusChange Unknown txId env)
-- This should never be called in a context where there is a
-- Marconi indexer in the environment.
let ix = InstanceState.getUtxoIndexTxChanges env
bn = InstanceState.beLastSyncedBlockNo env
pure (AwaitTxStatusChangeResp txId <$> InstanceState.waitForTxStatusChange Unknown txId ix bn)

processTxOutStatusChangeRequestsSTM ::
forall effs.
Expand Down
9 changes: 5 additions & 4 deletions plutus-pab/src/Plutus/PAB/Core/ContractInstance/STM.hs
Expand Up @@ -407,10 +407,11 @@ insertInstance :: ContractInstanceId -> InstanceState -> InstancesState -> STM (
insertInstance instanceID state (InstancesState m) = STM.modifyTVar m (Map.insert instanceID state)

-- | Wait for the status of a transaction to change.
waitForTxStatusChange :: TxStatus -> TxId -> BlockchainEnv -> STM TxStatus
waitForTxStatusChange oldStatus tx env@BlockchainEnv{beLastSyncedBlockNo} = do
txIdState <- _usTxUtxoData . utxoState <$> STM.readTVar (getUtxoIndexTxChanges env)
blockNumber <- STM.readTVar beLastSyncedBlockNo
waitForTxStatusChange
:: TxStatus -> TxId -> TVar (UtxoIndex TxIdState) -> TVar BlockNumber -> STM TxStatus
waitForTxStatusChange oldStatus tx ix lastBlock = do
txIdState <- _usTxUtxoData . utxoState <$> STM.readTVar ix
blockNumber <- STM.readTVar lastBlock
let txStatus = transactionStatus blockNumber txIdState tx
-- Succeed only if we _found_ a status and it was different; if
-- the status hasn't changed, _or_ there was an error computing
Expand Down

0 comments on commit 7d99801

Please sign in to comment.