From c7d415f706fe9ec73a1a51ed8920756cdde8308a 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: add the names store and order signatures --- .../src/StoreService/Names/Abi.hs | 5 + .../src/StoreService/Names/Codes.hs | 139 +++- .../src/StoreService/Names/Phases.hs | 65 ++ .../src/StoreService/Names/Signatures.hs | 110 +++ .../src/StoreService/Names/Store.hs | 693 ++++++++++++++++- .../src/StoreService/Names/Turns.hs | 16 +- tests/Bots/StoreService/NamesCodesTests.hs | 337 ++++++++- tests/Bots/StoreService/NamesPhasesTests.hs | 321 ++++++++ .../Bots/StoreService/NamesSignatureTests.hs | 158 ++++ tests/Bots/StoreService/NamesStoreTests.hs | 709 ++++++++++++++++++ 10 files changed, 2529 insertions(+), 24 deletions(-) create mode 100644 apps/simplex-store-service/src/StoreService/Names/Phases.hs create mode 100644 apps/simplex-store-service/src/StoreService/Names/Signatures.hs create mode 100644 tests/Bots/StoreService/NamesPhasesTests.hs create mode 100644 tests/Bots/StoreService/NamesSignatureTests.hs create mode 100644 tests/Bots/StoreService/NamesStoreTests.hs diff --git a/apps/simplex-store-service/src/StoreService/Names/Abi.hs b/apps/simplex-store-service/src/StoreService/Names/Abi.hs index b986283090..983845a517 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Abi.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Abi.hs @@ -17,6 +17,8 @@ module StoreService.Names.Abi decodeWord64, decodeAddress, decodeBool, + decodeBytes32, + decodeBytes, decodeString, ) where @@ -113,6 +115,9 @@ decodeBool ret = 1 -> Right True _ -> Left AENotBool +decodeBytes32 :: ByteString -> Either AbiError ByteString +decodeBytes32 = wordAt 0 + decodeBytes :: ByteString -> Either AbiError ByteString decodeBytes ret = offsetAt 0 ret >>= bytesAt ret diff --git a/apps/simplex-store-service/src/StoreService/Names/Codes.hs b/apps/simplex-store-service/src/StoreService/Names/Codes.hs index 41ecfad94e..5b0d44c03d 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Codes.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Codes.hs @@ -1,26 +1,148 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TupleSections #-} module StoreService.Names.Codes - ( issueNameCode, + ( CodeState (..), + codeState, + codeStateText, + CodeRefusal (..), + codeRefusal, + codeRefusalTag, + reserveRefusal, + readPrepaidCode, + reserveCode, + releaseCode, + spendCode, + issueNameCode, revokeNameCode, ) where +import Control.Applicative ((<|>)) +import qualified Control.Exception as E +import Data.ByteString (ByteString) import Data.Functor ((<&>)) +import Data.Int (Int64) +import Data.Maybe (fromMaybe, isJust) import Data.Text (Text) -import Data.Time.Clock (getCurrentTime) +import Data.Time.Clock (UTCTime, getCurrentTime) import Simplex.Chat.Badges.Code (NamePrepaidCode, maxNameLength, minNameLength, namePrepaidCodeHash, randomNamePrepaidCode) import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) import Simplex.Chat.Bot.Store (withDB') import Simplex.Chat.Controller (ChatController (..)) +import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) +import qualified Simplex.Messaging.Agent.Store.DB as DB import StoreService.Config (minNameYears) import StoreService.Group.Command (maxNameYears) -import StoreService.Names.Store (CodeHashTaken (..), NewPrepaidCode (..), insertPrepaidCode, revokePrepaidCode) +import StoreService.Names.Phases (RegistrationId) +import StoreService.Names.Store (CodeHashTaken (..), NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, getPrepaidCodeById, insertPrepaidCode, releasePrepaidCode, reservePrepaidCode, revokePrepaidCode, spendPrepaidCode) import StoreService.Names.Turns (Turns, withCodeTurn) import StoreService.Store (RevokeResult (..)) +data CodeState = CSUnused | CSReserved | CSUsed | CSRevoked | CSExpired | CSUnpaid + deriving (Eq, Show, Enum, Bounded) + +-- | A code revoked while its order held it can still be spent by that order, so a spent code reads as used. +codeState :: UTCTime -> PrepaidCode -> CodeState +codeState now PrepaidCode {pcReservedBy, pcSpentAt, pcRevokedAt, pcPaymentStatus, pcExpiresAt} + | isJust pcSpentAt = CSUsed + | isJust pcRevokedAt = CSRevoked + | isJust pcReservedBy = CSReserved + | CPSUnpaid <- pcPaymentStatus = CSUnpaid + | maybe False (<= now) pcExpiresAt = CSExpired + | otherwise = CSUnused + +codeStateText :: CodeState -> Text +codeStateText = \case + CSUnused -> "unused" + CSReserved -> "reserved" + CSUsed -> "used" + CSRevoked -> "revoked" + CSExpired -> "expired" + CSUnpaid -> "unpaid" + +data CodeRefusal = CRInvalid | CRUsed | CRReserved | CRExpired | CRPaymentPending | CRNotCovering + deriving (Eq, Show) + +codeRefusalTag :: CodeRefusal -> Text +codeRefusalTag = \case + CRInvalid -> "code_invalid" + CRUsed -> "code_used" + CRReserved -> "code_reserved" + CRExpired -> "code_expired" + CRPaymentPending -> "payment_pending" + CRNotCovering -> "code_not_covering" + +-- | Why the code cannot pay for the registration's name of this length. A code the registration +-- already holds stays good for it until spent, even if revoked or expired since. +codeRefusal :: UTCTime -> RegistrationId -> Int -> PrepaidCode -> Maybe CodeRefusal +codeRefusal now rid len code@PrepaidCode {pcReservedBy, pcSpentAt, pcMinLength} + | isJust pcSpentAt = Just CRUsed + | pcReservedBy == Just rid = covering + | otherwise = stateRefusal (codeState now code) <|> covering + where + covering + | len < pcMinLength = Just CRNotCovering + | otherwise = Nothing + +stateRefusal :: CodeState -> Maybe CodeRefusal +stateRefusal = \case + CSUnused -> Nothing + CSReserved -> Just CRReserved + CSUsed -> Just CRUsed + CSRevoked -> Just CRInvalid + CSExpired -> Just CRExpired + CSUnpaid -> Just CRPaymentPending + +-- | Why a conditional reservation of the code refused it, as the code reads now. +reserveRefusal :: DB.Connection -> UTCTime -> ByteString -> RegistrationId -> Int -> IO CodeRefusal +reserveRefusal db now codeHash rid len = maybe CRInvalid (fromMaybe CRReserved . codeRefusal now rid len) <$> getPrepaidCode db codeHash + +readPrepaidCode :: DBStore -> NamePrepaidCode -> IO (Maybe PrepaidCode) +readPrepaidCode st code = withTransaction st (`getPrepaidCode` namePrepaidCodeHash code) + +newtype ReservationRefused = ReservationRefused CodeRefusal + deriving (Show) + +instance E.Exception ReservationRefused + +-- | In the code's turn and one transaction, the code is checked, the write runs, then the code is reserved for the +-- registration, so the registration's row is locked before the code's. A refused reservation rolls the write back. +reserveCode :: Turns -> DBStore -> UTCTime -> ByteString -> RegistrationId -> Int -> (DB.Connection -> IO (Either e a)) -> IO (Either CodeRefusal (Either e a)) +reserveCode turns st now codeHash rid len write = + withCodeTurn turns codeHash $ either (\(ReservationRefused refusal) -> Left refusal) id <$> E.try (withTransaction st reserve) + where + reserve db = + getPrepaidCode db codeHash >>= \case + Nothing -> pure $ Left CRInvalid + Just code@PrepaidCode {pcId, pcReservedBy} + | Just refusal <- codeRefusal now rid len code -> pure $ Left refusal + | otherwise -> + write db >>= \case + written@(Right _) + | pcReservedBy /= Just rid -> + reservePrepaidCode db pcId rid now >>= \case + True -> pure $ Right written + -- Only a writer outside this process, which takes no turn here, can refuse it. + False -> E.throwIO . ReservationRefused =<< reserveRefusal db now codeHash rid len + written -> pure $ Right written + +-- | The write runs first, and the code is released only if it succeeds. False when the registration did not hold the code. +releaseCode :: Turns -> DBStore -> Int64 -> RegistrationId -> (DB.Connection -> IO (Either e a)) -> IO (Either e (a, Bool)) +releaseCode turns st codeId rid = afterWrite turns st codeId (\db -> releasePrepaidCode db codeId rid) + +-- | The write runs first, and the code is spent only if it succeeds. False when the registration did not hold the code. +spendCode :: Turns -> DBStore -> UTCTime -> Int64 -> RegistrationId -> (DB.Connection -> IO (Either e a)) -> IO (Either e (a, Bool)) +spendCode turns st now codeId rid = afterWrite turns st codeId (\db -> spendPrepaidCode db codeId rid now) + +afterWrite :: Turns -> DBStore -> Int64 -> (DB.Connection -> IO Bool) -> (DB.Connection -> IO (Either e a)) -> IO (Either e (a, Bool)) +afterWrite turns st codeId change write = do + codeHash_ <- withTransaction st $ \db -> fmap pcCodeHash <$> getPrepaidCodeById db codeId + let run = withTransaction st $ \db -> write db >>= traverse (\a -> (a,) <$> change db) + maybe run (\codeHash -> withCodeTurn turns codeHash run) codeHash_ + -- | The code table keeps only the hash, so the caller must deliver the code. issueNameCode :: ChatController -> Int -> Int -> BadgeCodePaymentStatus -> IO (Either String NamePrepaidCode) issueNameCode cc len years paymentStatus @@ -39,11 +161,12 @@ issueNameCode cc len years paymentStatus revokeNameCode :: Turns -> ChatController -> NamePrepaidCode -> IO (Either Text Text) revokeNameCode turns cc code = withCodeTurn turns codeHash $ do now <- getCurrentTime - withDB' "revokeNameCode" cc (\db -> revokePrepaidCode db codeHash now) <&> \case - Right (Revoked _) -> Right "Revoked." - Right (AlreadyRevoked _) -> Right "Already revoked." - Right AlreadyRedeemed -> Left "Already used. It cannot be revoked." - Right NoSuchCode -> Left "No such code." + withDB' "revokeNameCode" cc (\db -> (,) <$> revokePrepaidCode db codeHash now <*> getPrepaidCode db codeHash) <&> \case + Right (Revoked _, Just PrepaidCode {pcReservedBy = Just _}) -> Right "Revoked. The order holding it keeps it." + Right (Revoked _, _) -> Right "Revoked." + Right (AlreadyRevoked _, _) -> Right "Already revoked." + Right (AlreadyRedeemed, _) -> Left "Already used. It cannot be revoked." + Right (NoSuchCode, _) -> Left "No such code." Left _ -> Left "The code could not be revoked." where codeHash = namePrepaidCodeHash code diff --git a/apps/simplex-store-service/src/StoreService/Names/Phases.hs b/apps/simplex-store-service/src/StoreService/Names/Phases.hs new file mode 100644 index 0000000000..95033d1ba4 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Names/Phases.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE NamedFieldPuns #-} + +module StoreService.Names.Phases (Phases, RegistrationId (..), newPhases, publish, awaitChange, phasesCount, phasesWaiting) where + +import Control.Concurrent.STM +import Control.Exception (bracket) +import Data.Int (Int64) +import Data.Map.Strict (Map) +import qualified Data.Map.Strict as Map +import Data.Text (Text) + +newtype RegistrationId = RegistrationId Text + deriving (Eq, Ord, Show) + +-- | wVersion only grows, so a publish that arrives after a newer one cannot wake anyone. +data Watch = Watch {wVersion :: TVar Int64, wRefs :: TVar Int} + +newtype Phases = Phases (TVar (Map RegistrationId Watch)) + +newPhases :: IO Phases +newPhases = Phases <$> newTVarIO Map.empty + +-- | Call after the transaction that wrote this version commits. +publish :: Phases -> RegistrationId -> Int64 -> STM () +publish (Phases watches) rid version = + readTVar watches >>= mapM_ (\Watch {wVersion} -> modifyTVar' wVersion (max version)) . Map.lookup rid + +-- | The registrations with a waiter. +phasesCount :: Phases -> IO Int +phasesCount (Phases watches) = Map.size <$> readTVarIO watches + +-- | The waiters across all registrations. +phasesWaiting :: Phases -> STM Int +phasesWaiting (Phases watches) = readTVar watches >>= fmap sum . mapM (\Watch {wRefs} -> readTVar wRefs) . Map.elems + +subscribe :: Phases -> RegistrationId -> STM Watch +subscribe (Phases watches) rid = + readTVar watches >>= \ws -> case Map.lookup rid ws of + Just watch@Watch {wRefs} -> watch <$ modifyTVar' wRefs (+ 1) + Nothing -> do + watch <- Watch <$> newTVar minBound <*> newTVar 1 + watch <$ writeTVar watches (Map.insert rid watch ws) + +release :: Phases -> RegistrationId -> STM () +release (Phases watches) rid = + readTVar watches >>= \ws -> mapM_ (drop' ws) (Map.lookup rid ws) + where + drop' ws Watch {wRefs} = do + n <- pred <$> readTVar wRefs + if n <= 0 then writeTVar watches (Map.delete rid ws) else writeTVar wRefs n + +-- | True once the version differs from @seen@, False after @micros@ without a change. +-- Subscribes before reading the version, so a publish between the read and the wait is not missed. +awaitChange :: Phases -> RegistrationId -> IO Int64 -> Int64 -> Int -> IO Bool +awaitChange phases rid readVersion seen micros = + bracket (atomically $ subscribe phases rid) (const . atomically $ release phases rid) $ \Watch {wVersion} -> do + current <- readVersion + atomically $ modifyTVar' wVersion (max current) + if current /= seen + then pure True + else do + timer <- registerDelay micros + atomically $ + (readTVar wVersion >>= \v -> if v > seen then pure True else retry) + `orElse` (False <$ (readTVar timer >>= check)) diff --git a/apps/simplex-store-service/src/StoreService/Names/Signatures.hs b/apps/simplex-store-service/src/StoreService/Names/Signatures.hs new file mode 100644 index 0000000000..266ddb47f4 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Names/Signatures.hs @@ -0,0 +1,110 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module StoreService.Names.Signatures + ( SignatureError (..), + AddressError (..), + OrderFields (..), + parseSignature, + personalDigest, + orderMessage, + parseChecksumAddress, + parseAnyCaseAddress, + ) +where + +import Control.Monad (unless, when) +import Crypto.Number.Serialize (os2ip) +import qualified Crypto.PubKey.ECC.Types as ECC +import Data.Bifunctor (first) +import Data.ByteArray.Encoding (Base (Base16), convertFromBase) +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import qualified Data.ByteString.Char8 as BC +import Data.Char (isAsciiLower, isDigit, isHexDigit) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Text.Encoding (encodeUtf8) +import Data.Word (Word64) +import Simplex.Messaging.Crypto (keccak256, sha256Hash) +import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature (..)) +import Simplex.Messaging.Encoding.String (strDecode, strEncode) +import Simplex.Messaging.Eth.Address (Address) +import Simplex.Messaging.SimplexName (LabelHash (..)) +import Simplex.Messaging.Util (tshow) +import StoreService.Names.Abi (hex) + +data SignatureError + = SEFormat + | SERecoveryId + | SEHighS + deriving (Eq, Show) + +data AddressError + = ADFormat + | ADChecksum + deriving (Eq, Show) + +-- | The fields an owner signs in an order and in another name for it. +data OrderFields = OrderFields + { ofLabelHash :: LabelHash, + ofLength :: Int, + ofDuration :: Word64, + ofCommitment :: ByteString, + ofSealed :: Text + } + deriving (Eq) + +signatureHexLength :: Int +signatureHexLength = 130 + +scalarSize :: Int +scalarSize = 32 + +addressHexLength :: Int +addressHexLength = 40 + +-- the contracts' SignatureChecker refuses a higher s, though recovery accepts it +maxS :: Integer +maxS = ECC.ecc_n (ECC.common_curve $ ECC.getCurveByName ECC.SEC_p256k1) `div` 2 + +parseSignature :: Text -> Either SignatureError RecoverableSignature +parseSignature t = do + h <- maybe (Left SEFormat) Right $ T.stripPrefix "0x" t + unless (T.length h == signatureHexLength && T.all isLowerHex h) $ Left SEFormat + sig <- first (const SEFormat) $ convertFromBase Base16 (encodeUtf8 h) + let (rsR, (rsS, v)) = B.splitAt scalarSize <$> B.splitAt scalarSize sig + rsRecId <- case B.unpack v of + [27] -> Right 0 + [28] -> Right 1 + _ -> Left SERecoveryId + when (os2ip rsS > maxS) $ Left SEHighS + pure RecoverableSignature {rsR, rsS, rsRecId} + where + isLowerHex c = isDigit c || (isAsciiLower c && isHexDigit c) + +-- | EIP-191 @personal_sign@ digest. +personalDigest :: ByteString -> ByteString +personalDigest msg = keccak256 $ "\x19\&Ethereum Signed Message:\n" <> BC.pack (show $ B.length msg) <> msg + +orderMessage :: OrderFields -> Word64 -> ByteString +orderMessage order timestamp = message $ ["order"] <> orderWords order <> [tshow timestamp] + +message :: [Text] -> ByteString +message = encodeUtf8 . T.unwords . ("simplex.domains" :) + +orderWords :: OrderFields -> [Text] +orderWords OrderFields {ofLabelHash = LabelHash labelHash, ofLength, ofDuration, ofCommitment, ofSealed} = + [hex labelHash, tshow ofLength, tshow ofDuration, hex ofCommitment, hex $ sha256Hash $ encodeUtf8 ofSealed] + +-- the address parser checks EIP-55 only on mixed-case hex, so one-case text is refused here by comparing the encoding +parseChecksumAddress :: Text -> Either AddressError Address +parseChecksumAddress t = do + a <- parseAnyCaseAddress t + unless (strEncode a == encodeUtf8 t) $ Left ADChecksum + pure a + +parseAnyCaseAddress :: Text -> Either AddressError Address +parseAnyCaseAddress t = case T.stripPrefix "0x" t of + Just h | T.length h == addressHexLength && T.all isHexDigit h -> first (const ADFormat) $ strDecode $ encodeUtf8 $ T.toLower h + _ -> Left ADFormat diff --git a/apps/simplex-store-service/src/StoreService/Names/Store.hs b/apps/simplex-store-service/src/StoreService/Names/Store.hs index f7b2e0313a..9d66e0f8b4 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Store.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Store.hs @@ -1,30 +1,92 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TypeOperators #-} module StoreService.Names.Store - ( PrepaidCode (..), + ( NamePhase (..), + RegistrationPayment (..), + NameReveal (..), + NameRegistration (..), + NewRegistration (..), + PaidOrder (..), + RegistrationRefused (..), + VersionWrite (..), + LiveOrder (..), + OrderTerms (..), + PrepaidCode (..), NewPrepaidCode (..), CodeHashTaken (..), + TxFor (..), + TxStatus (..), + NameTransaction (..), + NewTransaction (..), + findLimit, + orderHash, + orderLive, + newRegistrationId, + methodText, + textMethod, + createPaidRegistration, + createCodeRegistration, + getRegistration, + getRegistrationByOrderHash, + getLiveOrders, + closeOverdue, + updateRegistration, + findRegistrations, + isLabelRegistering, + activeRegistrations, + unpaidCleanupCandidates, + deleteUnpaidRegistration, + failedCodeOrdersBefore, insertPrepaidCode, getPrepaidCode, + getPrepaidCodeById, + reservePrepaidCode, + releasePrepaidCode, + spendPrepaidCode, revokePrepaidCode, + insertTransaction, + recordTransactionOutcome, + pendingTransactions, + registrationTransactions, ) where +import qualified Control.Exception as E +import Control.Monad (when) +import Crypto.Number.Serialize (i2ospOf_) +import Data.ByteArray.Encoding (Base (Base16), convertToBase) import Data.ByteString (ByteString) +import qualified Data.ByteString as B import Data.Int (Int64) -import Data.Maybe (isJust) +import Data.Functor ((<&>)) +import Data.Maybe (isJust, listToMaybe) +import Data.Text (Text) +import Data.Text.Encoding (encodeUtf8) import Data.Time.Clock (UTCTime) +import Data.Word (Word64) import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) +import Simplex.Chat.PaymentService.Types (CardProvider (..), CryptoCurrency (..), InvoiceId (..), InvoiceStatus (..), ServicePaymentMethod (..)) import Simplex.Chat.Store.Shared (insertedRowId) -import Simplex.Messaging.Agent.Store.DB (Binary (..)) +import Simplex.Messaging.Agent.Store.DB (Binary (..), BoolInt (..), FromField (..), ToField (..), fromTextField_) import qualified Simplex.Messaging.Agent.Store.DB as DB -import Simplex.Messaging.Util (maybeFirstRow) +import Simplex.Messaging.Crypto (sha256Hash) +import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature) +import Simplex.Messaging.Encoding.String (TextEncoding (..), strDecode) +import Simplex.Messaging.Eth.Address (Address, unAddress) +import Simplex.Messaging.Eth.EIP712 (serializeSignature) +import Simplex.Messaging.SimplexName (LabelHash (..)) +import Simplex.Messaging.Util (maybeFirstRow, tshow) +import StoreService.Names.Abi (hex) +import StoreService.Names.Chain (TxHash (..)) +import StoreService.Names.Phases (RegistrationId (..)) +import StoreService.Names.Signatures (OrderFields (..), parseSignature) import StoreService.Store (RevokeResult (..)) -import StoreService.Store.Invoices (executeChanging, truncateToSecond) +import StoreService.Store.Invoices (StoreDecodeError (..), executeChanging, invoiceStatusText, lockInvoice, newInvoiceId, truncateToSecond, unfundedQuery) #if defined(dbPostgres) import Database.PostgreSQL.Simple (Only (..), Query, (:.) (..)) @@ -34,12 +96,123 @@ import Database.SQLite.Simple (Only (..), Query, (:.) (..)) import Database.SQLite.Simple.QQ (sql) #endif +data NamePhase + = PUnpaid + | PQueued + | PCommitting + | PCommitted + | PRegistering + | PRegistered + | PTaken + | PFailed + deriving (Eq, Show, Enum, Bounded) + +instance TextEncoding NamePhase where + textEncode = \case + PUnpaid -> "unpaid" + PQueued -> "queued" + PCommitting -> "committing" + PCommitted -> "committed" + PRegistering -> "registering" + PRegistered -> "registered" + PTaken -> "taken" + PFailed -> "failed" + textDecode t = lookup t [(textEncode p, p) | p <- [minBound .. maxBound]] + +instance FromField NamePhase where fromField = fromTextField_ textDecode + +instance ToField NamePhase where toField = toField . textEncode + +data RegistrationPayment + = RPInvoice ServicePaymentMethod InvoiceId + | RPCode Int64 + deriving (Eq, Show) + +-- | The name, links and owner's intent, all set by an accepted reveal and cleared together. +data NameReveal = NameReveal + { rvLabel :: Text, + rvSecret :: ByteString, + rvContactLink :: Maybe Text, + rvChannelLink :: Maybe Text, + rvNonce :: Word64, + rvDeadline :: Word64, + rvSignature :: RecoverableSignature + } + deriving (Eq, Show) + +data NameRegistration = NameRegistration + { nrId :: RegistrationId, + nrVersion :: Int64, + nrLabelHash :: LabelHash, + nrLength :: Int, + nrDuration :: Word64, + nrOwner :: Address, + nrCommitment :: ByteString, + nrSealed :: Text, + nrPhase :: NamePhase, + nrYears :: Int, + nrPrice :: Int, + nrPayment :: RegistrationPayment, + nrReveal :: Maybe NameReveal, + nrCommitTx :: Maybe TxHash, + nrCommitBlock :: Maybe Word64, + nrRegisterTx :: Maybe TxHash, + nrRegisterBlock :: Maybe Word64, + nrRevealAfter :: Maybe Word64, + nrRevealBefore :: Maybe Word64, + nrExpiresAt :: Maybe Word64, + nrRegisteredAt :: Maybe UTCTime, + nrFailedAt :: Maybe UTCTime, + nrError :: Maybe Text, + nrLive :: Bool, + nrOrderHash :: ByteString, + nrMoves :: Int, + nrLinksRefusedAt :: Maybe UTCTime, + nrCreatedAt :: UTCTime, + nrUpdatedAt :: UTCTime + } + deriving (Eq, Show) + +data NewRegistration = NewRegistration + { nrgId :: RegistrationId, + nrgOwner :: Address, + nrgOrder :: OrderFields, + nrgYears :: Int + } + +-- | The invoice row is inserted by the caller, in the same transaction and before the registration. +data PaidOrder = PaidOrder + { poMethod :: ServicePaymentMethod, + poPrice :: Int, + poInvoiceId :: InvoiceId, + poProviderRef :: Text + } + +data RegistrationRefused = RRLiveOrder | RRCodeNotReservable + deriving (Eq, Show) + +-- | NotLive: the write would make a closed order live. LiveOrderExists: another live order holds its commitment, or its owner and label hash. +data VersionWrite = Written Int64 | VersionChanged | NotLive | LiveOrderExists + deriving (Eq, Show) + +-- | A live order an order request meets; overdue when its open invoice is past its expiry time. +data LiveOrder = LiveOrder + { loRegistration :: NameRegistration, + loOverdue :: Bool + } + deriving (Eq, Show) + +-- | What an order pays with, as its hash covers it. +data OrderTerms = OTInvoice ServicePaymentMethod Int | OTCode ByteString + data PrepaidCode = PrepaidCode { pcId :: Int64, pcCodeHash :: ByteString, pcMinLength :: Int, pcYears :: Int, pcPaymentStatus :: BadgeCodePaymentStatus, + pcReservedBy :: Maybe RegistrationId, + pcReservedAt :: Maybe UTCTime, pcSpentAt :: Maybe UTCTime, pcRevokedAt :: Maybe UTCTime, pcExpiresAt :: Maybe UTCTime, @@ -58,6 +231,383 @@ data NewPrepaidCode = NewPrepaidCode data CodeHashTaken = CodeHashTaken deriving (Eq, Show) +data TxFor = TxCommit RegistrationId | TxRegister RegistrationId + deriving (Eq, Show) + +-- | A dropped transaction has no receipt and its registrar nonce is used. +data TxStatus = TSPending | TSMined | TSReverted | TSDropped + deriving (Eq, Show, Enum, Bounded) + +instance TextEncoding TxStatus where + textEncode = \case + TSPending -> "pending" + TSMined -> "mined" + TSReverted -> "reverted" + TSDropped -> "dropped" + textDecode t = lookup t [(textEncode s, s) | s <- [minBound .. maxBound]] + +instance FromField TxStatus where fromField = fromTextField_ textDecode + +instance ToField TxStatus where toField = toField . textEncode + +data NameTransaction = NameTransaction + { ntId :: Int64, + ntFor :: TxFor, + ntRaw :: ByteString, + ntHash :: TxHash, + ntNonce :: Word64, + ntSentAt :: UTCTime, + ntMinedBlock :: Maybe Word64, + ntStatus :: TxStatus + } + deriving (Eq, Show) + +data NewTransaction = NewTransaction + { ntxRaw :: ByteString, + ntxHash :: TxHash, + ntxNonce :: Word64 + } + +findLimit :: Int +findLimit = 50 + +initialVersion :: Int64 +initialVersion = 1 + +orderHashFieldBytes :: Int +orderHashFieldBytes = 4 + +-- | Each field is length-prefixed, so no two different orders encode the same. +orderHash :: Address -> OrderFields -> OrderTerms -> ByteString +orderHash owner OrderFields {ofLabelHash = LabelHash lh, ofLength, ofDuration, ofCommitment, ofSealed} terms = + sha256Hash . B.concat . map field $ [tag, lh, num ofLength, num ofDuration, unAddress owner, ofCommitment, encodeUtf8 ofSealed] <> payment + where + (tag, payment) = case terms of + OTInvoice method price -> ("name-invoice", [encodeUtf8 $ methodText method, num price]) + OTCode codeHash -> ("name-code", [codeHash]) + field bs = i2ospOf_ orderHashFieldBytes (toInteger $ B.length bs) <> bs + num :: Show a => a -> ByteString + num = encodeUtf8 . tshow + +newRegistrationId :: IO RegistrationId +newRegistrationId = (\(InvoiceId t) -> RegistrationId t) <$> newInvoiceId + +methodText :: ServicePaymentMethod -> Text +methodText = \case + SPMCard CPStripe -> "card" + SPMCrypto CCBtc -> "btc" + SPMCrypto CCXmr -> "xmr" + +textMethod :: Text -> Maybe ServicePaymentMethod +textMethod t = lookup t [(methodText m, m) | m <- [SPMCard CPStripe, SPMCrypto CCBtc, SPMCrypto CCXmr]] + +createPaidRegistration :: DB.Connection -> NewRegistration -> PaidOrder -> UTCTime -> IO (Either RegistrationRefused ()) +createPaidRegistration db new@NewRegistration {nrgId = RegistrationId rid, nrgOwner, nrgOrder} PaidOrder {poMethod, poPrice, poInvoiceId = InvoiceId invId, poProviderRef} now' = do + let now = truncateToSecond now' + hash = orderHash nrgOwner nrgOrder (OTInvoice poMethod poPrice) + inserted <- insertRegistration db new PUnpaid poPrice (Just poMethod) Nothing hash now + when inserted $ + DB.execute + db + "INSERT INTO sx_badge_service_name_invoices (invoice_id, registration_id, provider_ref, created_at) VALUES (?,?,?,?)" + (invId, rid, poProviderRef, now) + pure $ if inserted then Right () else Left RRLiveOrder + +-- | Reserving the code is the conditional update of the transaction that creates the registration. +createCodeRegistration :: DB.Connection -> NewRegistration -> PrepaidCode -> UTCTime -> IO (Either RegistrationRefused ()) +createCodeRegistration db new@NewRegistration {nrgId = regId@(RegistrationId rid), nrgOwner, nrgOrder} PrepaidCode {pcId, pcCodeHash} now' = do + let now = truncateToSecond now' + inserted <- insertRegistration db new PQueued 0 Nothing (Just pcId) (orderHash nrgOwner nrgOrder (OTCode pcCodeHash)) now + if not inserted + then pure $ Left RRLiveOrder + else + reservePrepaidCode db pcId regId now >>= \case + True -> pure $ Right () + False -> do + DB.execute db "DELETE FROM sx_badge_service_name_registrations WHERE registration_id = ?" (Only rid) + pure $ Left RRCodeNotReservable + +-- | False when the registration would be a second live one for its commitment, or for its owner and label hash. +insertRegistration :: DB.Connection -> NewRegistration -> NamePhase -> Int -> Maybe ServicePaymentMethod -> Maybe Int64 -> ByteString -> UTCTime -> IO Bool +insertRegistration db NewRegistration {nrgId = RegistrationId rid, nrgOwner, nrgOrder, nrgYears} phase price method codeId hash now = + (> 0) + <$> executeChanging + db + [sql| + INSERT INTO sx_badge_service_name_registrations + (registration_id, version, label_hash, length, duration, owner, commitment, sealed, phase, years, + price, method, name_prepaid_code_id, live, order_hash, moves, created_at, updated_at) + VALUES (?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?) + ON CONFLICT DO NOTHING + |] + ( (rid, initialVersion, Binary lh, ofLength, int64 ofDuration, Binary $ unAddress nrgOwner, Binary ofCommitment, ofSealed, phase, nrgYears) + :. (price, methodText <$> method, codeId, BI True, Binary hash, 0 :: Int, now, now) + ) + where + OrderFields {ofLabelHash = LabelHash lh, ofLength, ofDuration, ofCommitment, ofSealed} = nrgOrder + +registrationColumns :: Query +registrationColumns = + [sql| + SELECT r.registration_id, r.version, r.label_hash, r.length, r.duration, r.owner, r.commitment, r.sealed, r.phase, r.years, + r.price, r.method, ni.invoice_id, r.name_prepaid_code_id, r.label, r.secret, r.contact_link, r.channel_link, r.intent_nonce, r.intent_deadline, + r.intent_signature, r.commit_tx, r.commit_block, r.register_tx, r.register_block, r.reveal_after, r.reveal_before, r.expires_at, r.registered_at, + r.failed_at, r.error, r.live, r.order_hash, r.moves, r.links_refused_at, r.created_at, r.updated_at + |] + +registrationFrom :: Query +registrationFrom = + " " + <> [sql| + FROM sx_badge_service_name_registrations r + LEFT JOIN sx_badge_service_name_invoices ni ON ni.registration_id = r.registration_id + |] + +selectRegistrations :: Query +selectRegistrations = registrationColumns <> registrationFrom + +type RegistrationRow = + (Text, Int64, Binary ByteString, Int, Int64, Binary ByteString, Binary ByteString, Text, NamePhase, Int) + :. (Int, Maybe Text, Maybe Text, Maybe Int64, Maybe Text, Maybe (Binary ByteString), Maybe Text, Maybe Text, Maybe Int64, Maybe Int64) + :. (Maybe (Binary ByteString), Maybe (Binary ByteString), Maybe Int64, Maybe (Binary ByteString), Maybe Int64, Maybe Int64, Maybe Int64, Maybe Int64, Maybe UTCTime) + :. (Maybe UTCTime, Maybe Text, BoolInt, Binary ByteString, Int, Maybe UTCTime, UTCTime, UTCTime) + +toRegistration :: RegistrationRow -> Either Text NameRegistration +toRegistration + ( (rid, nrVersion, Binary lh, nrLength, duration, Binary owner, Binary nrCommitment, nrSealed, nrPhase, nrYears) + :. (nrPrice, method_, invoiceId_, codeId_, label_, secret_, nrContactLink, nrChannelLink, nonce_, deadline_) + :. (signature_, commitTx, commitBlock, registerTx, registerBlock, revealAfter, revealBefore, expiresAt, nrRegisteredAt) + :. (nrFailedAt, nrError, BI nrLive, Binary nrOrderHash, nrMoves, nrLinksRefusedAt, nrCreatedAt, nrUpdatedAt) + ) = do + nrOwner <- decodeAddress owner + nrPayment <- case (method_, invoiceId_, codeId_) of + (Just m, Just invId, Nothing) -> (`RPInvoice` InvoiceId invId) <$> note ("name_registrations.method: " <> m) (textMethod m) + (Nothing, Nothing, Just codeId) -> Right $ RPCode codeId + _ -> Left $ "name_registrations payment: " <> rid + nrReveal <- case (label_, secret_, nonce_, deadline_, signature_) of + (Nothing, Nothing, Nothing, Nothing, Nothing) -> Right Nothing + (Just rvLabel, Just (Binary rvSecret), Just nonce, Just deadline, Just (Binary sig)) -> do + rvSignature <- decodeSignature sig + Right $ Just NameReveal {rvLabel, rvSecret, rvContactLink = nrContactLink, rvChannelLink = nrChannelLink, rvNonce = word64 nonce, rvDeadline = word64 deadline, rvSignature} + _ -> Left $ "name_registrations reveal: " <> rid + pure + NameRegistration + { nrId = RegistrationId rid, + nrVersion, + nrLabelHash = LabelHash lh, + nrLength, + nrDuration = word64 duration, + nrOwner, + nrCommitment, + nrSealed, + nrPhase, + nrYears, + nrPrice, + nrPayment, + nrReveal, + nrCommitTx = txHash <$> commitTx, + nrCommitBlock = word64 <$> commitBlock, + nrRegisterTx = txHash <$> registerTx, + nrRegisterBlock = word64 <$> registerBlock, + nrRevealAfter = word64 <$> revealAfter, + nrRevealBefore = word64 <$> revealBefore, + nrExpiresAt = word64 <$> expiresAt, + nrRegisteredAt, + nrFailedAt, + nrError, + nrLive, + nrOrderHash, + nrMoves, + nrLinksRefusedAt, + nrCreatedAt, + nrUpdatedAt + } + +getRegistration :: DB.Connection -> RegistrationId -> IO (Maybe NameRegistration) +getRegistration db (RegistrationId rid) = + firstDecoded toRegistration $ DB.query db (selectRegistrations <> " WHERE r.registration_id = ?") (Only rid) + +-- | The newest stored order, live or not, with exactly these fields. +getRegistrationByOrderHash :: DB.Connection -> ByteString -> IO (Maybe NameRegistration) +getRegistrationByOrderHash db hash = + firstDecoded toRegistration $ + DB.query db (selectRegistrations <> " WHERE r.order_hash = ? ORDER BY r.created_at DESC, r.registration_id DESC LIMIT 1") (Only $ Binary hash) + +-- | Overdue whatever status the poller has marked, as long as the invoice is not paid and holds no money, as the sweep decides. +getLiveOrders :: DB.Connection -> ByteString -> Address -> LabelHash -> UTCTime -> IO [LiveOrder] +getLiveOrders db commitment owner (LabelHash lh) now = + DB.query + db + ( registrationColumns + <> ", CASE WHEN r.phase = ? AND i.status <> ? AND i.expires_at < ?" + <> unfunded + <> " THEN 1 ELSE 0 END" + <> registrationFrom + <> " LEFT JOIN sx_badge_service_invoices i ON i.invoice_id = ni.invoice_id" + <> " WHERE r.live = 1 AND (r.commitment = ? OR (r.owner = ? AND r.label_hash = ?))" + ) + (PUnpaid, invoiceStatusText ISPaid, truncateToSecond now, Binary commitment, Binary $ unAddress owner, Binary lh) + >>= decodeAll toLiveOrder + where + toLiveOrder (row :. Only (BI loOverdue)) = (\loRegistration -> LiveOrder {loRegistration, loOverdue}) <$> toRegistration row + +-- | The sweep's own check, on the invoice joined as i. +unfunded :: Query +unfunded = unfundedQuery "i.invoice_id" + +-- | Run in the transaction that inserts the next order, so the unique constraints let it in; the registration's new version. +-- Nothing when the order changed since it was read, or its invoice is no longer open, not past its expiry, or holds money: the caller rolls back, +-- since the invoice is written before the registration, as settlement, cancel and the sweep write them. +closeOverdue :: DB.Connection -> NameRegistration -> UTCTime -> IO (Maybe Int64) +closeOverdue db r@NameRegistration {nrId = RegistrationId rid, nrVersion, nrPayment} now = do + -- locked first, so the funding check below, a later statement, sees a payment a concurrent settlement committed + mapM_ (\invId -> lockInvoice db invId now) $ case nrPayment of + RPInvoice _ invId -> Just invId + RPCode _ -> Nothing + expired <- + executeChanging + db + ( [sql| + UPDATE sx_badge_service_invoices SET status = ?, updated_at = ? + WHERE status = ? AND invoice_id IN ( + SELECT i.invoice_id FROM sx_badge_service_name_invoices ni + JOIN sx_badge_service_name_registrations r ON r.registration_id = ni.registration_id + JOIN sx_badge_service_invoices i ON i.invoice_id = ni.invoice_id + WHERE ni.registration_id = ? AND r.version = ? AND r.live = 1 AND i.expires_at < ? + |] + <> unfunded + <> " )" + ) + (invoiceStatusText ISExpired, truncateToSecond now, invoiceStatusText ISOpen, rid, nrVersion, truncateToSecond now) + if expired > 0 + then + updateRegistration db r {nrLive = False} now <&> \case + Written v -> Just v + _ -> Nothing + else pure Nothing + +-- | Whether an order can still progress to a registration for its name. The unique live indexes allow one such order per commitment, +-- and one per owner and label hash, so a wallet can order a name again once its registration is done. +-- An unpaid order is live while its invoice is open, which nrLive carries; a taken order is final. +orderLive :: NameRegistration -> Bool +orderLive NameRegistration {nrPhase, nrLive} = case nrPhase of + PUnpaid -> nrLive + PQueued -> True + PCommitting -> True + PCommitted -> True + PRegistering -> True + PFailed -> True + PTaken -> False + PRegistered -> False + +-- | Writes every changeable field of a registration read at its nrVersion, unless it changed since, with live as orderLive decides. +-- A closed order is never made live again, and a write that would make the order meet another live one changes nothing. +updateRegistration :: DB.Connection -> NameRegistration -> UTCTime -> IO VersionWrite +updateRegistration db r now = do + changed <- + executeChanging + db + [sql| + UPDATE sx_badge_service_name_registrations + SET version = version + 1, label_hash = ?, length = ?, duration = ?, commitment = ?, sealed = ?, phase = ?, price = ?, + label = ?, secret = ?, contact_link = ?, channel_link = ?, intent_nonce = ?, intent_deadline = ?, intent_signature = ?, + commit_tx = ?, commit_block = ?, register_tx = ?, register_block = ?, reveal_after = ?, reveal_before = ?, expires_at = ?, + registered_at = ?, failed_at = ?, error = ?, live = ?, moves = ?, links_refused_at = ?, updated_at = ? + WHERE registration_id = ? AND version = ? + AND (? = 0 OR (live = 1 AND NOT EXISTS ( + SELECT 1 FROM sx_badge_service_name_registrations o + WHERE o.live = 1 AND o.registration_id <> ? AND (o.commitment = ? OR (o.owner = ? AND o.label_hash = ?)) + ))) + |] + ( (Binary lh, nrLength, int64 nrDuration, Binary nrCommitment, nrSealed, nrPhase, nrPrice) + :. (rvLabel <$> nrReveal, Binary . rvSecret <$> nrReveal, rvContactLink =<< nrReveal, rvChannelLink =<< nrReveal, int64 . rvNonce <$> nrReveal, int64 . rvDeadline <$> nrReveal, Binary . serializeSignature . rvSignature <$> nrReveal) + :. (txHashField <$> nrCommitTx, int64 <$> nrCommitBlock, txHashField <$> nrRegisterTx, int64 <$> nrRegisterBlock, int64 <$> nrRevealAfter, int64 <$> nrRevealBefore, int64 <$> nrExpiresAt) + :. (truncateToSecond <$> nrRegisteredAt, truncateToSecond <$> nrFailedAt, nrError, BI live, nrMoves, truncateToSecond <$> nrLinksRefusedAt, truncateToSecond now) + :. (rid, nrVersion, BI live, rid, Binary nrCommitment, Binary $ unAddress nrOwner, Binary lh) + ) + if changed > 0 then pure $ Written (nrVersion + 1) else refusal + where + NameRegistration {nrId = RegistrationId rid, nrVersion, nrLabelHash = LabelHash lh, nrLength, nrDuration, nrOwner, nrCommitment, nrSealed, nrPhase, nrPrice, nrReveal} = r + NameRegistration {nrCommitTx, nrCommitBlock, nrRegisterTx, nrRegisterBlock, nrRevealAfter, nrRevealBefore, nrExpiresAt} = r + NameRegistration {nrRegisteredAt, nrFailedAt, nrError, nrMoves, nrLinksRefusedAt} = r + live = orderLive r + refusal = + DB.query db "SELECT version, live FROM sx_badge_service_name_registrations WHERE registration_id = ?" (Only rid) >>= \case + [(version, BI storedLive)] | version == nrVersion -> pure $ if storedLive then LiveOrderExists else NotLive + _ -> pure VersionChanged + +-- | Newest first, at most findLimit. +findRegistrations :: DB.Connection -> Address -> IO [NameRegistration] +findRegistrations db owner = + DB.query db (selectRegistrations <> " WHERE r.owner = ? ORDER BY r.created_at DESC, r.registration_id DESC LIMIT ?") (Binary $ unAddress owner, findLimit) + >>= decodeAll toRegistration + +isLabelRegistering :: DB.Connection -> LabelHash -> IO Bool +isLabelRegistering db (LabelHash lh) = + not . null + <$> ( DB.query db "SELECT 1 FROM sx_badge_service_name_registrations WHERE label_hash = ? AND phase = ? LIMIT 1" (Binary lh, PRegistering) :: + IO [Only Int] + ) + +-- | The registrations a worker moves on, oldest first. +activeRegistrations :: DB.Connection -> IO [NameRegistration] +activeRegistrations db = + DB.query db (selectRegistrations <> " WHERE r.phase IN (?,?,?,?) ORDER BY r.created_at, r.registration_id") (PQueued, PCommitting, PCommitted, PRegistering) + >>= decodeAll toRegistration + +-- | Unpaid orders whose invoice expired before the cutoff, without a recorded payment. +unpaidCleanupCandidates :: DB.Connection -> UTCTime -> IO [(RegistrationId, InvoiceId)] +unpaidCleanupCandidates db expiredBefore = + map (\(rid, invId) -> (RegistrationId rid, InvoiceId invId)) + <$> DB.query + db + [sql| + SELECT r.registration_id, ni.invoice_id + FROM sx_badge_service_name_registrations r + JOIN sx_badge_service_name_invoices ni ON ni.registration_id = r.registration_id + JOIN sx_badge_service_invoices i ON i.invoice_id = ni.invoice_id + WHERE r.phase = ? AND i.expires_at < ? + AND NOT EXISTS (SELECT 1 FROM sx_badge_service_payments p WHERE p.invoice_id = ni.invoice_id) + ORDER BY i.expires_at, r.registration_id + |] + (PUnpaid, truncateToSecond expiredBefore) + +-- | Deletes an unpaid registration and its invoice, unless a payment was recorded for it. +deleteUnpaidRegistration :: DB.Connection -> RegistrationId -> IO Bool +deleteUnpaidRegistration db (RegistrationId rid) = do + invoiceIds <- DB.query db "SELECT invoice_id FROM sx_badge_service_name_invoices WHERE registration_id = ?" (Only rid) + deleted <- + executeChanging + db + [sql| + DELETE FROM sx_badge_service_name_registrations + WHERE registration_id = ? AND phase = ? + AND NOT EXISTS ( + SELECT 1 FROM sx_badge_service_name_invoices ni + JOIN sx_badge_service_payments p ON p.invoice_id = ni.invoice_id + WHERE ni.registration_id = ? + ) + |] + (rid, PUnpaid, rid) + when (deleted > 0) $ + mapM_ (DB.execute db "DELETE FROM sx_badge_service_invoices WHERE invoice_id = ?" . Only . fromOnly) (invoiceIds :: [Only Text]) + pure $ deleted > 0 + +-- | Code orders failed before the cutoff that still hold their unspent code. +failedCodeOrdersBefore :: DB.Connection -> UTCTime -> IO [(RegistrationId, Int64)] +failedCodeOrdersBefore db cutoff = + map (\(rid, codeId) -> (RegistrationId rid, codeId)) + <$> DB.query + db + [sql| + SELECT r.registration_id, c.name_prepaid_code_id + FROM sx_badge_service_name_registrations r + JOIN sx_badge_service_name_prepaid_codes c ON c.reserved_by = r.registration_id + WHERE r.phase = ? AND r.failed_at < ? AND c.spent_at IS NULL + ORDER BY r.failed_at, r.registration_id + |] + (PFailed, truncateToSecond cutoff) + insertPrepaidCode :: DB.Connection -> NewPrepaidCode -> UTCTime -> IO (Either CodeHashTaken Int64) insertPrepaidCode db NewPrepaidCode {npcCodeHash, npcMinLength, npcYears, npcPaymentStatus, npcExpiresAt} now = do inserted <- @@ -74,21 +624,60 @@ insertPrepaidCode db NewPrepaidCode {npcCodeHash, npcMinLength, npcYears, npcPay prepaidCodeColumns :: Query prepaidCodeColumns = [sql| - SELECT name_prepaid_code_id, code_hash, min_length, years, code_payment_status, spent_at, revoked_at, expires_at, created_at + SELECT name_prepaid_code_id, code_hash, min_length, years, code_payment_status, reserved_by, reserved_at, spent_at, revoked_at, expires_at, created_at FROM sx_badge_service_name_prepaid_codes |] -type PrepaidCodeRow = (Int64, Binary ByteString, Int, Int, BadgeCodePaymentStatus, Maybe UTCTime, Maybe UTCTime, Maybe UTCTime) :. Only UTCTime +type PrepaidCodeRow = (Int64, Binary ByteString, Int, Int, BadgeCodePaymentStatus, Maybe Text, Maybe UTCTime, Maybe UTCTime, Maybe UTCTime, Maybe UTCTime) :. Only UTCTime toPrepaidCode :: PrepaidCodeRow -> PrepaidCode -toPrepaidCode ((pcId, Binary pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, pcSpentAt, pcRevokedAt, pcExpiresAt) :. Only pcCreatedAt) = - PrepaidCode {pcId, pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, pcSpentAt, pcRevokedAt, pcExpiresAt, pcCreatedAt} +toPrepaidCode ((pcId, Binary pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, reservedBy, pcReservedAt, pcSpentAt, pcRevokedAt, pcExpiresAt) :. Only pcCreatedAt) = + PrepaidCode {pcId, pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, pcReservedBy = RegistrationId <$> reservedBy, pcReservedAt, pcSpentAt, pcRevokedAt, pcExpiresAt, pcCreatedAt} getPrepaidCode :: DB.Connection -> ByteString -> IO (Maybe PrepaidCode) getPrepaidCode db codeHash = maybeFirstRow toPrepaidCode $ DB.query db (prepaidCodeColumns <> " WHERE code_hash = ?") (Only $ Binary codeHash) --- | A spent code cannot be revoked. +getPrepaidCodeById :: DB.Connection -> Int64 -> IO (Maybe PrepaidCode) +getPrepaidCodeById db codeId = + maybeFirstRow toPrepaidCode $ DB.query db (prepaidCodeColumns <> " WHERE name_prepaid_code_id = ?") (Only codeId) + +-- | Only an unused code: not reserved, spent or revoked, paid or free, and not expired. +reservePrepaidCode :: DB.Connection -> Int64 -> RegistrationId -> UTCTime -> IO Bool +reservePrepaidCode db codeId (RegistrationId rid) now' = + (> 0) + <$> executeChanging + db + [sql| + UPDATE sx_badge_service_name_prepaid_codes SET reserved_by = ?, reserved_at = ? + WHERE name_prepaid_code_id = ? AND reserved_by IS NULL AND spent_at IS NULL AND revoked_at IS NULL + AND code_payment_status <> ? AND (expires_at IS NULL OR expires_at > ?) + |] + (rid, now, codeId, CPSUnpaid, now) + where + now = truncateToSecond now' + +releasePrepaidCode :: DB.Connection -> Int64 -> RegistrationId -> IO Bool +releasePrepaidCode db codeId (RegistrationId rid) = + (> 0) + <$> executeChanging + db + [sql| + UPDATE sx_badge_service_name_prepaid_codes SET reserved_by = NULL, reserved_at = NULL + WHERE name_prepaid_code_id = ? AND reserved_by = ? AND spent_at IS NULL + |] + (codeId, rid) + +-- | A spent code keeps reserved_by, naming the registration it paid for. +spendPrepaidCode :: DB.Connection -> Int64 -> RegistrationId -> UTCTime -> IO Bool +spendPrepaidCode db codeId (RegistrationId rid) now = + (> 0) + <$> executeChanging + db + "UPDATE sx_badge_service_name_prepaid_codes SET spent_at = ? WHERE name_prepaid_code_id = ? AND reserved_by = ? AND spent_at IS NULL" + (truncateToSecond now, codeId, rid) + +-- | A reserved code can be revoked, and its order keeps it; a spent one cannot. revokePrepaidCode :: DB.Connection -> ByteString -> UTCTime -> IO RevokeResult revokePrepaidCode db codeHash now = do revoked <- @@ -102,3 +691,87 @@ revokePrepaidCode db codeHash now = do | revoked > 0 = Revoked pcId | isJust pcRevokedAt = AlreadyRevoked pcId | otherwise = AlreadyRedeemed + +txKind :: TxFor -> (Text, Text) +txKind = \case + TxCommit (RegistrationId rid) -> ("commit", rid) + TxRegister (RegistrationId rid) -> ("register", rid) + +-- | Saved once signed and before it is sent, so a restart can rebroadcast it. +insertTransaction :: DB.Connection -> TxFor -> NewTransaction -> UTCTime -> IO Int64 +insertTransaction db for NewTransaction {ntxRaw, ntxHash, ntxNonce} now = do + let (kind, rid) = txKind for + DB.execute + db + [sql| + INSERT INTO sx_badge_service_name_transactions + (registration_id, kind, raw, tx_hash, registrar_nonce, sent_at, status) + VALUES (?,?,?,?,?,?,?) + |] + (rid, kind, Binary ntxRaw, txHashField ntxHash, int64 ntxNonce, truncateToSecond now, TSPending) + insertedRowId db + +-- | Moves a transaction on from the status the caller read, so an outcome is never overwritten unseen; False when it is not in that status. +recordTransactionOutcome :: DB.Connection -> TxHash -> TxStatus -> TxStatus -> Maybe Word64 -> IO Bool +recordTransactionOutcome db hash from status block = + (> 0) + <$> executeChanging + db + "UPDATE sx_badge_service_name_transactions SET status = ?, mined_block = ? WHERE tx_hash = ? AND status = ?" + (status, int64 <$> block, txHashField hash, from) + +transactionColumns :: Query +transactionColumns = + [sql| + SELECT name_transaction_id, kind, registration_id, raw, tx_hash, registrar_nonce, sent_at, mined_block, status + FROM sx_badge_service_name_transactions + |] + +type TransactionRow = (Int64, Text, Maybe Text, Binary ByteString, Binary ByteString, Int64, UTCTime, Maybe Int64, TxStatus) + +toTransaction :: TransactionRow -> Either Text NameTransaction +toTransaction (ntId, kind, rid_, Binary ntRaw, hash, nonce, ntSentAt, block, ntStatus) = do + ntFor <- case (kind, rid_) of + ("commit", Just rid) -> Right $ TxCommit $ RegistrationId rid + ("register", Just rid) -> Right $ TxRegister $ RegistrationId rid + _ -> Left $ "name_transactions.kind: " <> kind + pure NameTransaction {ntId, ntFor, ntRaw, ntHash = txHash hash, ntNonce = word64 nonce, ntSentAt, ntMinedBlock = word64 <$> block, ntStatus} + +-- | Those without a receipt, in nonce order, for the sender to resume. +pendingTransactions :: DB.Connection -> IO [NameTransaction] +pendingTransactions db = + DB.query db (transactionColumns <> " WHERE status = ? ORDER BY registrar_nonce, name_transaction_id") (Only TSPending) + >>= decodeAll toTransaction + +registrationTransactions :: DB.Connection -> RegistrationId -> IO [NameTransaction] +registrationTransactions db (RegistrationId rid) = + DB.query db (transactionColumns <> " WHERE registration_id = ? ORDER BY name_transaction_id") (Only rid) + >>= decodeAll toTransaction + +int64 :: Word64 -> Int64 +int64 = fromIntegral + +txHash :: Binary ByteString -> TxHash +txHash (Binary h) = TxHash h + +txHashField :: TxHash -> Binary ByteString +txHashField (TxHash h) = Binary h + +word64 :: Int64 -> Word64 +word64 = fromIntegral + +-- | Address has no exported constructor; its parser takes lowercase hex without the EIP-55 check. +decodeAddress :: ByteString -> Either Text Address +decodeAddress bs = either (const $ Left "address column") Right $ strDecode $ convertToBase Base16 bs + +decodeSignature :: ByteString -> Either Text RecoverableSignature +decodeSignature bs = either (Left . ("signature column: " <>) . tshow) Right $ parseSignature $ hex bs + +note :: Text -> Maybe a -> Either Text a +note err = maybe (Left err) Right + +decodeAll :: (row -> Either Text a) -> [row] -> IO [a] +decodeAll decode = either (E.throwIO . StoreDecodeError) pure . traverse decode + +firstDecoded :: (row -> Either Text a) -> IO [row] -> IO (Maybe a) +firstDecoded decode rows = rows >>= fmap listToMaybe . decodeAll decode . take 1 diff --git a/apps/simplex-store-service/src/StoreService/Names/Turns.hs b/apps/simplex-store-service/src/StoreService/Names/Turns.hs index 7ef1eceaf0..ee0e4d57bc 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Turns.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Turns.hs @@ -1,21 +1,29 @@ {-# LANGUAGE NamedFieldPuns #-} --- | A thread that takes a turn it already holds waits forever. -module StoreService.Names.Turns (Turns, newTurns, withCodeTurn) where +-- | A thread that takes a turn it already holds waits forever. Label turns are taken before a code turn. +module StoreService.Names.Turns (Turns, newTurns, withLabelTurn, withCodeTurn, turnsCount) where import Control.Concurrent.STM import Control.Exception (bracket, bracket_) import Data.ByteString (ByteString) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map +import Simplex.Messaging.SimplexName (LabelHash (..)) -- | Each lock counts the requests holding or waiting for it, and its entry goes when none are left. type TurnLocks = TVar (Map ByteString (TMVar (), Int)) -data Turns = Turns {codeTurns :: TurnLocks} +data Turns = Turns {labelTurns :: TurnLocks, codeTurns :: TurnLocks} newTurns :: IO Turns -newTurns = Turns <$> newTVarIO Map.empty +newTurns = Turns <$> newTVarIO Map.empty <*> newTVarIO Map.empty + +withLabelTurn :: Turns -> LabelHash -> IO a -> IO a +withLabelTurn Turns {labelTurns} (LabelHash h) = withTurn labelTurns h + +-- | The label and code hashes with a holder or a waiter. +turnsCount :: Turns -> IO Int +turnsCount Turns {labelTurns, codeTurns} = atomically $ (+) <$> (Map.size <$> readTVar labelTurns) <*> (Map.size <$> readTVar codeTurns) -- | Keyed by the code's SHA-256 hash. withCodeTurn :: Turns -> ByteString -> IO a -> IO a diff --git a/tests/Bots/StoreService/NamesCodesTests.hs b/tests/Bots/StoreService/NamesCodesTests.hs index 765fc38f1c..0debaec019 100644 --- a/tests/Bots/StoreService/NamesCodesTests.hs +++ b/tests/Bots/StoreService/NamesCodesTests.hs @@ -1,24 +1,357 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TupleSections #-} module Bots.StoreService.NamesCodesTests (namesCodesTests) where -import Control.Monad (forM_) -import Simplex.Chat.Badges.Code (NameCode (..), NamePrepaidCode, formatNameCode, randomNamePrepaidCode) +import Bots.StoreService.NamesStoreTests (order, ownerA, ownerB, placeCode, placePaid, usable) +import Bots.StoreService.WebTests (withServiceStore) +import Control.Arrow ((&&&)) +import Control.Concurrent (threadDelay) +import Control.Concurrent.Async (concurrently) +import Control.Monad (forM_, 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.Char8 as LB +import Data.IORef (newIORef, readIORef, writeIORef) +import Data.Maybe (fromJust, fromMaybe) +import Data.Text (Text) +import Data.Time.Calendar (fromGregorian) +import Data.Time.Clock (UTCTime (..), addUTCTime, getCurrentTime) +import Network.HTTP.Client (Manager, Response, RequestBody (..), defaultManagerSettings, httpLbs, method, newManager, parseRequest, requestBody, responseBody, responseHeaders, responseStatus) +import Network.HTTP.Types (hContentType) +import Network.HTTP.Types.Status (statusCode) +import qualified Network.Wai.Handler.Warp as Warp +import Simplex.Chat.Badges.Code (NameCode (..), NamePrepaidCode, formatNameCode, namePrepaidCodeHash, parseNameCode, randomNamePrepaidCode) +import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) import Simplex.Chat.Types.Shared (GroupMemberRole (..)) +import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) +import qualified Simplex.Messaging.Agent.Store.DB as DB import qualified Simplex.Messaging.Crypto as C +import StoreService.Config (ListenerConfig (..)) import StoreService.Group.Command (CmdAction (..), GroupCmd (..), groupCmdAction) +import StoreService.Names.Codes +import StoreService.Names.Phases (RegistrationId (..)) +import StoreService.Names.Store (NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, insertPrepaidCode, reservePrepaidCode, revokePrepaidCode, spendPrepaidCode) +import StoreService.Names.Turns (Turns, newTurns) +import StoreService.Store (RevokeResult (..)) +import StoreService.Web.Http (codeLimit, newHttpEnv, notFound) +import StoreService.Web.Names (Route (..), namesApp) +import StoreService.Web.Names.Codes (codeCheckHandler) import Test.Hspec namesCodesTests :: Spec namesCodesTests = describe "StoreService names: prepaid codes" $ do + describe "state" $ + it "reads each state from the code's row, a spent code as used even if revoked while held" testCodeStates + describe "check" $ do + it "answers each state with what the code covers, and changes no code" testCheckStates + it "folds a code typed in lower case with spaces to the issued one" testCheckFolded + it "answers code_invalid to an unknown, mistyped, zero-padded, owner or badge code" testCheckInvalid + it "answers bad_request to a body that is not an object with a code text, and 405 to GET" testCheckBadBody + it "takes a code token for every check, whatever its outcome" testCheckLimit + describe "reservations" $ do + it "reserves an unused code after the registration's write, in its transaction" testReserve + it "refuses each unusable code with its tag, before the write runs" testReserveRefusals + it "keeps a code its registration holds even if revoked or expired since, still checking the length" testReserveHeld + it "refuses a spent code to its own registration with code_used, before the write runs" testReserveSpent + it "reserves nothing when the write refuses" testReserveWriteRefused + it "rolls the write back when the code's reservation is refused after it" testReserveRollsBack + it "lets one of two concurrent reservations of a code win, the other answered code_reserved" testConcurrentReserve + it "releases and spends a code only after the write, and only for its holder" testReleaseSpend describe "group commands" $ do it "parses /issue name and /bulk name with the length, years and count" testGroupNameCommands it "answers the usage to a bad length, years or count, or a badge option" testGroupNameCommandsRefused it "parses /revoke of a prepaid code, telling a moderator it was not revoked" testGroupRevokeNameCommand +r1, r2 :: RegistrationId +r1 = RegistrationId "r1" +r2 = RegistrationId "r2" + +tNow :: UTCTime +tNow = UTCTime (fromGregorian 2026 10 21) 3600 + +baseCode :: PrepaidCode +baseCode = + PrepaidCode + { pcId = 1, + pcCodeHash = "", + pcMinLength = 8, + pcYears = 2, + pcPaymentStatus = CPSPaid, + pcReservedBy = Nothing, + pcReservedAt = Nothing, + pcSpentAt = Nothing, + pcRevokedAt = Nothing, + pcExpiresAt = Nothing, + pcCreatedAt = tNow + } + +testCodeStates :: IO () +testCodeStates = do + let stateOf = codeState tNow + held = baseCode {pcReservedBy = Just r1, pcReservedAt = Just tNow} + revoked = Just tNow + map codeStateText [minBound .. maxBound] `shouldBe` ["unused", "reserved", "used", "revoked", "expired", "unpaid"] + stateOf baseCode `shouldBe` CSUnused + stateOf baseCode {pcPaymentStatus = CPSFree, pcExpiresAt = Just (addUTCTime 1 tNow)} `shouldBe` CSUnused + stateOf held `shouldBe` CSReserved + stateOf held {pcSpentAt = Just tNow} `shouldBe` CSUsed + stateOf held {pcSpentAt = Just tNow, pcRevokedAt = revoked} `shouldBe` CSUsed + stateOf baseCode {pcRevokedAt = revoked} `shouldBe` CSRevoked + stateOf held {pcRevokedAt = revoked} `shouldBe` CSRevoked + stateOf baseCode {pcPaymentStatus = CPSUnpaid} `shouldBe` CSUnpaid + stateOf baseCode {pcPaymentStatus = CPSUnpaid, pcExpiresAt = Just tNow} `shouldBe` CSUnpaid + stateOf baseCode {pcExpiresAt = Just tNow} `shouldBe` CSExpired + stateOf held {pcExpiresAt = Just tNow} `shouldBe` CSReserved + +-- The route as namesRoutes registers it, since a NamesEnv needs a running chat controller. +withCodeCheck :: (DBStore -> Client -> IO a) -> IO a +withCodeCheck action = withServiceStore $ \st -> do + http <- newHttpEnv ListenerConfig {lHost = "127.0.0.1", lBadgesPort = 0, lBadgesStaticDir = "", lServeWebapp = False, lBadgesExportDir = Nothing, lTrustForwardedFor = False, lNamesStaticDir = Nothing, lNamesExportDir = Nothing, lNamesPort = Nothing} + let routes = \case + ["api", "code", "check"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeCheckHandler st} + _ -> Nothing + app req respond = fromMaybe (respond notFound) (namesApp http routes req respond) + mgr <- newManager defaultManagerSettings + Warp.testWithApplication (pure app) $ \port -> action st (mgr, "http://127.0.0.1:" <> show port) + +type Client = (Manager, String) + +postCheck :: Client -> LB.ByteString -> IO (Response LB.ByteString) +postCheck (mgr, base) body = do + req <- parseRequest (base <> "/api/code/check") + httpLbs req {method = "POST", requestBody = RequestBodyLBS body} mgr + +checkText :: Client -> Text -> IO (Response LB.ByteString) +checkText client code = postCheck client (J.encode (J.object ["code" J..= code])) + +checkCode :: Client -> NamePrepaidCode -> IO (Response LB.ByteString) +checkCode client = checkText client . formatNameCode . NCPrepaid + +answerOf :: Response LB.ByteString -> (Int, Maybe J.Object) +answerOf r = (statusCode (responseStatus r), J.decode (responseBody r)) + +coverage :: Int -> Int -> Text -> (Int, Maybe J.Object) +coverage minLength years state = (200, Just $ KM.fromList [("minLength", J.toJSON minLength), ("years", J.toJSON years), ("state", J.String state)]) + +refused :: Int -> Text -> (Int, Maybe J.Object) +refused status tag = (status, Just $ KM.fromList [("error", J.String tag)]) + newCode :: Int -> IO NamePrepaidCode newCode len = C.newRandom >>= (`randomNamePrepaidCode` len) +issueAs :: DBStore -> NamePrepaidCode -> NewPrepaidCode -> IO PrepaidCode +issueAs st code new = withTransaction st $ \db -> do + Right _ <- insertPrepaidCode db new {npcCodeHash = namePrepaidCodeHash code} tNow + fromJust <$> getPrepaidCode db (namePrepaidCodeHash code) + +readCode :: DBStore -> PrepaidCode -> IO (Maybe PrepaidCode) +readCode st PrepaidCode {pcCodeHash} = withTransaction st (`getPrepaidCode` pcCodeHash) + +testCheckStates :: IO () +testCheckStates = withCodeCheck $ \st client -> do + now <- getCurrentTime + [unused, reserved, used, revoked, expired, unpaid] <- mapM newCode [8, 6, 7, 9, 12, 63] + unusedRow <- issueAs st unused usable {npcYears = 5, npcExpiresAt = Just (addUTCTime 3600 now)} + reservedRow <- issueAs st reserved usable {npcMinLength = 6} + usedRow <- issueAs st used usable {npcMinLength = 7} + revokedRow <- issueAs st revoked usable {npcMinLength = 9, npcPaymentStatus = CPSFree} + expiredRow <- issueAs st expired usable {npcMinLength = 12, npcExpiresAt = Just (addUTCTime (-60) now)} + unpaidRow <- issueAs st unpaid usable {npcMinLength = 63, npcYears = 10, npcPaymentStatus = CPSUnpaid} + placeCode st "r1" ownerA (order "alicebakery" 1) reservedRow `shouldReturn` Right () + placeCode st "r2" ownerA (order "bobsbakery" 2) usedRow `shouldReturn` Right () + withTransaction st (\db -> spendPrepaidCode db (pcId usedRow) r2 tNow) `shouldReturn` True + withTransaction st (\db -> revokePrepaidCode db (pcCodeHash revokedRow) tNow) `shouldReturn` Revoked (pcId revokedRow) + let rows = [unusedRow, reservedRow, usedRow, revokedRow, expiredRow, unpaidRow] + rowsBefore <- mapM (readCode st) rows + answerOf <$> checkCode client unused `shouldReturn` coverage 8 5 "unused" + answerOf <$> checkCode client reserved `shouldReturn` coverage 6 2 "reserved" + answerOf <$> checkCode client used `shouldReturn` coverage 7 2 "used" + answerOf <$> checkCode client revoked `shouldReturn` coverage 9 2 "revoked" + answerOf <$> checkCode client expired `shouldReturn` coverage 12 2 "expired" + answerOf <$> checkCode client unpaid `shouldReturn` coverage 63 10 "unpaid" + mapM (readCode st) rows `shouldReturn` rowsBefore + +-- The webapp's and core's vector: these two spellings are one code. +testCheckFolded :: IO () +testCheckFolded = withCodeCheck $ \st client -> do + Just (NCPrepaid code) <- pure $ parseNameCode "SN7-BJTBP-29596-R3HJ8-25YM7" + _ <- issueAs st code usable {npcMinLength = 7} + answerOf <$> checkText client "sn7 bjtbp 29596 r3hj8 25ym7" `shouldReturn` coverage 7 2 "unused" + +testCheckInvalid :: IO () +testCheckInvalid = withCodeCheck $ \st client -> do + Just (NCPrepaid issued) <- pure $ parseNameCode "SN7-BJTBP-29596-R3HJ8-25YM7" + _ <- issueAs st issued usable {npcMinLength = 7} + unknown <- newCode 8 + let invalid = refused 400 "code_invalid" + answerOf <$> checkCode client unknown `shouldReturn` invalid + forM_ + [ "SN7-BJTBP-29596-R3HJ8-25YM8", + "SN07-BJTBP-29596-R3HJ8-25YM7", + "SN-DWPHS-6JBFR-ZR1MA-T5HQ9-P17QM-F5GM5", + "SB-00000-00000-00000-00000", + "alicebakery", + "" + ] + $ \text -> (text,) . answerOf <$> checkText client text `shouldReturn` (text, invalid) + +testCheckBadBody :: IO () +testCheckBadBody = withCodeCheck $ \_ client@(mgr, base) -> do + let badRequest = refused 400 "bad_request" + forM_ ["", "not json", "[\"SN7-BJTBP-29596-R3HJ8-25YM7\"]", "{}", "{\"code\": 7}", "{\"code\": null}"] $ \body -> + (body,) . answerOf <$> postCheck client body `shouldReturn` (body, badRequest) + let overCap = "{\"code\": \"SN7-BJTBP-29596-R3HJ8-25YM7\", \"pad\": \"" <> LB.replicate 8192 'x' <> "\"}" + answerOf <$> postCheck client overCap `shouldReturn` badRequest + req <- parseRequest (base <> "/api/code/check") + r <- httpLbs req mgr + statusCode (responseStatus r) `shouldBe` 405 + lookup "Allow" (responseHeaders r) `shouldBe` Just "POST" + lookup hContentType (responseHeaders r) `shouldBe` Just "application/json" + +testCheckLimit :: IO () +testCheckLimit = withCodeCheck $ \st client -> do + issued <- newCode 8 + _ <- issueAs st issued usable + unknown <- newCode 8 + replicateM_ 5 $ checkCode client issued >>= (`shouldBe` 200) . fst . answerOf + replicateM_ 4 $ checkCode client unknown >>= (`shouldBe` 400) . fst . answerOf + postCheck client "{}" >>= (`shouldBe` 400) . fst . answerOf + limited <- checkCode client issued + answerOf limited `shouldBe` refused 429 "rate_limited" + lookup "Retry-After" (responseHeaders limited) `shouldSatisfy` maybe False (BC.all (`elem` ['0' .. '9'])) + +-- | Two paid orders, so a code can be reserved for either. +withOrders :: (Turns -> DBStore -> IO a) -> IO a +withOrders action = withServiceStore $ \st -> do + placePaid st "r1" ownerA (order "alicebakery" 1) tNow `shouldReturn` Right () + placePaid st "r2" ownerB (order "bobsbakery" 2) tNow `shouldReturn` Right () + turns <- newTurns + action turns st + +writeOk :: DB.Connection -> IO (Either Text ()) +writeOk _ = pure $ Right () + +testReserve :: IO () +testReserve = withOrders $ \turns st -> do + code <- newCode 8 + PrepaidCode {pcCodeHash} <- issueAs st code usable + let holderInWrite db = Right . (pcReservedBy =<<) <$> getPrepaidCode db pcCodeHash :: IO (Either Text (Maybe RegistrationId)) + reserveCode turns st tNow pcCodeHash r1 8 holderInWrite `shouldReturn` Right (Right Nothing) + (pcReservedBy =<<) <$> withTransaction st (`getPrepaidCode` pcCodeHash) `shouldReturn` Just r1 + reserveCode turns st tNow pcCodeHash r1 11 holderInWrite `shouldReturn` Right (Right (Just r1)) + +testReserveRefusals :: IO () +testReserveRefusals = withOrders $ \turns st -> do + [reserved, used, revoked, expired, unpaid, unknown, short] <- mapM newCode [8, 8, 8, 8, 8, 8, 8] + reservedRow <- issueAs st reserved usable + usedRow <- issueAs st used usable + revokedRow <- issueAs st revoked usable + expiredRow <- issueAs st expired usable {npcExpiresAt = Just tNow} + unpaidRow <- issueAs st unpaid usable {npcPaymentStatus = CPSUnpaid} + shortRow <- issueAs st short usable + placeCode st "r5" ownerA (order "erinsbakery" 5) reservedRow `shouldReturn` Right () + placeCode st "r3" ownerB (order "carolsbakery" 4) usedRow `shouldReturn` Right () + withTransaction st (\db -> spendPrepaidCode db (pcId usedRow) (RegistrationId "r3") tNow) `shouldReturn` True + withTransaction st (\db -> revokePrepaidCode db (pcCodeHash revokedRow) tNow) `shouldReturn` Revoked (pcId revokedRow) + wrote <- newIORef False + let reserve hash len = reserveCode turns st tNow hash r1 len (\_ -> Right () <$ writeIORef wrote True :: IO (Either Text ())) + rows = [reservedRow, usedRow, revokedRow, expiredRow, unpaidRow, shortRow] + rowsBefore <- mapM (readCode st) rows + reserve (pcCodeHash reservedRow) 8 `shouldReturn` Left CRReserved + reserve (pcCodeHash usedRow) 8 `shouldReturn` Left CRUsed + reserve (pcCodeHash revokedRow) 8 `shouldReturn` Left CRInvalid + reserve (pcCodeHash expiredRow) 8 `shouldReturn` Left CRExpired + reserve (pcCodeHash unpaidRow) 8 `shouldReturn` Left CRPaymentPending + reserve (namePrepaidCodeHash unknown) 8 `shouldReturn` Left CRInvalid + reserve (pcCodeHash shortRow) 7 `shouldReturn` Left CRNotCovering + reserve (pcCodeHash revokedRow) 7 `shouldReturn` Left CRInvalid + readIORef wrote `shouldReturn` False + mapM (readCode st) rows `shouldReturn` rowsBefore + map codeRefusalTag [CRInvalid, CRUsed, CRReserved, CRExpired, CRPaymentPending, CRNotCovering] + `shouldBe` ["code_invalid", "code_used", "code_reserved", "code_expired", "payment_pending", "code_not_covering"] + +testReserveHeld :: IO () +testReserveHeld = withOrders $ \turns st -> do + code <- newCode 8 + row@PrepaidCode {pcCodeHash} <- issueAs st code usable + placeCode st "r3" ownerB (order "carolsbakery" 3) row `shouldReturn` Right () + let r3 = RegistrationId "r3" + withTransaction st (\db -> revokePrepaidCode db pcCodeHash tNow) `shouldReturn` Revoked (pcId row) + withTransaction st $ \db -> DB.execute db "UPDATE sx_badge_service_name_prepaid_codes SET expires_at = ? WHERE code_hash = ?" (tNow, DB.Binary pcCodeHash) + held <- readCode st row + reserveCode turns st tNow pcCodeHash r3 9 writeOk `shouldReturn` Right (Right ()) + reserveCode turns st tNow pcCodeHash r3 7 writeOk `shouldReturn` Left CRNotCovering + reserveCode turns st tNow pcCodeHash r1 9 writeOk `shouldReturn` Left CRInvalid + readCode st row `shouldReturn` held + +testReserveSpent :: IO () +testReserveSpent = withOrders $ \turns st -> do + code <- newCode 8 + row@PrepaidCode {pcCodeHash} <- issueAs st code usable + placeCode st "r3" ownerB (order "carolsbakery" 3) row `shouldReturn` Right () + let r3 = RegistrationId "r3" + withTransaction st (\db -> spendPrepaidCode db (pcId row) r3 tNow) `shouldReturn` True + spent <- readCode st row + wrote <- newIORef False + reserveCode turns st tNow pcCodeHash r3 8 (\_ -> Right () <$ writeIORef wrote True :: IO (Either Text ())) `shouldReturn` Left CRUsed + readIORef wrote `shouldReturn` False + readCode st row `shouldReturn` spent + +testReserveWriteRefused :: IO () +testReserveWriteRefused = withOrders $ \turns st -> do + code <- newCode 8 + row@PrepaidCode {pcCodeHash} <- issueAs st code usable + reserveCode turns st tNow pcCodeHash r1 8 (\_ -> pure (Left "registration_changed" :: Either Text ())) `shouldReturn` Right (Left "registration_changed") + (pcReservedBy =<<) <$> readCode st row `shouldReturn` Nothing + reserveCode turns st tNow pcCodeHash r2 8 writeOk `shouldReturn` Right (Right ()) + (pcReservedBy =<<) <$> readCode st row `shouldReturn` Just r2 + +-- | The write reserves the code for another registration, as a writer outside this process could between the check and the reservation. +testReserveRollsBack :: IO () +testReserveRollsBack = withOrders $ \turns st -> do + code <- newCode 8 + row@PrepaidCode {pcId, pcCodeHash} <- issueAs st code usable + let takenMeanwhile db = Right () <$ reservePrepaidCode db pcId r2 tNow :: IO (Either Text ()) + reserveCode turns st tNow pcCodeHash r1 8 takenMeanwhile `shouldReturn` Left CRReserved + readCode st row `shouldReturn` Just row + +testConcurrentReserve :: IO () +testConcurrentReserve = withOrders $ \turns st -> do + code <- newCode 8 + row@PrepaidCode {pcCodeHash} <- issueAs st code usable + let slowWrite _ = Right () <$ threadDelay 100000 :: IO (Either Text ()) + reserveFor rid = reserveCode turns st tNow pcCodeHash rid 8 slowWrite + outcomes <- concurrently (reserveFor r1) (reserveFor r2) + outcomes `shouldSatisfy` (`elem` [(Right (Right ()), Left CRReserved), (Left CRReserved, Right (Right ()))]) + holder <- (pcReservedBy =<<) <$> readCode st row + holder `shouldBe` Just (if fst outcomes == Right (Right ()) then r1 else r2) + +testReleaseSpend :: IO () +testReleaseSpend = withOrders $ \turns st -> do + [spent, released] <- mapM newCode [8, 8] + spentRow <- issueAs st spent usable + releasedRow <- issueAs st released usable + placeCode st "r3" ownerB (order "carolsbakery" 3) spentRow `shouldReturn` Right () + placeCode st "r4" ownerB (order "davesbakery" 4) releasedRow `shouldReturn` Right () + let r3 = RegistrationId "r3" + r4 = RegistrationId "r4" + holderInWrite row db = Right . (pcReservedBy =<<) <$> getPrepaidCode db (pcCodeHash row) :: IO (Either Text (Maybe RegistrationId)) + changed _ = pure (Left "registration_changed") :: IO (Either Text ()) + spendCode turns st tNow (pcId spentRow) r3 changed `shouldReturn` Left "registration_changed" + (pcSpentAt =<<) <$> readCode st spentRow `shouldReturn` Nothing + spendCode turns st tNow (pcId spentRow) r1 writeOk `shouldReturn` Right ((), False) + spendCode turns st tNow (pcId spentRow) r3 writeOk `shouldReturn` Right ((), True) + ((pcReservedBy =<<) &&& (pcSpentAt =<<)) <$> readCode st spentRow `shouldReturn` (Just r3, Just tNow) + releaseCode turns st (pcId spentRow) r3 writeOk `shouldReturn` Right ((), False) + releaseCode turns st (pcId releasedRow) r4 changed `shouldReturn` Left "registration_changed" + releaseCode turns st (pcId releasedRow) r1 writeOk `shouldReturn` Right ((), False) + releaseCode turns st (pcId releasedRow) r4 (holderInWrite releasedRow) `shouldReturn` Right (Just r4, True) + (pcReservedBy =<<) <$> readCode st releasedRow `shouldReturn` Nothing + testGroupNameCommands :: IO () testGroupNameCommands = do let p = groupCmdAction GRModerator diff --git a/tests/Bots/StoreService/NamesPhasesTests.hs b/tests/Bots/StoreService/NamesPhasesTests.hs new file mode 100644 index 0000000000..56fe224215 --- /dev/null +++ b/tests/Bots/StoreService/NamesPhasesTests.hs @@ -0,0 +1,321 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TupleSections #-} + +module Bots.StoreService.NamesPhasesTests (namesPhasesTests, change, held, woken, prompt, registrationPath, seenVersion) where + +import Bots.StoreService.NamesOrdersTests +import Bots.StoreService.NamesStoreTests (reveal) +import Bots.StoreService.WebTests (bounded, jsonObject, rcv, statusOf, webGet) +import Control.Concurrent (threadDelay) +import Control.Concurrent.Async (Async, async, cancel, wait, waitCatchSTM) +import qualified Control.Concurrent.Async as Async +import Control.Concurrent.STM (STM, atomically, check, orElse) +import Control.Monad (forM_) +import qualified Data.Aeson as J +import qualified Data.Aeson.Key as K +import qualified Data.Aeson.KeyMap as KM +import Data.IORef (readIORef, writeIORef) +import Data.Int (Int64) +import Data.Maybe (fromJust, fromMaybe, isNothing) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, diffUTCTime, getCurrentTime) +import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds) +import Network.HTTP.Client (defaultManagerSettings, newManager) +import qualified Network.Wai.Handler.Warp as Warp +import Simplex.Chat.PaymentService.Types (InvoiceId (..), InvoiceStatus (..)) +import Simplex.Messaging.Agent.Store.Common (withTransaction) +import StoreService.Names.Orders (OrdersEnv (..)) +import StoreService.Names.Phases (RegistrationId (..), phasesWaiting, publish) +import StoreService.Names.Store (NamePhase (..), NameRegistration (..), VersionWrite (..), getRegistration, updateRegistration) +import StoreService.Providers (Funded (..), PaymentSignal (..)) +import StoreService.Store.Invoices (InvoiceRow (..), getInvoice) +import StoreService.Waiters (publishPayment, waitingCountSTM) +import StoreService.Web.Http (holdMicros, readLimit) +import StoreService.Web.Names (Route (..), namesApp) +import StoreService.Web.Names.Registration (registrationHandler) +import StoreService.Web.Server (WebEnv (..), webApp) +import System.Timeout (timeout) +import Test.Hspec + +namesPhasesTests :: Spec +namesPhasesTests = describe "StoreService names: phases" $ do + describe "changes" $ do + it "wakes a held invoice poll and a held registration poll on each change: phase, label set and cleared, reveal times, error" testEachChangeWakes + it "holds a paid name invoice on its registration's version alone, and answers at once without a valid version" testPaidHoldsOnVersion + it "answers at once an invoice paid after its order closed" testLatePaidAnswersAtOnce + it "wakes an open invoice poll by a version change, and holds it on its status without a valid version" testOpenWakesOnVersion + describe "time" $ do + it "wakes an unpaid invoice held on its status at its expiry, reading it expired" testExpiryWakes + it "reads a funded open invoice past its expiry as open, and holds it" testFundedStaysOpen + it "reads a committed registration with no reveal past revealBefore as failed, waking held registration and invoice polls" testLapseReadsFailed + describe "registration poll" $ do + it "answers at once without seenVersion, with another version, or with one that is not a number" testAnswersAtOnce + it "answers unchanged after the hold" testHoldTimesOut + it "answers an unknown registration 404" testUnknownRegistration + it "leaves waitingCount to polls waiting on a payment" testWaitingCount + +data Polls = Polls + { pCtx :: Ctx, + -- | Sets the service's clock, which then runs on in real time, so a hold can end at a moment. + pSetNow :: UTCTime -> IO () + } + +-- | The registration route as namesRoutes registers it, beside the invoice routes, since a NamesEnv needs a running chat controller. +withPolls :: Int -> (Polls -> IO a) -> IO a +withPolls hold action = withOrders $ \ctx@Ctx {cClock, cOrders, cWeb} -> do + started <- getCurrentTime + let elapsed = (`diffUTCTime` started) <$> getCurrentTime + running = addUTCTime <$> elapsed <*> readIORef cClock + orders = cOrders {oeNow = running} + web = cWeb {weOrders = Just orders} + routes = \case + ["api", "registration", rid] -> Just Route {rMethod = "GET", rLimit = readLimit, rHandler = registrationHandler orders hold rid} + _ -> Nothing + app req respond = fromMaybe (webApp web req respond) (namesApp (weHttp web) routes req respond) + pSetNow t = elapsed >>= \e -> writeIORef cClock (addUTCTime (negate e) t) + mgr <- newManager defaultManagerSettings + Warp.testWithApplication (pure app) $ \port -> + action Polls {pCtx = ctx {cOrders = orders, cWeb = web, cClient = (mgr, "http://127.0.0.1:" <> show port)}, pSetNow} + +-- | How long a held poll is watched for a wake that must not come. +parkDelay :: Int +parkDelay = 200000 + +-- | How long a poll may take to be held or to answer once woken; far below the hold. +prompt :: Int +prompt = 6000000 + +-- | How far ahead of a moment the clock is set, so a poll is held well before the moment wakes it. +shortOfMoment :: NominalDiffTime +shortOfMoment = 3 + +shortHold :: Int +shortHold = 500000 + +get :: Ctx -> String -> IO (Int, J.Object) +get Ctx {cClient} path = webGet cClient path >>= \r -> (statusOf r,) <$> jsonObject r + +-- | The holds the server has taken: invoices held on a payment, and waiters on a registration's version. +subscriptions :: Ctx -> STM (Int, Int) +subscriptions Ctx {cWeb, cOrders} = (,) <$> waitingCountSTM (weWaiters cWeb) <*> phasesWaiting (oePhases cOrders) + +-- | A poll once the server holds it, having taken these many more holds of each kind. +held :: Ctx -> (Int, Int) -> String -> IO (Async (Int, J.Object)) +held ctx (payments, versions) path = do + (payments0, versions0) <- atomically $ subscriptions ctx + poll <- async $ get ctx path + let holding = subscriptions ctx >>= check . (== (payments0 + payments, versions0 + versions)) + timeout prompt (atomically $ (Left <$> waitCatchSTM poll) `orElse` (Right <$> holding)) >>= \case + Just (Right ()) -> pure poll + Just (Left answered) -> fail $ path <> " was answered at once: " <> show answered + Nothing -> cancel poll >> fail (path <> " was not held with " <> show (payments, versions) <> " more holds") + +woken :: String -> Async (Int, J.Object) -> IO (Int, J.Object) +woken what poll = timeout prompt (wait poll) >>= maybe (cancel poll >> fail (what <> " was not woken")) pure + +answeredAtOnce :: Ctx -> String -> IO (Int, J.Object) +answeredAtOnce ctx path = timeout prompt (get ctx path) >>= maybe (fail (path <> " was held")) pure + +invoicePath :: InvoiceId -> String +invoicePath (InvoiceId t) = "/api/invoice/" <> T.unpack t + +registrationPath :: RegistrationId -> String +registrationPath (RegistrationId t) = "/api/registration/" <> T.unpack t + +-- | The query the page sends to follow an invoice, as it last saw it. +following :: InvoiceStatus -> Int64 -> Text -> Bool -> String +following status version = followingAs status (Just $ show version) + +-- | With seenVersion as given, a number or not, or left out. +followingAs :: InvoiceStatus -> Maybe String -> Text -> Bool -> String +followingAs status version paid full = + "?wait=" <> statusText <> maybe "" ("&seenVersion=" <>) version <> "&seenPaid=" <> T.unpack paid <> "&seenFull=" <> (if full then "1" else "0") + where + statusText = case status of + ISOpen -> "open" + ISPaid -> "paid" + ISExpired -> "expired" + +seenVersion :: Int64 -> String +seenVersion v = "?seenVersion=" <> show v + +-- | A write as the worker or a reveal makes it: a compare-and-set, then a publish after the commit. +change :: Ctx -> RegistrationId -> (NameRegistration -> NameRegistration) -> IO Int64 +change Ctx {cStore, cOrders, cClock} rid f = do + now <- readIORef cClock + written <- withTransaction cStore $ \db -> getRegistration db rid >>= \r -> updateRegistration db (f $ fromJust r) now + case written of + Written v -> v <$ atomically (publish (oePhases cOrders) rid v) + other -> fail $ "registration not written: " <> show other + +key :: Text -> J.Object -> Maybe J.Value +key k = KM.lookup (K.fromText k) + +seconds :: UTCTime -> Integer +seconds = floor . utcTimeToPOSIXSeconds + +-- | The settled payment's marks: its crypto amount and paid in full. +settledPaid :: Text +settledPaid = "0.00156" + +-- tests + +-- | Paid and queued at version 2; each change below is one the worker, a reveal or a re-commit makes. +testEachChangeWakes :: IO () +testEachChangeWakes = bounded "each change wakes" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (inv, _) <- placed ctx aliceOrder + settleNow ctx inv settled `shouldReturn` Right ISPaid + NameRegistration {nrId} <- invoiceRegistration ctx inv + let day = seconds orderTime + 86400 + changes :: [(String, NameRegistration -> NameRegistration, Text, J.Value)] + changes = + [ ("phase", \r -> r {nrPhase = PCommitting}, "phase", "committing"), + ("committed", \r -> r {nrPhase = PCommitted, nrRevealAfter = Just (fromInteger day), nrRevealBefore = Just (fromInteger $ day + 86400)}, "revealBefore", J.toJSON (day + 86400)), + ("label set", \r -> r {nrReveal = Just reveal}, "label", "alicebakery"), + ("label cleared", \r -> r {nrReveal = Nothing}, "label", J.Null), + ("reveal times after a re-commit", \r -> r {nrRevealAfter = Just (fromInteger $ day + 60), nrRevealBefore = Just (fromInteger $ day + 86460)}, "revealBefore", J.toJSON (day + 86460)), + ("error", \r -> r {nrPhase = PFailed, nrError = Just "The commit transaction failed"}, "error", "The commit transaction failed") + ] + forM_ changes $ \(what, f, k, expected) -> do + v <- nrVersion <$> invoiceRegistration ctx inv + invoicePoll <- held ctx (0, 1) $ invoicePath inv <> following ISPaid v settledPaid True + registrationPoll <- held ctx (0, 1) $ registrationPath nrId <> seenVersion v + v' <- change ctx nrId f + (status, o) <- woken (what <> ": invoice poll") invoicePoll + (status, key "status" o, key "version" (registrationOf o), key k (registrationOf o)) `shouldBe` (200, Just "paid", Just (J.toJSON v'), Just expected) + (status', r) <- woken (what <> ": registration poll") registrationPoll + (status', key "version" r, key k r) `shouldBe` (200, Just (J.toJSON v'), Just expected) + +testPaidHoldsOnVersion :: IO () +testPaidHoldsOnVersion = bounded "paid holds on version" $ withPolls holdMicros $ \Polls {pCtx = ctx@Ctx {cWeb}} -> do + (inv, _) <- placed ctx aliceOrder + settleNow ctx inv settled `shouldReturn` Right ISPaid + NameRegistration {nrId, nrVersion} <- invoiceRegistration ctx inv + -- without a version there is nothing a paid invoice could wait for + forM_ [Nothing, Just "two"] $ \version -> + (fst <$> answeredAtOnce ctx (invoicePath inv <> followingAs ISPaid version settledPaid True)) `shouldReturn` 200 + poll <- held ctx (0, 1) $ invoicePath inv <> following ISPaid nrVersion settledPaid True + atomically $ publishPayment (weWaiters cWeb) inv + threadDelay parkDelay + Async.poll poll >>= (`shouldSatisfy` isNothing) + v' <- change ctx nrId $ \r -> r {nrPhase = PCommitting} + (_, o) <- woken "paid invoice poll" poll + (key "version" (registrationOf o), key "phase" (registrationOf o)) `shouldBe` (Just (J.toJSON v'), Just "committing") + -- marks other than the page saw answer at once + (fst <$> answeredAtOnce ctx (invoicePath inv <> following ISPaid v' "0.001" True)) `shouldReturn` 200 + +testLatePaidAnswersAtOnce :: IO () +testLatePaidAnswersAtOnce = bounded "late paid" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (inv, _) <- placed ctx aliceOrder + fst <$> post ctx (invoicePath inv <> "/cancel") "" `shouldReturn` 200 + settleNow ctx inv settled `shouldReturn` Right ISPaid + NameRegistration {nrVersion} <- invoiceRegistration ctx inv + (status, o) <- answeredAtOnce ctx $ invoicePath inv <> following ISPaid nrVersion settledPaid True + (status, key "status" o, key "phase" (registrationOf o)) `shouldBe` (200, Just "paid", Just "unpaid") + +testOpenWakesOnVersion :: IO () +testOpenWakesOnVersion = bounded "open wakes on version" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (inv, _) <- placed ctx aliceOrder + NameRegistration {nrId, nrVersion} <- invoiceRegistration ctx inv + -- a version the page has not seen answers at once + (fst <$> answeredAtOnce ctx (invoicePath inv <> following ISOpen (nrVersion - 1) "" False)) `shouldReturn` 200 + poll <- held ctx (1, 1) $ invoicePath inv <> following ISOpen nrVersion "" False + v' <- change ctx nrId $ \r -> r {nrError = Just "Checking the payment"} + (_, o) <- woken "open invoice poll" poll + (key "status" o, key "version" (registrationOf o)) `shouldBe` (Just "open", Just (J.toJSON v')) + -- a seenVersion that is no version leaves the hold on the status and payment alone + byStatus <- held ctx (1, 0) $ invoicePath inv <> followingAs ISOpen (Just "two") "" False + settleNow ctx inv (SigFunded (rcv 5000 (Just "0.00078")) PaidInPart) `shouldReturn` Right ISOpen + (_, paidInPart) <- woken "open invoice poll without a version" byStatus + (key "status" paidInPart, key "cryptoAmountPaid" paidInPart) `shouldBe` (Just "open", Just "0.00078") + +-- | The stub's crypto invoice expires 45 minutes after the order; the clock is set just short of it. +testExpiryWakes :: IO () +testExpiryWakes = bounded "expiry wakes" $ withPolls holdMicros $ \Polls {pCtx = ctx, pSetNow} -> do + (inv, _) <- placed ctx aliceOrder + Just InvoiceRow {irExpiresAt} <- getInvoice (cStore ctx) inv + NameRegistration {nrVersion} <- invoiceRegistration ctx inv + pSetNow $ addUTCTime (negate shortOfMoment) irExpiresAt + poll <- held ctx (1, 0) $ invoicePath inv <> followingAs ISOpen Nothing "" False + (status, o) <- woken "unpaid invoice poll" poll + (status, key "status" o, key "version" (registrationOf o)) `shouldBe` (200, Just "expired", Just (J.toJSON nrVersion)) + -- read so, not written: the sweep or the next order closes it + (irStatus <$>) <$> getInvoice (cStore ctx) inv `shouldReturn` Just ISOpen + (fst <$> answeredAtOnce ctx (invoicePath inv <> following ISOpen nrVersion "" False)) `shouldReturn` 200 + +testFundedStaysOpen :: IO () +testFundedStaysOpen = bounded "funded stays open" $ withPolls holdMicros $ \Polls {pCtx = ctx, pSetNow} -> do + (inv, _) <- placed ctx aliceOrder + settleNow ctx inv (SigFunded (rcv 5000 (Just "0.00078")) PaidInPart) `shouldReturn` Right ISOpen + Just InvoiceRow {irExpiresAt} <- getInvoice (cStore ctx) inv + NameRegistration {nrVersion} <- invoiceRegistration ctx inv + pSetNow $ addUTCTime 60 irExpiresAt + (status, o) <- get ctx (invoicePath inv) + (status, key "status" o) `shouldBe` (200, Just "open") + poll <- held ctx (1, 1) $ invoicePath inv <> following ISOpen nrVersion "0.00078" False + cancel poll + +testLapseReadsFailed :: IO () +testLapseReadsFailed = bounded "lapse" $ withPolls holdMicros $ \Polls {pCtx = ctx@Ctx {cOrders}, pSetNow} -> do + (inv, _) <- placed ctx aliceOrder + settleNow ctx inv settled `shouldReturn` Right ISPaid + NameRegistration {nrId} <- invoiceRegistration ctx inv + now <- oeNow cOrders + let revealBefore = seconds now + 600 + lapsedAt = posixSecondsToUTCTime (fromInteger revealBefore) + pSetNow $ addUTCTime (negate shortOfMoment) lapsedAt + v <- change ctx nrId $ \r -> r {nrPhase = PCommitted, nrRevealAfter = Just (fromInteger $ revealBefore - 3600), nrRevealBefore = Just (fromInteger revealBefore)} + (_, r) <- get ctx (registrationPath nrId) + key "phase" r `shouldBe` Just "committed" + invoicePoll <- held ctx (0, 1) $ invoicePath inv <> following ISPaid v settledPaid True + poll <- held ctx (0, 1) $ registrationPath nrId <> seenVersion v + (status, lapsed) <- woken "registration poll at the lapse" poll + (status, key "phase" lapsed, key "error" lapsed, key "version" lapsed) `shouldBe` (200, Just "failed", Just "The name was not signed in time", Just (J.toJSON v)) + (_, o) <- woken "invoice poll at the lapse" invoicePoll + (key "status" o, key "phase" (registrationOf o), key "error" (registrationOf o)) `shouldBe` (Just "paid", Just "failed", Just "The name was not signed in time") + (nrPhase <$> invoiceRegistration ctx inv) `shouldReturn` PCommitted + -- one with a reveal set stays committed past revealBefore + revealed <- change ctx nrId $ \r' -> r' {nrReveal = Just reveal} + pSetNow $ addUTCTime 3600 lapsedAt + (_, r') <- get ctx (registrationPath nrId) + (key "phase" r', key "label" r', key "version" r') `shouldBe` (Just "committed", Just "alicebakery", Just (J.toJSON revealed)) + +testAnswersAtOnce :: IO () +testAnswersAtOnce = bounded "at once" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (inv, _) <- placed ctx aliceOrder + NameRegistration {nrId = rid@(RegistrationId ridText), nrVersion} <- invoiceRegistration ctx inv + forM_ ["", seenVersion (nrVersion + 1), seenVersion (nrVersion - 1), "?seenVersion=one", "?seenVersion=-1", "?seenVersion=99999999999999999999"] $ \q -> do + (status, r) <- answeredAtOnce ctx (registrationPath rid <> q) + (q, status, key "id" r, key "version" r, key "phase" r) `shouldBe` (q, 200, Just (J.String ridText), Just (J.toJSON nrVersion), Just "unpaid") + +testHoldTimesOut :: IO () +testHoldTimesOut = bounded "hold times out" $ withPolls shortHold $ \Polls {pCtx = ctx} -> do + (inv, _) <- placed ctx aliceOrder + NameRegistration {nrId, nrVersion} <- invoiceRegistration ctx inv + started <- getCurrentTime + (status, r) <- get ctx $ registrationPath nrId <> seenVersion nrVersion + waited <- (`diffUTCTime` started) <$> getCurrentTime + (status, key "version" r) `shouldBe` (200, Just (J.toJSON nrVersion)) + waited `shouldSatisfy` (>= (fromIntegral shortHold / 1000000 :: NominalDiffTime)) + +testUnknownRegistration :: IO () +testUnknownRegistration = bounded "unknown" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (status, o) <- answeredAtOnce ctx (registrationPath (RegistrationId "t2Jk8RmQ4vXw9NcP3yLb6g") <> seenVersion 1) + (status, o) `shouldBe` (404, KM.fromList [("error", "not_found")]) + +-- | The poller reads providers sooner while a page waits on a payment; a hold on a version waits on none. +testWaitingCount :: IO () +testWaitingCount = bounded "waiting count" $ withPolls holdMicros $ \Polls {pCtx = ctx} -> do + (paid, _) <- placed ctx aliceOrder + settleNow ctx paid settled `shouldReturn` Right ISPaid + NameRegistration {nrId, nrVersion} <- invoiceRegistration ctx paid + polls <- mapM (held ctx (0, 1)) [registrationPath nrId <> seenVersion nrVersion, invoicePath paid <> following ISPaid nrVersion settledPaid True] + atomically (subscriptions ctx) `shouldReturn` (0, 2) + (open, _) <- placed ctx aliceOrder {oLabel = "dynamis", oPrice = 20000, oCommitment = commitmentOf 2} + NameRegistration {nrVersion = openVersion} <- invoiceRegistration ctx open + openPoll <- held ctx (1, 1) $ invoicePath open <> following ISOpen openVersion "" False + atomically (subscriptions ctx) `shouldReturn` (1, 3) + mapM_ cancel (openPoll : polls) diff --git a/tests/Bots/StoreService/NamesSignatureTests.hs b/tests/Bots/StoreService/NamesSignatureTests.hs new file mode 100644 index 0000000000..aee9554c47 --- /dev/null +++ b/tests/Bots/StoreService/NamesSignatureTests.hs @@ -0,0 +1,158 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Bots.StoreService.NamesSignatureTests (namesSignatureTests) where + +import Control.Monad (forM_) +import Data.ByteArray.Encoding (Base (Base16), convertFromBase) +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import Data.Maybe (fromMaybe) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Text.Encoding (encodeUtf8) +import Data.Word (Word64) +import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature (..)) +import Simplex.Messaging.Encoding.String (strDecode) +import Simplex.Messaging.Eth.Address (Address, recoverAddress) +import Simplex.Messaging.SimplexName (SimplexTLD (..), labelHash) +import StoreService.Names.Abi +import StoreService.Names.Registration +import StoreService.Names.Signatures +import Test.Hspec + +namesSignatureTests :: Spec +namesSignatureTests = describe "StoreService names: signatures" $ do + -- expected values are viem 2.57.5's for the fixtures below, the webapp's wallet (BIP-39 entropy 6f2d1c9a4b7e3f80d15a2c6e9b04f7a3, account 0) + -- and the SimplexSponsor, SimplexController, SimplexResolver and SimplexPriceOracle ABIs of ens-contracts PR #36 at 23892cc + describe "EIP-712" $ do + it "commits to a registration with one contact link" $ + makeCommitment dynamis `shouldBe` commitment + it "hashes RegisterName" $ + registerIntentDigest (sponsorDomain chainId sponsor) dynamis 0 1800000000 `shouldBe` hx "0x4b0bcc282db581446f6c25e0e3fbe2839409acbc011bb3e082523dd9eab85a6a" + describe "EIP-191 messages" $ do + it "signs an order" $ + testMessage + (orderMessage dynamisOrder timestamp) + "simplex.domains order 0x78b602f03f164366531da963764c1f2fdfd588d0487daf2aa261ca799e5ffe33 7 69259500 0x9cf04ddef2eedfb02e6e7ba58f879c0636c584b6acc67ffe9f97a06bfb016ac1 0xb1971beb567c8dc1b2f01f0d823754c6168d8d6830db2221702a7678e1804674 1792574100" + "0x5deee1b3589df9e53665e34aa65a491a4eabfd76a39207b0e9303457f7c44b7a" + "0x73e32be461e99e64b093a4ad09b04b4d810e5623a1b0e9c9fdb4701ec2a6a2a84df9ffdccc39c5cb827cde4d846c7662f591665f1b868ab1c15778646d3f414e1b" + describe "signature format" $ do + it "refuses a high s, though recovery accepts it" testHighS + it "refuses v other than 27 or 28" $ + forM_ ["00", "01", "1d"] $ \v -> parseSignature (T.dropEnd 2 orderSignature <> v) `shouldBe` Left SERecoveryId + it "refuses uppercase hex, no 0x and a wrong length" $ do + parseSignature ("0x" <> T.toUpper (T.drop 2 orderSignature)) `shouldBe` Left SEFormat + parseSignature (T.drop 2 orderSignature) `shouldBe` Left SEFormat + parseSignature (T.dropEnd 2 orderSignature) `shouldBe` Left SEFormat + parseSignature (orderSignature <> "00") `shouldBe` Left SEFormat + describe "addresses" $ do + it "accepts the EIP-55 form in orders" $ + parseChecksumAddress ownerText `shouldBe` Right owner + it "refuses a letter in the wrong case and one-case hex in orders" $ do + parseChecksumAddress "0x014F9c09CcEeDEF1B713110d04D3a07d5B5D9b73" `shouldBe` Left ADChecksum + parseChecksumAddress ownerLower `shouldBe` Left ADChecksum + parseChecksumAddress ownerUpper `shouldBe` Left ADChecksum + it "accepts any case in find" $ + forM_ [ownerText, ownerLower, ownerUpper, "0x014F9c09CcEeDEF1B713110d04D3a07d5B5D9b73"] $ \a -> + parseAnyCaseAddress a `shouldBe` Right owner + it "refuses what is not an address" $ + forM_ ["014f9c09cceedef1b713110d04d3a07d5b5d9b73", "0X014f9c09cceedef1b713110d04d3a07d5b5d9b73", T.dropEnd 1 ownerLower, ownerLower <> "0", "0x014f9c09cceedef1b713110d04d3a07d5b5d9b7g"] $ \a -> do + parseAnyCaseAddress a `shouldBe` Left ADFormat + parseChecksumAddress a `shouldBe` Left ADFormat + describe "ABI return data" $ do + it "decodes each type as viem encodes it" $ do + decodeUint256 (hx "00000000000000000000000000000000000000018ee90ff6c373e0ee4e3f0ad2") `shouldBe` Right 123456789012345678901234567890 + decodeAddress (hx "000000000000000000000000014f9c09cceedef1b713110d04d3a07d5b5d9b73") `shouldBe` Right owner + decodeBool (hx "0000000000000000000000000000000000000000000000000000000000000001") `shouldBe` Right True + decodeBool (B.replicate 32 0) `shouldBe` Right False + decodeBytes32 (hx "833cc73a7eae1ddee67ec8845ec3d39799406c7bc87fab7c504a7e0ab5cb89bb") `shouldBe` Right node + decodeString retString `shouldBe` Right "https://smp8.simplex.im/a#contact" + decodeString (hx "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000") `shouldBe` Right "" + decodeBytes (hx "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000010410f13a8c833cc73a7eae1ddee67ec8845ec3d39799406c7bc87fab7c504a7e0ab5cb89bb000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000f73696d706c65782e636f6e746163740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002168747470733a2f2f736d70382e73696d706c65782e696d2f6123636f6e746163740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") `shouldBe` Right (hx "10f13a8c833cc73a7eae1ddee67ec8845ec3d39799406c7bc87fab7c504a7e0ab5cb89bb000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000f73696d706c65782e636f6e746163740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002168747470733a2f2f736d70382e73696d706c65782e696d2f6123636f6e7461637400000000000000000000000000000000000000000000000000000000000000") + it "refuses data that does not hold the type" $ do + decodeUint256 (B.replicate 31 0) `shouldBe` Left AEShort + decodeAddress (B.singleton 1 <> B.drop 1 (hx "000000000000000000000000014f9c09cceedef1b713110d04d3a07d5b5d9b73")) `shouldBe` Left AENotAddress + decodeBool (uint256 (2 :: Int)) `shouldBe` Left AENotBool + decodeString (uint256 (4096 :: Int) <> B.drop 32 retString) `shouldBe` Left AEOffset + decodeString (B.take 80 retString) `shouldBe` Left AEShort + decodeString (abiTuple [Dynamic $ abiBytes "\xff"]) `shouldBe` Left AENotUtf8 + +testMessage :: ByteString -> ByteString -> Text -> Text -> IO () +testMessage msg expected digest signature = do + msg `shouldBe` expected + personalDigest msg `shouldBe` hx digest + sig <- either (fail . show) pure $ parseSignature signature + recoverAddress sig (personalDigest msg) `shouldReturn` Right owner + +testHighS :: IO () +testHighS = do + parseSignature highS `shouldBe` Left SEHighS + let (rsR, (rsS, v)) = B.splitAt 32 <$> B.splitAt 32 (hx highS) + recoverAddress RecoverableSignature {rsR, rsS, rsRecId = B.head v - 27} (personalDigest $ orderMessage dynamisOrder timestamp) + `shouldReturn` Right owner + where + highS = "0x73e32be461e99e64b093a4ad09b04b4d810e5623a1b0e9c9fdb4701ec2a6a2a8b206002333c63a347d8321b27b93899bc51d768793c21589fe7ae62862f6fff31c" + +-- fixtures + +dynamis :: Registration +dynamis = + Registration + { rLabel = "dynamis", + rTLD = TLDSimplex, + rOwner = owner, + rDuration = duration, + rSecret = B.replicate 32 0x11, + rResolver = resolver, + rContactLink = Just "https://smp8.simplex.im/a#contact", + rChannelLink = Nothing + } + +dynamisOrder :: OrderFields +dynamisOrder = + OrderFields + { ofLabelHash = labelHash "dynamis", + ofLength = 7, + ofDuration = duration, + ofCommitment = commitment, + ofSealed = "q3Zk0Yx7c2VhbGVkLW9yZGVyLWZpeHR1cmU" + } + +commitment :: ByteString +commitment = hx "0x9cf04ddef2eedfb02e6e7ba58f879c0636c584b6acc67ffe9f97a06bfb016ac1" + +node :: ByteString +node = hx "0x833cc73a7eae1ddee67ec8845ec3d39799406c7bc87fab7c504a7e0ab5cb89bb" + +-- 21 October 2026 09:15 UTC +timestamp :: Word64 +timestamp = 1792574100 + +-- 2 years of 365 days and the free days until 1 January 2027 +duration :: Word64 +duration = 69259500 + +orderSignature :: Text +orderSignature = "0x73e32be461e99e64b093a4ad09b04b4d810e5623a1b0e9c9fdb4701ec2a6a2a84df9ffdccc39c5cb827cde4d846c7662f591665f1b868ab1c15778646d3f414e1b" + +retString :: ByteString +retString = hx "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002168747470733a2f2f736d70382e73696d706c65782e696d2f6123636f6e7461637400000000000000000000000000000000000000000000000000000000000000" + +owner :: Address +owner = either error id $ strDecode $ encodeUtf8 ownerText + +ownerText, ownerLower, ownerUpper :: Text +ownerText = "0x014f9c09CcEeDEF1B713110d04D3a07d5B5D9b73" +ownerLower = T.toLower ownerText +ownerUpper = "0x" <> T.toUpper (T.drop 2 ownerText) + +sponsor, resolver :: Address +sponsor = either error id $ strDecode "0x9a676e781a523b5d0c0e43731313a708cb607508" +resolver = either error id $ strDecode "0x2279b7a0a67db372996a5fab50d91eaa73d2ebe6" + +chainId :: Word64 +chainId = 31337 + +hx :: Text -> ByteString +hx t = either error id $ convertFromBase Base16 $ encodeUtf8 $ fromMaybe t $ T.stripPrefix "0x" t diff --git a/tests/Bots/StoreService/NamesStoreTests.hs b/tests/Bots/StoreService/NamesStoreTests.hs new file mode 100644 index 0000000000..aa7e724d59 --- /dev/null +++ b/tests/Bots/StoreService/NamesStoreTests.hs @@ -0,0 +1,709 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module Bots.StoreService.NamesStoreTests (namesStoreTests, at, btc, insertInvoiceRow, invoiceStatus, issue, newReg, order, ownerA, ownerB, paidOrder, placeCode, placePaid, price, reg, reveal, signature, t0, usable, write) where + +import Bots.StoreService.WebTests (columnsOf, digestFixture, runMigrations, withServiceStore) +import Control.Arrow ((&&&)) +import Control.Monad (forM_, void) +import Data.List (sort) +import Data.Maybe (fromJust, mapMaybe) +import Data.Text (Text) +import qualified Data.Text as T +import Data.Time.Calendar (fromGregorian) +import Data.Time.Clock (UTCTime (..), addUTCTime) +import Data.Word (Word8) +import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) +import Simplex.Chat.PaymentService.Types (CardProvider (..), CryptoCurrency (..), InvoiceId (..), ServicePaymentMethod (..)) +import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) +import qualified Simplex.Messaging.Agent.Store.DB as DB +import Simplex.Messaging.Agent.Store.Shared (Migration (..), MigrationsToRun (..), toDownMigration) +import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature) +import Simplex.Messaging.Eth.Address (Address) +import Simplex.Messaging.SimplexName (labelHash) +import StoreService.Names.Chain (TxHash (..)) +import StoreService.Names.Phases (RegistrationId (..)) +import StoreService.Names.Signatures (OrderFields (..), parseAnyCaseAddress, parseSignature) +import StoreService.Names.Store +import StoreService.Store (RevokeResult (..)) +import Test.Hspec + +#if defined(dbPostgres) +import Database.PostgreSQL.Simple (Only (..), (:.) (..)) +import Database.PostgreSQL.Simple.Errors (ConstraintViolation (..), constraintViolation) +import StoreService.Store.Postgres.Migrations (storeServiceSchemaMigrations) +#else +import Database.SQLite.Simple (Error (ErrorConstraint), Only (..), SQLError (..), (:.) (..)) +import StoreService.Store.SQLite.Migrations (storeServiceSchemaMigrations) +#endif + +namesStoreTests :: Spec +namesStoreTests = describe "StoreService names: store" $ do + describe "migration" $ do + it "creates the names tables with the planned columns" testNamesColumns + it "rolls back and re-applies only the names migrations, with rows in them, leaving the badge tables and paid invoices as they were" testNamesDownUp + it "turns a code_issued order taken at price 0 and not live at its next version, dropping issued_code_id with its index and foreign key, which the down migration restores" testDropTakenCodes + describe "order hash" $ do + it "is equal for the same order fields, and differs when any one field differs" testOrderHashFields + it "keeps field boundaries, so moving a byte between two fields changes it" testOrderHashBoundaries + describe "registrations" $ do + it "creates a paid order unpaid and live at version 1, linked to its invoice" testCreatePaid + it "creates a code order queued with its code reserved" testCreateCode + it "refuses a second live order for one owner and label hash, or for one commitment" testLiveUnique + it "leaves no registration when the code cannot be reserved" testCodeNotReservable + it "finds an exact resend by its order hash, live or not" testOrderHashLookup + it "tells an overdue live order apart, whatever the poller marked, unless paid" testLiveOrdersOverdue + it "closes an overdue order so a new one from the same wallet fits in the same transaction" testCloseOverdue + it "closes nothing from a stale version" testCloseOverdueStale + it "bumps the version on every write and changes nothing for a stale one" testVersionBumps + it "writes and reads back every changeable field, and clears the reveal" testRoundTrip + it "keeps live in step with the phase" testLiveFollowsPhase + it "is live while an order can still progress to a registration for its name" testOrderLive + it "lets a wallet order a name again once its registration is done" testReorderAfterRegistered + it "refuses to queue a closed order, also when a newer live order holds its name" testLatePayment + it "finds an owner's orders newest first, at most 50" testFind + it "orders registrations made in the same second by their id" testSameSecondOrder + it "tells a label being registered here" testLabelRegistering + it "lists the registrations a worker moves on" testActive + it "lists unpaid orders whose invoice expired before the cutoff without a payment, and deletes one with its invoice" testUnpaidCleanup + it "lists code orders failed before the cutoff that still hold their code" testFailedCodeOrders + describe "prepaid codes" $ do + it "issues one code per hash" testIssueCode + it "reserves only an unused code, releases and spends it only for its holder" testReserveReleaseSpend + it "refuses an unpaid, revoked or expired code" testReserveRefused + it "lets a registration hold one code" testOneCodePerRegistration + it "revokes an unspent code, reserved or not, never a spent one" testRevoke + describe "transactions" $ do + it "keeps a signed transaction pending until its outcome, and lists them by owner" testTransactions + it "moves a transaction only from the status its caller read" testTransactionFromStatus + +t0 :: UTCTime +t0 = UTCTime (fromGregorian 2026 10 21) 0 + +at :: Int -> UTCTime +at minutes = addUTCTime (fromIntegral $ minutes * 60) t0 + +ownerA :: Address +ownerA = address "0x014f9c09CcEeDEF1B713110d04D3a07d5B5D9b73" + +ownerB :: Address +ownerB = address "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318" + +address :: Text -> Address +address = either (error . show) id . parseAnyCaseAddress + +-- r all 0x11, s all 0x22 (a low s), v 27 +signature :: RecoverableSignature +signature = either (error . show) id $ parseSignature $ "0x" <> T.replicate 32 "11" <> T.replicate 32 "22" <> "1b" + +twoYears :: Int +twoYears = 2 + +order :: Text -> Word8 -> OrderFields +order label n = + OrderFields + { ofLabelHash = labelHash label, + ofLength = T.length label, + ofDuration = 69292800, + ofCommitment = digestFixture n, + ofSealed = "sealed-" <> label <> "-" <> T.pack (show n) + } + +newReg :: Text -> Address -> OrderFields -> NewRegistration +newReg rid nrgOwner nrgOrder = NewRegistration {nrgId = RegistrationId rid, nrgOwner, nrgOrder, nrgYears = twoYears} + +btc :: ServicePaymentMethod +btc = SPMCrypto CCBtc + +price :: Int +price = 20000 + +paidOrder :: Text -> PaidOrder +paidOrder invId = PaidOrder {poMethod = btc, poPrice = price, poInvoiceId = InvoiceId invId, poProviderRef = "ref-" <> invId} + +insertInvoiceRow :: DB.Connection -> Text -> UTCTime -> Text -> IO () +insertInvoiceRow db invId expiresAt status = + DB.execute + db + "INSERT INTO sx_badge_service_invoices (invoice_id, provider, price, amount, currency, payment_address, payment_crypto_currency, payment_crypto_amount, expires_at, status, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)" + ((invId, "crypto" :: Text, price, price, "usd" :: Text, "bc1qexampleaddress" :: Text, "btc" :: Text, "0.00312" :: Text) :. (expiresAt, status, t0, t0)) + +-- | A paid order whose invoice, named after the registration, expires at the given time. +placePaid :: DBStore -> Text -> Address -> OrderFields -> UTCTime -> IO (Either RegistrationRefused ()) +placePaid st rid owner o expiresAt = withTransaction st $ \db -> do + insertInvoiceRow db ("inv-" <> rid) expiresAt "open" + createPaidRegistration db (newReg rid owner o) (paidOrder $ "inv-" <> rid) t0 + +issue :: DBStore -> Word8 -> NewPrepaidCode -> IO PrepaidCode +issue st n c = withTransaction st $ \db -> do + Right codeId <- insertPrepaidCode db c {npcCodeHash = digestFixture n} t0 + fromJust <$> getPrepaidCodeById db codeId + +usable :: NewPrepaidCode +usable = NewPrepaidCode {npcCodeHash = "", npcMinLength = 8, npcYears = twoYears, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing} + +placeCode :: DBStore -> Text -> Address -> OrderFields -> PrepaidCode -> IO (Either RegistrationRefused ()) +placeCode st rid owner o code = withTransaction st $ \db -> createCodeRegistration db (newReg rid owner o) code t0 + +reg :: DBStore -> Text -> IO NameRegistration +reg st rid = withTransaction st $ \db -> fromJust <$> getRegistration db (RegistrationId rid) + +write :: DBStore -> NameRegistration -> IO VersionWrite +write st r = withTransaction st $ \db -> updateRegistration db r (at 1) + +namesTables :: [Text] +namesTables = map ("sx_badge_service_name_" <>) ["prepaid_codes", "registrations", "invoices", "record_edits", "transactions"] + +badgeTables :: [Text] +badgeTables = map ("sx_badge_service_" <>) ["invoices", "payments", "badge_prices", "badge_offers", "badge_purchases", "badge_ledger", "badge_issuances", "badge_codes", "badge_code_invoices", "group"] + +testNamesColumns :: IO () +testNamesColumns = withServiceStore $ \st -> do + let has table cols = columnsOf st table >>= \cs -> filter (`notElem` cs) cols `shouldBe` [] + has "sx_badge_service_name_prepaid_codes" ["name_prepaid_code_id", "code_hash", "min_length", "years", "code_payment_status", "reserved_by", "reserved_at", "spent_at", "revoked_at", "expires_at", "created_at"] + has + "sx_badge_service_name_registrations" + [ "registration_id", "label_hash", "length", "label", "secret", "contact_link", "channel_link", "intent_nonce", "intent_deadline", "intent_signature", + "sealed", "owner", "commitment", "phase", "commit_tx", "commit_block", "register_tx", "register_block", "reveal_after", "reveal_before", + "years", "duration", "expires_at", "price", "name_prepaid_code_id", "registered_at", "failed_at", "error", "created_at", + "updated_at", "version", "live", "order_hash", "method", "moves", "links_refused_at" + ] + has "sx_badge_service_name_invoices" ["invoice_id", "registration_id", "provider_ref"] + has "sx_badge_service_name_record_edits" ["label_hash", "owner", "nonce", "body_hash", "state", "revert", "mined_tx", "created_at"] + has "sx_badge_service_name_transactions" ["registration_id", "name_record_edit_id", "kind", "raw", "tx_hash", "registrar_nonce", "sent_at", "mined_block", "status"] + +testNamesDownUp :: IO () +testNamesDownUp = withServiceStore $ \st -> do + let names = dropWhile ((/= "20261013_names") . name) storeServiceSchemaMigrations + badgeColumns = mapM (fmap sort . columnsOf st) badgeTables + map name names `shouldBe` ["20261013_names", "20261022_drop_taken_codes"] + badgeBefore <- badgeColumns + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + placePaid st "r2" ownerB (order "bobsbakery" 2) (at 60) `shouldReturn` Right () + placePaid st "r3" ownerB (order "carolsbakery" 3) (at 60) `shouldReturn` Right () + withTransaction st $ \db -> insertPayment db "inv-r3" + runMigrations st $ MTRDown (mapMaybe toDownMigration names) + mapM (columnsOf st) namesTables `shouldReturn` map (const []) namesTables + mapM (invoiceCount st) ["inv-r2", "inv-r3"] `shouldReturn` [0, 1] + badgeColumns `shouldReturn` badgeBefore + runMigrations st $ MTRUp names + mapM (fmap null . columnsOf st) namesTables `shouldReturn` map (const False) namesTables + badgeColumns `shouldReturn` badgeBefore + +testDropTakenCodes :: IO () +testDropTakenCodes = withServiceStore $ \st -> do + let dropCodes = [m | m@Migration {name = "20261022_drop_taken_codes"} <- storeServiceSchemaMigrations] + issuedCode = (,) <$> (elem "issued_code_id" <$> columnsOf st "sx_badge_service_name_registrations") <*> indexExists st "sx_badge_service_idx_name_registrations_issued_code" + worth r = (nrPhase r, nrPrice r, nrLive r, nrVersion r) + length dropCodes `shouldBe` 1 + runMigrations st $ MTRDown (mapMaybe toDownMigration dropCodes) + issuedCode `shouldReturn` (True, True) + code <- issue st 1 usable + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + placePaid st "r2" ownerB (order "bobsbakery" 2) (at 60) `shouldReturn` Right () + withTransaction st $ \db -> DB.execute db "UPDATE sx_badge_service_name_registrations SET phase = 'code_issued', issued_code_id = ? WHERE registration_id = 'r1'" (Only $ pcId code) + r2 <- reg st "r2" + write st r2 {nrPhase = PTaken} `shouldReturn` Written 2 + taken <- reg st "r2" + runMigrations st $ MTRUp dropCodes + issuedCode `shouldReturn` (False, False) + worth <$> reg st "r1" `shouldReturn` (PTaken, 0, False, 2) + reg st "r2" `shouldReturn` taken + withTransaction st (\db -> getPrepaidCodeById db (pcId code)) `shouldReturn` Just code + runMigrations st $ MTRDown (mapMaybe toDownMigration dropCodes) + issuedCode `shouldReturn` (True, True) + worth <$> reg st "r1" `shouldReturn` (PTaken, 0, False, 2) + withTransaction st (\db -> DB.execute db "UPDATE sx_badge_service_name_registrations SET issued_code_id = ? WHERE registration_id = 'r1'" (Only $ pcId code + 1)) `shouldThrow` foreignKeyViolation + +testOrderHashFields :: IO () +testOrderHashFields = do + let o = order "alicebakery" 1 + paid = OTInvoice btc price + base = orderHash ownerA o paid + variants = + [ ("labelHash" :: Text, orderHash ownerA o {ofLabelHash = labelHash "alicebakers"} paid), + ("length", orderHash ownerA o {ofLength = 12} paid), + ("duration", orderHash ownerA o {ofDuration = 69292801} paid), + ("owner", orderHash ownerB o paid), + ("commitment", orderHash ownerA o {ofCommitment = digestFixture 2} paid), + ("sealed", orderHash ownerA o {ofSealed = ofSealed o <> "x"} paid), + ("method", orderHash ownerA o (OTInvoice (SPMCard CPStripe) price)), + ("price", orderHash ownerA o (OTInvoice btc (price + 1))), + ("code instead of invoice", orderHash ownerA o (OTCode $ digestFixture 3)), + ("another code", orderHash ownerA o (OTCode $ digestFixture 4)) + ] + orderHash ownerA (order "alicebakery" 1) paid `shouldBe` base + forM_ variants $ \(field, h) -> (field, h == base) `shouldBe` (field, False) + orderHash ownerA o (OTCode $ digestFixture 3) `shouldNotBe` orderHash ownerA o (OTCode $ digestFixture 4) + +testOrderHashBoundaries :: IO () +testOrderHashBoundaries = do + let o = order "alicebakery" 1 + orderHash ownerA o {ofSealed = "ab"} (OTCode "c") `shouldNotBe` orderHash ownerA o {ofSealed = "a"} (OTCode "bc") + +testCreatePaid :: IO () +testCreatePaid = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + placePaid st "r1" ownerA o (at 60) `shouldReturn` Right () + r <- reg st "r1" + r + `shouldBe` NameRegistration + { nrId = RegistrationId "r1", + nrVersion = 1, + nrLabelHash = ofLabelHash o, + nrLength = 11, + nrDuration = 69292800, + nrOwner = ownerA, + nrCommitment = ofCommitment o, + nrSealed = ofSealed o, + nrPhase = PUnpaid, + nrYears = twoYears, + nrPrice = price, + nrPayment = RPInvoice btc (InvoiceId "inv-r1"), + nrReveal = Nothing, + nrCommitTx = Nothing, + nrCommitBlock = Nothing, + nrRegisterTx = Nothing, + nrRegisterBlock = Nothing, + nrRevealAfter = Nothing, + nrRevealBefore = Nothing, + nrExpiresAt = Nothing, + nrRegisteredAt = Nothing, + nrFailedAt = Nothing, + nrError = Nothing, + nrLive = True, + nrOrderHash = orderHash ownerA o (OTInvoice btc price), + nrMoves = 0, + nrLinksRefusedAt = Nothing, + nrCreatedAt = t0, + nrUpdatedAt = t0 + } + +testCreateCode :: IO () +testCreateCode = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + code@PrepaidCode {pcId, pcCodeHash} <- issue st 1 usable + placeCode st "r1" ownerA o code `shouldReturn` Right () + r <- reg st "r1" + (nrPhase r, nrLive r, nrPrice r, nrPayment r, nrVersion r) `shouldBe` (PQueued, True, 0, RPCode pcId, 1) + nrOrderHash r `shouldBe` orderHash ownerA o (OTCode pcCodeHash) + held <- withTransaction st $ \db -> getPrepaidCode db pcCodeHash + (pcReservedBy =<< held, pcReservedAt =<< held) `shouldBe` (Just (RegistrationId "r1"), Just t0) + +testLiveUnique :: IO () +testLiveUnique = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + placePaid st "r1" ownerA o (at 60) `shouldReturn` Right () + placePaid st "r2" ownerA o {ofCommitment = digestFixture 2} (at 60) `shouldReturn` Left RRLiveOrder + placePaid st "r3" ownerB o (at 60) `shouldReturn` Left RRLiveOrder + code <- issue st 9 usable + placeCode st "r4" ownerA (order "alicebakery" 3) code `shouldReturn` Left RRLiveOrder + placePaid st "r5" ownerB (order "alicebakery" 5) (at 60) `shouldReturn` Right () + existing <- withTransaction st $ \db -> mapM (getRegistration db . RegistrationId) ["r2", "r3", "r4"] + existing `shouldBe` [Nothing, Nothing, Nothing] + free <- withTransaction st $ \db -> getPrepaidCodeById db (pcId code) + (pcReservedBy =<< free) `shouldBe` Nothing + +testCodeNotReservable :: IO () +testCodeNotReservable = withServiceStore $ \st -> do + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + placeCode st "r2" ownerB (order "bobsbakery" 2) code `shouldReturn` Left RRCodeNotReservable + withTransaction st (\db -> getRegistration db (RegistrationId "r2")) `shouldReturn` Nothing + held <- withTransaction st $ \db -> getPrepaidCodeById db (pcId code) + (pcReservedBy =<< held) `shouldBe` Just (RegistrationId "r1") + +testOrderHashLookup :: IO () +testOrderHashLookup = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + placePaid st "r1" ownerA o (at 1) `shouldReturn` Right () + r <- reg st "r1" + closed <- withTransaction st $ \db -> closeOverdue db r (at 2) + closed `shouldBe` Just 2 + found <- withTransaction st $ \db -> getRegistrationByOrderHash db (orderHash ownerA o (OTInvoice btc price)) + (nrId <$> found, nrLive <$> found) `shouldBe` (Just (RegistrationId "r1"), Just False) + withTransaction st (\db -> getRegistrationByOrderHash db (orderHash ownerA o (OTInvoice btc (price + 1)))) `shouldReturn` Nothing + +testLiveOrdersOverdue :: IO () +testLiveOrdersOverdue = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + live now = withTransaction st $ \db -> map (\LiveOrder {loRegistration, loOverdue} -> (nrId loRegistration, loOverdue)) <$> getLiveOrders db (ofCommitment o) ownerA (ofLabelHash o) now + setStatus status = withTransaction st $ \db -> DB.execute db "UPDATE sx_badge_service_invoices SET status = ? WHERE invoice_id = ?" (status :: Text, "inv-r1" :: Text) + placePaid st "r1" ownerA o (at 60) `shouldReturn` Right () + placePaid st "r2" ownerB (order "alicebakery" 2) (at 60) `shouldReturn` Right () + live (at 30) `shouldReturn` [(RegistrationId "r1", False)] + live (at 61) `shouldReturn` [(RegistrationId "r1", True)] + withTransaction st (\db -> map (nrId . loRegistration) <$> getLiveOrders db (ofCommitment o) ownerB (labelHash "zzzzzzzz") (at 30)) + `shouldReturn` [RegistrationId "r1"] + setStatus "expired" + live (at 61) `shouldReturn` [(RegistrationId "r1", True)] + setStatus "paid" + live (at 61) `shouldReturn` [(RegistrationId "r1", False)] + +testCloseOverdue :: IO () +testCloseOverdue = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + o' = o {ofCommitment = digestFixture 2, ofSealed = "new"} + placePaid st "r1" ownerA o (at 60) `shouldReturn` Right () + placed <- withTransaction st $ \db -> do + [LiveOrder {loRegistration, loOverdue = True}] <- getLiveOrders db (ofCommitment o') ownerA (ofLabelHash o') (at 61) + void $ closeOverdue db loRegistration (at 61) + insertInvoiceRow db "inv-r2" (at 120) "open" + createPaidRegistration db (newReg "r2" ownerA o') (paidOrder "inv-r2") (at 61) + placed `shouldBe` Right () + old <- reg st "r1" + (nrLive old, nrPhase old, nrVersion old) `shouldBe` (False, PUnpaid, 2) + invoiceStatus st "inv-r1" `shouldReturn` "expired" + invoiceStatus st "inv-r2" `shouldReturn` "open" + +testCloseOverdueStale :: IO () +testCloseOverdueStale = withServiceStore $ \st -> do + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + r <- reg st "r1" + write st r {nrPhase = PQueued} `shouldReturn` Written 2 + withTransaction st (\db -> closeOverdue db r (at 61)) `shouldReturn` Nothing + invoiceStatus st "inv-r1" `shouldReturn` "open" + (nrLive &&& nrPhase) <$> reg st "r1" `shouldReturn` (True, PQueued) + +invoiceStatus :: DBStore -> Text -> IO Text +invoiceStatus st invId = withTransaction st $ \db -> + DB.query db "SELECT status FROM sx_badge_service_invoices WHERE invoice_id = ?" (Only invId) >>= \case + [Only status] -> pure status + rows -> fail $ "invoice " <> show invId <> ": " <> show (length rows) <> " rows" + +reveal :: NameReveal +reveal = + NameReveal + { rvLabel = "alicebakery", + rvSecret = digestFixture 7, + rvContactLink = Just "https://smp8.simplex.im/a#abc", + rvChannelLink = Nothing, + rvNonce = 3, + rvDeadline = 1792915140, + rvSignature = signature + } + +testVersionBumps :: IO () +testVersionBumps = withServiceStore $ \st -> do + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + r1 <- reg st "r1" + write st r1 {nrPhase = PCommitting, nrCommitTx = Just (TxHash $ digestFixture 8)} `shouldReturn` Written 2 + r2 <- reg st "r1" + write st r2 {nrPhase = PCommitted, nrRevealAfter = Just 1792828800, nrRevealBefore = Just 1792915140} `shouldReturn` Written 3 + r3 <- reg st "r1" + write st r3 {nrReveal = Just reveal} `shouldReturn` Written 4 + write st r3 {nrLinksRefusedAt = Just (at 5)} `shouldReturn` VersionChanged + write st r1 {nrPhase = PFailed} `shouldReturn` VersionChanged + r4 <- reg st "r1" + (nrVersion r4, nrPhase r4, nrReveal r4, nrLinksRefusedAt r4) `shouldBe` (4, PCommitted, Just reveal, Nothing) + +testRoundTrip :: IO () +testRoundTrip = withServiceStore $ \st -> do + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + r <- reg st "r1" + let changed = + r + { nrPhase = PRegistered, + nrReveal = Just reveal {rvChannelLink = Just "simplex:/c#xyz"}, + nrCommitTx = Just (TxHash $ digestFixture 8), + nrCommitBlock = Just 100, + nrRegisterTx = Just (TxHash $ digestFixture 9), + nrRegisterBlock = Just 120, + nrRevealAfter = Just 1792828800, + nrRevealBefore = Just 1792915140, + nrExpiresAt = Just 1861833600, + nrRegisteredAt = Just (at 3), + nrFailedAt = Just (at 2), + nrError = Just "The name was not signed in time", + nrLinksRefusedAt = Just (at 4), + nrMoves = 2 + } + write st changed `shouldReturn` Written 2 + reg st "r1" `shouldReturn` changed {nrVersion = 2, nrLive = False, nrUpdatedAt = at 1} + write st changed {nrVersion = 2, nrReveal = Nothing} `shouldReturn` Written 3 + nrReveal <$> reg st "r1" `shouldReturn` Nothing + +testLiveFollowsPhase :: IO () +testLiveFollowsPhase = withServiceStore $ \st -> do + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + code <- issue st 1 usable + placeCode st "r2" ownerB (order "bobsbakery" 2) code `shouldReturn` Right () + let step rid phase expected = do + r <- reg st rid + write st r {nrPhase = phase} `shouldReturn` Written (nrVersion r + 1) + (nrPhase &&& nrLive) <$> reg st rid `shouldReturn` (phase, expected) + step "r1" PQueued True + step "r1" PFailed True + step "r1" PTaken False + r1 <- reg st "r1" + write st r1 {nrPhase = PFailed} `shouldReturn` NotLive + step "r2" PRegistering True + step "r2" PTaken False + placePaid st "r3" ownerA (order "carolsbakery" 3) (at 60) `shouldReturn` Right () + r3 <- reg st "r3" + write st r3 {nrLive = False} `shouldReturn` Written 2 + nrLive <$> reg st "r3" `shouldReturn` False + +testOrderLive :: IO () +testOrderLive = withServiceStore $ \st -> do + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + paid <- reg st "r1" + let code = paid {nrPayment = RPCode 1} + liveIn r phase = (phase, orderLive r {nrPhase = phase}) + map (liveIn paid) [minBound .. maxBound] + `shouldBe` [(PUnpaid, True), (PQueued, True), (PCommitting, True), (PCommitted, True), (PRegistering, True), (PRegistered, False), (PTaken, False), (PFailed, True)] + map (liveIn code) [PTaken, PFailed, PRegistered] `shouldBe` [(PTaken, False), (PFailed, True), (PRegistered, False)] + orderLive paid {nrLive = False} `shouldBe` False + orderLive paid {nrLive = False, nrPhase = PQueued} `shouldBe` True + +testReorderAfterRegistered :: IO () +testReorderAfterRegistered = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + code <- issue st 1 usable + placeCode st "r1" ownerA o code `shouldReturn` Right () + placePaid st "r2" ownerA o {ofCommitment = digestFixture 2} (at 60) `shouldReturn` Left RRLiveOrder + r <- reg st "r1" + write st r {nrPhase = PRegistered, nrRegisteredAt = Just (at 1)} `shouldReturn` Written 2 + nrLive <$> reg st "r1" `shouldReturn` False + placePaid st "r3" ownerA o {ofCommitment = digestFixture 2} (at 60) `shouldReturn` Right () + +testLatePayment :: IO () +testLatePayment = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + o' = o {ofCommitment = digestFixture 2, ofSealed = "new"} + placePaid st "r1" ownerA o (at 60) `shouldReturn` Right () + closed <- reg st "r1" + withTransaction st (\db -> closeOverdue db closed (at 61)) `shouldReturn` Just 2 + r1 <- reg st "r1" + write st r1 {nrPhase = PQueued} `shouldReturn` NotLive + reg st "r1" `shouldReturn` r1 + placePaid st "r2" ownerA o' (at 120) `shouldReturn` Right () + write st r1 {nrPhase = PQueued} `shouldReturn` NotLive + reg st "r1" `shouldReturn` r1 + (nrPhase &&& nrLive) <$> reg st "r2" `shouldReturn` (PUnpaid, True) + write st r1 {nrVersion = 1, nrPhase = PQueued} `shouldReturn` VersionChanged + +testFind :: IO () +testFind = withServiceStore $ \st -> do + forM_ [1 .. 52 :: Int] $ \i -> withTransaction st $ \db -> do + let rid = "r" <> T.pack (show i) + o = order ("alicebakery" <> T.pack (show i)) (fromIntegral i) + insertInvoiceRow db ("inv-" <> rid) (at 60) "open" + createPaidRegistration db (newReg rid ownerA o) (paidOrder $ "inv-" <> rid) (at i) `shouldReturn` Right () + placePaid st "other" ownerB (order "bobsbakery" 99) (at 60) `shouldReturn` Right () + found <- withTransaction st $ \db -> findRegistrations db ownerA + map nrId found `shouldBe` [RegistrationId $ "r" <> T.pack (show i) | i <- [52, 51 .. 3 :: Int]] + withTransaction st (\db -> findRegistrations db (address "0x0000000000000000000000000000000000000001")) `shouldReturn` [] + +testSameSecondOrder :: IO () +testSameSecondOrder = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + ids = ["r2", "r3", "r1"] + forM_ (zip ids [10 ..]) $ \(rid, n) -> do + placePaid st rid ownerA (order ("alicebakery" <> rid) n) (at 60) `shouldReturn` Right () + r <- reg st rid + write st r {nrPhase = PQueued} `shouldReturn` Written 2 + map nrId <$> withTransaction st (`findRegistrations` ownerA) `shouldReturn` map RegistrationId ["r3", "r2", "r1"] + map nrId <$> withTransaction st activeRegistrations `shouldReturn` map RegistrationId ["r1", "r2", "r3"] + forM_ ["r5", "r4"] $ \rid -> do + placePaid st rid ownerB o (at 60) `shouldReturn` Right () + r <- reg st rid + withTransaction st (\db -> closeOverdue db r (at 61)) `shouldReturn` Just 2 + found <- withTransaction st $ \db -> getRegistrationByOrderHash db (orderHash ownerB o (OTInvoice btc price)) + nrId <$> found `shouldBe` Just (RegistrationId "r5") + +testLabelRegistering :: IO () +testLabelRegistering = withServiceStore $ \st -> do + let o = order "alicebakery" 1 + registering = withTransaction st $ \db -> isLabelRegistering db (ofLabelHash o) + code <- issue st 1 usable + placeCode st "r1" ownerA o code `shouldReturn` Right () + registering `shouldReturn` False + r <- reg st "r1" + void $ write st r {nrPhase = PRegistering} + registering `shouldReturn` True + r' <- reg st "r1" + void $ write st r' {nrPhase = PRegistered} + registering `shouldReturn` False + +testActive :: IO () +testActive = withServiceStore $ \st -> do + let phases = [PUnpaid ..] + forM_ (zip [1 ..] phases) $ \(i, phase) -> do + let rid = "r" <> T.pack (show i) + placePaid st rid ownerA (order ("alicebakery" <> T.pack (show i)) i) (at 60) `shouldReturn` Right () + r <- reg st rid + void $ write st r {nrPhase = phase} + active <- withTransaction st activeRegistrations + sort (map (fromEnum . nrPhase) active) `shouldBe` map fromEnum [PQueued, PCommitting, PCommitted, PRegistering] + +testUnpaidCleanup :: IO () +testUnpaidCleanup = withServiceStore $ \st -> do + placePaid st "old" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + placePaid st "funded" ownerA (order "bobsbakery" 2) (at 60) `shouldReturn` Right () + placePaid st "queued" ownerA (order "carolsbakery" 3) (at 60) `shouldReturn` Right () + placePaid st "open" ownerA (order "davesbakery" 4) (at 120) `shouldReturn` Right () + withTransaction st $ \db -> insertPayment db "inv-funded" + r <- reg st "queued" + void $ write st r {nrPhase = PQueued} + candidates <- withTransaction st $ \db -> unpaidCleanupCandidates db (at 61) + map (\(RegistrationId rid, InvoiceId invId) -> (rid, invId)) candidates `shouldBe` [("old", "inv-old")] + withTransaction st (\db -> mapM (deleteUnpaidRegistration db . RegistrationId) ["old", "funded", "queued"]) `shouldReturn` [True, False, False] + withTransaction st (\db -> getRegistration db (RegistrationId "old")) `shouldReturn` Nothing + invoiceCount st "inv-old" `shouldReturn` 0 + invoiceCount st "inv-funded" `shouldReturn` 1 + nrPhase <$> reg st "funded" `shouldReturn` PUnpaid + +insertPayment :: DB.Connection -> Text -> IO () +insertPayment db invId = + DB.execute + db + "INSERT INTO sx_badge_service_payments (payment_id, invoice_id, provider, status, created_at, updated_at) VALUES (?,?,?,?,?,?)" + (invId, invId, "crypto" :: Text, "pending" :: Text, t0, t0) + +foreignKeyViolation :: Selector DB.SQLError +#if defined(dbPostgres) +foreignKeyViolation e = case constraintViolation e of + Just ForeignKeyViolation {} -> True + _ -> False +#else +foreignKeyViolation e = sqlError e == ErrorConstraint && "FOREIGN KEY" `T.isInfixOf` sqlErrorDetails e +#endif + +indexExists :: DBStore -> Text -> IO Bool +indexExists st index = withTransaction st $ \db -> do + rows <- DB.query db indexQuery (Only index) + pure $ not $ null (rows :: [Only Text]) + where +#if defined(dbPostgres) + indexQuery = "SELECT indexname FROM pg_indexes WHERE schemaname = current_schema() AND indexname = ?" +#else + indexQuery = "SELECT name FROM sqlite_master WHERE type = 'index' AND name = ?" +#endif + +invoiceCount :: DBStore -> Text -> IO Int +invoiceCount st invId = withTransaction st $ \db -> + DB.query db "SELECT COUNT(*) FROM sx_badge_service_invoices WHERE invoice_id = ?" (Only invId) >>= \case + [Only n] -> pure n + rows -> fail $ "count: " <> show (length rows) <> " rows" + +testFailedCodeOrders :: IO () +testFailedCodeOrders = withServiceStore $ \st -> do + codes@[oldCode, _, releasedCode, _] <- mapM (\n -> issue st n usable) [1, 2, 3, 4] + forM_ (zip3 ["old", "recent", "released", "queued"] [1 ..] codes) $ \(rid, n, code) -> + placeCode st rid ownerA (order ("alicebakery" <> rid) n) code `shouldReturn` Right () + let failAt rid t = reg st rid >>= \r -> void (write st r {nrPhase = PFailed, nrFailedAt = Just t}) + failAt "old" (at 1) + failAt "recent" (at 30) + failAt "released" (at 1) + withTransaction st (\db -> releasePrepaidCode db (pcId releasedCode) (RegistrationId "released")) `shouldReturn` True + withTransaction st (\db -> failedCodeOrdersBefore db (at 10)) `shouldReturn` [(RegistrationId "old", pcId oldCode)] + +testIssueCode :: IO () +testIssueCode = withServiceStore $ \st -> do + code <- issue st 1 usable {npcMinLength = 6, npcYears = 5, npcPaymentStatus = CPSFree, npcExpiresAt = Just (at 100)} + (pcMinLength code, pcYears code, pcPaymentStatus code, pcExpiresAt code, pcCreatedAt code) `shouldBe` (6, 5, CPSFree, Just (at 100), t0) + withTransaction st (\db -> insertPrepaidCode db usable {npcCodeHash = digestFixture 1} t0) `shouldReturn` Left CodeHashTaken + withTransaction st (\db -> getPrepaidCode db (digestFixture 1)) `shouldReturn` Just code + +testReserveReleaseSpend :: IO () +testReserveReleaseSpend = withServiceStore $ \st -> do + PrepaidCode {pcId} <- issue st 1 usable + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + placePaid st "r2" ownerB (order "bobsbakery" 2) (at 60) `shouldReturn` Right () + let r1 = RegistrationId "r1" + r2 = RegistrationId "r2" + run :: (DB.Connection -> IO a) -> IO a + run = withTransaction st + run (\db -> releasePrepaidCode db pcId r1) `shouldReturn` False + run (\db -> spendPrepaidCode db pcId r1 t0) `shouldReturn` False + run (\db -> reservePrepaidCode db pcId r1 (at 1)) `shouldReturn` True + run (\db -> reservePrepaidCode db pcId r2 (at 1)) `shouldReturn` False + run (\db -> releasePrepaidCode db pcId r2) `shouldReturn` False + run (\db -> spendPrepaidCode db pcId r2 (at 2)) `shouldReturn` False + run (\db -> releasePrepaidCode db pcId r1) `shouldReturn` True + run (\db -> reservePrepaidCode db pcId r2 (at 2)) `shouldReturn` True + run (\db -> spendPrepaidCode db pcId r2 (at 3)) `shouldReturn` True + run (\db -> releasePrepaidCode db pcId r2) `shouldReturn` False + run (\db -> reservePrepaidCode db pcId r1 (at 4)) `shouldReturn` False + run (\db -> spendPrepaidCode db pcId r2 (at 4)) `shouldReturn` False + spent <- run $ \db -> getPrepaidCodeById db pcId + (pcReservedBy =<< spent, pcSpentAt =<< spent) `shouldBe` (Just r2, Just (at 3)) + +testReserveRefused :: IO () +testReserveRefused = withServiceStore $ \st -> do + placePaid st "r1" ownerA (order "alicebakery" 1) (at 60) `shouldReturn` Right () + unpaid <- issue st 1 usable {npcPaymentStatus = CPSUnpaid} + expired <- issue st 2 usable {npcExpiresAt = Just (at 10)} + revoked <- issue st 3 usable + free <- issue st 4 usable {npcPaymentStatus = CPSFree, npcExpiresAt = Just (at 30)} + withTransaction st (\db -> revokePrepaidCode db (pcCodeHash revoked) t0) `shouldReturn` Revoked (pcId revoked) + let reserve code = withTransaction st $ \db -> reservePrepaidCode db (pcId code) (RegistrationId "r1") (at 20) + mapM reserve [unpaid, expired, revoked] `shouldReturn` [False, False, False] + reserve free `shouldReturn` True + +testOneCodePerRegistration :: IO () +testOneCodePerRegistration = withServiceStore $ \st -> do + first <- issue st 1 usable + second <- issue st 2 usable + placeCode st "r1" ownerA (order "alicebakery" 1) first `shouldReturn` Right () + withTransaction st (\db -> reservePrepaidCode db (pcId second) (RegistrationId "r1") t0) `shouldThrow` anyException + free <- withTransaction st $ \db -> getPrepaidCodeById db (pcId second) + (pcReservedBy =<< free) `shouldBe` Nothing + +testRevoke :: IO () +testRevoke = withServiceStore $ \st -> do + reserved <- issue st 1 usable + spent <- issue st 2 usable + unused <- issue st 3 usable + placeCode st "r1" ownerA (order "alicebakery" 1) reserved `shouldReturn` Right () + placeCode st "r2" ownerA (order "bobsbakery" 2) spent `shouldReturn` Right () + withTransaction st (\db -> spendPrepaidCode db (pcId spent) (RegistrationId "r2") t0) `shouldReturn` True + let revoke code = withTransaction st $ \db -> revokePrepaidCode db (pcCodeHash code) (at 1) + revoke reserved `shouldReturn` Revoked (pcId reserved) + revoke unused `shouldReturn` Revoked (pcId unused) + revoke unused `shouldReturn` AlreadyRevoked (pcId unused) + revoke spent `shouldReturn` AlreadyRedeemed + withTransaction st (\db -> revokePrepaidCode db (digestFixture 9) t0) `shouldReturn` NoSuchCode + held <- withTransaction st $ \db -> getPrepaidCodeById db (pcId reserved) + (pcReservedBy =<< held, pcRevokedAt =<< held) `shouldBe` (Just (RegistrationId "r1"), Just (at 1)) + +testTransactions :: IO () +testTransactions = withServiceStore $ \st -> do + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + let r1 = RegistrationId "r1" + tx :: Word8 -> NewTransaction + tx n = NewTransaction {ntxRaw = digestFixture (n + 100), ntxHash = TxHash $ digestFixture n, ntxNonce = fromIntegral n} + [commitId, registerId] <- withTransaction st $ \db -> + sequence [insertTransaction db (TxCommit r1) (tx 2) (at 1), insertTransaction db (TxRegister r1) (tx 3) (at 3)] + unsettled <- withTransaction st pendingTransactions + map (\NameTransaction {ntFor, ntNonce, ntStatus} -> (ntFor, ntNonce, ntStatus)) unsettled + `shouldBe` [(TxCommit r1, 2, TSPending), (TxRegister r1, 3, TSPending)] + map ntRaw unsettled `shouldBe` map (\n -> digestFixture (n + 100)) [2, 3] + let outcome n from status block = withTransaction st $ \db -> recordTransactionOutcome db (TxHash $ digestFixture n) from status block + outcome 2 TSPending TSMined (Just 77) `shouldReturn` True + outcome 50 TSPending TSMined (Just 79) `shouldReturn` False + outcome 3 TSMined TSPending Nothing `shouldReturn` False + map ntFor <$> withTransaction st pendingTransactions `shouldReturn` [TxRegister r1] + ofRegistration <- withTransaction st $ \db -> registrationTransactions db r1 + map (\NameTransaction {ntId, ntFor, ntMinedBlock, ntStatus, ntSentAt} -> (ntId, ntFor, ntMinedBlock, ntStatus, ntSentAt)) ofRegistration + `shouldBe` [(commitId, TxCommit r1, Just 77, TSMined, at 1), (registerId, TxRegister r1, Nothing, TSPending, at 3)] + +testTransactionFromStatus :: IO () +testTransactionFromStatus = withServiceStore $ \st -> do + code <- issue st 1 usable + placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right () + let outcome from status block = withTransaction st $ \db -> recordTransactionOutcome db (TxHash $ digestFixture 2) from status block + current = withTransaction st $ \db -> map (\NameTransaction {ntStatus, ntMinedBlock} -> (ntStatus, ntMinedBlock)) <$> registrationTransactions db (RegistrationId "r1") + void $ withTransaction st $ \db -> insertTransaction db (TxCommit $ RegistrationId "r1") NewTransaction {ntxRaw = "raw", ntxHash = TxHash $ digestFixture 2, ntxNonce = 0} t0 + outcome TSPending TSMined (Just 77) `shouldReturn` True + outcome TSPending TSDropped Nothing `shouldReturn` False + current `shouldReturn` [(TSMined, Just 77)] + outcome TSMined TSPending Nothing `shouldReturn` True + current `shouldReturn` [(TSPending, Nothing)]