Skip to content

Commit

Permalink
mock: rename HasTxs to HasMockTxs to avoid confusion with Mempool.HasTxs
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrisby committed Mar 16, 2020
1 parent b4d98e9 commit e3767e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ instance (SimpleCrypto c, Typeable ext) => HasHeader (SimpleBlock c ext) where
instance (SimpleCrypto c, Typeable ext) => StandardHash (SimpleBlock c ext)

{-------------------------------------------------------------------------------
HasTxs instance
HasMockTxs instance
-------------------------------------------------------------------------------}

instance Mock.HasTxs (SimpleBlock' c ext ext') where
getTxs = Mock.getTxs . simpleBody
instance Mock.HasMockTxs (SimpleBlock' c ext ext') where
getMockTxs = Mock.getMockTxs . simpleBody

instance Mock.HasTxs SimpleBody where
getTxs = simpleTxs
instance Mock.HasMockTxs SimpleBody where
getMockTxs = simpleTxs

{-------------------------------------------------------------------------------
Envelope validation
Expand Down Expand Up @@ -320,7 +320,7 @@ updateSimpleLedgerState :: (SimpleCrypto c, Typeable ext)
updateSimpleLedgerState b (SimpleLedgerState st) =
SimpleLedgerState <$> updateMockState b st

updateSimpleUTxO :: Mock.HasTxs a
updateSimpleUTxO :: Mock.HasMockTxs a
=> a
-> TickedLedgerState (SimpleBlock c ext)
-> Except (MockError (SimpleBlock c ext))
Expand Down Expand Up @@ -364,8 +364,8 @@ instance (Typeable p, Typeable c) => NoUnexpectedThunks (GenTx (SimpleBlock p c)
instance HasTxs (SimpleBlock c ext) where
extractTxs = map mkSimpleGenTx . simpleTxs . simpleBody

instance Mock.HasTxs (GenTx (SimpleBlock p c)) where
getTxs = Mock.getTxs . simpleGenTx
instance Mock.HasMockTxs (GenTx (SimpleBlock p c)) where
getMockTxs = Mock.getMockTxs . simpleGenTx

instance Condense (GenTx (SimpleBlock p c)) where
condense = condense . simpleGenTx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ deriving instance Serialise (HeaderHash blk) => Serialise (MockError blk)
updateMockState :: ( GetHeader blk
, HasHeader (Header blk)
, StandardHash blk
, HasTxs blk
, HasMockTxs blk
)
=> blk
-> MockState blk
Expand All @@ -82,12 +82,12 @@ updateMockTip hdr (MockState u c t)
| otherwise
= throwError $ MockInvalidHash (headerPrevHash hdr) (pointHash t)

updateMockUTxO :: HasTxs a
updateMockUTxO :: HasMockTxs a
=> SlotNo
-> a
-> MockState blk
-> Except (MockError blk) (MockState blk)
updateMockUTxO now = repeatedlyM (updateMockUTxO1 now) . getTxs
updateMockUTxO now = repeatedlyM (updateMockUTxO1 now) . getMockTxs

updateMockUTxO1 :: forall blk.
SlotNo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Ouroboros.Consensus.Mock.Ledger.UTxO (
, Utxo
, Expiry(..)
-- * Computing UTxO
, HasTxs(..)
, HasMockTxs(..)
, txIns
, txOuts
, confirmed
Expand Down Expand Up @@ -104,37 +104,39 @@ data UtxoError
instance Condense UtxoError where
condense = show

class HasTxs a where
getTxs :: a -> [Tx]
class HasMockTxs a where
-- | The transactions in the order they are to be applied
--
getMockTxs :: a -> [Tx]

instance HasTxs Tx where
getTxs = (:[])
instance HasMockTxs Tx where
getMockTxs = (:[])

instance HasTxs a => HasTxs [a] where
getTxs = concatMap getTxs
instance HasMockTxs a => HasMockTxs [a] where
getMockTxs = concatMap getMockTxs

instance HasTxs a => HasTxs (Chain a) where
getTxs = getTxs . toOldestFirst
instance HasMockTxs a => HasMockTxs (Chain a) where
getMockTxs = getMockTxs . toOldestFirst

txIns :: HasTxs a => a -> Set TxIn
txIns = Set.unions . map each . getTxs
txIns :: HasMockTxs a => a -> Set TxIn
txIns = Set.unions . map each . getMockTxs
where
each (Tx _expiry ins _outs) = ins

txOuts :: HasTxs a => a -> Utxo
txOuts = Map.unions . map each . getTxs
txOuts :: HasMockTxs a => a -> Utxo
txOuts = Map.unions . map each . getMockTxs
where
each tx@(Tx _expiry _ins outs) =
Map.fromList $ zipWith aux [0..] outs
where
aux :: Ix -> TxOut -> (TxIn, TxOut)
aux ix out = ((hash tx, ix), out)

confirmed :: HasTxs a => a -> Set TxId
confirmed = Set.fromList . map hash . getTxs
confirmed :: HasMockTxs a => a -> Set TxId
confirmed = Set.fromList . map hash . getMockTxs

updateUtxo :: HasTxs a => a -> Utxo -> Except UtxoError Utxo
updateUtxo = repeatedlyM each . getTxs
updateUtxo :: HasMockTxs a => a -> Utxo -> Except UtxoError Utxo
updateUtxo = repeatedlyM each . getMockTxs
where
each tx = execStateT $ do
-- Remove all inputs from the Utxo and calculate the sum of all the
Expand Down

0 comments on commit e3767e2

Please sign in to comment.