Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CBR-305] Rebase, fix tests, address PR feedback
Browse files Browse the repository at this point in the history
Also placate hlint, stylish-haskell, stack2nix
  • Loading branch information
edsko committed Aug 23, 2018
1 parent 7dcfea3 commit 7928afc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 46 deletions.
8 changes: 8 additions & 0 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17778,6 +17778,7 @@ license = stdenv.lib.licenses.mit;
, cardano-sl-binary
, cardano-sl-binary-test
, cardano-sl-chain
, cardano-sl-chain-test
, cardano-sl-client
, cardano-sl-core
, cardano-sl-core-test
Expand All @@ -17791,6 +17792,7 @@ license = stdenv.lib.licenses.mit;
, cardano-sl-wallet
, cardano-sl-wallet-test
, cassava
, cereal
, conduit
, connection
, constraints
Expand All @@ -17799,6 +17801,7 @@ license = stdenv.lib.licenses.mit;
, data-default
, data-default-class
, directory
, ed25519
, exceptions
, filepath
, formatting
Expand Down Expand Up @@ -17901,13 +17904,15 @@ cardano-sl-node-ipc
cardano-sl-util
cardano-sl-wallet
cardano-sl-wallet-test
cereal
conduit
connection
containers
cryptonite
data-default
data-default-class
directory
ed25519
exceptions
formatting
generics-sop
Expand Down Expand Up @@ -18004,6 +18009,7 @@ cardano-sl
cardano-sl-binary
cardano-sl-binary-test
cardano-sl-chain
cardano-sl-chain-test
cardano-sl-client
cardano-sl-core
cardano-sl-core-test
Expand All @@ -18012,6 +18018,7 @@ cardano-sl-db
cardano-sl-util
cardano-sl-util-test
cardano-sl-wallet
cereal
conduit
constraints
containers
Expand All @@ -18032,6 +18039,7 @@ quickcheck-instances
random
reflection
safe-exceptions
safecopy
serokell-util
servant
servant-server
Expand Down
9 changes: 0 additions & 9 deletions wallet-new/src/Cardano/Wallet/Kernel/ChainState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Cardano.Wallet.Kernel.ChainState (
import Universum

import qualified Data.Map.Strict as Map
import Data.SafeCopy (SafeCopy (..))

import Pos.Chain.Block (HeaderHash, gbHeader, headerHash,
mainBlockSlot, prevBlockL)
Expand Down Expand Up @@ -249,14 +248,6 @@ data ChainStateException =

instance Exception ChainStateException

{-------------------------------------------------------------------------------
Serialization
-------------------------------------------------------------------------------}

instance SafeCopy ChainBrief where
getCopy = error "TODO: getCopy for ChainBrief"
putCopy = error "TODO: putCopy for ChainBrief"

{-------------------------------------------------------------------------------
Pretty printing
-------------------------------------------------------------------------------}
Expand Down
2 changes: 1 addition & 1 deletion wallet-new/src/Cardano/Wallet/Kernel/DB/AcidState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import Cardano.Wallet.Kernel.DB.HdWallet
import qualified Cardano.Wallet.Kernel.DB.HdWallet.Create as HD
import qualified Cardano.Wallet.Kernel.DB.HdWallet.Delete as HD
import qualified Cardano.Wallet.Kernel.DB.HdWallet.Update as HD
import Cardano.Wallet.Kernel.DB.InDb (InDb(InDb))
import Cardano.Wallet.Kernel.DB.InDb (InDb (InDb))
import Cardano.Wallet.Kernel.DB.Spec
import Cardano.Wallet.Kernel.DB.Spec.Pending (Pending)
import qualified Cardano.Wallet.Kernel.DB.Spec.Update as Spec
Expand Down
22 changes: 6 additions & 16 deletions wallet-new/src/Cardano/Wallet/Kernel/DB/BlockMeta.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import Universum
import Control.Lens (at, non)
import Control.Lens.TH (makeLenses, makeWrapped)
import qualified Data.Map.Strict as Map
import Data.SafeCopy (SafeCopy (..), base, contain, deriveSafeCopy,
safeGet, safePut)
import Data.SafeCopy (base, deriveSafeCopy)
import Formatting (bprint, build, (%))
import qualified Formatting.Buildable
import Serokell.Util (mapJson)
Expand Down Expand Up @@ -63,15 +62,6 @@ instance Monoid AddressMeta where
makeLenses ''AddressMeta
deriveSafeCopy 1 'base ''AddressMeta

instance SafeCopy (InDb AddressMeta) where
getCopy = contain $ do
isUsed <- safeGet
isChange <- safeGet
pure . InDb $ AddressMeta isUsed isChange
putCopy (InDb (AddressMeta isUsed isChange)) = contain $ do
safePut isUsed
safePut isChange

{-------------------------------------------------------------------------------
Block metadata
-------------------------------------------------------------------------------}
Expand All @@ -81,7 +71,7 @@ data BlockMeta = BlockMeta {
-- | Slot each transaction got confirmed in
_blockMetaSlotId :: !(InDb (Map Txp.TxId Core.SlotId))
-- | Address metadata
, _blockMetaAddressMeta :: !(InDb (Map Core.Address AddressMeta))
, _blockMetaAddressMeta :: !(Map (InDb Core.Address) AddressMeta)
} deriving Eq

makeLenses ''BlockMeta
Expand All @@ -92,12 +82,12 @@ deriveSafeCopy 1 'base ''BlockMeta
-- When the block metadata does not contain any information about this address,
-- we assume 'mempty'.
addressMeta :: Core.Address -> Lens' BlockMeta AddressMeta
addressMeta addr = blockMetaAddressMeta . fromDb . at addr . non mempty
addressMeta addr = blockMetaAddressMeta . at (InDb addr) . non mempty

emptyBlockMeta :: BlockMeta
emptyBlockMeta = BlockMeta {
_blockMetaSlotId = InDb Map.empty
, _blockMetaAddressMeta = InDb Map.empty
, _blockMetaAddressMeta = Map.empty
}

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -125,7 +115,7 @@ appendBlockMeta :: BlockMeta -> LocalBlockMeta -> BlockMeta
appendBlockMeta cur (LocalBlockMeta new) = BlockMeta {
_blockMetaSlotId = combineUsing (liftA2 Map.union)
_blockMetaSlotId
, _blockMetaAddressMeta = combineUsing (liftA2 (Map.unionWith (<>)))
, _blockMetaAddressMeta = combineUsing (Map.unionWith (<>))
_blockMetaAddressMeta
}
where
Expand Down Expand Up @@ -171,4 +161,4 @@ instance Buildable BlockMeta where
% "}"
)
(_fromDb _blockMetaSlotId)
(_fromDb _blockMetaAddressMeta)
_blockMetaAddressMeta
48 changes: 39 additions & 9 deletions wallet-new/src/Cardano/Wallet/Kernel/DB/InDb.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.Wallet.Kernel.DB.InDb
( InDb(..)
Expand All @@ -24,7 +25,6 @@ import qualified Data.Vector as V
import Test.QuickCheck (Arbitrary (..))

import qualified Pos.Chain.Block as Core
import qualified Pos.Chain.Txp as Core
import qualified Pos.Core as Core
import qualified Pos.Core.Attributes as Core
import qualified Pos.Core.Delegation as Core
Expand All @@ -35,13 +35,37 @@ import qualified Pos.Crypto as Core

import qualified Cardano.Crypto.Wallet as CCW

{-# ANN module ("HLint: ignore Reduce duplication" :: Text) #-}

{-------------------------------------------------------------------------------
Wrap core types so that we can make independent serialization decisions
-------------------------------------------------------------------------------}

-- | Wrapped type (with potentially different 'SC' instance)
-- | Wrapped type (with potentially different 'SafeCopy' instance)
--
-- NOTE:
--
-- 1. We want to be independent from the 'SafeCopy' instances in core. For this
-- reason, we wrap all core types in 'InDb', and provide explicit 'SafeCopy'
-- instances for 'InDb SomeCoreType'.
-- 2. We never use 'InDb' for types that we have control over in the wallet
-- itself.
-- 3. To avoid too much code bloat everywhere else, we don't nest 'InDb';
-- i.e., we don't use @InDb (.... InDb ....)@. Instead, we use 'InDb' only
-- /around/ (possibly nested) core types; for example, we use
-- @InDb (Map SomeCoreType SomeOtherCoreType)@. We then translate this to
-- @Map (InDb SomeCoreType) (InDb SomeOtherCoreType)@ in the 'SafeCopy'
-- instances themselves, so that in the rest of the code we don't have to
-- do too much wrapping and unwrapping.
--
-- A consequence of these rules is that something like
--
-- > safePut (InDb x) = safePut x
--
-- is correct /only/ if @x@ has a primitive type (i.e., not one defined in
-- the Cardano core, but in the Haskell base libraries).
newtype InDb a = InDb { _fromDb :: a }
deriving (Eq, Show, Ord)
deriving (Eq, Show, Ord, Buildable)

instance Functor InDb where
fmap f = InDb . f . _fromDb
Expand Down Expand Up @@ -80,7 +104,7 @@ instance SC.SafeCopy (InDb Core.AddrAttributes) where
pure (InDb (Core.AddrAttributes (fmap _fromDb yiap) ast))
putCopy (InDb (Core.AddrAttributes yap asr)) = SC.contain $ do
SC.safePut (fmap InDb yap)
SC.safePut asr
SC.safePut (InDb asr)

instance SC.SafeCopy (InDb Core.AddrStakeDistribution) where
getCopy = SC.contain $ fmap InDb $ do
Expand Down Expand Up @@ -196,7 +220,9 @@ instance SC.SafeCopy (InDb Txp.TxInWitness) where
2 -> Txp.RedeemWitness
<$> fmap _fromDb SC.safeGet
<*> fmap _fromDb SC.safeGet
3 -> Txp.RedeemWitness <$> SC.safeGet <*> SC.safeGet
3 -> Txp.UnknownWitnessType
<$> SC.safeGet
<*> SC.safeGet
(n :: Word8) -> fail
$ "Expected 0,1,2,3 for tag of TxInWitness, got: "
<> show n
Expand Down Expand Up @@ -341,10 +367,14 @@ instance (SC.SafeCopy (InDb a), SC.SafeCopy (InDb b))
SC.safePut (InDb b)

instance SC.SafeCopy (InDb Txp.TxIn) where
getCopy = SC.contain $ fmap InDb $ do
getCopy = SC.contain $
SC.safeGet >>= \case
0 -> Txp.TxInUtxo <$> SC.safeGet <*> SC.safeGet
1 -> Txp.TxInUnknown <$> SC.safeGet <*> SC.safeGet
0 -> do InDb txId <- SC.safeGet
w <- SC.safeGet
pure (InDb (Txp.TxInUtxo txId w))
1 -> do w <- SC.safeGet
b <- SC.safeGet
pure (InDb (Txp.TxInUnknown w b))
(n :: Word8) -> fail
$ "Expected one of 0,1 for TxIn tag, got: "
<> show n
Expand Down
5 changes: 1 addition & 4 deletions wallet-new/src/Cardano/Wallet/Kernel/DB/Util/IxSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ data Indexed a = Indexed {
}

Lens.makeLenses ''Indexed

instance SafeCopy a => SafeCopy (Indexed a) where
getCopy = error "TODO"
putCopy = error "TODO"
deriveSafeCopy 1 'base ''Indexed

instance Buildable a => Buildable (Indexed a) where
build (Indexed (AutoIncrementKey idx) r) =
Expand Down
6 changes: 3 additions & 3 deletions wallet-new/src/Cardano/Wallet/Kernel/PrefilterTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ mkBlockMeta slotId addrs_ = LocalBlockMeta BlockMeta{..}
indexedAddrs = indexByAddr addrs_

_blockMetaSlotId = InDb . Map.fromList . map (,slotId) $ txIds'
_blockMetaAddressMeta = InDb $ Map.map mkAddressMeta indexedAddrs
_blockMetaAddressMeta = Map.map mkAddressMeta indexedAddrs

-- | This function is called once for each address found in a particular block of
-- transactions. The collection of address summaries passed to this function
Expand Down Expand Up @@ -347,11 +347,11 @@ mkAddressMeta addrs
-- | Index the list of address summaries by Address.
-- NOTE: Since there will be at least one AddressSummary per Address,
-- we can safely use NE.fromList.
indexByAddr :: [AddressSummary] -> Map Address (NE.NonEmpty AddressSummary)
indexByAddr :: [AddressSummary] -> Map (InDb Address) (NE.NonEmpty AddressSummary)
indexByAddr addrs =
Map.map NE.fromList (Map.fromListWith (++) addrs')
where
fromAddrSummary addrSummary = (addrSummaryAddr addrSummary, [addrSummary])
fromAddrSummary addrSummary = (InDb (addrSummaryAddr addrSummary), [addrSummary])
addrs' = map fromAddrSummary addrs

fromUtxoSummary :: Map TxIn (TxOutAux,AddressSummary)
Expand Down
8 changes: 4 additions & 4 deletions wallet-new/test/unit/UTxO/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,12 @@ instance DSL.Hash h Addr => Interpret h (BlockMeta' h) where
_blockMetaAddressMeta <- intAddrMetas addrMeta'
return $ BlockMeta {..}
where
intAddrMetas :: Map Addr AddressMeta -> IntT h e m (InDb (Map Address AddressMeta))
intAddrMetas addrMetas= InDb . Map.fromList <$> mapM intAddrMeta (Map.toList addrMetas)
intAddrMetas :: Map Addr AddressMeta -> IntT h e m (Map (InDb Address) AddressMeta)
intAddrMetas addrMetas= Map.fromList <$> mapM intAddrMeta (Map.toList addrMetas)

-- Interpret only the key, leaving the indexed value AddressMeta unchanged
intAddrMeta :: (Addr,AddressMeta) -> IntT h e m (Address,AddressMeta)
intAddrMeta (addr,addrMeta) = (,addrMeta) . addrInfoCardano <$> int addr
intAddrMeta :: (Addr,AddressMeta) -> IntT h e m (InDb Address, AddressMeta)
intAddrMeta (addr,addrMeta) = (,addrMeta) . InDb . addrInfoCardano <$> int addr

intTxIds :: Map (h (DSL.Transaction h Addr)) SlotId -> IntT h e m (InDb (Map TxId SlotId))
intTxIds txIds = InDb . Map.fromList <$> mapM intTxId (Map.toList txIds)
Expand Down

0 comments on commit 7928afc

Please sign in to comment.