{-# LANGUAGE CPP                 #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE MultiWayIf          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TupleSections       #-}
module Haskoin.Store.Web
    ( -- * Web
      WebConfig (..)
    , Except (..)
    , WebLimits (..)
    , WebTimeouts (..)
    , runWeb
    ) where

import           Conduit                       (ConduitT, await, dropC,
                                                runConduit, sinkList, takeC,
                                                yield, (.|))
import           Control.Applicative           ((<|>))
import           Control.Arrow                 (second)
import           Control.Lens
import           Control.Monad                 (forever, unless, when, (<=<))
import           Control.Monad.Logger          (MonadLoggerIO, logDebugS,
                                                logErrorS, logInfoS)
import           Control.Monad.Reader          (ReaderT, ask, asks, local,
                                                runReaderT)
import           Control.Monad.Trans           (lift)
import           Control.Monad.Trans.Control   (liftWith, restoreT)
import           Control.Monad.Trans.Maybe     (MaybeT (..), runMaybeT)
import           Data.Aeson                    (Encoding, ToJSON (..), Value)
import           Data.Aeson.Encode.Pretty      (Config (..), defConfig,
                                                encodePretty')
import           Data.Aeson.Encoding           (encodingToLazyByteString, list)
import           Data.Aeson.Text               (encodeToLazyText)
import           Data.ByteString.Builder       (lazyByteString)
import qualified Data.ByteString.Lazy          as L
import qualified Data.ByteString.Lazy.Char8    as C
import           Data.ByteString.Short         (fromShort)
import           Data.Char                     (isSpace)
import           Data.Default                  (Default (..))
import           Data.Function                 (on, (&))
import           Data.HashMap.Strict           (HashMap)
import qualified Data.HashMap.Strict           as HashMap
import           Data.HashSet                  (HashSet)
import qualified Data.HashSet                  as HashSet
import           Data.Int                      (Int64)
import           Data.List                     (nub, sortBy)
import qualified Data.Map.Strict               as Map
import           Data.Maybe                    (catMaybes, fromJust, fromMaybe,
                                                isJust, listToMaybe, mapMaybe,
                                                maybeToList)
import           Data.Proxy                    (Proxy (..))
import           Data.Serialize                as Serialize
import qualified Data.Set                      as Set
import           Data.String                   (fromString)
import           Data.String.Conversions       (cs)
import           Data.Text                     (Text)
import qualified Data.Text                     as T
import qualified Data.Text.Encoding            as T
import           Data.Text.Lazy                (toStrict)
import qualified Data.Text.Lazy                as TL
import           Data.Time.Clock               (NominalDiffTime, diffUTCTime)
import           Data.Time.Clock.System        (getSystemTime, systemSeconds,
                                                systemToUTCTime)
import           Data.Word                     (Word32, Word64)
import           Database.RocksDB              (Property (..), getProperty)
import           Haskoin.Address
import qualified Haskoin.Block                 as H
import           Haskoin.Constants
import           Haskoin.Keys
import           Haskoin.Network
import           Haskoin.Node                  (Chain, OnlinePeer (..),
                                                PeerManager, chainGetBest,
                                                getPeers, sendMessage)
import           Haskoin.Store.BlockStore
import           Haskoin.Store.Cache
import           Haskoin.Store.Common
import           Haskoin.Store.Data
import           Haskoin.Store.Database.Reader
import           Haskoin.Store.Logic
import           Haskoin.Store.Manager
import           Haskoin.Store.Stats
import           Haskoin.Store.WebCommon
import           Haskoin.Transaction
import           Haskoin.Util
import           NQE                           (Inbox, receive,
                                                withSubscription)
import           Network.HTTP.Types            (Status (..), status400,
                                                status403, status404, status409,
                                                status500, status503,
                                                statusIsClientError,
                                                statusIsServerError,
                                                statusIsSuccessful)
import           Network.Wai                   (Middleware, Request (..),
                                                responseStatus)
import           Network.Wai.Handler.Warp      (defaultSettings, setHost,
                                                setPort)
import qualified Network.Wreq                  as Wreq
import qualified System.Metrics                as Metrics
import qualified System.Metrics.Counter        as Metrics (Counter)
import qualified System.Metrics.Counter        as Metrics.Counter
import qualified System.Metrics.Gauge          as Metrics (Gauge)
import qualified System.Metrics.Gauge          as Metrics.Gauge
import           Text.Printf                   (printf)
import           UnliftIO                      (MonadIO, MonadUnliftIO, TVar,
                                                askRunInIO, atomically, bracket,
                                                bracket_, handleAny, liftIO,
                                                newTVarIO, readTVarIO, timeout,
                                                withAsync, writeTVar)
import           UnliftIO.Concurrent           (threadDelay)
import           Web.Scotty.Internal.Types     (ActionT)
import           Web.Scotty.Trans              (Parsable)
import qualified Web.Scotty.Trans              as S

type WebT m = ActionT Except (ReaderT WebState m)

data WebLimits = WebLimits
    { WebLimits -> Word32
maxLimitCount      :: !Word32
    , WebLimits -> Word32
maxLimitFull       :: !Word32
    , WebLimits -> Word32
maxLimitOffset     :: !Word32
    , WebLimits -> Word32
maxLimitDefault    :: !Word32
    , WebLimits -> Word32
maxLimitGap        :: !Word32
    , WebLimits -> Word32
maxLimitInitialGap :: !Word32
    }
    deriving (WebLimits -> WebLimits -> Bool
(WebLimits -> WebLimits -> Bool)
-> (WebLimits -> WebLimits -> Bool) -> Eq WebLimits
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WebLimits -> WebLimits -> Bool
$c/= :: WebLimits -> WebLimits -> Bool
== :: WebLimits -> WebLimits -> Bool
$c== :: WebLimits -> WebLimits -> Bool
Eq, Int -> WebLimits -> ShowS
[WebLimits] -> ShowS
WebLimits -> String
(Int -> WebLimits -> ShowS)
-> (WebLimits -> String)
-> ([WebLimits] -> ShowS)
-> Show WebLimits
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WebLimits] -> ShowS
$cshowList :: [WebLimits] -> ShowS
show :: WebLimits -> String
$cshow :: WebLimits -> String
showsPrec :: Int -> WebLimits -> ShowS
$cshowsPrec :: Int -> WebLimits -> ShowS
Show)

instance Default WebLimits where
    def :: WebLimits
def =
        $WWebLimits :: Word32
-> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> WebLimits
WebLimits
            { maxLimitCount :: Word32
maxLimitCount = 200000
            , maxLimitFull :: Word32
maxLimitFull = 5000
            , maxLimitOffset :: Word32
maxLimitOffset = 50000
            , maxLimitDefault :: Word32
maxLimitDefault = 100
            , maxLimitGap :: Word32
maxLimitGap = 32
            , maxLimitInitialGap :: Word32
maxLimitInitialGap = 20
            }

data WebConfig = WebConfig
    { WebConfig -> String
webHost       :: !String
    , WebConfig -> Int
webPort       :: !Int
    , WebConfig -> Store
webStore      :: !Store
    , WebConfig -> Int
webMaxDiff    :: !Int
    , WebConfig -> Int
webMaxPending :: !Int
    , WebConfig -> WebLimits
webMaxLimits  :: !WebLimits
    , WebConfig -> WebTimeouts
webTimeouts   :: !WebTimeouts
    , WebConfig -> String
webVersion    :: !String
    , WebConfig -> Bool
webNoMempool  :: !Bool
    , WebConfig -> Maybe Store
webStats      :: !(Maybe Metrics.Store)
    }

data WebState = WebState
    { WebState -> WebConfig
webConfig  :: !WebConfig
    , WebState -> TVar (HashMap Text BinfoTicker)
webTicker  :: !(TVar (HashMap Text BinfoTicker))
    , WebState -> Maybe WebMetrics
webMetrics :: !(Maybe WebMetrics)
    }

data ErrorCounter = ErrorCounter
    { ErrorCounter -> Counter
clientErrors :: Metrics.Counter
    , ErrorCounter -> Counter
serverErrors :: Metrics.Counter
    }

createErrorCounter :: MonadIO m
                   => Text
                   -> Metrics.Store
                   -> m ErrorCounter
createErrorCounter :: Text -> Store -> m ErrorCounter
createErrorCounter t :: Text
t s :: Store
s = IO ErrorCounter -> m ErrorCounter
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ErrorCounter -> m ErrorCounter)
-> IO ErrorCounter -> m ErrorCounter
forall a b. (a -> b) -> a -> b
$ do
    Counter
clientErrors <- Text -> Store -> IO Counter
Metrics.createCounter (Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ".client_errors") Store
s
    Counter
serverErrors <- Text -> Store -> IO Counter
Metrics.createCounter (Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ".server_errors") Store
s
    ErrorCounter -> IO ErrorCounter
forall (m :: * -> *) a. Monad m => a -> m a
return ErrorCounter :: Counter -> Counter -> ErrorCounter
ErrorCounter{..}

data WebMetrics = WebMetrics
    { WebMetrics -> StatDist
everyResponseTime       :: !StatDist
    , WebMetrics -> ErrorCounter
everyError              :: !ErrorCounter
    , WebMetrics -> StatDist
blockResponseTime       :: !StatDist
    , WebMetrics -> StatDist
rawBlockResponseTime    :: !StatDist
    , WebMetrics -> ErrorCounter
blockErrors             :: !ErrorCounter
    , WebMetrics -> StatDist
txResponseTime          :: !StatDist
    , WebMetrics -> StatDist
txsBlockResponseTime    :: !StatDist
    , WebMetrics -> ErrorCounter
txErrors                :: !ErrorCounter
    , WebMetrics -> StatDist
txAfterResponseTime     :: !StatDist
    , WebMetrics -> StatDist
postTxResponseTime      :: !StatDist
    , WebMetrics -> ErrorCounter
postTxErrors            :: !ErrorCounter
    , WebMetrics -> StatDist
mempoolResponseTime     :: !StatDist
    , WebMetrics -> StatDist
addrTxResponseTime      :: !StatDist
    , WebMetrics -> StatDist
addrTxFullResponseTime  :: !StatDist
    , WebMetrics -> StatDist
addrBalanceResponseTime :: !StatDist
    , WebMetrics -> StatDist
addrUnspentResponseTime :: !StatDist
    , WebMetrics -> StatDist
xPubResponseTime        :: !StatDist
    , WebMetrics -> StatDist
xPubTxResponseTime      :: !StatDist
    , WebMetrics -> StatDist
xPubTxFullResponseTime  :: !StatDist
    , WebMetrics -> StatDist
xPubUnspentResponseTime :: !StatDist
    , WebMetrics -> StatDist
multiaddrResponseTime   :: !StatDist
    , WebMetrics -> ErrorCounter
multiaddrErrors         :: !ErrorCounter
    , WebMetrics -> StatDist
unspentResponseTime     :: !StatDist
    , WebMetrics -> ErrorCounter
unspentErrors           :: !ErrorCounter
    , WebMetrics -> StatDist
rawtxResponseTime       :: !StatDist
    , WebMetrics -> ErrorCounter
rawtxErrors             :: !ErrorCounter
    , WebMetrics -> StatDist
peerResponseTime        :: !StatDist
    , WebMetrics -> StatDist
healthResponseTime      :: !StatDist
    , WebMetrics -> StatDist
dbStatsResponseTime     :: !StatDist
    , WebMetrics -> Gauge
eventsConnected         :: !Metrics.Gauge
    }

createMetrics :: MonadIO m => Metrics.Store -> m WebMetrics
createMetrics :: Store -> m WebMetrics
createMetrics s :: Store
s = IO WebMetrics -> m WebMetrics
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO WebMetrics -> m WebMetrics) -> IO WebMetrics -> m WebMetrics
forall a b. (a -> b) -> a -> b
$ do
    StatDist
everyResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "all.response_time_ms"
    ErrorCounter
everyError                <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "all.errors"
    StatDist
blockResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "block.response_time_ms"
    ErrorCounter
blockErrors               <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "block.errors"
    StatDist
rawBlockResponseTime      <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "block_raw.response_time_ms"
    StatDist
txResponseTime            <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx.response_time"
    StatDist
txsBlockResponseTime      <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "txs_block.response_time"
    ErrorCounter
txErrors                  <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "tx.errors"
    StatDist
txAfterResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx_after.response_time_ms"
    StatDist
postTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "tx_post.response_time_ms"
    ErrorCounter
postTxErrors              <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "tx_post.errors"
    StatDist
mempoolResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "mempool.response_time_ms"
    StatDist
addrBalanceResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_balance.response_time_ms"
    StatDist
addrTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_tx.response_time_ms"
    StatDist
addrTxFullResponseTime    <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_tx_full.response_time_ms"
    StatDist
addrUnspentResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "addr_unspent.response_time_ms"
    StatDist
xPubResponseTime          <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_balance.response_time_ms"
    StatDist
xPubTxResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_tx.response_time_ms"
    StatDist
xPubTxFullResponseTime    <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_tx_full.response_time_ms"
    StatDist
xPubUnspentResponseTime   <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "xpub_unspent.response_time_ms"
    StatDist
multiaddrResponseTime     <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "multiaddr.response_time_ms"
    ErrorCounter
multiaddrErrors           <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "multiaddr.errors"
    StatDist
rawtxResponseTime         <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "rawtx.response_time_ms"
    ErrorCounter
rawtxErrors               <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "rawtx.errors"
    StatDist
peerResponseTime          <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "peer.response_time_ms"
    StatDist
healthResponseTime        <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "health.response_time_ms"
    StatDist
dbStatsResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "dbstats.response_time_ms"
    Gauge
eventsConnected           <- Text -> IO Gauge
g "events.connected"
    StatDist
unspentResponseTime       <- Text -> IO StatDist
forall (m :: * -> *). MonadIO m => Text -> m StatDist
d "unspent.response_time_ms"
    ErrorCounter
unspentErrors             <- Text -> IO ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> m ErrorCounter
e "unspent.errors"
    WebMetrics -> IO WebMetrics
forall (m :: * -> *) a. Monad m => a -> m a
return $WWebMetrics :: StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> StatDist
-> ErrorCounter
-> StatDist
-> ErrorCounter
-> StatDist
-> ErrorCounter
-> StatDist
-> StatDist
-> StatDist
-> Gauge
-> WebMetrics
WebMetrics{..}
  where
    d :: Text -> m StatDist
d x :: Text
x = Text -> Store -> m StatDist
forall (m :: * -> *). MonadIO m => Text -> Store -> m StatDist
createStatDist       ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s
    g :: Text -> IO Gauge
g x :: Text
x = Text -> Store -> IO Gauge
Metrics.createGauge  ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s
    e :: Text -> m ErrorCounter
e x :: Text
x = Text -> Store -> m ErrorCounter
forall (m :: * -> *). MonadIO m => Text -> Store -> m ErrorCounter
createErrorCounter   ("web." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x) Store
s

withGaugeIncrease :: MonadUnliftIO m
                  => (WebMetrics -> Metrics.Gauge)
                  -> WebT m a
                  -> WebT m a
withGaugeIncrease :: (WebMetrics -> Gauge) -> WebT m a -> WebT m a
withGaugeIncrease gf :: WebMetrics -> Gauge
gf go :: WebT m a
go =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> WebT m a
go
        Just m :: WebMetrics
m -> do
            (Either (ActionError Except) a, ScottyResponse)
s <- (Run (ActionT Except)
 -> ReaderT
      WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run (ActionT Except)
  -> ReaderT
       WebState m (Either (ActionError Except) a, ScottyResponse))
 -> ActionT
      Except
      (ReaderT WebState m)
      (Either (ActionError Except) a, ScottyResponse))
-> (Run (ActionT Except)
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall a b. (a -> b) -> a -> b
$ \run :: Run (ActionT Except)
run ->
                ReaderT WebState m ()
-> ReaderT WebState m ()
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> m b -> m c -> m c
bracket_
                (WebMetrics -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> m ()
start WebMetrics
m)
                (WebMetrics -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> m ()
end WebMetrics
m)
                (WebT m a -> ReaderT WebState m (StT (ActionT Except) a)
Run (ActionT Except)
run WebT m a
go)
            ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (ReaderT WebState m (StT (ActionT Except) a) -> WebT m a)
-> ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall a b. (a -> b) -> a -> b
$ (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (ActionError Except) a, ScottyResponse)
s
  where
    start :: WebMetrics -> m ()
start m :: WebMetrics
m = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Gauge -> IO ()
Metrics.Gauge.inc (WebMetrics -> Gauge
gf WebMetrics
m)
    end :: WebMetrics -> m ()
end m :: WebMetrics
m = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Gauge -> IO ()
Metrics.Gauge.dec (WebMetrics -> Gauge
gf WebMetrics
m)

withMetrics :: MonadUnliftIO m
            => (WebMetrics -> StatDist)
            -> Int
            -> WebT m a
            -> WebT m a
withMetrics :: (WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics df :: WebMetrics -> StatDist
df i :: Int
i go :: WebT m a
go =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> WebT m a
go
        Just m :: WebMetrics
m -> do
            (Either (ActionError Except) a, ScottyResponse)
x <-
                (Run (ActionT Except)
 -> ReaderT
      WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
(Run t -> m a) -> t m a
liftWith ((Run (ActionT Except)
  -> ReaderT
       WebState m (Either (ActionError Except) a, ScottyResponse))
 -> ActionT
      Except
      (ReaderT WebState m)
      (Either (ActionError Except) a, ScottyResponse))
-> (Run (ActionT Except)
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ActionT
     Except
     (ReaderT WebState m)
     (Either (ActionError Except) a, ScottyResponse)
forall a b. (a -> b) -> a -> b
$ \run :: Run (ActionT Except)
run ->
                ReaderT WebState m UTCTime
-> (UTCTime -> ReaderT WebState m ())
-> (UTCTime
    -> ReaderT
         WebState m (Either (ActionError Except) a, ScottyResponse))
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket
                (SystemTime -> UTCTime
systemToUTCTime (SystemTime -> UTCTime)
-> ReaderT WebState m SystemTime -> ReaderT WebState m UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> ReaderT WebState m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime)
                (WebMetrics -> UTCTime -> ReaderT WebState m ()
forall (m :: * -> *). MonadIO m => WebMetrics -> UTCTime -> m ()
end WebMetrics
m)
                (ReaderT WebState m (Either (ActionError Except) a, ScottyResponse)
-> UTCTime
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall a b. a -> b -> a
const (WebT m a -> ReaderT WebState m (StT (ActionT Except) a)
Run (ActionT Except)
run WebT m a
go))
            ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTransControl t, Monad m) =>
m (StT t a) -> t m a
restoreT (ReaderT WebState m (StT (ActionT Except) a) -> WebT m a)
-> ReaderT WebState m (StT (ActionT Except) a) -> WebT m a
forall a b. (a -> b) -> a -> b
$ (Either (ActionError Except) a, ScottyResponse)
-> ReaderT
     WebState m (Either (ActionError Except) a, ScottyResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either (ActionError Except) a, ScottyResponse)
x
  where
    end :: WebMetrics -> UTCTime -> m ()
end metrics :: WebMetrics
metrics t1 :: UTCTime
t1 = do
        UTCTime
t2 <- SystemTime -> UTCTime
systemToUTCTime (SystemTime -> UTCTime) -> m SystemTime -> m UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime
        let diff :: Int64
diff = NominalDiffTime -> Int64
forall a b. (RealFrac a, Integral b) => a -> b
round (NominalDiffTime -> Int64) -> NominalDiffTime -> Int64
forall a b. (a -> b) -> a -> b
$ UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
t2 UTCTime
t1 NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* 1000
        WebMetrics -> StatDist
df WebMetrics
metrics StatDist -> StatEntry -> m ()
forall (m :: * -> *). MonadIO m => StatDist -> StatEntry -> m ()
`addStatEntry` Int64 -> Int64 -> StatEntry
StatEntry Int64
diff (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)
        WebMetrics -> StatDist
everyResponseTime WebMetrics
metrics StatDist -> StatEntry -> m ()
forall (m :: * -> *). MonadIO m => StatDist -> StatEntry -> m ()
`addStatEntry` Int64 -> Int64 -> StatEntry
StatEntry Int64
diff (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i)

data WebTimeouts = WebTimeouts
    { WebTimeouts -> Word64
txTimeout    :: !Word64
    , WebTimeouts -> Word64
blockTimeout :: !Word64
    }
    deriving (WebTimeouts -> WebTimeouts -> Bool
(WebTimeouts -> WebTimeouts -> Bool)
-> (WebTimeouts -> WebTimeouts -> Bool) -> Eq WebTimeouts
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WebTimeouts -> WebTimeouts -> Bool
$c/= :: WebTimeouts -> WebTimeouts -> Bool
== :: WebTimeouts -> WebTimeouts -> Bool
$c== :: WebTimeouts -> WebTimeouts -> Bool
Eq, Int -> WebTimeouts -> ShowS
[WebTimeouts] -> ShowS
WebTimeouts -> String
(Int -> WebTimeouts -> ShowS)
-> (WebTimeouts -> String)
-> ([WebTimeouts] -> ShowS)
-> Show WebTimeouts
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WebTimeouts] -> ShowS
$cshowList :: [WebTimeouts] -> ShowS
show :: WebTimeouts -> String
$cshow :: WebTimeouts -> String
showsPrec :: Int -> WebTimeouts -> ShowS
$cshowsPrec :: Int -> WebTimeouts -> ShowS
Show)

data SerialAs = SerialAsBinary | SerialAsJSON | SerialAsPrettyJSON
    deriving (SerialAs -> SerialAs -> Bool
(SerialAs -> SerialAs -> Bool)
-> (SerialAs -> SerialAs -> Bool) -> Eq SerialAs
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SerialAs -> SerialAs -> Bool
$c/= :: SerialAs -> SerialAs -> Bool
== :: SerialAs -> SerialAs -> Bool
$c== :: SerialAs -> SerialAs -> Bool
Eq, Int -> SerialAs -> ShowS
[SerialAs] -> ShowS
SerialAs -> String
(Int -> SerialAs -> ShowS)
-> (SerialAs -> String) -> ([SerialAs] -> ShowS) -> Show SerialAs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SerialAs] -> ShowS
$cshowList :: [SerialAs] -> ShowS
show :: SerialAs -> String
$cshow :: SerialAs -> String
showsPrec :: Int -> SerialAs -> ShowS
$cshowsPrec :: Int -> SerialAs -> ShowS
Show)

instance Default WebTimeouts where
    def :: WebTimeouts
def = $WWebTimeouts :: Word64 -> Word64 -> WebTimeouts
WebTimeouts {txTimeout :: Word64
txTimeout = 300, blockTimeout :: Word64
blockTimeout = 7200}

instance (MonadUnliftIO m, MonadLoggerIO m) =>
         StoreReadBase (ReaderT WebState m) where
    getNetwork :: ReaderT WebState m Network
getNetwork = CacheT (DatabaseReaderT m) Network -> ReaderT WebState m Network
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Network
forall (m :: * -> *). StoreReadBase m => m Network
getNetwork
    getBestBlock :: ReaderT WebState m (Maybe BlockHash)
getBestBlock = CacheT (DatabaseReaderT m) (Maybe BlockHash)
-> ReaderT WebState m (Maybe BlockHash)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
    getBlocksAtHeight :: Word32 -> ReaderT WebState m [BlockHash]
getBlocksAtHeight height :: Word32
height = CacheT (DatabaseReaderT m) [BlockHash]
-> ReaderT WebState m [BlockHash]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (Word32 -> CacheT (DatabaseReaderT m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight Word32
height)
    getBlock :: BlockHash -> ReaderT WebState m (Maybe BlockData)
getBlock bh :: BlockHash
bh = CacheT (DatabaseReaderT m) (Maybe BlockData)
-> ReaderT WebState m (Maybe BlockData)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (BlockHash -> CacheT (DatabaseReaderT m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh)
    getTxData :: TxHash -> ReaderT WebState m (Maybe TxData)
getTxData th :: TxHash
th = CacheT (DatabaseReaderT m) (Maybe TxData)
-> ReaderT WebState m (Maybe TxData)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (TxHash -> CacheT (DatabaseReaderT m) (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData TxHash
th)
    getSpender :: OutPoint -> ReaderT WebState m (Maybe Spender)
getSpender op :: OutPoint
op = CacheT (DatabaseReaderT m) (Maybe Spender)
-> ReaderT WebState m (Maybe Spender)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (OutPoint -> CacheT (DatabaseReaderT m) (Maybe Spender)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Spender)
getSpender OutPoint
op)
    getUnspent :: OutPoint -> ReaderT WebState m (Maybe Unspent)
getUnspent op :: OutPoint
op = CacheT (DatabaseReaderT m) (Maybe Unspent)
-> ReaderT WebState m (Maybe Unspent)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (OutPoint -> CacheT (DatabaseReaderT m) (Maybe Unspent)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Unspent)
getUnspent OutPoint
op)
    getBalance :: Address -> ReaderT WebState m (Maybe Balance)
getBalance a :: Address
a = CacheT (DatabaseReaderT m) (Maybe Balance)
-> ReaderT WebState m (Maybe Balance)
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (Address -> CacheT (DatabaseReaderT m) (Maybe Balance)
forall (m :: * -> *).
StoreReadBase m =>
Address -> m (Maybe Balance)
getBalance Address
a)
    getMempool :: ReaderT WebState m [(Word64, TxHash)]
getMempool = CacheT (DatabaseReaderT m) [(Word64, TxHash)]
-> ReaderT WebState m [(Word64, TxHash)]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

instance (MonadUnliftIO m, MonadLoggerIO m) =>
         StoreReadExtra (ReaderT WebState m) where
    getMaxGap :: ReaderT WebState m Word32
getMaxGap = CacheT (DatabaseReaderT m) Word32 -> ReaderT WebState m Word32
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getMaxGap
    getInitialGap :: ReaderT WebState m Word32
getInitialGap = CacheT (DatabaseReaderT m) Word32 -> ReaderT WebState m Word32
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader CacheT (DatabaseReaderT m) Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getInitialGap
    getBalances :: [Address] -> ReaderT WebState m [Balance]
getBalances as :: [Address]
as = CacheT (DatabaseReaderT m) [Balance]
-> ReaderT WebState m [Balance]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader ([Address] -> CacheT (DatabaseReaderT m) [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances [Address]
as)
    getAddressesTxs :: [Address] -> Limits -> ReaderT WebState m [TxRef]
getAddressesTxs as :: [Address]
as = CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef])
-> (Limits -> CacheT (DatabaseReaderT m) [TxRef])
-> Limits
-> ReaderT WebState m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> CacheT (DatabaseReaderT m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
as
    getAddressesUnspents :: [Address] -> Limits -> ReaderT WebState m [Unspent]
getAddressesUnspents as :: [Address]
as = CacheT (DatabaseReaderT m) [Unspent]
-> ReaderT WebState m [Unspent]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [Unspent]
 -> ReaderT WebState m [Unspent])
-> (Limits -> CacheT (DatabaseReaderT m) [Unspent])
-> Limits
-> ReaderT WebState m [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> CacheT (DatabaseReaderT m) [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
as
    xPubBals :: XPubSpec -> ReaderT WebState m [XPubBal]
xPubBals = CacheT (DatabaseReaderT m) [XPubBal]
-> ReaderT WebState m [XPubBal]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [XPubBal]
 -> ReaderT WebState m [XPubBal])
-> (XPubSpec -> CacheT (DatabaseReaderT m) [XPubBal])
-> XPubSpec
-> ReaderT WebState m [XPubBal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> CacheT (DatabaseReaderT m) [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals
    xPubSummary :: XPubSpec -> ReaderT WebState m XPubSummary
xPubSummary = CacheT (DatabaseReaderT m) XPubSummary
-> ReaderT WebState m XPubSummary
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) XPubSummary
 -> ReaderT WebState m XPubSummary)
-> (XPubSpec -> CacheT (DatabaseReaderT m) XPubSummary)
-> XPubSpec
-> ReaderT WebState m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> CacheT (DatabaseReaderT m) XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary
    xPubUnspents :: XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
xPubUnspents xpub :: XPubSpec
xpub = CacheT (DatabaseReaderT m) [XPubUnspent]
-> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [XPubUnspent]
 -> ReaderT WebState m [XPubUnspent])
-> (Limits -> CacheT (DatabaseReaderT m) [XPubUnspent])
-> Limits
-> ReaderT WebState m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> CacheT (DatabaseReaderT m) [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents XPubSpec
xpub
    xPubTxs :: XPubSpec -> Limits -> ReaderT WebState m [TxRef]
xPubTxs xpub :: XPubSpec
xpub = CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxRef] -> ReaderT WebState m [TxRef])
-> (Limits -> CacheT (DatabaseReaderT m) [TxRef])
-> Limits
-> ReaderT WebState m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> CacheT (DatabaseReaderT m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
xpub
    getNumTxData :: Word64 -> ReaderT WebState m [TxData]
getNumTxData = CacheT (DatabaseReaderT m) [TxData] -> ReaderT WebState m [TxData]
forall (m :: * -> *) a.
MonadIO m =>
CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader (CacheT (DatabaseReaderT m) [TxData]
 -> ReaderT WebState m [TxData])
-> (Word64 -> CacheT (DatabaseReaderT m) [TxData])
-> Word64
-> ReaderT WebState m [TxData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> CacheT (DatabaseReaderT m) [TxData]
forall (m :: * -> *). StoreReadExtra m => Word64 -> m [TxData]
getNumTxData

instance (MonadUnliftIO m, MonadLoggerIO m) => StoreReadBase (WebT m) where
    getNetwork :: WebT m Network
getNetwork = ReaderT WebState m Network -> WebT m Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Network
forall (m :: * -> *). StoreReadBase m => m Network
getNetwork
    getBestBlock :: WebT m (Maybe BlockHash)
getBestBlock = ReaderT WebState m (Maybe BlockHash) -> WebT m (Maybe BlockHash)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
    getBlocksAtHeight :: Word32 -> WebT m [BlockHash]
getBlocksAtHeight = ReaderT WebState m [BlockHash] -> WebT m [BlockHash]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [BlockHash] -> WebT m [BlockHash])
-> (Word32 -> ReaderT WebState m [BlockHash])
-> Word32
-> WebT m [BlockHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> ReaderT WebState m [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight
    getBlock :: BlockHash -> WebT m (Maybe BlockData)
getBlock = ReaderT WebState m (Maybe BlockData) -> WebT m (Maybe BlockData)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe BlockData) -> WebT m (Maybe BlockData))
-> (BlockHash -> ReaderT WebState m (Maybe BlockData))
-> BlockHash
-> WebT m (Maybe BlockData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHash -> ReaderT WebState m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock
    getTxData :: TxHash -> WebT m (Maybe TxData)
getTxData = ReaderT WebState m (Maybe TxData) -> WebT m (Maybe TxData)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe TxData) -> WebT m (Maybe TxData))
-> (TxHash -> ReaderT WebState m (Maybe TxData))
-> TxHash
-> WebT m (Maybe TxData)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxHash -> ReaderT WebState m (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData
    getSpender :: OutPoint -> WebT m (Maybe Spender)
getSpender = ReaderT WebState m (Maybe Spender) -> WebT m (Maybe Spender)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Spender) -> WebT m (Maybe Spender))
-> (OutPoint -> ReaderT WebState m (Maybe Spender))
-> OutPoint
-> WebT m (Maybe Spender)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutPoint -> ReaderT WebState m (Maybe Spender)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Spender)
getSpender
    getUnspent :: OutPoint -> WebT m (Maybe Unspent)
