{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
module Haskoin.Store.Database.Writer (WriterT , runWriter) where

import           Control.Monad.Reader          (ReaderT (..))
import qualified Control.Monad.Reader          as R
import qualified Data.ByteString.Short         as B.Short
import           Data.HashMap.Strict           (HashMap)
import qualified Data.HashMap.Strict           as M
import           Data.List                     (sortOn)
import           Data.Ord                      (Down (..))
import           Data.Tuple                    (swap)
import           Database.RocksDB              (BatchOp, DB)
import           Database.RocksDB.Query        (deleteOp, deleteOpCF, insertOp,
                                                insertOpCF, writeBatch)
import           Haskoin                       (Address, BlockHash, BlockHeight,
                                                Network, OutPoint (..), TxHash,
                                                headerHash, txHash)
import           Haskoin.Store.Common
import           Haskoin.Store.Data
import           Haskoin.Store.Database.Reader
import           Haskoin.Store.Database.Types
import           UnliftIO                      (MonadIO, TVar, atomically,
                                                liftIO, modifyTVar, newTVarIO,
                                                readTVarIO)

data Writer = Writer { Writer -> DatabaseReader
getReader :: !DatabaseReader
                     , Writer -> TVar Memory
getState  :: !(TVar Memory) }

type WriterT = ReaderT Writer

instance MonadIO m => StoreReadBase (WriterT m) where
    getNetwork :: WriterT m Network
getNetwork = WriterT m Network
forall (m :: * -> *). MonadIO m => WriterT m Network
getNetworkI
    getBestBlock :: WriterT m (Maybe BlockHash)
getBestBlock = WriterT m (Maybe BlockHash)
forall (m :: * -> *). MonadIO m => WriterT m (Maybe BlockHash)
getBestBlockI
    getBlocksAtHeight :: BlockHeight -> WriterT m [BlockHash]
getBlocksAtHeight = BlockHeight -> WriterT m [BlockHash]
forall (m :: * -> *).
MonadIO m =>
BlockHeight -> WriterT m [BlockHash]
getBlocksAtHeightI
    getBlock :: BlockHash -> WriterT m (Maybe BlockData)
getBlock = BlockHash -> WriterT m (Maybe BlockData)
forall (m :: * -> *).
MonadIO m =>
BlockHash -> WriterT m (Maybe BlockData)
getBlockI
    getTxData :: TxHash -> WriterT m (Maybe TxData)
getTxData = TxHash -> WriterT m (Maybe TxData)
forall (m :: * -> *).
MonadIO m =>
TxHash -> WriterT m (Maybe TxData)
getTxDataI
    getSpender :: OutPoint -> WriterT m (Maybe Spender)
getSpender = OutPoint -> WriterT m (Maybe Spender)
forall (m :: * -> *).
MonadIO m =>
OutPoint -> WriterT m (Maybe Spender)
getSpenderI
    getUnspent :: OutPoint -> WriterT m (Maybe Unspent)
getUnspent = OutPoint -> WriterT m (Maybe Unspent)
forall (m :: * -> *).
MonadIO m =>
OutPoint -> WriterT m (Maybe Unspent)
getUnspentI
    getBalance :: Address -> WriterT m (Maybe Balance)
getBalance = Address -> WriterT m (Maybe Balance)
forall (m :: * -> *).
MonadIO m =>
Address -> WriterT m (Maybe Balance)
getBalanceI
    getMempool :: WriterT m [(UnixTime, TxHash)]
getMempool = WriterT m [(UnixTime, TxHash)]
forall (m :: * -> *). MonadIO m => WriterT m [(UnixTime, TxHash)]
getMempoolI

data Memory = Memory
    { Memory -> Maybe Network
hNet
      :: !(Maybe Network)
    , Memory -> Maybe (Maybe BlockHash)
hBest
      :: !(Maybe (Maybe BlockHash))
    , Memory -> HashMap BlockHash (Maybe BlockData)
hBlock
      :: !(HashMap BlockHash (Maybe BlockData))
    , Memory -> HashMap BlockHeight [BlockHash]
hHeight
      :: !(HashMap BlockHeight [BlockHash])
    , Memory -> HashMap TxHash (Maybe TxData)
hTx
      :: !(HashMap TxHash (Maybe TxData))
    , Memory -> HashMap OutPoint (Maybe Spender)
hSpender
      :: !(HashMap OutPoint (Maybe Spender))
    , Memory -> HashMap OutPoint (Maybe Unspent)
hUnspent
      :: !(HashMap OutPoint (Maybe Unspent))
    , Memory -> HashMap Address (Maybe Balance)
hBalance
      :: !(HashMap Address (Maybe Balance))
    , Memory -> HashMap (Address, TxRef) (Maybe ())
hAddrTx
      :: !(HashMap (Address, TxRef) (Maybe ()))
    , Memory -> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut
      :: !(HashMap (Address, BlockRef, OutPoint) (Maybe OutVal))
    , Memory -> HashMap TxHash UnixTime
hMempool
      :: !(HashMap TxHash UnixTime)
    } deriving (Memory -> Memory -> Bool
(Memory -> Memory -> Bool)
-> (Memory -> Memory -> Bool) -> Eq Memory
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Memory -> Memory -> Bool
$c/= :: Memory -> Memory -> Bool
== :: Memory -> Memory -> Bool
$c== :: Memory -> Memory -> Bool
Eq, Int -> Memory -> ShowS
[Memory] -> ShowS
Memory -> String
(Int -> Memory -> ShowS)
-> (Memory -> String) -> ([Memory] -> ShowS) -> Show Memory
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Memory] -> ShowS
$cshowList :: [Memory] -> ShowS
show :: Memory -> String
$cshow :: Memory -> String
showsPrec :: Int -> Memory -> ShowS
$cshowsPrec :: Int -> Memory -> ShowS
Show)

instance MonadIO m => StoreWrite (WriterT m) where
    setBest :: BlockHash -> WriterT m ()
setBest h :: BlockHash
h =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        BlockHash -> Memory -> Memory
setBestH BlockHash
h
    insertBlock :: BlockData -> WriterT m ()
insertBlock b :: BlockData
b =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        BlockData -> Memory -> Memory
insertBlockH BlockData
b
    setBlocksAtHeight :: [BlockHash] -> BlockHeight -> WriterT m ()
setBlocksAtHeight h :: [BlockHash]
h g :: BlockHeight
g =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        [BlockHash] -> BlockHeight -> Memory -> Memory
setBlocksAtHeightH [BlockHash]
h BlockHeight
g
    insertTx :: TxData -> WriterT m ()
