From 28ea3b757a7166d04d22d6fa4772aaea081f151f Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Sun, 1 Nov 2026 15:27:04 +0000 Subject: [PATCH] store: take name orders by card, crypto or code --- .../src/StoreService/Log.hs | 18 + .../src/StoreService/Names/Env.hs | 26 +- .../src/StoreService/Names/Orders.hs | 243 +++ .../src/StoreService/Names/View.hs | 104 +- .../src/StoreService/Orders.hs | 178 +- .../src/StoreService/Poller.hs | 131 +- .../src/StoreService/Service.hs | 23 +- .../src/StoreService/Store/Invoices.hs | 159 +- .../src/StoreService/Web/Http.hs | 105 +- .../src/StoreService/Web/Names.hs | 14 +- .../src/StoreService/Web/Names/Codes.hs | 47 + .../src/StoreService/Web/Names/Orders.hs | 240 +++ .../StoreService/Web/Names/Registration.hs | 77 + .../src/StoreService/Web/Names/Search.hs | 24 +- .../src/StoreService/Web/Server.hs | 290 ++-- simplex-chat.cabal | 17 + tests/Bots/StoreService/BotTests.hs | 11 +- .../StoreService/GroupIntegrationTests.hs | 98 +- tests/Bots/StoreService/NamesOrdersTests.hs | 1443 +++++++++++++++++ tests/Bots/StoreService/NamesSearchTests.hs | 56 +- tests/Bots/StoreService/NamesWiringTests.hs | 155 +- tests/Bots/StoreService/WebTests.hs | 137 +- tests/Test.hs | 8 + 23 files changed, 3260 insertions(+), 344 deletions(-) create mode 100644 apps/simplex-store-service/src/StoreService/Names/Orders.hs create mode 100644 apps/simplex-store-service/src/StoreService/Web/Names/Codes.hs create mode 100644 apps/simplex-store-service/src/StoreService/Web/Names/Orders.hs create mode 100644 apps/simplex-store-service/src/StoreService/Web/Names/Registration.hs create mode 100644 tests/Bots/StoreService/NamesOrdersTests.hs diff --git a/apps/simplex-store-service/src/StoreService/Log.hs b/apps/simplex-store-service/src/StoreService/Log.hs index 5aff5d2580..e394c95563 100644 --- a/apps/simplex-store-service/src/StoreService/Log.hs +++ b/apps/simplex-store-service/src/StoreService/Log.hs @@ -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) diff --git a/apps/simplex-store-service/src/StoreService/Names/Env.hs b/apps/simplex-store-service/src/StoreService/Names/Env.hs index 96513d5ec4..759563c223 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Env.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Env.hs @@ -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} diff --git a/apps/simplex-store-service/src/StoreService/Names/Orders.hs b/apps/simplex-store-service/src/StoreService/Names/Orders.hs new file mode 100644 index 0000000000..d2a86e1b31 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Names/Orders.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Names/View.hs b/apps/simplex-store-service/src/StoreService/Names/View.hs index e6bc7c0a76..733da26cb3 100644 --- a/apps/simplex-store-service/src/StoreService/Names/View.hs +++ b/apps/simplex-store-service/src/StoreService/Names/View.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Orders.hs b/apps/simplex-store-service/src/StoreService/Orders.hs index d81976732e..2c20f1450b 100644 --- a/apps/simplex-store-service/src/StoreService/Orders.hs +++ b/apps/simplex-store-service/src/StoreService/Orders.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Poller.hs b/apps/simplex-store-service/src/StoreService/Poller.hs index 2f78dfac61..516295532b 100644 --- a/apps/simplex-store-service/src/StoreService/Poller.hs +++ b/apps/simplex-store-service/src/StoreService/Poller.hs @@ -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) diff --git a/apps/simplex-store-service/src/StoreService/Service.hs b/apps/simplex-store-service/src/StoreService/Service.hs index b6a5dc2608..c66280bdad 100644 --- a/apps/simplex-store-service/src/StoreService/Service.hs +++ b/apps/simplex-store-service/src/StoreService/Service.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Store/Invoices.hs b/apps/simplex-store-service/src/StoreService/Store/Invoices.hs index 7b12b2dfe8..0e16980e22 100644 --- a/apps/simplex-store-service/src/StoreService/Store/Invoices.hs +++ b/apps/simplex-store-service/src/StoreService/Store/Invoices.hs @@ -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) diff --git a/apps/simplex-store-service/src/StoreService/Web/Http.hs b/apps/simplex-store-service/src/StoreService/Web/Http.hs index 59afbdcc8a..649334fe57 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Http.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Http.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Web/Names.hs b/apps/simplex-store-service/src/StoreService/Web/Names.hs index 437cc6b61b..0d87ec3fbf 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Names.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Names.hs @@ -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/ 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. diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Codes.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Codes.hs new file mode 100644 index 0000000000..e27bebcc98 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Codes.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Orders.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Orders.hs new file mode 100644 index 0000000000..92a5c19a98 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Orders.hs @@ -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" diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Registration.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Registration.hs new file mode 100644 index 0000000000..1a255b8085 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Registration.hs @@ -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 diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs index 6a1d57bfc8..4ab8d095c5 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs @@ -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] diff --git a/apps/simplex-store-service/src/StoreService/Web/Server.hs b/apps/simplex-store-service/src/StoreService/Web/Server.hs index 44c2f3a55b..4a2a59ca97 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Server.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Server.hs @@ -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 diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 54a4721f0a..288d384c16 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -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 diff --git a/tests/Bots/StoreService/BotTests.hs b/tests/Bots/StoreService/BotTests.hs index 8eab5b46c8..4b9357371c 100644 --- a/tests/Bots/StoreService/BotTests.hs +++ b/tests/Bots/StoreService/BotTests.hs @@ -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 = diff --git a/tests/Bots/StoreService/GroupIntegrationTests.hs b/tests/Bots/StoreService/GroupIntegrationTests.hs index 96a72d7907..c851e4e8af 100644 --- a/tests/Bots/StoreService/GroupIntegrationTests.hs +++ b/tests/Bots/StoreService/GroupIntegrationTests.hs @@ -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 = "" + 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" diff --git a/tests/Bots/StoreService/NamesOrdersTests.hs b/tests/Bots/StoreService/NamesOrdersTests.hs new file mode 100644 index 0000000000..eadbe69955 --- /dev/null +++ b/tests/Bots/StoreService/NamesOrdersTests.hs @@ -0,0 +1,1443 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Bots.StoreService.NamesOrdersTests + ( namesOrdersTests, + Ctx (..), + Order (..), + withOrders, + withOrdersCancelling, + withSettings, + configured, + codesOnly, + aliceOrder, + commitmentOf, + orderTime, + placeOrder, + placed, + post, + settleNow, + settled, + invoiceRegistration, + registrationOf, + keyA, + keyB, + ownerAText, + ownerBText, + yearsDuration, + codeRoutes, + issueCode, + registered, + registrationById, + field, + advance, + refusedWith, + ) +where + +import Bots.StoreService.FakeBTCPay (FakeBTCPay (..), setInvoiceState, withFakeBTCPay) +import Bots.StoreService.FakeEthNode +import Bots.StoreService.NamesContractsTests (alice, answerRegistered, answerRegistrar, nameExpiresCall, noncesCall, registrarOf, rentPriceAnswer, rentPriceCall, reservedNamesCall, unhex) +import Bots.StoreService.NamesStoreTests (btc, order, ownerA, ownerB, placeCode, placePaid, t0) +import Bots.StoreService.WebTests +import Control.Concurrent (threadDelay) +import Control.Concurrent.Async (async, concurrently, wait) +import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar) +import Control.Concurrent.STM (TVar, atomically, check, modifyTVar', newTVarIO, readTVar) +import qualified Control.Exception as E +import Control.Arrow ((&&&)) +import Control.Monad (forM_, void) +import Crypto.Random (ChaChaDRG) +import Data.Aeson ((.=)) +import qualified Data.Aeson as J +import qualified Data.Aeson.Key as K +import qualified Data.Aeson.KeyMap as KM +import qualified Data.ByteArray as BA +import Data.ByteString (ByteString) +import qualified Data.ByteString.Char8 as BC +import qualified Data.ByteString.Lazy.Char8 as LB +import Data.IORef (IORef, atomicModifyIORef', modifyIORef', newIORef, readIORef, writeIORef) +import Data.List (sort) +import Data.Maybe (fromJust, fromMaybe, isJust, isNothing) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, getCurrentTime) +import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds) +import Data.Word (Word64) +import Network.HTTP.Client (defaultManagerSettings, newManager, responseBody) +import Network.HTTP.Types (hContentType) +import Network.Wai (Application, requestHeaders) +import qualified Network.Wai.Handler.Warp as Warp +import Simplex.Chat.Badges.Code (NameCode (..), formatNameCode, namePrepaidCodeHash, randomNamePrepaidCode) +import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) +import Simplex.Chat.PaymentService.Types (CardProvider (..), CryptoCurrency (..), CurrencyAmount (..), InvoiceId (..), InvoiceStatus (..), PaymentProvider (..), ServicePaymentDestination (..), ServicePaymentMethod (..)) +import Simplex.Messaging.Agent.Store.Common (DBStore, withConnection, withTransaction) +import qualified Simplex.Messaging.Agent.Store.DB as DB +import qualified Simplex.Messaging.Crypto as C +import Simplex.Messaging.Crypto.Secp256k1 (Secp256k1PrivateKey, mkPrivateKey, signRecoverable) +import Simplex.Messaging.Encoding.String (strEncode) +import Simplex.Messaging.Eth.Address (Address) +import Simplex.Messaging.Eth.EIP712 (serializeSignature) +import Simplex.Messaging.SimplexName (LabelHash (..), SimplexTLD (..), labelHash) +import Simplex.Messaging.Util (safeDecodeUtf8, tshow) +import StoreService.Config (ChainConfig (..), NamesConfig (..), ServiceConfig (..)) +import StoreService.Names.Abi (hex, uint256) +import StoreService.Names.Chain (newChainClient) +import StoreService.Names.Contracts (newContracts) +import StoreService.Names.Codes (CodeRefusal (..)) +import StoreService.Names.Orders (OrdersEnv (..)) +import qualified StoreService.Names.Orders as Orders +import StoreService.Names.Phases (RegistrationId (..), awaitChange, newPhases) +import StoreService.Names.Signatures (OrderFields (..), orderMessage, personalDigest) +import StoreService.Names.Store +import StoreService.Names.Turns (newTurns, withCodeTurn) +import StoreService.Names.View (ViewReads (..), readViewChain, viewJSON) +import StoreService.Orders (cancelOpenInvoice, expireOverdue, settleOrder) +import StoreService.Poller (cleanupInterval, cleanupReadsPerPass, cleanupUnpaid, drainHints, newPollerEnv, newReadHints, queueReadHint, runOnePass, unpaidRetention) +import StoreService.Providers.BTCPay (btcpayProvider) +import StoreService.Providers (Funded (..), OrderDraft (..), PaymentSignal (..), Provider (..), ProviderError (..), ProviderInvoice (..)) +import StoreService.Store.Invoices (InvoicePayment (..), InvoiceProduct (..), InvoiceRow (..), OverdueInvoice (..), ProviderRefConflict (..), getInvoice, getInvoiceByProviderRef, overdueInvoices, unpaidRefs) +import StoreService.Waiters (Waiters, newWaiters) +import StoreService.Web.Http (codeLimit) +import StoreService.Web.Names (Route (..), namesApp) +import StoreService.Web.Names.Codes (codeCheckHandler) +import StoreService.Web.Names.Orders (codeOrderHandler) +import StoreService.Web.Server (WebEnv (..), newWebEnv, webApp, webSettings) +import System.Directory (createDirectoryIfMissing) +import System.Timeout (timeout) +import Test.Hspec +import UnliftIO.Temporary (withTempDirectory) + +namesOrdersTests :: Spec +namesOrdersTests = describe "StoreService names: orders" $ do + describe "paid orders" $ do + it "places an order as an unpaid live registration and answers the name invoice view" testPaidOrder + it "accepts the order the webapp's wallet signed, as viem signs it" testViemOrder + it "answers an exact resend with its invoice, also with a new timestamp and signature, and after the timestamp window" testExactResend + it "answers an exact resend after its order went past unpaid, before any chain read" testResendPastUnpaid + it "answers an exact resend of an expired order with its expired invoice" testResendExpired + it "reads an exact resend's unpaid invoice past its expiry as expired, and its lapsed commitment as failed, writing neither" testResendReadsTime + it "refuses a resend differing in any one field, or a new commitment, with a bare order_exists" testOneFieldDiffers + it "refuses an order carrying another wallet's commitment without disclosing anything" testForeignCommitment + it "refuses a bad signature, or another signer's" testBadSignature + it "refuses a stale timestamp on a new order" testStaleTimestamp + it "lets one of two concurrent orders for one owner and name win, the other never reaching the provider" testConcurrentOrders + it "cancels the provider invoice of an order whose insert lost to another with the same commitment" testLostInsertCancels + it "cancels the provider invoice and logs it when the order's insert fails" testInsertFailureCancels + it "checks the duration within the slack, before and after the term start" testDurationSlack + it "answers catalog_changed with the chain price" testCatalogChanged + it "answers name_taken for a registered, reserved or being registered name" testNameTaken + it "answers name_check_unavailable when a status or price read fails" testNameCheckUnavailable + it "refuses sealed missing or over 11095 characters and a body over 16384 bytes, accepting the longest order" testSizes + it "refuses malformed fields, an unknown kind, and an order outside 6-63 letters" testFormats + it "accepts orders for one name from two wallets" testTwoWallets + it "answers after the name's turn, so a slow reader of one answer holds no other order for the name, paid or by code" testSlowReaderHoldsNoTurn + describe "code orders" $ do + it "places a code order as a queued live registration holding its code, answering only its id" testCodeOrder + it "answers an exact resend with its id in any phase, the code spent or not, before any chain read" testCodeResend + it "refuses a resend differing in any one field, the code included, with a bare order_exists" testCodeOneFieldDiffers + it "refuses a bad signature, or another signer's, after the code check" testCodeBadSignature + it "refuses a stale timestamp on a new code order" testCodeStaleTimestamp + it "refuses a code order meeting an open invoice for the name with order_exists" testCodeMeetsInvoice + it "closes an overdue paid order of the same wallet and name, cancelling its invoice after" testCodeClosesOverdue + it "refuses each code that cannot pay with its tag, the state before the cover" testCodeRefusals + it "checks the duration for the code's years" testCodeYears + it "answers name_taken and name_check_unavailable before the code's state and cover, leaving the code unreserved" testCodeNameChecks + it "refuses sealed over 11095 characters, a body over 16384 bytes and malformed fields, accepting the longest order" testCodeSizes + it "runs code orders in the code's turn, one of two spending one code for two names winning, the other answered code_reserved" testConcurrentCodeOrders + it "answers the code's refusal when its reservation fails in the insert, writing nothing" testCodeNotReservable + it "counts code orders and code checks against one code limit" testCodeLimitShared + describe "closed orders" $ do + it "accepts a new order of the same wallet and name after a cancel" testOrderAfterCancel + it "closes an overdue order of the same wallet and name before the poller marks it, cancelling its invoice after" testOverdueClosed + it "records a payment of the closed order as a late payment, leaving the newer order alone" testLatePaymentAfterNewOrder + it "keeps an overdue order whose invoice holds money, refusing the next order, and settles it" testFundedOverdueKept + it "alerts on a late payment as BTCPay reports it for an expired, cancelled or swept invoice, once" testLateBTCPayPayments + it "alerts once on money read while open when the provider then closes the invoice, also after a restart" testOneLateAlert + it "answers cancel with funded, not_open, paid when settlement won, and expired with the payment read meanwhile" testCancelAnswers + it "alerts once when a cancel expires an invoice a payment landed on while the provider was told" testCancelAlertsFunded + it "leaves a late payment of a cancelled invoice unpaid and alerts" testLatePayment + it "queues an order settled while open and publishes its version" testSettlementQueues + it "takes the invoice's row before recording a payment that moves no status" testSettlementLocksInvoice + it "closes the order of an invoice the provider closed or the sweep expired" testExpiryCloses + it "sweeps each invoice on its own, skipping one whose order cannot be read" testSweepSkipsBadRow + it "keeps out of a pass's sweep only the invoices whose read or settlement failed" testPassSweepsAroundFailures + it "cleans up unpaid orders after 30 days, reading the provider first" testCleanup + it "reads at most a pass's share of the provider in a cleanup, carrying the rest to the next pass" testCleanupCapped + it "answers 503 for a name invoice while names are off, cancelling nothing" testNamesOff + describe "codes only" $ do + it "answers a name invoice 403 codes_only before reading the order, reaching no provider and writing nothing" testInvoiceCodesOnly + it "sells a badge as before" testBadgeCodesOnly + describe "invoice side" $ do + it "finds an invoice by provider ref in either table, refusing a ref both hold" testProviderRefs + it "reads and sweeps name invoices with the badge ones" testPollerQueries + it "agrees between the sweep and the overdue close on which invoices hold money" testFundedAgreement + describe "registration view" $ do + it "has the contract's fields" testViewFields + it "reads the intent nonce in committed and nothing in taken" testViewReads + it "leaves a failed intentNonce read null and named in unread, reading nothing in taken" testViewUnread + +-- fixtures + +-- 21 October 2026 09:15 UTC, the webapp's signed order vector's timestamp +orderTime :: UTCTime +orderTime = posixSecondsToUTCTime 1792574100 + +termStart :: UTCTime +termStart = posixSecondsToUTCTime 1798761600 + +yearSeconds :: Word64 +yearSeconds = 31536000 + +slackSeconds :: Int +slackSeconds = 3600 + +seconds :: UTCTime -> Word64 +seconds = floor . utcTimeToPOSIXSeconds + +-- | What the page sends: 2 paid years and the free days until the term starts. +durationAt :: UTCTime -> Word64 +durationAt = yearsDuration 2 + +yearsDuration :: Word64 -> UTCTime -> Word64 +yearsDuration years now = years * yearSeconds + fromIntegral (max 0 (toInteger (seconds termStart) - toInteger (seconds now))) + +-- the webapp's wallet, BIP-39 entropy 6f2d1c9a4b7e3f80d15a2c6e9b04f7a3, accounts 0 and 1 as viem derives them +keyA, keyB :: Secp256k1PrivateKey +keyA = privateKey "14bb03a88d47f77e17be83d8fe479ab982ac8b18b5f4bdec7e40ffccb1af9325" +keyB = privateKey "e8068cb3aaf53e5c46b40aefd8b78480453a8265d158df06174ba0e49bf0f130" + +privateKey :: ByteString -> Secp256k1PrivateKey +privateKey = either error id . mkPrivateKey . BA.convert . unhex + +ownerAText, ownerBText :: Text +ownerAText = "0x014f9c09CcEeDEF1B713110d04D3a07d5B5D9b73" +ownerBText = "0x39ad11775d14FC8c9118Bd94f374DF3c7ad5655d" + +data Order = Order + { oLabel :: Text, + oOwner :: Text, + oKey :: Secp256k1PrivateKey, + oCommitment :: ByteString, + oSealed :: Text, + oDuration :: Word64, + oMethod :: Text, + oPrice :: Int, + oTimestamp :: Word64 + } + +aliceOrder :: Order +aliceOrder = + Order + { oLabel = "alicebakery", + oOwner = ownerAText, + oKey = keyA, + oCommitment = commitmentOf 1, + oSealed = "sealed-alicebakery-1", + oDuration = durationAt orderTime, + oMethod = "btc", + oPrice = alicePrice, + oTimestamp = seconds orderTime + } + +-- the launch price of 11 letters for 2 years +alicePrice :: Int +alicePrice = 10000 + +commitmentOf :: Int -> ByteString +commitmentOf n = C.sha256Hash $ BC.pack $ "commitment " <> show n + +fieldsOf :: Order -> OrderFields +fieldsOf Order {oLabel, oCommitment, oSealed, oDuration} = + OrderFields {ofLabelHash = labelHash oLabel, ofLength = T.length oLabel, ofDuration = oDuration, ofCommitment = oCommitment, ofSealed = oSealed} + +signedBody :: TVar ChaChaDRG -> Order -> IO J.Object +signedBody g o@Order {oKey, oTimestamp} = do + sig <- either error id <$> signRecoverable g oKey (personalDigest $ orderMessage (fieldsOf o) oTimestamp) + pure $ withSignature (hex $ serializeSignature sig) (unsignedBody o) + +unsignedBody :: Order -> J.Object +unsignedBody o@Order {oOwner, oMethod, oPrice, oTimestamp} = + KM.fromList + [ ("kind", "name"), + ("labelHash", J.String $ hex lh), + ("length", J.toJSON ofLength), + ("duration", J.toJSON ofDuration), + ("owner", J.String oOwner), + ("commitment", J.String $ hex ofCommitment), + ("sealed", J.String ofSealed), + ("method", J.String oMethod), + ("price", J.toJSON oPrice), + ("timestamp", J.toJSON oTimestamp) + ] + where + OrderFields {ofLabelHash = LabelHash lh, ofLength, ofDuration, ofCommitment, ofSealed} = fieldsOf o + +withSignature :: Text -> J.Object -> J.Object +withSignature sig = KM.insert "signature" (J.String sig) + +data Ctx = Ctx + { cStore :: DBStore, + cNode :: FakeEthNode, + cOrders :: OrdersEnv, + cWeb :: WebEnv, + cClient :: WebClient, + cStub :: IORef StubState, + cClock :: IORef UTCTime, + cRandom :: TVar ChaChaDRG, + cIps :: IORef Int + } + +testNamesConfig :: FakeEthNode -> NamesConfig +testNamesConfig node = + NamesConfig + { nTLD = TLDSimplex, + nYears = 2, + nTermStart = termStart, + nTermSlackSeconds = slackSeconds, + nCodesOnly = False, + nChain = fakeChainConfig node + } + +-- | Each invoice the stub creates gets its own provider ref, and a card invoice its client secret. +freshRefs :: IORef Int -> Provider -> Provider +freshRefs counter p = p {pCreateInvoice = \method draft -> pCreateInvoice p method draft >>= traverse fresh} + where + fresh pinv = do + n <- atomicModifyIORef' counter (\i -> (i + 1, i)) + let ref = "pref-" <> tshow n + pure $ case pProvider p of + PPStripe -> pinv {piProviderRef = ref, piDestination = SPDCard CPStripe ("pi_secret_" <> tshow n)} + _ -> pinv {piProviderRef = ref} + +withOrders :: (Ctx -> IO a) -> IO a +withOrders = withOrdersVia id + +-- | With the providers changed as a test needs them. +withOrdersVia :: (Provider -> Provider) -> (Ctx -> IO a) -> IO a +withOrdersVia via action = withServiceStore $ \cStore -> withFakeEthNode $ \cNode -> do + cRandom <- C.newRandom + ct <- newContracts TLDSimplex =<< newChainClient cRandom (fakeChainConfig cNode) + turns <- newTurns + phases <- newPhases + cClock <- newIORef orderTime + cStub <- newIORef (newStubState (Right sampleProviderInvoice)) + counter <- newIORef (1 :: Int) + cIps <- newIORef 1 + answerAvailable cNode + createDirectoryIfMissing True "tests/tmp" + withTempDirectory "tests/tmp" "names-orders" $ \root -> do + staticDir <- prepareStaticDir root + let cfg = (testServiceConfig staticDir True) {btcpay = Just testBTCPayConfig} + cOrders = OrdersEnv {oeStore = cStore, oeConfig = testNamesConfig cNode, oeContracts = ct, oeTurns = turns, oePhases = phases, oeNow = readIORef cClock} + crypto = via $ freshRefs counter (stubProvider cStub) + card = via $ freshRefs counter (stubProvider cStub) {pProvider = PPStripe} + waiters <- newWaiters + hints <- newReadHints + built <- newWebEnv cStore cfg waiters hints [crypto, card] Nothing + let cWeb = built {weOrders = Just cOrders} + mgr <- newManager defaultManagerSettings + Warp.testWithApplicationSettings (webSettings (listener cfg)) (pure $ ordersApp cWeb cOrders) $ \port -> + action Ctx {cStore, cNode, cOrders, cWeb, cClient = (mgr, "http://127.0.0.1:" <> show port), cStub, cClock, cRandom, cIps} + +-- | The context's store, chain, providers and clock with its names settings changed, as after a restart. +configured :: (NamesConfig -> NamesConfig) -> Ctx -> Ctx +configured change ctx@Ctx {cOrders, cWeb} = ctx {cOrders = orders, cWeb = cWeb {weConfig = (weConfig cWeb) {names = Just namesCfg}, weOrders = Just orders}} + where + namesCfg = change (oeConfig cOrders) + orders = cOrders {oeConfig = namesCfg} + +codesOnly :: NamesConfig -> NamesConfig +codesOnly cfg = cfg {nCodesOnly = True} + +-- | Another server of the orders routes over the context with its names settings changed. +withSettings :: (NamesConfig -> NamesConfig) -> Ctx -> (Ctx -> IO a) -> IO a +withSettings change ctx action = do + let ctx'@Ctx {cOrders, cWeb} = configured change ctx + mgr <- newManager defaultManagerSettings + Warp.testWithApplicationSettings (webSettings (listener $ weConfig cWeb)) (pure $ ordersApp cWeb cOrders) $ \port -> + action ctx' {cClient = (mgr, "http://127.0.0.1:" <> show port)} + +ordersApp :: WebEnv -> OrdersEnv -> Application +ordersApp web orders req respond = fromMaybe (webApp web req respond) (namesApp (weHttp web) (codeRoutes orders (weProviders web) (weWaiters web)) req respond) + +-- | The routes as namesRoutes registers them, since a NamesEnv needs a running chat controller. +codeRoutes :: OrdersEnv -> [Provider] -> Waiters -> [Text] -> Maybe Route +codeRoutes orders providers waiters = \case + ["api", "name", "register"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeOrderHandler orders providers waiters} + ["api", "code", "check"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeCheckHandler (oeStore orders)} + _ -> Nothing + +-- | alicebakery and dynamis available, at the launch prices for 2 years. +answerAvailable :: FakeEthNode -> IO () +answerAvailable node = do + answerRegistrar node + setBlock node 100 (seconds orderTime) + forM_ [nameExpiresCall, dynamisExpiresCall] $ \call -> answerCall node (registrarOf node) call $ Returns $ uint256 (0 :: Int) + forM_ [reservedNamesCall, dynamisReservedCall] $ \call -> answerCall node fakeController call $ Returns $ uint256 (0 :: Int) + answerCall node fakeController (rentPriceCall 11) $ Returns $ rentPriceAnswer 10000 + answerCall node fakeController (rentPriceCall 7) $ Returns $ rentPriceAnswer 20000 + +-- viem: nameExpires and reservedNames of labelhash("dynamis") +dynamisExpiresCall, dynamisReservedCall :: ByteString +dynamisExpiresCall = unhex "d6e4fa8678b602f03f164366531da963764c1f2fdfd588d0487daf2aa261ca799e5ffe33" +dynamisReservedCall = unhex "389c074878b602f03f164366531da963764c1f2fdfd588d0487daf2aa261ca799e5ffe33" + +-- | Each request from its own client address, so the create limit counts none twice. +post :: Ctx -> String -> LB.ByteString -> IO (Int, Maybe J.Object) +post Ctx {cClient, cIps} path body = do + n <- atomicModifyIORef' cIps (\i -> (i + 1, i)) + let ip = "198.51." <> show (n `div` 250) <> "." <> show (n `mod` 250 + 1) + r <- webRequestBody cClient "POST" path [(hContentType, "application/json"), ("X-Forwarded-For", BC.pack ip)] body + pure (statusOf r, J.decode (responseBody r)) + +postOrder :: Ctx -> J.Object -> IO (Int, Maybe J.Object) +postOrder ctx = post ctx "/api/invoice" . J.encode + +placeOrder :: Ctx -> Order -> IO (Int, Maybe J.Object) +placeOrder ctx o = signedBody (cRandom ctx) o >>= postOrder ctx + +-- | A placed order's invoice id, failing the test unless the order was accepted. +placed :: Ctx -> Order -> IO (InvoiceId, J.Object) +placed ctx o = + placeOrder ctx o >>= \case + (200, Just v) | Just (J.String invId) <- KM.lookup "invoiceId" v -> pure (InvoiceId invId, v) + other -> error $ "order refused: " <> show other + +errorOnly :: Text -> Maybe J.Object +errorOnly tag = Just $ KM.fromList [("error", J.String tag)] + +refusedWith :: Int -> Text -> (Int, Maybe J.Object) +refusedWith status tag = (status, errorOnly tag) + +registrationOf :: J.Object -> J.Object +registrationOf v = case KM.lookup "registration" v of + Just (J.Object r) -> r + _ -> error "no registration in the view" + +field :: Text -> J.Object -> Maybe J.Value +field k = KM.lookup (K.fromText k) + +advance :: Ctx -> NominalDiffTime -> IO () +advance Ctx {cClock} by = modifyIORef' cClock (addUTCTime by) + +registrationsOf :: Ctx -> Address -> IO [NameRegistration] +registrationsOf Ctx {cStore} owner = withTransaction cStore (`findRegistrations` owner) + +invoiceRegistration :: Ctx -> InvoiceId -> IO NameRegistration +invoiceRegistration Ctx {cStore} invId = + getInvoice cStore invId >>= \case + Just InvoiceRow {irProduct = IPName rid} -> withTransaction cStore $ \db -> fromJust <$> getRegistration db rid + other -> error $ "not a name invoice: " <> show other + +settleNow :: Ctx -> InvoiceId -> PaymentSignal -> IO (Either Text InvoiceStatus) +settleNow Ctx {cStore, cWeb, cOrders, cClock} invId signal = readIORef cClock >>= settleOrder cStore (weWaiters cWeb) (Just $ oePhases cOrders) invId signal + +settled :: PaymentSignal +settled = SigSettled (rcv 10000 (Just "0.00156")) orderTime + +textOf :: Text -> Maybe J.Object -> Text +textOf k v = case field k =<< v of + Just (J.String t) -> t + other -> error $ T.unpack k <> ": " <> show other + +objectOf :: J.Value -> J.Object +objectOf = \case + J.Object o -> o + v -> error $ "not an object: " <> show v + +-- tests + +testPaidOrder :: IO () +testPaidOrder = withOrders $ \ctx -> do + (invId, v) <- placed ctx aliceOrder + sort (KM.keys v) + `shouldBe` sort ["invoiceId", "status", "amount", "currency", "expiresAt", "kind", "registration", "address", "cryptoAmount", "cryptoCurrency", "requiredConfirmations"] + field "kind" v `shouldBe` Just "name" + field "status" v `shouldBe` Just "open" + field "amount" v `shouldBe` Just (J.Number 10000) + field "currency" v `shouldBe` Just "usd" + field "expiresAt" v `shouldBe` Just (J.toJSON $ addUTCTime (45 * 60) orderTime) + let r = registrationOf v + field "phase" r `shouldBe` Just "unpaid" + field "version" r `shouldBe` Just (J.Number 1) + field "owner" r `shouldBe` Just (J.String ownerAText) + field "labelHash" r `shouldBe` Just (J.String $ hex $ (\(LabelHash h) -> h) alice) + field "price" r `shouldBe` Just (J.Number 10000) + field "duration" r `shouldBe` Just (J.toJSON $ durationAt orderTime) + let reference = "name:" <> hex (orderHash ownerA (fieldsOf aliceOrder) (OTInvoice (SPMCrypto CCBtc) 10000)) + stubCalls (cStub ctx) `shouldReturn` [StubCreate (SPMCrypto CCBtc) OrderDraft {odAmount = CurrencyAmount 10000, odCurrency = "usd", odReference = reference}] + reg <- invoiceRegistration ctx invId + (nrPhase reg, nrLive reg, nrVersion reg, nrYears reg, nrPrice reg, nrPayment reg) `shouldBe` (PUnpaid, True, 1, 2, 10000, RPInvoice (SPMCrypto CCBtc) invId) + nrOrderHash reg `shouldBe` orderHash ownerA (fieldsOf aliceOrder) (OTInvoice (SPMCrypto CCBtc) 10000) + +-- | The body of the webapp's order vector, with viem's signature over it (NamesSignatureTests). +testViemOrder :: IO () +testViemOrder = withOrders $ \ctx -> do + let dynamis = + aliceOrder + { oLabel = "dynamis", + oCommitment = unhex "9cf04ddef2eedfb02e6e7ba58f879c0636c584b6acc67ffe9f97a06bfb016ac1", + oSealed = "q3Zk0Yx7c2VhbGVkLW9yZGVyLWZpeHR1cmU", + oDuration = 69259500, + oPrice = 20000 + } + viemSignature = "0x73e32be461e99e64b093a4ad09b04b4d810e5623a1b0e9c9fdb4701ec2a6a2a84df9ffdccc39c5cb827cde4d846c7662f591665f1b868ab1c15778646d3f414e1b" + signedBody (cRandom ctx) dynamis >>= (`shouldBe` withSignature viemSignature (unsignedBody dynamis)) + (status, v) <- postOrder ctx $ withSignature viemSignature (unsignedBody dynamis) + status `shouldBe` 200 + (field "phase" . registrationOf =<< v) `shouldBe` Just "unpaid" + +testExactResend :: IO () +testExactResend = withOrders $ \ctx -> do + body <- signedBody (cRandom ctx) aliceOrder + (invId, first') <- placed ctx aliceOrder + advance ctx 60 + resigned <- placed ctx aliceOrder {oTimestamp = seconds orderTime + 60} + resigned `shouldBe` (invId, first') + advance ctx 600 + postOrder ctx body `shouldReturn` (200, Just first') + length <$> stubCalls (cStub ctx) `shouldReturn` 1 + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + +testResendPastUnpaid :: IO () +testResendPastUnpaid = withOrders $ \ctx -> do + (invId, _) <- placed ctx aliceOrder + settleNow ctx invId settled `shouldReturn` Right ISPaid + setFault (cNode ctx) "eth_call" HttpFailure + (status, v) <- placeOrder ctx aliceOrder {oTimestamp = seconds orderTime + 1} + status `shouldBe` 200 + (field "invoiceId" =<< v) `shouldBe` Just (J.String $ (\(InvoiceId t) -> t) invId) + (field "status" =<< v) `shouldBe` Just "paid" + (field "phase" . registrationOf =<< v) `shouldBe` Just "queued" + +testResendExpired :: IO () +testResendExpired = withOrders $ \ctx -> do + (InvoiceId invId, _) <- placed ctx aliceOrder + fst <$> post ctx ("/api/invoice/" <> T.unpack invId <> "/cancel") "" `shouldReturn` 200 + (status, v) <- placeOrder ctx aliceOrder + status `shouldBe` 200 + (field "invoiceId" =<< v) `shouldBe` Just (J.String invId) + (field "status" =<< v) `shouldBe` Just "expired" + +testResendReadsTime :: IO () +testResendReadsTime = do + withOrders $ \ctx -> do + (invId, _) <- placed ctx aliceOrder + advance ctx (45 * 60 + 1) + (status, v) <- placeOrder ctx aliceOrder + (status, field "status" =<< v, field "phase" . registrationOf =<< v) `shouldBe` (200, Just "expired", Just "unpaid") + fmap irStatus <$> getInvoice (cStore ctx) invId `shouldReturn` Just ISOpen + withOrders $ \ctx@Ctx {cStore, cClock} -> do + (invId, _) <- placed ctx aliceOrder + settleNow ctx invId settled `shouldReturn` Right ISPaid + r <- invoiceRegistration ctx invId + now <- readIORef cClock + let lapse = seconds now + 600 + Written version <- withTransaction cStore $ \db -> + updateRegistration db r {nrPhase = PCommitted, nrRevealAfter = Just (lapse - 3600), nrRevealBefore = Just lapse} now + let resend = (\(status, v) -> (status, field "status" =<< v, (field "phase" &&& field "error") . registrationOf <$> v)) <$> placeOrder ctx aliceOrder + resend `shouldReturn` (200, Just "paid", Just (Just "committed", Just J.Null)) + advance ctx 601 + resend `shouldReturn` (200, Just "paid", Just (Just "failed", Just "The name was not signed in time")) + (nrPhase &&& nrVersion) <$> invoiceRegistration ctx invId `shouldReturn` (PCommitted, version) + +testOneFieldDiffers :: IO () +testOneFieldDiffers = withOrders $ \ctx -> do + void $ placed ctx aliceOrder + let variants = + [ aliceOrder {oLabel = "dynamis"}, + aliceOrder {oCommitment = commitmentOf 2}, + aliceOrder {oSealed = "sealed-alicebakery-2"}, + aliceOrder {oDuration = oDuration aliceOrder + 1}, + aliceOrder {oMethod = "xmr"}, + aliceOrder {oMethod = "card"}, + aliceOrder {oPrice = alicePrice + 1} + ] + forM_ variants $ \o -> placeOrder ctx o `shouldReturn` refusedWith 409 "order_exists" + length <$> stubCalls (cStub ctx) `shouldReturn` 1 + +testForeignCommitment :: IO () +testForeignCommitment = withOrders $ \ctx -> do + void $ placed ctx aliceOrder + placeOrder ctx aliceOrder {oOwner = ownerBText, oKey = keyB, oLabel = "dynamis", oPrice = 20000} + `shouldReturn` refusedWith 409 "order_exists" + placeOrder ctx aliceOrder {oOwner = ownerBText, oKey = keyB} + `shouldReturn` refusedWith 409 "order_exists" + length <$> registrationsOf ctx ownerB `shouldReturn` 0 + +testBadSignature :: IO () +testBadSignature = withOrders $ \ctx -> do + byB <- signedBody (cRandom ctx) aliceOrder {oKey = keyB} + postOrder ctx byB `shouldReturn` refusedWith 400 "bad_request" + good <- signedBody (cRandom ctx) aliceOrder + sig <- case KM.lookup "signature" good of + Just (J.String t) -> pure t + other -> error $ "signature: " <> show other + let highS = "0x" <> T.replicate 32 "11" <> "ff" <> T.replicate 31 "11" <> "1b" + forM_ [T.toUpper sig, T.dropEnd 2 sig <> "1d", highS, ""] $ \bad -> + postOrder ctx (withSignature bad good) `shouldReturn` refusedWith 400 "bad_request" + postOrder ctx (KM.delete "signature" good) `shouldReturn` refusedWith 400 "bad_request" + stubCalls (cStub ctx) `shouldReturn` [] + +testStaleTimestamp :: IO () +testStaleTimestamp = withOrders $ \ctx -> do + let at offset = aliceOrder {oTimestamp = fromIntegral (toInteger (seconds orderTime) + offset)} + forM_ [-601, 601] $ \offset -> placeOrder ctx (at offset) `shouldReturn` refusedWith 400 "bad_request" + -- near the top of Word64, where a sum in the timestamp's own type would wrap + placeOrder ctx aliceOrder {oTimestamp = maxBound} `shouldReturn` refusedWith 400 "bad_request" + fst <$> placeOrder ctx (at (-600)) `shouldReturn` 200 + +-- | The provider is slow, so without the name's turn both orders would pass the existing-order check and create an invoice. +testConcurrentOrders :: IO () +testConcurrentOrders = withOrdersVia (\p -> p {pCreateInvoice = \m d -> threadDelay 300000 >> pCreateInvoice p m d}) $ \ctx -> do + (a, b) <- concurrently (placeOrder ctx aliceOrder) (placeOrder ctx aliceOrder {oCommitment = commitmentOf 2}) + sort [fst a, fst b] `shouldBe` [200, 409] + filter ((== 409) . fst) [a, b] `shouldBe` [refusedWith 409 "order_exists"] + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + length <$> stubCalls (cStub ctx) `shouldReturn` 1 + +-- | Two names take two turns, and both creates are let through together, so both orders reach the insert. +testLostInsertCancels :: IO () +testLostInsertCancels = do + arrived <- newTVarIO (0 :: Int) + let meet = atomically (modifyTVar' arrived (+ 1)) >> atomically (readTVar arrived >>= check . (>= 2)) + withOrdersVia (\p -> p {pCreateInvoice = \m d -> meet >> pCreateInvoice p m d}) $ \ctx -> do + (a, b) <- concurrently (placeOrder ctx aliceOrder) (placeOrder ctx aliceOrder {oLabel = "dynamis", oPrice = 20000}) + sort [fst a, fst b] `shouldBe` [200, 409] + filter ((== 409) . fst) [a, b] `shouldBe` [refusedWith 409 "order_exists"] + [(_, winner)] <- pure $ filter ((== 200) . fst) [a, b] + Just InvoiceRow {irProviderRef = won} <- getInvoice (cStore ctx) (InvoiceId $ textOf "invoiceId" winner) + calls <- stubCalls (cStub ctx) + let created = length [() | StubCreate {} <- calls] + cancelled = [r | StubCancel r <- calls] + created `shouldBe` 2 + length cancelled `shouldBe` 1 + cancelled `shouldNotBe` [won] + +-- | The stub gives every invoice one ref, so the second order's insert breaks the unique ref. +testInsertFailureCancels :: IO () +testInsertFailureCancels = withOrdersVia (\p -> p {pCreateInvoice = \m d -> fmap (\i -> i {piProviderRef = "pref-same"}) <$> pCreateInvoice p m d}) $ \ctx -> do + void $ placed ctx aliceOrder + -- a listener that answers the handler's exception instead of rethrowing it into the test + mgr <- newManager defaultManagerSettings + logs <- Warp.withApplication (pure $ webApp $ cWeb ctx) $ \port -> do + let answering = ctx {cClient = (mgr, "http://127.0.0.1:" <> show port)} + capturingStderr $ fst <$> placeOrder answering aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} `shouldReturn` 500 + logs `shouldSatisfy` T.isInfixOf "invoice pref-same is cancelled at the provider" + calls <- stubCalls (cStub ctx) + [r | StubCancel r <- calls] `shouldBe` ["pref-same"] + +testDurationSlack :: IO () +testDurationSlack = withOrders $ \ctx -> do + let slack = fromIntegral slackSeconds + withDuration n d = aliceOrder {oCommitment = commitmentOf n, oDuration = d} + early = durationAt orderTime + placeOrder ctx (withDuration 2 (early + slack + 1)) `shouldReturn` refusedWith 400 "bad_request" + placeOrder ctx (withDuration 3 (early - slack - 1)) `shouldReturn` refusedWith 400 "bad_request" + fst <$> placeOrder ctx (withDuration 4 (early + slack)) `shouldReturn` 200 + -- after the term start no free days are left, and fewer than the paid years are refused + let later = addUTCTime 86400 termStart + afterStart n d = (withDuration n d) {oLabel = "dynamis", oPrice = 20000, oTimestamp = seconds later} + writeIORef (cClock ctx) later + durationAt later `shouldBe` 2 * yearSeconds + placeOrder ctx (afterStart 5 (2 * yearSeconds - 1)) `shouldReturn` refusedWith 400 "bad_request" + placeOrder ctx (afterStart 6 (2 * yearSeconds + slack + 1)) `shouldReturn` refusedWith 400 "bad_request" + fst <$> placeOrder ctx (afterStart 7 (2 * yearSeconds + slack)) `shouldReturn` 200 + +testCatalogChanged :: IO () +testCatalogChanged = withOrders $ \ctx -> do + placeOrder ctx aliceOrder {oPrice = 9000} `shouldReturn` (409, Just $ KM.fromList [("error", "catalog_changed"), ("price", J.Number 10000)]) + stubCalls (cStub ctx) `shouldReturn` [] + +testNameTaken :: IO () +testNameTaken = withOrders $ \ctx@Ctx {cNode = node} -> do + answerCall node fakeController reservedNamesCall $ Returns $ uint256 (1 :: Int) + placeOrder ctx aliceOrder `shouldReturn` refusedWith 409 "name_taken" + answerCall node fakeController reservedNamesCall $ Returns $ uint256 (0 :: Int) + answerRegistered node (cResolverOf node) + answerCall node (registrarOf node) nameExpiresCall $ Returns $ uint256 (seconds orderTime + yearSeconds) + placeOrder ctx aliceOrder `shouldReturn` refusedWith 409 "name_taken" + answerCall node (registrarOf node) nameExpiresCall $ Returns $ uint256 (0 :: Int) + -- another wallet's order of the name being registered here + Right () <- placePaid (cStore ctx) "other" ownerB (order "alicebakery" 9) (addUTCTime 3600 t0) + other <- withTransaction (cStore ctx) $ \db -> fromJust <$> getRegistration db (RegistrationId "other") + Written _ <- withTransaction (cStore ctx) $ \db -> updateRegistration db other {nrPhase = PRegistering} t0 + placeOrder ctx aliceOrder `shouldReturn` refusedWith 409 "name_taken" + stubCalls (cStub ctx) `shouldReturn` [] + where + cResolverOf = cResolver . fakeChainConfig + +testNameCheckUnavailable :: IO () +testNameCheckUnavailable = withOrders $ \ctx@Ctx {cNode = node} -> do + setFault node "eth_getBlockByNumber" HttpFailure + placeOrder ctx aliceOrder `shouldReturn` refusedWith 503 "name_check_unavailable" + clearFault node "eth_getBlockByNumber" + answerCall node fakeController (rentPriceCall 11) $ Reverts "" + placeOrder ctx aliceOrder `shouldReturn` refusedWith 503 "name_check_unavailable" + stubCalls (cStub ctx) `shouldReturn` [] + +testSizes :: IO () +testSizes = withOrders $ \ctx -> do + missing <- KM.delete "sealed" <$> signedBody (cRandom ctx) aliceOrder + postOrder ctx missing `shouldReturn` refusedWith 400 "bad_request" + placeOrder ctx aliceOrder {oSealed = T.replicate 11096 "a"} `shouldReturn` refusedWith 400 "bad_request" + padded <- KM.insert "padding" (J.String $ T.replicate 16384 "a") <$> signedBody (cRandom ctx) aliceOrder + postOrder ctx padded `shouldReturn` refusedWith 400 "bad_request" + longest <- signedBody (cRandom ctx) aliceOrder {oSealed = T.replicate 11095 "a"} + LB.length (J.encode longest) `shouldSatisfy` (< 16384) + fst <$> postOrder ctx longest `shouldReturn` 200 + +testFormats :: IO () +testFormats = withOrders $ \ctx -> do + good <- signedBody (cRandom ctx) aliceOrder + let with k v = KM.insert k v good + lowerOwner = J.String $ T.toLower ownerAText + forM_ + [ with "labelHash" (J.String $ T.toUpper $ hex $ (\(LabelHash h) -> h) alice), + with "commitment" (J.String "0x1234"), + with "owner" lowerOwner, + with "method" "eth", + with "price" (J.Number 100.5), + with "duration" (J.Number (-1)), + with "timestamp" "1792574100", + with "length" (J.Number 1e30), + KM.delete "kind" good, + with "kind" "badge" + ] + $ \body -> postOrder ctx body `shouldReturn` refusedWith 400 "bad_request" + forM_ [5, 64] $ \len -> postOrder ctx (with "length" (J.Number len)) `shouldReturn` refusedWith 400 "invalid_name" + stubCalls (cStub ctx) `shouldReturn` [] + +testTwoWallets :: IO () +testTwoWallets = withOrders $ \ctx -> do + (a, _) <- placed ctx aliceOrder + (b, _) <- placed ctx aliceOrder {oOwner = ownerBText, oKey = keyB, oCommitment = commitmentOf 2} + a `shouldNotBe` b + mapM (fmap nrLive . invoiceRegistration ctx) [a, b] `shouldReturn` [True, True] + +-- code orders + +-- | A prepaid code as the operator issues it, with its text as the page sends it. +issueCode :: Ctx -> Int -> Int -> (NewPrepaidCode -> NewPrepaidCode) -> IO (Text, PrepaidCode) +issueCode Ctx {cStore, cRandom} len years change = do + code <- randomNamePrepaidCode cRandom len + let new = change NewPrepaidCode {npcCodeHash = namePrepaidCodeHash code, npcMinLength = len, npcYears = years, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing} + stored <- withTransaction cStore $ \db -> + insertPrepaidCode db new orderTime >>= \case + Right codeId -> fromJust <$> getPrepaidCodeById db codeId + Left e -> error $ "code not issued: " <> show e + pure (formatNameCode $ NCPrepaid code, stored) + +unissuedCode :: Ctx -> Int -> IO Text +unissuedCode Ctx {cRandom} len = formatNameCode . NCPrepaid <$> randomNamePrepaidCode cRandom len + +readCode :: Ctx -> PrepaidCode -> IO PrepaidCode +readCode Ctx {cStore} PrepaidCode {pcId} = withTransaction cStore $ \db -> fromJust <$> getPrepaidCodeById db pcId + +-- | The body of /api/name/register: the order's fields and the code, without a kind, method or price. +signedCodeBody :: Ctx -> Text -> Order -> IO J.Object +signedCodeBody ctx code o = KM.insert "code" (J.String code) . KM.delete "kind" . KM.delete "method" . KM.delete "price" <$> signedBody (cRandom ctx) o + +postRegister :: Ctx -> J.Object -> IO (Int, Maybe J.Object) +postRegister ctx = post ctx "/api/name/register" . J.encode + +register :: Ctx -> Text -> Order -> IO (Int, Maybe J.Object) +register ctx code o = signedCodeBody ctx code o >>= postRegister ctx + +-- | A code order's registration id, failing the test unless the order was accepted with that alone. +registered :: Ctx -> Text -> Order -> IO RegistrationId +registered ctx code o = + register ctx code o >>= \case + (200, Just v) | [("registrationId", J.String rid)] <- KM.toList v -> pure $ RegistrationId rid + other -> error $ "code order refused: " <> show other + +registrationAnswer :: RegistrationId -> (Int, Maybe J.Object) +registrationAnswer (RegistrationId rid) = (200, Just $ KM.fromList [("registrationId", J.String rid)]) + +registrationById :: Ctx -> RegistrationId -> IO NameRegistration +registrationById Ctx {cStore} rid = withTransaction cStore $ \db -> fromJust <$> getRegistration db rid + +testCodeOrder :: IO () +testCodeOrder = withOrders $ \ctx -> do + (code, issued@PrepaidCode {pcId, pcCodeHash}) <- issueCode ctx 8 2 id + rid <- registered ctx code aliceOrder + r <- registrationById ctx rid + (nrPhase r, nrLive r, nrVersion r, nrYears r, nrPrice r, nrPayment r) `shouldBe` (PQueued, True, 1, 2, 0, RPCode pcId) + (nrOwner r, nrDuration r, nrCommitment r, nrSealed r) `shouldBe` (ownerA, durationAt orderTime, oCommitment aliceOrder, oSealed aliceOrder) + nrOrderHash r `shouldBe` orderHash ownerA (fieldsOf aliceOrder) (OTCode pcCodeHash) + held <- readCode ctx issued + (pcReservedBy held, pcReservedAt held, pcSpentAt held) `shouldBe` (Just rid, Just orderTime, Nothing) + stubCalls (cStub ctx) `shouldReturn` [] + +testCodeResend :: IO () +testCodeResend = withOrders $ \ctx -> do + (code, PrepaidCode {pcId}) <- issueCode ctx 8 2 id + body <- signedCodeBody ctx code aliceOrder + rid <- registered ctx code aliceOrder + advance ctx 60 + register ctx code aliceOrder {oTimestamp = seconds orderTime + 60} `shouldReturn` registrationAnswer rid + r <- registrationById ctx rid + now <- readIORef (cClock ctx) + withTransaction (cStore ctx) $ \db -> do + updateRegistration db r {nrPhase = PRegistered} now `shouldReturn` Written 2 + spendPrepaidCode db pcId rid now `shouldReturn` True + advance ctx 3600 + setFault (cNode ctx) "eth_call" HttpFailure + postRegister ctx body `shouldReturn` registrationAnswer rid + register ctx code aliceOrder {oTimestamp = seconds orderTime + 3660} `shouldReturn` registrationAnswer rid + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + +testCodeOneFieldDiffers :: IO () +testCodeOneFieldDiffers = withOrders $ \ctx -> do + (code, _) <- issueCode ctx 7 2 id + (other, otherCode) <- issueCode ctx 8 2 id + void $ registered ctx code aliceOrder + let variants = + [ (code, aliceOrder {oLabel = "dynamis"}), + (code, aliceOrder {oCommitment = commitmentOf 2}), + (code, aliceOrder {oSealed = "sealed-alicebakery-2"}), + (code, aliceOrder {oDuration = oDuration aliceOrder + 1}), + (other, aliceOrder) + ] + forM_ variants $ \(c, o) -> register ctx c o `shouldReturn` refusedWith 409 "order_exists" + -- the same fields as a paid order are another order + placeOrder ctx aliceOrder `shouldReturn` refusedWith 409 "order_exists" + pcReservedBy <$> readCode ctx otherCode `shouldReturn` Nothing + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + +testCodeBadSignature :: IO () +testCodeBadSignature = withOrders $ \ctx -> do + (code, issued) <- issueCode ctx 8 2 id + register ctx code aliceOrder {oKey = keyB} `shouldReturn` refusedWith 400 "bad_request" + good <- signedCodeBody ctx code aliceOrder + let highS = "0x" <> T.replicate 32 "11" <> "ff" <> T.replicate 31 "11" <> "1b" + forM_ [highS, ""] $ \bad -> postRegister ctx (withSignature bad good) `shouldReturn` refusedWith 400 "bad_request" + postRegister ctx (KM.delete "signature" good) `shouldReturn` refusedWith 400 "bad_request" + unknown <- unissuedCode ctx 8 + register ctx unknown aliceOrder {oKey = keyB} `shouldReturn` refusedWith 400 "code_invalid" + pcReservedBy <$> readCode ctx issued `shouldReturn` Nothing + length <$> registrationsOf ctx ownerA `shouldReturn` 0 + +testCodeStaleTimestamp :: IO () +testCodeStaleTimestamp = withOrders $ \ctx -> do + (code, _) <- issueCode ctx 8 2 id + let at offset = aliceOrder {oTimestamp = fromIntegral (toInteger (seconds orderTime) + offset)} + forM_ [-601, 601] $ \offset -> register ctx code (at offset) `shouldReturn` refusedWith 400 "bad_request" + register ctx code aliceOrder {oTimestamp = maxBound} `shouldReturn` refusedWith 400 "bad_request" + void $ registered ctx code (at (-600)) + +testCodeMeetsInvoice :: IO () +testCodeMeetsInvoice = withOrders $ \ctx -> do + (code, issued) <- issueCode ctx 8 2 id + void $ placed ctx aliceOrder + register ctx code aliceOrder `shouldReturn` refusedWith 409 "order_exists" + register ctx code aliceOrder {oCommitment = commitmentOf 2} `shouldReturn` refusedWith 409 "order_exists" + -- another wallet sending the open order's commitment + register ctx code aliceOrder {oOwner = ownerBText, oKey = keyB} `shouldReturn` refusedWith 409 "order_exists" + pcReservedBy <$> readCode ctx issued `shouldReturn` Nothing + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + length <$> registrationsOf ctx ownerB `shouldReturn` 0 + +testCodeClosesOverdue :: IO () +testCodeClosesOverdue = withOrders $ \ctx -> do + (code, _) <- issueCode ctx 8 2 id + (short, _) <- issueCode ctx 12 2 id + (oldInv, _) <- placed ctx aliceOrder {oMethod = "card"} + -- past the card invoice's 60 minutes, before any sweep + advance ctx (61 * 60) + now <- readIORef (cClock ctx) + let next = aliceOrder {oCommitment = commitmentOf 2, oTimestamp = seconds now, oDuration = durationAt now} + register ctx short next `shouldReturn` refusedWith 409 "code_not_covering" + nrLive <$> invoiceRegistration ctx oldInv `shouldReturn` True + rid <- registered ctx code next + Just oldRow <- getInvoice (cStore ctx) oldInv + irStatus oldRow `shouldBe` ISExpired + old <- invoiceRegistration ctx oldInv + (nrPhase old, nrLive old, nrVersion old) `shouldBe` (PUnpaid, False, 2) + (nrPhase &&& nrLive) <$> registrationById ctx rid `shouldReturn` (PQueued, True) + calls <- stubCalls (cStub ctx) + [c | c@StubCancel {} <- calls] `shouldBe` [StubCancel (irProviderRef oldRow)] + +testCodeRefusals :: IO () +testCodeRefusals = withOrders $ \ctx@Ctx {cStore} -> do + unknown <- unissuedCode ctx 8 + let typo = T.init unknown <> (if T.last unknown == '0' then "1" else "0") + ownerCode = "SN-DWPHS-6JBFR-ZR1MA-T5HQ9-P17QM-F5GM5" + forM_ [unknown, typo, ownerCode, "not a code"] $ \c -> register ctx c aliceOrder `shouldReturn` refusedWith 400 "code_invalid" + (revoked, revokedCode) <- issueCode ctx 8 2 id + withTransaction cStore $ \db -> void $ revokePrepaidCode db (pcCodeHash revokedCode) orderTime + register ctx revoked aliceOrder `shouldReturn` refusedWith 400 "code_invalid" + (reserved, reservedCode) <- issueCode ctx 8 2 id + placeCode cStore "holder" ownerB (order "holderone" 1) reservedCode `shouldReturn` Right () + register ctx reserved aliceOrder `shouldReturn` refusedWith 409 "code_reserved" + (used, usedCode) <- issueCode ctx 8 2 id + placeCode cStore "spender" ownerB (order "spendertwo" 2) usedCode `shouldReturn` Right () + withTransaction cStore (\db -> spendPrepaidCode db (pcId usedCode) (RegistrationId "spender") t0) `shouldReturn` True + register ctx used aliceOrder `shouldReturn` refusedWith 409 "code_used" + (expired, _) <- issueCode ctx 8 2 $ \c -> c {npcExpiresAt = Just orderTime} + register ctx expired aliceOrder `shouldReturn` refusedWith 409 "code_expired" + (unpaid, _) <- issueCode ctx 8 2 $ \c -> c {npcPaymentStatus = CPSUnpaid} + register ctx unpaid aliceOrder `shouldReturn` refusedWith 409 "payment_pending" + (long, _) <- issueCode ctx 12 2 id + register ctx long aliceOrder `shouldReturn` refusedWith 409 "code_not_covering" + (longUnpaid, _) <- issueCode ctx 12 2 $ \c -> c {npcPaymentStatus = CPSUnpaid} + register ctx longUnpaid aliceOrder `shouldReturn` refusedWith 409 "payment_pending" + length <$> registrationsOf ctx ownerA `shouldReturn` 0 + +testCodeYears :: IO () +testCodeYears = withOrders $ \ctx -> do + (code, _) <- issueCode ctx 8 5 id + register ctx code aliceOrder `shouldReturn` refusedWith 400 "bad_request" + rid <- registered ctx code aliceOrder {oDuration = yearsDuration 5 orderTime} + (nrYears &&& nrDuration) <$> registrationById ctx rid `shouldReturn` (5, yearsDuration 5 orderTime) + +testCodeNameChecks :: IO () +testCodeNameChecks = withOrders $ \ctx@Ctx {cNode = node} -> do + (code, issued) <- issueCode ctx 8 2 id + (long, _) <- issueCode ctx 12 2 id + let unreserved = pcReservedBy <$> readCode ctx issued `shouldReturn` Nothing + answerCall node fakeController reservedNamesCall $ Returns $ uint256 (1 :: Int) + register ctx code aliceOrder `shouldReturn` refusedWith 409 "name_taken" + register ctx long aliceOrder `shouldReturn` refusedWith 409 "name_taken" + answerCall node fakeController reservedNamesCall $ Returns $ uint256 (0 :: Int) + answerRegistered node (cResolver $ fakeChainConfig node) + answerCall node (registrarOf node) nameExpiresCall $ Returns $ uint256 (seconds orderTime + yearSeconds) + register ctx code aliceOrder `shouldReturn` refusedWith 409 "name_taken" + register ctx long aliceOrder `shouldReturn` refusedWith 409 "name_taken" + unreserved + answerCall node (registrarOf node) nameExpiresCall $ Returns $ uint256 (0 :: Int) + setFault node "eth_call" HttpFailure + register ctx code aliceOrder `shouldReturn` refusedWith 503 "name_check_unavailable" + register ctx long aliceOrder `shouldReturn` refusedWith 503 "name_check_unavailable" + unreserved + length <$> registrationsOf ctx ownerA `shouldReturn` 0 + clearFault node "eth_call" + void $ registered ctx code aliceOrder + +testCodeSizes :: IO () +testCodeSizes = withOrders $ \ctx -> do + (code, _) <- issueCode ctx 8 2 id + register ctx code aliceOrder {oSealed = T.replicate 11096 "a"} `shouldReturn` refusedWith 400 "bad_request" + padded <- KM.insert "padding" (J.String $ T.replicate 16384 "a") <$> signedCodeBody ctx code aliceOrder + postRegister ctx padded `shouldReturn` refusedWith 400 "bad_request" + good <- signedCodeBody ctx code aliceOrder + let with k v = KM.insert k v good + forM_ + [ KM.delete "sealed" good, + KM.delete "code" good, + with "code" (J.Number 8), + with "labelHash" (J.String $ T.toUpper $ hex $ (\(LabelHash h) -> h) alice), + with "owner" (J.String $ T.toLower ownerAText), + with "timestamp" "1792574100" + ] + $ \body -> postRegister ctx body `shouldReturn` refusedWith 400 "bad_request" + forM_ ["[]", "{"] $ \body -> post ctx "/api/name/register" body `shouldReturn` (400, errorOnly "bad_request") + -- the formats and the length come before the code + forM_ [5, 64] $ \len -> postRegister ctx (KM.insert "code" "not a code" $ with "length" (J.Number len)) `shouldReturn` refusedWith 400 "invalid_name" + longest <- signedCodeBody ctx code aliceOrder {oSealed = T.replicate 11095 "a"} + LB.length (J.encode longest) `shouldSatisfy` (< 16384) + fst <$> postRegister ctx longest `shouldReturn` 200 + +-- | Both orders wait while the test holds the code's turn, so neither checks or reserves the code outside it. +testConcurrentCodeOrders :: IO () +testConcurrentCodeOrders = withOrders $ \ctx -> do + (code, issued@PrepaidCode {pcCodeHash}) <- issueCode ctx 7 2 id + entered <- newEmptyMVar + release <- newEmptyMVar + holder <- async $ withCodeTurn (oeTurns $ cOrders ctx) pcCodeHash $ putMVar entered () >> takeMVar release + takeMVar entered + orders <- async $ concurrently (register ctx code aliceOrder) (register ctx code aliceOrder {oLabel = "dynamis", oCommitment = commitmentOf 2}) + timeout 300000 (wait orders) >>= (`shouldSatisfy` isNothing) + pcReservedBy <$> readCode ctx issued `shouldReturn` Nothing + putMVar release () + wait holder + (a, b) <- wait orders + sort [fst a, fst b] `shouldBe` [200, 409] + filter ((== 409) . fst) [a, b] `shouldBe` [refusedWith 409 "code_reserved"] + [(_, winner)] <- pure $ filter ((== 200) . fst) [a, b] + pcReservedBy <$> readCode ctx issued `shouldReturn` Just (RegistrationId $ textOf "registrationId" winner) + length <$> registrationsOf ctx ownerA `shouldReturn` 1 + +-- | A writer outside this process, which takes no turn here, reserves or revokes the code between the check and the insert. +testCodeNotReservable :: IO () +testCodeNotReservable = withOrders $ \ctx@Ctx {cStore} -> do + (_, reservedLate) <- issueCode ctx 8 2 id + placeCode cStore "outsider" ownerB (order "holderone" 1) reservedLate `shouldReturn` Right () + (_, revokedLate) <- issueCode ctx 8 2 id + withTransaction cStore $ \db -> void $ revokePrepaidCode db (pcCodeHash revokedLate) orderTime + req <- fromJust . Orders.parseOrderRequest <$> signedBody (cRandom ctx) aliceOrder + let new = NewRegistration {nrgId = RegistrationId "late", nrgOwner = ownerA, nrgOrder = fieldsOf aliceOrder, nrgYears = 2} + forM_ [(reservedLate, CRReserved), (revokedLate, CRInvalid)] $ \(stale, refusal) -> do + placedOrder <- Orders.placeOrder (cOrders ctx) req (Orders.codeOrderInsert new stale) + either Just (const Nothing) placedOrder `shouldBe` Just (Orders.ORCode refusal) + length <$> registrationsOf ctx ownerA `shouldReturn` 0 + +-- | One client's code checks and code orders share its 10 a minute; another client is not limited by them. +testCodeLimitShared :: IO () +testCodeLimitShared = withOrders $ \ctx@Ctx {cClient} -> do + (code, _) <- issueCode ctx 8 2 id + body <- J.encode <$> signedCodeBody ctx code aliceOrder + let from path b = statusOf <$> webRequestBody cClient "POST" path [(hContentType, "application/json"), ("X-Forwarded-For", "203.0.113.7")] b + codeCheck = from "/api/code/check" (J.encode $ J.object ["code" .= code]) + codeOrder = from "/api/name/register" body + sequence (concat $ replicate 5 [codeCheck, codeOrder]) `shouldReturn` replicate 10 200 + codeCheck `shouldReturn` 429 + codeOrder `shouldReturn` 429 + fst <$> register ctx code aliceOrder `shouldReturn` 200 + +-- | A response held before it is written, as for a client that reads it slowly, must not hold the name's turn. +testSlowReaderHoldsNoTurn :: IO () +testSlowReaderHoldsNoTurn = withOrders $ \ctx@Ctx {cWeb, cOrders} -> do + (codeA, _) <- issueCode ctx 7 2 id + (codeB, _) <- issueCode ctx 7 2 id + writing <- newEmptyMVar + release <- newEmptyMVar + let app = ordersApp cWeb cOrders + slow req respond + | isJust (lookup slowReader $ requestHeaders req) = app req $ \r -> putMVar writing () >> takeMVar release >> respond r + | otherwise = app req respond + mgr <- newManager defaultManagerSettings + Warp.testWithApplication (pure slow) $ \port -> do + let c = ctx {cClient = (mgr, "http://127.0.0.1:" <> show port)} + slowPost path body = statusOf <$> webRequestBody (cClient c) "POST" path [(hContentType, "application/json"), (slowReader, "1")] (J.encode body) + whileHeld first second = do + held <- async first + takeMVar writing + timeout 2000000 second >>= (`shouldSatisfy` maybe False ((== 200) . fst)) + putMVar release () + wait held `shouldReturn` 200 + byB o = o {oOwner = ownerBText, oKey = keyB, oCommitment = commitmentOf 2} + whileHeld (signedBody (cRandom c) aliceOrder >>= slowPost "/api/invoice") (placeOrder c $ byB aliceOrder) + let dynamis = aliceOrder {oLabel = "dynamis", oCommitment = commitmentOf 3} + whileHeld (signedCodeBody c codeA dynamis >>= slowPost "/api/name/register") (register c codeB (byB dynamis) {oCommitment = commitmentOf 4}) + where + slowReader = "x-slow-reader" + +testOrderAfterCancel :: IO () +testOrderAfterCancel = withOrders $ \ctx -> do + (invId@(InvoiceId invText), _) <- placed ctx aliceOrder + (status, v) <- post ctx ("/api/invoice/" <> T.unpack invText <> "/cancel") "" + status `shouldBe` 200 + (field "status" =<< v) `shouldBe` Just "expired" + (field "version" . registrationOf =<< v) `shouldBe` Just (J.Number 2) + old <- invoiceRegistration ctx invId + nrLive old `shouldBe` False + (newInvId, _) <- placed ctx aliceOrder {oCommitment = commitmentOf 2, oTimestamp = seconds orderTime + 1} + newInvId `shouldNotBe` invId + nrLive <$> invoiceRegistration ctx newInvId `shouldReturn` True + +testOverdueClosed :: IO () +testOverdueClosed = withOrders $ \ctx -> do + (oldInv, _) <- placed ctx aliceOrder {oMethod = "card"} + -- a card invoice expires after the session's 60 minutes; this is past it, before any sweep + advance ctx (61 * 60) + now <- readIORef (cClock ctx) + -- a refused order closes nothing + placeOrder ctx aliceOrder {oCommitment = commitmentOf 2, oTimestamp = seconds now} `shouldReturn` refusedWith 400 "bad_request" + nrLive <$> invoiceRegistration ctx oldInv `shouldReturn` True + (newInv, _) <- placed ctx aliceOrder {oMethod = "card", oCommitment = commitmentOf 2, oTimestamp = seconds now, oDuration = durationAt now} + Just oldRow <- getInvoice (cStore ctx) oldInv + irStatus oldRow `shouldBe` ISExpired + old <- invoiceRegistration ctx oldInv + (nrPhase old, nrLive old, nrVersion old) `shouldBe` (PUnpaid, False, 2) + nrLive <$> invoiceRegistration ctx newInv `shouldReturn` True + calls <- stubCalls (cStub ctx) + [c | c@StubCancel {} <- calls] `shouldBe` [StubCancel (irProviderRef oldRow)] + +testLatePaymentAfterNewOrder :: IO () +testLatePaymentAfterNewOrder = withOrders $ \ctx -> do + (oldInv, _) <- placed ctx aliceOrder + advance ctx (46 * 60) + now <- readIORef (cClock ctx) + (newInv, _) <- placed ctx aliceOrder {oCommitment = commitmentOf 2, oTimestamp = seconds now, oDuration = durationAt now} + logs <- capturingStderr $ settleNow ctx oldInv settled `shouldReturn` Right ISPaid + logs `shouldSatisfy` T.isInfixOf "refund" + old <- invoiceRegistration ctx oldInv + (nrPhase old, nrLive old) `shouldBe` (PUnpaid, False) + new <- invoiceRegistration ctx newInv + (nrPhase new, nrLive new, nrVersion new) `shouldBe` (PUnpaid, True, 1) + settleNow ctx newInv settled `shouldReturn` Right ISPaid + (nrPhase <$> invoiceRegistration ctx newInv) `shouldReturn` PQueued + +-- | Paid in full at minute 44 of 45 and confirming after; a second tab's order at minute 46 must not refund it. +testFundedOverdueKept :: IO () +testFundedOverdueKept = withOrders $ \ctx -> do + (inv, _) <- placed ctx aliceOrder + advance ctx (44 * 60) + settleNow ctx inv (SigFunded (rcv 10000 (Just "0.00156")) PaidInFull) `shouldReturn` Right ISOpen + advance ctx (2 * 60) + now <- readIORef (cClock ctx) + placeOrder ctx aliceOrder {oCommitment = commitmentOf 2, oTimestamp = seconds now, oDuration = durationAt now} `shouldReturn` refusedWith 409 "order_exists" + r <- invoiceRegistration ctx inv + (nrLive r, nrPhase r) `shouldBe` (True, PUnpaid) + withTransaction (cStore ctx) (\db -> closeOverdue db r now) `shouldReturn` Nothing + (irStatus <$>) <$> getInvoice (cStore ctx) inv `shouldReturn` Just ISOpen + settleNow ctx inv settled `shouldReturn` Right ISPaid + nrPhase <$> invoiceRegistration ctx inv `shouldReturn` PQueued + +-- | The real BTCPay adapter against the fake: a payment into an expired invoice reads Expired with PaidLate, +-- into a cancelled one Invalid, and into one only we closed New and paid in part. +testLateBTCPayPayments :: IO () +testLateBTCPayPayments = withFakeBTCPay $ \fake -> do + btcpay <- btcpayProvider (fbConfig fake) + withOrdersVia (const btcpay) $ \ctx@Ctx {cStore = st, cWeb} -> do + poller <- newPollerEnv st (weWaiters cWeb) (Just $ oePhases $ cOrders ctx) (weHints cWeb) (weProviders cWeb) (poll $ weConfig cWeb) + let refOf inv = maybe (error "no invoice") irProviderRef <$> getInvoice st inv + readAt ref = queueReadHint (weHints cWeb) ref >> drainHints poller + paidLate ref fields = capturingStderr $ setInvoiceState fake ref fields >> readAt ref + alerted logs = T.isInfixOf "stays unpaid; refund the payment" logs + later = addUTCTime 3600 orderTime + (expired, _) <- placed ctx aliceOrder + expiredRef <- refOf expired + setInvoiceState fake expiredRef ["status" .= ("Expired" :: Text)] + readAt expiredRef + (irStatus <$>) <$> getInvoice st expired `shouldReturn` Just ISExpired + paidLate expiredRef ["additionalStatus" .= ("PaidLate" :: Text), "paymentMethodPaid" .= ("0.00050000" :: Text)] >>= (`shouldSatisfy` alerted) + capturingStderr (readAt expiredRef) >>= (`shouldNotSatisfy` alerted) + (cancelled, _) <- placed ctx aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + cancelledRef <- refOf cancelled + cancelOpenInvoice st (weWaiters cWeb) (Just $ oePhases $ cOrders ctx) cancelled later `shouldReturn` True + paidLate cancelledRef ["status" .= ("Invalid" :: Text), "paymentMethodPaid" .= ("0.00078000" :: Text)] >>= (`shouldSatisfy` alerted) + (swept, _) <- placed ctx aliceOrder {oOwner = ownerBText, oKey = keyB, oCommitment = commitmentOf 3} + sweptRef <- refOf swept + expireOverdue st (weWaiters cWeb) (Just $ oePhases $ cOrders ctx) later later [swept] `shouldReturn` [swept] + paidLate sweptRef ["paymentMethodPaid" .= ("0.00025000" :: Text)] >>= (`shouldSatisfy` alerted) + forM_ [expired, cancelled, swept] $ \inv -> do + r <- invoiceRegistration ctx inv + (nrPhase r, nrLive r) `shouldBe` (PUnpaid, False) + +-- | Paid in part while open, then expired by BTCPay with the same figures: one refund line, none from a restarted poller. +testOneLateAlert :: IO () +testOneLateAlert = withOrders $ \ctx@Ctx {cStore = st, cWeb} -> do + (inv, _) <- placed ctx aliceOrder + Just InvoiceRow {irProviderRef = ref} <- getInvoice st inv + let paid = rcv 5000 (Just "0.00078") + newPoller = newPollerEnv st (weWaiters cWeb) (Just $ oePhases $ cOrders ctx) (weHints cWeb) (weProviders cWeb) (poll $ weConfig cWeb) + readWith poller = capturingStderr $ queueReadHint (weHints cWeb) ref >> drainHints poller + refunds = length . T.breakOnAll "refund" + poller <- newPoller + setSignals (cStub ctx) [(ref, SigFunded paid PaidInPart)] + refunds <$> readWith poller `shouldReturn` 0 + setSignals (cStub ctx) [(ref, SigClosed paid PaidInPart)] + logs <- readWith poller + refunds logs `shouldBe` 1 + logs `shouldSatisfy` T.isInfixOf "stays unpaid; refund the payment" + restarted <- newPoller + refunds <$> readWith restarted `shouldReturn` 0 + (nrPhase &&& nrLive) <$> invoiceRegistration ctx inv `shouldReturn` (PUnpaid, False) + +testCancelAnswers :: IO () +testCancelAnswers = withOrders $ \ctx -> do + let cancel = cancelAt ctx + (funded, _) <- placed ctx aliceOrder + settleNow ctx funded (SigFunded (rcv 5000 (Just "0.00078")) PaidInPart) `shouldReturn` Right ISOpen + cancel funded `shouldReturn` refusedWith 409 "funded" + (other, _) <- placed ctx aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + fst <$> cancel other `shouldReturn` 200 + cancel other `shouldReturn` refusedWith 409 "not_open" + cancel (InvoiceId "no-such-invoice") `shouldReturn` refusedWith 404 "not_found" + -- the provider settles the invoice, or reads a payment into it, while taking the cancel + withOrdersCancelling settled $ \ctx' -> do + (invId, _) <- placed ctx' aliceOrder + (status, v) <- cancelAt ctx' invId + (status, field "status" =<< v, field "phase" . registrationOf =<< v) `shouldBe` (200, Just "paid", Just "queued") + withOrdersCancelling (SigFunded (rcv 0 Nothing) PaidInFull) $ \ctx' -> do + (invId, _) <- placed ctx' aliceOrder + (status, v) <- cancelAt ctx' invId + (status, field "status" =<< v, field "paidInFull" =<< v) `shouldBe` (200, Just "expired", Just (J.Bool True)) + r <- invoiceRegistration ctx' invId + (nrLive r, nrVersion r) `shouldBe` (False, 2) + where + cancelAt c (InvoiceId t) = post c ("/api/invoice/" <> T.unpack t <> "/cancel") "" + +-- | Cancel takes the invoice row and decides on the payment it then reads, as settlement does. +testCancelAlertsFunded :: IO () +testCancelAlertsFunded = withOrdersCancelling (SigFunded paid PaidInPart) $ \ctx -> do + (invId, _) <- placed ctx aliceOrder + logs <- capturingStderr $ do + (status, v) <- post ctx ("/api/invoice/" <> T.unpack (invoiceText invId) <> "/cancel") "" + (status, field "status" =<< v, field "cryptoAmountPaid" =<< v) `shouldBe` (200, Just "expired", Just "0.00078") + length (T.breakOnAll (alertOf invId) logs) `shouldBe` 1 + capturingStderr (settleNow ctx invId (SigClosed paid PaidInPart) `shouldReturn` Right ISExpired) >>= (`shouldNotSatisfy` T.isInfixOf (alertOf invId)) + (nrPhase &&& nrLive) <$> invoiceRegistration ctx invId `shouldReturn` (PUnpaid, False) + where + paid = rcv 5000 (Just "0.00078") + invoiceText (InvoiceId t) = t + alertOf invId = "invoice " <> invoiceText invId <> " was paid after its order closed" + +testNamesOff :: IO () +testNamesOff = withOrders $ \ctx -> do + (invId@(InvoiceId t), _) <- placed ctx aliceOrder + mgr <- newManager defaultManagerSettings + Warp.testWithApplication (pure $ webApp (cWeb ctx) {weOrders = Nothing}) $ \port -> do + let off = ctx {cClient = (mgr, "http://127.0.0.1:" <> show port)} + logs <- capturingStderr $ do + r <- webGet (cClient off) ("/api/invoice/" <> T.unpack t) + (statusOf r, J.decode (responseBody r)) `shouldBe` refusedWith 503 "provider_unavailable" + post off ("/api/invoice/" <> T.unpack t <> "/cancel") "" `shouldReturn` refusedWith 503 "provider_unavailable" + logs `shouldSatisfy` T.isInfixOf ("invoice " <> t <> " is a name's, and this service sells no names") + calls <- stubCalls (cStub ctx) + [c | c@StubCancel {} <- calls] `shouldBe` [] + (irStatus <$>) <$> getInvoice (cStore ctx) invId `shouldReturn` Just ISOpen + +testInvoiceCodesOnly :: IO () +testInvoiceCodesOnly = withOrders $ \open -> withSettings codesOnly open $ \ctx -> do + placeOrder ctx aliceOrder `shouldReturn` refusedWith 403 "codes_only" + postOrder ctx (KM.fromList [("kind", "name")]) `shouldReturn` refusedWith 403 "codes_only" + stubCalls (cStub ctx) `shouldReturn` [] + registrationsOf ctx ownerA `shouldReturn` [] + -- nothing the refusals left meets the same order placed with codes_only off + void $ placed open aliceOrder + +testBadgeCodesOnly :: IO () +testBadgeCodesOnly = withOrders $ \open -> withSettings codesOnly open $ \ctx@Ctx {cStore} -> do + seedCheckoutCatalog cStore + (status, view) <- post ctx "/api/invoice" (createBody supporterPriceId Nothing "btc" (codeHashText sampleCode)) + (status, field "badgeType" =<< view) `shouldBe` (200, Just "supporter") + stubCalls (cStub ctx) >>= \case + [StubCreate method OrderDraft {odReference}] -> (method, T.isPrefixOf "badge:" odReference) `shouldBe` (btc, True) + other -> expectationFailure ("provider calls: " <> show other) + +-- | The provider's cancel settles the invoice with the signal before it answers. +withOrdersCancelling :: PaymentSignal -> (Ctx -> IO a) -> IO a +withOrdersCancelling signal action = withOrders $ \ctx -> do + let Ctx {cWeb} = ctx + cancelling p = p {pCancelInvoice = \ref -> settleRef ctx ref >> pCancelInvoice p ref} + web = cWeb {weProviders = map cancelling (weProviders cWeb)} + mgr <- newManager defaultManagerSettings + Warp.testWithApplication (pure (webApp web)) $ \port -> action ctx {cWeb = web, cClient = (mgr, "http://127.0.0.1:" <> show port)} + where + settleRef ctx ref = + getInvoiceByProviderRef (cStore ctx) ref >>= mapM_ (\InvoiceRow {irInvoiceId} -> settleNow ctx irInvoiceId signal) + +testLatePayment :: IO () +testLatePayment = withOrders $ \ctx -> do + (invId@(InvoiceId t), _) <- placed ctx aliceOrder + fst <$> post ctx ("/api/invoice/" <> T.unpack t <> "/cancel") "" `shouldReturn` 200 + closed <- invoiceRegistration ctx invId + logs <- capturingStderr $ settleNow ctx invId settled `shouldReturn` Right ISPaid + logs `shouldSatisfy` T.isInfixOf ("registration " <> (\(RegistrationId r) -> r) (nrId closed) <> " stays unpaid") + invoiceRegistration ctx invId `shouldReturn` closed + r <- webGet (cClient ctx) ("/api/invoice/" <> T.unpack t) + v <- jsonObject r + (field "status" v, field "phase" (registrationOf v)) `shouldBe` (Just "paid", Just "unpaid") + +testSettlementQueues :: IO () +testSettlementQueues = withOrders $ \ctx -> do + (invId, _) <- placed ctx aliceOrder + NameRegistration {nrId} <- invoiceRegistration ctx invId + parked <- newEmptyMVar + -- the version read is pinned, so only a publish can wake the waiter + waiter <- async $ awaitChange (oePhases $ cOrders ctx) nrId (putMVar parked () >> pure 1) 1 10000000 + takeMVar parked + settleNow ctx invId settled `shouldReturn` Right ISPaid + timeout 2000000 (wait waiter) `shouldReturn` Just True + r <- invoiceRegistration ctx invId + (nrPhase r, nrLive r, nrVersion r) `shouldBe` (PQueued, True, 2) + +-- | Postgres serializes the payment with a concurrent overdue close only on this row; SQLite shows that it is written. +testSettlementLocksInvoice :: IO () +testSettlementLocksInvoice = withOrders $ \ctx -> do + (inv@(InvoiceId t), _) <- placed ctx aliceOrder + advance ctx 60 + settleNow ctx inv (SigFunded (rcv 10000 (Just "0.00156")) PaidInFull) `shouldReturn` Right ISOpen + now <- readIORef (cClock ctx) + withConnection (cStore ctx) (\db -> DB.query db "SELECT updated_at, status FROM sx_badge_service_invoices WHERE invoice_id = ?" [t]) + `shouldReturn` [(now, "open" :: Text)] + +testExpiryCloses :: IO () +testExpiryCloses = withOrders $ \ctx -> do + (closedByProvider, _) <- placed ctx aliceOrder + settleNow ctx closedByProvider (SigClosed (rcv 0 Nothing) PaidInPart) `shouldReturn` Right ISExpired + r <- invoiceRegistration ctx closedByProvider + (nrPhase r, nrLive r, nrVersion r) `shouldBe` (PUnpaid, False, 2) + (swept, _) <- placed ctx aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + let later = addUTCTime 3600 orderTime + expireOverdue (cStore ctx) (weWaiters $ cWeb ctx) (Just $ oePhases $ cOrders ctx) later later [swept] `shouldReturn` [swept] + s <- invoiceRegistration ctx swept + (nrPhase s, nrLive s, nrVersion s) `shouldBe` (PUnpaid, False, 2) + +testSweepSkipsBadRow :: IO () +testSweepSkipsBadRow = withOrders $ \ctx@Ctx {cStore = st} -> do + (bad, _) <- placed ctx aliceOrder + (good, _) <- placed ctx aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + NameRegistration {nrId = RegistrationId badId} <- invoiceRegistration ctx bad + withConnection st $ \db -> DB.execute db "UPDATE sx_badge_service_name_registrations SET phase = 'lost' WHERE registration_id = ? AND phase = ?" (badId, "unpaid" :: Text) + let later = addUTCTime 3600 orderTime + logs <- capturingStderr $ expireOverdue st (weWaiters $ cWeb ctx) (Just $ oePhases $ cOrders ctx) later later [bad, good] `shouldReturn` [good] + logs `shouldSatisfy` T.isInfixOf ("invoice " <> invoiceText bad) + (irStatus <$>) <$> getInvoice st bad `shouldReturn` Just ISOpen + r <- invoiceRegistration ctx good + (nrLive r, nrVersion r) `shouldBe` (False, 2) + where + invoiceText (InvoiceId t) = t + +-- | The poller reads by the wall clock, so the orders are placed two hours ago by it. +testPassSweepsAroundFailures :: IO () +testPassSweepsAroundFailures = do + unreadable <- newIORef "" + let failing p = p {pReadInvoice = \r -> readIORef unreadable >>= \u -> if r == u then pure (Left $ ProviderError "down") else pReadInvoice p r} + withOrdersVia failing $ \ctx@Ctx {cStore = st, cWeb} -> do + placedAt <- addUTCTime (-7200) <$> getCurrentTime + writeIORef (cClock ctx) placedAt + let at' o = o {oTimestamp = seconds placedAt, oDuration = durationAt placedAt} + refOf inv = maybe (error "no invoice") irProviderRef <$> getInvoice st inv + (poisoned, _) <- placed ctx $ at' aliceOrder + (unread, _) <- placed ctx $ at' aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + (good, _) <- placed ctx $ at' aliceOrder {oOwner = ownerBText, oKey = keyB, oCommitment = commitmentOf 3} + NameRegistration {nrId = RegistrationId poisonedId} <- invoiceRegistration ctx poisoned + withConnection st $ \db -> DB.execute db "UPDATE sx_badge_service_name_registrations SET phase = 'lost' WHERE registration_id = ? AND phase = ?" (poisonedId, "unpaid" :: Text) + poisonedRef <- refOf poisoned + refOf unread >>= writeIORef unreadable + setSignals (cStub ctx) [(poisonedRef, SigClosed (rcv 0 Nothing) PaidInPart)] + poller <- newPollerEnv st (weWaiters cWeb) (Just $ oePhases $ cOrders ctx) (weHints cWeb) (weProviders cWeb) (poll $ weConfig cWeb) + void $ capturingStderr $ runOnePass poller + mapM (fmap (irStatus <$>) . getInvoice st) [poisoned, unread, good] `shouldReturn` map Just [ISOpen, ISOpen, ISExpired] + +testCleanup :: IO () +testCleanup = withOrders $ \ctx -> testCleanupIn ctx + +testCleanupIn :: Ctx -> IO () +testCleanupIn ctx@Ctx {cStore = st} = do + let expired = t0 + due = addUTCTime (unpaidRetention + 1) expired + place rid n = placePaid st rid ownerA (order (T.pack ("alicebakery" <> show n)) n) expired `shouldReturn` Right () + mapM_ (uncurry place) [("gone", 1), ("funded", 2), ("late", 3), ("unread", 4)] + placePaid st "recent" ownerA (order "alicebakery5" 5) (addUTCTime 3600 due) `shouldReturn` Right () + settleOrder st (weWaiters $ cWeb ctx) Nothing (InvoiceId "inv-funded") (SigFunded (rcv 100 (Just "0.00001")) PaidInPart) t0 `shouldReturn` Right ISOpen + setSignals (cStub ctx) [(ref "late", SigClosed (rcv 5000 (Just "0.00078")) PaidInPart)] + failing <- newIORef False + let reading p = p {pReadInvoice = \r -> readIORef failing >>= \f -> if f && r == ref "unread" then pure (Left $ ProviderError "down") else pReadInvoice p r} + providers = map reading (weProviders $ cWeb ctx) + writeIORef failing True + poller <- newPollerEnv st (weWaiters $ cWeb ctx) Nothing (weHints $ cWeb ctx) providers (poll $ weConfig $ cWeb ctx) + logs <- capturingStderr $ cleanupUnpaid poller due + let present rid = isJust <$> withTransaction st (`getRegistration` RegistrationId rid) + mapM present ["gone", "funded", "late", "unread", "recent"] `shouldReturn` [False, True, True, True, True] + isNothing <$> getInvoice st (InvoiceId "inv-gone") `shouldReturn` True + Just InvoiceRow {irPayment = latePayment} <- getInvoice st (InvoiceId "inv-late") + (ipCryptoPaid <$> latePayment) `shouldBe` Just (Just "0.00078") + logs `shouldSatisfy` T.isInfixOf "registration late is kept because invoice inv-late received a payment" + logs `shouldSatisfy` T.isInfixOf "registration unread is kept until the next run" + reads' <- stubReads + sort reads' `shouldBe` [ref "gone", ref "late"] + writeIORef failing False + cleanupUnpaid poller (addUTCTime cleanupInterval due) + mapM present ["unread", "recent"] `shouldReturn` [False, True] + drop (length reads') <$> stubReads `shouldReturn` [ref "unread"] + where + stubReads = (\calls -> [r | StubRead r <- calls]) <$> stubCalls (cStub ctx) + ref rid = "ref-inv-" <> rid + +testCleanupCapped :: IO () +testCleanupCapped = withOrders $ \ctx@Ctx {cStore = st} -> do + let extra = 3 + count = cleanupReadsPerPass + extra + due = addUTCTime (unpaidRetention + 1) t0 + reads' = (\calls -> length [() | StubRead _ <- calls]) <$> stubCalls (cStub ctx) + forM_ [1 .. count] $ \n -> placePaid st ("old" <> tshow n) ownerA (order ("oldname" <> tshow n) (fromIntegral n)) t0 `shouldReturn` Right () + poller <- newPollerEnv st (weWaiters $ cWeb ctx) Nothing (weHints $ cWeb ctx) (weProviders $ cWeb ctx) (poll $ weConfig $ cWeb ctx) + cleanupUnpaid poller due + reads' `shouldReturn` cleanupReadsPerPass + cleanupUnpaid poller (addUTCTime 1 due) + reads' `shouldReturn` count + length <$> registrationsOf ctx ownerA `shouldReturn` 0 + +testProviderRefs :: IO () +testProviderRefs = withServiceStore $ \st -> do + Right () <- placePaid st "r1" ownerA (order "alicebakery" 1) (addUTCTime 3600 t0) + getInvoiceByProviderRef st "ref-inv-r1" >>= \case + Just InvoiceRow {irInvoiceId, irProduct} -> (irInvoiceId, irProduct) `shouldBe` (InvoiceId "inv-r1", IPName (RegistrationId "r1")) + Nothing -> expectationFailure "the name invoice was not found by its ref" + withConnection st $ \db -> + DB.execute + db + "INSERT INTO sx_badge_service_invoices (invoice_id, provider, price, amount, currency, expires_at, status, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?)" + ("badge-1" :: Text, "crypto" :: Text, 500 :: Int, 500 :: Int, "usd" :: Text, t0, "open" :: Text, t0, t0) + withConnection st $ \db -> do + DB.execute db "INSERT INTO sx_badge_service_badge_codes (code_hash, badge_type, months, code_payment_status, created_at) VALUES (?,?,?,?,?)" (DB.Binary ("hash" :: ByteString), "supporter" :: Text, 1 :: Int, "unpaid" :: Text, t0) + DB.execute db "INSERT INTO sx_badge_service_badge_prices (price_id, badge_type, month_price, currency, status, created_at) VALUES (?,?,?,?,?,?)" ("p1" :: Text, "supporter" :: Text, 500 :: Int, "usd" :: Text, "active" :: Text, t0) + DB.execute db "INSERT INTO sx_badge_service_badge_code_invoices (invoice_id, badge_code_id, price_id, provider_ref, created_at) VALUES (?, (SELECT badge_code_id FROM sx_badge_service_badge_codes), ?, ?, ?)" ("badge-1" :: Text, "p1" :: Text, "ref-inv-r1" :: Text, t0) + E.try (getInvoiceByProviderRef st "ref-inv-r1") `shouldReturn` Left (ProviderRefConflict "ref-inv-r1") + +testPollerQueries :: IO () +testPollerQueries = withServiceStore $ \st -> do + Right () <- placePaid st "r1" ownerA (order "alicebakery" 1) (addUTCTime 3600 t0) + unpaidRefs st (addUTCTime (-60) t0) `shouldReturn` [("crypto", "ref-inv-r1")] + map oiProviderRef <$> overdueInvoices st (addUTCTime 7200 t0) `shouldReturn` ["ref-inv-r1"] + overdueInvoices st t0 `shouldReturn` [] + +testFundedAgreement :: IO () +testFundedAgreement = withServiceStore $ \st -> do + waiters <- newWaiters + let now = addUTCTime 3600 t0 + shapes = + [ ("amount", Just (SigFunded (rcv 100 Nothing) PaidInPart), True), + ("crypto", Just (SigFunded (rcv 0 (Just "0.00001")) PaidInPart), True), + ("verdict", Just (SigFunded (rcv 0 Nothing) PaidInFull), True), + ("nothing", Nothing, False) + ] + forM_ (zip [1 ..] shapes) $ \(n, (rid, signal, _)) -> do + placePaid st rid ownerA (order ("fundname" <> rid) n) t0 `shouldReturn` Right () + forM_ signal $ \sig -> void $ settleOrder st waiters Nothing (InvoiceId $ "inv-" <> rid) sig t0 + listed <- map oiInvoiceId <$> overdueInvoices st now + forM_ (zip [1 ..] shapes) $ \(n, (rid, _, funded)) -> do + let o = order ("fundname" <> rid) n + (InvoiceId ("inv-" <> rid) `elem` listed) `shouldBe` not funded + [LiveOrder {loRegistration, loOverdue}] <- withTransaction st $ \db -> getLiveOrders db (ofCommitment o) ownerA (ofLabelHash o) now + (rid, loOverdue) `shouldBe` (rid, not funded) + closed <- withTransaction st $ \db -> closeOverdue db loRegistration now + (rid, closed) `shouldBe` (rid, if funded then Nothing else Just (nrVersion loRegistration + 1)) + +testViewFields :: IO () +testViewFields = withServiceStore $ \st -> withFakeEthNode $ \node -> do + Right () <- placePaid st "r1" ownerA (order "alicebakery" 1) (addUTCTime 3600 t0) + r <- withTransaction st $ \db -> fromJust <$> getRegistration db (RegistrationId "r1") + let v = objectOf $ viewJSON (testNamesConfig node) r ViewReads {vrIntentNonce = Nothing, vrUnread = []} + sort (KM.keys v) + `shouldBe` sort + [ "id", "version", "moves", "linksRefused", "phase", "labelHash", "label", "owner", "commitment", "sealed", "resolver", "sponsor", "chainId", "tld", + "years", "duration", "price", "commitTx", "registerTx", "revealAfter", "revealBefore", "intentNonce", "unread", "expiresAt", "error" + ] + map (`field` v) ["id", "phase", "owner", "tld", "chainId", "linksRefused", "label", "unread", "sponsor"] + `shouldBe` map + Just + [ "r1", + "unpaid", + J.String ownerAText, + "simplex", + J.Number 31337, + J.Bool False, + J.Null, + J.Array mempty, + J.String $ safeDecodeUtf8 $ strEncode fakeSponsor + ] + +testViewReads :: IO () +testViewReads = withServiceStore $ \st -> withFakeEthNode $ \node -> do + g <- C.newRandom + ct <- newContracts TLDSimplex =<< newChainClient g (fakeChainConfig node) + answerCall node fakeSponsor noncesCall $ Returns $ uint256 (4 :: Int) + Right () <- placePaid st "paid" ownerA (order "alicebakery" 1) (addUTCTime 3600 t0) + let inPhase phase = do + r <- withTransaction st $ \db -> fromJust <$> getRegistration db (RegistrationId "paid") + Written _ <- withTransaction st $ \db -> updateRegistration db r {nrPhase = phase} t0 + withTransaction st $ \db -> fromJust <$> getRegistration db (RegistrationId "paid") + committed <- inPhase PCommitted + readViewChain ct committed `shouldReturn` ViewReads (Just 4) [] + taken <- inPhase PTaken + readViewChain ct taken `shouldReturn` ViewReads Nothing [] + +testViewUnread :: IO () +testViewUnread = withServiceStore $ \st -> withFakeEthNode $ \node -> do + g <- C.newRandom + ct <- newContracts TLDSimplex =<< newChainClient g (fakeChainConfig node) + setFault node "eth_call" HttpFailure + Right () <- placePaid st "paid" ownerA (order "alicebakery" 1) (addUTCTime 3600 t0) + r <- withTransaction st $ \db -> fromJust <$> getRegistration db (RegistrationId "paid") + readViewChain ct r {nrPhase = PCommitted} `shouldReturn` ViewReads Nothing ["intentNonce"] + readViewChain ct r {nrPhase = PTaken} `shouldReturn` ViewReads Nothing [] + let v = objectOf $ viewJSON (testNamesConfig node) r {nrPhase = PCommitted} (ViewReads Nothing ["intentNonce"]) + (field "intentNonce" v, field "unread" v) `shouldBe` (Just J.Null, Just (J.toJSON ["intentNonce" :: Text])) diff --git a/tests/Bots/StoreService/NamesSearchTests.hs b/tests/Bots/StoreService/NamesSearchTests.hs index b0e6eb37b7..c7b1abbfb1 100644 --- a/tests/Bots/StoreService/NamesSearchTests.hs +++ b/tests/Bots/StoreService/NamesSearchTests.hs @@ -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 diff --git a/tests/Bots/StoreService/NamesWiringTests.hs b/tests/Bots/StoreService/NamesWiringTests.hs index e02a9eb629..e815c6786f 100644 --- a/tests/Bots/StoreService/NamesWiringTests.hs +++ b/tests/Bots/StoreService/NamesWiringTests.hs @@ -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 diff --git a/tests/Bots/StoreService/WebTests.hs b/tests/Bots/StoreService/WebTests.hs index f9e346c00e..621d0e869c 100644 --- a/tests/Bots/StoreService/WebTests.hs +++ b/tests/Bots/StoreService/WebTests.hs @@ -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)) diff --git a/tests/Test.hs b/tests/Test.hs index 25d19350a6..63049b9d65 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -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