IntersectMBO / plutus-apps

The Plutus application platform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wallet's utxos in Emulator's diskstate have incorrect indexes

catch-21 opened this issue · comments

commented

Summary

Accessing the wallet's utxos from the WalletState of the EmulatorTrace results in TxOutRefs with indexes in the range 0-9 for all wallets, this cannot be correct because each wallet must have unique utxos. The Contract sees unique TxOutRefs for each wallet, e.g. w1 uses index range 50-59 and w2 uses 20-29.

Steps to reproduce the behavior

There are currently workarounds for this bug in existing tests mustSpendPubKeyOutput plutus-context tests, such as this. Please remove workarounds once fixed.

There is a commented out test to recreate this bug, see here. Edit: this also now affects MustReferenceOutput tests

Actual Result

Getting TxOutRef from WalletState, w1TxoRefs will have indexes in range 0-9.

let trace :: Trace.EmulatorTrace () = do
        w1State <- Trace.agentState w1
        let w1TxoRefs = S.elems $ head $ M.elems $ w1State ^. chainIndexEmulatorState . diskState . addressMap . unCredentialMap

Logging in Contract will print indexes in range 50-59

logInfo @String $ "pubKeyUtxos:: " ++ show pubKeyUtxos

Expected Result

Wallets' utxos should be consistent.

Describe the approach you would take to fix this

No response

System info

a8540db

There is no bug, at least no reused TxOutIdx but we need to clarify the API: addressMap in diskState, contains a map the TxOutRef for all the Credentials, not only those of the given wallet. So we need to access the right entry of the map, and not take the head of its entries:

let trace :: Trace.EmulatorTrace () = do
        w1State <- Trace.agentState w1
        let cred = addressCredential $ Wallet.ownAddress w1State
             w1TxoRefs = w1State ^. chainIndexEmulatorState . diskState . addressMap . at cred . non mempty