insertTx t :: TxData
t =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        TxData -> Memory -> Memory
insertTxH TxData
t
    insertSpender :: OutPoint -> Spender -> WriterT m ()
insertSpender p :: OutPoint
p s' :: Spender
s' =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        OutPoint -> Spender -> Memory -> Memory
insertSpenderH OutPoint
p Spender
s'
    deleteSpender :: OutPoint -> WriterT m ()
deleteSpender p :: OutPoint
p =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        OutPoint -> Memory -> Memory
deleteSpenderH OutPoint
p
    insertAddrTx :: Address -> TxRef -> WriterT m ()
insertAddrTx a :: Address
a t :: TxRef
t =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Address -> TxRef -> Memory -> Memory
insertAddrTxH Address
a TxRef
t
    deleteAddrTx :: Address -> TxRef -> WriterT m ()
deleteAddrTx a :: Address
a t :: TxRef
t =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Address -> TxRef -> Memory -> Memory
deleteAddrTxH Address
a TxRef
t
    insertAddrUnspent :: Address -> Unspent -> WriterT m ()
insertAddrUnspent a :: Address
a u :: Unspent
u =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Address -> Unspent -> Memory -> Memory
insertAddrUnspentH Address
a Unspent
u
    deleteAddrUnspent :: Address -> Unspent -> WriterT m ()
deleteAddrUnspent a :: Address
a u :: Unspent
u =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Address -> Unspent -> Memory -> Memory
deleteAddrUnspentH Address
a Unspent
u
    addToMempool :: TxHash -> UnixTime -> WriterT m ()
addToMempool x :: TxHash
x t :: UnixTime
t =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
            TxHash -> UnixTime -> Memory -> Memory
addToMempoolH TxHash
x UnixTime
t
    deleteFromMempool :: TxHash -> WriterT m ()
deleteFromMempool x :: TxHash
x =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
            TxHash -> Memory -> Memory
deleteFromMempoolH TxHash
x
    setBalance :: Balance -> WriterT m ()
setBalance b :: Balance
b =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Balance -> Memory -> Memory
setBalanceH Balance
b
    insertUnspent :: Unspent -> WriterT m ()
insertUnspent h :: Unspent
h =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        Unspent -> Memory -> Memory
insertUnspentH Unspent
h
    deleteUnspent :: OutPoint -> WriterT m ()
deleteUnspent p :: OutPoint
p =
        (Writer -> m ()) -> WriterT m ()
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m ()) -> WriterT m ())
-> (Writer -> m ()) -> WriterT m ()
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
s } ->
        IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> ((Memory -> Memory) -> IO ()) -> (Memory -> Memory) -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM () -> IO ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> IO ())
-> ((Memory -> Memory) -> STM ()) -> (Memory -> Memory) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar Memory -> (Memory -> Memory) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Memory
s ((Memory -> Memory) -> m ()) -> (Memory -> Memory) -> m ()
forall a b. (a -> b) -> a -> b
$
        OutPoint -> Memory -> Memory
deleteUnspentH OutPoint
p

getLayered :: MonadIO m
           => (Memory -> Maybe a)
           -> DatabaseReaderT m a
           -> WriterT m a
getLayered :: (Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered f :: Memory -> Maybe a
f g :: DatabaseReaderT m a
g =
    (Writer -> m a) -> WriterT m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m a) -> WriterT m a) -> (Writer -> m a) -> WriterT m a
forall a b. (a -> b) -> a -> b
$ \Writer { getReader :: Writer -> DatabaseReader
getReader = DatabaseReader
db, getState :: Writer -> TVar Memory
getState = TVar Memory
tmem } ->
        Memory -> Maybe a
