tweag / cooked-validators

MIT License
39 stars 11 forks source link

Small fixes after first audit with cooked V2 #354

Closed carlhammann closed 1 year ago

carlhammann commented 1 year ago

This PR incorporates a few changes we made to cooked during our first audit using V2. These are:

Export permanentValue from the Cooked.Currencies module

For some reason, we had forgotten it in the export list.

Add an UtxoSearch that takes a list of TxOutRefs

We now have

txOutByRefSearch :: MonadBlockChainBalancing m => [TxOutRef] -> UtxoSearch m TxOut

This function will try to resolve the TxOut at each of the given TxOutRefs, and returns an UtxoSearch of all those TxOuts that could be resolved.

Add a function to resolve typed datums

The function

resolveTypedDatum :: (IsAbstractOutput out, ..., FromBuiltinData a, MonadBlockChainBalancing m) => 
  out ->
  m (Maybe (ConcreteOutput (OwnerType our) a (ValueType out) (ReferenceScriptType out))

tries to resolve a datum of a specific type on any output type with an IsAbstractOutput instance. This can be used as a filter in UtxoSearches, for example.

Fix a bug in how we keep track of data in MockChainT

On successful submission of transactions, we were deleting the data on the transaction inputs from the mcstDatums. This meant that, if exactly the same datum sat around on another UTxO somewhere, MockChainT would not be able to resolve it, and consequently run into errors at the next time the datum was requested. (This happens at the latest while creating the transaction that tries to cosume the UTxO.)

The solution I choose is the simplest: Just never delete anything from the mcstDatums. This is obviously suboptimal, since it means that mcstDatumsalways grows during a trace. I don't think that's too bad, though, since it is a map (this means constant access time), and we're probably never never going to use so many datums that the size becomes a problem. The only cheap alternative I see at the moment would be scanning the whole mcstIndex every time we want to delete a datum, and that seems even less appealing.