<p>85% sure this is a bug introduced by changes to the chain index.</p>
<p>A work around is to mod the <code>StateMachine.hs</code> file</p>
<pre><code>getDatum ::
    ( AsSMContractError e)
    =&gt; Tx.ChainIndexTxOut
    -&gt; Contract w schema e Tx.ChainIndexTxOut
getDatum tx = mapError (review _SMContractError) $ do
    case Tx._ciTxOutDatum tx of
         Left dh -&gt; do
            dM &lt;- datumFromHash dh
            case dM of
                 Just d -&gt; return (set Tx.ciTxOutDatum (Right d) tx)
                 Nothing -&gt; return tx
         Right _ -&gt; return tx



{-| Get the current on-chain state of the state machine instance.
    Return Nothing if there is no state on chain.
    Throws an @SMContractError@ if the number of outputs at the machine address is greater than one.
-}
getOnChainState ::
    ( AsSMContractError e
    , PlutusTx.FromData state
    , PlutusTx.ToData state
    )
    =&gt; StateMachineClient state i
    -&gt; Contract w schema e (Maybe (OnChainState state i, Map TxOutRef Tx.ChainIndexTxOut))
getOnChainState StateMachineClient{scInstance, scChooser} = mapError (review _SMContractError) $ do
    utxoTx_ &lt;- utxosAt (SM.machineAddress scInstance)
    utxoTx &lt;- sequence (Map.map getDatum utxoTx_) -- FETCH DATUMS
    let states = getStates scInstance utxoTx
    case states of
        [] -&gt; pure Nothing
        _  -&gt; case scChooser states of
                Left err    -&gt; throwing _SMContractError err
                Right state -&gt; pure $ Just (state, utxoTx)

</code></pre>
<p>Not pretty but I think it works.
Alternative is to mod the chain index code emulating the old behavior.</p>
<p><a href="https://github.com/input-output-hk/plutus-apps/issues/398" rel="nofollow noreferrer">https://github.com/input-output-hk/plutus-apps/issues/398</a></p>

CC BY-SA 4.0
2022-04-06T09:06:33.043Z
2022-04-06T09:06:33.043Z
waalge
<p>My first sugestion would be to avoid using the out-of-the box statemachine from the plutus library at the moment.  In it's current form it is highly inneficient and you will quickly run into maxTx size limits on testnet.   If you want to run a state machine, it is best to build your own so it can be as efficient as possible.   Also, chain index and pab is a nightmare when trying to test on testnet, so it is probably best to use a service like blockfrost.</p>
<p>I have just release The Open Source Cardano Lottery project, which has source code that implements a state machine as well as how to test on testnet (and devnent).   It is a beta non-prod release, so there is room for improvements, but it is fully functional on the testnet.</p>
<p>You can find the source code here:</p>
<p><a href="https://github.com/lley154/cardano-lottery" rel="nofollow noreferrer">https://github.com/lley154/cardano-lottery</a></p>

CC BY-SA 4.0
2022-05-07T00:07:30.397Z
2022-05-07T00:07:30.397Z
lley154
<p>I want to run a statemachine on testnet.</p>
<p>The SM runs in the emulator, and I can initialize on testnet,
but if I try to make a new client it throws an error.
The error comes from transition failure, but this is due to the PAB not being able to re-establish state.</p>
<p>I find this pretty hard to debug, but as far as I can see we have the following:</p>
<ul>
<li>The SM has the function <code>getState</code> function (<a href="https://github.com/input-output-hk/plutus-apps/blob/255879bb998fe1329abab3e07698f834e6cbbba6/plutus-contract/src/Plutus/Contract/StateMachine.hs#L131" rel="nofollow noreferrer">here</a>)</li>
<li>This calls <code>typeScriptTxOut</code> (<a href="https://github.com/input-output-hk/plutus-apps/blob/46e831e49287c3a04f83497b9dd9e70718092cde/plutus-ledger/src/Ledger/Typed/Tx.hs#L271" rel="nofollow noreferrer">here</a>) which expects a value <code>Right Datum</code> or else errors.</li>
<li>The <code>ChainIndexTxOut</code> (<a href="https://github.com/input-output-hk/plutus-apps/blob/63deb7d7ebd1c8c8ddf2640e1873d8f0f49559a1/plutus-ledger/src/Ledger/Tx.hs#L91" rel="nofollow noreferrer">Here</a>) has the value type <code>Either DatumHash Datum</code></li>
<li>the PAB ask the relies on the chain-index for this data.</li>
<li>The chain-index seems to return only <code>Left DatumHash</code> (<a href="https://github.com/input-output-hk/plutus-apps/blob/9ab49d8ba0f3ad95bc7fdf089d5bd98879cfc845/plutus-chain-index-core/src/Plutus/ChainIndex/Handlers.hs#L148" rel="nofollow noreferrer">here</a>)</li>
</ul>
<p>The chain index does know the datum from the hash.
However this requires an additional query to the datum table or equivalent join.</p>
<p>Has anyone else had this problem / solved this problem/ able to explain how I've got it wrong, and there's no problem here?</p>
<p>(plutus-apps <code>tag:7ed28bd67d7653b3ffe08b45848134380a738148</code>)</p>