f (Memory -> Maybe a) -> m Memory -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar Memory -> m Memory
forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar Memory
tmem m (Maybe a) -> (Maybe a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Just x :: a
x -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
            Nothing -> DatabaseReaderT m a -> DatabaseReader -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT DatabaseReaderT m a
g DatabaseReader
db

runWriter :: MonadIO m
          => DatabaseReader
          -> WriterT m a
          -> m a
runWriter :: DatabaseReader -> WriterT m a -> m a
runWriter bdb :: DatabaseReader
bdb@DatabaseReader{ databaseHandle :: DatabaseReader -> DB
databaseHandle = DB
db } f :: WriterT m a
f = do
    [(UnixTime, TxHash)]
mem <- ReaderT DatabaseReader m [(UnixTime, TxHash)]
-> DatabaseReader -> m [(UnixTime, TxHash)]
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT DatabaseReader m [(UnixTime, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(UnixTime, TxHash)]
getMempool DatabaseReader
bdb
    TVar Memory
hm <- Memory -> m (TVar Memory)
forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO ([(UnixTime, TxHash)] -> Memory
newMemory [(UnixTime, TxHash)]
mem)
    a
x <- WriterT m a -> Writer -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
R.runReaderT WriterT m a
f $WWriter :: DatabaseReader -> TVar Memory -> Writer
Writer { getReader :: DatabaseReader
getReader = DatabaseReader
bdb, getState :: TVar Memory
getState = TVar Memory
hm }
    [BatchOp]
ops <- DB -> Memory -> [BatchOp]
hashMapOps DB
db (Memory -> [BatchOp]) -> m Memory -> m [BatchOp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar Memory -> m Memory
forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar Memory
hm
    DB -> [BatchOp] -> m ()
forall (m :: * -> *). MonadIO m => DB -> [BatchOp] -> m ()
writeBatch DB
db [BatchOp]
ops
    a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x

hashMapOps :: DB -> Memory -> [BatchOp]
hashMapOps :: DB -> Memory -> [BatchOp]
hashMapOps db :: DB
db mem :: Memory
mem =
    Maybe (Maybe BlockHash) -> [BatchOp]
bestBlockOp (Memory -> Maybe (Maybe BlockHash)
hBest Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap BlockHash (Maybe BlockData) -> [BatchOp]
blockHashOps DB
db (Memory -> HashMap BlockHash (Maybe BlockData)
hBlock Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap BlockHeight [BlockHash] -> [BatchOp]
blockHeightOps DB
db (Memory -> HashMap BlockHeight [BlockHash]
hHeight Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap TxHash (Maybe TxData) -> [BatchOp]
txOps DB
db (Memory -> HashMap TxHash (Maybe TxData)
hTx Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap OutPoint (Maybe Spender) -> [BatchOp]
spenderOps DB
db (Memory -> HashMap OutPoint (Maybe Spender)
hSpender Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap Address (Maybe Balance) -> [BatchOp]
balOps DB
db (Memory -> HashMap Address (Maybe Balance)
hBalance Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap (Address, TxRef) (Maybe ()) -> [BatchOp]
addrTxOps DB
db (Memory -> HashMap (Address, TxRef) (Maybe ())
hAddrTx Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> [BatchOp]
addrOutOps DB
db (Memory -> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    HashMap TxHash UnixTime -> [BatchOp]
mempoolOp (Memory -> HashMap TxHash UnixTime
hMempool Memory
mem) [BatchOp] -> [BatchOp] -> [BatchOp]
forall a. Semigroup a => a -> a -> a
<>
    DB -> HashMap OutPoint (Maybe Unspent) -> [BatchOp]
unspentOps DB
db (Memory -> HashMap OutPoint (Maybe Unspent)
hUnspent Memory
mem)

bestBlockOp :: Maybe (Maybe BlockHash) -> [BatchOp]
bestBlockOp :: Maybe (Maybe BlockHash) -> [BatchOp]
bestBlockOp Nothing         = []
bestBlockOp (Just Nothing)  = [BestKey -> BatchOp
forall key. (Key key, Serialize key) => key -> BatchOp
deleteOp BestKey
BestKey]
bestBlockOp (Just (Just b :: BlockHash
b)) = [BestKey -> BlockHash -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
key -> value -> BatchOp
insertOp BestKey
BestKey BlockHash
b]

blockHashOps :: DB -> HashMap BlockHash (Maybe BlockData) -> [BatchOp]
blockHashOps :: DB -> HashMap BlockHash (Maybe BlockData) -> [BatchOp]
blockHashOps db :: DB
db = ((BlockHash, Maybe BlockData) -> BatchOp)
-> [(BlockHash, Maybe BlockData)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((BlockHash -> Maybe BlockData -> BatchOp)
-> (BlockHash, Maybe BlockData) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry BlockHash -> Maybe BlockData -> BatchOp
forall value.
(KeyValue BlockKey value, Serialize value) =>
BlockHash -> Maybe value -> BatchOp
f) ([(BlockHash, Maybe BlockData)] -> [BatchOp])
-> (HashMap BlockHash (Maybe BlockData)
    -> [(BlockHash, Maybe BlockData)])
-> HashMap BlockHash (Maybe BlockData)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap BlockHash (Maybe BlockData)
-> [(BlockHash, Maybe BlockData)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: BlockHash -> Maybe value -> BatchOp
f k :: BlockHash
k (Just d :: value
d) = ColumnFamily -> BlockKey -> value -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
blockCF DB
db) (BlockHash -> BlockKey
BlockKey BlockHash
k) value
d
    f k :: BlockHash
k Nothing  = ColumnFamily -> BlockKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
blockCF DB
db) (BlockHash -> BlockKey
BlockKey BlockHash
k)

blockHeightOps :: DB -> HashMap BlockHeight [BlockHash] -> [BatchOp]
blockHeightOps :: DB -> HashMap BlockHeight [BlockHash] -> [BatchOp]
blockHeightOps db :: DB
db = ((BlockHeight, [BlockHash]) -> BatchOp)
-> [(BlockHeight, [BlockHash])] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((BlockHeight -> [BlockHash] -> BatchOp)
-> (BlockHeight, [BlockHash]) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry BlockHeight -> [BlockHash] -> BatchOp
f) ([(BlockHeight, [BlockHash])] -> [BatchOp])
-> (HashMap BlockHeight [BlockHash]
    -> [(BlockHeight, [BlockHash])])
-> HashMap BlockHeight [BlockHash]
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap BlockHeight [BlockHash] -> [(BlockHeight, [BlockHash])]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: BlockHeight -> [BlockHash] -> BatchOp
f = ColumnFamily -> HeightKey -> [BlockHash] -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
heightCF DB
db) (HeightKey -> [BlockHash] -> BatchOp)
-> (BlockHeight -> HeightKey)
-> BlockHeight
-> [BlockHash]
-> BatchOp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHeight -> HeightKey
HeightKey

txOps :: DB -> HashMap TxHash (Maybe TxData) -> [BatchOp]
txOps :: DB -> HashMap TxHash (Maybe TxData) -> [BatchOp]
txOps db :: DB
db = ((TxHash, Maybe TxData) -> BatchOp)
-> [(TxHash, Maybe TxData)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((TxHash -> Maybe TxData -> BatchOp)
-> (TxHash, Maybe TxData) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TxHash -> Maybe TxData -> BatchOp
forall value.
(KeyValue TxKey value, Serialize value) =>
TxHash -> Maybe value -> BatchOp
f) ([(TxHash, Maybe TxData)] -> [BatchOp])
-> (HashMap TxHash (Maybe TxData) -> [(TxHash, Maybe TxData)])
-> HashMap TxHash (Maybe TxData)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap TxHash (Maybe TxData) -> [(TxHash, Maybe TxData)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: TxHash -> Maybe value -> BatchOp
f k :: TxHash
k (Just t :: value
t) = ColumnFamily -> TxKey -> value -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
txCF DB
db) (TxHash -> TxKey
TxKey TxHash
k) value
t
    f k :: TxHash
k Nothing  = ColumnFamily -> TxKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
txCF DB
db) (TxHash -> TxKey
TxKey TxHash
k)

spenderOps :: DB -> HashMap OutPoint (Maybe Spender) -> [BatchOp]
spenderOps :: DB -> HashMap OutPoint (Maybe Spender) -> [BatchOp]
spenderOps db :: DB
db = ((OutPoint, Maybe Spender) -> BatchOp)
-> [(OutPoint, Maybe Spender)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((OutPoint -> Maybe Spender -> BatchOp)
-> (OutPoint, Maybe Spender) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry OutPoint -> Maybe Spender -> BatchOp
forall value.
(KeyValue SpenderKey value, Serialize value) =>
OutPoint -> Maybe value -> BatchOp
f) ([(OutPoint, Maybe Spender)] -> [BatchOp])
-> (HashMap OutPoint (Maybe Spender)
    -> [(OutPoint, Maybe Spender)])
-> HashMap OutPoint (Maybe Spender)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap OutPoint (Maybe Spender) -> [(OutPoint, Maybe Spender)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: OutPoint -> Maybe value -> BatchOp
f o :: OutPoint
o (Just s :: value
s) = ColumnFamily -> SpenderKey -> value -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
spenderCF DB
db) (OutPoint -> SpenderKey
SpenderKey OutPoint
o) value
s
    f o :: OutPoint
o Nothing  = ColumnFamily -> SpenderKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
spenderCF DB
db) (OutPoint -> SpenderKey
SpenderKey OutPoint
o)

balOps :: DB -> HashMap Address (Maybe Balance) -> [BatchOp]
balOps :: DB -> HashMap Address (Maybe Balance) -> [BatchOp]
balOps db :: DB
db = ((Address, Maybe Balance) -> BatchOp)
-> [(Address, Maybe Balance)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((Address -> Maybe Balance -> BatchOp)
-> (Address, Maybe Balance) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Address -> Maybe Balance -> BatchOp
f) ([(Address, Maybe Balance)] -> [BatchOp])
-> (HashMap Address (Maybe Balance) -> [(Address, Maybe Balance)])
-> HashMap Address (Maybe Balance)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Address (Maybe Balance) -> [(Address, Maybe Balance)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: Address -> Maybe Balance -> BatchOp
f a :: Address
a (Just b :: Balance
b) = ColumnFamily -> BalKey -> BalVal -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
balanceCF DB
db) (Address -> BalKey
BalKey Address
a) (Balance -> BalVal
balanceToVal Balance
b)
    f a :: Address
a Nothing  = ColumnFamily -> BalKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
balanceCF DB
db) (Address -> BalKey
BalKey Address
a)

addrTxOps :: DB -> HashMap (Address, TxRef) (Maybe ()) -> [BatchOp]
addrTxOps :: DB -> HashMap (Address, TxRef) (Maybe ()) -> [BatchOp]
addrTxOps db :: DB
db = (((Address, TxRef), Maybe ()) -> BatchOp)
-> [((Address, TxRef), Maybe ())] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map (((Address, TxRef) -> Maybe () -> BatchOp)
-> ((Address, TxRef), Maybe ()) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Address, TxRef) -> Maybe () -> BatchOp
f) ([((Address, TxRef), Maybe ())] -> [BatchOp])
-> (HashMap (Address, TxRef) (Maybe ())
    -> [((Address, TxRef), Maybe ())])
-> HashMap (Address, TxRef) (Maybe ())
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap (Address, TxRef) (Maybe ())
-> [((Address, TxRef), Maybe ())]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: (Address, TxRef) -> Maybe () -> BatchOp
f (a :: Address
a, t :: TxRef
t) (Just ()) = ColumnFamily -> AddrTxKey -> () -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
addrTxCF DB
db) (Address -> TxRef -> AddrTxKey
AddrTxKey Address
a TxRef
t) ()
    f (a :: Address
a, t :: TxRef
t) Nothing   = ColumnFamily -> AddrTxKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
addrTxCF DB
db) (Address -> TxRef -> AddrTxKey
AddrTxKey Address
a TxRef
t)

addrOutOps :: DB
           -> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
           -> [BatchOp]
addrOutOps :: DB
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> [BatchOp]
addrOutOps db :: DB
db = (((Address, BlockRef, OutPoint), Maybe OutVal) -> BatchOp)
-> [((Address, BlockRef, OutPoint), Maybe OutVal)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map (((Address, BlockRef, OutPoint) -> Maybe OutVal -> BatchOp)
-> ((Address, BlockRef, OutPoint), Maybe OutVal) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Address, BlockRef, OutPoint) -> Maybe OutVal -> BatchOp
forall value.
(KeyValue AddrOutKey value, Serialize value) =>
(Address, BlockRef, OutPoint) -> Maybe value -> BatchOp
f) ([((Address, BlockRef, OutPoint), Maybe OutVal)] -> [BatchOp])
-> (HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
    -> [((Address, BlockRef, OutPoint), Maybe OutVal)])
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> [((Address, BlockRef, OutPoint), Maybe OutVal)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: (Address, BlockRef, OutPoint) -> Maybe value -> BatchOp
f (a :: Address
a, b :: BlockRef
b, p :: OutPoint
p) (Just l :: value
l) =
        ColumnFamily -> AddrOutKey -> value -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF
        (DB -> ColumnFamily
addrOutCF DB
db)
        ($WAddrOutKey :: Address -> BlockRef -> OutPoint -> AddrOutKey
AddrOutKey { addrOutKeyA :: Address
addrOutKeyA = Address
a
                    , addrOutKeyB :: BlockRef
addrOutKeyB = BlockRef
b
                    , addrOutKeyP :: OutPoint
addrOutKeyP = OutPoint
p
                    }
        )
        value
l
    f (a :: Address
a, b :: BlockRef
b, p :: OutPoint
p) Nothing =
        ColumnFamily -> AddrOutKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF
        (DB -> ColumnFamily
addrOutCF DB
db)
        $WAddrOutKey :: Address -> BlockRef -> OutPoint -> AddrOutKey
AddrOutKey { addrOutKeyA :: Address
addrOutKeyA = Address
a
                    , addrOutKeyB :: BlockRef
addrOutKeyB = BlockRef
b
                    , addrOutKeyP :: OutPoint
addrOutKeyP = OutPoint
p
                    }

mempoolOp :: HashMap TxHash UnixTime -> [BatchOp]
mempoolOp :: HashMap TxHash UnixTime -> [BatchOp]
mempoolOp = BatchOp -> [BatchOp]
forall (m :: * -> *) a. Monad m => a -> m a
return (BatchOp -> [BatchOp])
-> (HashMap TxHash UnixTime -> BatchOp)
-> HashMap TxHash UnixTime
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemKey -> [(UnixTime, TxHash)] -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
key -> value -> BatchOp
insertOp MemKey
MemKey ([(UnixTime, TxHash)] -> BatchOp)
-> (HashMap TxHash UnixTime -> [(UnixTime, TxHash)])
-> HashMap TxHash UnixTime
-> BatchOp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((UnixTime, TxHash) -> Down (UnixTime, TxHash))
-> [(UnixTime, TxHash)] -> [(UnixTime, TxHash)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (UnixTime, TxHash) -> Down (UnixTime, TxHash)
forall a. a -> Down a
Down ([(UnixTime, TxHash)] -> [(UnixTime, TxHash)])
-> (HashMap TxHash UnixTime -> [(UnixTime, TxHash)])
-> HashMap TxHash UnixTime
-> [(UnixTime, TxHash)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TxHash, UnixTime) -> (UnixTime, TxHash))
-> [(TxHash, UnixTime)] -> [(UnixTime, TxHash)]
forall a b. (a -> b) -> [a] -> [b]
map (TxHash, UnixTime) -> (UnixTime, TxHash)
forall a b. (a, b) -> (b, a)
swap ([(TxHash, UnixTime)] -> [(UnixTime, TxHash)])
-> (HashMap TxHash UnixTime -> [(TxHash, UnixTime)])
-> HashMap TxHash UnixTime
-> [(UnixTime, TxHash)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap TxHash UnixTime -> [(TxHash, UnixTime)]
forall k v. HashMap k v -> [(k, v)]
M.toList

unspentOps :: DB -> HashMap OutPoint (Maybe Unspent) -> [BatchOp]
unspentOps :: DB -> HashMap OutPoint (Maybe Unspent) -> [BatchOp]
unspentOps db :: DB
db = ((OutPoint, Maybe Unspent) -> BatchOp)
-> [(OutPoint, Maybe Unspent)] -> [BatchOp]
forall a b. (a -> b) -> [a] -> [b]
map ((OutPoint -> Maybe Unspent -> BatchOp)
-> (OutPoint, Maybe Unspent) -> BatchOp
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry OutPoint -> Maybe Unspent -> BatchOp
f) ([(OutPoint, Maybe Unspent)] -> [BatchOp])
-> (HashMap OutPoint (Maybe Unspent)
    -> [(OutPoint, Maybe Unspent)])
-> HashMap OutPoint (Maybe Unspent)
-> [BatchOp]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap OutPoint (Maybe Unspent) -> [(OutPoint, Maybe Unspent)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    f :: OutPoint -> Maybe Unspent -> BatchOp
f p :: OutPoint
p (Just u :: Unspent
u) =
        ColumnFamily -> UnspentKey -> UnspentVal -> BatchOp
forall key value.
(KeyValue key value, Serialize key, Serialize value) =>
ColumnFamily -> key -> value -> BatchOp
insertOpCF (DB -> ColumnFamily
unspentCF DB
db) (OutPoint -> UnspentKey
UnspentKey OutPoint
p) ((OutPoint, UnspentVal) -> UnspentVal
forall a b. (a, b) -> b
snd (Unspent -> (OutPoint, UnspentVal)
unspentToVal Unspent
u))
    f p :: OutPoint
p Nothing  =
        ColumnFamily -> UnspentKey -> BatchOp
forall key.
(Key key, Serialize key) =>
ColumnFamily -> key -> BatchOp
deleteOpCF (DB -> ColumnFamily
unspentCF DB
db) (OutPoint -> UnspentKey
UnspentKey OutPoint
p)

getNetworkI :: MonadIO m => WriterT m Network
getNetworkI :: WriterT m Network
getNetworkI = (Memory -> Maybe Network)
-> DatabaseReaderT m Network -> WriterT m Network
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered Memory -> Maybe Network
hNet DatabaseReaderT m Network
forall (m :: * -> *). StoreReadBase m => m Network
getNetwork

getBestBlockI :: MonadIO m => WriterT m (Maybe BlockHash)
getBestBlockI :: WriterT m (Maybe BlockHash)
getBestBlockI = (Memory -> Maybe (Maybe BlockHash))
-> DatabaseReaderT m (Maybe BlockHash)
-> WriterT m (Maybe BlockHash)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered Memory -> Maybe (Maybe BlockHash)
getBestBlockH DatabaseReaderT m (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock

getBlocksAtHeightI :: MonadIO m => BlockHeight -> WriterT m [BlockHash]
getBlocksAtHeightI :: BlockHeight -> WriterT m [BlockHash]
getBlocksAtHeightI bh :: BlockHeight
bh =
    (Memory -> Maybe [BlockHash])
-> DatabaseReaderT m [BlockHash] -> WriterT m [BlockHash]
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (BlockHeight -> Memory -> Maybe [BlockHash]
getBlocksAtHeightH BlockHeight
bh) (BlockHeight -> DatabaseReaderT m [BlockHash]
forall (m :: * -> *).
StoreReadBase m =>
BlockHeight -> m [BlockHash]
getBlocksAtHeight BlockHeight
bh)

getBlockI :: MonadIO m => BlockHash -> WriterT m (Maybe BlockData)
getBlockI :: BlockHash -> WriterT m (Maybe BlockData)
getBlockI bh :: BlockHash
bh = (Memory -> Maybe (Maybe BlockData))
-> DatabaseReaderT m (Maybe BlockData)
-> WriterT m (Maybe BlockData)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (BlockHash -> Memory -> Maybe (Maybe BlockData)
getBlockH BlockHash
bh) (BlockHash -> DatabaseReaderT m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh)

getTxDataI :: MonadIO m => TxHash -> WriterT m (Maybe TxData)
getTxDataI :: TxHash -> WriterT m (Maybe TxData)
getTxDataI th :: TxHash
th = (Memory -> Maybe (Maybe TxData))
-> DatabaseReaderT m (Maybe TxData) -> WriterT m (Maybe TxData)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (TxHash -> Memory -> Maybe (Maybe TxData)
getTxDataH TxHash
th) (TxHash -> DatabaseReaderT m (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData TxHash
th)

getSpenderI :: MonadIO m => OutPoint -> WriterT m (Maybe Spender)
getSpenderI :: OutPoint -> WriterT m (Maybe Spender)
getSpenderI op :: OutPoint
op = (Memory -> Maybe (Maybe Spender))
-> DatabaseReaderT m (Maybe Spender) -> WriterT m (Maybe Spender)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (OutPoint -> Memory -> Maybe (Maybe Spender)
getSpenderH OutPoint
op) (OutPoint -> DatabaseReaderT m (Maybe Spender)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Spender)
getSpender OutPoint
op)

getBalanceI :: MonadIO m => Address -> WriterT m (Maybe Balance)
getBalanceI :: Address -> WriterT m (Maybe Balance)
getBalanceI a :: Address
a = (Memory -> Maybe (Maybe Balance))
-> DatabaseReaderT m (Maybe Balance) -> WriterT m (Maybe Balance)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (Address -> Memory -> Maybe (Maybe Balance)
getBalanceH Address
a) (Address -> DatabaseReaderT m (Maybe Balance)
forall (m :: * -> *).
StoreReadBase m =>
Address -> m (Maybe Balance)
getBalance Address
a)

getUnspentI :: MonadIO m => OutPoint -> WriterT m (Maybe Unspent)
getUnspentI :: OutPoint -> WriterT m (Maybe Unspent)
getUnspentI op :: OutPoint
op = (Memory -> Maybe (Maybe Unspent))
-> DatabaseReaderT m (Maybe Unspent) -> WriterT m (Maybe Unspent)
forall (m :: * -> *) a.
MonadIO m =>
(Memory -> Maybe a) -> DatabaseReaderT m a -> WriterT m a
getLayered (OutPoint -> Memory -> Maybe (Maybe Unspent)
getUnspentH OutPoint
op) (OutPoint -> DatabaseReaderT m (Maybe Unspent)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Unspent)
getUnspent OutPoint
op)

getMempoolI :: MonadIO m => WriterT m [(UnixTime, TxHash)]
getMempoolI :: WriterT m [(UnixTime, TxHash)]
getMempoolI =
    (Writer -> m [(UnixTime, TxHash)])
-> WriterT m [(UnixTime, TxHash)]
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((Writer -> m [(UnixTime, TxHash)])
 -> WriterT m [(UnixTime, TxHash)])
-> (Writer -> m [(UnixTime, TxHash)])
-> WriterT m [(UnixTime, TxHash)]
forall a b. (a -> b) -> a -> b
$ \Writer { getState :: Writer -> TVar Memory
getState = TVar Memory
tmem } ->
        Memory -> [(UnixTime, TxHash)]
getMempoolH (Memory -> [(UnixTime, TxHash)])
-> m Memory -> m [(UnixTime, TxHash)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TVar Memory -> m Memory
forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar Memory
tmem

newMemory :: [(UnixTime, TxHash)] -> Memory
newMemory :: [(UnixTime, TxHash)] -> Memory
newMemory mem :: [(UnixTime, TxHash)]
mem =
    $WMemory :: Maybe Network
-> Maybe (Maybe BlockHash)
-> HashMap BlockHash (Maybe BlockData)
-> HashMap BlockHeight [BlockHash]
-> HashMap TxHash (Maybe TxData)
-> HashMap OutPoint (Maybe Spender)
-> HashMap OutPoint (Maybe Unspent)
-> HashMap Address (Maybe Balance)
-> HashMap (Address, TxRef) (Maybe ())
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> HashMap TxHash UnixTime
-> Memory
Memory { hNet :: Maybe Network
hNet     = Maybe Network
forall a. Maybe a
Nothing
           , hBest :: Maybe (Maybe BlockHash)
hBest    = Maybe (Maybe BlockHash)
forall a. Maybe a
Nothing
           , hBlock :: HashMap BlockHash (Maybe BlockData)
hBlock   = HashMap BlockHash (Maybe BlockData)
forall k v. HashMap k v
M.empty
           , hHeight :: HashMap BlockHeight [BlockHash]
hHeight  = HashMap BlockHeight [BlockHash]
forall k v. HashMap k v
M.empty
           , hTx :: HashMap TxHash (Maybe TxData)
hTx      = HashMap TxHash (Maybe TxData)
forall k v. HashMap k v
M.empty
           , hSpender :: HashMap OutPoint (Maybe Spender)
hSpender = HashMap OutPoint (Maybe Spender)
forall k v. HashMap k v
M.empty
           , hUnspent :: HashMap OutPoint (Maybe Unspent)
hUnspent = HashMap OutPoint (Maybe Unspent)
forall k v. HashMap k v
M.empty
           , hBalance :: HashMap Address (Maybe Balance)
hBalance = HashMap Address (Maybe Balance)
forall k v. HashMap k v
M.empty
           , hAddrTx :: HashMap (Address, TxRef) (Maybe ())
hAddrTx  = HashMap (Address, TxRef) (Maybe ())
forall k v. HashMap k v
M.empty
           , hAddrOut :: HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut = HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
forall k v. HashMap k v
M.empty
           , hMempool :: HashMap TxHash UnixTime
hMempool = [(TxHash, UnixTime)] -> HashMap TxHash UnixTime
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList (((UnixTime, TxHash) -> (TxHash, UnixTime))
-> [(UnixTime, TxHash)] -> [(TxHash, UnixTime)]
forall a b. (a -> b) -> [a] -> [b]
map (UnixTime, TxHash) -> (TxHash, UnixTime)
forall a b. (a, b) -> (b, a)
swap [(UnixTime, TxHash)]
mem)
           }

getBestBlockH :: Memory -> Maybe (Maybe BlockHash)
getBestBlockH :: Memory -> Maybe (Maybe BlockHash)
getBestBlockH = Memory -> Maybe (Maybe BlockHash)
hBest

getBlocksAtHeightH :: BlockHeight -> Memory -> Maybe [BlockHash]
getBlocksAtHeightH :: BlockHeight -> Memory -> Maybe [BlockHash]
getBlocksAtHeightH h :: BlockHeight
h = BlockHeight -> HashMap BlockHeight [BlockHash] -> Maybe [BlockHash]
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup BlockHeight
h (HashMap BlockHeight [BlockHash] -> Maybe [BlockHash])
-> (Memory -> HashMap BlockHeight [BlockHash])
-> Memory
-> Maybe [BlockHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory -> HashMap BlockHeight [BlockHash]
hHeight

getBlockH :: BlockHash -> Memory -> Maybe (Maybe BlockData)
getBlockH :: BlockHash -> Memory -> Maybe (Maybe BlockData)
getBlockH h :: BlockHash
h = BlockHash
-> HashMap BlockHash (Maybe BlockData) -> Maybe (Maybe BlockData)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup BlockHash
h (HashMap BlockHash (Maybe BlockData) -> Maybe (Maybe BlockData))
-> (Memory -> HashMap BlockHash (Maybe BlockData))
-> Memory
-> Maybe (Maybe BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory -> HashMap BlockHash (Maybe BlockData)
hBlock

getTxDataH :: TxHash -> Memory -> Maybe (Maybe TxData)
getTxDataH :: TxHash -> Memory -> Maybe (Maybe TxData)
getTxDataH t :: TxHash
t = TxHash -> HashMap TxHash (Maybe TxData) -> Maybe (Maybe TxData)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup TxHash
t (HashMap TxHash (Maybe TxData) -> Maybe (Maybe TxData))
-> (Memory -> HashMap TxHash (Maybe TxData))
-> Memory
-> Maybe (Maybe TxData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory -> HashMap TxHash (Maybe TxData)
hTx

getSpenderH :: OutPoint -> Memory -> Maybe (Maybe Spender)
getSpenderH :: OutPoint -> Memory -> Maybe (Maybe Spender)
getSpenderH op :: OutPoint
op db :: Memory
db = OutPoint
-> HashMap OutPoint (Maybe Spender) -> Maybe (Maybe Spender)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup OutPoint
op (Memory -> HashMap OutPoint (Maybe Spender)
hSpender Memory
db)

getBalanceH :: Address -> Memory -> Maybe (Maybe Balance)
getBalanceH :: Address -> Memory -> Maybe (Maybe Balance)
getBalanceH a :: Address
a = Address -> HashMap Address (Maybe Balance) -> Maybe (Maybe Balance)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup Address
a (HashMap Address (Maybe Balance) -> Maybe (Maybe Balance))
-> (Memory -> HashMap Address (Maybe Balance))
-> Memory
-> Maybe (Maybe Balance)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory -> HashMap Address (Maybe Balance)
hBalance

getMempoolH :: Memory -> [(UnixTime, TxHash)]
getMempoolH :: Memory -> [(UnixTime, TxHash)]
getMempoolH = ((UnixTime, TxHash) -> Down (UnixTime, TxHash))
-> [(UnixTime, TxHash)] -> [(UnixTime, TxHash)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (UnixTime, TxHash) -> Down (UnixTime, TxHash)
forall a. a -> Down a
Down ([(UnixTime, TxHash)] -> [(UnixTime, TxHash)])
-> (Memory -> [(UnixTime, TxHash)])
-> Memory
-> [(UnixTime, TxHash)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((TxHash, UnixTime) -> (UnixTime, TxHash))
-> [(TxHash, UnixTime)] -> [(UnixTime, TxHash)]
forall a b. (a -> b) -> [a] -> [b]
map (TxHash, UnixTime) -> (UnixTime, TxHash)
forall a b. (a, b) -> (b, a)
swap ([(TxHash, UnixTime)] -> [(UnixTime, TxHash)])
-> (Memory -> [(TxHash, UnixTime)])
-> Memory
-> [(UnixTime, TxHash)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap TxHash UnixTime -> [(TxHash, UnixTime)]
forall k v. HashMap k v -> [(k, v)]
M.toList (HashMap TxHash UnixTime -> [(TxHash, UnixTime)])
-> (Memory -> HashMap TxHash UnixTime)
-> Memory
-> [(TxHash, UnixTime)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Memory -> HashMap TxHash UnixTime
hMempool

setBestH :: BlockHash -> Memory -> Memory
setBestH :: BlockHash -> Memory -> Memory
setBestH h :: BlockHash
h db :: Memory
db = Memory
db {hBest :: Maybe (Maybe BlockHash)
hBest = Maybe BlockHash -> Maybe (Maybe BlockHash)
forall a. a -> Maybe a
Just (BlockHash -> Maybe BlockHash
forall a. a -> Maybe a
Just BlockHash
h)}

insertBlockH :: BlockData -> Memory -> Memory
insertBlockH :: BlockData -> Memory -> Memory
insertBlockH bd :: BlockData
bd db :: Memory
db =
    Memory
db { hBlock :: HashMap BlockHash (Maybe BlockData)
hBlock = BlockHash
-> Maybe BlockData
-> HashMap BlockHash (Maybe BlockData)
-> HashMap BlockHash (Maybe BlockData)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert
                  (BlockHeader -> BlockHash
headerHash (BlockData -> BlockHeader
blockDataHeader BlockData
bd))
                  (BlockData -> Maybe BlockData
forall a. a -> Maybe a
Just BlockData
bd)
                  (Memory -> HashMap BlockHash (Maybe BlockData)
hBlock Memory
db)
       }

setBlocksAtHeightH :: [BlockHash] -> BlockHeight -> Memory -> Memory
setBlocksAtHeightH :: [BlockHash] -> BlockHeight -> Memory -> Memory
setBlocksAtHeightH hs :: [BlockHash]
hs g :: BlockHeight
g db :: Memory
db =
    Memory
db {hHeight :: HashMap BlockHeight [BlockHash]
hHeight = BlockHeight
-> [BlockHash]
-> HashMap BlockHeight [BlockHash]
-> HashMap BlockHeight [BlockHash]
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert BlockHeight
g [BlockHash]
hs (Memory -> HashMap BlockHeight [BlockHash]
hHeight Memory
db)}

insertTxH :: TxData -> Memory -> Memory
insertTxH :: TxData -> Memory -> Memory
insertTxH tx :: TxData
tx db :: Memory
db =
    Memory
db {hTx :: HashMap TxHash (Maybe TxData)
hTx = TxHash
-> Maybe TxData
-> HashMap TxHash (Maybe TxData)
-> HashMap TxHash (Maybe TxData)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Tx -> TxHash
txHash (TxData -> Tx
txData TxData
tx)) (TxData -> Maybe TxData
forall a. a -> Maybe a
Just TxData
tx) (Memory -> HashMap TxHash (Maybe TxData)
hTx Memory
db)}

insertSpenderH :: OutPoint -> Spender -> Memory -> Memory
insertSpenderH :: OutPoint -> Spender -> Memory -> Memory
insertSpenderH op :: OutPoint
op s :: Spender
s db :: Memory
db =
    Memory
db { hSpender :: HashMap OutPoint (Maybe Spender)
hSpender = OutPoint
-> Maybe Spender
-> HashMap OutPoint (Maybe Spender)
-> HashMap OutPoint (Maybe Spender)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert OutPoint
op (Spender -> Maybe Spender
forall a. a -> Maybe a
Just Spender
s) (Memory -> HashMap OutPoint (Maybe Spender)
hSpender Memory
db) }

deleteSpenderH :: OutPoint -> Memory -> Memory
deleteSpenderH :: OutPoint -> Memory -> Memory
deleteSpenderH op :: OutPoint
op db :: Memory
db =
    Memory
db { hSpender :: HashMap OutPoint (Maybe Spender)
hSpender = OutPoint
-> Maybe Spender
-> HashMap OutPoint (Maybe Spender)
-> HashMap OutPoint (Maybe Spender)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert OutPoint
op Maybe Spender
forall a. Maybe a
Nothing (Memory -> HashMap OutPoint (Maybe Spender)
hSpender Memory
db) }

setBalanceH :: Balance -> Memory -> Memory
setBalanceH :: Balance -> Memory -> Memory
setBalanceH bal :: Balance
bal db :: Memory
db =
    Memory
db {hBalance :: HashMap Address (Maybe Balance)
hBalance = Address
-> Maybe Balance
-> HashMap Address (Maybe Balance)
-> HashMap Address (Maybe Balance)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Balance -> Address
balanceAddress Balance
bal) (Balance -> Maybe Balance
forall a. a -> Maybe a
Just Balance
bal) (Memory -> HashMap Address (Maybe Balance)
hBalance Memory
db)}

insertAddrTxH :: Address -> TxRef -> Memory -> Memory
insertAddrTxH :: Address -> TxRef -> Memory -> Memory
insertAddrTxH a :: Address
a tr :: TxRef
tr db :: Memory
db =
    Memory
db { hAddrTx :: HashMap (Address, TxRef) (Maybe ())
hAddrTx = (Address, TxRef)
-> Maybe ()
-> HashMap (Address, TxRef) (Maybe ())
-> HashMap (Address, TxRef) (Maybe ())
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Address
a, TxRef
tr) (() -> Maybe ()
forall a. a -> Maybe a
Just ()) (Memory -> HashMap (Address, TxRef) (Maybe ())
hAddrTx Memory
db) }

deleteAddrTxH :: Address -> TxRef -> Memory -> Memory
deleteAddrTxH :: Address -> TxRef -> Memory -> Memory
deleteAddrTxH a :: Address
a tr :: TxRef
tr db :: Memory
db =
    Memory
db { hAddrTx :: HashMap (Address, TxRef) (Maybe ())
hAddrTx = (Address, TxRef)
-> Maybe ()
-> HashMap (Address, TxRef) (Maybe ())
-> HashMap (Address, TxRef) (Maybe ())
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Address
a, TxRef
tr) Maybe ()
forall a. Maybe a
Nothing (Memory -> HashMap (Address, TxRef) (Maybe ())
hAddrTx Memory
db) }

insertAddrUnspentH :: Address -> Unspent -> Memory -> Memory
insertAddrUnspentH :: Address -> Unspent -> Memory -> Memory
insertAddrUnspentH a :: Address
a u :: Unspent
u db :: Memory
db =
    let k :: (Address, BlockRef, OutPoint)
k = (Address
a, Unspent -> BlockRef
unspentBlock Unspent
u, Unspent -> OutPoint
unspentPoint Unspent
u)
        v :: OutVal
v = $WOutVal :: UnixTime -> ByteString -> OutVal
OutVal { outValAmount :: UnixTime
outValAmount = Unspent -> UnixTime
unspentAmount Unspent
u
                   , outValScript :: ByteString
outValScript = ShortByteString -> ByteString
B.Short.fromShort (Unspent -> ShortByteString
unspentScript Unspent
u)
                   }
     in Memory
db { hAddrOut :: HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut = (Address, BlockRef, OutPoint)
-> Maybe OutVal
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Address, BlockRef, OutPoint)
k (OutVal -> Maybe OutVal
forall a. a -> Maybe a
Just OutVal
v) (Memory -> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut Memory
db) }

deleteAddrUnspentH :: Address -> Unspent -> Memory -> Memory
deleteAddrUnspentH :: Address -> Unspent -> Memory -> Memory
deleteAddrUnspentH a :: Address
a u :: Unspent
u db :: Memory
db =
    let k :: (Address, BlockRef, OutPoint)
k = (Address
a, Unspent -> BlockRef
unspentBlock Unspent
u, Unspent -> OutPoint
unspentPoint Unspent
u)
     in Memory
db { hAddrOut :: HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut = (Address, BlockRef, OutPoint)
-> Maybe OutVal
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
-> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert (Address, BlockRef, OutPoint)
k Maybe OutVal
forall a. Maybe a
Nothing (Memory -> HashMap (Address, BlockRef, OutPoint) (Maybe OutVal)
hAddrOut Memory
db) }

addToMempoolH :: TxHash -> UnixTime -> Memory -> Memory
addToMempoolH :: TxHash -> UnixTime -> Memory -> Memory
addToMempoolH h :: TxHash
h t :: UnixTime
t db :: Memory
db =
    Memory
db { hMempool :: HashMap TxHash UnixTime
hMempool = TxHash
-> UnixTime -> HashMap TxHash UnixTime -> HashMap TxHash UnixTime
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert TxHash
h UnixTime
t (Memory -> HashMap TxHash UnixTime
hMempool Memory
db) }

deleteFromMempoolH :: TxHash -> Memory -> Memory
deleteFromMempoolH :: TxHash -> Memory -> Memory
deleteFromMempoolH h :: TxHash
h db :: Memory
db =
    Memory
db { hMempool :: HashMap TxHash UnixTime
hMempool = TxHash -> HashMap TxHash UnixTime -> HashMap TxHash UnixTime
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
M.delete TxHash
h (Memory -> HashMap TxHash UnixTime
hMempool Memory
db) }

getUnspentH :: OutPoint -> Memory -> Maybe (Maybe Unspent)
getUnspentH :: OutPoint -> Memory -> Maybe (Maybe Unspent)
getUnspentH op :: OutPoint
op db :: Memory
db = OutPoint
-> HashMap OutPoint (Maybe Unspent) -> Maybe (Maybe Unspent)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup OutPoint
op (Memory -> HashMap OutPoint (Maybe Unspent)
hUnspent Memory
db)

insertUnspentH :: Unspent -> Memory -> Memory
insertUnspentH :: Unspent -> Memory -> Memory
insertUnspentH u :: Unspent
u db :: Memory
db =
    let k :: OutPoint
k = (OutPoint, UnspentVal) -> OutPoint
forall a b. (a, b) -> a
fst (Unspent -> (OutPoint, UnspentVal)
unspentToVal Unspent
u)
     in Memory
db { hUnspent :: HashMap OutPoint (Maybe Unspent)
hUnspent = OutPoint
-> Maybe Unspent
-> HashMap OutPoint (Maybe Unspent)
-> HashMap OutPoint (Maybe Unspent)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert OutPoint
k (Unspent -> Maybe Unspent
forall a. a -> Maybe a
Just Unspent
u) (Memory -> HashMap OutPoint (Maybe Unspent)
hUnspent Memory
db) }

deleteUnspentH :: OutPoint -> Memory -> Memory
deleteUnspentH :: OutPoint -> Memory -> Memory
deleteUnspentH op :: OutPoint
op db :: Memory
db =
    Memory
db { hUnspent :: HashMap OutPoint (Maybe Unspent)
hUnspent = OutPoint
-> Maybe Unspent
-> HashMap OutPoint (Maybe Unspent)
-> HashMap OutPoint (Maybe Unspent)
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
M.insert OutPoint
op Maybe Unspent
forall a. Maybe a
Nothing (Memory -> HashMap OutPoint (Maybe Unspent)
hUnspent Memory
db) }