store: take name orders by card, crypto or code

This commit is contained in:
sh-agents committed 2026-11-01 15:27:04 +00:00
1 parent c7d415f706
commit 28ea3b757a
23 files changed
+3260 -344

No files matched your search

@@ -1,13 +1,18 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | The SMP agent logs unconditionally at info on the global logger, so the service keeps its own info lines on this separate channel when that logger is raised to Warn.
module StoreService.Log
( logInfo,
logWarn,
logError,
safely,
safelyWith,
)
where
import Control.Exception (SomeAsyncException, SomeException, fromException, throwIO, try)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Data.ByteString as B
import Data.Text (Text)
@@ -16,6 +21,7 @@ import Data.Text.Encoding (encodeUtf8)
import Data.Time.Format (defaultTimeLocale, formatTime)
import Data.Time.LocalTime (getZonedTime)
import GHC.Stack (CallStack, HasCallStack, callStack, getCallStack, srcLocFile, srcLocStartLine)
import Simplex.Messaging.Util (tshow)
import System.IO (stderr)
logInfo :: (HasCallStack, MonadIO m) => Text -> m ()
@@ -36,3 +42,15 @@ logAt tag cs msg = liftIO $ do
loc = case getCallStack cs of
((_, l) : _) -> T.pack (srcLocFile l) <> ":" <> T.pack (show (srcLocStartLine l))
[] -> "unknown"
safely :: Text -> IO () -> IO Bool
safely what action = safelyWith what False (True <$ action)
-- | Asynchronous exceptions are rethrown, since that is how the race stops this thread.
safelyWith :: Text -> a -> IO a -> IO a
safelyWith what fallback action =
try action >>= \case
Right a -> pure a
Left (e :: SomeException) -> case fromException e :: Maybe SomeAsyncException of
Just _ -> throwIO e
Nothing -> fallback <$ logError (what <> " failed; the next pass will run: " <> tshow e)
@@ -4,20 +4,34 @@ module StoreService.Names.Env (NamesEnv (..), newNamesEnv) where
import Simplex.Chat.Controller (ChatController (..))
import Simplex.Messaging.Agent.Protocol (UserId)
import Simplex.Messaging.Agent.Store.Common (DBStore)
import StoreService.Config (NamesConfig (..))
import StoreService.Names.Chain (newChainClient)
import StoreService.Names.Chain (ChainClient, newChainClient)
import StoreService.Names.Contracts (Contracts, newContracts)
import StoreService.Names.Phases (Phases, newPhases)
import StoreService.Names.Profiles (Profiles, chatProfile, defaultProfileLimits, monotonicSeconds, newProfiles, systemResolver)
import StoreService.Names.Turns (Turns)
import StoreService.Providers (Provider)
import StoreService.Waiters (Waiters)
data NamesEnv = NamesEnv
{ neConfig :: NamesConfig,
{ neChat :: ChatController,
neUserId :: UserId,
neConfig :: NamesConfig,
neStore :: DBStore,
nePhases :: Phases,
neTurns :: Turns,
neChain :: ChainClient,
neContracts :: Contracts,
neProfiles :: Profiles
neProfiles :: Profiles,
neWaiters :: Waiters,
neProviders :: [Provider]
}
newNamesEnv :: ChatController -> UserId -> NamesConfig -> IO NamesEnv
newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain} = do
newNamesEnv :: ChatController -> UserId -> NamesConfig -> DBStore -> Turns -> Waiters -> [Provider] -> IO NamesEnv
newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain} neStore neTurns neWaiters neProviders = do
nePhases <- newPhases
neChain <- newChainClient random nChain
neContracts <- newContracts nTLD neChain
neProfiles <- newProfiles defaultProfileLimits monotonicSeconds (chatProfile neChat neUserId) systemResolver
pure NamesEnv {neConfig, neContracts, neProfiles}
pure NamesEnv {neChat, neUserId, neConfig, neStore, nePhases, neTurns, neChain, neContracts, neProfiles, neWaiters, neProviders}
@@ -0,0 +1,243 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
-- | The checks every order runs, by paid invoice or prepaid code, in the contract's order.
module StoreService.Names.Orders
( OrdersEnv (..),
ordersEnv,
OrderRequest (..),
OrderRefusal (..),
ExistingOrder (..),
ClosedOrder (..),
maxSealedChars,
signedRequestSeconds,
parseOrderRequest,
parseOrderFields,
textField,
integerField,
checkLength,
checkSigner,
checkOrderSigner,
existingOrder,
placeOrder,
placeCodeOrder,
codeOrderInsert,
checkTimestamp,
checkDuration,
checkNameFree,
checkPrice,
)
where
import qualified Control.Exception as E
import Control.Monad (unless, when)
import qualified Data.Aeson as J
import qualified Data.Aeson.KeyMap as KM
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import Data.Int (Int64)
import Data.Scientific (toBoundedInteger)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (UTCTime, getCurrentTime)
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Data.Word (Word64)
import Simplex.Chat.Badges.Code (maxNameLength, minNameLength)
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import qualified Simplex.Messaging.Agent.Store.DB as DB
import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature)
import Simplex.Messaging.Eth.Address (Address, recoverAddress)
import Simplex.Messaging.SimplexName (LabelHash (..))
import Simplex.Messaging.Util (tshow)
import StoreService.Config (NamesConfig (..))
import StoreService.Log (logWarn)
import StoreService.Names.Codes (CodeRefusal (..), codeRefusal, reserveRefusal)
import StoreService.Names.Contracts (Contracts, NameStatus (..), chainPriceCents, nameStatus, secondsPerYear)
import StoreService.Names.Env (NamesEnv (..))
import StoreService.Names.Phases (Phases, RegistrationId (..))
import StoreService.Names.Abi (bytes32FromHex)
import StoreService.Names.Signatures (OrderFields (..), orderMessage, parseChecksumAddress, parseSignature, personalDigest)
import StoreService.Names.Store (LiveOrder (..), NameRegistration (..), NewRegistration (..), PrepaidCode (..), RegistrationRefused (..), closeOverdue, createCodeRegistration, getLiveOrders, getPrepaidCode, getRegistrationByOrderHash, isLabelRegistering)
import StoreService.Names.Turns (Turns, withCodeTurn)
data OrdersEnv = OrdersEnv
{ oeStore :: DBStore,
oeConfig :: NamesConfig,
oeContracts :: Contracts,
oeTurns :: Turns,
oePhases :: Phases,
oeNow :: IO UTCTime
}
ordersEnv :: NamesEnv -> OrdersEnv
ordersEnv NamesEnv {neStore, neConfig, neContracts, neTurns, nePhases} =
OrdersEnv {oeStore = neStore, oeConfig = neConfig, oeContracts = neContracts, oeTurns = neTurns, oePhases = nePhases, oeNow = getCurrentTime}
-- | The fields both kinds of order carry, with the owner's signature over them.
data OrderRequest = OrderRequest
{ orOwner :: Address,
orFields :: OrderFields,
orTimestamp :: Word64,
orSignature :: RecoverableSignature
}
data OrderRefusal
= ORBadRequest
| ORInvalidName
| OROrderExists
| ORNameTaken
| ORNameCheckUnavailable
| ORCatalogChanged Int
| ORCode CodeRefusal
| ORCodesOnly
deriving (Eq, Show)
newtype OrderRefused = OrderRefused OrderRefusal
deriving (Show)
instance E.Exception OrderRefused
-- | An exact resend of a stored order, live or not; or the overdue orders of the same wallet and name the new order closes.
data ExistingOrder = EOResent NameRegistration | EONew [NameRegistration] | EOMeets
-- | An overdue order a new order closed, at its new version; its invoice is now expired.
data ClosedOrder = ClosedOrder {coRegistration :: NameRegistration, coVersion :: Int64}
maxSealedChars :: Int
maxSealedChars = 11095
-- | How far a signed request's timestamp may be from the service's clock.
signedRequestSeconds :: Integer
signedRequestSeconds = 600
-- | Every format check; the length's range is checkLength's, after the formats a kind of order adds.
parseOrderRequest :: J.Object -> Maybe OrderRequest
parseOrderRequest o = do
orOwner <- either (const Nothing) Just . parseChecksumAddress =<< textField o "owner"
(orFields, orTimestamp, orSignature) <- parseOrderFields o
pure OrderRequest {orOwner, orFields, orTimestamp, orSignature}
-- | The signed fields of an order, with their timestamp and signature.
parseOrderFields :: J.Object -> Maybe (OrderFields, Word64, RecoverableSignature)
parseOrderFields o = do
ofLabelHash <- LabelHash <$> (bytes32FromHex =<< textField o "labelHash")
ofLength <- integerField =<< KM.lookup "length" o
ofDuration <- integerField =<< KM.lookup "duration" o
ofCommitment <- bytes32FromHex =<< textField o "commitment"
ofSealed <- textField o "sealed"
unless (T.length ofSealed <= maxSealedChars) Nothing
timestamp <- integerField =<< KM.lookup "timestamp" o
signature <- either (const Nothing) Just . parseSignature =<< textField o "signature"
pure (OrderFields {ofLabelHash, ofLength, ofDuration, ofCommitment, ofSealed}, timestamp, signature)
textField :: J.Object -> J.Key -> Maybe Text
textField o k = case KM.lookup k o of
Just (J.String t) -> Just t
_ -> Nothing
-- | A JSON integer within the type's range; a fraction, an exponent past it, or any other value is Nothing.
integerField :: (Integral a, Bounded a) => J.Value -> Maybe a
integerField = \case
J.Number n -> toBoundedInteger n
_ -> Nothing
checkLength :: OrderFields -> Either OrderRefusal ()
checkLength OrderFields {ofLength} =
unless (ofLength >= minNameLength && ofLength <= maxNameLength) $ Left ORInvalidName
-- | The owner's EIP-191 signature over the message.
checkSigner :: Address -> ByteString -> RecoverableSignature -> IO (Either OrderRefusal ())
checkSigner owner msg signature =
recoverAddress signature (personalDigest msg) >>= \case
Right signer | signer == owner -> pure $ Right ()
_ -> pure $ Left ORBadRequest
checkOrderSigner :: OrderRequest -> IO (Either OrderRefusal ())
checkOrderSigner OrderRequest {orOwner, orFields, orTimestamp, orSignature} = checkSigner orOwner (orderMessage orFields orTimestamp) orSignature
existingOrder :: DB.Connection -> OrderRequest -> ByteString -> UTCTime -> IO ExistingOrder
existingOrder db req hash now =
getRegistrationByOrderHash db hash >>= \case
Just r -> pure $ EOResent r
Nothing -> maybe EOMeets EONew <$> overdueOrdersMet db req now
-- | The live orders of the request's commitment, or of its owner and label hash, when all are overdue orders of the same wallet and name;
-- Nothing when any other live order holds them.
overdueOrdersMet :: DB.Connection -> OrderRequest -> UTCTime -> IO (Maybe [NameRegistration])
overdueOrdersMet db OrderRequest {orOwner, orFields = OrderFields {ofLabelHash, ofCommitment}} now = do
orders <- getLiveOrders db ofCommitment orOwner ofLabelHash now
pure $ if all closable orders then Just (map loRegistration orders) else Nothing
where
closable LiveOrder {loRegistration = NameRegistration {nrOwner, nrLabelHash}, loOverdue} =
loOverdue && nrOwner == orOwner && nrLabelHash == ofLabelHash
-- | In one transaction, the overdue orders the request meets are closed, then the order is inserted.
-- Refused, changing nothing, when it meets an order it cannot close, or the insert refuses.
placeOrder :: OrdersEnv -> OrderRequest -> (DB.Connection -> UTCTime -> IO (Either OrderRefusal ())) -> IO (Either OrderRefusal [ClosedOrder])
placeOrder OrdersEnv {oeStore, oeNow} req insert = do
now <- oeNow
first (\(OrderRefused refusal) -> refusal) <$> E.try (withTransaction oeStore $ \db -> transaction db now)
where
transaction db now =
overdueOrdersMet db req now >>= \case
Just overdue -> do
closed <- mapM (close db now) overdue
insert db now >>= either (E.throwIO . OrderRefused) (const $ pure closed)
Nothing -> E.throwIO $ OrderRefused OROrderExists
close db now r =
closeOverdue db r now >>= \case
Just v -> pure ClosedOrder {coRegistration = r, coVersion = v}
Nothing -> E.throwIO $ OrderRefused OROrderExists
-- | In the code's turn, the code as it reads there must pay for the name; the order's insert then reserves it.
placeCodeOrder :: OrdersEnv -> OrderRequest -> RegistrationId -> PrepaidCode -> IO (Either OrderRefusal [ClosedOrder])
placeCodeOrder env@OrdersEnv {oeStore, oeTurns, oeNow} req@OrderRequest {orOwner, orFields = fields@OrderFields {ofLength}} rid PrepaidCode {pcCodeHash, pcYears} =
withCodeTurn oeTurns pcCodeHash $ do
now <- oeNow
withTransaction oeStore (`getPrepaidCode` pcCodeHash) >>= \case
Nothing -> pure $ Left $ ORCode CRInvalid
Just code
| Just refusal <- codeRefusal now rid ofLength code -> pure $ Left $ ORCode refusal
| otherwise -> placeOrder env req $ codeOrderInsert NewRegistration {nrgId = rid, nrgOwner = orOwner, nrgOrder = fields, nrgYears = pcYears} code
-- | The code was checked in its turn, so only a writer outside this process can make its reservation fail.
codeOrderInsert :: NewRegistration -> PrepaidCode -> DB.Connection -> UTCTime -> IO (Either OrderRefusal ())
codeOrderInsert new@NewRegistration {nrgId, nrgOrder = OrderFields {ofLength}} code@PrepaidCode {pcCodeHash} db now =
createCodeRegistration db new code now >>= \case
Right () -> pure $ Right ()
Left RRLiveOrder -> pure $ Left OROrderExists
Left RRCodeNotReservable -> Left . ORCode <$> reserveRefusal db now pcCodeHash nrgId ofLength
checkTimestamp :: UTCTime -> Word64 -> Either OrderRefusal ()
checkTimestamp now timestamp =
unless (abs (toInteger timestamp - unixSeconds now) <= signedRequestSeconds) $ Left ORBadRequest
-- | The paid years plus the free days until the term starts, by the service's clock, within the slack.
checkDuration :: NamesConfig -> UTCTime -> Int -> Word64 -> Either OrderRefusal ()
checkDuration NamesConfig {nTermStart, nTermSlackSeconds} now years duration = do
let free = toInteger duration - toInteger years * secondsPerYear
expected = max 0 (unixSeconds nTermStart - unixSeconds now)
when (free < 0 || abs (free - expected) > toInteger nTermSlackSeconds) $ Left ORBadRequest
-- | A name being registered here searches as available, so it is checked here too.
checkNameFree :: Contracts -> DBStore -> LabelHash -> IO (Either OrderRefusal ())
checkNameFree ct st lh =
nameStatus ct lh >>= \case
Left e -> Left ORNameCheckUnavailable <$ logWarn ("names: an order's name check failed: " <> tshow e)
Right NSAvailable -> do
registering <- withTransaction st (`isLabelRegistering` lh)
pure $ if registering then Left ORNameTaken else Right ()
Right _ -> pure $ Left ORNameTaken
-- | The chain price for the length and years, which must be the price the order saw.
checkPrice :: Contracts -> Int -> Int -> Int -> IO (Either OrderRefusal ())
checkPrice ct len years price =
chainPriceCents ct len years >>= \case
Left e -> Left ORNameCheckUnavailable <$ logWarn ("names: an order's price read failed: " <> tshow e)
Right chainPrice
| chainPrice /= price -> pure $ Left $ ORCatalogChanged chainPrice
| otherwise -> pure $ Right ()
unixSeconds :: UTCTime -> Integer
unixSeconds = floor . utcTimeToPOSIXSeconds
@@ -1,15 +1,95 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module StoreService.Names.View
( addressText,
( ViewReads (..),
registrationView,
readViewChain,
viewJSON,
addressText,
tldText,
registrationAt,
lapseMoment,
notSignedInTime,
registrationVersion,
)
where
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import Data.Int (Int64)
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import Simplex.Messaging.Encoding.String (strEncode)
import Data.Time.Clock (UTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Data.Word (Word64)
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import Simplex.Messaging.Encoding.String (strEncode, textEncode)
import Simplex.Messaging.Eth.Address (Address)
import Simplex.Messaging.SimplexName (SimplexTLD)
import Simplex.Messaging.SimplexName (LabelHash (..), SimplexTLD)
import Simplex.Messaging.Util (safeDecodeUtf8)
import StoreService.Config (ChainConfig (..), NamesConfig (..))
import StoreService.Names.Abi (hex)
import StoreService.Names.Chain (TxHash (..))
import StoreService.Names.Contracts (Contracts, sponsorNonce)
import StoreService.Names.Phases (RegistrationId (..))
import StoreService.Names.Store (NamePhase (..), NameRegistration (..), NameReveal (..), getRegistration)
-- | The view's chain reads; a field whose read failed is null and named in vrUnread.
data ViewReads = ViewReads
{ vrIntentNonce :: Maybe Word64,
vrUnread :: [Text]
}
deriving (Eq, Show)
registrationView :: DBStore -> NamesConfig -> Contracts -> NameRegistration -> IO J.Value
registrationView _ cfg ct r = viewJSON cfg r <$> readViewChain ct r
-- | The intent nonce only in committed.
readViewChain :: Contracts -> NameRegistration -> IO ViewReads
readViewChain ct NameRegistration {nrPhase, nrOwner} = case nrPhase of
PCommitted -> sponsorNonce ct nrOwner >>= \case
Right nonce -> pure none {vrIntentNonce = Just nonce}
Left _ -> pure none {vrUnread = ["intentNonce"]}
_ -> pure none
where
none = ViewReads {vrIntentNonce = Nothing, vrUnread = []}
viewJSON :: NamesConfig -> NameRegistration -> ViewReads -> J.Value
viewJSON NamesConfig {nTLD, nChain = ChainConfig {cChainId, cResolver, cSponsor}} r ViewReads {vrIntentNonce, vrUnread} =
J.object
[ "id" .= rid,
"version" .= nrVersion,
"moves" .= nrMoves,
"linksRefused" .= isJust nrLinksRefusedAt,
"phase" .= (textEncode nrPhase :: Text),
"labelHash" .= hex lh,
"label" .= (rvLabel <$> nrReveal),
"owner" .= addressText nrOwner,
"commitment" .= hex nrCommitment,
"sealed" .= nrSealed,
"resolver" .= addressText cResolver,
"sponsor" .= addressText cSponsor,
"chainId" .= cChainId,
"tld" .= tldText nTLD,
"years" .= nrYears,
"duration" .= nrDuration,
"price" .= nrPrice,
"commitTx" .= (txText <$> nrCommitTx),
"registerTx" .= (txText <$> nrRegisterTx),
"revealAfter" .= nrRevealAfter,
"revealBefore" .= nrRevealBefore,
"intentNonce" .= vrIntentNonce,
"unread" .= vrUnread,
"expiresAt" .= nrExpiresAt,
"error" .= nrError
]
where
NameRegistration {nrId = RegistrationId rid, nrVersion, nrMoves, nrLinksRefusedAt, nrPhase, nrLabelHash = LabelHash lh, nrReveal, nrOwner, nrCommitment, nrSealed} = r
NameRegistration {nrYears, nrDuration, nrPrice, nrCommitTx, nrRegisterTx, nrRevealAfter, nrRevealBefore, nrExpiresAt, nrError} = r
txText (TxHash h) = hex h
-- | EIP-55, as the page compares owners.
addressText :: Address -> Text
@@ -18,3 +98,21 @@ addressText = safeDecodeUtf8 . strEncode
-- | The TLD as the page shows it after the name's dot.
tldText :: SimplexTLD -> Text
tldText = T.dropWhile (== '.') . safeDecodeUtf8 . strEncode
-- | Versions start at 1, so a registration deleted during a hold reads as changed.
registrationVersion :: DBStore -> RegistrationId -> IO Int64
registrationVersion st rid = maybe 0 nrVersion <$> withTransaction st (`getRegistration` rid)
notSignedInTime :: Text
notSignedInTime = "The name was not signed in time"
-- | Past revealBefore with no reveal, the commitment can no longer register, so it reads failed.
registrationAt :: UTCTime -> NameRegistration -> NameRegistration
registrationAt now r = case lapseMoment r of
Just lapse | now > lapse -> r {nrPhase = PFailed, nrError = Just notSignedInTime}
_ -> r
lapseMoment :: NameRegistration -> Maybe UTCTime
lapseMoment = \case
NameRegistration {nrPhase = PCommitted, nrReveal = Nothing, nrRevealBefore = Just before} -> Just $ posixSecondsToUTCTime $ fromIntegral before
_ -> Nothing
@@ -1,19 +1,36 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module StoreService.Orders (settleOrder, decide, codeLifetime) where
module StoreService.Orders
( RegistrationChanged (..),
settleOrder,
cancelOpenInvoice,
expireOverdue,
publishRegistration,
decide,
codeLifetime,
)
where
import StoreService.Log (logError, safelyWith)
import StoreService.Names.Phases (Phases, RegistrationId (..))
import qualified StoreService.Names.Phases as Phases
import StoreService.Names.Store (NamePhase (..), NameRegistration (..), VersionWrite (..), getRegistration, updateRegistration)
import StoreService.Providers (Funded (..), PaymentSignal (..), Received (..))
import StoreService.Store.Invoices (InvoicePayment (..), InvoiceRow (..), markCodePaid, paymentStatusText, settlementCodeHash, settlementInvoice, truncateToSecond, updateInvoiceStatus, upsertPayment)
import StoreService.Store.Invoices (InvoicePayment (..), InvoiceProduct (..), InvoiceRow (..), expireOverdueInvoice, lockInvoice, markCodePaid, paymentHolds, paymentStatusText, settlementCodeHash, settlementInvoice, truncateToSecond, updateInvoiceStatus, upsertPayment)
import StoreService.Waiters (Waiters, publish, publishPayment)
import Control.Concurrent.STM (atomically)
import Control.Monad (when)
import Control.Concurrent.STM (STM, atomically)
import Control.Exception (Exception)
import qualified Control.Exception as E
import Control.Monad (forM_, when)
import Data.ByteString (ByteString)
import Data.Maybe (fromMaybe)
import Data.Int (Int64)
import Data.Maybe (catMaybes, fromMaybe, isJust)
import Data.Text (Text)
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime)
import Simplex.Chat.PaymentService.Types (CurrencyAmount (..), InvoiceId, InvoiceStatus (..), PaymentStatus (..))
import Simplex.Chat.PaymentService.Types (CurrencyAmount (..), InvoiceId (..), InvoiceStatus (..), PaymentStatus (..))
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import qualified Simplex.Messaging.Agent.Store.DB as DB
@@ -21,6 +38,12 @@ import qualified Simplex.Messaging.Agent.Store.DB as DB
codeLifetime :: NominalDiffTime
codeLifetime = 365 * 24 * 60 * 60
-- | Thrown inside the transaction that read the registration, so the invoice's own change is rolled back with it.
newtype RegistrationChanged = RegistrationChanged RegistrationId
deriving (Eq, Show)
instance Exception RegistrationChanged
data Write = Write
{ wStatus :: Maybe InvoiceStatus,
wPayment :: PaymentStatus,
@@ -59,31 +82,54 @@ maxBackdate = 30 * 24 * 60 * 60
data Published = PubNothing | PubStatus InvoiceStatus | PubPayment
settleOrder :: DBStore -> Waiters -> InvoiceId -> PaymentSignal -> UTCTime -> IO (Either Text InvoiceStatus)
settleOrder st waiters invId signal now' = do
-- | What the invoice pays for, read before any write.
data Product = PBadge (Maybe ByteString) | PName NameRegistration
-- | A name order's change at settlement: its new version, or money received after it closed, which stays unpaid.
data NameOutcome = NOWritten RegistrationId Int64 | NOLate RegistrationId
settleOrder :: DBStore -> Waiters -> Maybe Phases -> InvoiceId -> PaymentSignal -> UTCTime -> IO (Either Text InvoiceStatus)
settleOrder st waiters phases invId signal now' = do
outcome <- withTransaction st $ \db ->
settlementInvoice db invId >>= \case
Nothing -> pure (Left "no such invoice")
Just row@InvoiceRow {irStatus} -> case decide irStatus signal of
Nothing -> pure (Right (irStatus, PubNothing))
-- look up the code first, so an invoice with none leaves the transaction empty, not paid with an unpaid code
Just w -> codeToMark db w >>= either (pure . Left) (settle db row w)
Just InvoiceRow {irStatus}
| isJust (decide irStatus signal) -> do
-- On Postgres, a concurrent overdue close holding the row is waited for, and the read below sees its writes;
-- a close that comes later waits for this payment and then sees it.
lockInvoice db invId now
settlementInvoice db invId >>= settleRow db
row_ -> settleRow db row_
case outcome of
Left e -> pure (Left e)
Right (status, toPublish) -> do
Right (status, toPublish, name) -> do
-- after the commit, or a woken reader will not see the write
atomically $ case toPublish of
PubStatus s -> publish waiters invId s
PubPayment -> publishPayment waiters invId
PubNothing -> pure ()
atomically $ do
case toPublish of
PubStatus s -> publish waiters invId s
PubPayment -> publishPayment waiters invId
PubNothing -> pure ()
forM_ name $ \case
NOWritten rid v -> publishRegistration phases (rid, v)
NOLate _ -> pure ()
forM_ name $ \case
NOLate rid -> latePayment invId rid
NOWritten {} -> pure ()
pure (Right status)
where
now = truncateToSecond now'
at = truncateToSecond (settledInstant signal now)
codeToMark :: DB.Connection -> Write -> IO (Either Text (Maybe ByteString))
codeToMark db Write {wCode}
| not wCode = pure (Right Nothing)
| otherwise = maybe (Left "settled invoice has no code hash") (Right . Just) <$> settlementCodeHash db invId
settleRow db = \case
Nothing -> pure (Left "no such invoice")
Just row@InvoiceRow {irStatus} -> case decide irStatus signal of
Nothing -> pure (Right (irStatus, PubNothing, []))
-- read the product first, so an invoice without one is not marked paid with nothing to show for it
Just w -> productOf db row w >>= either (pure . Left) (settle db row w)
productOf :: DB.Connection -> InvoiceRow -> Write -> IO (Either Text Product)
productOf db InvoiceRow {irProduct} Write {wCode} = case irProduct of
IPBadge {}
| not wCode -> pure (Right (PBadge Nothing))
| otherwise -> maybe (Left "settled invoice has no code hash") (Right . PBadge . Just) <$> settlementCodeHash db invId
IPName rid -> maybe (Left "name invoice has no registration") (Right . PName) <$> getRegistration db rid
newPayment :: InvoiceRow -> Write -> Bool
newPayment InvoiceRow {irPayment} Write {wPayment}
-- Monero reports an invoice as confirming while its figures are still zero, so a funded verdict earns a row
@@ -101,17 +147,95 @@ settleOrder st waiters invId signal now' = do
where
Received {rcvAmount, rcvCrypto} = received signal
CurrencyAmount minor = rcvAmount
settle :: DB.Connection -> InvoiceRow -> Write -> Maybe ByteString -> IO (Either Text (InvoiceStatus, Published))
settle db row@InvoiceRow {irStatus} w@Write {wStatus, wPayment} codeHash = do
settle :: DB.Connection -> InvoiceRow -> Write -> Product -> IO (Either Text (InvoiceStatus, Published, [NameOutcome]))
settle db row@InvoiceRow {irStatus} w@Write {wStatus, wPayment} sold = do
let Received {rcvAmount, rcvCrypto, rcvDue} = received signal
wrotePayment = newPayment row w
when wrotePayment $ upsertPayment db row wPayment rcvAmount rcvCrypto rcvDue (paidInFull signal) at
moved <- maybe (pure True) (\new -> updateInvoiceStatus db invId irStatus new at) wStatus
if moved
then do
mapM_ (\h -> markCodePaid db h (addUTCTime codeLifetime at)) codeHash
pure (Right (fromMaybe irStatus wStatus, maybe (if wrotePayment then PubPayment else PubNothing) PubStatus wStatus))
name <- case sold of
PBadge codeHash -> [] <$ mapM_ (\h -> markCodePaid db h (addUTCTime codeLifetime at)) codeHash
PName r -> nameSettled db irStatus wStatus wrotePayment (wrotePayment || maybe False paymentHolds (irPayment row)) r now
pure (Right (fromMaybe irStatus wStatus, maybe (if wrotePayment then PubPayment else PubNothing) PubStatus wStatus, name))
else do
-- another writer moved the row first, so publish what we found
current <- maybe irStatus (\InvoiceRow {irStatus = s} -> s) <$> settlementInvoice db invId
pure (Right (current, PubStatus current))
pure (Right (current, PubStatus current, []))
-- | A paid open invoice queues its order only while the order is live; a payment of a closed one is late.
-- An invoice the provider closed closes its order, and is late once if it holds money, read now or before.
-- BTCPay reports money paid into a closed invoice as Expired or Invalid, not Settled, so a new payment figure
-- on a closed invoice is late too; the monotonic payment write reports each figure once.
nameSettled :: DB.Connection -> InvoiceStatus -> Maybe InvoiceStatus -> Bool -> Bool -> NameRegistration -> UTCTime -> IO [NameOutcome]
nameSettled db before after wrotePayment holdsMoney r@NameRegistration {nrId, nrPhase} now
| nrPhase /= PUnpaid = pure []
| otherwise = case after of
Just ISPaid
| before /= ISOpen -> pure [NOLate nrId]
| otherwise ->
updateRegistration db r {nrPhase = PQueued} now >>= \case
Written v -> pure [NOWritten nrId v]
NotLive -> pure [NOLate nrId]
-- not expected while this order is live; kept so the payment is still recorded
LiveOrderExists -> pure [NOLate nrId]
_ -> E.throwIO (RegistrationChanged nrId)
Just ISExpired -> (<> [NOLate nrId | holdsMoney]) . maybe [] (\(rid, v) -> [NOWritten rid v]) <$> closeOrder db r now
_ -> pure [NOLate nrId | before /= ISOpen, wrotePayment]
-- | Clears a live unpaid order's live flag, as its invoice closes in the same transaction.
closeOrder :: DB.Connection -> NameRegistration -> UTCTime -> IO (Maybe (RegistrationId, Int64))
closeOrder db r@NameRegistration {nrId, nrPhase, nrLive} now
| nrPhase /= PUnpaid || not nrLive = pure Nothing
| otherwise =
updateRegistration db r {nrLive = False} now >>= \case
Written v -> pure (Just (nrId, v))
_ -> E.throwIO (RegistrationChanged nrId)
-- | The order of a name invoice whose status this transaction moved to expired.
closeInvoiceOrder :: DB.Connection -> InvoiceId -> UTCTime -> IO (Maybe (RegistrationId, Int64))
closeInvoiceOrder db invId now =
settlementInvoice db invId >>= \case
Just InvoiceRow {irProduct = IPName rid} -> getRegistration db rid >>= maybe (pure Nothing) (\r -> closeOrder db r now)
_ -> pure Nothing
latePayment :: InvoiceId -> RegistrationId -> IO ()
latePayment (InvoiceId invText) (RegistrationId rid) =
logError ("names: invoice " <> invText <> " was paid after its order closed, so registration " <> rid <> " stays unpaid; refund the payment")
-- | False when the invoice was no longer open. Throws RegistrationChanged, changing nothing, when its order changed meanwhile.
-- The provider has taken the cancel, so a name invoice a payment landed on meanwhile expires too, and its payment is late.
cancelOpenInvoice :: DBStore -> Waiters -> Maybe Phases -> InvoiceId -> UTCTime -> IO Bool
cancelOpenInvoice st waiters phases invId now = do
closed <- withTransaction st $ \db -> do
-- On Postgres, a settlement holding the row is waited for, and the read below sees its payment.
lockInvoice db invId now
settlementInvoice db invId >>= \case
Just row@InvoiceRow {irStatus = ISOpen} -> do
cancelled <- updateInvoiceStatus db invId ISOpen ISExpired now
if cancelled then Just . (,lateOf row) <$> closeInvoiceOrder db invId now else pure Nothing
_ -> pure Nothing
forM_ closed $ \(order, late) -> do
atomically $ publish waiters invId ISExpired >> mapM_ (publishRegistration phases) order
mapM_ (latePayment invId) late
pure (isJust closed)
where
lateOf InvoiceRow {irProduct, irPayment} = case irProduct of
IPName rid | maybe False paymentHolds irPayment -> Just rid
_ -> Nothing
-- | Of the given invoices, expires those still open, with no money received, and past the cutoff, closing their orders.
-- Each in its own transaction, so an invoice that fails is logged and skipped, and the others still expire.
expireOverdue :: DBStore -> Waiters -> Maybe Phases -> UTCTime -> UTCTime -> [InvoiceId] -> IO [InvoiceId]
expireOverdue st waiters phases now cutoff = fmap catMaybes . mapM expire
where
expire invId@(InvoiceId invText) = safelyWith ("expiring invoice " <> invText) Nothing $ do
expired <- withTransaction st $ \db -> do
moved <- expireOverdueInvoice db cutoff invId
if moved then Just <$> closeInvoiceOrder db invId now else pure Nothing
forM_ expired $ \order -> atomically $ publish waiters invId ISExpired >> mapM_ (publishRegistration phases) order
pure (invId <$ expired)
publishRegistration :: Maybe Phases -> (RegistrationId, Int64) -> STM ()
publishRegistration phases (rid, v) = mapM_ (\p -> Phases.publish p rid v) phases
@@ -24,17 +24,22 @@ module StoreService.Poller
readsPerPass,
skipWarnInterval,
maxSkipReasons,
cleanupInterval,
cleanupReadsPerPass,
unpaidRetention,
cleanupUnpaid,
)
where
import StoreService.Config (PollConfig (..))
import StoreService.Orders (decide, settleOrder)
import StoreService.Names.Phases (Phases, RegistrationId (..))
import StoreService.Names.Store (deleteUnpaidRegistration, unpaidCleanupCandidates)
import StoreService.Orders (decide, expireOverdue, settleOrder)
import StoreService.Providers (ListPass (..), PaymentSignal (..), Provider (..), ProviderError (..), Received (..), expiresItself, settleWindow)
import StoreService.Store.Invoices (InvoiceRow (..), OverdueInvoice (..), expireOverdue, getInvoiceByProviderRef, overdueInvoices, unpaidRefs)
import StoreService.Waiters (Waiters, publish, waitingCount, waitingCountSTM)
import StoreService.Store.Invoices (InvoiceProduct (..), InvoiceRow (..), OverdueInvoice (..), getInvoice, getInvoiceByProviderRef, overdueInvoices, unpaidRefs)
import StoreService.Waiters (Waiters, waitingCount, waitingCountSTM)
import Control.Concurrent.STM
import Control.Exception (SomeAsyncException, SomeException, fromException, throwIO, try)
import StoreService.Log (logError, logInfo, logWarn)
import StoreService.Log (logError, logInfo, logWarn, safely, safelyWith)
import Control.Monad (filterM, forM_, forever, unless, void, when)
import Data.List (find, partition, sortOn)
import Data.Map.Strict (Map)
@@ -45,8 +50,8 @@ import qualified Data.Set as S
import Data.Text (Text)
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, diffUTCTime, getCurrentTime)
import Numeric.Natural (Natural)
import Simplex.Chat.PaymentService.Types (InvoiceStatus (..), PaymentProvider (..))
import Simplex.Messaging.Agent.Store.Common (DBStore)
import Simplex.Chat.PaymentService.Types (InvoiceId (..), InvoiceStatus (..), PaymentProvider (..))
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import Simplex.Messaging.Encoding.String (textEncode)
import Simplex.Messaging.Util (tshow)
@@ -54,6 +59,18 @@ import Simplex.Messaging.Util (tshow)
expiryGrace :: NominalDiffTime
expiryGrace = 600
-- | How often unpaid name orders are cleaned up; a provider that does not answer keeps an order until the next run.
cleanupInterval :: NominalDiffTime
cleanupInterval = 24 * 3600
-- | The provider reads one pass's cleanup makes, so settlement reads are not held up; the rest wait for the next pass.
cleanupReadsPerPass :: Int
cleanupReadsPerPass = 25
-- | How long an unpaid name order is kept after its invoice expired.
unpaidRetention :: NominalDiffTime
unpaidRetention = 30 * 24 * 3600
-- | Give up on a failing cancel only after an hour, not on the first failure.
cancelGiveUpDelay :: NominalDiffTime
cancelGiveUpDelay = 3600
@@ -92,6 +109,8 @@ minCadenceSeconds = 1
data PollerEnv = PollerEnv
{ peStore :: DBStore,
peWaiters :: Waiters,
-- | Nothing when the service sells no names.
pePhases :: Maybe Phases,
peHints :: ReadHints,
peProviders :: [Provider],
pePoll :: PollConfig,
@@ -99,15 +118,20 @@ data PollerEnv = PollerEnv
peListedAt :: TVar (Maybe UTCTime),
peStrayEvery :: NominalDiffTime,
-- When the cancel first failed, for orders older than 72 hours.
peCancelFailed :: TVar (Map Text UTCTime)
peCancelFailed :: TVar (Map Text UTCTime),
peCleanedAt :: TVar (Maybe UTCTime),
-- | The orders of the day's cleanup not read yet.
peCleanupLeft :: TVar [(RegistrationId, InvoiceId)]
}
newPollerEnv :: DBStore -> Waiters -> ReadHints -> [Provider] -> PollConfig -> IO PollerEnv
newPollerEnv peStore peWaiters peHints peProviders pePoll = do
newPollerEnv :: DBStore -> Waiters -> Maybe Phases -> ReadHints -> [Provider] -> PollConfig -> IO PollerEnv
newPollerEnv peStore peWaiters pePhases peHints peProviders pePoll = do
peSkipped <- newTVarIO M.empty
peListedAt <- newTVarIO Nothing
peCancelFailed <- newTVarIO M.empty
pure PollerEnv {peStore, peWaiters, peHints, peProviders, pePoll, peSkipped, peListedAt, peStrayEvery = strayListInterval, peCancelFailed}
peCleanedAt <- newTVarIO Nothing
peCleanupLeft <- newTVarIO []
pure PollerEnv {peStore, peWaiters, pePhases, peHints, peProviders, pePoll, peSkipped, peListedAt, peStrayEvery = strayListInterval, peCancelFailed, peCleanedAt, peCleanupLeft}
-- | Read providers before the sweep, and leave out of the sweep every invoice whose read or settlement failed,
-- so an invoice with money in it is never expired. A list that cannot account for every invoice skips the sweep.
@@ -127,7 +151,9 @@ runOnePass env@PollerEnv {peStore, peProviders} = do
when listNow $ mapM_ (listPass env now) peProviders
pure (Just missed)
pruneSkipLog env now
unless (null peProviders) $ forM_ missed $ sweepExpired env now
-- the cleanup runs whatever the sweep met
unless (null peProviders) $ forM_ missed $ \refs -> safely "badge poller: the sweep" (sweepExpired env now refs)
cleanupUnpaid env now
-- | The cases the stray list catches are rare and not urgent, so minutes, not the pass cadence.
strayListInterval :: NominalDiffTime
@@ -161,7 +187,7 @@ readRows :: PollerEnv -> UTCTime -> [(Text, Text)] -> IO (Set Text)
readRows env now rows =
S.fromList . map snd <$> filterM (\r -> not <$> safelyWith (readWhat r) False (readRow r)) rows
where
readWhat (provider, ref) = "reading " <> provider <> " invoice " <> ref
readWhat (provider, ref) = "badge poller: reading " <> provider <> " invoice " <> ref
readRow row@(_, ref) = coveringProvider env now row >>= maybe (pure False) (`readOne` ref)
readOne p ref =
pReadInvoice p ref >>= \case
@@ -191,23 +217,24 @@ listPass env now p =
then Nothing
else Just $ S.fromList $ map fst unsettled <> catMaybes skippedOurs
where
settleWhat (ref, _) = "settling " <> tshow (pProvider p) <> " invoice " <> ref
skipWhat (ref, _) = "reading the skipped " <> tshow (pProvider p) <> " invoice " <> fromMaybe "the provider did not name" ref
settleWhat (ref, _) = "badge poller: settling " <> tshow (pProvider p) <> " invoice " <> ref
skipWhat (ref, _) = "badge poller: reading the skipped " <> tshow (pProvider p) <> " invoice " <> fromMaybe "the provider did not name" ref
-- | provider_ref is unique table-wide, not per provider, so check the provider too.
settleMoved :: PollerEnv -> PaymentProvider -> UTCTime -> (Text, PaymentSignal) -> IO ()
settleMoved env@PollerEnv {peStore, peWaiters} provider now (ref, signal) =
settleMoved env@PollerEnv {peStore, peWaiters, pePhases} provider now (ref, signal) =
getInvoiceByProviderRef peStore ref >>= \case
Just InvoiceRow {irInvoiceId, irStatus, irProvider} | irProvider == provider ->
Just InvoiceRow {irInvoiceId, irStatus, irProvider, irProduct} | irProvider == provider ->
when (isJust (decide irStatus signal)) $
settleOrder peStore peWaiters irInvoiceId signal now >>= \case
settleOrder peStore peWaiters pePhases irInvoiceId signal now >>= \case
Left e -> logError ("badge poller: settling order " <> ref <> " failed: " <> e)
Right status -> reportSettled irStatus status
Right status -> reportSettled irProduct irStatus status
_ -> pure ()
where
reportSettled before after
-- checked before the no-change case, so a refund still alerts when money lands after expiry
| after == ISExpired, SigClosed Received {rcvCrypto = Just paid} _ <- signal = do
reportSettled sold before after
-- checked before the no-change case, so a refund still alerts when money lands after expiry;
-- settlement itself alerts once for a name order's late payment
| IPBadge {} <- sold, after == ISExpired, SigClosed Received {rcvCrypto = Just paid} _ <- signal = do
let alert = "badge poller: order " <> ref <> " expired holding " <> paid <> ", which needs a refund"
due <- dueToWarn env now alert
when due $ logError alert
@@ -234,22 +261,52 @@ readHint env@PollerEnv {peStore, peProviders} now ref =
-- | Leaves out the invoices whose read or settlement failed this pass.
sweepExpired :: PollerEnv -> UTCTime -> Set Text -> IO ()
sweepExpired env@PollerEnv {peStore, peWaiters, peCancelFailed} now missed = do
sweepExpired env@PollerEnv {peStore, peWaiters, pePhases, peCancelFailed} now missed = do
let cutoff = addUTCTime (negate expiryGrace) now
(selfExpiring, payable) <- partition (expiresItself . oiProvider) . filter ((`S.notMember` missed) . oiProviderRef) <$> overdueInvoices peStore cutoff
atomically $ modifyTVar' peCancelFailed (`M.restrictKeys` S.fromList (map oiProviderRef payable))
-- Cancel only a few orders per pass, so a long list does not block other work.
cancelled <- filterM (closedAtProvider env now) (take readsPerPass payable)
expired <- expireOverdue peStore cutoff (map oiInvoiceId (selfExpiring <> cancelled))
-- publish only after the write commits, or a woken reader will not see it
unless (null expired) $ do
atomically $ mapM_ (\invId -> publish peWaiters invId ISExpired) expired
logInfo ("badge poller: expired " <> tshow (length expired) <> " invoice(s) past their window")
expired <- expireOverdue peStore peWaiters pePhases now cutoff (map oiInvoiceId (selfExpiring <> cancelled))
unless (null expired) $ logInfo ("badge poller: expired " <> tshow (length expired) <> " invoice(s) past their window")
cleanupDue :: PollerEnv -> UTCTime -> IO Bool
cleanupDue PollerEnv {peCleanedAt} now = atomically $ do
due <- maybe True (\at -> diffUTCTime now at >= cleanupInterval) <$> readTVar peCleanedAt
when due $ writeTVar peCleanedAt (Just now)
pure due
-- | The poller reads an invoice only within settleWindow of its creation, so each order's invoice is read once more
-- before the order goes: a payment whose webhook was lost is recorded then, and the order is kept for support.
-- Each pass reads at most cleanupReadsPerPass of the day's orders.
cleanupUnpaid :: PollerEnv -> UTCTime -> IO ()
cleanupUnpaid env@PollerEnv {peStore, peCleanupLeft} now = do
due <- cleanupDue env now
when due $ withTransaction peStore (`unpaidCleanupCandidates` addUTCTime (negate unpaidRetention) now) >>= atomically . writeTVar peCleanupLeft
candidates <- atomically $ stateTVar peCleanupLeft (splitAt cleanupReadsPerPass)
deleted <- filterM (\c@(RegistrationId rid, _) -> safelyWith ("names cleanup: registration " <> rid) False (cleanOne c)) candidates
unless (null deleted) $ logInfo ("names cleanup: deleted " <> tshow (length deleted) <> " unpaid order(s)")
where
cleanOne (rid@(RegistrationId ridText), invId@(InvoiceId invText)) =
getInvoice peStore invId >>= \case
Nothing -> pure False
Just InvoiceRow {irProvider, irProviderRef} -> case find ((== irProvider) . pProvider) (peProviders env) of
Nothing -> keep ("no " <> tshow irProvider <> " provider is configured to read invoice " <> invText)
Just p ->
pReadInvoice p irProviderRef >>= \case
Left (ProviderError e) -> keep ("reading invoice " <> invText <> " failed: " <> e)
Right signal_ -> do
mapM_ (\signal -> settleMoved env irProvider now (irProviderRef, signal)) signal_
deleted <- withTransaction peStore (`deleteUnpaidRegistration` rid)
unless deleted $ logError ("names cleanup: unpaid registration " <> ridText <> " is kept because invoice " <> invText <> " received a payment; refund it")
pure deleted
where
keep why = False <$ logWarn ("names cleanup: unpaid registration " <> ridText <> " is kept until the next run, " <> why)
-- | Cancel the order at the provider first, so the buyer can no longer pay it.
closedAtProvider :: PollerEnv -> UTCTime -> OverdueInvoice -> IO Bool
closedAtProvider env@PollerEnv {peProviders} now oi@OverdueInvoice {oiProvider, oiProviderRef = ref} =
safelyWith ("cancelling " <> tshow oiProvider <> " invoice " <> ref) False $
safelyWith ("badge poller: cancelling " <> tshow oiProvider <> " invoice " <> ref) False $
case find ((== oiProvider) . pProvider) peProviders of
Nothing -> uncancelled env now oi "no provider is configured for it"
Just p ->
@@ -379,19 +436,7 @@ drainHints :: PollerEnv -> IO ()
drainHints env@PollerEnv {peHints = ReadHints q _} = serveHints env (isEmptyTBQueue q >>= check)
passSafely :: PollerEnv -> IO ()
passSafely env = void $ safely "the pass" (runOnePass env)
passSafely env = void $ safely "badge poller: the pass" (runOnePass env)
hintSafely :: PollerEnv -> UTCTime -> Text -> IO ()
hintSafely env now ref = void $ safely ("the hinted read of " <> ref) (readHint env now ref)
safely :: Text -> IO () -> IO Bool
safely what action = safelyWith what False (True <$ action)
-- | Asynchronous exceptions are rethrown, since that is how the race stops this thread.
safelyWith :: Text -> a -> IO a -> IO a
safelyWith what fallback action =
try action >>= \case
Right a -> pure a
Left (e :: SomeException) -> case fromException e :: Maybe SomeAsyncException of
Just _ -> throwIO e
Nothing -> fallback <$ logError ("badge poller: " <> what <> " failed; the next pass will run: " <> tshow e)
hintSafely env now ref = void $ safely ("badge poller: the hinted read of " <> ref) (readHint env now ref)
@@ -40,12 +40,12 @@ import StoreService.Codes (issueFailedText, issueOneCode, singleUse)
import StoreService.Config (AppleStoreConfig (..), BadgeIssuerKey (..), ChainConfig (..), NamesConfig (..), PlayStoreConfig (..), ServiceConfig (..), minNameYears, readServiceConfig)
import StoreService.Group (GroupEvent, ensureManagedGroup, groupEvent, hasTracker, refreshTracker, revokeWithTracker, runGroupLane)
import StoreService.Group.Command (badgeTypeP, codeP, maxMonths, maxNameYears, nameCodeP, prepaidCodeP, textTokenP)
import StoreService.Log (logError, logInfo, logWarn)
import StoreService.Names.Codes (issueNameCode, revokeNameCode)
import StoreService.Names.Env (NamesEnv (..), newNamesEnv)
import StoreService.Names.Turns (Turns, newTurns)
import StoreService.Options
import StoreService.Poller (newPollerEnv, newReadHints, runPoller)
import StoreService.Providers (Provider)
import StoreService.Providers.BTCPay (btcpayProvider)
import StoreService.Providers.Stripe (stripeProvider)
import StoreService.Store
@@ -60,6 +60,7 @@ import StoreService.Web.Http (Bucket, takeFrom)
import StoreService.Web.Server (checkSiteDirs, exportWebapp, newWebEnv, runWebListeners)
import Control.Applicative (optional)
import Control.Concurrent.STM
import StoreService.Log (logError, logInfo, logWarn)
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import qualified Data.Aeson as J
@@ -218,7 +219,7 @@ storeService opts@StoreServiceOpts {serviceConfigFile} cfg env = do
| isJust groupCfg -> forM_ (groupEvent ev) $ atomically . writeTQueue (groupEventQ env)
_ -> pure ()
startLanes cc = do
lanes <- maybe (pure []) (serviceLanes waiters cc) serviceCfg
lanes <- maybe (pure []) (serviceLanes waiters turns cc) serviceCfg
-- The group is resolved before the other lanes start, so a failed lookup exits at once rather than waiting for them to end.
groupLane_ <- forM groupCfg $ \gc -> runGroupLane cc turns namesOn (groupEventQ env) <$> ensureManagedGroup cc gc
raceAny_ $ processQueuedRequests key trackerQ_ requestEnv : maybeToList groupLane_ <> lanes
@@ -231,27 +232,29 @@ storeService opts@StoreServiceOpts {serviceConfigFile} cfg env = do
-- The reader runs from the start and must not block, since outputQ carries every chat event and a full queue stalls the core.
simplexChatCore cfg {chatHooks} (mkChatOpts opts) $ \_ cc -> raceAny_ [readEvents cc, startLanes cc]
where
serviceLanes :: Waiters -> ChatController -> ServiceConfig -> IO [IO ()]
serviceLanes ws cc@ChatController {chatStore} sc = do
serviceLanes :: Waiters -> Turns -> ChatController -> ServiceConfig -> IO [IO ()]
serviceLanes ws turns cc@ChatController {chatStore} sc = do
-- Seed before the listener accepts anything, since every checkout is priced from these.
seedServiceCatalog chatStore
btc <- maybe (pure []) (fmap (: []) . btcpayProvider) (btcpay sc)
str <- maybe (pure []) (fmap (: []) . stripeProvider) (stripe sc)
let providers = btc <> str
hints <- newReadHints
namesEnv <- forM (names sc) (startNames cc)
namesEnv <- forM (names sc) (startNames cc turns ws providers)
webEnv <- newWebEnv chatStore sc ws hints providers namesEnv
exportWebapp sc
pollerEnv <- newPollerEnv chatStore ws hints providers (poll sc)
pollerEnv <- newPollerEnv chatStore ws (nePhases <$> namesEnv) hints providers (poll sc)
pure [runWebListeners webEnv, runPoller pollerEnv]
startNames :: ChatController -> NamesConfig -> IO NamesEnv
startNames cc nc@NamesConfig {nTLD, nChain = ChainConfig {cChainId}} =
startNames :: ChatController -> Turns -> Waiters -> [Provider] -> NamesConfig -> IO NamesEnv
startNames cc@ChatController {chatStore} turns waiters providers nc@NamesConfig {nTLD, nCodesOnly, nChain = ChainConfig {cChainId}} =
readTVarIO (currentUser cc) >>= \case
Nothing -> putStrLn "No current user" >> exitFailure
Just User {userId} -> do
logInfo $ "names: on, TLD " <> safeDecodeUtf8 (strEncode nTLD) <> ", chain " <> tshow cChainId
newNamesEnv cc userId nc
logInfo $ "names: on, TLD " <> safeDecodeUtf8 (strEncode nTLD) <> ", chain " <> tshow cChainId <> codesOnlyNote
newNamesEnv cc userId nc chatStore turns waiters providers
where
codesOnlyNote = if nCodesOnly then ", codes only: registration needs a prepaid code" else ""
seedServiceCatalog :: DBStore -> IO ()
seedServiceCatalog st = do
@@ -7,12 +7,16 @@
module StoreService.Store.Invoices
( InvoiceRow (..),
InvoiceProduct (..),
InvoicePayment (..),
paymentHolds,
NewInvoice (..),
NewNameInvoice (..),
CreateError (..),
StoreDecodeError (..),
ProviderRefConflict (..),
createInvoiceRows,
insertNameInvoice,
executeChanging,
getInvoice,
getInvoiceByProviderRef,
@@ -20,8 +24,9 @@ module StoreService.Store.Invoices
codeHashExists,
OverdueInvoice (..),
overdueInvoices,
expireOverdue,
cancelOpenInvoice,
expireOverdueInvoice,
lockInvoice,
unfundedQuery,
newInvoiceId,
readCatalogRows,
seedCatalog,
@@ -40,7 +45,6 @@ where
import Control.Exception (Exception)
import qualified Control.Exception as E
import Control.Monad (filterM)
import Crypto.Random (getRandomBytes)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64.URL as B64U
@@ -60,6 +64,7 @@ import Simplex.Messaging.Agent.Store.Common (DBStore, withConnection, withTransa
import qualified Simplex.Messaging.Agent.Store.DB as DB
import Simplex.Messaging.Encoding.String (textDecode, textEncode)
import Simplex.Messaging.Util (safeDecodeUtf8, tshow)
import StoreService.Names.Phases (RegistrationId (..))
#if defined(dbPostgres)
import StoreService.Store.Postgres.Migrations (servicePrefix, withPrefix)
@@ -76,8 +81,7 @@ data InvoiceRow = InvoiceRow
{ irInvoiceId :: InvoiceId,
irProvider :: PaymentProvider,
irProviderRef :: Text,
irBadgeType :: BadgeType,
irMonths :: Word8,
irProduct :: InvoiceProduct,
irPrice :: CurrencyAmount,
irAmount :: CurrencyAmount,
irCurrency :: Text,
@@ -89,6 +93,10 @@ data InvoiceRow = InvoiceRow
}
deriving (Eq, Show)
-- | A badge's type and months, or the name registration the invoice pays for.
data InvoiceProduct = IPBadge BadgeType Word8 | IPName RegistrationId
deriving (Eq, Show)
paymentHolds :: InvoicePayment -> Bool
paymentHolds InvoicePayment {ipAmount, ipCryptoPaid, ipPaidInFull} =
maybe False (\(CurrencyAmount a) -> a > 0) ipAmount || ipCryptoPaid /= Nothing || ipPaidInFull
@@ -120,6 +128,17 @@ data NewInvoice = NewInvoice
niCreatedAt :: UTCTime
}
-- | The invoice row of a name order; the registration links it, with its provider ref, in the same transaction.
data NewNameInvoice = NewNameInvoice
{ nniInvoiceId :: InvoiceId,
nniProvider :: PaymentProvider,
nniAmount :: CurrencyAmount,
nniCurrency :: Text,
nniDestination :: ServicePaymentDestination,
nniExpiresAt :: UTCTime,
nniCreatedAt :: UTCTime
}
data CreateError = CECodeConflict | CERefConflict | CEOther Text
deriving (Eq, Show)
@@ -128,6 +147,12 @@ newtype StoreDecodeError = StoreDecodeError Text
instance Exception StoreDecodeError
-- | A provider ref held by both a badge invoice and a name invoice, so a payment to it cannot be credited to either.
newtype ProviderRefConflict = ProviderRefConflict Text
deriving (Eq, Show)
instance Exception ProviderRefConflict
-- | SQLite stores timestamps as text, so `expires_at < ?` compares as strings and only sorts chronologically when every value has the same width.
truncateToSecond :: UTCTime -> UTCTime
truncateToSecond = posixSecondsToUTCTime . fromInteger . truncate . utcTimeToPOSIXSeconds
@@ -163,45 +188,72 @@ qInsertBadgeCode =
"INSERT INTO @badge_codes (code_hash, badge_type, months, code_payment_status, created_at) "
<> "VALUES (?,?,?,?,?)"
-- | Every invoice has a badge code row or a name registration row, never both.
productJoins :: Text
productJoins =
"LEFT JOIN @badge_code_invoices ci ON ci.invoice_id = i.invoice_id "
<> "LEFT JOIN @badge_codes bc ON bc.badge_code_id = ci.badge_code_id "
<> "LEFT JOIN @name_invoices ni ON ni.invoice_id = i.invoice_id "
providerRefColumn :: Text
providerRefColumn = "COALESCE(ci.provider_ref, ni.provider_ref)"
invoiceRowSelect :: Text
invoiceRowSelect =
"SELECT i.invoice_id, i.provider, ci.provider_ref, bc.badge_type, bc.months, "
"SELECT i.invoice_id, i.provider, "
<> providerRefColumn
<> ", bc.badge_type, bc.months, ni.registration_id, "
<> "i.price, i.amount, i.currency, i.payment_url, i.payment_address, i.payment_crypto_currency, "
<> "i.payment_crypto_amount, i.expires_at, i.status, i.created_at, "
<> "p.amount, p.crypto_paid, p.crypto_due, p.paid_in_full, p.status, p.updated_at "
<> "FROM @invoices i "
<> "JOIN @badge_code_invoices ci ON ci.invoice_id = i.invoice_id "
<> "JOIN @badge_codes bc ON bc.badge_code_id = ci.badge_code_id "
<> productJoins
<> "LEFT JOIN @payments p ON p.invoice_id = i.invoice_id "
qGetInvoice :: Query
qGetInvoice = mkQuery (invoiceRowSelect <> "WHERE i.invoice_id = ?")
qGetInvoiceByProviderRef :: Query
qGetInvoiceByProviderRef = mkQuery (invoiceRowSelect <> "WHERE ci.provider_ref = ?")
qGetInvoiceByProviderRef =
mkQuery $
invoiceRowSelect
<> "WHERE i.invoice_id IN (SELECT invoice_id FROM @badge_code_invoices WHERE provider_ref = ? "
<> "UNION ALL SELECT invoice_id FROM @name_invoices WHERE provider_ref = ?)"
qUnpaidRefs :: Query
qUnpaidRefs =
mkQuery $
"SELECT i.provider, ci.provider_ref FROM @invoices i "
<> "JOIN @badge_code_invoices ci ON ci.invoice_id = i.invoice_id "
"SELECT i.provider, "
<> providerRefColumn
<> " FROM @invoices i "
<> productJoins
<> "WHERE i.status <> 'paid' AND i.created_at >= ? "
<> "ORDER BY i.created_at"
-- created_at is whole seconds; the id breaks ties the same way on SQLite and Postgres
<> "ORDER BY i.created_at, i.invoice_id"
qCodeHashExists :: Query
qCodeHashExists = mkQuery "SELECT 1 FROM @badge_codes WHERE code_hash = ? LIMIT 1"
unfundedOnly :: Text
unfundedOnly =
-- | No payment holding money was read for the invoice whose id is in this column, as paymentHolds reads one.
-- The sweep and the overdue close of a name order must agree on it, so both use this.
unfundedBy :: Text -> Text
unfundedBy invoiceId =
"AND NOT EXISTS (SELECT 1 FROM @payments p "
<> "WHERE p.invoice_id = @invoices.invoice_id AND ("
<> ("WHERE p.invoice_id = " <> invoiceId <> " AND (")
<> "COALESCE(p.amount, 0) > 0 OR p.crypto_paid IS NOT NULL OR p.paid_in_full = 1)) "
unfundedQuery :: Text -> Query
unfundedQuery = mkQuery . (" " <>) . unfundedBy
unfundedOnly :: Text
unfundedOnly = unfundedBy "@invoices.invoice_id"
qOverdueInvoices :: Query
qOverdueInvoices =
mkQuery $
"SELECT @invoices.invoice_id, @invoices.provider, ci.provider_ref, @invoices.created_at FROM @invoices "
<> "JOIN @badge_code_invoices ci ON ci.invoice_id = @invoices.invoice_id "
"SELECT @invoices.invoice_id, @invoices.provider, COALESCE(ci.provider_ref, ni.provider_ref), @invoices.created_at FROM @invoices "
<> "LEFT JOIN @badge_code_invoices ci ON ci.invoice_id = @invoices.invoice_id "
<> "LEFT JOIN @name_invoices ni ON ni.invoice_id = @invoices.invoice_id "
<> "WHERE status = 'open' AND expires_at < ? "
<> unfundedOnly
<> "ORDER BY @invoices.created_at"
@@ -212,7 +264,6 @@ qExpireOverdue =
"UPDATE @invoices SET status = 'expired', updated_at = ? "
<> "WHERE status = 'open' AND expires_at < ? AND invoice_id = ? "
<> unfundedOnly
<> "AND EXISTS (SELECT 1 FROM @badge_code_invoices ci WHERE ci.invoice_id = @invoices.invoice_id)"
qCodeHashForInvoice :: Query
qCodeHashForInvoice =
@@ -255,6 +306,9 @@ qUpsertPayment =
where
alreadySettled = "@payments.status = '" <> paymentStatusText PSSettled <> "'"
qLockInvoice :: Query
qLockInvoice = mkQuery "UPDATE @invoices SET updated_at = ? WHERE invoice_id = ?"
qUpdateInvoiceStatus :: Query
qUpdateInvoiceStatus =
mkQuery "UPDATE @invoices SET status = ?, updated_at = ? WHERE invoice_id = ? AND status = ?"
@@ -348,19 +402,23 @@ note :: Text -> Maybe a -> Either Text a
note ctx = maybe (Left ctx) Right
type InvoiceCols =
(Text, Text, Text, BadgeType, Word32)
(Text, Text, Text, Maybe BadgeType, Maybe Word32, Maybe Text)
:. (Word32, Word32, Text, Maybe Text, Maybe Text, Maybe Text)
:. (Maybe Text, UTCTime, Text, UTCTime)
:. (Maybe Word32, Maybe Text, Maybe Text, Maybe Int, Maybe Text, Maybe UTCTime)
mkInvoiceRow :: InvoiceCols -> Either Text InvoiceRow
mkInvoiceRow
( (invId, providerTxt, providerRef, badgeType, months)
( (invId, providerTxt, providerRef, badgeType, months, registrationId)
:. (price, amount, currency, url, addr, cryptoCur)
:. (cryptoAmt, expiresAt, statusTxt, createdAt)
:. (pAmount, pCryptoPaid, pCryptoDue, pPaidInFull, pStatus, pUpdatedAt)
) = do
provider <- note "invoices.provider" (textDecode providerTxt)
invoiceProduct <- case (badgeType, months, registrationId) of
(Just bt, Just m, Nothing) -> Right (IPBadge bt (fromIntegral m))
(Nothing, Nothing, Just rid) -> Right (IPName (RegistrationId rid))
_ -> Left ("invoice product: " <> invId)
status <- note "invoices.status" (textToInvoiceStatus statusTxt)
destination <- note "invoice payment destination" (mkDestination url addr cryptoCur cryptoAmt)
pure
@@ -368,8 +426,7 @@ mkInvoiceRow
{ irInvoiceId = InvoiceId invId,
irProvider = provider,
irProviderRef = providerRef,
irBadgeType = badgeType,
irMonths = fromIntegral months,
irProduct = invoiceProduct,
irPrice = CurrencyAmount price,
irAmount = CurrencyAmount amount,
irCurrency = currency,
@@ -389,7 +446,10 @@ selectInvoiceRow db q param = do
rows <- DB.query db q (Only param)
case rows of
[] -> pure Nothing
(row : _) -> either (E.throwIO . StoreDecodeError) (pure . Just) (mkInvoiceRow row)
(row : _) -> Just <$> decodeInvoiceRow row
decodeInvoiceRow :: InvoiceCols -> IO InvoiceRow
decodeInvoiceRow = either (E.throwIO . StoreDecodeError) pure . mkInvoiceRow
type BadgePriceCols = (Text, BadgeType, Word32, Text, Text, UTCTime)
@@ -432,19 +492,11 @@ insertInvoiceRows db NewInvoice {..} = do
let InvoiceId invId = niInvoiceId
CurrencyAmount price = niPrice
CurrencyAmount amount = niAmount
discountAmount = price - amount
(url, addr, cryptoCur, cryptoAmt) = destinationCols niDestination
expiresAt = truncateToSecond niExpiresAt
createdAt = truncateToSecond niCreatedAt
BadgePriceId priceId = niPriceId
offerId = (\(BadgeOfferId o) -> o) <$> niOfferId
months = fromIntegral niMonths :: Word32
DB.execute
db
qInsertInvoice
( (invId, textEncode niProvider, price, discountAmount, Nothing :: Maybe Word32, amount, niCurrency)
:. (url, addr, cryptoCur, cryptoAmt, expiresAt, invoiceStatusText ISOpen, createdAt, createdAt)
)
insertInvoiceRow db niInvoiceId niProvider price amount niCurrency niDestination niExpiresAt createdAt
DB.execute
db
qInsertBadgeCode
@@ -455,6 +507,25 @@ insertInvoiceRows db NewInvoice {..} = do
qInsertBadgeCodeInvoice
(invId, badgeCodeId, priceId, offerId, niProviderRef, createdAt)
-- | A name is sold at its chain price, with no discount.
insertNameInvoice :: DB.Connection -> NewNameInvoice -> IO ()
insertNameInvoice db NewNameInvoice {..} =
insertInvoiceRow db nniInvoiceId nniProvider amount amount nniCurrency nniDestination nniExpiresAt nniCreatedAt
where
CurrencyAmount amount = nniAmount
insertInvoiceRow :: DB.Connection -> InvoiceId -> PaymentProvider -> Word32 -> Word32 -> Text -> ServicePaymentDestination -> UTCTime -> UTCTime -> IO ()
insertInvoiceRow db (InvoiceId invId) provider price amount currency destination expiresAt' createdAt' =
DB.execute
db
qInsertInvoice
( (invId, textEncode provider, price, price - amount, Nothing :: Maybe Word32, amount, currency)
:. (url, addr, cryptoCur, cryptoAmt, truncateToSecond expiresAt', invoiceStatusText ISOpen, createdAt, createdAt)
)
where
(url, addr, cryptoCur, cryptoAmt) = destinationCols destination
createdAt = truncateToSecond createdAt'
#if defined(dbPostgres)
classifyCreateError :: DB.SQLError -> CreateError
classifyCreateError e = case constraintViolation e of
@@ -483,8 +554,13 @@ classifyCreateError e
getInvoice :: DBStore -> InvoiceId -> IO (Maybe InvoiceRow)
getInvoice st (InvoiceId invId) = withConnection st $ \db -> selectInvoiceRow db qGetInvoice invId
-- | Throws ProviderRefConflict for a ref both a badge invoice and a name invoice hold.
getInvoiceByProviderRef :: DBStore -> Text -> IO (Maybe InvoiceRow)
getInvoiceByProviderRef st ref = withConnection st $ \db -> selectInvoiceRow db qGetInvoiceByProviderRef ref
getInvoiceByProviderRef st ref = withConnection st $ \db ->
DB.query db qGetInvoiceByProviderRef (ref, ref) >>= \case
[] -> pure Nothing
[row] -> Just <$> decodeInvoiceRow row
_ -> E.throwIO (ProviderRefConflict ref)
unpaidRefs :: DBStore -> UTCTime -> IO [(Text, Text)]
unpaidRefs st since = withConnection st $ \db -> DB.query db qUnpaidRefs (Only since)
@@ -510,14 +586,11 @@ overdueInvoices st cutoff = withConnection st $ \db -> do
Just oiProvider -> Right OverdueInvoice {oiInvoiceId = InvoiceId i, oiProvider, oiProviderRef, oiCreatedAt}
Nothing -> Left ("invoices.provider: " <> providerTxt)
-- | Of the given invoices, expire those that are still open, have received no money, and are past their deadline.
expireOverdue :: DBStore -> UTCTime -> [InvoiceId] -> IO [InvoiceId]
expireOverdue st cutoff' invIds = withTransaction st $ \db -> do
let cutoff = truncateToSecond cutoff'
filterM (\(InvoiceId i) -> (> 0) <$> executeChanging db qExpireOverdue (cutoff, cutoff, i)) invIds
cancelOpenInvoice :: DBStore -> InvoiceId -> UTCTime -> IO Bool
cancelOpenInvoice st invId at = withTransaction st $ \db -> updateInvoiceStatus db invId ISOpen ISExpired at
-- | Expires the invoice if it is still open, has received no money, and is past the cutoff.
expireOverdueInvoice :: DB.Connection -> UTCTime -> InvoiceId -> IO Bool
expireOverdueInvoice db cutoff' (InvoiceId i) = (> 0) <$> executeChanging db qExpireOverdue (cutoff, cutoff, i)
where
cutoff = truncateToSecond cutoff'
readCatalogRows :: DBStore -> IO ([BadgePrice], [BadgeOffer])
readCatalogRows st = withConnection st $ \db -> do
@@ -578,6 +651,10 @@ upsertPayment db InvoiceRow {irInvoiceId, irProvider, irProviderRef, irCurrency}
InvoiceId invId = irInvoiceId
at = truncateToSecond at'
-- | Takes the invoice row's lock for the rest of the transaction; SQLite's transactions hold the whole database already.
lockInvoice :: DB.Connection -> InvoiceId -> UTCTime -> IO ()
lockInvoice db (InvoiceId invId) at = DB.execute db qLockInvoice (truncateToSecond at, invId)
updateInvoiceStatus :: DB.Connection -> InvoiceId -> InvoiceStatus -> InvoiceStatus -> UTCTime -> IO Bool
updateInvoiceStatus db (InvoiceId invId) observed new at =
(> 0) <$> executeChanging db qUpdateInvoiceStatus (invoiceStatusText new, truncateToSecond at, invId, invoiceStatusText observed)
@@ -18,6 +18,7 @@ module StoreService.Web.Http
createLimit,
searchLimit,
previewLimit,
codeLimit,
holdMicros,
jsonResponse,
jsonError,
@@ -32,19 +33,30 @@ module StoreService.Web.Http
internalError,
readBoundedBody,
maxBodyBytes,
nameBodyBytes,
parseCodeHash,
invoicePairs,
invoiceAt,
expiryMoment,
destinationPairs,
amountJSON,
confirmationsFor,
invoiceWindow,
providerFor,
providerNamed,
)
where
import Control.Concurrent.STM
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import Data.Aeson.Types (Pair)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64.URL as B64U
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as LB
import Data.List (sortOn)
import Data.List (find, sortOn)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
@@ -57,8 +69,11 @@ import Network.HTTP.Types (Header, Method, Status, hCacheControl, hContentType,
import Network.Socket (SockAddr (..), hostAddress6ToTuple, hostAddressToTuple)
import Network.Wai
import Numeric (showHex)
import Simplex.Chat.PaymentService.Types (CardProvider (..), CurrencyAmount (..), InvoiceStatus (..), PaymentProvider (..), ServicePaymentDestination (..), ServicePaymentMethod (..))
import Simplex.Messaging.Util (safeDecodeUtf8)
import StoreService.Config (ListenerConfig (..))
import StoreService.Config (BTCPayConfig (..), ListenerConfig (..), ServiceConfig (..), SpeedPolicy (..), StripeConfig (..), defaultExpiryMinutes, defaultSessionMinutes)
import StoreService.Providers (Provider (..))
import StoreService.Store.Invoices (InvoicePayment (..), InvoiceRow (..), cryptoCurrencyText, invoiceStatusText, paymentHolds)
import Text.Read (readMaybe)
data HttpEnv = HttpEnv
@@ -77,15 +92,20 @@ type Respond = Response -> IO ResponseReceived
holdMicros :: Int
holdMicros = 30 * 1000000
readLimit, createLimit, searchLimit, previewLimit :: Limit
readLimit, createLimit, searchLimit, previewLimit, codeLimit :: Limit
readLimit = Limit {lmName = "read", lmPerMinute = 60}
createLimit = Limit {lmName = "create", lmPerMinute = 5}
searchLimit = Limit {lmName = "search", lmPerMinute = 30}
previewLimit = Limit {lmName = "preview", lmPerMinute = 20}
codeLimit = Limit {lmName = "code", lmPerMinute = 10}
maxBodyBytes :: Int
maxBodyBytes = 8192
-- | The cap of the names endpoints whose bodies carry a sealed name code.
nameBodyBytes :: Int
nameBodyBytes = 16384
sha256Bytes :: Int
sha256Bytes = 32
@@ -278,3 +298,82 @@ parseCodeHash t
_ -> Nothing
where
canonical = T.filter (/= '=') . safeDecodeUtf8 . B64U.encode
-- | An open name invoice past its expiry with no money received reads expired before the sweep marks it.
invoiceAt :: UTCTime -> InvoiceRow -> InvoiceRow
invoiceAt now row = case expiryMoment row of
Just expiry | now > expiry -> row {irStatus = ISExpired}
_ -> row
-- | A funded open invoice stays open until it settles.
expiryMoment :: InvoiceRow -> Maybe UTCTime
expiryMoment InvoiceRow {irStatus, irExpiresAt, irPayment}
| irStatus == ISOpen && not (maybe False paymentHolds irPayment) = Just irExpiresAt
| otherwise = Nothing
-- | The fields every invoice view has, a badge's and a name's.
invoicePairs :: Maybe Int -> InvoiceRow -> [Pair]
invoicePairs confirmations InvoiceRow {irAmount, irCurrency, irDestination, irExpiresAt, irStatus, irPayment} =
[ "status" .= invoiceStatusText irStatus,
"amount" .= amountJSON irAmount,
"currency" .= irCurrency,
"expiresAt" .= irExpiresAt
]
<> destinationPairs confirmations irDestination
<> maybe [] paymentPairs irPayment
where
paymentPairs p =
concat
[ maybe [] (\a -> ["amountPaid" .= amountJSON a]) (ipAmount p),
maybe [] (\a -> ["cryptoAmountPaid" .= a]) (ipCryptoPaid p),
maybe [] (\a -> ["cryptoAmountDue" .= a]) (ipCryptoDue p),
["paidInFull" .= ipPaidInFull p],
["settledAt" .= ipUpdatedAt p | irStatus == ISPaid]
]
destinationPairs :: Maybe Int -> ServicePaymentDestination -> [Pair]
destinationPairs confirmations = \case
SPDCard _ url -> ["clientSecret" .= url]
SPDCrypto currency address cryptoAmount ->
[ "address" .= address,
"cryptoAmount" .= cryptoAmount,
"cryptoCurrency" .= cryptoCurrencyText currency
]
<> maybe [] (\n -> ["requiredConfirmations" .= n]) confirmations
amountJSON :: CurrencyAmount -> J.Value
amountJSON (CurrencyAmount a) = J.toJSON a
-- | Greenfield reports no confirmation count, so this comes from the store's speed policy.
confirmationsFor :: ServiceConfig -> Maybe Int
confirmationsFor cfg = speedPolicyConfirmations . bSpeedPolicy <$> btcpay cfg
-- | BTCPay's own store setting, whose numbering is not in speed order.
speedPolicyConfirmations :: SpeedPolicy -> Int
speedPolicyConfirmations = \case
HighSpeed -> 0
MediumSpeed -> 1
LowMediumSpeed -> 2
LowSpeed -> 6
secondsPerMinute :: Int
secondsPerMinute = 60
-- | The expiry shown and stored must come from the same key the provider sets the invoice's real expiry from.
invoiceWindow :: ServiceConfig -> ServicePaymentMethod -> NominalDiffTime
invoiceWindow cfg = \case
SPMCard CPStripe -> minutes (maybe defaultSessionMinutes sSessionMinutes (stripe cfg))
SPMCrypto _ -> minutes (maybe defaultExpiryMinutes bExpiryMinutes (btcpay cfg))
where
minutes n = fromIntegral (secondsPerMinute * n)
providerFor :: [Provider] -> ServicePaymentMethod -> Maybe Provider
providerFor providers method = providerNamed providers (providerOf method)
providerNamed :: [Provider] -> PaymentProvider -> Maybe Provider
providerNamed providers provider = find ((== provider) . pProvider) providers
providerOf :: ServicePaymentMethod -> PaymentProvider
providerOf = \case
SPMCard CPStripe -> PPStripe
SPMCrypto _ -> PPCrypto
@@ -8,7 +8,11 @@ import Data.Text (Text)
import Network.HTTP.Types (Method)
import Network.Wai (Request, ResponseReceived, pathInfo)
import StoreService.Names.Env (NamesEnv (..))
import StoreService.Web.Http (HttpEnv, Limit, Respond, limited, only, previewLimit, searchLimit)
import StoreService.Names.Orders (ordersEnv)
import StoreService.Web.Http (HttpEnv, Limit, Respond, codeLimit, holdMicros, limited, only, previewLimit, readLimit, searchLimit)
import StoreService.Web.Names.Codes (codeCheckHandler)
import StoreService.Web.Names.Orders (codeOrderHandler)
import StoreService.Web.Names.Registration (registrationHandler)
import StoreService.Web.Names.Search (previewHandler, searchHandler)
data Route = Route
@@ -18,9 +22,13 @@ data Route = Route
}
namesRoutes :: NamesEnv -> [Text] -> Maybe Route
namesRoutes NamesEnv {neConfig, neContracts, neProfiles} = \case
namesRoutes env@NamesEnv {neConfig, neStore, neContracts, neProfiles, neWaiters, neProviders} = \case
["api", "name", "register"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeOrderHandler (ordersEnv env) neProviders neWaiters}
["api", "code", "check"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeCheckHandler neStore}
["api", "link", "preview"] -> Just Route {rMethod = "POST", rLimit = previewLimit, rHandler = previewHandler neProfiles}
["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler neConfig neContracts neProfiles hash}
-- below every fixed /api/name/<word> route, which it would otherwise match
["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler neConfig neStore neContracts neProfiles hash}
["api", "registration", rid] -> Just Route {rMethod = "GET", rLimit = readLimit, rHandler = registrationHandler (ordersEnv env) holdMicros rid}
_ -> Nothing
-- | Nothing for a path no route knows. The limit is taken only once the route and the method match.
@@ -0,0 +1,47 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module StoreService.Web.Names.Codes (codeCheckHandler, codeRefusalResponse) where
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import qualified Data.Aeson.KeyMap as KM
import qualified Data.ByteString.Lazy as LB
import Data.Text (Text)
import Data.Time.Clock (UTCTime, getCurrentTime)
import Network.HTTP.Types (status200, status400, status409)
import Network.Wai (Request, Response, ResponseReceived)
import Simplex.Chat.Badges.Code (NameCode (..), parseNameCode)
import Simplex.Messaging.Agent.Store.Common (DBStore)
import StoreService.Names.Codes (CodeRefusal (..), codeRefusalTag, codeState, codeStateText, readPrepaidCode)
import StoreService.Names.Store (PrepaidCode (..))
import StoreService.Web.Http (Respond, badRequest, jsonError, jsonResponse, maxBodyBytes, readBoundedBody)
-- | It names no registration, even for a reserved code: holding a code proves nothing about an order.
codeCheckHandler :: DBStore -> Request -> Respond -> IO ResponseReceived
codeCheckHandler st req respond =
readBoundedBody maxBodyBytes req >>= \case
Just body | Just text <- codeField body -> case parseNameCode text of
Just (NCPrepaid code) -> do
now <- getCurrentTime
readPrepaidCode st code >>= respond . maybe (codeRefusalResponse CRInvalid) (codeAnswer now)
_ -> respond $ codeRefusalResponse CRInvalid
_ -> respond badRequest
codeField :: LB.ByteString -> Maybe Text
codeField body = case J.decode body of
Just (J.Object o) | Just (J.String t) <- KM.lookup "code" o -> Just t
_ -> Nothing
codeAnswer :: UTCTime -> PrepaidCode -> Response
codeAnswer now code@PrepaidCode {pcMinLength, pcYears} =
jsonResponse status200 $ J.object ["minLength" .= pcMinLength, "years" .= pcYears, "state" .= codeStateText (codeState now code)]
-- | Not 404 for an unknown code, which the page reads as not_found whatever the body.
codeRefusalResponse :: CodeRefusal -> Response
codeRefusalResponse refusal = jsonError status (codeRefusalTag refusal)
where
status = case refusal of
CRInvalid -> status400
_ -> status409
@@ -0,0 +1,240 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module StoreService.Web.Names.Orders
( Answer (..),
answerNow,
sendAnswer,
firstRefusal,
nameInvoiceHandler,
codeOrderHandler,
nameInvoiceView,
orderRefusalResponse,
)
where
import Control.Concurrent.STM (atomically)
import qualified Control.Exception as E
import Control.Monad (forM_, unless)
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import qualified Data.Aeson.KeyMap as KM
import Data.Bifunctor (first)
import Data.Maybe (isJust)
import Data.Text (Text)
import Data.Time.Clock (addUTCTime)
import Data.Word (Word32)
import Network.HTTP.Types (status200, status403, status409, status503)
import Network.Wai (Request, Response, ResponseReceived)
import Simplex.Chat.Badges.Code (NameCode (..), parseNameCode)
import Simplex.Chat.PaymentService.Types (CurrencyAmount (..), InvoiceId (..), InvoiceStatus (..), ServicePaymentMethod)
import Simplex.Messaging.Agent.Store.Common (withTransaction)
import Simplex.Messaging.Util (tshow)
import StoreService.Catalog (catalogCurrency)
import StoreService.Config (NamesConfig (..), ServiceConfig)
import StoreService.Log (logError, logInfo, logWarn)
import StoreService.Names.Abi (hex)
import StoreService.Names.Codes (CodeRefusal (..), readPrepaidCode)
import StoreService.Names.Orders
import StoreService.Names.Phases (RegistrationId (..))
import StoreService.Names.Signatures (OrderFields (..))
import StoreService.Names.Store (NameRegistration (..), NewRegistration (..), OrderTerms (..), PaidOrder (..), PrepaidCode (..), RegistrationPayment (..), createPaidRegistration, getRegistration, newRegistrationId, orderHash, textMethod)
import StoreService.Names.Turns (withLabelTurn)
import StoreService.Names.View (registrationAt, registrationView)
import StoreService.Orders (publishRegistration)
import StoreService.Providers (OrderDraft (..), Provider (..), ProviderError (..), ProviderInvoice (..), expiresItself)
import StoreService.Store.Invoices (InvoiceRow (..), NewNameInvoice (..), getInvoice, insertNameInvoice, newInvoiceId, truncateToSecond)
import StoreService.Waiters (Waiters, publish)
import StoreService.Web.Http (Respond, badRequest, confirmationsFor, internalError, invalidName, invoiceAt, invoicePairs, invoiceWindow, jsonError, jsonResponse, nameBodyBytes, providerFor, providerNamed, providerUnavailable, readBoundedBody)
import StoreService.Web.Names.Codes (codeRefusalResponse)
data InvoiceOrder = InvoiceOrder
{ ioRequest :: OrderRequest,
ioMethod :: ServicePaymentMethod,
ioPrice :: Word32
}
parseInvoiceOrder :: J.Object -> Either OrderRefusal InvoiceOrder
parseInvoiceOrder o = do
order@InvoiceOrder {ioRequest} <- maybe (Left ORBadRequest) Right $ do
ioRequest <- parseOrderRequest o
ioMethod <- case KM.lookup "method" o of
Just (J.String t) -> textMethod t
_ -> Nothing
ioPrice <- integerField =<< KM.lookup "price" o
pure InvoiceOrder {ioRequest, ioMethod, ioPrice}
order <$ checkLength (orFields ioRequest)
-- | An order's answer and the work its commit leaves, both done after the name's turn, so a slow client holds no turn.
data Answer = Answer {aResponse :: Response, aAfter :: IO ()}
answerNow :: Response -> Answer
answerNow aResponse = Answer {aResponse, aAfter = pure ()}
sendAnswer :: Respond -> Answer -> IO ResponseReceived
sendAnswer respond Answer {aResponse, aAfter} = aAfter >> respond aResponse
-- | POST /api/invoice for a name, its body already read and its kind checked.
nameInvoiceHandler :: OrdersEnv -> ServiceConfig -> [Provider] -> Waiters -> J.Object -> Respond -> IO ResponseReceived
nameInvoiceHandler env@OrdersEnv {oeStore, oeTurns, oeNow} cfg providers waiters body respond
| nCodesOnly (oeConfig env) = sendAnswer respond =<< refuse ORCodesOnly
| otherwise = sendAnswer respond =<< case parseInvoiceOrder body of
Left refusal -> refuse refusal
Right order@InvoiceOrder {ioRequest = req@OrderRequest {orOwner, orFields}, ioMethod, ioPrice} ->
checkOrderSigner req >>= \case
Left refusal -> refuse refusal
Right () -> withLabelTurn oeTurns (ofLabelHash orFields) $ do
now <- oeNow
let hash = orderHash orOwner orFields (OTInvoice ioMethod $ fromIntegral ioPrice)
withTransaction oeStore (\db -> existingOrder db req hash now) >>= \case
EOResent r -> answerResend now r
EOMeets -> refuse OROrderExists
EONew _ -> newOrder order
where
refuse refusal = do
logInfo ("POST /api/invoice (name) refused: " <> tshow refusal)
pure $ answerNow $ orderRefusalResponse refusal
answerResend now r@NameRegistration {nrPayment} = case nrPayment of
RPInvoice _ invId -> getInvoice oeStore invId >>= maybe (refuse OROrderExists) (fmap answerNow . viewOf (registrationAt now r) . invoiceAt now)
RPCode _ -> refuse OROrderExists
viewOf r row = jsonResponse status200 <$> nameInvoiceView env (confirmationsFor cfg) row r
newOrder order@InvoiceOrder {ioRequest = OrderRequest {orFields = OrderFields {ofLabelHash, ofLength, ofDuration}, orTimestamp}, ioMethod, ioPrice} = do
now <- oeNow
let years = nYears $ oeConfig env
checks =
[ pure $ checkTimestamp now orTimestamp,
pure $ checkDuration (oeConfig env) now years ofDuration,
checkNameFree (oeContracts env) oeStore ofLabelHash,
checkPrice (oeContracts env) ofLength years (fromIntegral ioPrice)
]
firstRefusal checks >>= \case
Just refusal -> refuse refusal
Nothing -> case providerFor providers ioMethod of
Nothing -> do
logInfo ("POST /api/invoice (name) refused: no provider configured for " <> tshow ioMethod)
pure $ answerNow providerUnavailable
Just provider -> createAtProvider order provider
createAtProvider InvoiceOrder {ioRequest = req@OrderRequest {orOwner, orFields}, ioMethod, ioPrice} provider = do
invId <- newInvoiceId
now <- truncateToSecond <$> oeNow
let amount = CurrencyAmount ioPrice
pCreateInvoice provider ioMethod OrderDraft {odAmount = amount, odCurrency = catalogCurrency, odReference = "name:" <> hex (orderHash orOwner orFields (OTInvoice ioMethod $ fromIntegral ioPrice))} >>= \case
Left (ProviderError e) -> do
logError ("POST /api/invoice (name) failed: provider refused to create an invoice: " <> e)
pure $ answerNow providerUnavailable
Right ProviderInvoice {piProviderRef, piDestination} -> do
rid <- newRegistrationId
let invoice = NewNameInvoice {nniInvoiceId = invId, nniProvider = pProvider provider, nniAmount = amount, nniCurrency = catalogCurrency, nniDestination = piDestination, nniExpiresAt = addUTCTime (invoiceWindow cfg ioMethod) now, nniCreatedAt = now}
registration = NewRegistration {nrgId = rid, nrgOwner = orOwner, nrgOrder = orFields, nrgYears = nYears $ oeConfig env}
paid = PaidOrder {poMethod = ioMethod, poPrice = fromIntegral ioPrice, poInvoiceId = invId, poProviderRef = piProviderRef}
placeOrder env req (\db at -> insertNameInvoice db invoice >> first (const OROrderExists) <$> createPaidRegistration db registration paid at)
`E.catch` strandedBy provider piProviderRef
>>= \case
Left refusal -> do
logWarn ("POST /api/invoice (name): the order lost to another, so invoice " <> piProviderRef <> " is cancelled at the provider")
(\a -> a {aAfter = cancelAtProvider provider piProviderRef}) <$> refuse refusal
Right closed -> do
row_ <- getInvoice oeStore invId
registration_ <- withTransaction oeStore (`getRegistration` rid)
response <- case (row_, registration_) of
(Just row, Just r) -> viewOf r row
_ -> internalError <$ logError ("POST /api/invoice (name): order " <> invText invId <> " is not readable after its commit")
pure Answer {aResponse = response, aAfter = announceClosed env providers waiters closed}
-- An invoice created at the provider for an order that was not written would stay payable with nothing watching it.
strandedBy :: Provider -> Text -> E.SomeException -> IO a
strandedBy provider ref e = do
unless (isAsync e) $ do
logError ("POST /api/invoice (name): the order was not written, so invoice " <> ref <> " is cancelled at the provider: " <> tshow e)
cancelAtProvider provider ref
E.throwIO e
isAsync e = isJust (E.fromException e :: Maybe E.SomeAsyncException)
-- | POST /api/name/register, an order paid with a prepaid code, in the contract's order of checks.
codeOrderHandler :: OrdersEnv -> [Provider] -> Waiters -> Request -> Respond -> IO ResponseReceived
codeOrderHandler env@OrdersEnv {oeStore, oeConfig, oeContracts, oeTurns, oeNow} providers waiters req respond =
readBoundedBody nameBodyBytes req >>= ordered >>= sendAnswer respond
where
ordered = \case
Just body | Just (J.Object o) <- J.decode body -> either refuse withCode (parseCodeOrder o)
_ -> refuse ORBadRequest
refuse refusal = do
logInfo ("POST /api/name/register refused: " <> tshow refusal)
pure $ answerNow $ orderRefusalResponse refusal
withCode (order, codeText) =
issuedCode codeText >>= \case
Nothing -> refuse $ ORCode CRInvalid
Just code -> checkOrderSigner order >>= either refuse (const $ signed order code)
issuedCode t = case parseNameCode t of
Just (NCPrepaid code) -> readPrepaidCode oeStore code
_ -> pure Nothing
signed order@OrderRequest {orOwner, orFields} code@PrepaidCode {pcCodeHash} = withLabelTurn oeTurns (ofLabelHash orFields) $ do
now <- oeNow
withTransaction oeStore (\db -> existingOrder db order (orderHash orOwner orFields (OTCode pcCodeHash)) now) >>= \case
EOResent NameRegistration {nrId} -> pure $ answerNow $ registrationAnswer nrId
EOMeets -> refuse OROrderExists
EONew _ -> newOrder now order code
newOrder now order@OrderRequest {orFields = OrderFields {ofLabelHash, ofDuration}, orTimestamp} code@PrepaidCode {pcYears} = do
let checks =
[ pure $ checkTimestamp now orTimestamp,
pure $ checkDuration oeConfig now pcYears ofDuration,
checkNameFree oeContracts oeStore ofLabelHash
]
firstRefusal checks >>= \case
Just refusal -> refuse refusal
Nothing -> do
rid <- newRegistrationId
placeCodeOrder env order rid code >>= \case
Left refusal -> refuse refusal
Right closed -> pure Answer {aResponse = registrationAnswer rid, aAfter = announceClosed env providers waiters closed}
registrationAnswer (RegistrationId rid) = jsonResponse status200 $ J.object ["registrationId" .= rid]
parseCodeOrder :: J.Object -> Either OrderRefusal (OrderRequest, Text)
parseCodeOrder o = do
order@(req, _) <- maybe (Left ORBadRequest) Right $ (,) <$> parseOrderRequest o <*> textField o "code"
order <$ checkLength (orFields req)
-- | After the commit, the closed orders' invoices and versions are published, and their invoices cancelled at the provider,
-- best effort, as the poller's sweep would cancel them.
announceClosed :: OrdersEnv -> [Provider] -> Waiters -> [ClosedOrder] -> IO ()
announceClosed OrdersEnv {oeStore, oePhases} providers waiters closed = do
atomically $ forM_ closed $ \ClosedOrder {coRegistration = NameRegistration {nrId, nrPayment}, coVersion} -> do
forM_ (paidInvoice nrPayment) $ \invId -> publish waiters invId ISExpired
publishRegistration (Just oePhases) (nrId, coVersion)
forM_ closed $ \ClosedOrder {coRegistration = NameRegistration {nrPayment}} ->
forM_ (paidInvoice nrPayment) $ \invId ->
getInvoice oeStore invId >>= mapM_ (\InvoiceRow {irProvider, irProviderRef} -> forM_ (providerNamed providers irProvider) $ \p -> unless (expiresItself irProvider) $ cancelAtProvider p irProviderRef)
where
paidInvoice = \case
RPInvoice _ invId -> Just invId
RPCode _ -> Nothing
cancelAtProvider :: Provider -> Text -> IO ()
cancelAtProvider Provider {pCancelInvoice} ref =
pCancelInvoice ref >>= \case
Left (ProviderError e) -> logWarn ("names: cancelling invoice " <> ref <> " at the provider failed: " <> e)
Right () -> pure ()
invText :: InvoiceId -> Text
invText (InvoiceId t) = t
firstRefusal :: [IO (Either OrderRefusal ())] -> IO (Maybe OrderRefusal)
firstRefusal = \case
[] -> pure Nothing
check : rest -> check >>= either (pure . Just) (const $ firstRefusal rest)
nameInvoiceView :: OrdersEnv -> Maybe Int -> InvoiceRow -> NameRegistration -> IO J.Value
nameInvoiceView OrdersEnv {oeStore, oeConfig, oeContracts} confirmations row@InvoiceRow {irInvoiceId = InvoiceId invId} r = do
view <- registrationView oeStore oeConfig oeContracts r
pure $ J.object $ ["invoiceId" .= invId, "kind" .= ("name" :: Text), "registration" .= view] <> invoicePairs confirmations row
orderRefusalResponse :: OrderRefusal -> Response
orderRefusalResponse = \case
ORBadRequest -> badRequest
ORInvalidName -> invalidName
OROrderExists -> jsonError status409 "order_exists"
ORNameTaken -> jsonError status409 "name_taken"
ORNameCheckUnavailable -> jsonError status503 "name_check_unavailable"
ORCatalogChanged price -> jsonResponse status409 $ J.object ["error" .= ("catalog_changed" :: Text), "price" .= price]
ORCode refusal -> codeRefusalResponse refusal
ORCodesOnly -> jsonError status403 "codes_only"
@@ -0,0 +1,77 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module StoreService.Web.Names.Registration
( registrationHandler,
registrationChangedResponse,
registrationRefusalResponse,
heldFor,
versionParam,
)
where
import Control.Monad (join)
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import qualified Data.ByteString.Char8 as B
import Data.Char (isDigit)
import Data.Int (Int64)
import Data.Maybe (maybeToList)
import Data.Text (Text)
import Data.Time.Clock (UTCTime, diffUTCTime)
import Network.HTTP.Types (status200, status409)
import Network.Wai (Request, Response, ResponseReceived, queryString)
import Simplex.Messaging.Agent.Store.Common (withTransaction)
import StoreService.Names.Orders (OrdersEnv (..))
import StoreService.Names.Phases (RegistrationId (..), awaitChange)
import StoreService.Names.Store (NameRegistration (..), getRegistration)
import StoreService.Names.View (lapseMoment, registrationAt, registrationVersion, registrationView)
import StoreService.Web.Http (Respond, jsonResponse, notFound)
import Text.Read (readMaybe)
-- | GET /api/registration/:id, held while its version is seenVersion.
registrationHandler :: OrdersEnv -> Int -> Text -> Request -> Respond -> IO ResponseReceived
registrationHandler OrdersEnv {oeStore, oeConfig, oeContracts, oePhases, oeNow} hold ridText req respond =
readNow >>= \case
Nothing -> respond notFound
Just (now, r@NameRegistration {nrVersion})
| versionParam req == Just nrVersion -> do
_ <- awaitChange oePhases rid (registrationVersion oeStore rid) nrVersion (heldFor hold now $ maybeToList $ lapseMoment r)
readNow >>= maybe (respond notFound) answer
| otherwise -> answer (now, r)
where
rid = RegistrationId ridText
readNow = oeNow >>= \now -> fmap (now,) <$> withTransaction oeStore (`getRegistration` rid)
answer (now, r) = respond . jsonResponse status200 =<< registrationView oeStore oeConfig oeContracts (registrationAt now r)
-- | The registration's current view, for a request that lost the race to change it.
registrationChangedResponse :: OrdersEnv -> RegistrationId -> IO Response
registrationChangedResponse env = registrationRefusalResponse env "registration_changed"
-- | A refusal with the registration's current view, so the page follows its state.
registrationRefusalResponse :: OrdersEnv -> Text -> RegistrationId -> IO Response
registrationRefusalResponse env tag rid = maybe notFound refusal <$> currentView env rid
where
refusal view = jsonResponse status409 $ J.object ["error" .= tag, "registration" .= view]
-- | The registration's view as it reads now.
currentView :: OrdersEnv -> RegistrationId -> IO (Maybe J.Value)
currentView OrdersEnv {oeStore, oeConfig, oeContracts, oeNow} rid =
withTransaction oeStore (`getRegistration` rid) >>= traverse (\r -> oeNow >>= \now -> registrationView oeStore oeConfig oeContracts (registrationAt now r))
-- | The hold, cut to end just past the next moment at which the answer changes with time alone.
heldFor :: Int -> UTCTime -> [UTCTime] -> Int
heldFor hold now moments = fromInteger $ minimum $ toInteger hold : [ceiling (diffUTCTime moment now * microsPerSecond) + 1 | moment <- moments, moment >= now]
where
microsPerSecond = 1000000
-- | Nothing unless seenVersion is a decimal version, so any other value answers at once.
versionParam :: Request -> Maybe Int64
versionParam req = case join $ lookup "seenVersion" $ queryString req of
Just s | not (B.null s) && B.all isDigit s -> readMaybe (B.unpack s) >>= inRange
_ -> Nothing
where
inRange :: Integer -> Maybe Int64
inRange v = if v <= toInteger (maxBound :: Int64) then Just (fromInteger v) else Nothing
@@ -20,6 +20,7 @@ import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Network.HTTP.Types (status200)
import Network.Wai (Request, Response, ResponseReceived, queryString)
import Simplex.Chat.Badges.Code (maxNameLength, minNameLength)
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import Simplex.Messaging.SimplexName (LabelHash (..))
import Simplex.Messaging.Util (tshow)
import StoreService.Config (ChainConfig (..), NamesConfig (..))
@@ -28,6 +29,7 @@ import StoreService.Names.Abi (bytes32FromHex)
import StoreService.Names.Contracts
import StoreService.Names.Links (LinkKind (..), linkKindOf)
import StoreService.Names.Profiles (Entity, Profiles, linkEntity, linkPreview)
import StoreService.Names.Store (isLabelRegistering)
import StoreService.Names.View (addressText, tldText)
import StoreService.Web.Http (Respond, badRequest, invalidLink, invalidName, jsonResponse, maxBodyBytes, providerUnavailable, readBoundedBody)
import System.Timeout (timeout)
@@ -35,24 +37,29 @@ import Text.Read (readMaybe)
data Found
= FAvailable Int
| -- | the chain says available while a registration here has its register sent, not mined
FRegistering
| FReserved
| FRegistered RegisteredName (Maybe NameRecords)
-- | The chain reads of one search share one deadline, so a node that answers slowly fails the search once.
searchHandler :: NamesConfig -> Contracts -> Profiles -> Text -> Request -> Respond -> IO ResponseReceived
searchHandler cfg@NamesConfig {nYears} ct ps hashText req respond = case LabelHash <$> bytes32FromHex hashText of
searchHandler :: NamesConfig -> DBStore -> Contracts -> Profiles -> Text -> Request -> Respond -> IO ResponseReceived
searchHandler cfg@NamesConfig {nYears} st ct ps hashText req respond = case LabelHash <$> bytes32FromHex hashText of
Nothing -> respond badRequest
Just lh -> case queryLength req of
Nothing -> respond invalidName
Just len ->
timeout (ctDeadline ct) (runExceptT $ found lh len) >>= \case
Just len -> do
registering <- withTransaction st (`isLabelRegistering` lh)
timeout (ctDeadline ct) (runExceptT $ found lh len registering) >>= \case
Just (Right f) -> respond =<< searchAnswer cfg ps f
Just (Left e) -> unavailable $ tshow e
Nothing -> unavailable "timed out"
where
found lh len =
found lh len registering =
ExceptT (nameStatus ct lh) >>= \case
NSAvailable -> FAvailable <$> ExceptT (chainPriceCents ct len nYears)
NSAvailable
| registering -> pure FRegistering
| otherwise -> FAvailable <$> ExceptT (chainPriceCents ct len nYears)
NSReserved -> pure FReserved
NSRegistered name -> FRegistered name <$> ExceptT (nameRecords ct lh name)
unavailable :: Text -> IO ResponseReceived
@@ -76,17 +83,18 @@ searchAnswer NamesConfig {nTLD, nYears, nTermStart, nChain = ChainConfig {cChain
["status" .= ("available" :: Text), "price" .= price, "currency" .= ("usd" :: Text), "years" .= nYears, "termStart" .= termStart]
<> ["resolver" .= addressText cResolver, "sponsor" .= addressText cSponsor]
<> chain
FRegistering -> ok ["status" .= ("registered" :: Text), "entities" .= ([] :: [Entity]), "tld" .= tldText nTLD]
FReserved -> ok ["status" .= ("reserved" :: Text)]
FRegistered RegisteredName {rnOwner, rnResolver, rnExpiresAt, rnGracePeriod} records_ -> do
entities <- maybe (pure []) (recordEntities ps) records_
ok $
["status" .= ("registered" :: Text), "entities" .= entities, "records" .= (recordsJSON <$> records_)]
<> ["owner" .= addressText rnOwner, "resolver" .= addressText rnResolver]
<> ["owner" .= addressText rnOwner, "resolver" .= addressText rnResolver, "recordsNonce" .= (rdNonce <$> records_)]
<> ["expiresAt" .= rnExpiresAt, "gracePeriod" .= rnGracePeriod]
<> chain
where
ok :: [JT.Pair] -> IO Response
ok fields = pure $ jsonResponse status200 $ J.object fields
ok = pure . jsonResponse status200 . J.object
chain = ["chainId" .= cChainId, "tld" .= tldText nTLD]
termStart = floor (utcTimeToPOSIXSeconds nTermStart) :: Integer
recordsJSON NameRecords {rdContact, rdChannel} = J.object ["contact" .= rdContact, "channel" .= rdChannel]
@@ -17,41 +17,49 @@ module StoreService.Web.Server
where
import StoreService.Catalog (PricedOffer (..), priceOffer)
import StoreService.Config (BTCPayConfig (..), ListenerConfig (..), ServiceConfig (..), SpeedPolicy (..), StripeConfig (..), defaultExpiryMinutes, defaultSessionMinutes)
import StoreService.Config (ListenerConfig (..), NamesConfig (..), ServiceConfig (..), StripeConfig (..))
import StoreService.Names.Abi (hex)
import StoreService.Names.Env (NamesEnv)
import StoreService.Names.Orders (OrdersEnv (..), ordersEnv)
import StoreService.Names.Phases (RegistrationId, awaitChange)
import StoreService.Names.Store (NamePhase (..), NameRegistration (..), getRegistration, textMethod)
import StoreService.Names.View (lapseMoment, registrationAt, registrationVersion)
import StoreService.Orders (RegistrationChanged (..), cancelOpenInvoice)
import StoreService.Poller (ReadHints, queueReadHint)
import StoreService.Providers (OrderDraft (..), Provider (..), ProviderError (..), ProviderInvoice (..), WebhookError (..))
import StoreService.Store.Invoices (CreateError (..), InvoicePayment (..), InvoiceRow (..), NewInvoice (..), cancelOpenInvoice, codeHashExists, createInvoiceRows, cryptoCurrencyText, getInvoice, getInvoiceByProviderRef, invoiceStatusText, newInvoiceId, paymentHolds, readCatalogRows, textToInvoiceStatus, truncateToSecond)
import StoreService.Waiters (Seen, Waiters, awaitStatus, publish)
import StoreService.Store.Invoices (CreateError (..), InvoicePayment (..), InvoiceProduct (..), InvoiceRow (..), NewInvoice (..), codeHashExists, createInvoiceRows, getInvoice, getInvoiceByProviderRef, newInvoiceId, paymentHolds, readCatalogRows, textToInvoiceStatus, truncateToSecond)
import StoreService.Waiters (Seen, Waiters, awaitStatus)
import StoreService.Web.Http
import StoreService.Web.Names (namesApp, namesRoutes)
import Control.Concurrent.STM
import StoreService.Web.Names.Orders (nameInvoiceHandler, nameInvoiceView)
import StoreService.Web.Names.Registration (heldFor, registrationChangedResponse, versionParam)
import Control.Concurrent.Async (race)
import qualified Control.Exception as E
import StoreService.Log (logError, logInfo, logWarn)
import Control.Monad (filterM, forM_, when)
import Control.Monad (filterM, forM_, void, when)
import Data.Aeson ((.=))
import qualified Data.Aeson as J
import qualified Data.Aeson.KeyMap as KM
import Data.Aeson.Types (Pair)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LB
import Data.Char (toLower)
import Data.Functor ((<&>))
import Data.List (find, isPrefixOf)
import Data.Maybe (fromMaybe, isJust)
import Data.List (isPrefixOf)
import Data.Maybe (catMaybes, fromMaybe, isJust)
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8', encodeUtf8)
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, getCurrentTime)
import Data.Time.Clock (UTCTime, addUTCTime, getCurrentTime)
import Data.Word (Word8)
import Network.HTTP.Types (Header, Status, hCacheControl, hContentType, status200, status400, status409, status413)
import Network.Wai
import qualified Network.Wai.Handler.Warp as Warp
import Simplex.Chat.Badges (BadgeType)
import Simplex.Chat.Badges.Types (BadgeOfferId (..), BadgePriceId (..))
import Simplex.Chat.PaymentService.Types (CardProvider (..), CryptoCurrency (..), CurrencyAmount (..), InvoiceId (..), InvoiceStatus (..), PaymentProvider (..), ServicePaymentDestination (..), ServicePaymentMethod (..))
import Simplex.Messaging.Agent.Store.Common (DBStore)
import Simplex.Chat.PaymentService.Types (InvoiceId (..), InvoiceStatus (..), PaymentProvider (..), ServicePaymentDestination (..), ServicePaymentMethod (..))
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import Simplex.Messaging.Encoding.String (textEncode)
import Simplex.Messaging.Util (raceAny_, tshow)
import System.Directory (canonicalizePath, copyFile, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, listDirectory, removePathForcibly)
@@ -70,7 +78,9 @@ data WebEnv = WebEnv
-- | @Nothing@ unless the names site is served on @names_port@.
weNamesSite :: Maybe Site,
-- | @Nothing@ when the ini has no @[names]@ section.
weNames :: Maybe NamesEnv
weNames :: Maybe NamesEnv,
-- | The name orders' part of weNames, which tests set without a chat controller.
weOrders :: Maybe OrdersEnv
}
-- | A built site, served when @serve_webapp@ is on.
@@ -83,12 +93,12 @@ data Site = Site
newWebEnv :: DBStore -> ServiceConfig -> Waiters -> ReadHints -> [Provider] -> Maybe NamesEnv -> IO WebEnv
newWebEnv weStore weConfig weWaiters weHints weProviders weNames = do
weHttp <- newHttpEnv lc
weBadgesSite <- if lServeWebapp then Just <$> loadSite lBadgesStaticDir else pure Nothing
weNamesSite <- mapM loadSite (lNamesPort *> lNamesStaticDir)
pure WebEnv {weStore, weConfig, weWaiters, weProviders, weHoldMicros = holdMicros, weHints, weHttp, weBadgesSite, weNamesSite, weNames}
weBadgesSite <- if lServeWebapp then Just <$> loadSite (keyFills weConfig) lBadgesStaticDir else pure Nothing
weNamesSite <- mapM (loadSite (namesFills weConfig)) (lNamesPort *> lNamesStaticDir)
pure WebEnv {weStore, weConfig, weWaiters, weProviders, weHoldMicros = holdMicros, weHints, weHttp, weBadgesSite, weNamesSite, weNames, weOrders = ordersEnv <$> weNames}
where
lc@ListenerConfig {lServeWebapp, lBadgesStaticDir, lNamesStaticDir, lNamesPort} = listener weConfig
loadSite dir = Site dir <$> prepareShell dir (keyFills weConfig)
loadSite fills dir = Site dir <$> prepareShell dir fills
listenerConfig :: WebEnv -> ListenerConfig
listenerConfig WebEnv {weConfig} = listener weConfig
@@ -155,6 +165,14 @@ fillMeta (name, shipped, served) = T.replace (meta shipped) (meta served)
keyFills :: ServiceConfig -> [MetaFill]
keyFills ServiceConfig {stripe} = [("stripe-publishable-key", "", sPublishableKey) | Just StripeConfig {sPublishableKey} <- [stripe]]
-- | The badges site's shell never carries the names mode.
namesFills :: ServiceConfig -> [MetaFill]
namesFills cfg@ServiceConfig {names} = keyFills cfg <> [codesMode | Just NamesConfig {nCodesOnly = True} <- [names]]
-- | The page offers only prepaid codes when it reads this mode.
codesMode :: MetaFill
codesMode = ("simplex-names-mode", "open", "codes")
-- | @Nothing@ (serve the pristine file) when there is nothing to fill, no shell is present, or it cannot be read.
prepareShell :: FilePath -> [MetaFill] -> IO (Maybe LB.ByteString)
prepareShell _ [] = pure Nothing
@@ -182,14 +200,14 @@ prepareShell dir fills =
exportWebapp :: ServiceConfig -> IO ()
exportWebapp cfg = do
forM_ lBadgesExportDir $ exportSite lBadgesStaticDir
forM_ ((,) <$> lNamesStaticDir <*> lNamesExportDir) $ uncurry exportSite
forM_ lBadgesExportDir $ exportSite (keyFills cfg) lBadgesStaticDir
forM_ ((,) <$> lNamesStaticDir <*> lNamesExportDir) $ uncurry (exportSite (namesFills cfg))
where
ListenerConfig {lBadgesStaticDir, lBadgesExportDir, lNamesStaticDir, lNamesExportDir} = listener cfg
exportSite dir out = do
exportSite fills dir out = do
emptyDir out
copyTree dir out
prepareShell dir (keyFills cfg) >>= mapM_ (LB.writeFile (out </> shellFile))
prepareShell dir fills >>= mapM_ (LB.writeFile (out </> shellFile))
logInfo ("store service: exported " <> T.pack dir <> " to " <> T.pack out)
-- | Contents only: the directory itself may be a bind or volume mount point that cannot be removed.
@@ -251,26 +269,82 @@ readInvoiceHandler :: WebEnv -> InvoiceId -> Request -> Respond -> IO ResponseRe
readInvoiceHandler env@WebEnv {weStore} invId req respond =
getInvoice weStore invId >>= \case
Nothing -> respond notFound
Just row@InvoiceRow {irProduct = IPName rid} | Just orders <- weOrders env -> readNameInvoice env orders row rid req respond
Just row -> case holdFor row of
Nothing -> respond (jsonResponse status200 (invoiceView (confirmationsFor env) row))
Nothing -> respondInvoice env row respond
Just seen -> do
-- Settlement publishes after it commits, so the row read below is at least as new as whatever woke us.
_ <- awaitStatus (weWaiters env) invId readSeen seen (weHoldMicros env)
getInvoice weStore invId >>= \case
Nothing -> respond notFound
Just row' -> respond (jsonResponse status200 (invoiceView (confirmationsFor env) row'))
_ <- awaitStatus (weWaiters env) invId (readSeen env invId (pure id)) seen (weHoldMicros env)
respondCurrent env invId respond
where
holdFor :: InvoiceRow -> Maybe Seen
holdFor row@InvoiceRow {irStatus} = case waitParam req of
Just seen | seen == irStatus, seen /= ISPaid, samePayment -> Just (irStatus, paymentMark row)
Just seen | seen == irStatus, seen /= ISPaid, samePayment req row -> Just (seenOf row)
_ -> Nothing
where
-- The counter a hold watches starts at zero, so a payment recorded before this request arrived cannot wake it.
samePayment = case paidParam req of
Nothing -> True
Just seen -> seen == paymentMark row
readSeen :: IO Seen
readSeen = maybe (ISOpen, ("", False)) (\row -> (irStatus row, paymentMark row)) <$> getInvoice weStore invId
-- | A name invoice also holds while its registration's version is seenVersion, a paid one on that alone,
-- except one paid after its order closed, which has nothing more to wait for.
readNameInvoice :: WebEnv -> OrdersEnv -> InvoiceRow -> RegistrationId -> Request -> Respond -> IO ResponseReceived
readNameInvoice env@WebEnv {weConfig, weWaiters, weHoldMicros} orders@OrdersEnv {oeStore, oeNow, oePhases} stored@InvoiceRow {irInvoiceId = invId} rid req respond =
readName orders stored rid >>= \case
Nothing -> respond notFound
Just (now, row, r) -> case waitParam req of
Just wait | holds wait row r -> hold now row r >> respondCurrent env invId respond
_ -> respond . jsonResponse status200 =<< nameInvoiceView orders (confirmationsFor weConfig) row r
where
seenVersion = versionParam req
holds wait row@InvoiceRow {irStatus} NameRegistration {nrPhase, nrVersion} =
wait == irStatus
&& not (irStatus == ISPaid && nrPhase == PUnpaid)
&& samePayment req row
&& maybe (irStatus /= ISPaid) (== nrVersion) seenVersion
hold now row r = do
let micros = heldFor weHoldMicros now $ catMaybes [expiryMoment row, lapseMoment r]
onStatus = awaitStatus weWaiters invId (readSeen env invId (invoiceAt <$> oeNow)) (seenOf row) micros
onVersion v = awaitChange oePhases rid (registrationVersion oeStore rid) v micros
case (irStatus row, seenVersion) of
(ISPaid, Just v) -> void $ onVersion v
(_, Just v) -> void $ race onStatus (onVersion v)
(_, Nothing) -> void onStatus
-- | The invoice and its registration as a read now shows them.
readName :: OrdersEnv -> InvoiceRow -> RegistrationId -> IO (Maybe (UTCTime, InvoiceRow, NameRegistration))
readName OrdersEnv {oeStore, oeNow} row rid = do
now <- oeNow
fmap (\r -> (now, invoiceAt now row, registrationAt now r)) <$> withTransaction oeStore (`getRegistration` rid)
-- | The counter a hold watches starts at zero, so a payment recorded before this request arrived cannot wake it.
samePayment :: Request -> InvoiceRow -> Bool
samePayment req row = maybe True (== paymentMark row) (paidParam req)
seenOf :: InvoiceRow -> Seen
seenOf row@InvoiceRow {irStatus} = (irStatus, paymentMark row)
-- | What a hold compares, read as the answer would show it.
readSeen :: WebEnv -> InvoiceId -> IO (InvoiceRow -> InvoiceRow) -> IO Seen
readSeen WebEnv {weStore} invId reading = do
readAs <- reading
maybe (ISOpen, ("", False)) (seenOf . readAs) <$> getInvoice weStore invId
respondCurrent :: WebEnv -> InvoiceId -> Respond -> IO ResponseReceived
respondCurrent env@WebEnv {weStore} invId respond = getInvoice weStore invId >>= maybe (respond notFound) (\row -> respondInvoice env row respond)
respondInvoice :: WebEnv -> InvoiceRow -> Respond -> IO ResponseReceived
respondInvoice env row respond = invoiceViewOf env row >>= respond . either id (jsonResponse status200)
-- | A badge's view, or a name's with its registration.
invoiceViewOf :: WebEnv -> InvoiceRow -> IO (Either Response J.Value)
invoiceViewOf env@WebEnv {weConfig} row@InvoiceRow {irInvoiceId = InvoiceId invId, irProduct} = case irProduct of
IPBadge badgeType months -> pure $ Right $ badgeInvoiceView (confirmationsFor weConfig) badgeType months row
IPName rid -> case weOrders env of
Nothing -> Left providerUnavailable <$ namesOff invId
Just orders ->
readName orders row rid >>= \case
Nothing -> pure $ Left notFound
Just (_, row', r) -> Right <$> nameInvoiceView orders (confirmationsFor weConfig) row' r
namesOff :: Text -> IO ()
namesOff invId = logError ("invoice " <> invId <> " is a name's, and this service sells no names")
-- | Monero reports a payment as confirming while its figures are zero, so the verdict counts as much as the figure.
paymentMark :: InvoiceRow -> (Text, Bool)
@@ -279,12 +353,14 @@ paymentMark InvoiceRow {irPayment} =
-- | The provider is cancelled first, because cancelling our record first would leave an address the buyer can still pay into with nothing watching it.
cancelInvoiceHandler :: WebEnv -> InvoiceId -> Respond -> IO ResponseReceived
cancelInvoiceHandler env@WebEnv {weStore} invId respond =
cancelInvoiceHandler env@WebEnv {weStore, weProviders} invId@(InvoiceId invText) respond =
getInvoice weStore invId >>= \case
Nothing -> respond notFound
Just InvoiceRow {irStatus} | irStatus /= ISOpen -> respond (jsonError status409 "not_open")
Just row | funded row -> respond (jsonError status409 "funded")
Just InvoiceRow {irProvider, irProviderRef} -> case providerNamed env irProvider of
-- refused before the provider's cancel, since the answer could not show the order
Just InvoiceRow {irProduct = IPName _} | Nothing <- weOrders env -> namesOff invText >> respond providerUnavailable
Just InvoiceRow {irProvider, irProviderRef} -> case providerNamed weProviders irProvider of
Nothing -> respond providerUnavailable
Just Provider {pCancelInvoice} ->
pCancelInvoice irProviderRef >>= \case
@@ -296,11 +372,9 @@ cancelInvoiceHandler env@WebEnv {weStore} invId respond =
funded InvoiceRow {irPayment} = maybe False paymentHolds irPayment
finish = do
now <- getCurrentTime
cancelled <- cancelOpenInvoice weStore invId now
when cancelled $ atomically $ publish (weWaiters env) invId ISExpired
getInvoice weStore invId >>= \case
Nothing -> respond notFound
Just row -> respond (jsonResponse status200 (invoiceView (confirmationsFor env) row))
E.try (cancelOpenInvoice weStore (weWaiters env) (oePhases <$> weOrders env) invId now) >>= \case
Left (RegistrationChanged rid) -> maybe (pure providerUnavailable) (`registrationChangedResponse` rid) (weOrders env) >>= respond
Right _ -> respondCurrent env invId respond
paidParam :: Request -> Maybe (Text, Bool)
paidParam req = case lookup "seenPaid" (queryString req) of
@@ -314,53 +388,9 @@ waitParam req = case lookup "wait" (queryString req) of
Just (Just raw) -> either (const Nothing) textToInvoiceStatus (decodeUtf8' raw)
_ -> Nothing
-- | Greenfield reports no confirmation count, so this comes from the store's speed policy.
confirmationsFor :: WebEnv -> Maybe Int
confirmationsFor WebEnv {weConfig} = speedPolicyConfirmations . bSpeedPolicy <$> btcpay weConfig
-- | BTCPay's own store setting, whose numbering is not in speed order.
speedPolicyConfirmations :: SpeedPolicy -> Int
speedPolicyConfirmations = \case
HighSpeed -> 0
MediumSpeed -> 1
LowMediumSpeed -> 2
LowSpeed -> 6
invoiceView :: Maybe Int -> InvoiceRow -> J.Value
invoiceView confirmations InvoiceRow {irBadgeType, irMonths, irAmount, irCurrency, irDestination, irExpiresAt, irStatus, irPayment} =
J.object $
[ "status" .= invoiceStatusText irStatus,
"badgeType" .= (textEncode irBadgeType :: Text),
"months" .= irMonths,
"amount" .= amountJSON irAmount,
"currency" .= irCurrency,
"expiresAt" .= irExpiresAt
]
<> destinationPairs confirmations irDestination
<> paidPairs
where
paidPairs = maybe [] paymentPairs irPayment
paymentPairs p =
concat
[ maybe [] (\a -> ["amountPaid" .= amountJSON a]) (ipAmount p),
maybe [] (\a -> ["cryptoAmountPaid" .= a]) (ipCryptoPaid p),
maybe [] (\a -> ["cryptoAmountDue" .= a]) (ipCryptoDue p),
["paidInFull" .= ipPaidInFull p],
["settledAt" .= ipUpdatedAt p | irStatus == ISPaid]
]
destinationPairs :: Maybe Int -> ServicePaymentDestination -> [Pair]
destinationPairs confirmations = \case
SPDCard _ url -> ["clientSecret" .= url]
SPDCrypto currency address cryptoAmount ->
[ "address" .= address,
"cryptoAmount" .= cryptoAmount,
"cryptoCurrency" .= cryptoCurrencyText currency
]
<> maybe [] (\n -> ["requiredConfirmations" .= n]) confirmations
amountJSON :: CurrencyAmount -> J.Value
amountJSON (CurrencyAmount a) = J.toJSON a
badgeInvoiceView :: Maybe Int -> BadgeType -> Word8 -> InvoiceRow -> J.Value
badgeInvoiceView confirmations badgeType months row =
J.object $ ["badgeType" .= (textEncode badgeType :: Text), "months" .= months] <> invoicePairs confirmations row
data CreateRequest = CreateRequest
{ crPriceId :: BadgePriceId,
@@ -369,25 +399,32 @@ data CreateRequest = CreateRequest
crCodeHash :: ByteString
}
-- | A name order carries its kind and a sealed name code, so it alone may use the larger body cap.
createInvoiceHandler :: WebEnv -> Request -> Respond -> IO ResponseReceived
createInvoiceHandler env@WebEnv {weStore} req respond =
readBoundedBody maxBodyBytes req >>= \case
createInvoiceHandler env@WebEnv {weStore, weConfig, weProviders, weWaiters} req respond =
readBoundedBody nameBodyBytes req >>= \case
Nothing -> refuse "body over the size limit" badRequest
Just body -> case parseCreateRequest body of
Nothing -> refuse "malformed body" badRequest
Just cr@CreateRequest {crPriceId, crOfferId, crMethod, crCodeHash} -> do
(prices, offers) <- readCatalogRows weStore
case priceOffer prices offers crPriceId crOfferId of
Left reason -> refuse (tshow reason) catalogChanged
Right priced ->
codeHashExists weStore crCodeHash >>= \case
True -> refuse "code hash already sold" codeConflict
False -> case providerFor env crMethod of
Nothing -> refuse ("no provider configured for " <> tshow crMethod) providerUnavailable
Just provider -> createAtProvider env provider cr priced respond
Just body -> case J.decode body of
Just (J.Object o) -> case KM.lookup "kind" o of
Nothing
| LB.length body > fromIntegral maxBodyBytes -> refuse "body over the size limit" badRequest
| otherwise -> maybe (refuse "malformed body" badRequest) badgeInvoice (parseCreateRequest o)
Just (J.String "name") | Just orders <- weOrders env -> nameInvoiceHandler orders weConfig weProviders weWaiters o respond
Just kind -> refuse ("no order of kind " <> tshow kind) badRequest
_ -> refuse "malformed body" badRequest
where
refuse :: Text -> Response -> IO ResponseReceived
refuse why response = logInfo ("POST /api/invoice refused: " <> why) >> respond response
badgeInvoice cr@CreateRequest {crPriceId, crOfferId, crMethod, crCodeHash} = do
(prices, offers) <- readCatalogRows weStore
case priceOffer prices offers crPriceId crOfferId of
Left reason -> refuse (tshow reason) catalogChanged
Right priced ->
codeHashExists weStore crCodeHash >>= \case
True -> refuse "code hash already sold" codeConflict
False -> case providerFor weProviders crMethod of
Nothing -> refuse ("no provider configured for " <> tshow crMethod) providerUnavailable
Just provider -> createAtProvider env provider cr priced respond
-- | Once the provider call succeeds an invoice exists at BTCPay, and any path below that does not write our rows leaves it stranded.
createAtProvider :: WebEnv -> Provider -> CreateRequest -> PricedOffer -> Respond -> IO ResponseReceived
@@ -439,54 +476,23 @@ createdInvoice (InvoiceId invId) PricedOffer {poBadgeType, poMonths, poAmount, p
]
<> destinationPairs Nothing destination
secondsPerMinute :: Int
secondsPerMinute = 60
-- | The expiry shown and stored must come from the same key the provider sets the invoice's real expiry from.
invoiceWindow :: ServiceConfig -> ServicePaymentMethod -> NominalDiffTime
invoiceWindow cfg = \case
SPMCard CPStripe -> minutes (maybe defaultSessionMinutes sSessionMinutes (stripe cfg))
SPMCrypto _ -> minutes (maybe defaultExpiryMinutes bExpiryMinutes (btcpay cfg))
parseCreateRequest :: J.Object -> Maybe CreateRequest
parseCreateRequest o = do
crPriceId <- BadgePriceId <$> textField "priceId"
crOfferId <- offerField
crMethod <- textMethod =<< textField "method"
crCodeHash <- parseCodeHash =<< textField "codeHash"
pure CreateRequest {crPriceId, crOfferId, crMethod, crCodeHash}
where
minutes n = fromIntegral (secondsPerMinute * n)
providerFor :: WebEnv -> ServicePaymentMethod -> Maybe Provider
providerFor env method = providerNamed env (providerOf method)
providerNamed :: WebEnv -> PaymentProvider -> Maybe Provider
providerNamed WebEnv {weProviders} provider = find ((== provider) . pProvider) weProviders
providerOf :: ServicePaymentMethod -> PaymentProvider
providerOf = \case
SPMCard CPStripe -> PPStripe
SPMCrypto _ -> PPCrypto
parseCreateRequest :: LB.ByteString -> Maybe CreateRequest
parseCreateRequest body = case J.decode body of
Just (J.Object o) -> do
crPriceId <- BadgePriceId <$> textField o "priceId"
crOfferId <- offerField o
crMethod <- methodFromText =<< textField o "method"
crCodeHash <- parseCodeHash =<< textField o "codeHash"
pure CreateRequest {crPriceId, crOfferId, crMethod, crCodeHash}
_ -> Nothing
where
textField o k = case KM.lookup k o of
textField k = case KM.lookup k o of
Just (J.String t) | not (T.null t) -> Just t
_ -> Nothing
offerField o = case KM.lookup "offerId" o of
offerField = case KM.lookup "offerId" o of
Nothing -> Just Nothing
Just J.Null -> Just Nothing
Just (J.String t) | not (T.null t) -> Just (Just (BadgeOfferId t))
_ -> Nothing
methodFromText :: Text -> Maybe ServicePaymentMethod
methodFromText = \case
"card" -> Just (SPMCard CPStripe)
"btc" -> Just (SPMCrypto CCBtc)
"xmr" -> Just (SPMCrypto CCXmr)
_ -> Nothing
-- | A provider retries a delivery it sees fail, so anything thrown below is caught and answered 200 anyway.
webhookHandler :: WebEnv -> PaymentProvider -> Text -> Request -> Respond -> IO ResponseReceived
webhookHandler env@WebEnv {weStore, weHints} provider route req respond =
@@ -499,7 +505,7 @@ webhookHandler env@WebEnv {weStore, weHints} provider route req respond =
respond webhookOk
where
deliver :: IO ResponseReceived
deliver = case providerNamed env provider of
deliver = case providerNamed (weProviders env) provider of
Nothing -> do
logWarn (route <> ": no " <> tshow provider <> " adapter is configured, so every delivery is refused")
respond webhookRefused
+17
View File
@@ -457,10 +457,14 @@ executable simplex-store-service
StoreService.Names.Codes
StoreService.Names.Contracts
StoreService.Names.Env
StoreService.Names.Label
StoreService.Names.Links
StoreService.Names.Orders
StoreService.Names.Phases
StoreService.Names.Profiles
StoreService.Names.Registration
StoreService.Names.Revert
StoreService.Names.Signatures
StoreService.Names.Store
StoreService.Names.Turns
StoreService.Names.View
@@ -481,6 +485,9 @@ executable simplex-store-service
StoreService.Waiters
StoreService.Web.Http
StoreService.Web.Names
StoreService.Web.Names.Codes
StoreService.Web.Names.Orders
StoreService.Web.Names.Registration
StoreService.Web.Names.Search
StoreService.Web.Server
Paths_simplex_chat
@@ -779,8 +786,12 @@ test-suite simplex-chat-test
Bots.StoreService.NamesContractsTests
Bots.StoreService.NamesLabelTests
Bots.StoreService.NamesLinksTests
Bots.StoreService.NamesOrdersTests
Bots.StoreService.NamesPhasesTests
Bots.StoreService.NamesProfilesTests
Bots.StoreService.NamesSearchTests
Bots.StoreService.NamesSignatureTests
Bots.StoreService.NamesStoreTests
Bots.StoreService.NamesWiringTests
Bots.StoreService.StoreVerifierTests
Bots.StoreService.StripeTests
@@ -811,9 +822,12 @@ test-suite simplex-chat-test
StoreService.Names.Env
StoreService.Names.Label
StoreService.Names.Links
StoreService.Names.Orders
StoreService.Names.Phases
StoreService.Names.Profiles
StoreService.Names.Registration
StoreService.Names.Revert
StoreService.Names.Signatures
StoreService.Names.Store
StoreService.Names.Turns
StoreService.Names.View
@@ -834,6 +848,9 @@ test-suite simplex-chat-test
StoreService.Waiters
StoreService.Web.Http
StoreService.Web.Names
StoreService.Web.Names.Codes
StoreService.Web.Names.Orders
StoreService.Web.Names.Registration
StoreService.Web.Names.Search
StoreService.Web.Server
Paths_simplex_chat
+7 -4
View File
@@ -427,13 +427,16 @@ issueCode cc badgeType months = issueCodeAs cc badgeType months "free"
revokeCodeAs :: HasCallStack => ChatController -> BadgeCode -> IO (Either T.Text T.Text)
revokeCodeAs cc code = revokeRaw cc (formatBadgeCode code)
-- | Left is a refusal or a failure the service answers as a command error.
revokeRaw :: HasCallStack => ChatController -> T.Text -> IO (Either T.Text T.Text)
revokeRaw cc args =
sendChatCmdStr cc ("//revoke " <> T.unpack args) >>= \case
revokeRaw cc args = operatorCmd cc ("//revoke " <> args)
-- | Left is a refusal or a failure the service answers as a command error.
operatorCmd :: HasCallStack => ChatController -> T.Text -> IO (Either T.Text T.Text)
operatorCmd cc cmd =
sendChatCmdStr cc (T.unpack cmd) >>= \case
Right (CRCustomChatResponse _ response) -> pure (Right response)
Left (ChatError (CECommandError e)) -> pure (Left (T.pack e))
r -> error $ "revoke failed: " <> show (() <$ r)
r -> error $ T.unpack cmd <> " failed: " <> show (() <$ r)
issueCodeAs :: HasCallStack => ChatController -> BadgeType -> Int -> String -> IO BadgeCode
issueCodeAs cc badgeType months status =
@@ -13,15 +13,17 @@ module Bots.StoreService.GroupIntegrationTests (badgeGroupIntegrationTests, name
import StoreService.Config (BadgeIssuerKey (..), GroupConfig (..))
import StoreService.Group (GroupAction (..), GroupEvent (..), codeInTracker, ensureManagedGroup)
import StoreService.Group.Command (maxUses)
import StoreService.Names.Codes (issueNameCode, revokeNameCode)
import StoreService.Names.Store (PrepaidCode (..), getPrepaidCode)
import StoreService.Names.Codes (issueNameCode, reserveCode, revokeNameCode)
import StoreService.Names.Phases (RegistrationId (..))
import StoreService.Names.Store (PrepaidCode (..), getPrepaidCode, spendPrepaidCode)
import StoreService.Names.Turns (newTurns, withCodeTurn)
import StoreService.Options (StoreServiceOpts (..))
import StoreService.Service (ServiceState (..), storeService, newServiceState)
import StoreService.Service (ServiceState (..), newServiceState, storeService)
import StoreService.Store (ManagedGroup (..), getManagedGroup)
import Bots.StoreService.BotTests (badgeBotName, badgeCmdUsage, badgeProfile, badgeTypeOf, credentialOf, entryOf, issueCode, issueRaw, mkStoreServiceOpts, newPurchaseKeys, redeemWithQueue, requestObject, revokeRaw, serviceDbPrefix, shouldAnswerError, statementOf, stopStoreService, testIssuerKeyIdx)
import Bots.StoreService.ConfigTests (chainSection, namesSection)
import Bots.StoreService.NamesSearchTests (aliceHex)
import Bots.StoreService.NamesStoreTests (order, ownerA, placeCode, placePaid)
import ChatClient
import ChatTests.DBUtils
import ChatTests.Utils
@@ -32,6 +34,8 @@ import Control.Concurrent.STM (TQueue, atomically, newTQueueIO, orElse, readTMVa
import Control.Exception (IOException, bracket, catch, finally, throwIO, try)
import Control.Monad (forM_, guard, mfilter, replicateM_, unless, void, when)
import qualified Data.Aeson as J
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.Either (isLeft, isRight)
import Data.Int (Int64)
import Data.List (isInfixOf)
@@ -44,7 +48,7 @@ import qualified Data.Text as T
import Data.Text.Encoding (encodeUtf8)
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (NominalDiffTime, UTCTime (..), addUTCTime, getCurrentTime)
import Network.HTTP.Client (defaultManagerSettings, httpLbs, newManager, parseRequest, responseBody, responseStatus)
import Network.HTTP.Client (RequestBody (..), defaultManagerSettings, httpLbs, method, newManager, parseRequest, requestBody, responseBody, responseHeaders, responseStatus)
import Network.HTTP.Types.Status (statusCode)
import Network.Socket (Family (..), SockAddr (..), SocketType (..), close, connect, defaultProtocol, socket, tupleToHostAddress)
import Network.Wai.Handler.Warp (openFreePort)
@@ -134,7 +138,8 @@ badgeGroupIntegrationTests = do
namesBootTests :: SpecWith TestParams
namesBootTests = do
it "boots with [names] and [chain], routing a search, and a path no route knows to 404" testNamesBoot
it "boots codes-only, refusing paid name orders 403 codes_only and answering code orders and code checks, the badges shell as built" testNamesBootCodesOnly
it "boots with [names] and [chain], routing fixed /api/name paths ahead of a search, registrations to GET, a path no route knows and a taken order's code to 404, and code checks and orders under one limit" testNamesBoot
testNamesBoot :: HasCallStack => TestParams -> IO ()
testNamesBoot ps = do
@@ -148,21 +153,58 @@ testNamesBoot ps = do
refused tag = (400, Just $ J.object ["error" J..= (tag :: Text)])
unknown <- request "/api/names/unknown"
statusCode . responseStatus <$> httpLbs unknown mgr `shouldReturn` 404
register <- request "/api/name/register"
answer register {method = "POST", requestBody = RequestBodyLBS "{}"} `shouldReturn` refused "bad_request"
search <- request $ "/api/name/" <> T.unpack aliceHex <> "?length=5"
answer search `shouldReturn` refused "invalid_name"
registration <- request "/api/registration/t2Jk8RmQ4vXw9NcP3yLb6g"
answer registration `shouldReturn` (404, Just $ J.object ["error" J..= ("not_found" :: Text)])
posted <- httpLbs registration {method = "POST"} mgr
(statusCode $ responseStatus posted, lookup "Allow" $ responseHeaders posted) `shouldBe` (405, Just "GET")
coded <- request "/api/registration/t2Jk8RmQ4vXw9NcP3yLb6g/code"
answer coded {method = "POST", requestBody = RequestBodyLBS "{}"} `shouldReturn` (404, Just $ J.object ["error" J..= ("not_found" :: Text)])
-- the register above took the first of this client's 10 code requests a minute, which the code check shares
let codeRequest path = request path >>= \r -> statusCode . responseStatus <$> httpLbs r {method = "POST", requestBody = RequestBodyLBS "{}"} mgr
forM_ (take 9 $ cycle ["/api/code/check", "/api/name/register"]) $ \path -> (path,) <$> codeRequest path `shouldReturn` (path, 400)
forM_ ["/api/code/check", "/api/name/register"] $ \path -> (path,) <$> codeRequest path `shouldReturn` (path, 429)
poll t >>= (`shouldSatisfy` isNothing)
testNamesBootCodesOnly :: HasCallStack => TestParams -> IO ()
testNamesBootCodesOnly ps = do
svc@GroupSvc {gsStaticDir} <- prepareGroupService ps
let shell = "<!doctype html><meta name=\"simplex-names-mode\" content=\"open\">"
B.writeFile (gsStaticDir </> "index.html") shell
(t, cc, _, port) <- startGroupService svc {gsIniSections = "" : codesOnlyNames <> chainSection} Nothing
flip finally (stopGroupService t cc) $ do
pollUntilTrue (listening port)
mgr <- newManager defaultManagerSettings
let posted path body = do
req <- parseRequest $ "http://127.0.0.1:" <> show port <> path
r <- httpLbs req {method = "POST", requestBody = RequestBodyLBS body} mgr
pure (statusCode $ responseStatus r, J.decode $ responseBody r :: Maybe J.Value)
answered tag status = (status, Just $ J.object ["error" J..= (tag :: Text)])
posted "/api/invoice" "{\"kind\":\"name\"}" `shouldReturn` answered "codes_only" 403
posted "/api/name/register" "{}" `shouldReturn` answered "bad_request" 400
posted "/api/code/check" "{}" `shouldReturn` answered "bad_request" 400
badgesShell <- parseRequest ("http://127.0.0.1:" <> show port <> "/") >>= (`httpLbs` mgr)
(statusCode $ responseStatus badgesShell, responseBody badgesShell) `shouldBe` (200, LB.fromStrict shell)
poll t >>= (`shouldSatisfy` isNothing)
-- | [names] with codes_only on, as an investors' launch runs.
codesOnlyNames :: [Text]
codesOnlyNames = namesSection <> ["codes_only = on"]
namesCodesChatTests :: SpecWith TestParams
namesCodesChatTests = describe "StoreService names: prepaid codes" $ do
it "issues prepaid codes on /issue name and /bulk name, answering the usage to years below 2" testGroupNameCodes
it "issues prepaid codes on //issue name and revokes them on //revoke" testOperatorNameCodes
it "revokes a prepaid code in the code's turn" testRevokeTakesCodeTurn
it "issues prepaid codes on //issue name, checks them through the names router and revokes them on //revoke" testOperatorNameCodes
it "revokes a prepaid code in the code's turn, after a reservation holding it" testRevokeTakesCodeTurn
it "revokes a prepaid code on /revoke, answering a repeat as already revoked" testGroupRevokeNameCode
testGroupNameCodes :: HasCallStack => TestParams -> IO ()
testGroupNameCodes ps = do
svc <- prepareGroupService ps
runWithOwner svc {gsIniSections = "" : namesSection <> chainSection} $ \cc _ _ alice -> do
runWithOwner svc {gsIniSections = "" : codesOnlyNames <> chainSection} $ \cc _ _ alice -> do
one <- prepaidIn . T.stripPrefix "Code: " =<< replyTo cc alice "/issue name 8 years 5"
namePrepaidCodeLength one `shouldBe` 8
prepaidTerms cc one `shouldReturn` Just (8, 5, CPSFree)
@@ -175,7 +217,7 @@ testGroupNameCodes ps = do
testGroupRevokeNameCode :: HasCallStack => TestParams -> IO ()
testGroupRevokeNameCode ps = do
svc <- prepareGroupService ps
runWithOwner svc {gsIniSections = "" : namesSection <> chainSection} $ \cc _ _ alice -> do
runWithOwner svc {gsIniSections = "" : codesOnlyNames <> chainSection} $ \cc _ _ alice -> do
code <- prepaidIn . T.stripPrefix "Code: " =<< replyTo cc alice "/issue name 8"
let codeText = formatNameCode (NCPrepaid code)
revokeInGroup cc alice codeText "Revoked."
@@ -185,7 +227,7 @@ testGroupRevokeNameCode ps = do
testOperatorNameCodes :: HasCallStack => TestParams -> IO ()
testOperatorNameCodes ps = do
svc <- prepareGroupService ps
(t, cc, _, port) <- startGroupService svc {gsIniSections = "" : namesSection <> chainSection} Nothing
(t, cc, _, port) <- startGroupService svc {gsIniSections = "" : codesOnlyNames <> chainSection} Nothing
flip finally (stopGroupService t cc) $ do
pollUntilTrue (listening port)
forM_ ["name", "name 5", "name 64", "name 07", "name 7 years 1", "name 7 years 11", "name 7 gratis"] $ \args ->
@@ -197,12 +239,30 @@ testOperatorNameCodes ps = do
unpaid <- issueNameRaw cc "name 6 unpaid"
prepaidTerms cc paid `shouldReturn` Just (7, 3, CPSPaid)
prepaidTerms cc unpaid `shouldReturn` Just (6, 2, CPSUnpaid)
let revoke = revokeRaw cc . formatNameCode . NCPrepaid
let check code = checkPrepaid port (formatNameCode (NCPrepaid code))
coverage :: Int -> Int -> Text -> (Int, Maybe J.Value)
coverage minLength years state = (200, Just $ J.object ["minLength" J..= minLength, "years" J..= years, "state" J..= state])
revoke = revokeRaw cc . formatNameCode . NCPrepaid
check paid `shouldReturn` coverage 7 3 "unused"
check unpaid `shouldReturn` coverage 6 2 "unpaid"
revokeRaw cc (formatNameCode (NCPrepaid paid) <> " junk") `shouldReturn` Left badgeCmdUsage
revoke paid `shouldReturn` Right "Revoked."
revoke paid `shouldReturn` Right "Already revoked."
check paid `shouldReturn` coverage 7 3 "revoked"
unknown <- C.newRandom >>= (`randomNamePrepaidCode` 7)
revoke unknown `shouldReturn` Left "No such code."
held <- issueNameRaw cc "name 8"
spent <- issueNameRaw cc "name 8"
let st = chatStore cc
rowOf code = fromMaybe (error "no prepaid code row") <$> withTransaction st (`getPrepaidCode` namePrepaidCodeHash code)
rowOf held >>= placeCode st "r1" ownerA (order "alicebakery" 1) >>= (`shouldBe` Right ())
rowOf spent >>= placeCode st "r2" ownerA (order "bobsbakery" 2) >>= (`shouldBe` Right ())
spentAt <- getCurrentTime
PrepaidCode {pcId = spentId} <- rowOf spent
withTransaction st (\db -> spendPrepaidCode db spentId (RegistrationId "r2") spentAt) `shouldReturn` True
revoke held `shouldReturn` Right "Revoked. The order holding it keeps it."
revoke spent `shouldReturn` Left "Already used. It cannot be revoked."
check spent `shouldReturn` coverage 8 2 "used"
testRevokeTakesCodeTurn :: HasCallStack => TestParams -> IO ()
testRevokeTakesCodeTurn ps = do
@@ -211,7 +271,8 @@ testRevokeTakesCodeTurn ps = do
flip finally (stopGroupService t cc) $ do
pollUntilTrue (listening port)
turns <- newTurns
let revokeAfter code release = do
let st = chatStore cc
revokeAfter code release = do
revoking <- async $ revokeNameCode turns cc code
threadDelay 200000
poll revoking >>= (`shouldSatisfy` isNothing)
@@ -221,6 +282,12 @@ testRevokeTakesCodeTurn ps = do
holder <- holdTurn $ \body -> withCodeTurn turns (namePrepaidCodeHash held) body
revokeAfter held (releaseTurn holder) `shouldReturn` Right "Revoked."
finished (thAction holder) `shouldReturn` ()
now <- getCurrentTime
placePaid st "r1" ownerA (order "alicebakery" 1) (addUTCTime 3600 now) `shouldReturn` Right ()
reserved <- issueNameRaw cc "name 8"
reserving <- holdTurn $ \body -> reserveCode turns st now (namePrepaidCodeHash reserved) (RegistrationId "r1") 8 (\_ -> Right <$> body :: IO (Either Text ()))
revokeAfter reserved (releaseTurn reserving) `shouldReturn` Right "Revoked. The order holding it keeps it."
finished (thAction reserving) `shouldReturn` Right (Right ())
data TurnHolder a = TurnHolder {thAction :: Async a, thRelease :: MVar ()}
@@ -255,6 +322,13 @@ prepaidTerms cc code =
fmap (\PrepaidCode {pcMinLength, pcYears, pcPaymentStatus} -> (pcMinLength, pcYears, pcPaymentStatus))
<$> withTransaction (chatStore cc) (`getPrepaidCode` namePrepaidCodeHash code)
checkPrepaid :: Int -> Text -> IO (Int, Maybe J.Value)
checkPrepaid port code = do
mgr <- newManager defaultManagerSettings
req <- parseRequest $ "http://127.0.0.1:" <> show port <> "/api/code/check"
r <- httpLbs req {method = "POST", requestBody = RequestBodyLBS (J.encode (J.object ["code" J..= code]))} mgr
pure (statusCode (responseStatus r), J.decode (responseBody r))
groupName :: String
groupName = "testbadges"
File diff suppressed because it is too large. Load diff
+44 -12
View File
@@ -26,6 +26,8 @@ import Bots.StoreService.NamesContractsTests
year,
)
import Bots.StoreService.NamesProfilesTests (bakeryLink, jpeg, newsLink)
import Bots.StoreService.NamesStoreTests (issue, order, ownerA, placeCode, t0, usable)
import Bots.StoreService.WebTests (withServiceStore)
import Control.Concurrent.STM
import Control.Monad (forM_, replicateM_)
import Crypto.Random (ChaChaDRG)
@@ -34,7 +36,7 @@ import qualified Data.Aeson as J
import qualified Data.Aeson.Types as JT
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy.Char8 as LB
import Data.Maybe (fromMaybe)
import Data.Maybe (fromJust, fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
@@ -44,6 +46,7 @@ import Network.HTTP.Client (Manager, Request, RequestBody (..), defaultManagerSe
import qualified Network.HTTP.Client as HTTP
import Network.HTTP.Types.Status (statusCode)
import qualified Network.Wai.Handler.Warp as Warp
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
import qualified Simplex.Messaging.Crypto as C
import Simplex.Messaging.Encoding.String (strEncode)
import Simplex.Messaging.SimplexName (SimplexTLD (..))
@@ -51,7 +54,9 @@ import Simplex.Messaging.Transport.Client (TransportHost (..))
import StoreService.Config (ListenerConfig (..), NamesConfig (..))
import StoreService.Names.Abi (AbiValue (..), abiBytes, abiTuple, uint256)
import StoreService.Names.Contracts (Contracts (..))
import StoreService.Names.Phases (RegistrationId (..))
import StoreService.Names.Profiles (ChatKind (..), HostResolver, LinkProfile (..), ProfileFetcher, ProfileLimits (..), defaultProfileLimits, monotonicSeconds, newProfiles)
import StoreService.Names.Store (NamePhase (..), NameRegistration (..), VersionWrite (..), getRegistration, updateRegistration)
import StoreService.Web.Http (newHttpEnv, notFound, previewLimit, searchLimit)
import StoreService.Web.Names (Route (..), namesApp)
import StoreService.Web.Names.Search (previewHandler, searchHandler)
@@ -63,7 +68,8 @@ namesSearchTests = describe "StoreService names: search" $ do
describe "search" $ do
it "answers an available name with the chain's price for the paid years, the term start and the order's contracts" $ testAvailable g
it "answers a reserved name" $ testReserved g
it "answers a registered name with its entities, raw records, owner, resolver, expiry and grace period" $ testRegistered g
it "answers a name the chain has available as registered while its register here is sent, without a price" $ testRegistering g
it "answers a registered name with its entities, raw records, owner, resolver, nonce, expiry and grace period" $ testRegistered g
it "makes one entity per short link of its record's kind, splitting on ';', and none from other values" $ testRecordLinks g
it "makes entities of the first 5 short links of a record only, fetching no other" $ testRecordLinksCap g
it "reads no records from another resolver" $ testOtherResolver g
@@ -83,22 +89,22 @@ namesSearchTests = describe "StoreService names: search" $ do
-- harness
data Search = Search {node :: FakeEthNode, client :: (Manager, String), fetched :: TVar [Text]}
data Search = Search {node :: FakeEthNode, store :: DBStore, client :: (Manager, String), fetched :: TVar [Text]}
-- | The routes as namesRoutes registers them, since a NamesEnv needs a running chat controller.
withSearch :: TVar ChaChaDRG -> (NamesConfig -> NamesConfig) -> (Contracts -> Contracts) -> (Search -> IO a) -> IO a
withSearch g adjustConfig adjust action = withContracts g $ \node ct -> do
withSearch g adjustConfig adjust action = withServiceStore $ \store -> withContracts g $ \node ct -> do
fetched <- newTVarIO []
ps <- newProfiles defaultProfileLimits {fetchTimeout = fetchMicros} monotonicSeconds (fakeFetch fetched) stubResolver
http <- newHttpEnv ListenerConfig {lHost = "127.0.0.1", lBadgesPort = 0, lBadgesStaticDir = "", lServeWebapp = False, lBadgesExportDir = Nothing, lTrustForwardedFor = False, lNamesStaticDir = Nothing, lNamesExportDir = Nothing, lNamesPort = Nothing}
let cfg = adjustConfig $ namesConfig node
routes = \case
["api", "link", "preview"] -> Just Route {rMethod = "POST", rLimit = previewLimit, rHandler = previewHandler ps}
["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler cfg (adjust ct) ps hash}
["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler cfg store (adjust ct) ps hash}
_ -> Nothing
app req respond = fromMaybe (respond notFound) (namesApp http routes req respond)
mgr <- newManager defaultManagerSettings
Warp.testWithApplication (pure app) $ \port -> action Search {node, client = (mgr, "http://127.0.0.1:" <> show port), fetched}
Warp.testWithApplication (pure app) $ \port -> action Search {node, store, client = (mgr, "http://127.0.0.1:" <> show port), fetched}
namesConfig :: FakeEthNode -> NamesConfig
namesConfig node =
@@ -107,6 +113,7 @@ namesConfig node =
nYears = 2,
nTermStart = UTCTime (fromGregorian 2027 1 1) 0,
nTermSlackSeconds = 3600,
nCodesOnly = False,
nChain = fakeChainConfig node
}
@@ -229,22 +236,24 @@ availableAnswer price =
"tld" .= str "simplex"
]
registeredAnswer :: [J.Value] -> J.Value -> Text -> Answer
registeredAnswer entities records resolver =
registeredAnswer :: [J.Value] -> J.Value -> Text -> J.Value -> Answer
registeredAnswer entities records resolver nonce =
ok
[ "status" .= str "registered",
"entities" .= entities,
"records" .= records,
"owner" .= ownerText,
"resolver" .= resolver,
"recordsNonce" .= nonce,
"expiresAt" .= (now + year),
"gracePeriod" .= gracePeriod,
"chainId" .= fakeChainId,
"tld" .= str "simplex"
]
reservedAnswer :: Answer
reservedAnswer, registeringAnswer :: Answer
reservedAnswer = ok ["status" .= str "reserved"]
registeringAnswer = ok ["status" .= str "registered", "entities" .= ([] :: [J.Value]), "tld" .= str "simplex"]
recordsOf :: Text -> Text -> J.Value
recordsOf contact channel = J.object ["contact" .= contact, "channel" .= channel]
@@ -281,6 +290,15 @@ timed action = do
a <- action
(a,) . (`diffUTCTime` start) <$> getCurrentTime
setPhase :: DBStore -> NamePhase -> IO ()
setPhase st phase = withTransaction st $ \db -> do
r <- fromJust <$> getRegistration db (RegistrationId "r1")
updateRegistration db r {nrPhase = phase} t0 >>= (`shouldSatisfy` written)
where
written = \case
Written _ -> True
_ -> False
-- search
testAvailable :: TVar ChaChaDRG -> IO ()
@@ -296,6 +314,18 @@ testReserved g = withSearch g id id $ \s@Search {node} -> do
reserved node
searchAlice s 12 `shouldReturn` reservedAnswer
testRegistering :: TVar ChaChaDRG -> IO ()
testRegistering g = withSearch g id id $ \s@Search {node, store} -> do
available node
code <- issue store 1 usable
placeCode store "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right ()
setPhase store PRegistering
-- no price is answered, so reading one would fail the search
searchAlice s 12 `shouldReturn` registeringAnswer
setPhase store PRegistered
answerCall node fakeController (rentPriceCall 12) $ Returns $ rentPriceAnswer 10000
searchAlice s 12 `shouldReturn` availableAnswer 10000
testRegistered :: TVar ChaChaDRG -> IO ()
testRegistered g = withSearch g id id $ \s@Search {node} -> do
registeredWith node bakeryLink newsLink
@@ -306,6 +336,7 @@ testRegistered g = withSearch g id id $ \s@Search {node} -> do
]
(recordsOf bakeryLink newsLink)
resolverText
(J.toJSON (0 :: Int))
testRecordLinks :: TVar ChaChaDRG -> IO ()
testRecordLinks g = withSearch g id id $ \s@Search {node} -> do
@@ -318,7 +349,7 @@ testRecordLinks g = withSearch g id id $ \s@Search {node} -> do
`shouldBe` [("address", "contact", "ok", bakeryLink), ("address", "bot", "ok", botLink), ("channel", "channel", "ok", newsLink)]
(snd answer >>= JT.parseMaybe (J.withObject "answer" (J..: "records"))) `shouldBe` Just (recordsOf contact channel)
registeredWith node fullLink ""
searchAlice s 12 `shouldReturn` registeredAnswer [] (recordsOf fullLink "") resolverText
searchAlice s 12 `shouldReturn` registeredAnswer [] (recordsOf fullLink "") resolverText (J.toJSON (0 :: Int))
testRecordLinksCap :: TVar ChaChaDRG -> IO ()
testRecordLinksCap g = withSearch g id id $ \s@Search {node} -> do
@@ -334,7 +365,7 @@ testOtherResolver :: TVar ChaChaDRG -> IO ()
testOtherResolver g = withSearch g id id $ \s@Search {node} -> do
nameState node (toInteger $ now + year) 0
answerRegistered node otherResolver
searchAlice s 12 `shouldReturn` registeredAnswer [] J.Null otherResolverText
searchAlice s 12 `shouldReturn` registeredAnswer [] J.Null otherResolverText J.Null
testEntityProfiles :: TVar ChaChaDRG -> IO ()
testEntityProfiles g = withSearch g id id $ \s@Search {node} -> do
@@ -350,6 +381,7 @@ testEntityProfiles g = withSearch g id id $ \s@Search {node} -> do
]
(recordsOf (gone <> ";" <> privateName) privateLiteral)
resolverText
(J.toJSON (0 :: Int))
map (T.isInfixOf "#Gone") <$> fetchedLinks s `shouldReturn` [True]
where
deepLinkAt host kind tag = "simplex:/" <> T.singleton kind <> "#" <> snd (T.breakOnEnd "#" $ taggedLink host kind tag) <> "?h=" <> host
@@ -386,7 +418,7 @@ testOneDeadline g = withSearch g id (\ct -> ct {ctDeadline = deadline}) $ \s@Sea
answer `shouldBe` refused 503 "provider_unavailable"
elapsed `shouldSatisfy` (< 3)
answerRegistered node otherResolver
searchAlice s 12 `shouldReturn` registeredAnswer [] J.Null otherResolverText
searchAlice s 12 `shouldReturn` registeredAnswer [] J.Null otherResolverText J.Null
where
deadline = 2000000
+154 -1
View File
@@ -3,27 +3,118 @@
module Bots.StoreService.NamesWiringTests (namesWiringTests) where
import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (async, concurrently_, replicateConcurrently_, wait)
import qualified Control.Concurrent.Async as Async
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
import Control.Concurrent.STM
import qualified Control.Exception as E
import Control.Monad (replicateM_)
import qualified Data.Aeson as J
import qualified Data.Aeson.KeyMap as KM
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as LB
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe, isNothing)
import Network.HTTP.Client (Manager, Response, defaultManagerSettings, httpLbs, newManager, parseRequest, responseBody, responseHeaders, responseStatus)
import qualified Network.HTTP.Client as HTTP
import Network.HTTP.Types (Method, hCacheControl, hContentType, status200)
import Network.HTTP.Types.Status (statusCode)
import qualified Network.Wai.Handler.Warp as Warp
import Simplex.Messaging.SimplexName (labelHash)
import StoreService.Config (ListenerConfig (..))
import StoreService.Names.Phases
import StoreService.Names.Turns
import StoreService.Web.Http
import StoreService.Web.Names (Route (..), namesApp)
import System.Timeout (timeout)
import Test.Hspec
namesWiringTests :: Spec
namesWiringTests = describe "StoreService names: wiring" $ do
describe "phases" $ do
it "wakes a waiter on a newer published version, not on an older or the same one" testPublishWakesOnNewer
it "answers at once when the version read is not the one seen" testVersionAlreadyMoved
it "catches a publish that lands between subscribing and reading" testPublishBeforeRead
it "gives up after the hold when nothing is published" testHoldTimesOut
it "is not woken by another registration's publish" testOtherRegistration
it "drops a registration's watch once no waiter is left" testPhasesDropIdleWatches
describe "router" $ do
it "answers a known path with another method 405 with Allow, spending no token" testWrongMethod
it "leaves an unknown path to the caller" testUnknownPath
describe "turns" $ do
it "runs two holders of one label hash one after the other" testOneLabelSerialized
it "runs holders of different label hashes at once" testDifferentLabelsConcurrent
it "runs two holders of one code hash one after the other" testOneCodeSerialized
it "releases a turn whose holder throws" testTurnReleasedOnThrow
it "drops a lock once nobody holds or waits for it" testTurnsDropIdleLocks
-- | Long enough that only a missed wake-up could make a test wait it out.
longHold :: Int
longHold = 10000000
-- | How long a test waits for something that must happen promptly.
prompt :: Int
prompt = 2000000
parkDelay :: Int
parkDelay = 100000
rid1, rid2 :: RegistrationId
rid1 = RegistrationId "reg-1"
rid2 = RegistrationId "reg-2"
testPublishWakesOnNewer :: IO ()
testPublishWakesOnNewer = do
phases <- newPhases
parked <- newEmptyMVar
waiter <- async $ awaitChange phases rid1 (putMVar parked () >> pure 3) 3 longHold
takeMVar parked
atomically $ publish phases rid1 2
atomically $ publish phases rid1 3
threadDelay parkDelay
Async.poll waiter >>= (`shouldSatisfy` isNothing)
atomically $ publish phases rid1 4
timeout prompt (wait waiter) `shouldReturn` Just True
testVersionAlreadyMoved :: IO ()
testVersionAlreadyMoved = do
phases <- newPhases
timeout prompt (awaitChange phases rid1 (pure 5) 3 longHold) `shouldReturn` Just True
testPublishBeforeRead :: IO ()
testPublishBeforeRead = do
phases <- newPhases
-- the row read here is the old one, though version 4 was committed and published while it ran
let staleRead = atomically (publish phases rid1 4) >> pure 3
timeout prompt (awaitChange phases rid1 staleRead 3 longHold) `shouldReturn` Just True
testHoldTimesOut :: IO ()
testHoldTimesOut = do
phases <- newPhases
timeout prompt (awaitChange phases rid1 (pure 3) 3 parkDelay) `shouldReturn` Just False
testOtherRegistration :: IO ()
testOtherRegistration = do
phases <- newPhases
parked <- newEmptyMVar
waiter <- async $ awaitChange phases rid1 (putMVar parked () >> pure 1) 1 (3 * parkDelay)
takeMVar parked
atomically $ publish phases rid2 7
timeout prompt (wait waiter) `shouldReturn` Just False
testPhasesDropIdleWatches :: IO ()
testPhasesDropIdleWatches = do
phases <- newPhases
parked <- newEmptyMVar
woken <- async $ awaitChange phases rid1 (putMVar parked () >> pure 1) 1 longHold
cancelled <- async $ awaitChange phases rid2 (putMVar parked () >> pure 1) 1 longHold
replicateM_ 2 $ takeMVar parked
phasesCount phases `shouldReturn` 2
atomically $ publish phases rid1 2
timeout prompt (wait woken) `shouldReturn` Just True
Async.cancel cancelled
awaitChange phases rid1 (pure 2) 2 parkDelay `shouldReturn` False
phasesCount phases `shouldReturn` 0
type Client = (Manager, String)
@@ -71,3 +162,65 @@ testUnknownPath = withProbeRouter $ \client -> do
r <- send client "POST" "/api/probe"
statusCode (responseStatus r) `shouldBe` 404
errorTag r `shouldBe` Just (J.String "not_found")
-- | Runs the holders concurrently and returns the most that were inside at once.
mostInside :: Int -> ((IO () -> IO ()) -> IO ()) -> IO Int
mostInside n holder = do
inside <- newTVarIO (0 :: Int)
most <- newTVarIO 0
let enter = atomically $ do
k <- (+ 1) <$> readTVar inside
writeTVar inside k
modifyTVar' most (max k)
leave = atomically $ modifyTVar' inside (subtract 1)
replicateConcurrently_ n $ holder (\work -> enter >> work >> leave)
readTVarIO most
testOneLabelSerialized :: IO ()
testOneLabelSerialized = do
turns <- newTurns
r <- timeout (5 * prompt) $ mostInside 8 $ \inTurn -> withLabelTurn turns (labelHash "alice1") (inTurn (threadDelay 20000))
r `shouldBe` Just 1
testOneCodeSerialized :: IO ()
testOneCodeSerialized = do
turns <- newTurns
r <- timeout (5 * prompt) $ mostInside 8 $ \inTurn -> withCodeTurn turns (BC.replicate 32 'c') (inTurn (threadDelay 20000))
r `shouldBe` Just 1
-- | Each holder waits inside its turn for the other to be inside its own, which only concurrent turns allow.
testDifferentLabelsConcurrent :: IO ()
testDifferentLabelsConcurrent = do
turns <- newTurns
aInside <- newEmptyMVar
bInside <- newEmptyMVar
r <-
timeout prompt $
concurrently_
(withLabelTurn turns (labelHash "alice1") (putMVar aInside () >> takeMVar bInside))
(withLabelTurn turns (labelHash "bobby2") (putMVar bInside () >> takeMVar aInside))
r `shouldBe` Just ()
data Boom = Boom
deriving (Show)
instance E.Exception Boom
testTurnReleasedOnThrow :: IO ()
testTurnReleasedOnThrow = do
turns <- newTurns
let a = labelHash "alice1"
E.try (withLabelTurn turns a (E.throwIO Boom)) >>= \case
Left Boom -> pure ()
Right () -> expectationFailure "the holder's exception must reach the caller"
timeout prompt (withLabelTurn turns a (pure ())) `shouldReturn` Just ()
testTurnsDropIdleLocks :: IO ()
testTurnsDropIdleLocks = do
turns <- newTurns
let a = labelHash "alice1"
withLabelTurn turns a $ withLabelTurn turns (labelHash "bobby2") $ withCodeTurn turns (BC.replicate 32 'c') $ turnsCount turns `shouldReturn` 3
E.handle (\Boom -> pure ()) $ withLabelTurn turns a (E.throwIO Boom)
-- the waiter is cancelled by the timeout while the holder keeps the turn
withLabelTurn turns a $ timeout parkDelay (withLabelTurn turns a (pure ())) `shouldReturn` Nothing
turnsCount turns `shouldReturn` 0
+108 -29
View File
@@ -5,13 +5,44 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Bots.StoreService.WebTests (badgeWebTests) where
module Bots.StoreService.WebTests
( badgeWebTests,
withServiceStore,
columnsOf,
runMigrations,
digestFixture,
capturingStderr,
StubState (..),
StubCall (..),
newStubState,
stubProvider,
stubCalls,
setSignals,
sampleProviderInvoice,
prepareStaticDir,
testServiceConfig,
testBTCPayConfig,
WebClient,
webRequestBody,
webGet,
statusOf,
jsonObject,
fieldOf,
rcv,
bounded,
seedCheckoutCatalog,
createBody,
supporterPriceId,
codeHashText,
sampleCode,
)
where
import StoreService.Catalog (catalogCurrency, defaultCatalog)
import StoreService.Config (BTCPayConfig (..), ListenerConfig (..), PollConfig (..), ServiceConfig (..), SpeedPolicy (..), StripeConfig (..))
import StoreService.Config (BTCPayConfig (..), ListenerConfig (..), NamesConfig, PollConfig (..), ServiceConfig (..), SpeedPolicy (..), StripeConfig (..))
import StoreService.Log (logInfo)
import StoreService.Names.Abi (hex)
import StoreService.Orders (codeLifetime, settleOrder)
import StoreService.Orders (cancelOpenInvoice, codeLifetime, expireOverdue, settleOrder)
import StoreService.Poller
import StoreService.Providers
import StoreService.Providers.BTCPay (btcpayProvider, listPageSize, maxListPages)
@@ -24,6 +55,7 @@ import StoreService.Web.Http
import StoreService.Web.Server
import Bots.StoreService.BotTests (codeCounts, newPurchaseKeys)
import Bots.StoreService.CatalogTests (WebOffer (..), WebPrice (..), parseCatalogSource)
import Bots.StoreService.ConfigTests (chainSection, namesSection, parsedNames)
import Bots.StoreService.FakeBTCPay
import Bots.StoreService.FakeStripe (FakeStripe (..), fakeIntentStatus, setIntentState, stripeEvent, stripeSigHeader, withFakeStripe)
import Control.Concurrent (forkIO, threadDelay)
@@ -226,6 +258,9 @@ badgeWebTests = do
it "logs a handler exception as one service log record, never a bare line" testHandlerExceptionIsLogged
describe "StoreService names: sites" $ do
it "exports both sites, each with the publishable key" testExportsBothSites
it "serves the names shell in codes mode with codes_only on, with or without a publishable key, and the badges shell without it" testServesCodesMode
it "serves the names shell as built with codes_only off" testServesOpenMode
it "exports the names shell in codes mode only with codes_only on, and the badges shell as built" testExportsCodesMode
it "serves each site on its own port only, whatever the Host, and the API on both" testEachPortServesItsSite
it "listens on the badges port alone without the names site" testOneListenerWithoutTheNamesSite
it "serves a names asset with its type" testServesANamesAsset
@@ -382,7 +417,7 @@ testMigrationOrder = do
[(a, b) | (a, b) <- zip declared (drop 1 declared), a >= b] `shouldBe` []
let (badges, ours) = span (`elem` badgeMigrations) (map name storeServiceSchemaMigrations)
badges `shouldBe` badgeMigrations
ours `shouldBe` ["20261013_names"]
ours `shouldBe` ["20261013_names", "20261022_drop_taken_codes"]
where
badgeMigrations = ["20260915_badge_service_schema", "20260918_badge_group_ops", "20260925_badge_store_receipts", "20261012_badge_subscriptions"]
@@ -588,13 +623,16 @@ testExpireOverdueMovesOnlyTheNamed = withServiceStore $ \st -> do
unnamed = sampleInvoice {niInvoiceId = InvoiceId "inv-unnamed", niProviderRef = "p-unnamed", niCodeHash = digestFixture 13, niExpiresAt = pastExpiry}
createInvoiceRows st named `shouldReturn` Right ()
createInvoiceRows st unnamed `shouldReturn` Right ()
expireOverdue st now [niInvoiceId named] `shouldReturn` [niInvoiceId named]
expireNamed st now [niInvoiceId named] `shouldReturn` [niInvoiceId named]
invoiceStatus st (niInvoiceId named) `shouldReturn` ISExpired
invoiceStatus st (niInvoiceId unnamed) `shouldReturn` ISOpen
expireOverdue st now [niInvoiceId named] `shouldReturn` []
expireNamed st now [niInvoiceId named] `shouldReturn` []
expireAllOverdue :: DBStore -> UTCTime -> IO [InvoiceId]
expireAllOverdue st now = overdueInvoices st now >>= expireOverdue st now . map oiInvoiceId
expireAllOverdue st now = overdueInvoices st now >>= expireNamed st now . map oiInvoiceId
expireNamed :: DBStore -> UTCTime -> [InvoiceId] -> IO [InvoiceId]
expireNamed st now invIds = newWaiters >>= \waiters -> expireOverdue st waiters Nothing now now invIds
testRevokeAndRedeemExcludeEachOther :: IO ()
testRevokeAndRedeemExcludeEachOther = withServiceStore $ \st -> do
@@ -1296,6 +1334,49 @@ withBothSitesAs settings action =
Warp.testWithApplicationSettings (webSettings (listener cfg)) (pure namesSite) $ \prt ->
action badges (mgr, "http://127.0.0.1:" <> show prt)
-- | The [names] settings read from the ini, with codes_only on or off.
namesWithCodesOnly :: Bool -> IO NamesConfig
namesWithCodesOnly on = parsedNames (namesSection <> ["codes_only = " <> if on then "on" else "off"]) chainSection
shells :: WebClient -> WebClient -> IO ((Int, LB.ByteString), (Int, LB.ByteString))
shells badges namesSite = (,) <$> shellOf badges <*> shellOf namesSite
where
shellOf client = (\r -> (statusOf r, responseBody r)) <$> webGet client "/"
testServesCodesMode :: IO ()
testServesCodesMode = bounded "codes mode served" $ do
codesOnly <- namesWithCodesOnly True
withBothSitesAs (\cfg -> cfg {names = Just codesOnly}) $ \badges namesSite ->
shells badges namesSite `shouldReturn` ((200, badgesShellWithMode ""), (200, namesShellAs "" "codes"))
withBothSitesAs (\cfg -> cfg {names = Just codesOnly, stripe = Just publishTestStripe}) $ \badges namesSite ->
shells badges namesSite `shouldReturn` ((200, badgesShellWithMode "pk_test_injected"), (200, namesShellAs "pk_test_injected" "codes"))
testServesOpenMode :: IO ()
testServesOpenMode = bounded "open mode served" $ do
open <- namesWithCodesOnly False
withBothSitesAs (\cfg -> cfg {names = Just open}) $ \badges namesSite ->
shells badges namesSite `shouldReturn` ((200, badgesShellWithMode ""), (200, namesShellHtml))
testExportsCodesMode :: IO ()
testExportsCodesMode = do
codesOnly <- namesWithCodesOnly True
open <- namesWithCodesOnly False
createDirectoryIfMissing True "tests/tmp"
withTempDirectory "tests/tmp" "names-export" $ \root -> do
staticDir <- prepareStaticDir root
LB.writeFile (staticDir </> "index.html") (badgesShellWithMode "")
namesDir <- prepareNamesDir root
let badgesOut = root </> "badges-out"
namesOut = root </> "names-out"
base = testServiceConfig staticDir False
lc = (listener base) {lServeWebapp = False, lBadgesExportDir = Just badgesOut, lNamesStaticDir = Just namesDir, lNamesExportDir = Just namesOut}
exported namesCfg = do
exportWebapp base {listener = lc, names = namesCfg}
(,) <$> LB.readFile (badgesOut </> "index.html") <*> LB.readFile (namesOut </> "index.html")
exported (Just codesOnly) `shouldReturn` (badgesShellWithMode "", namesShellAs "" "codes")
exported (Just open) `shouldReturn` (badgesShellWithMode "", namesShellHtml)
exported Nothing `shouldReturn` (badgesShellWithMode "", namesShellHtml)
webGetAt :: WebClient -> ByteString -> String -> IO (Response LB.ByteString)
webGetAt client host target = webRequest client "GET" target [("Host", host)]
@@ -1999,8 +2080,7 @@ testCreateInvoice = bounded "create invoice" $ withCheckout $ \ref env client ->
irStatus row `shouldBe` ISOpen
irProvider row `shouldBe` PPCrypto
irProviderRef row `shouldBe` piProviderRef sampleProviderInvoice
irBadgeType row `shouldBe` BTSupporter
irMonths row `shouldBe` 1
irProduct row `shouldBe` IPBadge BTSupporter 1
irPrice row `shouldBe` CurrencyAmount 500
irAmount row `shouldBe` CurrencyAmount 500
irDestination row `shouldBe` piDestination sampleProviderInvoice
@@ -2058,8 +2138,7 @@ testCreateDerivesFromCatalog = bounded "derived from the catalog" $ withCheckout
fieldOf o "amount" `shouldBe` Just (J.Number 5400)
invId <- InvoiceId <$> stringField o "invoiceId"
Just row <- getInvoice (weStore env) invId
irBadgeType row `shouldBe` BTLegend
irMonths row `shouldBe` 3
irProduct row `shouldBe` IPBadge BTLegend 3
irPrice row `shouldBe` CurrencyAmount 6000
irAmount row `shouldBe` CurrencyAmount 5400
@@ -2341,7 +2420,7 @@ rcv :: Word32 -> Maybe Text -> Received
rcv amount crypto = Received {rcvAmount = CurrencyAmount amount, rcvCrypto = crypto, rcvDue = Nothing}
settle :: DBStore -> InvoiceId -> PaymentSignal -> UTCTime -> IO (Either Text InvoiceStatus)
settle st iid signal at = newWaiters >>= \waiters -> settleOrder st waiters iid signal at
settle st iid signal at = newWaiters >>= \waiters -> settleOrder st waiters Nothing iid signal at
seedOpen :: HasCallStack => DBStore -> IO InvoiceId
seedOpen st = do
@@ -2571,7 +2650,7 @@ testPublishIsAfterCommit = bounded "publish after commit" $ withServiceStore $ \
visible <- independentStatus st iid
putMVar seen (woken, visible)
threadDelay 100000
settleOrder st waiters iid (SigSettled (rcv 500 Nothing) settleAt) settleAt `shouldReturn` Right ISPaid
settleOrder st waiters Nothing iid (SigSettled (rcv 500 Nothing) settleAt) settleAt `shouldReturn` Right ISPaid
takeMVar seen `shouldReturn` (ISPaid, Just "paid")
waitCeiling :: Int
@@ -2581,7 +2660,7 @@ testSettlementWakesAHeldRequest :: IO ()
testSettlementWakesAHeldRequest = bounded "settlement wakes a hold" $ withWebApp $ \env client -> do
iid <- seedOpenInvoice env
let settled =
settleOrder (weStore env) (weWaiters env) iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
settleOrder (weStore env) (weWaiters env) Nothing iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
`shouldReturn` Right ISPaid
o <- wokenBy client iid settled "paid"
fieldOf o "amountPaid" `shouldBe` Just (J.Number 500)
@@ -2633,7 +2712,7 @@ testLosingTheStatusGuardStillWakesTheHold = bounded "losing the guard" $ withWeb
settledIntoAnExpiredRow :: HasCallStack => WebEnv -> InvoiceId -> IO ()
settledIntoAnExpiredRow env iid = do
settleOrder (weStore env) (weWaiters env) iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
settleOrder (weStore env) (weWaiters env) Nothing iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
`shouldReturn` Right ISExpired
invoiceStatus (weStore env) iid `shouldReturn` ISExpired
code <- settledCode (weStore env) iid
@@ -2675,7 +2754,7 @@ testCryptoAmountFillsInFromNull = bounded "crypto fills in" $ withServiceStore $
pollerFor :: WebEnv -> IO PollerEnv
pollerFor WebEnv {weStore, weConfig, weWaiters, weHints, weProviders} =
newPollerEnv weStore weWaiters weHints weProviders (poll weConfig)
newPollerEnv weStore weWaiters Nothing weHints weProviders (poll weConfig)
withStubPoller :: Int -> (IORef StubState -> PollerEnv -> WebEnv -> WebClient -> IO a) -> IO a
withStubPoller = withStubPollerOf stubProvider
@@ -2935,7 +3014,7 @@ testHintsDoNotPostponeThePass :: IO ()
testHintsDoNotPostponeThePass = bounded "hints do not postpone the pass" $ withStubPoller raceHold $ \ref _ env _ -> do
seedBadgePrice (weStore env) "price1"
mapM_ (\i -> seedOpenRef (weStore env) i (burstRef i) someExpiry) burstRefs
built <- newPollerEnv (weStore env) (weWaiters env) (weHints env) (weProviders env) PollConfig {pWaitingSeconds = 1, pIdleSeconds = 1}
built <- newPollerEnv (weStore env) (weWaiters env) Nothing (weHints env) (weProviders env) PollConfig {pWaitingSeconds = 1, pIdleSeconds = 1}
let poller = built {peStrayEvery = 0}
setSignals ref [(burstRef i, settledSignal) | i <- burstRefs]
runOnePass poller
@@ -2967,7 +3046,7 @@ awaitCalls ref p = do
testWaiterCutsTheIdleSleepShort :: IO ()
testWaiterCutsTheIdleSleepShort = bounded "waiter cuts the sleep" $ withStubPoller raceHold $ \_ _ env client -> do
iid <- seedOpenInvoice env
poller <- newPollerEnv (weStore env) (weWaiters env) (weHints env) (weProviders env) PollConfig {pWaitingSeconds = 1, pIdleSeconds = 10}
poller <- newPollerEnv (weStore env) (weWaiters env) Nothing (weHints env) (weProviders env) PollConfig {pWaitingSeconds = 1, pIdleSeconds = 10}
started <- getCurrentTime
slept <- async (passDue poller >>= atomically)
threadDelay 200000
@@ -3862,7 +3941,7 @@ testCancelExpiresAFundedInvoice = withServiceStore $ \st -> do
let iid = niInvoiceId sampleInvoice
createInvoiceRows st sampleInvoice `shouldReturn` Right ()
settle st iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) now `shouldReturn` Right ISOpen
cancelOpenInvoice st iid now `shouldReturn` True
newWaiters >>= \waiters -> cancelOpenInvoice st waiters Nothing iid now `shouldReturn` True
Just row <- getInvoice st iid
irStatus row `shouldBe` ISExpired
(ipCryptoPaid <$> irPayment row) `shouldBe` Just (Just "0.00050000")
@@ -3900,7 +3979,7 @@ testHeldWaitWakesOnAPaymentThatDoesNotSettle = bounded "funded wakes a hold" $ w
held <- async $ webGet client (invoicePath iid <> "?wait=open")
awaitWaitingCount env 1
waitingCount (weWaiters env) `shouldReturn` 1
settleOrder (weStore env) (weWaiters env) iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
`shouldReturn` Right ISOpen
r <- wait held
statusOf r `shouldBe` 200
@@ -3912,17 +3991,17 @@ testARepeatedSignalDoesNotWakeAHold :: IO ()
testARepeatedSignalDoesNotWakeAHold = bounded "repeat signal" $ withWebApp $ \env client -> do
iid <- seedOpenInvoice env
let funded = SigFunded (rcv 500 (Just "0.00050000")) PaidInPart
settleOrder (weStore env) (weWaiters env) iid funded settleAt `shouldReturn` Right ISOpen
settleOrder (weStore env) (weWaiters env) Nothing iid funded settleAt `shouldReturn` Right ISOpen
held <- async $ webGet client (invoicePath iid <> "?wait=open&seenPaid=0.00050000&seenFull=0")
threadDelay holdParkDelay
Async.poll held >>= (`shouldSatisfy` isNothing)
settleOrder (weStore env) (weWaiters env) iid funded settleAt `shouldReturn` Right ISOpen
settleOrder (weStore env) (weWaiters env) Nothing iid funded settleAt `shouldReturn` Right ISOpen
threadDelay holdParkDelay
stillParked <- Async.poll held
stillParked `shouldSatisfy` isNothing
settleOrder (weStore env) (weWaiters env) iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
settleOrder (weStore env) (weWaiters env) Nothing iid (SigSettled (rcv 500 (Just "0.00050000")) settleAt) settleAt
`shouldReturn` Right ISPaid
r <- wait held
statusOf r `shouldBe` 200
@@ -3934,7 +4013,7 @@ testHeldWaitIsNotWokenByAPassThatWroteNothing = bounded "no churn" $ withCheckou
iid <- seedOpenInvoice env
markPaid (weStore env) iid
waitingBefore <- waitingCount (weWaiters env)
settleOrder (weStore env) (weWaiters env) iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
`shouldReturn` Right ISPaid
waitingAfter <- waitingCount (weWaiters env)
waitingAfter `shouldBe` waitingBefore
@@ -3942,7 +4021,7 @@ testHeldWaitIsNotWokenByAPassThatWroteNothing = bounded "no churn" $ withCheckou
testViewCarriesTheProvidersPaidVerdict :: IO ()
testViewCarriesTheProvidersPaidVerdict = bounded "paid in full" $ withCheckout $ \_ env client -> do
iid <- seedOpenInvoice env
settleOrder (weStore env) (weWaiters env) iid (SigFunded (rcv 400 (Just "0.00040000")) PaidInFull) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigFunded (rcv 400 (Just "0.00040000")) PaidInFull) someCreated
`shouldReturn` Right ISOpen
o <- jsonObject =<< webGet client (invoicePath iid)
fieldOf o "status" `shouldBe` Just (J.String "open")
@@ -3952,9 +4031,9 @@ testViewCarriesTheProvidersPaidVerdict = bounded "paid in full" $ withCheckout $
testPaidVerdictIsNotWithdrawnByALaterRead :: IO ()
testPaidVerdictIsNotWithdrawnByALaterRead = bounded "verdict sticks" $ withCheckout $ \_ env client -> do
iid <- seedOpenInvoice env
settleOrder (weStore env) (weWaiters env) iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInFull) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInFull) someCreated
`shouldReturn` Right ISOpen
settleOrder (weStore env) (weWaiters env) iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigFunded (rcv 500 (Just "0.00050000")) PaidInPart) someCreated
`shouldReturn` Right ISOpen
o <- jsonObject =<< webGet client (invoicePath iid)
fieldOf o "paidInFull" `shouldBe` Just (J.Bool True)
@@ -3963,10 +4042,10 @@ testViewCarriesAClosedPaidVerdict :: IO ()
testViewCarriesAClosedPaidVerdict = bounded "closed in full" $ withCheckout $ \_ env client -> do
iid <- seedOpenInvoice env
let readVerdict = (\o -> (fieldOf o "status", fieldOf o "paidInFull", fieldOf o "amountPaid")) <$> (jsonObject =<< webGet client (invoicePath iid))
settleOrder (weStore env) (weWaiters env) iid (SigClosed (rcv 250 (Just "0.00025000")) PaidInPart) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigClosed (rcv 250 (Just "0.00025000")) PaidInPart) someCreated
`shouldReturn` Right ISExpired
readVerdict `shouldReturn` (Just (J.String "expired"), Just (J.Bool False), Just (J.Number 250))
settleOrder (weStore env) (weWaiters env) iid (SigClosed (rcv 500 (Just "0.00050000")) PaidInFull) someCreated
settleOrder (weStore env) (weWaiters env) Nothing iid (SigClosed (rcv 500 (Just "0.00050000")) PaidInFull) someCreated
`shouldReturn` Right ISExpired
readVerdict `shouldReturn` (Just (J.String "expired"), Just (J.Bool True), Just (J.Number 500))
+8
View File
@@ -15,8 +15,12 @@ import Bots.StoreService.NamesCodesTests
import Bots.StoreService.NamesContractsTests
import Bots.StoreService.NamesLabelTests
import Bots.StoreService.NamesLinksTests
import Bots.StoreService.NamesOrdersTests
import Bots.StoreService.NamesPhasesTests
import Bots.StoreService.NamesProfilesTests
import Bots.StoreService.NamesSearchTests
import Bots.StoreService.NamesSignatureTests
import Bots.StoreService.NamesStoreTests
import Bots.StoreService.NamesWiringTests
import Bots.StoreService.StoreVerifierTests
import Bots.StoreService.StripeTests
@@ -102,8 +106,12 @@ main = do
namesContractsTests
namesLabelTests
namesLinksTests
namesOrdersTests
namesPhasesTests
namesProfilesTests
namesSearchTests
namesSignatureTests
namesStoreTests
namesWiringTests
describe "SimpleX chat markdown" markdownTests
describe "JSON Tests" jsonTests