CC BY-SA 4.0
2022-05-06T11:24:15.250Z
Maybe a bit obvious but what constraints did you use in the endpoints? Did you use `MustHashDatum` or `MustIncludeDatum` in the construction of the transactions? The former only adds the hash of the datum to the output (and thus results in a `Left DatumHash`. The latter actually adds the datum to the transaction which creates the utxo (and thus results in a `Right Datum`).
Fermat
CC BY-SA 4.0
2022-05-06T15:26:26.253Z
Don't think I understand the comment. Constraints for SM datums are handled by the SM framework (https://github.com/input-output-hk/plutus-apps/blob/fdb0eec477fc0f99f5edfdbc11c041dbd35ca6bb/plutus-contract/src/Plutus/Contract/StateMachine.hs#L538).
 
The problem above has now been fixed. (Checkout the issue linked below and the referenced PR)
waalge
<plutus><plutus-pioneer-program><statemachines>
StateMachines on Testnet : How to step a client?
# StateMachines on Testnet : How to step a client?

(Asked by: username_0 on 2022-04-05)

I want to run a statemachine on testnet.

The SM runs in the emulator, and I can initialize on testnet,
but if I try to make a new client it throws an error.
The error comes from transition failure, but this is due to the PAB not being able to re-establish state.

I find this pretty hard to debug, but as far as I can see we have the following:

* The SM has the function `getState` function ([here](https://github.com/input-output-hk/plutus-apps/blob/255879bb998fe1329abab3e07698f834e6cbbba6/plutus-contract/src/Plutus/Contract/StateMachine.hs#L131))
* This calls `typeScriptTxOut` ([here](https://github.com/input-output-hk/plutus-apps/blob/46e831e49287c3a04f83497b9dd9e70718092cde/plutus-ledger/src/Ledger/Typed/Tx.hs#L271)) which expects a value `Right Datum` or else errors.
* The `ChainIndexTxOut` ([Here](https://github.com/input-output-hk/plutus-apps/blob/63deb7d7ebd1c8c8ddf2640e1873d8f0f49559a1/plutus-ledger/src/Ledger/Tx.hs#L91)) has the value type `Either DatumHash Datum`
* the PAB ask the relies on the chain-index for this data.
* The chain-index seems to return only `Left DatumHash` ([here](https://github.com/input-output-hk/plutus-apps/blob/9ab49d8ba0f3ad95bc7fdf089d5bd98879cfc845/plutus-chain-index-core/src/Plutus/ChainIndex/Handlers.hs#L148))

The chain index does know the datum from the hash.
However this requires an additional query to the datum table or equivalent join.

Has anyone else had this problem / solved this problem/ able to explain how I've got it wrong, and there's no problem here?

(plutus-apps `tag:7ed28bd67d7653b3ffe08b45848134380a738148`)

**Tags:** plutus, plutus-pioneer-program, statemachines
> **Comments:**
> **username_1**: Maybe a bit obvious but what constraints did you use in the endpoints? Did you use `MustHashDatum` or `MustIncludeDatum` in the construction of the transactions? The former only adds the hash of the datum to the output (and thus results in a `Left DatumHash`. The latter actually adds the datum to the transaction which creates the utxo (and thus results in a `Right Datum`).  
> **username_0**: Don't think I understand the comment. Constraints for SM datums are handled by the SM framework (https://github.com/input-output-hk/plutus-apps/blob/fdb0eec477fc0f99f5edfdbc11c041dbd35ca6bb/plutus-contract/src/Plutus/Contract/StateMachine.hs#L538).   The problem above has now been fixed. (Checkout the issue linked below and the referenced PR)  

---

## 2 answers

### Answer by username_0 on 2022-04-06. Score: 0

85% sure this is a bug introduced by changes to the chain index.
A work around is to mod the StateMachine.hs file
getDatum ::
    ( AsSMContractError e)
    => Tx.ChainIndexTxOut
    -> Contract w schema e Tx.ChainIndexTxOut
getDatum tx = mapError (review _SMContractError) $ do
    case Tx._ciTxOutDatum tx of
         Left dh -> do
            dM <- datumFromHash dh
            case dM of
                 Just d -> return (set Tx.ciTxOutDatum (Right d) tx)
                 Nothing -> return tx
         Right _ -> return tx



{-| Get the current on-chain state of the state machine instance.
    Return Nothing if there is no state on chain.
    Throws an @SMContractError@ if the number of outputs at the machine address is greater than one.
-}
getOnChainState ::
    ( AsSMContractError e
    , PlutusTx.FromData state
    , PlutusTx.ToData state
    )
    => StateMachineClient state i
    -> Contract w schema e (Maybe (OnChainState state i, Map TxOutRef Tx.ChainIndexTxOut))
getOnChainState StateMachineClient{scInstance, scChooser} = mapError (review _SMContractError) $ do
    utxoTx_ <- utxosAt (SM.machineAddress scInstance)
    utxoTx <- sequence (Map.map getDatum utxoTx_) -- FETCH DATUMS
    let states = getStates scInstance utxoTx
    case states of
        [] -> pure Nothing
        _  -> case scChooser states of
                Left err    -> throwing _SMContractError err
                Right state -> pure $ Just (state, utxoTx)


Not pretty but I think it works.
Alternative is to mod the chain index code emulating the old behavior.
https://github.com/input-output-hk/plutus-apps/issues/398


---

### Answer by username_2 on 2022-05-07. Score: 0

My first sugestion would be to avoid using the out-of-the box statemachine from the plutus library at the moment. In it's current form it is highly inneficient and you will quickly run into maxTx size limits on testnet. If you want to run a state machine, it is best to build your own so it can be as efficient as possible. Also, chain index and pab is a nightmare when trying to test on testnet, so it is probably best to use a service like blockfrost.

I have just release The Open Source Cardano Lottery project, which has source code that implements a state machine as well as how to test on testnet (and devnent). It is a beta non-prod release, so there is room for improvements, but it is fully functional on the testnet.

You can find the source code here:

<https://github.com/lley154/cardano-lottery>


---