getUnspent = ReaderT WebState m (Maybe Unspent) -> WebT m (Maybe Unspent)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Unspent) -> WebT m (Maybe Unspent))
-> (OutPoint -> ReaderT WebState m (Maybe Unspent))
-> OutPoint
-> WebT m (Maybe Unspent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OutPoint -> ReaderT WebState m (Maybe Unspent)
forall (m :: * -> *).
StoreReadBase m =>
OutPoint -> m (Maybe Unspent)
getUnspent
    getBalance :: Address -> WebT m (Maybe Balance)
getBalance = ReaderT WebState m (Maybe Balance) -> WebT m (Maybe Balance)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Maybe Balance) -> WebT m (Maybe Balance))
-> (Address -> ReaderT WebState m (Maybe Balance))
-> Address
-> WebT m (Maybe Balance)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> ReaderT WebState m (Maybe Balance)
forall (m :: * -> *).
StoreReadBase m =>
Address -> m (Maybe Balance)
getBalance
    getMempool :: WebT m [(Word64, TxHash)]
getMempool = ReaderT WebState m [(Word64, TxHash)] -> WebT m [(Word64, TxHash)]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

instance (MonadUnliftIO m, MonadLoggerIO m) => StoreReadExtra (WebT m) where
    getBalances :: [Address] -> WebT m [Balance]
getBalances = ReaderT WebState m [Balance] -> WebT m [Balance]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Balance] -> WebT m [Balance])
-> ([Address] -> ReaderT WebState m [Balance])
-> [Address]
-> WebT m [Balance]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> ReaderT WebState m [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances
    getAddressesTxs :: [Address] -> Limits -> WebT m [TxRef]
getAddressesTxs as :: [Address]
as = ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (Limits -> ReaderT WebState m [TxRef])
-> Limits
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
as
    getAddressesUnspents :: [Address] -> Limits -> WebT m [Unspent]
getAddressesUnspents as :: [Address]
as = ReaderT WebState m [Unspent] -> WebT m [Unspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Unspent] -> WebT m [Unspent])
-> (Limits -> ReaderT WebState m [Unspent])
-> Limits
-> WebT m [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> Limits -> ReaderT WebState m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
as
    xPubBals :: XPubSpec -> WebT m [XPubBal]
xPubBals = ReaderT WebState m [XPubBal] -> WebT m [XPubBal]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubBal] -> WebT m [XPubBal])
-> (XPubSpec -> ReaderT WebState m [XPubBal])
-> XPubSpec
-> WebT m [XPubBal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> ReaderT WebState m [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals
    xPubSummary :: XPubSpec -> WebT m XPubSummary
xPubSummary = ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> (XPubSpec -> ReaderT WebState m XPubSummary)
-> XPubSpec
-> WebT m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> ReaderT WebState m XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary
    xPubUnspents :: XPubSpec -> Limits -> WebT m [XPubUnspent]
xPubUnspents xpub :: XPubSpec
xpub = ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> (Limits -> ReaderT WebState m [XPubUnspent])
-> Limits
-> WebT m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents XPubSpec
xpub
    xPubTxs :: XPubSpec -> Limits -> WebT m [TxRef]
xPubTxs xpub :: XPubSpec
xpub = ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (Limits -> ReaderT WebState m [TxRef])
-> Limits
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubSpec -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
xpub
    getMaxGap :: WebT m Word32
getMaxGap = ReaderT WebState m Word32 -> WebT m Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getMaxGap
    getInitialGap :: WebT m Word32
getInitialGap = ReaderT WebState m Word32 -> WebT m Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT WebState m Word32
forall (m :: * -> *). StoreReadExtra m => m Word32
getInitialGap
    getNumTxData :: Word64 -> WebT m [TxData]
getNumTxData = ReaderT WebState m [TxData] -> WebT m [TxData]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxData] -> WebT m [TxData])
-> (Word64 -> ReaderT WebState m [TxData])
-> Word64
-> WebT m [TxData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> ReaderT WebState m [TxData]
forall (m :: * -> *). StoreReadExtra m => Word64 -> m [TxData]
getNumTxData

-------------------
-- Path Handlers --
-------------------

runWeb :: (MonadUnliftIO m, MonadLoggerIO m) => WebConfig -> m ()
runWeb :: WebConfig -> m ()
runWeb cfg :: WebConfig
cfg@WebConfig{ webHost :: WebConfig -> String
webHost = String
host
                    , webPort :: WebConfig -> Int
webPort = Int
port
                    , webStore :: WebConfig -> Store
webStore = Store
store
                    , webStats :: WebConfig -> Maybe Store
webStats = Maybe Store
stats
                    } = do
    TVar (HashMap Text BinfoTicker)
ticker <- HashMap Text BinfoTicker -> m (TVar (HashMap Text BinfoTicker))
forall (m :: * -> *) a. MonadIO m => a -> m (TVar a)
newTVarIO HashMap Text BinfoTicker
forall k v. HashMap k v
HashMap.empty
    Maybe WebMetrics
metrics <- (Store -> m WebMetrics) -> Maybe Store -> m (Maybe WebMetrics)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Store -> m WebMetrics
forall (m :: * -> *). MonadIO m => Store -> m WebMetrics
createMetrics Maybe Store
stats
    let st :: WebState
st = $WWebState :: WebConfig
-> TVar (HashMap Text BinfoTicker) -> Maybe WebMetrics -> WebState
WebState { webConfig :: WebConfig
webConfig = WebConfig
cfg, webTicker :: TVar (HashMap Text BinfoTicker)
webTicker = TVar (HashMap Text BinfoTicker)
ticker, webMetrics :: Maybe WebMetrics
webMetrics = Maybe WebMetrics
metrics }
    m () -> (Async () -> m ()) -> m ()
forall (m :: * -> *) a b.
MonadUnliftIO m =>
m a -> (Async a -> m b) -> m b
withAsync (Network -> TVar (HashMap Text BinfoTicker) -> m ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Network -> TVar (HashMap Text BinfoTicker) -> m ()
price (Store -> Network
storeNetwork Store
store) TVar (HashMap Text BinfoTicker)
ticker) ((Async () -> m ()) -> m ()) -> (Async () -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \_ -> do
        Middleware
reqLogger <- m Middleware
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
m Middleware
logIt
        m Response -> IO Response
runner <- m (m Response -> IO Response)
forall (m :: * -> *) a. MonadUnliftIO m => m (m a -> IO a)
askRunInIO
        Options
-> (ReaderT WebState m Response -> IO Response)
-> ScottyT Except (ReaderT WebState m) ()
-> m ()
forall (m :: * -> *) (n :: * -> *) e.
(Monad m, MonadIO n) =>
Options -> (m Response -> IO Response) -> ScottyT e m () -> n ()
S.scottyOptsT Options
opts (m Response -> IO Response
runner (m Response -> IO Response)
-> (ReaderT WebState m Response -> m Response)
-> ReaderT WebState m Response
-> IO Response
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ReaderT WebState m Response -> WebState -> m Response
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
`runReaderT` WebState
st)) (ScottyT Except (ReaderT WebState m) () -> m ())
-> ScottyT Except (ReaderT WebState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ do
            Middleware -> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *). Middleware -> ScottyT e m ()
S.middleware Middleware
reqLogger
            (Except -> ActionT Except (ReaderT WebState m) ())
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
(e -> ActionT e m ()) -> ScottyT e m ()
S.defaultHandler Except -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *). Monad m => Except -> WebT m ()
defHandler
            ScottyT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
ScottyT Except (ReaderT WebState m) ()
handlePaths
            ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
ActionT e m () -> ScottyT e m ()
S.notFound (ActionT Except (ReaderT WebState m) ()
 -> ScottyT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ Except -> ActionT Except (ReaderT WebState m) ()
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
ThingNotFound
  where
    opts :: Options
opts = Options
forall a. Default a => a
def {settings :: Settings
S.settings = Settings -> Settings
settings Settings
defaultSettings}
    settings :: Settings -> Settings
settings = Int -> Settings -> Settings
setPort Int
port (Settings -> Settings)
-> (Settings -> Settings) -> Settings -> Settings
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HostPreference -> Settings -> Settings
setHost (String -> HostPreference
forall a. IsString a => String -> a
fromString String
host)

price :: (MonadUnliftIO m, MonadLoggerIO m)
      => Network
      -> TVar (HashMap Text BinfoTicker)
      -> m ()
price :: Network -> TVar (HashMap Text BinfoTicker) -> m ()
price net :: Network
net v :: TVar (HashMap Text BinfoTicker)
v =
    case Maybe String
code of
        Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just s :: String
s  -> String -> m ()
forall (f :: * -> *) b.
(MonadUnliftIO f, MonadLogger f) =>
String -> f b
go String
s
  where
    code :: Maybe String
code | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
btc = String -> Maybe String
forall a. a -> Maybe a
Just "btc"
         | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
bch = String -> Maybe String
forall a. a -> Maybe a
Just "bch"
         | Bool
otherwise = Maybe String
forall a. Maybe a
Nothing
    go :: String -> f b
go s :: String
s = f () -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (f () -> f b) -> f () -> f b
forall a b. (a -> b) -> a -> b
$ do
        let err :: a -> m ()
err e :: a
e = $(Text
LogLevel
String -> String -> String -> CharPos -> CharPos -> Loc
Loc -> Text -> LogLevel -> Text -> m ()
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
b :: Text
a :: Text
logErrorS) "Price" (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (a -> String
forall a. Show a => a -> String
show a
e)
            url :: String
url = "https://api.blockchain.info/ticker" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "?" String -> ShowS
forall a. Semigroup a => a -> a -> a
<>
                  "base" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> "=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
s
        (SomeException -> f ()) -> f () -> f ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(SomeException -> m a) -> m a -> m a
handleAny SomeException -> f ()
forall (m :: * -> *) a. (MonadLogger m, Show a) => a -> m ()
err (f () -> f ()) -> f () -> f ()
forall a b. (a -> b) -> a -> b
$ do
            Response (HashMap Text BinfoTicker)
r <- IO (Response (HashMap Text BinfoTicker))
-> f (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Response (HashMap Text BinfoTicker))
 -> f (Response (HashMap Text BinfoTicker)))
-> IO (Response (HashMap Text BinfoTicker))
-> f (Response (HashMap Text BinfoTicker))
forall a b. (a -> b) -> a -> b
$ Response ByteString -> IO (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a.
(MonadThrow m, FromJSON a) =>
Response ByteString -> m (Response a)
Wreq.asJSON (Response ByteString -> IO (Response (HashMap Text BinfoTicker)))
-> IO (Response ByteString)
-> IO (Response (HashMap Text BinfoTicker))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> IO (Response ByteString)
Wreq.get String
url
            STM () -> f ()
forall (m :: * -> *) a. MonadIO m => STM a -> m a
atomically (STM () -> f ())
-> (HashMap Text BinfoTicker -> STM ())
-> HashMap Text BinfoTicker
-> f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar (HashMap Text BinfoTicker)
-> HashMap Text BinfoTicker -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (HashMap Text BinfoTicker)
v (HashMap Text BinfoTicker -> f ())
-> HashMap Text BinfoTicker -> f ()
forall a b. (a -> b) -> a -> b
$ Response (HashMap Text BinfoTicker)
r Response (HashMap Text BinfoTicker)
-> Getting
     (HashMap Text BinfoTicker)
     (Response (HashMap Text BinfoTicker))
     (HashMap Text BinfoTicker)
-> HashMap Text BinfoTicker
forall s a. s -> Getting a s a -> a
^. Getting
  (HashMap Text BinfoTicker)
  (Response (HashMap Text BinfoTicker))
  (HashMap Text BinfoTicker)
forall body0 body1.
Lens (Response body0) (Response body1) body0 body1
Wreq.responseBody
        Int -> f ()
forall (m :: * -> *). MonadIO m => Int -> m ()
threadDelay (Int -> f ()) -> Int -> f ()
forall a b. (a -> b) -> a -> b
$ 5 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 -- five minutes

raise_ :: MonadIO m => Except -> WebT m a
raise_ :: Except -> WebT m a
raise_ err :: Except
err =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Nothing -> Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err
    Just metrics :: WebMetrics
metrics -> do
        let status :: Status
status = Except -> Status
errStatus Except
err
        if | Status -> Bool
statusIsClientError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
           | Status -> Bool
statusIsServerError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
           | Bool
otherwise ->
                 () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err

raise :: MonadIO m => (WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise :: (WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise metric :: WebMetrics -> ErrorCounter
metric err :: Except
err =
    ReaderT WebState m (Maybe WebMetrics)
-> ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Maybe WebMetrics)
-> ReaderT WebState m (Maybe WebMetrics)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> Maybe WebMetrics
webMetrics) ActionT Except (ReaderT WebState m) (Maybe WebMetrics)
-> (Maybe WebMetrics -> WebT m a) -> WebT m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Nothing -> Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err
    Just metrics :: WebMetrics
metrics -> do
        let status :: Status
status = Except -> Status
errStatus Except
err
        if | Status -> Bool
statusIsClientError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
clientErrors (WebMetrics -> ErrorCounter
metric WebMetrics
metrics))
           | Status -> Bool
statusIsServerError Status
status -> IO () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ActionT Except (ReaderT WebState m) ())
-> IO () -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
everyError WebMetrics
metrics))
                 Counter -> IO ()
Metrics.Counter.inc (ErrorCounter -> Counter
serverErrors (WebMetrics -> ErrorCounter
metric WebMetrics
metrics))
           | Bool
otherwise ->
                 () -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Except -> WebT m a
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
e -> ActionT e m a
S.raise Except
err

errStatus :: Except -> Status
errStatus :: Except -> Status
errStatus ThingNotFound     = Status
status404
errStatus BadRequest        = Status
status400
errStatus UserError{}       = Status
status400
errStatus StringError{}     = Status
status400
errStatus ServerError       = Status
status500
errStatus BlockTooLarge     = Status
status403
errStatus TxIndexConflict{} = Status
status409

defHandler :: Monad m => Except -> WebT m ()
defHandler :: Except -> WebT m ()
defHandler e :: Except
e = do
    SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
False
    Status -> WebT m ()
forall (m :: * -> *) e. Monad m => Status -> ActionT e m ()
S.status (Status -> WebT m ()) -> Status -> WebT m ()
forall a b. (a -> b) -> a -> b
$ Except -> Status
errStatus Except
e
    ByteString -> WebT m ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (ByteString -> WebT m ()) -> ByteString -> WebT m ()
forall a b. (a -> b) -> a -> b
$ SerialAs
-> (Except -> Encoding)
-> (Except -> Value)
-> Except
-> ByteString
forall a.
Serialize a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto Except -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding Except -> Value
forall a. ToJSON a => a -> Value
toJSON Except
e

handlePaths :: (MonadUnliftIO m, MonadLoggerIO m)
            => S.ScottyT Except (ReaderT WebState m) ()
handlePaths :: ScottyT Except (ReaderT WebState m) ()
handlePaths = do
    -- Block Paths
    WebT m GetBlock
-> (GetBlock -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (BlockHash -> NoTx -> GetBlock
GetBlock (BlockHash -> NoTx -> GetBlock)
-> ActionT Except (ReaderT WebState m) BlockHash
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlock)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlock)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlock
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlock -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlock -> WebT m BlockData
scottyBlock
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlocks
-> (GetBlocks -> WebT m [BlockData])
-> (Network -> [BlockData] -> Encoding)
-> (Network -> [BlockData] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([BlockHash] -> NoTx -> GetBlocks
GetBlocks ([BlockHash] -> NoTx -> GetBlocks)
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlocks)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [BlockHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (NoTx -> GetBlocks)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlocks
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlocks -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlocks -> WebT m [BlockData]
scottyBlocks
        ((BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((BlockData -> Encoding) -> [BlockData] -> Encoding)
-> (Network -> BlockData -> Encoding)
-> Network
-> [BlockData]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> BlockData -> Encoding
blockDataToEncoding)
        ((Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON)
    WebT m GetBlockRaw
-> (GetBlockRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetBlockRaw
GetBlockRaw (BlockHash -> GetBlockRaw)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetBlockRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockRaw -> WebT m (RawResult Block)
scottyBlockRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockBest
-> (GetBlockBest -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (NoTx -> GetBlockBest
GetBlockBest (NoTx -> GetBlockBest)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockBest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockBest -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockBest -> WebT m BlockData
scottyBlockBest
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockBestRaw
-> (GetBlockBestRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (GetBlockBestRaw
GetBlockBestRaw GetBlockBestRaw
-> (GetBlockBestRaw -> WebT m GetBlockBestRaw)
-> WebT m GetBlockBestRaw
forall a b. a -> (a -> b) -> b
& GetBlockBestRaw -> WebT m GetBlockBestRaw
forall (m :: * -> *) a. Monad m => a -> m a
return)
        GetBlockBestRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockBestRaw -> WebT m (RawResult Block)
scottyBlockBestRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockLatest
-> (GetBlockLatest -> WebT m [BlockData])
-> (Network -> [BlockData] -> Encoding)
-> (Network -> [BlockData] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (NoTx -> GetBlockLatest
GetBlockLatest (NoTx -> GetBlockLatest)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockLatest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockLatest -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockLatest -> WebT m [BlockData]
scottyBlockLatest
        ((BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((BlockData -> Encoding) -> [BlockData] -> Encoding)
-> (Network -> BlockData -> Encoding)
-> Network
-> [BlockData]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> BlockData -> Encoding
blockDataToEncoding)
        ((Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON)
    WebT m GetBlockHeight
-> (GetBlockHeight -> WebT m [BlockData])
-> (Network -> [BlockData] -> Encoding)
-> (Network -> [BlockData] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (HeightParam -> NoTx -> GetBlockHeight
GetBlockHeight (HeightParam -> NoTx -> GetBlockHeight)
-> ActionT Except (ReaderT WebState m) HeightParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeight)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeight)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockHeight
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockHeight -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight
        ((BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((BlockData -> Encoding) -> [BlockData] -> Encoding)
-> (Network -> BlockData -> Encoding)
-> Network
-> [BlockData]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> BlockData -> Encoding
blockDataToEncoding)
        ((Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON)
    WebT m GetBlockHeights
-> (GetBlockHeights -> WebT m [BlockData])
-> (Network -> [BlockData] -> Encoding)
-> (Network -> [BlockData] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (HeightsParam -> NoTx -> GetBlockHeights
GetBlockHeights (HeightsParam -> NoTx -> GetBlockHeights)
-> ActionT Except (ReaderT WebState m) HeightsParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeights)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightsParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (NoTx -> GetBlockHeights)
-> ActionT Except (ReaderT WebState m) NoTx
-> WebT m GetBlockHeights
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockHeights -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeights -> WebT m [BlockData]
scottyBlockHeights
        ((BlockData -> Encoding) -> [BlockData] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((BlockData -> Encoding) -> [BlockData] -> Encoding)
-> (Network -> BlockData -> Encoding)
-> Network
-> [BlockData]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> BlockData -> Encoding
blockDataToEncoding)
        ((Network -> BlockData -> Value) -> Network -> [BlockData] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> BlockData -> Value
blockDataToJSON)
    WebT m GetBlockHeightRaw
-> (GetBlockHeightRaw -> WebT m (RawResultList Block))
-> (Network -> RawResultList Block -> Encoding)
-> (Network -> RawResultList Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (HeightParam -> GetBlockHeightRaw
GetBlockHeightRaw (HeightParam -> GetBlockHeightRaw)
-> ActionT Except (ReaderT WebState m) HeightParam
-> WebT m GetBlockHeightRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockHeightRaw -> WebT m (RawResultList Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockHeightRaw -> WebT m (RawResultList Block)
scottyBlockHeightRaw
        ((RawResultList Block -> Encoding)
-> Network -> RawResultList Block -> Encoding
forall a b. a -> b -> a
const RawResultList Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Block -> Value)
-> Network -> RawResultList Block -> Value
forall a b. a -> b -> a
const RawResultList Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockTime
-> (GetBlockTime -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TimeParam -> NoTx -> GetBlockTime
GetBlockTime (TimeParam -> NoTx -> GetBlockTime)
-> ActionT Except (ReaderT WebState m) TimeParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockTime)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockTime
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockTime -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockTime -> WebT m BlockData
scottyBlockTime
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockTimeRaw
-> (GetBlockTimeRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TimeParam -> GetBlockTimeRaw
GetBlockTimeRaw (TimeParam -> GetBlockTimeRaw)
-> ActionT Except (ReaderT WebState m) TimeParam
-> WebT m GetBlockTimeRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockTimeRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockTimeRaw -> WebT m (RawResult Block)
scottyBlockTimeRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetBlockMTP
-> (GetBlockMTP -> WebT m BlockData)
-> (Network -> BlockData -> Encoding)
-> (Network -> BlockData -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TimeParam -> NoTx -> GetBlockMTP
GetBlockMTP (TimeParam -> NoTx -> GetBlockMTP)
-> ActionT Except (ReaderT WebState m) TimeParam
-> ActionT Except (ReaderT WebState m) (NoTx -> GetBlockMTP)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (NoTx -> GetBlockMTP)
-> ActionT Except (ReaderT WebState m) NoTx -> WebT m GetBlockMTP
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoTx
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetBlockMTP -> WebT m BlockData
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockMTP -> WebT m BlockData
scottyBlockMTP
        Network -> BlockData -> Encoding
blockDataToEncoding
        Network -> BlockData -> Value
blockDataToJSON
    WebT m GetBlockMTPRaw
-> (GetBlockMTPRaw -> WebT m (RawResult Block))
-> (Network -> RawResult Block -> Encoding)
-> (Network -> RawResult Block -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TimeParam -> GetBlockMTPRaw
GetBlockMTPRaw (TimeParam -> GetBlockMTPRaw)
-> ActionT Except (ReaderT WebState m) TimeParam
-> WebT m GetBlockMTPRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TimeParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetBlockMTPRaw -> WebT m (RawResult Block)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetBlockMTPRaw -> WebT m (RawResult Block)
scottyBlockMTPRaw
        ((RawResult Block -> Encoding)
-> Network -> RawResult Block -> Encoding
forall a b. a -> b -> a
const RawResult Block -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Block -> Value) -> Network -> RawResult Block -> Value
forall a b. a -> b -> a
const RawResult Block -> Value
forall a. ToJSON a => a -> Value
toJSON)
    -- Transaction Paths
    WebT m GetTx
-> (GetTx -> WebT m Transaction)
-> (Network -> Transaction -> Encoding)
-> (Network -> Transaction -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (TxHash -> GetTx
GetTx (TxHash -> GetTx)
-> ActionT Except (ReaderT WebState m) TxHash -> WebT m GetTx
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTx -> WebT m Transaction
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTx -> WebT m Transaction
scottyTx
        Network -> Transaction -> Encoding
transactionToEncoding
        Network -> Transaction -> Value
transactionToJSON
    WebT m GetTxs
-> (GetTxs -> WebT m [Transaction])
-> (Network -> [Transaction] -> Encoding)
-> (Network -> [Transaction] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([TxHash] -> GetTxs
GetTxs ([TxHash] -> GetTxs)
-> ActionT Except (ReaderT WebState m) [TxHash] -> WebT m GetTxs
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [TxHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        GetTxs -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxs -> WebT m [Transaction]
scottyTxs
        ((Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Transaction -> Encoding) -> [Transaction] -> Encoding)
-> (Network -> Transaction -> Encoding)
-> Network
-> [Transaction]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Transaction -> Encoding
transactionToEncoding)
        ((Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON)
    WebT m GetTxRaw
-> (GetTxRaw -> WebT m (RawResult Tx))
-> (Network -> RawResult Tx -> Encoding)
-> (Network -> RawResult Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TxHash -> GetTxRaw
GetTxRaw (TxHash -> GetTxRaw)
-> ActionT Except (ReaderT WebState m) TxHash -> WebT m GetTxRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxRaw -> WebT m (RawResult Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw
        ((RawResult Tx -> Encoding) -> Network -> RawResult Tx -> Encoding
forall a b. a -> b -> a
const RawResult Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResult Tx -> Value) -> Network -> RawResult Tx -> Value
forall a b. a -> b -> a
const RawResult Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxsRaw
-> (GetTxsRaw -> WebT m (RawResultList Tx))
-> (Network -> RawResultList Tx -> Encoding)
-> (Network -> RawResultList Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([TxHash] -> GetTxsRaw
GetTxsRaw ([TxHash] -> GetTxsRaw)
-> ActionT Except (ReaderT WebState m) [TxHash] -> WebT m GetTxsRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [TxHash]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        GetTxsRaw -> WebT m (RawResultList Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsRaw -> WebT m (RawResultList Tx)
scottyTxsRaw
        ((RawResultList Tx -> Encoding)
-> Network -> RawResultList Tx -> Encoding
forall a b. a -> b -> a
const RawResultList Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Tx -> Value) -> Network -> RawResultList Tx -> Value
forall a b. a -> b -> a
const RawResultList Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxsBlock
-> (GetTxsBlock -> WebT m [Transaction])
-> (Network -> [Transaction] -> Encoding)
-> (Network -> [Transaction] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetTxsBlock
GetTxsBlock (BlockHash -> GetTxsBlock)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetTxsBlock
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxsBlock -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock
        ((Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Transaction -> Encoding) -> [Transaction] -> Encoding)
-> (Network -> Transaction -> Encoding)
-> Network
-> [Transaction]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Transaction -> Encoding
transactionToEncoding)
        ((Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON)
    WebT m GetTxsBlockRaw
-> (GetTxsBlockRaw -> WebT m (RawResultList Tx))
-> (Network -> RawResultList Tx -> Encoding)
-> (Network -> RawResultList Tx -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (BlockHash -> GetTxsBlockRaw
GetTxsBlockRaw (BlockHash -> GetTxsBlockRaw)
-> ActionT Except (ReaderT WebState m) BlockHash
-> WebT m GetTxsBlockRaw
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) BlockHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxsBlockRaw -> WebT m (RawResultList Tx)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxsBlockRaw -> WebT m (RawResultList Tx)
scottyTxsBlockRaw
        ((RawResultList Tx -> Encoding)
-> Network -> RawResultList Tx -> Encoding
forall a b. a -> b -> a
const RawResultList Tx -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((RawResultList Tx -> Value) -> Network -> RawResultList Tx -> Value
forall a b. a -> b -> a
const RawResultList Tx -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetTxAfter
-> (GetTxAfter -> WebT m (GenericResult (Maybe Bool)))
-> (Network -> GenericResult (Maybe Bool) -> Encoding)
-> (Network -> GenericResult (Maybe Bool) -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (TxHash -> HeightParam -> GetTxAfter
GetTxAfter (TxHash -> HeightParam -> GetTxAfter)
-> ActionT Except (ReaderT WebState m) TxHash
-> ActionT Except (ReaderT WebState m) (HeightParam -> GetTxAfter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) TxHash
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (HeightParam -> GetTxAfter)
-> ActionT Except (ReaderT WebState m) HeightParam
-> WebT m GetTxAfter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) HeightParam
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetTxAfter -> WebT m (GenericResult (Maybe Bool))
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetTxAfter -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter
        ((GenericResult (Maybe Bool) -> Encoding)
-> Network -> GenericResult (Maybe Bool) -> Encoding
forall a b. a -> b -> a
const GenericResult (Maybe Bool) -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((GenericResult (Maybe Bool) -> Value)
-> Network -> GenericResult (Maybe Bool) -> Value
forall a b. a -> b -> a
const GenericResult (Maybe Bool) -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m PostTx
-> (PostTx -> WebT m TxId)
-> (Network -> TxId -> Encoding)
-> (Network -> TxId -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (Tx -> PostTx
PostTx (Tx -> PostTx)
-> ActionT Except (ReaderT WebState m) Tx -> WebT m PostTx
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Tx
forall (m :: * -> *) a. (MonadIO m, Serialize a) => WebT m a
parseBody)
        PostTx -> WebT m TxId
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
PostTx -> WebT m TxId
scottyPostTx
        ((TxId -> Encoding) -> Network -> TxId -> Encoding
forall a b. a -> b -> a
const TxId -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((TxId -> Value) -> Network -> TxId -> Value
forall a b. a -> b -> a
const TxId -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetMempool
-> (GetMempool -> ActionT Except (ReaderT WebState m) [TxHash])
-> (Network -> [TxHash] -> Encoding)
-> (Network -> [TxHash] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Maybe LimitParam -> OffsetParam -> GetMempool
GetMempool (Maybe LimitParam -> OffsetParam -> GetMempool)
-> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
-> ActionT Except (ReaderT WebState m) (OffsetParam -> GetMempool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional ActionT Except (ReaderT WebState m) (OffsetParam -> GetMempool)
-> ActionT Except (ReaderT WebState m) OffsetParam
-> WebT m GetMempool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) OffsetParam
forall (m :: * -> *). MonadIO m => WebT m OffsetParam
parseOffset)
        GetMempool -> ActionT Except (ReaderT WebState m) [TxHash]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetMempool -> WebT m [TxHash]
scottyMempool
        (([TxHash] -> Encoding) -> Network -> [TxHash] -> Encoding
forall a b. a -> b -> a
const [TxHash] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        (([TxHash] -> Value) -> Network -> [TxHash] -> Value
forall a b. a -> b -> a
const [TxHash] -> Value
forall a. ToJSON a => a -> Value
toJSON)
    -- Address Paths
    WebT m GetAddrTxs
-> (GetAddrTxs -> WebT m [TxRef])
-> (Network -> [TxRef] -> Encoding)
-> (Network -> [TxRef] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> LimitsParam -> GetAddrTxs
GetAddrTxs (Address -> LimitsParam -> GetAddrTxs)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrTxs -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs
        (([TxRef] -> Encoding) -> Network -> [TxRef] -> Encoding
forall a b. a -> b -> a
const [TxRef] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        (([TxRef] -> Value) -> Network -> [TxRef] -> Value
forall a b. a -> b -> a
const [TxRef] -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetAddrsTxs
-> (GetAddrsTxs -> WebT m [TxRef])
-> (Network -> [TxRef] -> Encoding)
-> (Network -> [TxRef] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsTxs
GetAddrsTxs ([Address] -> LimitsParam -> GetAddrsTxs)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrsTxs -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs
        (([TxRef] -> Encoding) -> Network -> [TxRef] -> Encoding
forall a b. a -> b -> a
const [TxRef] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        (([TxRef] -> Value) -> Network -> [TxRef] -> Value
forall a b. a -> b -> a
const [TxRef] -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetAddrTxsFull
-> (GetAddrTxsFull -> WebT m [Transaction])
-> (Network -> [Transaction] -> Encoding)
-> (Network -> [Transaction] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (Address -> LimitsParam -> GetAddrTxsFull
GetAddrTxsFull (Address -> LimitsParam -> GetAddrTxsFull)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrTxsFull -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrTxsFull -> WebT m [Transaction]
scottyAddrTxsFull
        ((Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Transaction -> Encoding) -> [Transaction] -> Encoding)
-> (Network -> Transaction -> Encoding)
-> Network
-> [Transaction]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Transaction -> Encoding
transactionToEncoding)
        ((Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON)
    WebT m GetAddrsTxsFull
-> (GetAddrsTxsFull -> WebT m [Transaction])
-> (Network -> [Transaction] -> Encoding)
-> (Network -> [Transaction] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsTxsFull
GetAddrsTxsFull ([Address] -> LimitsParam -> GetAddrsTxsFull)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT
  Except (ReaderT WebState m) (LimitsParam -> GetAddrsTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrsTxsFull -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull
        ((Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Transaction -> Encoding) -> [Transaction] -> Encoding)
-> (Network -> Transaction -> Encoding)
-> Network
-> [Transaction]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Transaction -> Encoding
transactionToEncoding)
        ((Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON)
    WebT m GetAddrBalance
-> (GetAddrBalance -> WebT m Balance)
-> (Network -> Balance -> Encoding)
-> (Network -> Balance -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> GetAddrBalance
GetAddrBalance (Address -> GetAddrBalance)
-> ActionT Except (ReaderT WebState m) Address
-> WebT m GetAddrBalance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy)
        GetAddrBalance -> WebT m Balance
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrBalance -> WebT m Balance
scottyAddrBalance
        Network -> Balance -> Encoding
balanceToEncoding
        Network -> Balance -> Value
balanceToJSON
    WebT m GetAddrsBalance
-> (GetAddrsBalance -> WebT m [Balance])
-> (Network -> [Balance] -> Encoding)
-> (Network -> [Balance] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> GetAddrsBalance
GetAddrsBalance ([Address] -> GetAddrsBalance)
-> ActionT Except (ReaderT WebState m) [Address]
-> WebT m GetAddrsBalance
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param)
        GetAddrsBalance -> WebT m [Balance]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance
        ((Balance -> Encoding) -> [Balance] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Balance -> Encoding) -> [Balance] -> Encoding)
-> (Network -> Balance -> Encoding)
-> Network
-> [Balance]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Balance -> Encoding
balanceToEncoding)
        ((Network -> Balance -> Value) -> Network -> [Balance] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Balance -> Value
balanceToJSON)
    WebT m GetAddrUnspent
-> (GetAddrUnspent -> WebT m [Unspent])
-> (Network -> [Unspent] -> Encoding)
-> (Network -> [Unspent] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (Address -> LimitsParam -> GetAddrUnspent
GetAddrUnspent (Address -> LimitsParam -> GetAddrUnspent)
-> ActionT Except (ReaderT WebState m) Address
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) Address
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT Except (ReaderT WebState m) (LimitsParam -> GetAddrUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrUnspent -> WebT m [Unspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent
        ((Unspent -> Encoding) -> [Unspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Unspent -> Encoding) -> [Unspent] -> Encoding)
-> (Network -> Unspent -> Encoding)
-> Network
-> [Unspent]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Unspent -> Encoding
unspentToEncoding)
        ((Network -> Unspent -> Value) -> Network -> [Unspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Unspent -> Value
unspentToJSON)
    WebT m GetAddrsUnspent
-> (GetAddrsUnspent -> WebT m [Unspent])
-> (Network -> [Unspent] -> Encoding)
-> (Network -> [Unspent] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        ([Address] -> LimitsParam -> GetAddrsUnspent
GetAddrsUnspent ([Address] -> LimitsParam -> GetAddrsUnspent)
-> ActionT Except (ReaderT WebState m) [Address]
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> GetAddrsUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [Address]
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
param ActionT
  Except (ReaderT WebState m) (LimitsParam -> GetAddrsUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> WebT m GetAddrsUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits)
        GetAddrsUnspent -> WebT m [Unspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent
        ((Unspent -> Encoding) -> [Unspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Unspent -> Encoding) -> [Unspent] -> Encoding)
-> (Network -> Unspent -> Encoding)
-> Network
-> [Unspent]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Unspent -> Encoding
unspentToEncoding)
        ((Network -> Unspent -> Value) -> Network -> [Unspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Unspent -> Value
unspentToJSON)
    -- XPubs
    WebT m GetXPub
-> (GetXPub -> WebT m XPubSummary)
-> (Network -> XPubSummary -> Encoding)
-> (Network -> XPubSummary -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> NoCache -> GetXPub
GetXPub (XPubKey -> DeriveType -> NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except (ReaderT WebState m) (DeriveType -> NoCache -> GetXPub)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except (ReaderT WebState m) (DeriveType -> NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPub)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT Except (ReaderT WebState m) (NoCache -> GetXPub)
-> ActionT Except (ReaderT WebState m) NoCache -> WebT m GetXPub
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPub -> WebT m XPubSummary
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPub -> WebT m XPubSummary
scottyXPub
        ((XPubSummary -> Encoding) -> Network -> XPubSummary -> Encoding
forall a b. a -> b -> a
const XPubSummary -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        ((XPubSummary -> Value) -> Network -> XPubSummary -> Value
forall a b. a -> b -> a
const XPubSummary -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetXPubTxs
-> (GetXPubTxs -> WebT m [TxRef])
-> (Network -> [TxRef] -> Encoding)
-> (Network -> [TxRef] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxs
GetXPubTxs (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except (ReaderT WebState m) (LimitsParam -> NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except (ReaderT WebState m) (LimitsParam -> NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxs)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxs)
-> ActionT Except (ReaderT WebState m) NoCache -> WebT m GetXPubTxs
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPubTxs -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs
        (([TxRef] -> Encoding) -> Network -> [TxRef] -> Encoding
forall a b. a -> b -> a
const [TxRef] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        (([TxRef] -> Value) -> Network -> [TxRef] -> Value
forall a b. a -> b -> a
const [TxRef] -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetXPubTxsFull
-> (GetXPubTxsFull -> WebT m [Transaction])
-> (Network -> [Transaction] -> Encoding)
-> (Network -> [Transaction] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull
GetXPubTxsFull (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except
     (ReaderT WebState m)
     (LimitsParam -> NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except
  (ReaderT WebState m)
  (LimitsParam -> NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxsFull)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubTxsFull)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubTxsFull
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPubTxsFull -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubTxsFull -> WebT m [Transaction]
scottyXPubTxsFull
        ((Transaction -> Encoding) -> [Transaction] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((Transaction -> Encoding) -> [Transaction] -> Encoding)
-> (Network -> Transaction -> Encoding)
-> Network
-> [Transaction]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> Transaction -> Encoding
transactionToEncoding)
        ((Network -> Transaction -> Value)
-> Network -> [Transaction] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> Transaction -> Value
transactionToJSON)
    WebT m GetXPubBalances
-> (GetXPubBalances -> WebT m [XPubBal])
-> (Network -> [XPubBal] -> Encoding)
-> (Network -> [XPubBal] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> NoCache -> GetXPubBalances
GetXPubBalances (XPubKey -> DeriveType -> NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> NoCache -> GetXPubBalances)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubBalances)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT Except (ReaderT WebState m) (NoCache -> GetXPubBalances)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubBalances
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPubBalances -> WebT m [XPubBal]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances
        ((XPubBal -> Encoding) -> [XPubBal] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((XPubBal -> Encoding) -> [XPubBal] -> Encoding)
-> (Network -> XPubBal -> Encoding)
-> Network
-> [XPubBal]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> XPubBal -> Encoding
xPubBalToEncoding)
        ((Network -> XPubBal -> Value) -> Network -> [XPubBal] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> XPubBal -> Value
xPubBalToJSON)
    WebT m GetXPubUnspent
-> (GetXPubUnspent -> WebT m [XPubUnspent])
-> (Network -> [XPubUnspent] -> Encoding)
-> (Network -> [XPubUnspent] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent
GetXPubUnspent (XPubKey -> DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) XPubKey
-> ActionT
     Except
     (ReaderT WebState m)
     (DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) XPubKey
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m a
paramLazy ActionT
  Except
  (ReaderT WebState m)
  (DeriveType -> LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) DeriveType
-> ActionT
     Except
     (ReaderT WebState m)
     (LimitsParam -> NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) DeriveType
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef ActionT
  Except
  (ReaderT WebState m)
  (LimitsParam -> NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) LimitsParam
-> ActionT Except (ReaderT WebState m) (NoCache -> GetXPubUnspent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) LimitsParam
forall (m :: * -> *). MonadIO m => WebT m LimitsParam
parseLimits ActionT Except (ReaderT WebState m) (NoCache -> GetXPubUnspent)
-> ActionT Except (ReaderT WebState m) NoCache
-> WebT m GetXPubUnspent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) NoCache
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef)
        GetXPubUnspent -> WebT m [XPubUnspent]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetXPubUnspent -> WebT m [XPubUnspent]
scottyXPubUnspent
        ((XPubUnspent -> Encoding) -> [XPubUnspent] -> Encoding
forall a. (a -> Encoding) -> [a] -> Encoding
list ((XPubUnspent -> Encoding) -> [XPubUnspent] -> Encoding)
-> (Network -> XPubUnspent -> Encoding)
-> Network
-> [XPubUnspent]
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Network -> XPubUnspent -> Encoding
xPubUnspentToEncoding)
        ((Network -> XPubUnspent -> Value)
-> Network -> [XPubUnspent] -> Value
forall a t a. ToJSON a => (t -> a -> a) -> t -> [a] -> Value
json_list Network -> XPubUnspent -> Value
xPubUnspentToJSON)
    -- Network
    WebT m GetPeers
-> (GetPeers -> WebT m [PeerInformation])
-> (Network -> [PeerInformation] -> Encoding)
-> (Network -> [PeerInformation] -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
        (GetPeers
GetPeers GetPeers -> (GetPeers -> WebT m GetPeers) -> WebT m GetPeers
forall a b. a -> (a -> b) -> b
& GetPeers -> WebT m GetPeers
forall (m :: * -> *) a. Monad m => a -> m a
return)
        GetPeers -> WebT m [PeerInformation]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetPeers -> WebT m [PeerInformation]
scottyPeers
        (([PeerInformation] -> Encoding)
-> Network -> [PeerInformation] -> Encoding
forall a b. a -> b -> a
const [PeerInformation] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
        (([PeerInformation] -> Value)
-> Network -> [PeerInformation] -> Value
forall a b. a -> b -> a
const [PeerInformation] -> Value
forall a. ToJSON a => a -> Value
toJSON)
    WebT m GetHealth
-> (GetHealth -> WebT m HealthCheck)
-> (Network -> HealthCheck -> Encoding)
-> (Network -> HealthCheck -> Value)
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty
         (GetHealth
GetHealth GetHealth -> (GetHealth -> WebT m GetHealth) -> WebT m GetHealth
forall a b. a -> (a -> b) -> b
& GetHealth -> WebT m GetHealth
forall (m :: * -> *) a. Monad m => a -> m a
return)
         GetHealth -> WebT m HealthCheck
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
GetHealth -> WebT m HealthCheck
scottyHealth
         ((HealthCheck -> Encoding) -> Network -> HealthCheck -> Encoding
forall a b. a -> b -> a
const HealthCheck -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding)
         ((HealthCheck -> Value) -> Network -> HealthCheck -> Value
forall a b. a -> b -> a
const HealthCheck -> Value
forall a. ToJSON a => a -> Value
toJSON)
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/events" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyEvents
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/dbstats" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyDbStats
    -- Blockchain.info
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.post "/blockchain/multiaddr" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyMultiAddr
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get  "/blockchain/multiaddr" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyMultiAddr
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.post "/blockchain/unspent" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoUnspent
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get  "/blockchain/unspent" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoUnspent
    RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
RoutePattern -> ActionT e m () -> ScottyT e m ()
S.get "/blockchain/rawtx/:txid" ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
WebT m ()
scottyBinfoTx
  where
    json_list :: (t -> a -> a) -> t -> [a] -> Value
json_list f :: t -> a -> a
f net :: t
net = [a] -> Value
forall a. ToJSON a => [a] -> Value
toJSONList ([a] -> Value) -> ([a] -> [a]) -> [a] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (t -> a -> a
f t
net)

pathPretty ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> S.ScottyT Except (ReaderT WebState m) ()
pathPretty :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathPretty parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue =
    WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon WebT m a
parser a -> WebT m b
action Network -> b -> Encoding
encJson Network -> b -> Value
encValue Bool
True

pathCompact ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> S.ScottyT Except (ReaderT WebState m) ()
pathCompact :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> ScottyT Except (ReaderT WebState m) ()
pathCompact parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue =
    WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
forall a b (m :: * -> *).
(ApiResource a b, MonadIO m) =>
WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon WebT m a
parser a -> WebT m b
action Network -> b -> Encoding
encJson Network -> b -> Value
encValue Bool
False

pathCommon ::
       (ApiResource a b, MonadIO m)
    => WebT m a
    -> (a -> WebT m b)
    -> (Network -> b -> Encoding)
    -> (Network -> b -> Value)
    -> Bool
    -> S.ScottyT Except (ReaderT WebState m) ()
pathCommon :: WebT m a
-> (a -> WebT m b)
-> (Network -> b -> Encoding)
-> (Network -> b -> Value)
-> Bool
-> ScottyT Except (ReaderT WebState m) ()
pathCommon parser :: WebT m a
parser action :: a -> WebT m b
action encJson :: Network -> b -> Encoding
encJson encValue :: Network -> b -> Value
encValue pretty :: Bool
pretty =
    StdMethod
-> RoutePattern
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
StdMethod -> RoutePattern -> ActionT e m () -> ScottyT e m ()
S.addroute (Proxy a -> StdMethod
forall a b. ApiResource a b => Proxy a -> StdMethod
resourceMethod Proxy a
proxy) (Proxy a -> RoutePattern
forall a b. ApiResource a b => Proxy a -> RoutePattern
capturePath Proxy a
proxy) (ActionT Except (ReaderT WebState m) ()
 -> ScottyT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ScottyT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ do
        ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
        SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
pretty
        Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        a
apiRes <- WebT m a
parser
        b
res <- a -> WebT m b
action a
apiRes
        ByteString -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (ByteString -> ActionT Except (ReaderT WebState m) ())
-> ByteString -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ SerialAs -> (b -> Encoding) -> (b -> Value) -> b -> ByteString
forall a.
Serialize a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto (Network -> b -> Encoding
encJson Network
net) (Network -> b -> Value
encValue Network
net) b
res
  where
    toProxy :: WebT m a -> Proxy a
    toProxy :: WebT m a -> Proxy a
toProxy = Proxy a -> WebT m a -> Proxy a
forall a b. a -> b -> a
const Proxy a
forall k (t :: k). Proxy t
Proxy
    proxy :: Proxy a
proxy = WebT m a -> Proxy a
forall (m :: * -> *) a. WebT m a -> Proxy a
toProxy WebT m a
parser

streamEncoding :: Monad m => Encoding -> WebT m ()
streamEncoding :: Encoding -> WebT m ()
streamEncoding e :: Encoding
e = do
   Text -> Text -> WebT m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/json; charset=utf-8"
   ByteString -> WebT m ()
forall (m :: * -> *) e. Monad m => ByteString -> ActionT e m ()
S.raw (Encoding -> ByteString
forall a. Encoding' a -> ByteString
encodingToLazyByteString Encoding
e)

protoSerial
    :: Serialize a
    => SerialAs
    -> (a -> Encoding)
    -> (a -> Value)
    -> a
    -> L.ByteString
protoSerial :: SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAsBinary _ _     = Put -> ByteString
runPutLazy (Put -> ByteString) -> (a -> Put) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Put
forall t. Serialize t => Putter t
put
protoSerial SerialAsJSON f :: a -> Encoding
f _       = Encoding -> ByteString
forall a. Encoding' a -> ByteString
encodingToLazyByteString (Encoding -> ByteString) -> (a -> Encoding) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
f
protoSerial SerialAsPrettyJSON _ g :: a -> Value
g =
    Config -> Value -> ByteString
forall a. ToJSON a => Config -> a -> ByteString
encodePretty' Config
defConfig {confTrailingNewline :: Bool
confTrailingNewline = Bool
True} (Value -> ByteString) -> (a -> Value) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
g

setHeaders :: (Monad m, S.ScottyError e) => ActionT e m ()
setHeaders :: ActionT e m ()
setHeaders = Text -> Text -> ActionT e m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Access-Control-Allow-Origin" "*"

setupContentType :: Monad m => Bool -> ActionT Except m SerialAs
setupContentType :: Bool -> ActionT Except m SerialAs
setupContentType pretty :: Bool
pretty = do
    Maybe Text
accept <- Text -> ActionT Except m (Maybe Text)
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
Text -> ActionT e m (Maybe Text)
S.header "accept"
    ActionT Except m SerialAs
-> (Text -> ActionT Except m SerialAs)
-> Maybe Text
-> ActionT Except m SerialAs
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ActionT Except m SerialAs
goJson Text -> ActionT Except m SerialAs
forall a. (Eq a, IsString a) => a -> ActionT Except m SerialAs
setType Maybe Text
accept
  where
    setType :: a -> ActionT Except m SerialAs
setType "application/octet-stream" = ActionT Except m SerialAs
goBinary
    setType _                          = ActionT Except m SerialAs
goJson
    goBinary :: ActionT Except m SerialAs
goBinary = do
        Text -> Text -> ActionT Except m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/octet-stream"
        SerialAs -> ActionT Except m SerialAs
forall (m :: * -> *) a. Monad m => a -> m a
return SerialAs
SerialAsBinary
    goJson :: ActionT Except m SerialAs
goJson = do
        Text -> Text -> ActionT Except m ()
forall (m :: * -> *) e. Monad m => Text -> Text -> ActionT e m ()
S.setHeader "Content-Type" "application/json"
        Bool
p <- Text -> ActionT Except m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "pretty" ActionT Except m Bool
-> (Except -> ActionT Except m Bool) -> ActionT Except m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except m Bool -> Except -> ActionT Except m Bool
forall a b. a -> b -> a
const (Bool -> ActionT Except m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
pretty)
        SerialAs -> ActionT Except m SerialAs
forall (m :: * -> *) a. Monad m => a -> m a
return (SerialAs -> ActionT Except m SerialAs)
-> SerialAs -> ActionT Except m SerialAs
forall a b. (a -> b) -> a -> b
$ if Bool
p then SerialAs
SerialAsPrettyJSON else SerialAs
SerialAsJSON

-- GET Block / GET Blocks --

scottyBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlock -> WebT m BlockData
scottyBlock :: GetBlock -> WebT m BlockData
scottyBlock (GetBlock h :: BlockHash
h (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
noTx) (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
    BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h

getBlocks :: (MonadUnliftIO m, MonadLoggerIO m)
          => [H.BlockHash]
          -> Bool
          -> WebT m [BlockData]
getBlocks :: [BlockHash] -> Bool -> WebT m [BlockData]
getBlocks hs :: [BlockHash]
hs notx :: Bool
notx =
    (Bool -> BlockData -> BlockData
pruneTx Bool
notx (BlockData -> BlockData) -> [BlockData] -> [BlockData]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ([BlockData] -> [BlockData])
-> ([Maybe BlockData] -> [BlockData])
-> [Maybe BlockData]
-> [BlockData]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe BlockData] -> [BlockData]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe BlockData] -> [BlockData])
-> ActionT Except (ReaderT WebState m) [Maybe BlockData]
-> WebT m [BlockData]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (BlockHash
 -> ActionT Except (ReaderT WebState m) (Maybe BlockData))
-> [BlockHash]
-> ActionT Except (ReaderT WebState m) [Maybe BlockData]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock ([BlockHash] -> [BlockHash]
forall a. Eq a => [a] -> [a]
nub [BlockHash]
hs)

scottyBlocks ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlocks -> WebT m [BlockData]
scottyBlocks :: GetBlocks -> WebT m [BlockData]
scottyBlocks (GetBlocks hs :: [BlockHash]
hs (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime ([BlockHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BlockHash]
hs) (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$ [BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
getBlocks [BlockHash]
hs Bool
notx

pruneTx :: Bool -> BlockData -> BlockData
pruneTx :: Bool -> BlockData -> BlockData
pruneTx False b :: BlockData
b = BlockData
b
pruneTx True b :: BlockData
b  = BlockData
b {blockDataTxs :: [TxHash]
blockDataTxs = Int -> [TxHash] -> [TxHash]
forall a. Int -> [a] -> [a]
take 1 (BlockData -> [TxHash]
blockDataTxs BlockData
b)}

-- GET BlockRaw --

scottyBlockRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockRaw
    -> WebT m (RawResult H.Block)
scottyBlockRaw :: GetBlockRaw -> WebT m (RawResult Block)
scottyBlockRaw (GetBlockRaw h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock BlockHash
h

getRawBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => H.BlockHash -> WebT m H.Block
getRawBlock :: BlockHash -> WebT m Block
getRawBlock h :: BlockHash
h = do
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h
    (WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
refuseLargeBlock WebMetrics -> ErrorCounter
blockErrors BlockData
b
    BlockData -> WebT m Block
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b

toRawBlock :: (Monad m, StoreReadBase m) => BlockData -> m H.Block
toRawBlock :: BlockData -> m Block
toRawBlock b :: BlockData
b = do
    let ths :: [TxHash]
ths = BlockData -> [TxHash]
blockDataTxs BlockData
b
    [Tx]
txs <- (Transaction -> Tx) -> [Transaction] -> [Tx]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> Tx
transactionData ([Transaction] -> [Tx])
-> ([Maybe Transaction] -> [Transaction])
-> [Maybe Transaction]
-> [Tx]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Tx]) -> m [Maybe Transaction] -> m [Tx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> m (Maybe Transaction))
-> [TxHash] -> m [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction [TxHash]
ths
    Block -> m Block
forall (m :: * -> *) a. Monad m => a -> m a
return $WBlock :: BlockHeader -> [Tx] -> Block
H.Block {blockHeader :: BlockHeader
H.blockHeader = BlockData -> BlockHeader
blockDataHeader BlockData
b, blockTxns :: [Tx]
H.blockTxns = [Tx]
txs}

refuseLargeBlock :: MonadIO m
                 => (WebMetrics -> ErrorCounter)
                 -> BlockData
                 -> WebT m ()
refuseLargeBlock :: (WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
refuseLargeBlock metric :: WebMetrics -> ErrorCounter
metric BlockData {blockDataTxs :: BlockData -> [TxHash]
blockDataTxs = [TxHash]
txs} = do
    WebLimits {maxLimitFull :: WebLimits -> Word32
maxLimitFull = Word32
f} <- ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m WebLimits
 -> ActionT Except (ReaderT WebState m) WebLimits)
-> ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall a b. (a -> b) -> a -> b
$ (WebState -> WebLimits) -> ReaderT WebState m WebLimits
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Bool -> WebT m () -> WebT m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([TxHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxHash]
txs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
f) (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ (WebMetrics -> ErrorCounter) -> Except -> WebT m ()
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
metric Except
BlockTooLarge

-- GET BlockBest / BlockBestRaw --

scottyBlockBest ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlockBest -> WebT m BlockData
scottyBlockBest :: GetBlockBest -> WebT m BlockData
scottyBlockBest (GetBlockBest (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$
    ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> (Maybe BlockHash -> WebT m BlockData) -> WebT m BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> (WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound
        Just bb :: BlockHash
bb -> BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bb ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> (Maybe BlockData -> WebT m BlockData) -> WebT m BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> (WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound
            Just b :: BlockData
b  -> BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData) -> BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ Bool -> BlockData -> BlockData
pruneTx Bool
notx BlockData
b

scottyBlockBestRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockBestRaw
    -> WebT m (RawResult H.Block)
scottyBlockBestRaw :: GetBlockBestRaw -> WebT m (RawResult Block)
scottyBlockBestRaw _ =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Block -> RawResult Block
forall a. a -> RawResult a
RawResult (ActionT Except (ReaderT WebState m) Block
 -> WebT m (RawResult Block))
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$
    ActionT Except (ReaderT WebState m) Block
-> (BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> Maybe BlockHash
-> ActionT Except (ReaderT WebState m) Block
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock (Maybe BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock

-- GET BlockLatest --

scottyBlockLatest ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockLatest
    -> WebT m [BlockData]
scottyBlockLatest :: GetBlockLatest -> WebT m [BlockData]
scottyBlockLatest (GetBlockLatest (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 100 (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$
    WebT m [BlockData]
-> (BlockHash -> WebT m [BlockData])
-> Maybe BlockHash
-> WebT m [BlockData]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) ([BlockData] -> Maybe BlockData -> WebT m [BlockData]
forall (m :: * -> *).
StoreReadBase m =>
[BlockData] -> Maybe BlockData -> m [BlockData]
go [] (Maybe BlockData -> WebT m [BlockData])
-> (BlockHash
    -> ActionT Except (ReaderT WebState m) (Maybe BlockData))
-> BlockHash
-> WebT m [BlockData]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock) (Maybe BlockHash -> WebT m [BlockData])
-> ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> WebT m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock
  where
    go :: [BlockData] -> Maybe BlockData -> m [BlockData]
go acc :: [BlockData]
acc Nothing = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return [BlockData]
acc
    go acc :: [BlockData]
acc (Just b :: BlockData
b)
        | BlockData -> Word32
blockDataHeight BlockData
b Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return [BlockData]
acc
        | [BlockData] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BlockData]
acc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 99 = [BlockData] -> m [BlockData]
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData
bBlockData -> [BlockData] -> [BlockData]
forall a. a -> [a] -> [a]
:[BlockData]
acc)
        | Bool
otherwise = do
            let prev :: BlockHash
prev = BlockHeader -> BlockHash
H.prevBlock (BlockData -> BlockHeader
blockDataHeader BlockData
b)
            [BlockData] -> Maybe BlockData -> m [BlockData]
go (Bool -> BlockData -> BlockData
pruneTx Bool
noTx BlockData
b BlockData -> [BlockData] -> [BlockData]
forall a. a -> [a] -> [a]
: [BlockData]
acc) (Maybe BlockData -> m [BlockData])
-> m (Maybe BlockData) -> m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
prev

-- GET BlockHeight / BlockHeights / BlockHeightRaw --

scottyBlockHeight ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight :: GetBlockHeight -> WebT m [BlockData]
scottyBlockHeight (GetBlockHeight h :: HeightParam
h (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$
    ([BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
`getBlocks` Bool
notx) ([BlockHash] -> WebT m [BlockData])
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> WebT m [BlockData]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
h)

scottyBlockHeights ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockHeights
    -> WebT m [BlockData]
scottyBlockHeights :: GetBlockHeights -> WebT m [BlockData]
scottyBlockHeights (GetBlockHeights (HeightsParam heights :: [Natural]
heights) (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [BlockData] -> WebT m [BlockData]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime ([Natural] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Natural]
heights) (WebT m [BlockData] -> WebT m [BlockData])
-> WebT m [BlockData] -> WebT m [BlockData]
forall a b. (a -> b) -> a -> b
$ do
    [BlockHash]
bhs <- [[BlockHash]] -> [BlockHash]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[BlockHash]] -> [BlockHash])
-> ActionT Except (ReaderT WebState m) [[BlockHash]]
-> ActionT Except (ReaderT WebState m) [BlockHash]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word32 -> ActionT Except (ReaderT WebState m) [BlockHash])
-> [Word32] -> ActionT Except (ReaderT WebState m) [[BlockHash]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Natural -> Word32) -> [Natural] -> [Word32]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Natural]
heights)
    [BlockHash] -> Bool -> WebT m [BlockData]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
[BlockHash] -> Bool -> WebT m [BlockData]
getBlocks [BlockHash]
bhs Bool
notx

scottyBlockHeightRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetBlockHeightRaw
    -> WebT m (RawResultList H.Block)
scottyBlockHeightRaw :: GetBlockHeightRaw -> WebT m (RawResultList Block)
scottyBlockHeightRaw (GetBlockHeightRaw h :: HeightParam
h) =
    (WebMetrics -> StatDist)
-> Int
-> WebT m (RawResultList Block)
-> WebT m (RawResultList Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResultList Block) -> WebT m (RawResultList Block))
-> WebT m (RawResultList Block) -> WebT m (RawResultList Block)
forall a b. (a -> b) -> a -> b
$
    [Block] -> RawResultList Block
forall a. [a] -> RawResultList a
RawResultList ([Block] -> RawResultList Block)
-> ActionT Except (ReaderT WebState m) [Block]
-> WebT m (RawResultList Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((BlockHash -> ActionT Except (ReaderT WebState m) Block)
-> [BlockHash] -> ActionT Except (ReaderT WebState m) [Block]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM BlockHash -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m Block
getRawBlock ([BlockHash] -> ActionT Except (ReaderT WebState m) [Block])
-> ActionT Except (ReaderT WebState m) [BlockHash]
-> ActionT Except (ReaderT WebState m) [Block]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Word32 -> ActionT Except (ReaderT WebState m) [BlockHash]
forall (m :: * -> *). StoreReadBase m => Word32 -> m [BlockHash]
getBlocksAtHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
h))

-- GET BlockTime / BlockTimeRaw --

scottyBlockTime :: (MonadUnliftIO m, MonadLoggerIO m)
                => GetBlockTime -> WebT m BlockData
scottyBlockTime :: GetBlockTime -> WebT m BlockData
scottyBlockTime (GetBlockTime (TimeParam t :: Word64
t) (NoTx notx :: Bool
notx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrBefore Chain
ch Word64
t
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
notx) Maybe BlockData
m

scottyBlockMTP :: (MonadUnliftIO m, MonadLoggerIO m)
               => GetBlockMTP -> WebT m BlockData
scottyBlockMTP :: GetBlockMTP -> WebT m BlockData
scottyBlockMTP (GetBlockMTP (TimeParam t :: Word64
t) (NoTx noTx :: Bool
noTx)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m BlockData -> WebT m BlockData
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
blockResponseTime 1 (WebT m BlockData -> WebT m BlockData)
-> WebT m BlockData -> WebT m BlockData
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrAfterMTP Chain
ch Word64
t
    WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) (BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> WebT m BlockData)
-> (BlockData -> BlockData) -> BlockData -> WebT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BlockData -> BlockData
pruneTx Bool
noTx) Maybe BlockData
m

scottyBlockTimeRaw :: (MonadUnliftIO m, MonadLoggerIO m)
                   => GetBlockTimeRaw -> WebT m (RawResult H.Block)
scottyBlockTimeRaw :: GetBlockTimeRaw -> WebT m (RawResult Block)
scottyBlockTimeRaw (GetBlockTimeRaw (TimeParam t :: Word64
t)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrBefore Chain
ch Word64
t
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe BlockData
m
    (WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
refuseLargeBlock WebMetrics -> ErrorCounter
blockErrors BlockData
b
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockData -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b

scottyBlockMTPRaw :: (MonadUnliftIO m, MonadLoggerIO m)
                  => GetBlockMTPRaw -> WebT m (RawResult H.Block)
scottyBlockMTPRaw :: GetBlockMTPRaw -> WebT m (RawResult Block)
scottyBlockMTPRaw (GetBlockMTPRaw (TimeParam t :: Word64
t)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawBlockResponseTime 1 (WebT m (RawResult Block) -> WebT m (RawResult Block))
-> WebT m (RawResult Block) -> WebT m (RawResult Block)
forall a b. (a -> b) -> a -> b
$ do
    Chain
ch <- ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Chain
 -> ActionT Except (ReaderT WebState m) Chain)
-> ReaderT WebState m Chain
-> ActionT Except (ReaderT WebState m) Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> ReaderT WebState m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe BlockData
m <- Chain
-> Word64 -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrAfterMTP Chain
ch Word64
t
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
blockErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe BlockData
m
    (WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
refuseLargeBlock WebMetrics -> ErrorCounter
blockErrors BlockData
b
    Block -> RawResult Block
forall a. a -> RawResult a
RawResult (Block -> RawResult Block)
-> ActionT Except (ReaderT WebState m) Block
-> WebT m (RawResult Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockData -> ActionT Except (ReaderT WebState m) Block
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
BlockData -> m Block
toRawBlock BlockData
b

-- GET Transactions --

scottyTx :: (MonadUnliftIO m, MonadLoggerIO m) => GetTx -> WebT m Transaction
scottyTx :: GetTx -> WebT m Transaction
scottyTx (GetTx txid :: TxHash
txid) =
    (WebMetrics -> StatDist)
-> Int -> WebT m Transaction -> WebT m Transaction
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime 1 (WebT m Transaction -> WebT m Transaction)
-> WebT m Transaction -> WebT m Transaction
forall a b. (a -> b) -> a -> b
$
    WebT m Transaction
-> (Transaction -> WebT m Transaction)
-> Maybe Transaction
-> WebT m Transaction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m Transaction
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) Transaction -> WebT m Transaction
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Transaction -> WebT m Transaction)
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
-> WebT m Transaction
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
txid

scottyTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxs -> WebT m [Transaction]
scottyTxs :: GetTxs -> WebT m [Transaction]
scottyTxs (GetTxs txids :: [TxHash]
txids) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime ([TxHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxHash]
txids) (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxHash]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction ([TxHash] -> [TxHash]
forall a. Eq a => [a] -> [a]
nub [TxHash]
txids)

scottyTxRaw ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw :: GetTxRaw -> WebT m (RawResult Tx)
scottyTxRaw (GetTxRaw txid :: TxHash
txid) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResult Tx) -> WebT m (RawResult Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime 1 (WebT m (RawResult Tx) -> WebT m (RawResult Tx))
-> WebT m (RawResult Tx) -> WebT m (RawResult Tx)
forall a b. (a -> b) -> a -> b
$ do
    Transaction
tx <- WebT m Transaction
-> (Transaction -> WebT m Transaction)
-> Maybe Transaction
-> WebT m Transaction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m Transaction
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) Transaction -> WebT m Transaction
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Transaction -> WebT m Transaction)
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
-> WebT m Transaction
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
txid
    RawResult Tx -> WebT m (RawResult Tx)
forall (m :: * -> *) a. Monad m => a -> m a
return (RawResult Tx -> WebT m (RawResult Tx))
-> RawResult Tx -> WebT m (RawResult Tx)
forall a b. (a -> b) -> a -> b
$ Tx -> RawResult Tx
forall a. a -> RawResult a
RawResult (Tx -> RawResult Tx) -> Tx -> RawResult Tx
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData Transaction
tx

scottyTxsRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxsRaw
    -> WebT m (RawResultList Tx)
scottyTxsRaw :: GetTxsRaw -> WebT m (RawResultList Tx)
scottyTxsRaw (GetTxsRaw txids :: [TxHash]
txids) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txResponseTime ([TxHash] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxHash]
txids) (WebT m (RawResultList Tx) -> WebT m (RawResultList Tx))
-> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$ do
    [Transaction]
txs <- [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxHash]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction ([TxHash] -> [TxHash]
forall a. Eq a => [a] -> [a]
nub [TxHash]
txids)
    RawResultList Tx -> WebT m (RawResultList Tx)
forall (m :: * -> *) a. Monad m => a -> m a
return (RawResultList Tx -> WebT m (RawResultList Tx))
-> RawResultList Tx -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$ [Tx] -> RawResultList Tx
forall a. [a] -> RawResultList a
RawResultList ([Tx] -> RawResultList Tx) -> [Tx] -> RawResultList Tx
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData (Transaction -> Tx) -> [Transaction] -> [Tx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Transaction]
txs

getTxsBlock :: (MonadUnliftIO m, MonadLoggerIO m)
            => H.BlockHash
            -> WebT m [Transaction]
getTxsBlock :: BlockHash -> WebT m [Transaction]
getTxsBlock h :: BlockHash
h = do
    BlockData
b <- WebT m BlockData
-> (BlockData -> WebT m BlockData)
-> Maybe BlockData
-> WebT m BlockData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ((WebMetrics -> ErrorCounter) -> Except -> WebT m BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
txErrors Except
ThingNotFound) BlockData -> WebT m BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe BlockData -> WebT m BlockData)
-> ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> WebT m BlockData
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
h
    (WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> BlockData -> WebT m ()
refuseLargeBlock WebMetrics -> ErrorCounter
txErrors BlockData
b
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxHash]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (BlockData -> [TxHash]
blockDataTxs BlockData
b)

scottyTxsBlock ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock :: GetTxsBlock -> WebT m [Transaction]
scottyTxsBlock (GetTxsBlock h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txsBlockResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ BlockHash -> WebT m [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m [Transaction]
getTxsBlock BlockHash
h

scottyTxsBlockRaw ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxsBlockRaw
    -> WebT m (RawResultList Tx)
scottyTxsBlockRaw :: GetTxsBlockRaw -> WebT m (RawResultList Tx)
scottyTxsBlockRaw (GetTxsBlockRaw h :: BlockHash
h) =
    (WebMetrics -> StatDist)
-> Int -> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txsBlockResponseTime 1 (WebT m (RawResultList Tx) -> WebT m (RawResultList Tx))
-> WebT m (RawResultList Tx) -> WebT m (RawResultList Tx)
forall a b. (a -> b) -> a -> b
$
    [Tx] -> RawResultList Tx
forall a. [a] -> RawResultList a
RawResultList ([Tx] -> RawResultList Tx)
-> ([Transaction] -> [Tx]) -> [Transaction] -> RawResultList Tx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> Tx) -> [Transaction] -> [Tx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Transaction -> Tx
transactionData ([Transaction] -> RawResultList Tx)
-> ActionT Except (ReaderT WebState m) [Transaction]
-> WebT m (RawResultList Tx)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockHash -> ActionT Except (ReaderT WebState m) [Transaction]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
BlockHash -> WebT m [Transaction]
getTxsBlock BlockHash
h

-- GET TransactionAfterHeight --

scottyTxAfter ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetTxAfter
    -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter :: GetTxAfter -> WebT m (GenericResult (Maybe Bool))
scottyTxAfter (GetTxAfter txid :: TxHash
txid height :: HeightParam
height) =
    (WebMetrics -> StatDist)
-> Int
-> WebT m (GenericResult (Maybe Bool))
-> WebT m (GenericResult (Maybe Bool))
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
txAfterResponseTime 1 (WebT m (GenericResult (Maybe Bool))
 -> WebT m (GenericResult (Maybe Bool)))
-> WebT m (GenericResult (Maybe Bool))
-> WebT m (GenericResult (Maybe Bool))
forall a b. (a -> b) -> a -> b
$
    Maybe Bool -> GenericResult (Maybe Bool)
forall a. a -> GenericResult a
GenericResult (Maybe Bool -> GenericResult (Maybe Bool))
-> ActionT Except (ReaderT WebState m) (Maybe Bool)
-> WebT m (GenericResult (Maybe Bool))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word32
-> TxHash -> ActionT Except (ReaderT WebState m) (Maybe Bool)
forall (m :: * -> *).
(MonadIO m, StoreReadBase m) =>
Word32 -> TxHash -> m (Maybe Bool)
cbAfterHeight (HeightParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral HeightParam
height) TxHash
txid

-- | Check if any of the ancestors of this transaction is a coinbase after the
-- specified height. Returns 'Nothing' if answer cannot be computed before
-- hitting limits.
cbAfterHeight ::
       (MonadIO m, StoreReadBase m)
    => H.BlockHeight
    -> TxHash
    -> m (Maybe Bool)
cbAfterHeight :: Word32 -> TxHash -> m (Maybe Bool)
cbAfterHeight height :: Word32
height txid :: TxHash
txid =
    Integer
-> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
forall a (m :: * -> *).
(Eq a, Num a, StoreReadBase m) =>
a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs 10000 HashSet TxHash
forall a. HashSet a
HashSet.empty HashSet TxHash
forall a. HashSet a
HashSet.empty [TxHash
txid]
  where
    inputs :: a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs 0 _ _ [] = Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Bool
forall a. Maybe a
Nothing
    inputs i :: a
i is :: HashSet TxHash
is ns :: HashSet TxHash
ns [] =
        let is' :: HashSet TxHash
is' = HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.union HashSet TxHash
is HashSet TxHash
ns
            ns' :: HashSet a
ns' = HashSet a
forall a. HashSet a
HashSet.empty
            ts :: [TxHash]
ts = HashSet TxHash -> [TxHash]
forall a. HashSet a -> [a]
HashSet.toList (HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.difference HashSet TxHash
ns HashSet TxHash
is)
        in case [TxHash]
ts of
               [] -> Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False)
               _  -> a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs a
i HashSet TxHash
is' HashSet TxHash
forall a. HashSet a
ns' [TxHash]
ts
    inputs i :: a
i is :: HashSet TxHash
is ns :: HashSet TxHash
ns (t :: TxHash
t:ts :: [TxHash]
ts) = TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
t m (Maybe Transaction)
-> (Maybe Transaction -> m (Maybe Bool)) -> m (Maybe Bool)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Bool
forall a. Maybe a
Nothing
        Just tx :: Transaction
tx | Transaction -> Bool
height_check Transaction
tx ->
                      if Transaction -> Bool
cb_check Transaction
tx
                      then Maybe Bool -> m (Maybe Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True)
                      else let ns' :: HashSet TxHash
ns' = HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.union (Transaction -> HashSet TxHash
ins Transaction
tx) HashSet TxHash
ns
                          in a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs (a
i a -> a -> a
forall a. Num a => a -> a -> a
- 1) HashSet TxHash
is HashSet TxHash
ns' [TxHash]
ts
                | Bool
otherwise -> a -> HashSet TxHash -> HashSet TxHash -> [TxHash] -> m (Maybe Bool)
inputs (a
i a -> a -> a
forall a. Num a => a -> a -> a
- 1) HashSet TxHash
is HashSet TxHash
ns [TxHash]
ts
    cb_check :: Transaction -> Bool
cb_check = (StoreInput -> Bool) -> [StoreInput] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StoreInput -> Bool
isCoinbase ([StoreInput] -> Bool)
-> (Transaction -> [StoreInput]) -> Transaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [StoreInput]
transactionInputs
    ins :: Transaction -> HashSet TxHash
ins = [TxHash] -> HashSet TxHash
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([TxHash] -> HashSet TxHash)
-> (Transaction -> [TxHash]) -> Transaction -> HashSet TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (StoreInput -> TxHash) -> [StoreInput] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map (OutPoint -> TxHash
outPointHash (OutPoint -> TxHash)
-> (StoreInput -> OutPoint) -> StoreInput -> TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StoreInput -> OutPoint
inputPoint) ([StoreInput] -> [TxHash])
-> (Transaction -> [StoreInput]) -> Transaction -> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [StoreInput]
transactionInputs
    height_check :: Transaction -> Bool
height_check tx :: Transaction
tx =
        case Transaction -> BlockRef
transactionBlock Transaction
tx of
            BlockRef h :: Word32
h _ -> Word32
h Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
height
            _            -> Bool
True

-- POST Transaction --

scottyPostTx :: (MonadUnliftIO m, MonadLoggerIO m) => PostTx -> WebT m TxId
scottyPostTx :: PostTx -> WebT m TxId
scottyPostTx (PostTx tx :: Tx
tx) =
    (WebMetrics -> StatDist) -> Int -> WebT m TxId -> WebT m TxId
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
postTxResponseTime 1 (WebT m TxId -> WebT m TxId) -> WebT m TxId -> WebT m TxId
forall a b. (a -> b) -> a -> b
$
    ReaderT WebState m WebConfig
-> ActionT Except (ReaderT WebState m) WebConfig
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> WebConfig) -> ReaderT WebState m WebConfig
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> WebConfig
webConfig) ActionT Except (ReaderT WebState m) WebConfig
-> (WebConfig -> WebT m TxId) -> WebT m TxId
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \cfg :: WebConfig
cfg -> ReaderT WebState m (Either PubExcept ())
-> ActionT Except (ReaderT WebState m) (Either PubExcept ())
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WebConfig -> Tx -> ReaderT WebState m (Either PubExcept ())
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> Tx -> m (Either PubExcept ())
publishTx WebConfig
cfg Tx
tx) ActionT Except (ReaderT WebState m) (Either PubExcept ())
-> (Either PubExcept () -> WebT m TxId) -> WebT m TxId
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Right ()             -> TxId -> WebT m TxId
forall (m :: * -> *) a. Monad m => a -> m a
return (TxId -> WebT m TxId) -> TxId -> WebT m TxId
forall a b. (a -> b) -> a -> b
$ TxHash -> TxId
TxId (Tx -> TxHash
txHash Tx
tx)
        Left e :: PubExcept
e@(PubReject _) -> (WebMetrics -> ErrorCounter) -> Except -> WebT m TxId
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
postTxErrors (Except -> WebT m TxId) -> Except -> WebT m TxId
forall a b. (a -> b) -> a -> b
$ String -> Except
UserError (String -> Except) -> String -> Except
forall a b. (a -> b) -> a -> b
$ PubExcept -> String
forall a. Show a => a -> String
show PubExcept
e
        _                    -> (WebMetrics -> ErrorCounter) -> Except -> WebT m TxId
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
postTxErrors Except
ServerError

-- | Publish a new transaction to the network.
publishTx ::
       (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
    => WebConfig
    -> Tx
    -> m (Either PubExcept ())
publishTx :: WebConfig -> Tx -> m (Either PubExcept ())
publishTx cfg :: WebConfig
cfg tx :: Tx
tx =
    Publisher StoreEvent
-> (Inbox StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) msg a.
MonadUnliftIO m =>
Publisher msg -> (Inbox msg -> m a) -> m a
withSubscription Publisher StoreEvent
pub ((Inbox StoreEvent -> m (Either PubExcept ()))
 -> m (Either PubExcept ()))
-> (Inbox StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ \s :: Inbox StoreEvent
s ->
        TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (Tx -> TxHash
txHash Tx
tx) m (Maybe Transaction)
-> (Maybe Transaction -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Just _  -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
            Nothing -> Inbox StoreEvent -> m (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(MonadIO m, InChan mbox) =>
mbox StoreEvent -> m (Either PubExcept ())
go Inbox StoreEvent
s
  where
    pub :: Publisher StoreEvent
pub = Store -> Publisher StoreEvent
storePublisher (WebConfig -> Store
webStore WebConfig
cfg)
    mgr :: PeerManager
mgr = Store -> PeerManager
storeManager (WebConfig -> Store
webStore WebConfig
cfg)
    net :: Network
net = Store -> Network
storeNetwork (WebConfig -> Store
webStore WebConfig
cfg)
    go :: mbox StoreEvent -> m (Either PubExcept ())
go s :: mbox StoreEvent
s =
        PeerManager -> m [OnlinePeer]
forall (m :: * -> *). MonadIO m => PeerManager -> m [OnlinePeer]
getPeers PeerManager
mgr m [OnlinePeer]
-> ([OnlinePeer] -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            [] -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubNoPeers
            OnlinePeer {onlinePeerMailbox :: OnlinePeer -> Peer
onlinePeerMailbox = Peer
p}:_ -> do
                Tx -> Message
MTx Tx
tx Message -> Peer -> m ()
forall (m :: * -> *). MonadIO m => Message -> Peer -> m ()
`sendMessage` Peer
p
                let v :: InvType
v =
                        if Network -> Bool
getSegWit Network
net
                            then InvType
InvWitnessTx
                            else InvType
InvTx
                Message -> Peer -> m ()
forall (m :: * -> *). MonadIO m => Message -> Peer -> m ()
sendMessage
                    (GetData -> Message
MGetData ([InvVector] -> GetData
GetData [InvType -> Hash256 -> InvVector
InvVector InvType
v (TxHash -> Hash256
getTxHash (Tx -> TxHash
txHash Tx
tx))]))
                    Peer
p
                Peer -> mbox StoreEvent -> m (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(MonadIO m, InChan mbox) =>
Peer -> mbox StoreEvent -> m (Either PubExcept ())
f Peer
p mbox StoreEvent
s
    t :: Int
t = 5 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000 Int -> Int -> Int
forall a. Num a => a -> a -> a
* 1000
    f :: Peer -> mbox StoreEvent -> m (Either PubExcept ())
f p :: Peer
p s :: mbox StoreEvent
s
      | WebConfig -> Bool
webNoMempool WebConfig
cfg = Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
      | Bool
otherwise =
        IO (Maybe (Either PubExcept ())) -> m (Maybe (Either PubExcept ()))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> IO (Either PubExcept ()) -> IO (Maybe (Either PubExcept ()))
forall (m :: * -> *) a.
MonadUnliftIO m =>
Int -> m a -> m (Maybe a)
timeout Int
t (Peer -> mbox StoreEvent -> IO (Either PubExcept ())
forall (m :: * -> *) (mbox :: * -> *).
(InChan mbox, MonadIO m) =>
Peer -> mbox StoreEvent -> m (Either PubExcept ())
g Peer
p mbox StoreEvent
s)) m (Maybe (Either PubExcept ()))
-> (Maybe (Either PubExcept ()) -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing         -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubTimeout
            Just (Left e :: PubExcept
e)   -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
e
            Just (Right ()) -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
    g :: Peer -> mbox StoreEvent -> m (Either PubExcept ())
g p :: Peer
p s :: mbox StoreEvent
s =
        mbox StoreEvent -> m StoreEvent
forall (mbox :: * -> *) (m :: * -> *) msg.
(InChan mbox, MonadIO m) =>
mbox msg -> m msg
receive mbox StoreEvent
s m StoreEvent
-> (StoreEvent -> m (Either PubExcept ()))
-> m (Either PubExcept ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            StoreTxReject p' :: Peer
p' h' :: TxHash
h' c :: RejectCode
c _
                | Peer
p Peer -> Peer -> Bool
forall a. Eq a => a -> a -> Bool
== Peer
p' Bool -> Bool -> Bool
&& TxHash
h' TxHash -> TxHash -> Bool
forall a. Eq a => a -> a -> Bool
== Tx -> TxHash
txHash Tx
tx -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> (PubExcept -> Either PubExcept ())
-> PubExcept
-> m (Either PubExcept ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left (PubExcept -> m (Either PubExcept ()))
-> PubExcept -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ RejectCode -> PubExcept
PubReject RejectCode
c
            StorePeerDisconnected p' :: Peer
p'
                | Peer
p Peer -> Peer -> Bool
forall a. Eq a => a -> a -> Bool
== Peer
p' -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ PubExcept -> Either PubExcept ()
forall a b. a -> Either a b
Left PubExcept
PubPeerDisconnected
            StoreMempoolNew h' :: TxHash
h'
                | TxHash
h' TxHash -> TxHash -> Bool
forall a. Eq a => a -> a -> Bool
== Tx -> TxHash
txHash Tx
tx -> Either PubExcept () -> m (Either PubExcept ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either PubExcept () -> m (Either PubExcept ()))
-> Either PubExcept () -> m (Either PubExcept ())
forall a b. (a -> b) -> a -> b
$ () -> Either PubExcept ()
forall a b. b -> Either a b
Right ()
            _ -> Peer -> mbox StoreEvent -> m (Either PubExcept ())
g Peer
p mbox StoreEvent
s

-- GET Mempool / Events --

scottyMempool ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetMempool -> WebT m [TxHash]
scottyMempool :: GetMempool -> WebT m [TxHash]
scottyMempool (GetMempool limitM :: Maybe LimitParam
limitM (OffsetParam o :: Natural
o)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [TxHash] -> WebT m [TxHash]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
mempoolResponseTime 1 (WebT m [TxHash] -> WebT m [TxHash])
-> WebT m [TxHash] -> WebT m [TxHash]
forall a b. (a -> b) -> a -> b
$ do
    WebLimits
wl <- ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m WebLimits
 -> ActionT Except (ReaderT WebState m) WebLimits)
-> ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall a b. (a -> b) -> a -> b
$ (WebState -> WebLimits) -> ReaderT WebState m WebLimits
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    let wl' :: WebLimits
wl' = WebLimits
wl { maxLimitCount :: Word32
maxLimitCount = 0 }
        l :: Limits
l = Word32 -> Word32 -> Maybe Start -> Limits
Limits (WebLimits -> Bool -> Maybe LimitParam -> Word32
validateLimit WebLimits
wl' Bool
False Maybe LimitParam
limitM) (Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
o) Maybe Start
forall a. Maybe a
Nothing
    ((Word64, TxHash) -> TxHash) -> [(Word64, TxHash)] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, TxHash) -> TxHash
forall a b. (a, b) -> b
snd ([(Word64, TxHash)] -> [TxHash])
-> ([(Word64, TxHash)] -> [(Word64, TxHash)])
-> [(Word64, TxHash)]
-> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Limits -> [(Word64, TxHash)] -> [(Word64, TxHash)]
forall a. Limits -> [a] -> [a]
applyLimits Limits
l ([(Word64, TxHash)] -> [TxHash])
-> ActionT Except (ReaderT WebState m) [(Word64, TxHash)]
-> WebT m [TxHash]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool

scottyEvents :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyEvents :: WebT m ()
scottyEvents =
    (WebMetrics -> Gauge) -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> Gauge) -> WebT m a -> WebT m a
withGaugeIncrease WebMetrics -> Gauge
eventsConnected (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    SerialAs
proto <- Bool -> ActionT Except (ReaderT WebState m) SerialAs
forall (m :: * -> *). Monad m => Bool -> ActionT Except m SerialAs
setupContentType Bool
False
    Publisher StoreEvent
pub <- ReaderT WebState m (Publisher StoreEvent)
-> ActionT Except (ReaderT WebState m) (Publisher StoreEvent)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m (Publisher StoreEvent)
 -> ActionT Except (ReaderT WebState m) (Publisher StoreEvent))
-> ReaderT WebState m (Publisher StoreEvent)
-> ActionT Except (ReaderT WebState m) (Publisher StoreEvent)
forall a b. (a -> b) -> a -> b
$ (WebState -> Publisher StoreEvent)
-> ReaderT WebState m (Publisher StoreEvent)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Publisher StoreEvent
storePublisher (Store -> Publisher StoreEvent)
-> (WebState -> Store) -> WebState -> Publisher StoreEvent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    StreamingBody -> WebT m ()
forall (m :: * -> *) e. Monad m => StreamingBody -> ActionT e m ()
S.stream (StreamingBody -> WebT m ()) -> StreamingBody -> WebT m ()
forall a b. (a -> b) -> a -> b
$ \io :: Builder -> IO ()
io flush' :: IO ()
flush' ->
        Publisher StoreEvent -> (Inbox StoreEvent -> IO ()) -> IO ()
forall (m :: * -> *) msg a.
MonadUnliftIO m =>
Publisher msg -> (Inbox msg -> m a) -> m a
withSubscription Publisher StoreEvent
pub ((Inbox StoreEvent -> IO ()) -> IO ())
-> (Inbox StoreEvent -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \sub :: Inbox StoreEvent
sub ->
            IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            IO ()
flush' IO () -> IO (Maybe Event) -> IO (Maybe Event)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Inbox StoreEvent -> IO (Maybe Event)
receiveEvent Inbox StoreEvent
sub IO (Maybe Event) -> (Maybe Event -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> (Event -> IO ()) -> Maybe Event -> IO ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (Builder -> IO ()
io (Builder -> IO ()) -> (Event -> Builder) -> Event -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialAs -> Event -> Builder
forall a. (Serialize a, ToJSON a) => SerialAs -> a -> Builder
serial SerialAs
proto)
  where
    serial :: SerialAs -> a -> Builder
serial proto :: SerialAs
proto e :: a
e =
        ByteString -> Builder
lazyByteString (ByteString -> Builder) -> ByteString -> Builder
forall a b. (a -> b) -> a -> b
$ SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
forall a.
Serialize a =>
SerialAs -> (a -> Encoding) -> (a -> Value) -> a -> ByteString
protoSerial SerialAs
proto a -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding a -> Value
forall a. ToJSON a => a -> Value
toJSON a
e ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> SerialAs -> ByteString
forall p. (Monoid p, IsString p) => SerialAs -> p
newLine SerialAs
proto
    newLine :: SerialAs -> p
newLine SerialAsBinary     = p
forall a. Monoid a => a
mempty
    newLine SerialAsJSON       = "\n"
    newLine SerialAsPrettyJSON = p
forall a. Monoid a => a
mempty

receiveEvent :: Inbox StoreEvent -> IO (Maybe Event)
receiveEvent :: Inbox StoreEvent -> IO (Maybe Event)
receiveEvent sub :: Inbox StoreEvent
sub = do
    StoreEvent
se <- Inbox StoreEvent -> IO StoreEvent
forall (mbox :: * -> *) (m :: * -> *) msg.
(InChan mbox, MonadIO m) =>
mbox msg -> m msg
receive Inbox StoreEvent
sub
    Maybe Event -> IO (Maybe Event)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Event -> IO (Maybe Event))
-> Maybe Event -> IO (Maybe Event)
forall a b. (a -> b) -> a -> b
$
        case StoreEvent
se of
            StoreBestBlock b :: BlockHash
b  -> Event -> Maybe Event
forall a. a -> Maybe a
Just (BlockHash -> Event
EventBlock BlockHash
b)
            StoreMempoolNew t :: TxHash
t -> Event -> Maybe Event
forall a. a -> Maybe a
Just (TxHash -> Event
EventTx TxHash
t)
            _                 -> Maybe Event
forall a. Maybe a
Nothing

-- GET Address Transactions --

scottyAddrTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs :: GetAddrTxs -> WebT m [TxRef]
scottyAddrTxs (GetAddrTxs addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxResponseTime 1 (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    Address -> Limits -> WebT m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
addr (Limits -> WebT m [TxRef])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrsTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs :: GetAddrsTxs -> WebT m [TxRef]
scottyAddrsTxs (GetAddrsTxs addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    [Address] -> Limits -> WebT m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
addrs (Limits -> WebT m [TxRef])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrTxsFull ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetAddrTxsFull
    -> WebT m [Transaction]
scottyAddrTxsFull :: GetAddrTxsFull -> WebT m [Transaction]
scottyAddrTxsFull (GetAddrTxsFull addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxFullResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
txs <- Address -> Limits -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
addr (Limits -> ActionT Except (ReaderT WebState m) [TxRef])
-> ActionT Except (ReaderT WebState m) Limits
-> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
True LimitsParam
pLimits
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxRef -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxRef]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
txs

scottyAddrsTxsFull :: (MonadUnliftIO m, MonadLoggerIO m)
                   => GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull :: GetAddrsTxsFull -> WebT m [Transaction]
scottyAddrsTxsFull (GetAddrsTxsFull addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrTxFullResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
txs <- [Address] -> Limits -> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [TxRef]
getAddressesTxs [Address]
addrs (Limits -> ActionT Except (ReaderT WebState m) [TxRef])
-> ActionT Except (ReaderT WebState m) Limits
-> ActionT Except (ReaderT WebState m) [TxRef]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
True LimitsParam
pLimits
    [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Transaction] -> [Transaction])
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
-> WebT m [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (TxRef -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> [TxRef]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ActionT Except (ReaderT WebState m) (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ActionT Except (ReaderT WebState m) (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
txs

scottyAddrBalance :: (MonadUnliftIO m, MonadLoggerIO m)
                  => GetAddrBalance -> WebT m Balance
scottyAddrBalance :: GetAddrBalance -> WebT m Balance
scottyAddrBalance (GetAddrBalance addr :: Address
addr) =
    (WebMetrics -> StatDist) -> Int -> WebT m Balance -> WebT m Balance
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrBalanceResponseTime 1 (WebT m Balance -> WebT m Balance)
-> WebT m Balance -> WebT m Balance
forall a b. (a -> b) -> a -> b
$
    Address -> WebT m Balance
forall (m :: * -> *). StoreReadBase m => Address -> m Balance
getDefaultBalance Address
addr

scottyAddrsBalance ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance :: GetAddrsBalance -> WebT m [Balance]
scottyAddrsBalance (GetAddrsBalance addrs :: [Address]
addrs) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Balance] -> WebT m [Balance]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrBalanceResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Balance] -> WebT m [Balance])
-> WebT m [Balance] -> WebT m [Balance]
forall a b. (a -> b) -> a -> b
$
    [Address] -> WebT m [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances [Address]
addrs

scottyAddrUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent :: GetAddrUnspent -> WebT m [Unspent]
scottyAddrUnspent (GetAddrUnspent addr :: Address
addr pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Unspent] -> WebT m [Unspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrUnspentResponseTime 1 (WebT m [Unspent] -> WebT m [Unspent])
-> WebT m [Unspent] -> WebT m [Unspent]
forall a b. (a -> b) -> a -> b
$
    Address -> Limits -> WebT m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [Unspent]
getAddressUnspents Address
addr (Limits -> WebT m [Unspent])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [Unspent]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

scottyAddrsUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent :: GetAddrsUnspent -> WebT m [Unspent]
scottyAddrsUnspent (GetAddrsUnspent addrs :: [Address]
addrs pLimits :: LimitsParam
pLimits) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Unspent] -> WebT m [Unspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
addrUnspentResponseTime ([Address] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Address]
addrs) (WebT m [Unspent] -> WebT m [Unspent])
-> WebT m [Unspent] -> WebT m [Unspent]
forall a b. (a -> b) -> a -> b
$
    [Address] -> Limits -> WebT m [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
getAddressesUnspents [Address]
addrs (Limits -> WebT m [Unspent])
-> ActionT Except (ReaderT WebState m) Limits -> WebT m [Unspent]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bool -> LimitsParam -> ActionT Except (ReaderT WebState m) Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits

-- GET XPubs --

scottyXPub ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPub -> WebT m XPubSummary
scottyXPub :: GetXPub -> WebT m XPubSummary
scottyXPub (GetXPub xpub :: XPubKey
xpub deriv :: DeriveType
deriv (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m XPubSummary -> WebT m XPubSummary
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubResponseTime 1 (WebT m XPubSummary -> WebT m XPubSummary)
-> WebT m XPubSummary -> WebT m XPubSummary
forall a b. (a -> b) -> a -> b
$
    ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> (ReaderT WebState m XPubSummary
    -> ReaderT WebState m XPubSummary)
-> ReaderT WebState m XPubSummary
-> WebT m XPubSummary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m XPubSummary -> ReaderT WebState m XPubSummary
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (ReaderT WebState m XPubSummary -> WebT m XPubSummary)
-> ReaderT WebState m XPubSummary -> WebT m XPubSummary
forall a b. (a -> b) -> a -> b
$ XPubSpec -> ReaderT WebState m XPubSummary
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m XPubSummary
xPubSummary (XPubSpec -> ReaderT WebState m XPubSummary)
-> XPubSpec -> ReaderT WebState m XPubSummary
forall a b. (a -> b) -> a -> b
$ XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv

getXPubTxs :: (MonadUnliftIO m, MonadLoggerIO m)
           => XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs :: XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits nocache :: Bool
nocache = do
    Limits
limits <- Bool -> LimitsParam -> WebT m Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
plimits
    ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> (ReaderT WebState m [TxRef] -> ReaderT WebState m [TxRef])
-> ReaderT WebState m [TxRef]
-> WebT m [TxRef]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> ReaderT WebState m [TxRef] -> ReaderT WebState m [TxRef]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
nocache (ReaderT WebState m [TxRef] -> WebT m [TxRef])
-> ReaderT WebState m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$ XPubSpec -> Limits -> ReaderT WebState m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs (XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv) Limits
limits

scottyXPubTxs ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs :: GetXPubTxs -> WebT m [TxRef]
scottyXPubTxs (GetXPubTxs xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits (NoCache nocache :: Bool
nocache)) =
    (WebMetrics -> StatDist) -> Int -> WebT m [TxRef] -> WebT m [TxRef]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubTxResponseTime 1 (WebT m [TxRef] -> WebT m [TxRef])
-> WebT m [TxRef] -> WebT m [TxRef]
forall a b. (a -> b) -> a -> b
$
    XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs XPubKey
xpub DeriveType
deriv LimitsParam
plimits Bool
nocache

scottyXPubTxsFull ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetXPubTxsFull
    -> WebT m [Transaction]
scottyXPubTxsFull :: GetXPubTxsFull -> WebT m [Transaction]
scottyXPubTxsFull (GetXPubTxsFull xpub :: XPubKey
xpub deriv :: DeriveType
deriv plimits :: LimitsParam
plimits (NoCache nocache :: Bool
nocache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubTxFullResponseTime 1 (WebT m [Transaction] -> WebT m [Transaction])
-> WebT m [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ do
    [TxRef]
refs <- XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
XPubKey -> DeriveType -> LimitsParam -> Bool -> WebT m [TxRef]
getXPubTxs XPubKey
xpub DeriveType
deriv LimitsParam
plimits Bool
nocache
    [Maybe Transaction]
txs <- ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [Maybe Transaction]
 -> ActionT Except (ReaderT WebState m) [Maybe Transaction])
-> (ReaderT WebState m [Maybe Transaction]
    -> ReaderT WebState m [Maybe Transaction])
-> ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m [Maybe Transaction]
-> ReaderT WebState m [Maybe Transaction]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
nocache (ReaderT WebState m [Maybe Transaction]
 -> ActionT Except (ReaderT WebState m) [Maybe Transaction])
-> ReaderT WebState m [Maybe Transaction]
-> ActionT Except (ReaderT WebState m) [Maybe Transaction]
forall a b. (a -> b) -> a -> b
$ (TxRef -> ReaderT WebState m (Maybe Transaction))
-> [TxRef] -> ReaderT WebState m [Maybe Transaction]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (TxHash -> ReaderT WebState m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction (TxHash -> ReaderT WebState m (Maybe Transaction))
-> (TxRef -> TxHash)
-> TxRef
-> ReaderT WebState m (Maybe Transaction)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRef -> TxHash
txRefHash) [TxRef]
refs
    [Transaction] -> WebT m [Transaction]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Transaction] -> WebT m [Transaction])
-> [Transaction] -> WebT m [Transaction]
forall a b. (a -> b) -> a -> b
$ [Maybe Transaction] -> [Transaction]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Transaction]
txs

scottyXPubBalances ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances :: GetXPubBalances -> WebT m [XPubBal]
scottyXPubBalances (GetXPubBalances xpub :: XPubKey
xpub deriv :: DeriveType
deriv (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [XPubBal] -> WebT m [XPubBal]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubResponseTime 1 (WebT m [XPubBal] -> WebT m [XPubBal])
-> WebT m [XPubBal] -> WebT m [XPubBal]
forall a b. (a -> b) -> a -> b
$
    (XPubBal -> Bool) -> [XPubBal] -> [XPubBal]
forall a. (a -> Bool) -> [a] -> [a]
filter XPubBal -> Bool
f ([XPubBal] -> [XPubBal]) -> WebT m [XPubBal] -> WebT m [XPubBal]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT WebState m [XPubBal] -> WebT m [XPubBal]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Bool
-> ReaderT WebState m [XPubBal] -> ReaderT WebState m [XPubBal]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (XPubSpec -> ReaderT WebState m [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals XPubSpec
spec))
  where
    spec :: XPubSpec
spec = XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv
    f :: XPubBal -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (XPubBal -> Bool) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Balance -> Bool
nullBalance (Balance -> Bool) -> (XPubBal -> Balance) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubBal -> Balance
xPubBal

scottyXPubUnspent ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => GetXPubUnspent
    -> WebT m [XPubUnspent]
scottyXPubUnspent :: GetXPubUnspent -> WebT m [XPubUnspent]
scottyXPubUnspent (GetXPubUnspent xpub :: XPubKey
xpub deriv :: DeriveType
deriv pLimits :: LimitsParam
pLimits (NoCache noCache :: Bool
noCache)) =
    (WebMetrics -> StatDist)
-> Int -> WebT m [XPubUnspent] -> WebT m [XPubUnspent]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
xPubUnspentResponseTime 1 (WebT m [XPubUnspent] -> WebT m [XPubUnspent])
-> WebT m [XPubUnspent] -> WebT m [XPubUnspent]
forall a b. (a -> b) -> a -> b
$ do
    Limits
limits <- Bool -> LimitsParam -> WebT m Limits
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Bool -> LimitsParam -> WebT m Limits
paramToLimits Bool
False LimitsParam
pLimits
    ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> (ReaderT WebState m [XPubUnspent]
    -> ReaderT WebState m [XPubUnspent])
-> ReaderT WebState m [XPubUnspent]
-> WebT m [XPubUnspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> ReaderT WebState m [XPubUnspent]
-> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *) a.
MonadIO m =>
Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache Bool
noCache (ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent])
-> ReaderT WebState m [XPubUnspent] -> WebT m [XPubUnspent]
forall a b. (a -> b) -> a -> b
$ XPubSpec -> Limits -> ReaderT WebState m [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
xPubUnspents (XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
xpub DeriveType
deriv) Limits
limits

---------------------------------------
-- Blockchain.info API Compatibility --
---------------------------------------

netBinfoSymbol :: Network -> BinfoSymbol
netBinfoSymbol :: Network -> BinfoSymbol
netBinfoSymbol net :: Network
net
  | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
btc =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "BTC"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "BTC"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Bitcoin"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
True
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }
  | Network
net Network -> Network -> Bool
forall a. Eq a => a -> a -> Bool
== Network
bch =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "BCH"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "BCH"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Bitcoin Cash"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
True
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }
  | Bool
otherwise =
        $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = "XTS"
                   , getBinfoSymbolString :: Text
getBinfoSymbolString = "¤"
                   , getBinfoSymbolName :: Text
getBinfoSymbolName = "Test"
                   , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
                   , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
False
                   , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
False
                   }

binfoTickerToSymbol :: Text -> BinfoTicker -> BinfoSymbol
binfoTickerToSymbol :: Text -> BinfoTicker -> BinfoSymbol
binfoTickerToSymbol code :: Text
code BinfoTicker{..} =
    $WBinfoSymbol :: Text -> Text -> Text -> Double -> Bool -> Bool -> BinfoSymbol
BinfoSymbol{ getBinfoSymbolCode :: Text
getBinfoSymbolCode = Text
code
               , getBinfoSymbolString :: Text
getBinfoSymbolString = Text
binfoTickerSymbol
               , getBinfoSymbolName :: Text
getBinfoSymbolName = Text
name
               , getBinfoSymbolConversion :: Double
getBinfoSymbolConversion =
                       100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
binfoTicker15m -- sat/usd
               , getBinfoSymbolAfter :: Bool
getBinfoSymbolAfter = Bool
False
               , getBinfoSymbolLocal :: Bool
getBinfoSymbolLocal = Bool
True
               }
  where
    name :: Text
name = case Text
code of
        "EUR" -> "Euro"
        "USD" -> "U.S. dollar"
        "GBP" -> "British pound"
        x :: Text
x     -> Text
x

getBinfoAddrsParam :: MonadIO m
                   => (WebMetrics -> ErrorCounter)
                   -> Text
                   -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam :: (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam metric :: WebMetrics -> ErrorCounter
metric name :: Text
name = do
    Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig))
    Text
p <- Text -> ActionT Except (ReaderT WebState m) Text
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param (Text -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Text
name) ActionT Except (ReaderT WebState m) Text
-> (Except -> ActionT Except (ReaderT WebState m) Text)
-> ActionT Except (ReaderT WebState m) Text
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Text
-> Except -> ActionT Except (ReaderT WebState m) Text
forall a b. a -> b -> a
const (Text -> ActionT Except (ReaderT WebState m) Text
forall (m :: * -> *) a. Monad m => a -> m a
return "")
    case Network -> Text -> Maybe [BinfoAddr]
parseBinfoAddr Network
net Text
p of
        Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
metric (String -> Except
UserError "invalid active address")
        Just xs :: [BinfoAddr]
xs -> HashSet BinfoAddr -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashSet BinfoAddr -> WebT m (HashSet BinfoAddr))
-> HashSet BinfoAddr -> WebT m (HashSet BinfoAddr)
forall a b. (a -> b) -> a -> b
$ [BinfoAddr] -> HashSet BinfoAddr
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList [BinfoAddr]
xs

getBinfoActive :: MonadIO m
               => (WebMetrics -> ErrorCounter)
               -> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive :: (WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive metric :: WebMetrics -> ErrorCounter
metric = do
    HashSet BinfoAddr
active <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "active"
    HashSet BinfoAddr
p2sh <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "activeP2SH"
    HashSet BinfoAddr
bech32 <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
metric "activeBech32"
    let xspec :: DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec d :: DeriveType
d b :: BinfoAddr
b = (\x :: XPubKey
x -> (XPubKey
x, XPubKey -> DeriveType -> XPubSpec
XPubSpec XPubKey
x DeriveType
d)) (XPubKey -> (XPubKey, XPubSpec))
-> Maybe XPubKey -> Maybe (XPubKey, XPubSpec)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinfoAddr -> Maybe XPubKey
xpub BinfoAddr
b
        xspecs :: HashMap XPubKey XPubSpec
xspecs = [(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec)
-> [(XPubKey, XPubSpec)] -> HashMap XPubKey XPubSpec
forall a b. (a -> b) -> a -> b
$ [[(XPubKey, XPubSpec)]] -> [(XPubKey, XPubSpec)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
                 [ (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveNormal) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
active)
                 , (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveP2SH) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
p2sh)
                 , (BinfoAddr -> Maybe (XPubKey, XPubSpec))
-> [BinfoAddr] -> [(XPubKey, XPubSpec)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (DeriveType -> BinfoAddr -> Maybe (XPubKey, XPubSpec)
xspec DeriveType
DeriveP2WPKH) (HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
bech32)
                 ]
        addrs :: HashSet Address
addrs = [Address] -> HashSet Address
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([Address] -> HashSet Address)
-> ([BinfoAddr] -> [Address]) -> [BinfoAddr] -> HashSet Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BinfoAddr -> Maybe Address) -> [BinfoAddr] -> [Address]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoAddr -> Maybe Address
addr ([BinfoAddr] -> HashSet Address) -> [BinfoAddr] -> HashSet Address
forall a b. (a -> b) -> a -> b
$ HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
active
    (HashMap XPubKey XPubSpec, HashSet Address)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashMap XPubKey XPubSpec
xspecs, HashSet Address
addrs)
  where
    addr :: BinfoAddr -> Maybe Address
addr (BinfoAddr a :: Address
a) = Address -> Maybe Address
forall a. a -> Maybe a
Just Address
a
    addr (BinfoXpub x :: XPubKey
x) = Maybe Address
forall a. Maybe a
Nothing
    xpub :: BinfoAddr -> Maybe XPubKey
xpub (BinfoXpub x :: XPubKey
x) = XPubKey -> Maybe XPubKey
forall a. a -> Maybe a
Just XPubKey
x
    xpub (BinfoAddr _) = Maybe XPubKey
forall a. Maybe a
Nothing

getNumTxId :: MonadIO m => WebT m Bool
getNumTxId :: WebT m Bool
getNumTxId = (Bool -> Bool) -> WebT m Bool -> WebT m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not (WebT m Bool -> WebT m Bool) -> WebT m Bool -> WebT m Bool
forall a b. (a -> b) -> a -> b
$ Text -> WebT m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "txidindex" WebT m Bool -> (Except -> WebT m Bool) -> WebT m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` WebT m Bool -> Except -> WebT m Bool
forall a b. a -> b -> a
const (Bool -> WebT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)

scottyBinfoUnspent :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyBinfoUnspent :: WebT m ()
scottyBinfoUnspent =
    (WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive WebMetrics -> ErrorCounter
unspentErrors WebT m (HashMap XPubKey XPubSpec, HashSet Address)
-> ((HashMap XPubKey XPubSpec, HashSet Address) -> WebT m ())
-> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(xspecs :: HashMap XPubKey XPubSpec
xspecs, addrs :: HashSet Address
addrs) ->
    WebT m Bool
forall (m :: * -> *). MonadIO m => WebT m Bool
getNumTxId WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \numtxid :: Bool
numtxid ->
    ActionT Except (ReaderT WebState m) Int
get_limit ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \limit :: Int
limit ->
    ActionT Except (ReaderT WebState m) Int32
get_min_conf ActionT Except (ReaderT WebState m) Int32
-> (Int32 -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \min_conf :: Int32
min_conf ->
    let len :: Int
len = HashSet Address -> Int
forall a. HashSet a -> Int
HashSet.size HashSet Address
addrs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HashMap XPubKey XPubSpec -> Int
forall k v. HashMap k v -> Int
HashMap.size HashMap XPubKey XPubSpec
xspecs
    in (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
unspentResponseTime Int
len (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    HashMap XPubKey [XPubUnspent]
xuns <- HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
get_xpub_unspents HashMap XPubKey XPubSpec
xspecs
    [Unspent]
auns <- HashSet Address -> ActionT Except (ReaderT WebState m) [Unspent]
get_addr_unspents HashSet Address
addrs
    Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Word32
height <- ActionT Except (ReaderT WebState m) Word32
get_height
    let g :: XPubKey -> [XPubUnspent] -> [BinfoUnspent]
g k :: XPubKey
k = (XPubUnspent -> BinfoUnspent) -> [XPubUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Word32 -> XPubKey -> XPubUnspent -> BinfoUnspent
xpub_unspent Bool
numtxid Word32
height XPubKey
k)
        xbus :: [BinfoUnspent]
xbus = ((XPubKey, [XPubUnspent]) -> [BinfoUnspent])
-> [(XPubKey, [XPubUnspent])] -> [BinfoUnspent]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((XPubKey -> [XPubUnspent] -> [BinfoUnspent])
-> (XPubKey, [XPubUnspent]) -> [BinfoUnspent]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry XPubKey -> [XPubUnspent] -> [BinfoUnspent]
g) (HashMap XPubKey [XPubUnspent] -> [(XPubKey, [XPubUnspent])]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap XPubKey [XPubUnspent]
xuns)
        abus :: [BinfoUnspent]
abus = (Unspent -> BinfoUnspent) -> [Unspent] -> [BinfoUnspent]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Word32 -> Unspent -> BinfoUnspent
unspent Bool
numtxid Word32
height) [Unspent]
auns
        f :: BinfoUnspent -> ((TxHash, Word32), BinfoUnspent)
f u :: BinfoUnspent
u@BinfoUnspent{..} =
            ((TxHash
getBinfoUnspentHash, Word32
getBinfoUnspentOutputIndex), BinfoUnspent
u)
        busm :: HashMap (TxHash, Word32) BinfoUnspent
busm = [((TxHash, Word32), BinfoUnspent)]
-> HashMap (TxHash, Word32) BinfoUnspent
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ((BinfoUnspent -> ((TxHash, Word32), BinfoUnspent))
-> [BinfoUnspent] -> [((TxHash, Word32), BinfoUnspent)]
forall a b. (a -> b) -> [a] -> [b]
map BinfoUnspent -> ((TxHash, Word32), BinfoUnspent)
f ([BinfoUnspent]
xbus [BinfoUnspent] -> [BinfoUnspent] -> [BinfoUnspent]
forall a. [a] -> [a] -> [a]
++ [BinfoUnspent]
abus))
        bus :: [BinfoUnspent]
bus =
            Int -> [BinfoUnspent] -> [BinfoUnspent]
forall a. Int -> [a] -> [a]
take Int
limit ([BinfoUnspent] -> [BinfoUnspent])
-> [BinfoUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> a -> b
$
            (BinfoUnspent -> Bool) -> [BinfoUnspent] -> [BinfoUnspent]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile ((Int32
min_conf Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<=) (Int32 -> Bool) -> (BinfoUnspent -> Int32) -> BinfoUnspent -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BinfoUnspent -> Int32
getBinfoUnspentConfirmations) ([BinfoUnspent] -> [BinfoUnspent])
-> [BinfoUnspent] -> [BinfoUnspent]
forall a b. (a -> b) -> a -> b
$
            (BinfoUnspent -> BinfoUnspent -> Ordering)
-> [BinfoUnspent] -> [BinfoUnspent]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy
            ((Int32 -> Int32 -> Ordering) -> Int32 -> Int32 -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int32 -> Int32 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Int32 -> Int32 -> Ordering)
-> (BinfoUnspent -> Int32)
-> BinfoUnspent
-> BinfoUnspent
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` BinfoUnspent -> Int32
getBinfoUnspentConfirmations)
            (HashMap (TxHash, Word32) BinfoUnspent -> [BinfoUnspent]
forall k v. HashMap k v -> [v]
HashMap.elems HashMap (TxHash, Word32) BinfoUnspent
busm)
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    Encoding -> WebT m ()
forall (m :: * -> *). Monad m => Encoding -> WebT m ()
streamEncoding (Network -> BinfoUnspents -> Encoding
binfoUnspentsToEncoding Network
net ([BinfoUnspent] -> BinfoUnspents
BinfoUnspents [BinfoUnspent]
bus))
  where
    get_limit :: ActionT Except (ReaderT WebState m) Int
get_limit = (Int -> Int)
-> ActionT Except (ReaderT WebState m) Int
-> ActionT Except (ReaderT WebState m) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min 1000) (ActionT Except (ReaderT WebState m) Int
 -> ActionT Except (ReaderT WebState m) Int)
-> ActionT Except (ReaderT WebState m) Int
-> ActionT Except (ReaderT WebState m) Int
forall a b. (a -> b) -> a -> b
$ Text -> ActionT Except (ReaderT WebState m) Int
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "limit" ActionT Except (ReaderT WebState m) Int
-> (Except -> ActionT Except (ReaderT WebState m) Int)
-> ActionT Except (ReaderT WebState m) Int
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Int
-> Except -> ActionT Except (ReaderT WebState m) Int
forall a b. a -> b -> a
const (Int -> ActionT Except (ReaderT WebState m) Int
forall (m :: * -> *) a. Monad m => a -> m a
return 250)
    get_min_conf :: ActionT Except (ReaderT WebState m) Int32
get_min_conf = Text -> ActionT Except (ReaderT WebState m) Int32
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "confirmations" ActionT Except (ReaderT WebState m) Int32
-> (Except -> ActionT Except (ReaderT WebState m) Int32)
-> ActionT Except (ReaderT WebState m) Int32
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Int32
-> Except -> ActionT Except (ReaderT WebState m) Int32
forall a b. a -> b -> a
const (Int32 -> ActionT Except (ReaderT WebState m) Int32
forall (m :: * -> *) a. Monad m => a -> m a
return 0)
    get_height :: ActionT Except (ReaderT WebState m) Word32
get_height =
        ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> (Maybe BlockHash -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
unspentErrors Except
ThingNotFound
        Just bh :: BlockHash
bh -> BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> (Maybe BlockData -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
unspentErrors Except
ThingNotFound
            Just b :: BlockData
b  -> Word32 -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (BlockData -> Word32
blockDataHeight BlockData
b)
    xpub_unspent :: Bool -> Word32 -> XPubKey -> XPubUnspent -> BinfoUnspent
xpub_unspent numtxid :: Bool
numtxid height :: Word32
height xpub :: XPubKey
xpub (XPubUnspent p :: [Word32]
p u :: Unspent
u) =
        let path :: Maybe SoftPath
path = DerivPathI AnyDeriv -> Maybe SoftPath
forall t. DerivPathI t -> Maybe SoftPath
toSoft ([Word32] -> DerivPathI AnyDeriv
listToPath [Word32]
p)
            xp :: Maybe BinfoXPubPath
xp = XPubKey -> SoftPath -> BinfoXPubPath
BinfoXPubPath XPubKey
xpub (SoftPath -> BinfoXPubPath)
-> Maybe SoftPath -> Maybe BinfoXPubPath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe SoftPath
path
            bu :: BinfoUnspent
bu = Bool -> Word32 -> Unspent -> BinfoUnspent
unspent Bool
numtxid Word32
height Unspent
u
        in BinfoUnspent
bu {getBinfoUnspentXPub :: Maybe BinfoXPubPath
getBinfoUnspentXPub = Maybe BinfoXPubPath
xp}
    unspent :: Bool -> Word32 -> Unspent -> BinfoUnspent
unspent numtxid :: Bool
numtxid height :: Word32
height Unspent{..} =
        let conf :: Word32
conf = case BlockRef
unspentBlock of
                       MemRef{}     -> 0
                       BlockRef h :: Word32
h _ -> Word32
height Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
h Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ 1
            hash :: TxHash
hash = OutPoint -> TxHash
outPointHash OutPoint
unspentPoint
            idx :: Word32
idx = OutPoint -> Word32
outPointIndex OutPoint
unspentPoint
            val :: Word64
val = Word64
unspentAmount
            script :: ByteString
script = ShortByteString -> ByteString
fromShort ShortByteString
unspentScript
            txi :: BinfoTxId
txi = Bool -> TxHash -> BinfoTxId
encodeBinfoTxId Bool
numtxid TxHash
hash
        in $WBinfoUnspent :: TxHash
-> Word32
-> ByteString
-> Word64
-> Int32
-> BinfoTxId
-> Maybe BinfoXPubPath
-> BinfoUnspent
BinfoUnspent
           { getBinfoUnspentHash :: TxHash
getBinfoUnspentHash = TxHash
hash
           , getBinfoUnspentOutputIndex :: Word32
getBinfoUnspentOutputIndex = Word32
idx
           , getBinfoUnspentScript :: ByteString
getBinfoUnspentScript = ByteString
script
           , getBinfoUnspentValue :: Word64
getBinfoUnspentValue = Word64
val
           , getBinfoUnspentConfirmations :: Int32
getBinfoUnspentConfirmations = Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
conf
           , getBinfoUnspentTxIndex :: BinfoTxId
getBinfoUnspentTxIndex = BinfoTxId
txi
           , getBinfoUnspentXPub :: Maybe BinfoXPubPath
getBinfoUnspentXPub = Maybe BinfoXPubPath
forall a. Maybe a
Nothing
           }
    get_xpub_unspents :: HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
get_xpub_unspents = (XPubSpec -> ActionT Except (ReaderT WebState m) [XPubUnspent])
-> HashMap XPubKey XPubSpec
-> ActionT
     Except (ReaderT WebState m) (HashMap XPubKey [XPubUnspent])
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (XPubSpec
-> Limits -> ActionT Except (ReaderT WebState m) [XPubUnspent]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [XPubUnspent]
`xPubUnspents` Limits
forall a. Default a => a
def)
    get_addr_unspents :: HashSet Address -> ActionT Except (ReaderT WebState m) [Unspent]
get_addr_unspents = ([Address]
-> Limits -> ActionT Except (ReaderT WebState m) [Unspent]
forall (m :: * -> *).
StoreReadExtra m =>
[Address] -> Limits -> m [Unspent]
`getAddressesUnspents` Limits
forall a. Default a => a
def) ([Address] -> ActionT Except (ReaderT WebState m) [Unspent])
-> (HashSet Address -> [Address])
-> HashSet Address
-> ActionT Except (ReaderT WebState m) [Unspent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet Address -> [Address]
forall a. HashSet a -> [a]
HashSet.toList

getBinfoTxs :: (StoreReadExtra m, MonadIO m)
            => HashMap Address (Maybe BinfoXPubPath) -- address book
            -> HashSet XPubSpec -- show xpubs
            -> HashSet Address -- show addrs
            -> HashSet Address -- balance addresses
            -> BinfoFilter
            -> Bool -- numtxid
            -> Bool -- prune outputs
            -> Int64 -- starting balance
            -> ConduitT () BinfoTx m ()
getBinfoTxs :: HashMap Address (Maybe BinfoXPubPath)
-> HashSet XPubSpec
-> HashSet Address
-> HashSet Address
-> BinfoFilter
-> Bool
-> Bool
-> Int64
-> ConduitT () BinfoTx m ()
getBinfoTxs abook :: HashMap Address (Maybe BinfoXPubPath)
abook sxspecs :: HashSet XPubSpec
sxspecs saddrs :: HashSet Address
saddrs baddrs :: HashSet Address
baddrs bfilter :: BinfoFilter
bfilter numtxid :: Bool
numtxid prune :: Bool
prune bal :: Int64
bal =
    (TxRef -> TxRef -> Ordering)
-> [ConduitT () TxRef m ()] -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(a -> a -> Ordering) -> [ConduitT () a m ()] -> ConduitT () a m ()
joinStreams ((TxRef -> TxRef -> Ordering) -> TxRef -> TxRef -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip TxRef -> TxRef -> Ordering
forall a. Ord a => a -> a -> Ordering
compare) [ConduitT () TxRef m ()]
conduits ConduitT () TxRef m ()
-> ConduitM TxRef BinfoTx m () -> ConduitT () BinfoTx m ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| Int64 -> ConduitM TxRef BinfoTx m ()
forall (m :: * -> *).
StoreReadBase m =>
Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
bal
  where
    sxspecs_ls :: [XPubSpec]
sxspecs_ls = HashSet XPubSpec -> [XPubSpec]
forall a. HashSet a -> [a]
HashSet.toList HashSet XPubSpec
sxspecs
    saddrs_ls :: [Address]
saddrs_ls = HashSet Address -> [Address]
forall a. HashSet a -> [a]
HashSet.toList HashSet Address
saddrs
    conduits :: [ConduitT () TxRef m ()]
conduits = (XPubSpec -> ConduitT () TxRef m ())
-> [XPubSpec] -> [ConduitT () TxRef m ()]
forall a b. (a -> b) -> [a] -> [b]
map XPubSpec -> ConduitT () TxRef m ()
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> ConduitT () TxRef m ()
xpub_c [XPubSpec]
sxspecs_ls [ConduitT () TxRef m ()]
-> [ConduitT () TxRef m ()] -> [ConduitT () TxRef m ()]
forall a. Semigroup a => a -> a -> a
<> (Address -> ConduitT () TxRef m ())
-> [Address] -> [ConduitT () TxRef m ()]
forall a b. (a -> b) -> [a] -> [b]
map Address -> ConduitT () TxRef m ()
forall (m :: * -> *).
StoreReadExtra m =>
Address -> ConduitT () TxRef m ()
addr_c [Address]
saddrs_ls
    xpub_c :: XPubSpec -> ConduitT () TxRef m ()
xpub_c x :: XPubSpec
x = (Limits -> m [TxRef])
-> (TxRef -> TxHash) -> Limits -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(Limits -> m [a]) -> (a -> TxHash) -> Limits -> ConduitT () a m ()
streamThings (XPubSpec -> Limits -> m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
x) TxRef -> TxHash
txRefHash Limits
forall a. Default a => a
def
    addr_c :: Address -> ConduitT () TxRef m ()
addr_c a :: Address
a = (Limits -> m [TxRef])
-> (TxRef -> TxHash) -> Limits -> ConduitT () TxRef m ()
forall (m :: * -> *) a.
Monad m =>
(Limits -> m [a]) -> (a -> TxHash) -> Limits -> ConduitT () a m ()
streamThings (Address -> Limits -> m [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
Address -> Limits -> m [TxRef]
getAddressTxs Address
a) TxRef -> TxHash
txRefHash Limits
forall a. Default a => a
def
    binfo_tx :: Int64 -> Transaction -> BinfoTx
binfo_tx b :: Int64
b = Bool
-> HashMap Address (Maybe BinfoXPubPath)
-> Bool
-> Int64
-> Transaction
-> BinfoTx
toBinfoTx Bool
numtxid HashMap Address (Maybe BinfoXPubPath)
abook Bool
prune Int64
b
    compute_bal_change :: BinfoTx -> a
compute_bal_change BinfoTx{..} =
        let ins :: [BinfoTxOutput]
ins = (BinfoTxInput -> Maybe BinfoTxOutput)
-> [BinfoTxInput] -> [BinfoTxOutput]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoTxInput -> Maybe BinfoTxOutput
getBinfoTxInputPrevOut [BinfoTxInput]
getBinfoTxInputs
            out :: [BinfoTxOutput]
out = [BinfoTxOutput]
getBinfoTxOutputs
            f :: Bool -> BinfoTxOutput -> a
f b :: Bool
b BinfoTxOutput{..} =
                let val :: a
val = Word64 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
getBinfoTxOutputValue
                in case Maybe Address
getBinfoTxOutputAddress of
                       Nothing -> 0
                       Just a :: Address
a | Address
a Address -> HashSet Address -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet Address
baddrs ->
                                    if Bool
b then a
val else a -> a
forall a. Num a => a -> a
negate a
val
                              | Bool
otherwise -> 0
        in [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([a] -> a) -> [a] -> a
forall a b. (a -> b) -> a -> b
$ (BinfoTxOutput -> a) -> [BinfoTxOutput] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> BinfoTxOutput -> a
forall a. Num a => Bool -> BinfoTxOutput -> a
f Bool
False) [BinfoTxOutput]
ins [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> (BinfoTxOutput -> a) -> [BinfoTxOutput] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> BinfoTxOutput -> a
forall a. Num a => Bool -> BinfoTxOutput -> a
f Bool
True) [BinfoTxOutput]
out
    go :: Int64 -> ConduitT TxRef BinfoTx m ()
go b :: Int64
b = ConduitT TxRef BinfoTx m (Maybe TxRef)
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await ConduitT TxRef BinfoTx m (Maybe TxRef)
-> (Maybe TxRef -> ConduitT TxRef BinfoTx m ())
-> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just (TxRef _ t :: TxHash
t) -> m (Maybe Transaction)
-> ConduitT TxRef BinfoTx m (Maybe Transaction)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (TxHash -> m (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
t) ConduitT TxRef BinfoTx m (Maybe Transaction)
-> (Maybe Transaction -> ConduitT TxRef BinfoTx m ())
-> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b
            Just x :: Transaction
x -> do
                let a :: BinfoTx
a = Int64 -> Transaction -> BinfoTx
binfo_tx Int64
b Transaction
x
                    b' :: Int64
b' = Int64
b Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- BinfoTx -> Int64
forall a. Num a => BinfoTx -> a
compute_bal_change BinfoTx
a
                    c :: Bool
c = Maybe Word32 -> Bool
forall a. Maybe a -> Bool
isJust (BinfoTx -> Maybe Word32
getBinfoTxBlockHeight BinfoTx
a)
                    Just (d :: Int64
d, _) = BinfoTx -> Maybe (Int64, Int64)
getBinfoTxResultBal BinfoTx
a
                    r :: Int64
r = Int64
d Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BinfoTx -> Word64
getBinfoTxFee BinfoTx
a)
                case BinfoFilter
bfilter of
                    BinfoFilterAll ->
                        BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterSent
                        | 0 Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
r -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterReceived
                        | Int64
r Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> 0 -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterMoved
                        | Int64
r Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== 0 -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterConfirmed
                        | Bool
c -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                        | Bool
otherwise -> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'
                    BinfoFilterMempool
                        | Bool
c -> () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                        | Bool
otherwise -> BinfoTx -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield BinfoTx
a ConduitT TxRef BinfoTx m ()
-> ConduitT TxRef BinfoTx m () -> ConduitT TxRef BinfoTx m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int64 -> ConduitT TxRef BinfoTx m ()
go Int64
b'

scottyMultiAddr :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyMultiAddr :: WebT m ()
scottyMultiAddr =
    ActionT
  Except
  (ReaderT WebState m)
  (HashSet Address, HashSet XPubKey, HashSet Address,
   HashSet XPubKey, HashMap XPubKey XPubSpec)
get_addrs ActionT
  Except
  (ReaderT WebState m)
  (HashSet Address, HashSet XPubKey, HashSet Address,
   HashSet XPubKey, HashMap XPubKey XPubSpec)
-> ((HashSet Address, HashSet XPubKey, HashSet Address,
     HashSet XPubKey, HashMap XPubKey XPubSpec)
    -> WebT m ())
-> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(addrs' :: HashSet Address
addrs', xpubs :: HashSet XPubKey
xpubs, saddrs :: HashSet Address
saddrs, sxpubs :: HashSet XPubKey
sxpubs, xspecs :: HashMap XPubKey XPubSpec
xspecs) ->
    WebT m Bool
forall (m :: * -> *). MonadIO m => WebT m Bool
getNumTxId WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \numtxid :: Bool
numtxid ->
    ReaderT WebState m (TVar (HashMap Text BinfoTicker))
-> ActionT
     Except (ReaderT WebState m) (TVar (HashMap Text BinfoTicker))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> TVar (HashMap Text BinfoTicker))
-> ReaderT WebState m (TVar (HashMap Text BinfoTicker))
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> TVar (HashMap Text BinfoTicker)
webTicker) ActionT
  Except (ReaderT WebState m) (TVar (HashMap Text BinfoTicker))
-> (TVar (HashMap Text BinfoTicker) -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ticker :: TVar (HashMap Text BinfoTicker)
ticker ->
    TVar (HashMap Text BinfoTicker)
-> ActionT Except (ReaderT WebState m) BinfoSymbol
forall (m :: * -> *) b.
(ScottyError b, MonadIO m) =>
TVar (HashMap Text BinfoTicker) -> ActionT b m BinfoSymbol
get_price TVar (HashMap Text BinfoTicker)
ticker ActionT Except (ReaderT WebState m) BinfoSymbol
-> (BinfoSymbol -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \local :: BinfoSymbol
local ->
    WebT m Bool
get_cashaddr WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \cashaddr :: Bool
cashaddr ->
    ActionT Except (ReaderT WebState m) Int
get_offset ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \offset :: Int
offset ->
    ActionT Except (ReaderT WebState m) Int
get_count ActionT Except (ReaderT WebState m) Int
-> (Int -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \n :: Int
n ->
    WebT m Bool
get_prune WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \prune :: Bool
prune ->
    ActionT Except (ReaderT WebState m) BinfoFilter
get_filter ActionT Except (ReaderT WebState m) BinfoFilter
-> (BinfoFilter -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \fltr :: BinfoFilter
fltr ->
    let len :: Int
len = HashSet Address -> Int
forall a. HashSet a -> Int
HashSet.size HashSet Address
addrs' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ HashSet XPubKey -> Int
forall a. HashSet a -> Int
HashSet.size HashSet XPubKey
xpubs
    in (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
multiaddrResponseTime Int
len (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    HashMap XPubKey [XPubBal]
xbals <- HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal])
get_xbals HashMap XPubKey XPubSpec
xspecs
    HashMap XPubKey Int
xtxns <- (XPubSpec -> ActionT Except (ReaderT WebState m) Int)
-> HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey Int)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM XPubSpec -> ActionT Except (ReaderT WebState m) Int
forall (f :: * -> *). StoreReadExtra f => XPubSpec -> f Int
count_txs HashMap XPubKey XPubSpec
xspecs
    let sxbals :: HashMap XPubKey [XPubBal]
sxbals = HashSet XPubKey
-> HashMap XPubKey [XPubBal] -> HashMap XPubKey [XPubBal]
forall a v.
(Eq a, Hashable a) =>
HashSet a -> HashMap a v -> HashMap a v
subset HashSet XPubKey
sxpubs HashMap XPubKey [XPubBal]
xbals
        xabals :: HashMap Address Balance
xabals = HashMap XPubKey [XPubBal] -> HashMap Address Balance
forall k. HashMap k [XPubBal] -> HashMap Address Balance
compute_xabals HashMap XPubKey [XPubBal]
xbals
        addrs :: HashSet Address
addrs = HashSet Address
addrs' HashSet Address -> HashSet Address -> HashSet Address
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
`HashSet.difference` HashMap Address Balance -> HashSet Address
forall k a. HashMap k a -> HashSet k
HashMap.keysSet HashMap Address Balance
xabals
    HashMap Address Balance
abals <- HashSet Address
-> ActionT Except (ReaderT WebState m) (HashMap Address Balance)
get_abals HashSet Address
addrs
    let sxspecs :: HashSet XPubSpec
sxspecs = HashSet XPubKey -> HashMap XPubKey XPubSpec -> HashSet XPubSpec
forall a k.
(Eq a, Eq k, Hashable a, Hashable k) =>
HashSet k -> HashMap k a -> HashSet a
compute_sxspecs HashSet XPubKey
sxpubs HashMap XPubKey XPubSpec
xspecs
        sxabals :: HashMap Address Balance
sxabals = HashMap XPubKey [XPubBal] -> HashMap Address Balance
forall k. HashMap k [XPubBal] -> HashMap Address Balance
compute_xabals HashMap XPubKey [XPubBal]
sxbals
        sabals :: HashMap Address Balance
sabals = HashSet Address
-> HashMap Address Balance -> HashMap Address Balance
forall a v.
(Eq a, Hashable a) =>
HashSet a -> HashMap a v -> HashMap a v
subset HashSet Address
saddrs HashMap Address Balance
abals
        sallbals :: HashMap Address Balance
sallbals = HashMap Address Balance
sabals HashMap Address Balance
-> HashMap Address Balance -> HashMap Address Balance
forall a. Semigroup a => a -> a -> a
<> HashMap Address Balance
sxabals
        sbal :: Word64
sbal = HashMap Address Balance -> Word64
forall k. HashMap k Balance -> Word64
compute_bal HashMap Address Balance
sallbals
        allbals :: HashMap Address Balance
allbals = HashMap Address Balance
abals HashMap Address Balance
-> HashMap Address Balance -> HashMap Address Balance
forall a. Semigroup a => a -> a -> a
<> HashMap Address Balance
xabals
        abook :: HashMap Address (Maybe BinfoXPubPath)
abook = HashSet Address
-> HashMap XPubKey [XPubBal]
-> HashMap Address (Maybe BinfoXPubPath)
compute_abook HashSet Address
addrs HashMap XPubKey [XPubBal]
xbals
        sxaddrs :: HashSet Address
sxaddrs = HashMap XPubKey [XPubBal] -> HashSet Address
forall k. HashMap k [XPubBal] -> HashSet Address
compute_xaddrs HashMap XPubKey [XPubBal]
sxbals
        salladdrs :: HashSet Address
salladdrs = HashSet Address
saddrs HashSet Address -> HashSet Address -> HashSet Address
forall a. Semigroup a => a -> a -> a
<> HashSet Address
sxaddrs
        bal :: Word64
bal = HashMap Address Balance -> Word64
forall k. HashMap k Balance -> Word64
compute_bal HashMap Address Balance
allbals
    let ibal :: Int64
ibal = Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
sbal
    [BinfoTx]
ftxs <-
        ReaderT WebState m [BinfoTx]
-> ActionT Except (ReaderT WebState m) [BinfoTx]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [BinfoTx]
 -> ActionT Except (ReaderT WebState m) [BinfoTx])
-> (ConduitT () Void (ReaderT WebState m) [BinfoTx]
    -> ReaderT WebState m [BinfoTx])
-> ConduitT () Void (ReaderT WebState m) [BinfoTx]
-> ActionT Except (ReaderT WebState m) [BinfoTx]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConduitT () Void (ReaderT WebState m) [BinfoTx]
-> ReaderT WebState m [BinfoTx]
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit (ConduitT () Void (ReaderT WebState m) [BinfoTx]
 -> ActionT Except (ReaderT WebState m) [BinfoTx])
-> ConduitT () Void (ReaderT WebState m) [BinfoTx]
-> ActionT Except (ReaderT WebState m) [BinfoTx]
forall a b. (a -> b) -> a -> b
$
        HashMap Address (Maybe BinfoXPubPath)
-> HashSet XPubSpec
-> HashSet Address
-> HashSet Address
-> BinfoFilter
-> Bool
-> Bool
-> Int64
-> ConduitT () BinfoTx (ReaderT WebState m) ()
forall (m :: * -> *).
(StoreReadExtra m, MonadIO m) =>
HashMap Address (Maybe BinfoXPubPath)
-> HashSet XPubSpec
-> HashSet Address
-> HashSet Address
-> BinfoFilter
-> Bool
-> Bool
-> Int64
-> ConduitT () BinfoTx m ()
getBinfoTxs HashMap Address (Maybe BinfoXPubPath)
abook HashSet XPubSpec
sxspecs HashSet Address
saddrs HashSet Address
salladdrs BinfoFilter
fltr Bool
numtxid Bool
prune Int64
ibal ConduitT () BinfoTx (ReaderT WebState m) ()
-> ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
-> ConduitT () Void (ReaderT WebState m) [BinfoTx]
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.|
        (Int -> ConduitT BinfoTx Void (ReaderT WebState m) ()
forall (m :: * -> *) a o. Monad m => Int -> ConduitT a o m ()
dropC Int
offset ConduitT BinfoTx Void (ReaderT WebState m) ()
-> ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
-> ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> ConduitT BinfoTx BinfoTx (ReaderT WebState m) ()
forall (m :: * -> *) a. Monad m => Int -> ConduitT a a m ()
takeC Int
n ConduitT BinfoTx BinfoTx (ReaderT WebState m) ()
-> ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
-> ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM BinfoTx Void (ReaderT WebState m) [BinfoTx]
forall (m :: * -> *) a o. Monad m => ConduitT a o m [a]
sinkList)
    BlockData
best <- ActionT Except (ReaderT WebState m) BlockData
get_best_block
    Word32
peers <- ActionT Except (ReaderT WebState m) Word32
get_peers
    Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    let baddrs :: [BinfoBalance]
baddrs = HashMap Address Balance
-> HashMap XPubKey [XPubBal]
-> HashMap XPubKey Int
-> [BinfoBalance]
toBinfoAddrs HashMap Address Balance
sabals HashMap XPubKey [XPubBal]
sxbals HashMap XPubKey Int
xtxns
        abaddrs :: [BinfoBalance]
abaddrs = HashMap Address Balance
-> HashMap XPubKey [XPubBal]
-> HashMap XPubKey Int
-> [BinfoBalance]
toBinfoAddrs HashMap Address Balance
abals HashMap XPubKey [XPubBal]
xbals HashMap XPubKey Int
xtxns
        recv :: Word64
recv = [Word64] -> Word64
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Word64] -> Word64) -> [Word64] -> Word64
forall a b. (a -> b) -> a -> b
$ (BinfoBalance -> Word64) -> [BinfoBalance] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map BinfoBalance -> Word64
getBinfoAddrReceived [BinfoBalance]
abaddrs
        sent :: Word64
sent = [Word64] -> Word64
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Word64] -> Word64) -> [Word64] -> Word64
forall a b. (a -> b) -> a -> b
$ (BinfoBalance -> Word64) -> [BinfoBalance] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map BinfoBalance -> Word64
getBinfoAddrSent [BinfoBalance]
abaddrs
        txn :: Word64
txn = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ [BinfoTx] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [BinfoTx]
ftxs
        wallet :: BinfoWallet
wallet =
            $WBinfoWallet :: Word64 -> Word64 -> Word64 -> Word64 -> Word64 -> BinfoWallet
BinfoWallet
            { getBinfoWalletBalance :: Word64
getBinfoWalletBalance = Word64
bal
            , getBinfoWalletTxCount :: Word64
getBinfoWalletTxCount = Word64
txn
            , getBinfoWalletFilteredCount :: Word64
getBinfoWalletFilteredCount = Word64
txn
            , getBinfoWalletTotalReceived :: Word64
getBinfoWalletTotalReceived = Word64
recv
            , getBinfoWalletTotalSent :: Word64
getBinfoWalletTotalSent = Word64
sent
            }
        coin :: BinfoSymbol
coin = Network -> BinfoSymbol
netBinfoSymbol Network
net
        block :: BinfoBlockInfo
block =
            $WBinfoBlockInfo :: BlockHash -> Word32 -> Word32 -> Word32 -> BinfoBlockInfo
BinfoBlockInfo
            { getBinfoBlockInfoHash :: BlockHash
getBinfoBlockInfoHash = BlockHeader -> BlockHash
H.headerHash (BlockData -> BlockHeader
blockDataHeader BlockData
best)
            , getBinfoBlockInfoHeight :: Word32
getBinfoBlockInfoHeight = BlockData -> Word32
blockDataHeight BlockData
best
            , getBinfoBlockInfoTime :: Word32
getBinfoBlockInfoTime = BlockHeader -> Word32
H.blockTimestamp (BlockData -> BlockHeader
blockDataHeader BlockData
best)
            , getBinfoBlockInfoIndex :: Word32
getBinfoBlockInfoIndex = BlockData -> Word32
blockDataHeight BlockData
best
            }
        info :: BinfoInfo
info =
            $WBinfoInfo :: Word32
-> Double
-> BinfoSymbol
-> BinfoSymbol
-> BinfoBlockInfo
-> BinfoInfo
BinfoInfo
            { getBinfoConnected :: Word32
getBinfoConnected = Word32
peers
            , getBinfoConversion :: Double
getBinfoConversion = 100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* 1000
            , getBinfoLocal :: BinfoSymbol
getBinfoLocal = BinfoSymbol
local
            , getBinfoBTC :: BinfoSymbol
getBinfoBTC = BinfoSymbol
coin
            , getBinfoLatestBlock :: BinfoBlockInfo
getBinfoLatestBlock = BinfoBlockInfo
block
            }
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    Encoding -> WebT m ()
forall (m :: * -> *). Monad m => Encoding -> WebT m ()
streamEncoding (Encoding -> WebT m ()) -> Encoding -> WebT m ()
forall a b. (a -> b) -> a -> b
$ Network -> BinfoMultiAddr -> Encoding
binfoMultiAddrToEncoding Network
net
        $WBinfoMultiAddr :: [BinfoBalance]
-> BinfoWallet
-> [BinfoTx]
-> BinfoInfo
-> Bool
-> Bool
-> BinfoMultiAddr
BinfoMultiAddr
        { getBinfoMultiAddrAddresses :: [BinfoBalance]
getBinfoMultiAddrAddresses = [BinfoBalance]
baddrs
        , getBinfoMultiAddrWallet :: BinfoWallet
getBinfoMultiAddrWallet = BinfoWallet
wallet
        , getBinfoMultiAddrTxs :: [BinfoTx]
getBinfoMultiAddrTxs = [BinfoTx]
ftxs
        , getBinfoMultiAddrInfo :: BinfoInfo
getBinfoMultiAddrInfo = BinfoInfo
info
        , getBinfoMultiAddrRecommendFee :: Bool
getBinfoMultiAddrRecommendFee = Bool
True
        , getBinfoMultiAddrCashAddr :: Bool
getBinfoMultiAddrCashAddr = Bool
cashaddr
        }
  where
    count_txs :: XPubSpec -> f Int
count_txs xspec :: XPubSpec
xspec = [TxRef] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([TxRef] -> Int) -> f [TxRef] -> f Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> XPubSpec -> Limits -> f [TxRef]
forall (m :: * -> *).
StoreReadExtra m =>
XPubSpec -> Limits -> m [TxRef]
xPubTxs XPubSpec
xspec Limits
forall a. Default a => a
def
    get_filter :: ActionT Except (ReaderT WebState m) BinfoFilter
get_filter = Text -> ActionT Except (ReaderT WebState m) BinfoFilter
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "filter" ActionT Except (ReaderT WebState m) BinfoFilter
-> (Except -> ActionT Except (ReaderT WebState m) BinfoFilter)
-> ActionT Except (ReaderT WebState m) BinfoFilter
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) BinfoFilter
-> Except -> ActionT Except (ReaderT WebState m) BinfoFilter
forall a b. a -> b -> a
const (BinfoFilter -> ActionT Except (ReaderT WebState m) BinfoFilter
forall (m :: * -> *) a. Monad m => a -> m a
return BinfoFilter
BinfoFilterAll)
    get_best_block :: ActionT Except (ReaderT WebState m) BlockData
get_best_block =
        ActionT Except (ReaderT WebState m) (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock ActionT Except (ReaderT WebState m) (Maybe BlockHash)
-> (Maybe BlockHash
    -> ActionT Except (ReaderT WebState m) BlockData)
-> ActionT Except (ReaderT WebState m) BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
multiaddrErrors Except
ThingNotFound
        Just bh :: BlockHash
bh -> BlockHash -> ActionT Except (ReaderT WebState m) (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock BlockHash
bh ActionT Except (ReaderT WebState m) (Maybe BlockData)
-> (Maybe BlockData
    -> ActionT Except (ReaderT WebState m) BlockData)
-> ActionT Except (ReaderT WebState m) BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Nothing -> (WebMetrics -> ErrorCounter)
-> Except -> ActionT Except (ReaderT WebState m) BlockData
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
multiaddrErrors Except
ThingNotFound
            Just b :: BlockData
b  -> BlockData -> ActionT Except (ReaderT WebState m) BlockData
forall (m :: * -> *) a. Monad m => a -> m a
return BlockData
b
    get_price :: TVar (HashMap Text BinfoTicker) -> ActionT b m BinfoSymbol
get_price ticker :: TVar (HashMap Text BinfoTicker)
ticker = do
        Text
code <- Text -> Text
T.toUpper (Text -> Text) -> ActionT b m Text -> ActionT b m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ActionT b m Text
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "currency" ActionT b m Text -> (b -> ActionT b m Text) -> ActionT b m Text
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT b m Text -> b -> ActionT b m Text
forall a b. a -> b -> a
const (Text -> ActionT b m Text
forall (m :: * -> *) a. Monad m => a -> m a
return "USD")
        HashMap Text BinfoTicker
prices <- TVar (HashMap Text BinfoTicker)
-> ActionT b m (HashMap Text BinfoTicker)
forall (m :: * -> *) a. MonadIO m => TVar a -> m a
readTVarIO TVar (HashMap Text BinfoTicker)
ticker
        case Text -> HashMap Text BinfoTicker -> Maybe BinfoTicker
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
code HashMap Text BinfoTicker
prices of
            Nothing -> BinfoSymbol -> ActionT b m BinfoSymbol
forall (m :: * -> *) a. Monad m => a -> m a
return BinfoSymbol
forall a. Default a => a
def
            Just p :: BinfoTicker
p  -> BinfoSymbol -> ActionT b m BinfoSymbol
forall (m :: * -> *) a. Monad m => a -> m a
return (BinfoSymbol -> ActionT b m BinfoSymbol)
-> BinfoSymbol -> ActionT b m BinfoSymbol
forall a b. (a -> b) -> a -> b
$ Text -> BinfoTicker -> BinfoSymbol
binfoTickerToSymbol Text
code BinfoTicker
p
    get_prune :: WebT m Bool
get_prune = (Bool -> Bool) -> WebT m Bool -> WebT m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Bool
not (WebT m Bool -> WebT m Bool) -> WebT m Bool -> WebT m Bool
forall a b. (a -> b) -> a -> b
$ Text -> WebT m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "no_compact"
        WebT m Bool -> (Except -> WebT m Bool) -> WebT m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` WebT m Bool -> Except -> WebT m Bool
forall a b. a -> b -> a
const (Bool -> WebT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)
    get_cashaddr :: WebT m Bool
get_cashaddr = Text -> WebT m Bool
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "cashaddr"
        WebT m Bool -> (Except -> WebT m Bool) -> WebT m Bool
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` WebT m Bool -> Except -> WebT m Bool
forall a b. a -> b -> a
const (Bool -> WebT m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False)
    get_count :: ActionT Except (ReaderT WebState m) Int
get_count = do
        Word32
d <- ReaderT WebState m Word32
-> ActionT Except (ReaderT WebState m) Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Word32) -> ReaderT WebState m Word32
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebLimits -> Word32
maxLimitDefault (WebLimits -> Word32)
-> (WebState -> WebLimits) -> WebState -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig))
        Word32
x <- ReaderT WebState m Word32
-> ActionT Except (ReaderT WebState m) Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Word32) -> ReaderT WebState m Word32
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebLimits -> Word32
maxLimitFull (WebLimits -> Word32)
-> (WebState -> WebLimits) -> WebState -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig))
        Word32
i <- Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
min Word32
x (Word32 -> Word32)
-> ActionT Except (ReaderT WebState m) Word32
-> ActionT Except (ReaderT WebState m) Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ActionT Except (ReaderT WebState m) Word32
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "n" ActionT Except (ReaderT WebState m) Word32
-> (Except -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Word32
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall a b. a -> b -> a
const (Word32 -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a. Monad m => a -> m a
return Word32
d))
        Int -> ActionT Except (ReaderT WebState m) Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
i :: Int)
    get_offset :: ActionT Except (ReaderT WebState m) Int
get_offset = do
        Word32
x <- ReaderT WebState m Word32
-> ActionT Except (ReaderT WebState m) Word32
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((WebState -> Word32) -> ReaderT WebState m Word32
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebLimits -> Word32
maxLimitOffset (WebLimits -> Word32)
-> (WebState -> WebLimits) -> WebState -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig))
        Word32
o <- Text -> ActionT Except (ReaderT WebState m) Word32
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "offset" ActionT Except (ReaderT WebState m) Word32
-> (Except -> ActionT Except (ReaderT WebState m) Word32)
-> ActionT Except (ReaderT WebState m) Word32
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Word32
-> Except -> ActionT Except (ReaderT WebState m) Word32
forall a b. a -> b -> a
const (Word32 -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a. Monad m => a -> m a
return 0)
        Bool -> WebT m () -> WebT m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word32
o Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
x) (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$
            (WebMetrics -> ErrorCounter) -> Except -> WebT m ()
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
multiaddrErrors (Except -> WebT m ()) -> Except -> WebT m ()
forall a b. (a -> b) -> a -> b
$
            String -> Except
UserError (String -> Except) -> String -> Except
forall a b. (a -> b) -> a -> b
$ "offset exceeded: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Word32 -> String
forall a. Show a => a -> String
show Word32
o String -> ShowS
forall a. Semigroup a => a -> a -> a
<> " > " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Word32 -> String
forall a. Show a => a -> String
show Word32
x
        Int -> ActionT Except (ReaderT WebState m) Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
o :: Int)
    subset :: HashSet a -> HashMap a v -> HashMap a v
subset ks :: HashSet a
ks =
        (a -> v -> Bool) -> HashMap a v -> HashMap a v
forall k v. (k -> v -> Bool) -> HashMap k v -> HashMap k v
HashMap.filterWithKey (\k :: a
k _ -> a
k a -> HashSet a -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet a
ks)
    compute_sxspecs :: HashSet k -> HashMap k a -> HashSet a
compute_sxspecs sxpubs :: HashSet k
sxpubs =
        [a] -> HashSet a
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([a] -> HashSet a)
-> (HashMap k a -> [a]) -> HashMap k a -> HashSet a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k a -> [a]
forall k v. HashMap k v -> [v]
HashMap.elems (HashMap k a -> [a])
-> (HashMap k a -> HashMap k a) -> HashMap k a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet k -> HashMap k a -> HashMap k a
forall a v.
(Eq a, Hashable a) =>
HashSet a -> HashMap a v -> HashMap a v
subset HashSet k
sxpubs
    addr :: BinfoAddr -> Maybe Address
addr (BinfoAddr a :: Address
a) = Address -> Maybe Address
forall a. a -> Maybe a
Just Address
a
    addr (BinfoXpub x :: XPubKey
x) = Maybe Address
forall a. Maybe a
Nothing
    xpub :: BinfoAddr -> Maybe XPubKey
xpub (BinfoXpub x :: XPubKey
x) = XPubKey -> Maybe XPubKey
forall a. a -> Maybe a
Just XPubKey
x
    xpub (BinfoAddr _) = Maybe XPubKey
forall a. Maybe a
Nothing
    get_addrs :: ActionT
  Except
  (ReaderT WebState m)
  (HashSet Address, HashSet XPubKey, HashSet Address,
   HashSet XPubKey, HashMap XPubKey XPubSpec)
get_addrs = do
        (xspecs :: HashMap XPubKey XPubSpec
xspecs, addrs :: HashSet Address
addrs) <- (WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter)
-> WebT m (HashMap XPubKey XPubSpec, HashSet Address)
getBinfoActive WebMetrics -> ErrorCounter
multiaddrErrors
        HashSet BinfoAddr
sh <- (WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
forall (m :: * -> *).
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Text -> WebT m (HashSet BinfoAddr)
getBinfoAddrsParam WebMetrics -> ErrorCounter
multiaddrErrors "onlyShow"
        let xpubs :: HashSet XPubKey
xpubs = HashMap XPubKey XPubSpec -> HashSet XPubKey
forall k a. HashMap k a -> HashSet k
HashMap.keysSet HashMap XPubKey XPubSpec
xspecs
            actives :: HashSet BinfoAddr
actives = (Address -> BinfoAddr) -> HashSet Address -> HashSet BinfoAddr
forall b a.
(Hashable b, Eq b) =>
(a -> b) -> HashSet a -> HashSet b
HashSet.map Address -> BinfoAddr
BinfoAddr HashSet Address
addrs HashSet BinfoAddr -> HashSet BinfoAddr -> HashSet BinfoAddr
forall a. Semigroup a => a -> a -> a
<>
                      (XPubKey -> BinfoAddr) -> HashSet XPubKey -> HashSet BinfoAddr
forall b a.
(Hashable b, Eq b) =>
(a -> b) -> HashSet a -> HashSet b
HashSet.map XPubKey -> BinfoAddr
BinfoXpub HashSet XPubKey
xpubs
            sh' :: HashSet BinfoAddr
sh' = if HashSet BinfoAddr -> Bool
forall a. HashSet a -> Bool
HashSet.null HashSet BinfoAddr
sh then HashSet BinfoAddr
actives else HashSet BinfoAddr
sh
            saddrs :: HashSet Address
saddrs = [Address] -> HashSet Address
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([Address] -> HashSet Address)
-> ([BinfoAddr] -> [Address]) -> [BinfoAddr] -> HashSet Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BinfoAddr -> Maybe Address) -> [BinfoAddr] -> [Address]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoAddr -> Maybe Address
addr ([BinfoAddr] -> HashSet Address) -> [BinfoAddr] -> HashSet Address
forall a b. (a -> b) -> a -> b
$ HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
sh'
            sxpubs :: HashSet XPubKey
sxpubs = [XPubKey] -> HashSet XPubKey
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([XPubKey] -> HashSet XPubKey)
-> ([BinfoAddr] -> [XPubKey]) -> [BinfoAddr] -> HashSet XPubKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BinfoAddr -> Maybe XPubKey) -> [BinfoAddr] -> [XPubKey]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe BinfoAddr -> Maybe XPubKey
xpub ([BinfoAddr] -> HashSet XPubKey) -> [BinfoAddr] -> HashSet XPubKey
forall a b. (a -> b) -> a -> b
$ HashSet BinfoAddr -> [BinfoAddr]
forall a. HashSet a -> [a]
HashSet.toList HashSet BinfoAddr
sh'
        (HashSet Address, HashSet XPubKey, HashSet Address,
 HashSet XPubKey, HashMap XPubKey XPubSpec)
-> ActionT
     Except
     (ReaderT WebState m)
     (HashSet Address, HashSet XPubKey, HashSet Address,
      HashSet XPubKey, HashMap XPubKey XPubSpec)
forall (m :: * -> *) a. Monad m => a -> m a
return (HashSet Address
addrs, HashSet XPubKey
xpubs, HashSet Address
saddrs, HashSet XPubKey
sxpubs, HashMap XPubKey XPubSpec
xspecs)
    get_xbals :: HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal])
get_xbals =
        let f :: XPubBal -> Bool
f = Bool -> Bool
not (Bool -> Bool) -> (XPubBal -> Bool) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Balance -> Bool
nullBalance (Balance -> Bool) -> (XPubBal -> Balance) -> XPubBal -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubBal -> Balance
xPubBal
            g :: [(XPubKey, [XPubBal])] -> HashMap XPubKey [XPubBal]
g = [(XPubKey, [XPubBal])] -> HashMap XPubKey [XPubBal]
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(XPubKey, [XPubBal])] -> HashMap XPubKey [XPubBal])
-> ([(XPubKey, [XPubBal])] -> [(XPubKey, [XPubBal])])
-> [(XPubKey, [XPubBal])]
-> HashMap XPubKey [XPubBal]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((XPubKey, [XPubBal]) -> (XPubKey, [XPubBal]))
-> [(XPubKey, [XPubBal])] -> [(XPubKey, [XPubBal])]
forall a b. (a -> b) -> [a] -> [b]
map (([XPubBal] -> [XPubBal])
-> (XPubKey, [XPubBal]) -> (XPubKey, [XPubBal])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ((XPubBal -> Bool) -> [XPubBal] -> [XPubBal]
forall a. (a -> Bool) -> [a] -> [a]
filter XPubBal -> Bool
f))
            h :: (a, XPubSpec) -> f (a, [XPubBal])
h (k :: a
k, s :: XPubSpec
s) = (,) a
k ([XPubBal] -> (a, [XPubBal])) -> f [XPubBal] -> f (a, [XPubBal])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> XPubSpec -> f [XPubBal]
forall (m :: * -> *). StoreReadExtra m => XPubSpec -> m [XPubBal]
xPubBals XPubSpec
s
        in ([(XPubKey, [XPubBal])] -> HashMap XPubKey [XPubBal])
-> ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])]
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(XPubKey, [XPubBal])] -> HashMap XPubKey [XPubBal]
g (ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])]
 -> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal]))
-> (HashMap XPubKey XPubSpec
    -> ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])])
-> HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) (HashMap XPubKey [XPubBal])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((XPubKey, XPubSpec)
 -> ActionT Except (ReaderT WebState m) (XPubKey, [XPubBal]))
-> [(XPubKey, XPubSpec)]
-> ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (XPubKey, XPubSpec)
-> ActionT Except (ReaderT WebState m) (XPubKey, [XPubBal])
forall (f :: * -> *) a.
StoreReadExtra f =>
(a, XPubSpec) -> f (a, [XPubBal])
h ([(XPubKey, XPubSpec)]
 -> ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])])
-> (HashMap XPubKey XPubSpec -> [(XPubKey, XPubSpec)])
-> HashMap XPubKey XPubSpec
-> ActionT Except (ReaderT WebState m) [(XPubKey, [XPubBal])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap XPubKey XPubSpec -> [(XPubKey, XPubSpec)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList
    get_abals :: HashSet Address
-> ActionT Except (ReaderT WebState m) (HashMap Address Balance)
get_abals =
        let f :: Balance -> (Address, Balance)
f b :: Balance
b = (Balance -> Address
balanceAddress Balance
b, Balance
b)
            g :: [Balance] -> HashMap Address Balance
g = [(Address, Balance)] -> HashMap Address Balance
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Address, Balance)] -> HashMap Address Balance)
-> ([Balance] -> [(Address, Balance)])
-> [Balance]
-> HashMap Address Balance
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Balance -> (Address, Balance))
-> [Balance] -> [(Address, Balance)]
forall a b. (a -> b) -> [a] -> [b]
map Balance -> (Address, Balance)
f
        in ([Balance] -> HashMap Address Balance)
-> ActionT Except (ReaderT WebState m) [Balance]
-> ActionT Except (ReaderT WebState m) (HashMap Address Balance)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Balance] -> HashMap Address Balance
g (ActionT Except (ReaderT WebState m) [Balance]
 -> ActionT Except (ReaderT WebState m) (HashMap Address Balance))
-> (HashSet Address
    -> ActionT Except (ReaderT WebState m) [Balance])
-> HashSet Address
-> ActionT Except (ReaderT WebState m) (HashMap Address Balance)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Address] -> ActionT Except (ReaderT WebState m) [Balance]
forall (m :: * -> *). StoreReadExtra m => [Address] -> m [Balance]
getBalances ([Address] -> ActionT Except (ReaderT WebState m) [Balance])
-> (HashSet Address -> [Address])
-> HashSet Address
-> ActionT Except (ReaderT WebState m) [Balance]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet Address -> [Address]
forall a. HashSet a -> [a]
HashSet.toList
    addr_in_set :: HashSet Address -> Transaction -> Bool
addr_in_set s :: HashSet Address
s t :: Transaction
t =
        let f :: StoreInput -> Bool
f StoreCoinbase{}                    = Bool
False
            f StoreInput{inputAddress :: StoreInput -> Maybe Address
inputAddress = Maybe Address
Nothing} = Bool
False
            f StoreInput{inputAddress :: StoreInput -> Maybe Address
inputAddress = Just a :: Address
a}  = Address
a Address -> HashSet Address -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet Address
s
            g :: StoreOutput -> Bool
g StoreOutput{outputAddress :: StoreOutput -> Maybe Address
outputAddress = Maybe Address
m} = case Maybe Address
m of
                Nothing -> Bool
False
                Just a :: Address
a  -> Address
a Address -> HashSet Address -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet Address
s
            i :: Bool
i = (StoreInput -> Bool) -> [StoreInput] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StoreInput -> Bool
f (Transaction -> [StoreInput]
transactionInputs Transaction
t)
            o :: Bool
o = (StoreOutput -> Bool) -> [StoreOutput] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StoreOutput -> Bool
g (Transaction -> [StoreOutput]
transactionOutputs Transaction
t)
        in Bool
i Bool -> Bool -> Bool
|| Bool
o
    get_peers :: ActionT Except (ReaderT WebState m) Word32
get_peers = do
        [PeerInformation]
ps <- ReaderT WebState m [PeerInformation]
-> ActionT Except (ReaderT WebState m) [PeerInformation]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [PeerInformation]
 -> ActionT Except (ReaderT WebState m) [PeerInformation])
-> ReaderT WebState m [PeerInformation]
-> ActionT Except (ReaderT WebState m) [PeerInformation]
forall a b. (a -> b) -> a -> b
$ PeerManager -> ReaderT WebState m [PeerInformation]
forall (m :: * -> *).
MonadLoggerIO m =>
PeerManager -> m [PeerInformation]
getPeersInformation (PeerManager -> ReaderT WebState m [PeerInformation])
-> ReaderT WebState m PeerManager
-> ReaderT WebState m [PeerInformation]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<
            (WebState -> PeerManager) -> ReaderT WebState m PeerManager
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> PeerManager
storeManager (Store -> PeerManager)
-> (WebState -> Store) -> WebState -> PeerManager
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        Word32 -> ActionT Except (ReaderT WebState m) Word32
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([PeerInformation] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PeerInformation]
ps))
    compute_txids :: Set TxRef -> [TxHash]
compute_txids = (TxRef -> TxHash) -> [TxRef] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map TxRef -> TxHash
txRefHash ([TxRef] -> [TxHash])
-> (Set TxRef -> [TxRef]) -> Set TxRef -> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set TxRef -> [TxRef]
forall a. Set a -> [a]
Set.toDescList
    compute_etxids :: Bool -> HashMap Address a -> [Transaction] -> [TxHash]
compute_etxids prune :: Bool
prune abook :: HashMap Address a
abook =
        let f :: Transaction -> HashSet TxHash
f = HashSet Address -> Bool -> Transaction -> HashSet TxHash
relevantTxs (HashMap Address a -> HashSet Address
forall k a. HashMap k a -> HashSet k
HashMap.keysSet HashMap Address a
abook) Bool
prune
        in HashSet TxHash -> [TxHash]
forall a. HashSet a -> [a]
HashSet.toList (HashSet TxHash -> [TxHash])
-> ([Transaction] -> HashSet TxHash) -> [Transaction] -> [TxHash]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashSet TxHash -> HashSet TxHash -> HashSet TxHash)
-> HashSet TxHash -> [HashSet TxHash] -> HashSet TxHash
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl HashSet TxHash -> HashSet TxHash -> HashSet TxHash
forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
HashSet.union HashSet TxHash
forall a. HashSet a
HashSet.empty ([HashSet TxHash] -> HashSet TxHash)
-> ([Transaction] -> [HashSet TxHash])
-> [Transaction]
-> HashSet TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction -> HashSet TxHash)
-> [Transaction] -> [HashSet TxHash]
forall a b. (a -> b) -> [a] -> [b]
map Transaction -> HashSet TxHash
f
    compute_xabals :: HashMap k [XPubBal] -> HashMap Address Balance
compute_xabals =
        let f :: XPubBal -> (Address, Balance)
f b :: XPubBal
b = (Balance -> Address
balanceAddress (XPubBal -> Balance
xPubBal XPubBal
b), XPubBal -> Balance
xPubBal XPubBal
b)
        in [(Address, Balance)] -> HashMap Address Balance
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Address, Balance)] -> HashMap Address Balance)
-> (HashMap k [XPubBal] -> [(Address, Balance)])
-> HashMap k [XPubBal]
-> HashMap Address Balance
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([XPubBal] -> [(Address, Balance)])
-> [[XPubBal]] -> [(Address, Balance)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((XPubBal -> (Address, Balance))
-> [XPubBal] -> [(Address, Balance)]
forall a b. (a -> b) -> [a] -> [b]
map XPubBal -> (Address, Balance)
f) ([[XPubBal]] -> [(Address, Balance)])
-> (HashMap k [XPubBal] -> [[XPubBal]])
-> HashMap k [XPubBal]
-> [(Address, Balance)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k [XPubBal] -> [[XPubBal]]
forall k v. HashMap k v -> [v]
HashMap.elems
    compute_bal :: HashMap k Balance -> Word64
compute_bal =
        let f :: Balance -> Word64
f b :: Balance
b = Balance -> Word64
balanceAmount Balance
b Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Balance -> Word64
balanceZero Balance
b
        in [Word64] -> Word64
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Word64] -> Word64)
-> (HashMap k Balance -> [Word64]) -> HashMap k Balance -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Balance -> Word64) -> [Balance] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map Balance -> Word64
f ([Balance] -> [Word64])
-> (HashMap k Balance -> [Balance])
-> HashMap k Balance
-> [Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k Balance -> [Balance]
forall k v. HashMap k v -> [v]
HashMap.elems
    compute_abook :: HashSet Address
-> HashMap XPubKey [XPubBal]
-> HashMap Address (Maybe BinfoXPubPath)
compute_abook addrs :: HashSet Address
addrs xbals :: HashMap XPubKey [XPubBal]
xbals =
        let f :: XPubKey -> XPubBal -> (Address, Maybe BinfoXPubPath)
f k :: XPubKey
k XPubBal{..} =
                let a :: Address
a = Balance -> Address
balanceAddress Balance
xPubBal
                    e :: a
e = String -> a
forall a. HasCallStack => String -> a
error "lions and tigers and bears"
                    s :: Maybe SoftPath
s = DerivPathI AnyDeriv -> Maybe SoftPath
forall t. DerivPathI t -> Maybe SoftPath
toSoft ([Word32] -> DerivPathI AnyDeriv
listToPath [Word32]
xPubBalPath)
                    m :: SoftPath
m = SoftPath -> Maybe SoftPath -> SoftPath
forall a. a -> Maybe a -> a
fromMaybe SoftPath
forall a. a
e Maybe SoftPath
s
                in (Address
a, BinfoXPubPath -> Maybe BinfoXPubPath
forall a. a -> Maybe a
Just (XPubKey -> SoftPath -> BinfoXPubPath
BinfoXPubPath XPubKey
k SoftPath
m))
            g :: XPubKey -> [XPubBal] -> [(Address, Maybe BinfoXPubPath)]
g k :: XPubKey
k = (XPubBal -> (Address, Maybe BinfoXPubPath))
-> [XPubBal] -> [(Address, Maybe BinfoXPubPath)]
forall a b. (a -> b) -> [a] -> [b]
map (XPubKey -> XPubBal -> (Address, Maybe BinfoXPubPath)
f XPubKey
k)
            amap :: HashMap Address (Maybe a)
amap = (() -> Maybe a) -> HashMap Address () -> HashMap Address (Maybe a)
forall v1 v2 k. (v1 -> v2) -> HashMap k v1 -> HashMap k v2
HashMap.map (Maybe a -> () -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing) (HashMap Address () -> HashMap Address (Maybe a))
-> HashMap Address () -> HashMap Address (Maybe a)
forall a b. (a -> b) -> a -> b
$
                   HashSet Address -> HashMap Address ()
forall a. HashSet a -> HashMap a ()
HashSet.toMap HashSet Address
addrs
            xmap :: HashMap Address (Maybe BinfoXPubPath)
xmap = [(Address, Maybe BinfoXPubPath)]
-> HashMap Address (Maybe BinfoXPubPath)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Address, Maybe BinfoXPubPath)]
 -> HashMap Address (Maybe BinfoXPubPath))
-> ([(XPubKey, [XPubBal])] -> [(Address, Maybe BinfoXPubPath)])
-> [(XPubKey, [XPubBal])]
-> HashMap Address (Maybe BinfoXPubPath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                   ((XPubKey, [XPubBal]) -> [(Address, Maybe BinfoXPubPath)])
-> [(XPubKey, [XPubBal])] -> [(Address, Maybe BinfoXPubPath)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((XPubKey -> [XPubBal] -> [(Address, Maybe BinfoXPubPath)])
-> (XPubKey, [XPubBal]) -> [(Address, Maybe BinfoXPubPath)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry XPubKey -> [XPubBal] -> [(Address, Maybe BinfoXPubPath)]
g) ([(XPubKey, [XPubBal])] -> HashMap Address (Maybe BinfoXPubPath))
-> [(XPubKey, [XPubBal])] -> HashMap Address (Maybe BinfoXPubPath)
forall a b. (a -> b) -> a -> b
$
                   HashMap XPubKey [XPubBal] -> [(XPubKey, [XPubBal])]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList HashMap XPubKey [XPubBal]
xbals
        in HashMap Address (Maybe BinfoXPubPath)
forall a. HashMap Address (Maybe a)
amap HashMap Address (Maybe BinfoXPubPath)
-> HashMap Address (Maybe BinfoXPubPath)
-> HashMap Address (Maybe BinfoXPubPath)
forall a. Semigroup a => a -> a -> a
<> HashMap Address (Maybe BinfoXPubPath)
xmap
    compute_xaddrs :: HashMap k [XPubBal] -> HashSet Address
compute_xaddrs =
        let f :: [XPubBal] -> [Address]
f = (XPubBal -> Address) -> [XPubBal] -> [Address]
forall a b. (a -> b) -> [a] -> [b]
map (Balance -> Address
balanceAddress (Balance -> Address) -> (XPubBal -> Balance) -> XPubBal -> Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XPubBal -> Balance
xPubBal)
        in [Address] -> HashSet Address
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([Address] -> HashSet Address)
-> (HashMap k [XPubBal] -> [Address])
-> HashMap k [XPubBal]
-> HashSet Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([XPubBal] -> [Address]) -> [[XPubBal]] -> [Address]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [XPubBal] -> [Address]
f ([[XPubBal]] -> [Address])
-> (HashMap k [XPubBal] -> [[XPubBal]])
-> HashMap k [XPubBal]
-> [Address]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k [XPubBal] -> [[XPubBal]]
forall k v. HashMap k v -> [v]
HashMap.elems
    sent :: BinfoTx -> p
sent BinfoTx{getBinfoTxResultBal :: BinfoTx -> Maybe (Int64, Int64)
getBinfoTxResultBal = Just (r :: Int64
r, _)}
      | Int64
r Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< 0 = Int64 -> p
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int64
forall a. Num a => a -> a
negate Int64
r)
      | Bool
otherwise = 0
    sent _ = 0
    received :: BinfoTx -> p
received BinfoTx{getBinfoTxResultBal :: BinfoTx -> Maybe (Int64, Int64)
getBinfoTxResultBal = Just (r :: Int64
r, _)}
      | Int64
r Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = Int64 -> p
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
r
      | Bool
otherwise = 0
    received _ = 0

scottyBinfoTx :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyBinfoTx :: WebT m ()
scottyBinfoTx =
    WebT m Bool
forall (m :: * -> *). MonadIO m => WebT m Bool
getNumTxId WebT m Bool -> (Bool -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \numtxid :: Bool
numtxid ->
    Text -> ActionT Except (ReaderT WebState m) BinfoTxId
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "txid" ActionT Except (ReaderT WebState m) BinfoTxId
-> (BinfoTxId -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \txid :: BinfoTxId
txid ->
    (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
rawtxResponseTime 1 (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ Bool -> BinfoTxId -> WebT m ()
go Bool
numtxid BinfoTxId
txid
  where
    get_format :: ActionT Except (ReaderT WebState m) Text
get_format = Text -> ActionT Except (ReaderT WebState m) Text
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param "format" ActionT Except (ReaderT WebState m) Text
-> (Except -> ActionT Except (ReaderT WebState m) Text)
-> ActionT Except (ReaderT WebState m) Text
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) Text
-> Except -> ActionT Except (ReaderT WebState m) Text
forall a b. a -> b -> a
const (Text -> ActionT Except (ReaderT WebState m) Text
forall (m :: * -> *) a. Monad m => a -> m a
return ("json" :: Text))
    js :: Bool -> Transaction -> ActionT Except (ReaderT WebState m) ()
js numtxid :: Bool
numtxid t :: Transaction
t = do
        Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
        Encoding -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *). Monad m => Encoding -> WebT m ()
streamEncoding (Encoding -> ActionT Except (ReaderT WebState m) ())
-> Encoding -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ Network -> BinfoTx -> Encoding
binfoTxToEncoding Network
net (BinfoTx -> Encoding) -> BinfoTx -> Encoding
forall a b. (a -> b) -> a -> b
$ Bool -> Transaction -> BinfoTx
toBinfoTxSimple Bool
numtxid Transaction
t
    hex :: Transaction -> ActionT e m ()
hex t :: Transaction
t = do
        ActionT e m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
        Text -> ActionT e m ()
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
Text -> ActionT e m ()
S.text (Text -> ActionT e m ()) -> (Tx -> Text) -> Tx -> ActionT e m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.fromStrict (Text -> Text) -> (Tx -> Text) -> Tx -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
encodeHex (ByteString -> Text) -> (Tx -> ByteString) -> Tx -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tx -> ByteString
forall a. Serialize a => a -> ByteString
encode (Tx -> ActionT e m ()) -> Tx -> ActionT e m ()
forall a b. (a -> b) -> a -> b
$ Transaction -> Tx
transactionData Transaction
t
    go :: Bool -> BinfoTxId -> WebT m ()
go numtxid :: Bool
numtxid txid :: BinfoTxId
txid =
        let f :: BinfoTxId -> f [Transaction]
f (BinfoTxIdHash h :: TxHash
h)  = Maybe Transaction -> [Transaction]
forall a. Maybe a -> [a]
maybeToList (Maybe Transaction -> [Transaction])
-> f (Maybe Transaction) -> f [Transaction]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxHash -> f (Maybe Transaction)
forall (m :: * -> *).
(Monad m, StoreReadBase m) =>
TxHash -> m (Maybe Transaction)
getTransaction TxHash
h
            f (BinfoTxIdIndex i :: Word64
i) = Word64 -> f [Transaction]
forall (m :: * -> *).
(Monad m, StoreReadExtra m) =>
Word64 -> m [Transaction]
getNumTransaction Word64
i
        in BinfoTxId -> ActionT Except (ReaderT WebState m) [Transaction]
forall (f :: * -> *).
StoreReadExtra f =>
BinfoTxId -> f [Transaction]
f BinfoTxId
txid ActionT Except (ReaderT WebState m) [Transaction]
-> ([Transaction] -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            [] -> (WebMetrics -> ErrorCounter) -> Except -> WebT m ()
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
rawtxErrors Except
ThingNotFound
            [t :: Transaction
t] -> ActionT Except (ReaderT WebState m) Text
get_format ActionT Except (ReaderT WebState m) Text
-> (Text -> WebT m ()) -> WebT m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                "hex" -> Transaction -> WebT m ()
forall (m :: * -> *) e.
(Monad m, ScottyError e) =>
Transaction -> ActionT e m ()
hex Transaction
t
                _     -> Bool -> Transaction -> WebT m ()
forall (m :: * -> *).
Monad m =>
Bool -> Transaction -> ActionT Except (ReaderT WebState m) ()
js Bool
numtxid Transaction
t
            ts :: [Transaction]
ts ->
                let tids :: [TxHash]
tids = (Transaction -> TxHash) -> [Transaction] -> [TxHash]
forall a b. (a -> b) -> [a] -> [b]
map (Tx -> TxHash
txHash (Tx -> TxHash) -> (Transaction -> Tx) -> Transaction -> TxHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Tx
transactionData) [Transaction]
ts
                in (WebMetrics -> ErrorCounter) -> Except -> WebT m ()
forall (m :: * -> *) a.
MonadIO m =>
(WebMetrics -> ErrorCounter) -> Except -> WebT m a
raise WebMetrics -> ErrorCounter
rawtxErrors ([TxHash] -> Except
TxIndexConflict [TxHash]
tids)

-- GET Network Information --

scottyPeers :: (MonadUnliftIO m, MonadLoggerIO m)
            => GetPeers
            -> WebT m [PeerInformation]
scottyPeers :: GetPeers -> WebT m [PeerInformation]
scottyPeers _ =
    (WebMetrics -> StatDist)
-> Int -> WebT m [PeerInformation] -> WebT m [PeerInformation]
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
peerResponseTime 1 (WebT m [PeerInformation] -> WebT m [PeerInformation])
-> WebT m [PeerInformation] -> WebT m [PeerInformation]
forall a b. (a -> b) -> a -> b
$
    ReaderT WebState m [PeerInformation] -> WebT m [PeerInformation]
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m [PeerInformation] -> WebT m [PeerInformation])
-> ReaderT WebState m [PeerInformation] -> WebT m [PeerInformation]
forall a b. (a -> b) -> a -> b
$
    PeerManager -> ReaderT WebState m [PeerInformation]
forall (m :: * -> *).
MonadLoggerIO m =>
PeerManager -> m [PeerInformation]
getPeersInformation (PeerManager -> ReaderT WebState m [PeerInformation])
-> ReaderT WebState m PeerManager
-> ReaderT WebState m [PeerInformation]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (WebState -> PeerManager) -> ReaderT WebState m PeerManager
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> PeerManager
storeManager (Store -> PeerManager)
-> (WebState -> Store) -> WebState -> PeerManager
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)

-- | Obtain information about connected peers from peer manager process.
getPeersInformation
    :: MonadLoggerIO m => PeerManager -> m [PeerInformation]
getPeersInformation :: PeerManager -> m [PeerInformation]
getPeersInformation mgr :: PeerManager
mgr =
    (OnlinePeer -> Maybe PeerInformation)
-> [OnlinePeer] -> [PeerInformation]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe OnlinePeer -> Maybe PeerInformation
toInfo ([OnlinePeer] -> [PeerInformation])
-> m [OnlinePeer] -> m [PeerInformation]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PeerManager -> m [OnlinePeer]
forall (m :: * -> *). MonadIO m => PeerManager -> m [OnlinePeer]
getPeers PeerManager
mgr
  where
    toInfo :: OnlinePeer -> Maybe PeerInformation
toInfo op :: OnlinePeer
op = do
        Version
ver <- OnlinePeer -> Maybe Version
onlinePeerVersion OnlinePeer
op
        let as :: SockAddr
as = OnlinePeer -> SockAddr
onlinePeerAddress OnlinePeer
op
            ua :: ByteString
ua = VarString -> ByteString
getVarString (VarString -> ByteString) -> VarString -> ByteString
forall a b. (a -> b) -> a -> b
$ Version -> VarString
userAgent Version
ver
            vs :: Word32
vs = Version -> Word32
version Version
ver
            sv :: Word64
sv = Version -> Word64
services Version
ver
            rl :: Bool
rl = Version -> Bool
relay Version
ver
        PeerInformation -> Maybe PeerInformation
forall (m :: * -> *) a. Monad m => a -> m a
return
            $WPeerInformation :: ByteString -> String -> Word32 -> Word64 -> Bool -> PeerInformation
PeerInformation
                { peerUserAgent :: ByteString
peerUserAgent = ByteString
ua
                , peerAddress :: String
peerAddress = SockAddr -> String
forall a. Show a => a -> String
show SockAddr
as
                , peerVersion :: Word32
peerVersion = Word32
vs
                , peerServices :: Word64
peerServices = Word64
sv
                , peerRelay :: Bool
peerRelay = Bool
rl
                }

scottyHealth ::
       (MonadUnliftIO m, MonadLoggerIO m) => GetHealth -> WebT m HealthCheck
scottyHealth :: GetHealth -> WebT m HealthCheck
scottyHealth _ =
    (WebMetrics -> StatDist)
-> Int -> WebT m HealthCheck -> WebT m HealthCheck
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
healthResponseTime 1 (WebT m HealthCheck -> WebT m HealthCheck)
-> WebT m HealthCheck -> WebT m HealthCheck
forall a b. (a -> b) -> a -> b
$ do
    HealthCheck
h <- ReaderT WebState m HealthCheck -> WebT m HealthCheck
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m HealthCheck -> WebT m HealthCheck)
-> ReaderT WebState m HealthCheck -> WebT m HealthCheck
forall a b. (a -> b) -> a -> b
$ (WebState -> WebConfig) -> ReaderT WebState m WebConfig
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks WebState -> WebConfig
webConfig ReaderT WebState m WebConfig
-> (WebConfig -> ReaderT WebState m HealthCheck)
-> ReaderT WebState m HealthCheck
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= WebConfig -> ReaderT WebState m HealthCheck
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> m HealthCheck
healthCheck
    Bool
-> ActionT Except (ReaderT WebState m) ()
-> ActionT Except (ReaderT WebState m) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HealthCheck -> Bool
forall a. Healthy a => a -> Bool
isOK HealthCheck
h) (ActionT Except (ReaderT WebState m) ()
 -> ActionT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$ Status -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) e. Monad m => Status -> ActionT e m ()
S.status Status
status503
    HealthCheck -> WebT m HealthCheck
forall (m :: * -> *) a. Monad m => a -> m a
return HealthCheck
h

blockHealthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
                 => WebConfig -> m BlockHealth
blockHealthCheck :: WebConfig -> m BlockHealth
blockHealthCheck cfg :: WebConfig
cfg = do
    let ch :: Chain
ch = Store -> Chain
storeChain (Store -> Chain) -> Store -> Chain
forall a b. (a -> b) -> a -> b
$ WebConfig -> Store
webStore WebConfig
cfg
        blockHealthMaxDiff :: Int
blockHealthMaxDiff = WebConfig -> Int
webMaxDiff WebConfig
cfg
    Word32
blockHealthHeaders <-
        BlockNode -> Word32
H.nodeHeight (BlockNode -> Word32) -> m BlockNode -> m Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Chain -> m BlockNode
forall (m :: * -> *). MonadIO m => Chain -> m BlockNode
chainGetBest Chain
ch
    Word32
blockHealthBlocks <-
        Word32 -> (BlockData -> Word32) -> Maybe BlockData -> Word32
forall b a. b -> (a -> b) -> Maybe a -> b
maybe 0 BlockData -> Word32
blockDataHeight (Maybe BlockData -> Word32) -> m (Maybe BlockData) -> m Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
        MaybeT m BlockData -> m (Maybe BlockData)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (m (Maybe BlockHash) -> MaybeT m BlockHash
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT m (Maybe BlockHash)
forall (m :: * -> *). StoreReadBase m => m (Maybe BlockHash)
getBestBlock MaybeT m BlockHash
-> (BlockHash -> MaybeT m BlockData) -> MaybeT m BlockData
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m (Maybe BlockData) -> MaybeT m BlockData
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe BlockData) -> MaybeT m BlockData)
-> (BlockHash -> m (Maybe BlockData))
-> BlockHash
-> MaybeT m BlockData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHash -> m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock)
    BlockHealth -> m BlockHealth
forall (m :: * -> *) a. Monad m => a -> m a
return $WBlockHealth :: Word32 -> Word32 -> Int -> BlockHealth
BlockHealth {..}

lastBlockHealthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
                     => Chain -> WebTimeouts -> m TimeHealth
lastBlockHealthCheck :: Chain -> WebTimeouts -> m TimeHealth
lastBlockHealthCheck ch :: Chain
ch tos :: WebTimeouts
tos = do
    Int
n <- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (SystemTime -> Int64) -> SystemTime -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SystemTime -> Int64
systemSeconds (SystemTime -> Int) -> m SystemTime -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime
    Int
t <- Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> Int) -> (BlockNode -> Word32) -> BlockNode -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHeader -> Word32
H.blockTimestamp (BlockHeader -> Word32)
-> (BlockNode -> BlockHeader) -> BlockNode -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockNode -> BlockHeader
H.nodeHeader (BlockNode -> Int) -> m BlockNode -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Chain -> m BlockNode
forall (m :: * -> *). MonadIO m => Chain -> m BlockNode
chainGetBest Chain
ch
    let timeHealthAge :: Int
timeHealthAge = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t
        timeHealthMax :: Int
timeHealthMax = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Int) -> Word64 -> Int
forall a b. (a -> b) -> a -> b
$ WebTimeouts -> Word64
blockTimeout WebTimeouts
tos
    TimeHealth -> m TimeHealth
forall (m :: * -> *) a. Monad m => a -> m a
return $WTimeHealth :: Int -> Int -> TimeHealth
TimeHealth {..}

lastTxHealthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
                  => WebConfig -> m TimeHealth
lastTxHealthCheck :: WebConfig -> m TimeHealth
lastTxHealthCheck WebConfig {..} = do
    Int
n <- Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (SystemTime -> Int64) -> SystemTime -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SystemTime -> Int64
systemSeconds (SystemTime -> Int) -> m SystemTime -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO SystemTime -> m SystemTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO SystemTime
getSystemTime
    Int
b <- Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> Int) -> (BlockNode -> Word32) -> BlockNode -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHeader -> Word32
H.blockTimestamp (BlockHeader -> Word32)
-> (BlockNode -> BlockHeader) -> BlockNode -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockNode -> BlockHeader
H.nodeHeader (BlockNode -> Int) -> m BlockNode -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Chain -> m BlockNode
forall (m :: * -> *). MonadIO m => Chain -> m BlockNode
chainGetBest Chain
ch
    Int
t <- [(Word64, TxHash)] -> Maybe (Word64, TxHash)
forall a. [a] -> Maybe a
listToMaybe ([(Word64, TxHash)] -> Maybe (Word64, TxHash))
-> m [(Word64, TxHash)] -> m (Maybe (Word64, TxHash))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m [(Word64, TxHash)]
forall (m :: * -> *). StoreReadBase m => m [(Word64, TxHash)]
getMempool m (Maybe (Word64, TxHash))
-> (Maybe (Word64, TxHash) -> m Int) -> m Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Just t :: (Word64, TxHash)
t -> let x :: Int
x = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Int) -> Word64 -> Int
forall a b. (a -> b) -> a -> b
$ (Word64, TxHash) -> Word64
forall a b. (a, b) -> a
fst (Word64, TxHash)
t
                  in Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
x Int
b
        Nothing -> Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
b
    let timeHealthAge :: Int
timeHealthAge = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
t
        timeHealthMax :: Int
timeHealthMax = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
to
    TimeHealth -> m TimeHealth
forall (m :: * -> *) a. Monad m => a -> m a
return $WTimeHealth :: Int -> Int -> TimeHealth
TimeHealth {..}
  where
    ch :: Chain
ch = Store -> Chain
storeChain Store
webStore
    to :: Word64
to = if Bool
webNoMempool then WebTimeouts -> Word64
blockTimeout WebTimeouts
webTimeouts else WebTimeouts -> Word64
txTimeout WebTimeouts
webTimeouts

pendingTxsHealthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
                      => WebConfig -> m MaxHealth
pendingTxsHealthCheck :: WebConfig -> m MaxHealth
pendingTxsHealthCheck cfg :: WebConfig
cfg = do
    let maxHealthMax :: Int
maxHealthMax = WebConfig -> Int
webMaxPending WebConfig
cfg
    Int
maxHealthNum <- BlockStore -> m Int
forall (m :: * -> *). MonadIO m => BlockStore -> m Int
blockStorePendingTxs (Store -> BlockStore
storeBlock (WebConfig -> Store
webStore WebConfig
cfg))
    MaxHealth -> m MaxHealth
forall (m :: * -> *) a. Monad m => a -> m a
return $WMaxHealth :: Int -> Int -> MaxHealth
MaxHealth {..}

peerHealthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
                => PeerManager -> m CountHealth
peerHealthCheck :: PeerManager -> m CountHealth
peerHealthCheck mgr :: PeerManager
mgr = do
    let countHealthMin :: Int
countHealthMin = 1
    Int
countHealthNum <- [OnlinePeer] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([OnlinePeer] -> Int) -> m [OnlinePeer] -> m Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PeerManager -> m [OnlinePeer]
forall (m :: * -> *). MonadIO m => PeerManager -> m [OnlinePeer]
getPeers PeerManager
mgr
    CountHealth -> m CountHealth
forall (m :: * -> *) a. Monad m => a -> m a
return $WCountHealth :: Int -> Int -> CountHealth
CountHealth {..}

healthCheck :: (MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m)
            => WebConfig -> m HealthCheck
healthCheck :: WebConfig -> m HealthCheck
healthCheck cfg :: WebConfig
cfg@WebConfig {..} = do
    BlockHealth
healthBlocks     <- WebConfig -> m BlockHealth
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> m BlockHealth
blockHealthCheck WebConfig
cfg
    TimeHealth
healthLastBlock  <- Chain -> WebTimeouts -> m TimeHealth
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
Chain -> WebTimeouts -> m TimeHealth
lastBlockHealthCheck (Store -> Chain
storeChain Store
webStore) WebTimeouts
webTimeouts
    TimeHealth
healthLastTx     <- WebConfig -> m TimeHealth
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> m TimeHealth
lastTxHealthCheck WebConfig
cfg
    MaxHealth
healthPendingTxs <- WebConfig -> m MaxHealth
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
WebConfig -> m MaxHealth
pendingTxsHealthCheck WebConfig
cfg
    CountHealth
healthPeers      <- PeerManager -> m CountHealth
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m, StoreReadBase m) =>
PeerManager -> m CountHealth
peerHealthCheck (Store -> PeerManager
storeManager Store
webStore)
    let healthNetwork :: String
healthNetwork = Network -> String
getNetworkName (Store -> Network
storeNetwork Store
webStore)
        healthVersion :: String
healthVersion = String
webVersion
        hc :: HealthCheck
hc = $WHealthCheck :: BlockHealth
-> TimeHealth
-> TimeHealth
-> MaxHealth
-> CountHealth
-> String
-> String
-> HealthCheck
HealthCheck {..}
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HealthCheck -> Bool
forall a. Healthy a => a -> Bool
isOK HealthCheck
hc) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
        let t :: Text
t = Text -> Text
toStrict (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ HealthCheck -> Text
forall a. ToJSON a => a -> Text
encodeToLazyText HealthCheck
hc
        $(Text
LogLevel
String -> String -> String -> CharPos -> CharPos -> Loc
Loc -> Text -> LogLevel -> Text -> m ()
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
b :: Text
a :: Text
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
logErrorS) "Web" (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ "Health check failed: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t
    HealthCheck -> m HealthCheck
forall (m :: * -> *) a. Monad m => a -> m a
return HealthCheck
hc

scottyDbStats :: (MonadUnliftIO m, MonadLoggerIO m) => WebT m ()
scottyDbStats :: WebT m ()
scottyDbStats =
    (WebMetrics -> StatDist) -> Int -> WebT m () -> WebT m ()
forall (m :: * -> *) a.
MonadUnliftIO m =>
(WebMetrics -> StatDist) -> Int -> WebT m a -> WebT m a
withMetrics WebMetrics -> StatDist
dbStatsResponseTime 1 (WebT m () -> WebT m ()) -> WebT m () -> WebT m ()
forall a b. (a -> b) -> a -> b
$ do
    WebT m ()
forall (m :: * -> *) e. (Monad m, ScottyError e) => ActionT e m ()
setHeaders
    DB
db <- ReaderT WebState m DB -> ActionT Except (ReaderT WebState m) DB
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m DB -> ActionT Except (ReaderT WebState m) DB)
-> ReaderT WebState m DB -> ActionT Except (ReaderT WebState m) DB
forall a b. (a -> b) -> a -> b
$ (WebState -> DB) -> ReaderT WebState m DB
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (DatabaseReader -> DB
databaseHandle (DatabaseReader -> DB)
-> (WebState -> DatabaseReader) -> WebState -> DB
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Store -> DatabaseReader
storeDB (Store -> DatabaseReader)
-> (WebState -> Store) -> WebState -> DatabaseReader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe ByteString
statsM <- ReaderT WebState m (Maybe ByteString)
-> ActionT Except (ReaderT WebState m) (Maybe ByteString)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DB -> Property -> ReaderT WebState m (Maybe ByteString)
forall (m :: * -> *).
MonadIO m =>
DB -> Property -> m (Maybe ByteString)
getProperty DB
db Property
Stats)
    Text -> WebT m ()
forall e (m :: * -> *).
(ScottyError e, Monad m) =>
Text -> ActionT e m ()
S.text (Text -> WebT m ()) -> Text -> WebT m ()
forall a b. (a -> b) -> a -> b
$ Text -> (ByteString -> Text) -> Maybe ByteString -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "Could not get stats" ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Maybe ByteString
statsM

-----------------------
-- Parameter Parsing --
-----------------------

-- | Returns @Nothing@ if the parameter is not supplied. Raises an exception on
-- parse failure.
paramOptional :: (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional :: WebT m (Maybe a)
paramOptional = Proxy a -> WebT m (Maybe a)
forall a (m :: * -> *).
(Param a, MonadIO m) =>
Proxy a -> WebT m (Maybe a)
go Proxy a
forall k (t :: k). Proxy t
Proxy
  where
    go :: (Param a, MonadIO m) => Proxy a -> WebT m (Maybe a)
    go :: Proxy a -> WebT m (Maybe a)
go proxy :: Proxy a
proxy = do
        Network
net <- ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m Network
 -> ActionT Except (ReaderT WebState m) Network)
-> ReaderT WebState m Network
-> ActionT Except (ReaderT WebState m) Network
forall a b. (a -> b) -> a -> b
$ (WebState -> Network) -> ReaderT WebState m Network
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Network
storeNetwork (Store -> Network) -> (WebState -> Store) -> WebState -> Network
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        Maybe [Text]
tsM :: Maybe [Text] <- ActionT Except (ReaderT WebState m) (Maybe [Text])
p ActionT Except (ReaderT WebState m) (Maybe [Text])
-> (Except -> ActionT Except (ReaderT WebState m) (Maybe [Text]))
-> ActionT Except (ReaderT WebState m) (Maybe [Text])
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` ActionT Except (ReaderT WebState m) (Maybe [Text])
-> Except -> ActionT Except (ReaderT WebState m) (Maybe [Text])
forall a b. a -> b -> a
const (Maybe [Text] -> ActionT Except (ReaderT WebState m) (Maybe [Text])
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [Text]
forall a. Maybe a
Nothing)
        case Maybe [Text]
tsM of
            Nothing -> Maybe a -> WebT m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing -- Parameter was not supplied
            Just ts :: [Text]
ts -> WebT m (Maybe a)
-> (a -> WebT m (Maybe a)) -> Maybe a -> WebT m (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Except -> WebT m (Maybe a)
forall (m :: * -> *) a. MonadIO m => Except -> WebT m a
raise_ Except
err) (Maybe a -> WebT m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> WebT m (Maybe a))
-> (a -> Maybe a) -> a -> WebT m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just) (Maybe a -> WebT m (Maybe a)) -> Maybe a -> WebT m (Maybe a)
forall a b. (a -> b) -> a -> b
$ Network -> [Text] -> Maybe a
forall a. Param a => Network -> [Text] -> Maybe a
parseParam Network
net [Text]
ts
      where
        l :: Text
l = Proxy a -> Text
forall a. Param a => Proxy a -> Text
proxyLabel Proxy a
proxy
        p :: ActionT Except (ReaderT WebState m) (Maybe [Text])
p = [Text] -> Maybe [Text]
forall a. a -> Maybe a
Just ([Text] -> Maybe [Text])
-> ActionT Except (ReaderT WebState m) [Text]
-> ActionT Except (ReaderT WebState m) (Maybe [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ActionT Except (ReaderT WebState m) [Text]
forall a e (m :: * -> *).
(Parsable a, ScottyError e, Monad m) =>
Text -> ActionT e m a
S.param (Text -> Text
forall a b. ConvertibleStrings a b => a -> b
cs Text
l)
        err :: Except
err = String -> Except
UserError (String -> Except) -> String -> Except
forall a b. (a -> b) -> a -> b
$ "Unable to parse param " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a b. ConvertibleStrings a b => a -> b
cs Text
l

-- | Raises an exception if the parameter is not supplied
param :: (Param a, MonadIO m) => WebT m a
param :: WebT m a
param = Proxy a -> WebT m a
forall a (m :: * -> *). (Param a, MonadIO m) => Proxy a -> WebT m a
go Proxy a
forall k (t :: k). Proxy t
Proxy
  where
    go :: (Param a, MonadIO m) => Proxy a -> WebT m a
    go :: Proxy a -> WebT m a
go proxy :: Proxy a
proxy = do
        Maybe a
resM <- WebT m (Maybe a)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional
        case Maybe a
resM of
            Just res :: a
res -> a -> WebT m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res
            _ ->
                Except -> WebT m a
forall (m :: * -> *) a. MonadIO m => Except -> WebT m a
raise_ (Except -> WebT m a) -> (String -> Except) -> String -> WebT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Except
UserError (String -> WebT m a) -> String -> WebT m a
forall a b. (a -> b) -> a -> b
$
                "The param " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a b. ConvertibleStrings a b => a -> b
cs (Proxy a -> Text
forall a. Param a => Proxy a -> Text
proxyLabel Proxy a
proxy) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> " was not defined"

-- | Returns the default value of a parameter if it is not supplied. Raises an
-- exception on parse failure.
paramDef :: (Default a, Param a, MonadIO m) => WebT m a
paramDef :: WebT m a
paramDef = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
forall a. Default a => a
def (Maybe a -> a)
-> ActionT Except (ReaderT WebState m) (Maybe a) -> WebT m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) (Maybe a)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional

-- | Does not raise exceptions. Will call @Scotty.next@ if the parameter is
-- not supplied or if parsing fails.
paramLazy :: (Param a, MonadIO m) => WebT m a
paramLazy :: WebT m a
paramLazy = do
    Maybe a
resM <- WebT m (Maybe a)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional WebT m (Maybe a)
-> (Except -> WebT m (Maybe a)) -> WebT m (Maybe a)
forall e (m :: * -> *) a.
(ScottyError e, Monad m) =>
ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
`S.rescue` WebT m (Maybe a) -> Except -> WebT m (Maybe a)
forall a b. a -> b -> a
const (Maybe a -> WebT m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)
    WebT m a -> (a -> WebT m a) -> Maybe a -> WebT m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe WebT m a
forall e (m :: * -> *) a. (ScottyError e, Monad m) => ActionT e m a
S.next a -> WebT m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
resM

parseBody :: (MonadIO m, Serialize a) => WebT m a
parseBody :: WebT m a
parseBody = do
    ByteString
b <- ActionT Except (ReaderT WebState m) ByteString
forall e (m :: * -> *).
(ScottyError e, MonadIO m) =>
ActionT e m ByteString
S.body
    case ByteString -> Maybe a
hex ByteString
b Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ByteString -> Maybe a
bin (ByteString -> ByteString
L.toStrict ByteString
b) of
        Nothing -> Except -> WebT m a
forall (m :: * -> *) a. MonadIO m => Except -> WebT m a
raise_ (Except -> WebT m a) -> Except -> WebT m a
forall a b. (a -> b) -> a -> b
$ String -> Except
UserError "Failed to parse request body"
        Just x :: a
x  -> a -> WebT m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
  where
    bin :: ByteString -> Maybe a
bin = Either String a -> Maybe a
forall a b. Either a b -> Maybe b
eitherToMaybe (Either String a -> Maybe a)
-> (ByteString -> Either String a) -> ByteString -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String a
forall a. Serialize a => ByteString -> Either String a
Serialize.decode
    hex :: ByteString -> Maybe a
hex = ByteString -> Maybe a
bin (ByteString -> Maybe a)
-> (ByteString -> Maybe ByteString) -> ByteString -> Maybe a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Text -> Maybe ByteString
decodeHex (Text -> Maybe ByteString)
-> (ByteString -> Text) -> ByteString -> Maybe ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ByteString -> ByteString
C.filter (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace)

parseOffset :: MonadIO m => WebT m OffsetParam
parseOffset :: WebT m OffsetParam
parseOffset = do
    res :: OffsetParam
res@(OffsetParam o :: Natural
o) <- WebT m OffsetParam
forall a (m :: * -> *). (Default a, Param a, MonadIO m) => WebT m a
paramDef
    WebLimits
limits <- ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m WebLimits
 -> ActionT Except (ReaderT WebState m) WebLimits)
-> ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall a b. (a -> b) -> a -> b
$ (WebState -> WebLimits) -> ReaderT WebState m WebLimits
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Bool
-> ActionT Except (ReaderT WebState m) ()
-> ActionT Except (ReaderT WebState m) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (WebLimits -> Word32
maxLimitOffset WebLimits
limits Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> 0 Bool -> Bool -> Bool
&& Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
o Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> WebLimits -> Word32
maxLimitOffset WebLimits
limits) (ActionT Except (ReaderT WebState m) ()
 -> ActionT Except (ReaderT WebState m) ())
-> ActionT Except (ReaderT WebState m) ()
-> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
        Except -> ActionT Except (ReaderT WebState m) ()
forall (m :: * -> *) a. MonadIO m => Except -> WebT m a
raise_ (Except -> ActionT Except (ReaderT WebState m) ())
-> (String -> Except)
-> String
-> ActionT Except (ReaderT WebState m) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Except
UserError (String -> ActionT Except (ReaderT WebState m) ())
-> String -> ActionT Except (ReaderT WebState m) ()
forall a b. (a -> b) -> a -> b
$
        "offset exceeded: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Natural -> String
forall a. Show a => a -> String
show Natural
o String -> ShowS
forall a. Semigroup a => a -> a -> a
<> " > " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Word32 -> String
forall a. Show a => a -> String
show (WebLimits -> Word32
maxLimitOffset WebLimits
limits)
    OffsetParam -> WebT m OffsetParam
forall (m :: * -> *) a. Monad m => a -> m a
return OffsetParam
res

parseStart ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => Maybe StartParam
    -> WebT m (Maybe Start)
parseStart :: Maybe StartParam -> WebT m (Maybe Start)
parseStart Nothing = Maybe Start -> WebT m (Maybe Start)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Start
forall a. Maybe a
Nothing
parseStart (Just s :: StartParam
s) =
    MaybeT (ActionT Except (ReaderT WebState m)) Start
-> WebT m (Maybe Start)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT (ActionT Except (ReaderT WebState m)) Start
 -> WebT m (Maybe Start))
-> MaybeT (ActionT Except (ReaderT WebState m)) Start
-> WebT m (Maybe Start)
forall a b. (a -> b) -> a -> b
$
    case StartParam
s of
        StartParamHash {startParamHash :: StartParam -> Hash256
startParamHash = Hash256
h}     -> Hash256 -> MaybeT (ActionT Except (ReaderT WebState m)) Start
forall (m :: * -> *). StoreReadBase m => Hash256 -> MaybeT m Start
start_tx Hash256
h MaybeT (ActionT Except (ReaderT WebState m)) Start
-> MaybeT (ActionT Except (ReaderT WebState m)) Start
-> MaybeT (ActionT Except (ReaderT WebState m)) Start
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Hash256 -> MaybeT (ActionT Except (ReaderT WebState m)) Start
forall (m :: * -> *). StoreReadBase m => Hash256 -> MaybeT m Start
start_block Hash256
h
        StartParamHeight {startParamHeight :: StartParam -> Natural
startParamHeight = Natural
h} -> Natural -> MaybeT (ActionT Except (ReaderT WebState m)) Start
forall (m :: * -> *) a. (Monad m, Integral a) => a -> m Start
start_height Natural
h
        StartParamTime {startParamTime :: StartParam -> Word64
startParamTime = Word64
q}     -> Word64 -> MaybeT (ActionT Except (ReaderT WebState m)) Start
forall (m :: * -> *).
(MonadReader WebState m, MonadIO m, StoreReadExtra m) =>
Word64 -> MaybeT m Start
start_time Word64
q
  where
    start_height :: a -> m Start
start_height h :: a
h = Start -> m Start
forall (m :: * -> *) a. Monad m => a -> m a
return (Start -> m Start) -> Start -> m Start
forall a b. (a -> b) -> a -> b
$ Word32 -> Start
AtBlock (Word32 -> Start) -> Word32 -> Start
forall a b. (a -> b) -> a -> b
$ a -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
h
    start_block :: Hash256 -> MaybeT m Start
start_block h :: Hash256
h = do
        BlockData
b <- m (Maybe BlockData) -> MaybeT m BlockData
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe BlockData) -> MaybeT m BlockData)
-> m (Maybe BlockData) -> MaybeT m BlockData
forall a b. (a -> b) -> a -> b
$ BlockHash -> m (Maybe BlockData)
forall (m :: * -> *).
StoreReadBase m =>
BlockHash -> m (Maybe BlockData)
getBlock (Hash256 -> BlockHash
H.BlockHash Hash256
h)
        Start -> MaybeT m Start
forall (m :: * -> *) a. Monad m => a -> m a
return (Start -> MaybeT m Start) -> Start -> MaybeT m Start
forall a b. (a -> b) -> a -> b
$ Word32 -> Start
AtBlock (BlockData -> Word32
blockDataHeight BlockData
b)
    start_tx :: Hash256 -> MaybeT m Start
start_tx h :: Hash256
h = do
        TxData
_ <- m (Maybe TxData) -> MaybeT m TxData
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe TxData) -> MaybeT m TxData)
-> m (Maybe TxData) -> MaybeT m TxData
forall a b. (a -> b) -> a -> b
$ TxHash -> m (Maybe TxData)
forall (m :: * -> *). StoreReadBase m => TxHash -> m (Maybe TxData)
getTxData (Hash256 -> TxHash
TxHash Hash256
h)
        Start -> MaybeT m Start
forall (m :: * -> *) a. Monad m => a -> m a
return (Start -> MaybeT m Start) -> Start -> MaybeT m Start
forall a b. (a -> b) -> a -> b
$ TxHash -> Start
AtTx (Hash256 -> TxHash
TxHash Hash256
h)
    start_time :: Word64 -> MaybeT m Start
start_time q :: Word64
q = do
        Chain
ch <- m Chain -> MaybeT m Chain
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Chain -> MaybeT m Chain) -> m Chain -> MaybeT m Chain
forall a b. (a -> b) -> a -> b
$ (WebState -> Chain) -> m Chain
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Chain
storeChain (Store -> Chain) -> (WebState -> Store) -> WebState -> Chain
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
        BlockData
b <- m (Maybe BlockData) -> MaybeT m BlockData
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe BlockData) -> MaybeT m BlockData)
-> m (Maybe BlockData) -> MaybeT m BlockData
forall a b. (a -> b) -> a -> b
$ Chain -> Word64 -> m (Maybe BlockData)
forall (m :: * -> *).
(MonadIO m, StoreReadExtra m) =>
Chain -> Word64 -> m (Maybe BlockData)
blockAtOrBefore Chain
ch Word64
q
        let g :: Word32
g = BlockData -> Word32
blockDataHeight BlockData
b
        Start -> MaybeT m Start
forall (m :: * -> *) a. Monad m => a -> m a
return (Start -> MaybeT m Start) -> Start -> MaybeT m Start
forall a b. (a -> b) -> a -> b
$ Word32 -> Start
AtBlock Word32
g

parseLimits :: MonadIO m => WebT m LimitsParam
parseLimits :: WebT m LimitsParam
parseLimits = Maybe LimitParam -> OffsetParam -> Maybe StartParam -> LimitsParam
LimitsParam (Maybe LimitParam
 -> OffsetParam -> Maybe StartParam -> LimitsParam)
-> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
-> ActionT
     Except
     (ReaderT WebState m)
     (OffsetParam -> Maybe StartParam -> LimitsParam)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ActionT Except (ReaderT WebState m) (Maybe LimitParam)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional ActionT
  Except
  (ReaderT WebState m)
  (OffsetParam -> Maybe StartParam -> LimitsParam)
-> ActionT Except (ReaderT WebState m) OffsetParam
-> ActionT
     Except (ReaderT WebState m) (Maybe StartParam -> LimitsParam)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) OffsetParam
forall (m :: * -> *). MonadIO m => WebT m OffsetParam
parseOffset ActionT
  Except (ReaderT WebState m) (Maybe StartParam -> LimitsParam)
-> ActionT Except (ReaderT WebState m) (Maybe StartParam)
-> WebT m LimitsParam
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ActionT Except (ReaderT WebState m) (Maybe StartParam)
forall a (m :: * -> *). (Param a, MonadIO m) => WebT m (Maybe a)
paramOptional

paramToLimits ::
       (MonadUnliftIO m, MonadLoggerIO m)
    => Bool
    -> LimitsParam
    -> WebT m Limits
paramToLimits :: Bool -> LimitsParam -> WebT m Limits
paramToLimits full :: Bool
full (LimitsParam limitM :: Maybe LimitParam
limitM o :: OffsetParam
o startM :: Maybe StartParam
startM) = do
    WebLimits
wl <- ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT WebState m WebLimits
 -> ActionT Except (ReaderT WebState m) WebLimits)
-> ReaderT WebState m WebLimits
-> ActionT Except (ReaderT WebState m) WebLimits
forall a b. (a -> b) -> a -> b
$ (WebState -> WebLimits) -> ReaderT WebState m WebLimits
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WebConfig -> WebLimits
webMaxLimits (WebConfig -> WebLimits)
-> (WebState -> WebConfig) -> WebState -> WebLimits
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Word32 -> Word32 -> Maybe Start -> Limits
Limits (WebLimits -> Bool -> Maybe LimitParam -> Word32
validateLimit WebLimits
wl Bool
full Maybe LimitParam
limitM) (OffsetParam -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral OffsetParam
o) (Maybe Start -> Limits)
-> ActionT Except (ReaderT WebState m) (Maybe Start)
-> WebT m Limits
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StartParam
-> ActionT Except (ReaderT WebState m) (Maybe Start)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
Maybe StartParam -> WebT m (Maybe Start)
parseStart Maybe StartParam
startM

validateLimit :: WebLimits -> Bool -> Maybe LimitParam -> Word32
validateLimit :: WebLimits -> Bool -> Maybe LimitParam -> Word32
validateLimit wl :: WebLimits
wl full :: Bool
full limitM :: Maybe LimitParam
limitM =
    Word32 -> Word32 -> Word32
forall a. (Num a, Ord a) => a -> a -> a
f Word32
m (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Word32 -> (LimitParam -> Word32) -> Maybe LimitParam -> Word32
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word32
d (Natural -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Natural -> Word32)
-> (LimitParam -> Natural) -> LimitParam -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LimitParam -> Natural
getLimitParam) Maybe LimitParam
limitM
  where
    m :: Word32
m | Bool
full Bool -> Bool -> Bool
&& WebLimits -> Word32
maxLimitFull WebLimits
wl Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = WebLimits -> Word32
maxLimitFull WebLimits
wl
      | Bool
otherwise = WebLimits -> Word32
maxLimitCount WebLimits
wl
    d :: Word32
d = WebLimits -> Word32
maxLimitDefault WebLimits
wl
    f :: a -> a -> a
f a :: a
a 0 = a
a
    f 0 b :: a
b = a
b
    f a :: a
a b :: a
b = a -> a -> a
forall a. Ord a => a -> a -> a
min a
a a
b

---------------
-- Utilities --
---------------

runInWebReader ::
       MonadIO m
    => CacheT (DatabaseReaderT m) a
    -> ReaderT WebState m a
runInWebReader :: CacheT (DatabaseReaderT m) a -> ReaderT WebState m a
runInWebReader f :: CacheT (DatabaseReaderT m) a
f = do
    DatabaseReader
bdb <- (WebState -> DatabaseReader) -> ReaderT WebState m DatabaseReader
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> DatabaseReader
storeDB (Store -> DatabaseReader)
-> (WebState -> Store) -> WebState -> DatabaseReader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    Maybe CacheConfig
mc <- (WebState -> Maybe CacheConfig)
-> ReaderT WebState m (Maybe CacheConfig)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Store -> Maybe CacheConfig
storeCache (Store -> Maybe CacheConfig)
-> (WebState -> Store) -> WebState -> Maybe CacheConfig
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebConfig -> Store
webStore (WebConfig -> Store)
-> (WebState -> WebConfig) -> WebState -> Store
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WebState -> WebConfig
webConfig)
    m a -> ReaderT WebState m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT WebState m a) -> m a -> ReaderT WebState m a
forall a b. (a -> b) -> a -> b
$ ReaderT DatabaseReader m a -> DatabaseReader -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (Maybe CacheConfig
-> CacheT (DatabaseReaderT m) a -> ReaderT DatabaseReader m a
forall (m :: * -> *) a.
StoreReadBase m =>
Maybe CacheConfig -> CacheT m a -> m a
withCache Maybe CacheConfig
mc CacheT (DatabaseReaderT m) a
f) DatabaseReader
bdb

runNoCache :: MonadIO m => Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache :: Bool -> ReaderT WebState m a -> ReaderT WebState m a
runNoCache False f :: ReaderT WebState m a
f = ReaderT WebState m a
f
runNoCache True f :: ReaderT WebState m a
f = (WebState -> WebState)
-> ReaderT WebState m a -> ReaderT WebState m a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local WebState -> WebState
g ReaderT WebState m a
f
  where
    g :: WebState -> WebState
g s :: WebState
s = WebState
s { webConfig :: WebConfig
webConfig = WebConfig -> WebConfig
h (WebState -> WebConfig
webConfig WebState
s) }
    h :: WebConfig -> WebConfig
h c :: WebConfig
c = WebConfig
c { webStore :: Store
webStore = Store -> Store
i (WebConfig -> Store
webStore WebConfig
c) }
    i :: Store -> Store
i s :: Store
s = Store
s { storeCache :: Maybe CacheConfig
storeCache = Maybe CacheConfig
forall a. Maybe a
Nothing }

logIt :: (MonadUnliftIO m, MonadLoggerIO m) => m Middleware
logIt :: m Middleware
logIt = do
    m () -> IO ()
runner <- m (m () -> IO ())
forall (m :: * -> *) a. MonadUnliftIO m => m (m a -> IO a)
askRunInIO
    Middleware -> m Middleware
forall (m :: * -> *) a. Monad m => a -> m a
return (Middleware -> m Middleware) -> Middleware -> m Middleware
forall a b. (a -> b) -> a -> b
$ \app :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
app req :: Request
req respond :: Response -> IO ResponseReceived
respond -> do
        Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
app Request
req ((Response -> IO ResponseReceived) -> IO ResponseReceived)
-> (Response -> IO ResponseReceived) -> IO ResponseReceived
forall a b. (a -> b) -> a -> b
$ \res :: Response
res -> do
            let s :: Status
s = Response -> Status
responseStatus Response
res
                msg :: Text
msg = Request -> Text
fmtReq Request
req Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> " [" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Status -> Text
fmtStatus Status
s Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "]"
            if Status -> Bool
statusIsSuccessful Status
s
                then m () -> IO ()
runner (m () -> IO ()) -> m () -> IO ()
forall a b. (a -> b) -> a -> b
$ $(Text
LogLevel
String -> String -> String -> CharPos -> CharPos -> Loc
Loc -> Text -> LogLevel -> Text -> m ()
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
b :: Text
a :: Text
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
logDebugS) "Web" Text
msg
                else m () -> IO ()
runner (m () -> IO ()) -> m () -> IO ()
forall a b. (a -> b) -> a -> b
$ $(Text
LogLevel
String -> String -> String -> CharPos -> CharPos -> Loc
Loc -> Text -> LogLevel -> Text -> m ()
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
b :: Text
a :: Text
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> Text -> LogLevel -> msg -> m ()
logErrorS) "Web" Text
msg
            Response -> IO ResponseReceived
respond Response
res

fmtReq :: Request -> Text
fmtReq :: Request -> Text
fmtReq req :: Request
req =
    let m :: ByteString
m = Request -> ByteString
requestMethod Request
req
        v :: HttpVersion
v = Request -> HttpVersion
httpVersion Request
req
        p :: ByteString
p = Request -> ByteString
rawPathInfo Request
req
        q :: ByteString
q = Request -> ByteString
rawQueryString Request
req
     in ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
forall a b. (a -> b) -> a -> b
$ ByteString
m ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> " " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
p ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
q ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> " " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> String -> ByteString
forall a b. ConvertibleStrings a b => a -> b
cs (HttpVersion -> String
forall a. Show a => a -> String
show HttpVersion
v)

fmtStatus :: Status -> Text
fmtStatus :: Status -> Text
fmtStatus s :: Status
s = String -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (Int -> String
forall a. Show a => a -> String
show (Status -> Int
statusCode Status
s)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> " " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ByteString -> Text
forall a b. ConvertibleStrings a b => a -> b
cs (Status -> ByteString
statusMessage Status
s)