mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 10:07:31 +00:00
store: commit and register queued orders
This commit is contained in:
1 parent
33a393736a
commit
e82eb07076
11 files changed
+1724
-8
No files matched your search
@@ -15,6 +15,7 @@ module StoreService.Names.Codes
|
||||
reserveCode,
|
||||
releaseCode,
|
||||
spendCode,
|
||||
spendLandedCode,
|
||||
issueNameCode,
|
||||
revokeNameCode,
|
||||
)
|
||||
@@ -37,7 +38,7 @@ import qualified Simplex.Messaging.Agent.Store.DB as DB
|
||||
import StoreService.Config (minNameYears)
|
||||
import StoreService.Group.Command (maxNameYears)
|
||||
import StoreService.Names.Phases (RegistrationId)
|
||||
import StoreService.Names.Store (CodeHashTaken (..), NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, getPrepaidCodeById, insertPrepaidCode, releasePrepaidCode, reservePrepaidCode, revokePrepaidCode, spendPrepaidCode)
|
||||
import StoreService.Names.Store (CodeHashTaken (..), NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, getPrepaidCodeById, insertPrepaidCode, releasePrepaidCode, reservePrepaidCode, revokePrepaidCode, spendLandedPrepaidCode, spendPrepaidCode)
|
||||
import StoreService.Names.Turns (Turns, withCodeTurn)
|
||||
import StoreService.Store (RevokeResult (..))
|
||||
|
||||
@@ -137,6 +138,11 @@ releaseCode turns st codeId rid = afterWrite turns st codeId (\db -> releasePrep
|
||||
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)
|
||||
|
||||
-- | As spendCode, for a registration whose register went through: a code it no longer holds is spent too while still usable.
|
||||
-- False when the code was released and then spent, revoked or expired meanwhile.
|
||||
spendLandedCode :: Turns -> DBStore -> UTCTime -> Int64 -> RegistrationId -> (DB.Connection -> IO (Either e a)) -> IO (Either e (a, Bool))
|
||||
spendLandedCode turns st now codeId rid = afterWrite turns st codeId (\db -> spendLandedPrepaidCode 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
|
||||
|
||||
@@ -16,6 +16,7 @@ module StoreService.Names.Contracts
|
||||
chainPriceCents,
|
||||
sponsorNonce,
|
||||
resolverNonce,
|
||||
confirmedTag,
|
||||
ownerOfToken,
|
||||
nameExpires,
|
||||
commitmentTime,
|
||||
@@ -40,7 +41,7 @@ import Simplex.Messaging.SimplexName (LabelHash (..), SimplexTLD)
|
||||
import Simplex.Messaging.Util (tshow)
|
||||
import StoreService.Config (ChainConfig (..))
|
||||
import StoreService.Names.Abi
|
||||
import StoreService.Names.Chain (Block (..), BlockTag (..), ChainClient (..), ChainError (..), ethCallAt, latestBlock)
|
||||
import StoreService.Names.Chain (Block (..), BlockTag (..), ChainClient (..), ChainError (..), confirmedNumber, ethCallAt, latestBlock)
|
||||
import StoreService.Names.Registration (channelRecordKey, contactRecordKey, labelNode, nodeBytes)
|
||||
import StoreService.Names.Revert (RevertName (..))
|
||||
import System.Timeout (timeout)
|
||||
@@ -137,6 +138,10 @@ sponsorNonce ct owner = call ct (cSponsor $ chainConfig ct) ("nonces(address)",
|
||||
resolverNonce :: Contracts -> Address -> IO (Either ChainError Word64)
|
||||
resolverNonce ct owner = call ct (resolver ct) ("nonces(address)", addressWord owner) decodeWord64
|
||||
|
||||
-- | The newest block at the configured confirmation depth, so a read there sees only what a reorg cannot undo.
|
||||
confirmedTag :: Contracts -> IO (Either ChainError BlockTag)
|
||||
confirmedTag Contracts {ctChain} = fmap (AtBlock . confirmedNumber ctChain . blockNumber) <$> latestBlock ctChain
|
||||
|
||||
-- | Nothing when nobody holds the name: @ownerOf@ then fails the registrar's bare @require@ on the expiry,
|
||||
-- a revert without data. Any other revert is a failed read.
|
||||
ownerOfToken :: Contracts -> BlockTag -> LabelHash -> IO (Either ChainError (Maybe Address))
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
module StoreService.Names.Env (NamesEnv (..), newNamesEnv) where
|
||||
|
||||
import Data.Time.Clock (getCurrentTime)
|
||||
import Simplex.Chat.Controller (ChatController (..))
|
||||
import Simplex.Messaging.Agent.Protocol (UserId)
|
||||
import Simplex.Messaging.Agent.Store.Common (DBStore)
|
||||
@@ -10,6 +11,7 @@ import StoreService.Names.Chain (ChainClient, newChainClient)
|
||||
import StoreService.Names.Contracts (Contracts, newContracts)
|
||||
import StoreService.Names.Phases (Phases, newPhases)
|
||||
import StoreService.Names.Profiles (Profiles, chatProfile, defaultProfileLimits, monotonicSeconds, newProfiles, systemResolver)
|
||||
import StoreService.Names.Sender (Sender, newSender)
|
||||
import StoreService.Names.Turns (Turns)
|
||||
import StoreService.Providers (Provider)
|
||||
import StoreService.Waiters (Waiters)
|
||||
@@ -24,6 +26,7 @@ data NamesEnv = NamesEnv
|
||||
neChain :: ChainClient,
|
||||
neContracts :: Contracts,
|
||||
neProfiles :: Profiles,
|
||||
neSender :: Sender,
|
||||
neWaiters :: Waiters,
|
||||
neProviders :: [Provider]
|
||||
}
|
||||
@@ -34,4 +37,5 @@ newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD,
|
||||
neChain <- newChainClient random nChain
|
||||
neContracts <- newContracts nTLD neChain
|
||||
neProfiles <- newProfiles defaultProfileLimits monotonicSeconds (chatProfile neChat neUserId) systemResolver
|
||||
pure NamesEnv {neChat, neUserId, neConfig, neStore, nePhases, neTurns, neChain, neContracts, neProfiles, neWaiters, neProviders}
|
||||
neSender <- newSender random neChain neStore getCurrentTime
|
||||
pure NamesEnv {neChat, neUserId, neConfig, neStore, nePhases, neTurns, neChain, neContracts, neProfiles, neSender, neWaiters, neProviders}
|
||||
@@ -48,6 +48,7 @@ module StoreService.Names.Store
|
||||
reservePrepaidCode,
|
||||
releasePrepaidCode,
|
||||
spendPrepaidCode,
|
||||
spendLandedPrepaidCode,
|
||||
revokePrepaidCode,
|
||||
insertTransaction,
|
||||
recordTransactionOutcome,
|
||||
@@ -677,6 +678,21 @@ spendPrepaidCode db codeId (RegistrationId rid) now =
|
||||
"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)
|
||||
|
||||
-- | For a registration whose register went through: spends the code it holds, or one released and still usable; False otherwise.
|
||||
spendLandedPrepaidCode :: DB.Connection -> Int64 -> RegistrationId -> UTCTime -> IO Bool
|
||||
spendLandedPrepaidCode db codeId (RegistrationId rid) now' =
|
||||
(> 0)
|
||||
<$> executeChanging
|
||||
db
|
||||
[sql|
|
||||
UPDATE sx_badge_service_name_prepaid_codes SET reserved_by = ?, reserved_at = COALESCE(reserved_at, ?), spent_at = ?
|
||||
WHERE name_prepaid_code_id = ? AND spent_at IS NULL
|
||||
AND (reserved_by = ? OR (reserved_by IS NULL AND revoked_at IS NULL AND code_payment_status <> ? AND (expires_at IS NULL OR expires_at > ?)))
|
||||
|]
|
||||
(rid, now, now, codeId, rid, CPSUnpaid, now)
|
||||
where
|
||||
now = truncateToSecond now'
|
||||
|
||||
-- | 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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
|
||||
-- | 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
|
||||
module StoreService.Names.Turns (Turns, newTurns, withLabelTurn, withCodeTurn, turnsCount, labelTurnCount) where
|
||||
|
||||
import Control.Concurrent.STM
|
||||
import Control.Exception (bracket, bracket_)
|
||||
@@ -25,6 +25,10 @@ withLabelTurn Turns {labelTurns} (LabelHash h) = withTurn labelTurns h
|
||||
turnsCount :: Turns -> IO Int
|
||||
turnsCount Turns {labelTurns, codeTurns} = atomically $ (+) <$> (Map.size <$> readTVar labelTurns) <*> (Map.size <$> readTVar codeTurns)
|
||||
|
||||
-- | The requests holding or waiting for the label hash's turn.
|
||||
labelTurnCount :: Turns -> LabelHash -> STM Int
|
||||
labelTurnCount Turns {labelTurns} (LabelHash h) = maybe 0 snd . Map.lookup h <$> readTVar labelTurns
|
||||
|
||||
-- | Keyed by the code's SHA-256 hash.
|
||||
withCodeTurn :: Turns -> ByteString -> IO a -> IO a
|
||||
withCodeTurn Turns {codeTurns} = withTurn codeTurns
|
||||
|
||||
@@ -0,0 +1,460 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
-- | Each registration from queued to registered, taken or failed: its commit, the wait for the page's reveal, and its register;
|
||||
-- and Try again, which queues a failed one again. A step starts from the stored registration, the sender's transactions in flight
|
||||
-- and the chain, so a restart resumes any phase.
|
||||
module StoreService.Names.Worker
|
||||
( Worker,
|
||||
Next (..),
|
||||
newWorker,
|
||||
stepRegistration,
|
||||
runWorkers,
|
||||
sweepFailedCodes,
|
||||
runCodeSweep,
|
||||
maxUnexpiredWaits,
|
||||
Retried (..),
|
||||
RetryRefusal (..),
|
||||
retryRegistration,
|
||||
)
|
||||
where
|
||||
|
||||
import Control.Concurrent (threadDelay)
|
||||
import Control.Concurrent.Async (Async, asyncWithUnmask, cancel, poll)
|
||||
import Control.Concurrent.STM
|
||||
import Control.Exception (finally, mask_)
|
||||
import Control.Monad (forM, forM_, forever, void, when)
|
||||
import Control.Monad.Except (ExceptT (..), runExceptT)
|
||||
import Data.Functor ((<&>))
|
||||
import Data.Int (Int64)
|
||||
import Data.Map.Strict (Map)
|
||||
import qualified Data.Map.Strict as M
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, diffUTCTime)
|
||||
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
|
||||
import Data.Word (Word64)
|
||||
import Simplex.Messaging.Agent.Store.Common (withTransaction)
|
||||
import Simplex.Messaging.Util (tshow)
|
||||
import StoreService.Config (ChainConfig (..), NamesConfig (..))
|
||||
import StoreService.Log (logError, logInfo, logWarn, safely, safelyWith)
|
||||
import StoreService.Names.Chain (Block (..), ChainClient, ChainError (..), TxHash, blockAt)
|
||||
import StoreService.Names.Codes (CodeRefusal (..), codeRefusal, releaseCode, reserveCode)
|
||||
import StoreService.Names.Contracts (CommitmentAges (..), commitmentAges, commitmentTime, controllerAvailable, sponsorNonce)
|
||||
import StoreService.Names.Orders (Landing (..), OrdersEnv (..), WriteRefusal (..), confirmLanded, versionWritten)
|
||||
import StoreService.Names.Phases (RegistrationId (..), awaitChange, publish)
|
||||
import StoreService.Names.Registration (commitCalldata, registerWithCreditCalldata, sponsorDomain, verifyRegisterIntent)
|
||||
import StoreService.Names.Reveal (intentOf)
|
||||
import StoreService.Names.Revert (RevertName (..))
|
||||
import StoreService.Names.Sender (PendingTx, SendError (..), Sender, awaitOutcome, inFlightTransaction, ptHash, submitTransaction)
|
||||
import StoreService.Names.Store
|
||||
import StoreService.Names.Turns (withLabelTurn)
|
||||
import StoreService.Names.View (lapseMoment, notSignedInTime, registrationAt, registrationVersion)
|
||||
|
||||
data Worker = Worker
|
||||
{ wkOrders :: OrdersEnv,
|
||||
wkChain :: ChainClient,
|
||||
wkSender :: Sender,
|
||||
-- | the thread following each registration, kept by the scan
|
||||
wkThreads :: TVar (Map RegistrationId (Async ())),
|
||||
-- | the one-second waits in a row after a commit's dry run reverted UnexpiredCommitmentExists
|
||||
wkUnexpiredWaits :: TVar (Map RegistrationId Int)
|
||||
}
|
||||
|
||||
-- | What a step leaves to the next one.
|
||||
data Next
|
||||
= -- | the registration is in none of the worker's phases
|
||||
Done
|
||||
| -- | the step wrote, or the registration changed since it was read
|
||||
Again
|
||||
| -- | nothing to do before this time, unless the registration moves on from this version
|
||||
WaitUntil Int64 UTCTime
|
||||
| -- | a chain read or a send failed, and the phase is kept
|
||||
Retry Int64 Text
|
||||
| -- | a transaction of the registration is in flight
|
||||
Await PendingTx
|
||||
|
||||
-- | How often the stored registrations are scanned for one without a thread, such as an order just paid or requeued.
|
||||
scanInterval :: Int
|
||||
scanInterval = 5000000
|
||||
|
||||
retryInterval :: NominalDiffTime
|
||||
retryInterval = 15
|
||||
|
||||
-- | The registration is read again after this wait for its transaction's outcome.
|
||||
outcomeWait :: Int
|
||||
outcomeWait = 60000000
|
||||
|
||||
-- | A long wait is cut into waits of at most this, so a clock adjustment delays a moment by no more.
|
||||
maxWait :: NominalDiffTime
|
||||
maxWait = 60
|
||||
|
||||
-- | One-second waits in a row after a commit's dry run reverts UnexpiredCommitmentExists, before the usual retry interval:
|
||||
-- enough for the node's block to pass the lapse second, not to spin on a node that keeps disagreeing with its own read.
|
||||
maxUnexpiredWaits :: Int
|
||||
maxUnexpiredWaits = 3
|
||||
|
||||
-- | Block times are whole seconds.
|
||||
chainSecond :: NominalDiffTime
|
||||
chainSecond = 1
|
||||
|
||||
-- | A failed code order keeps its code this long for Try again.
|
||||
failedCodeHold :: NominalDiffTime
|
||||
failedCodeHold = 30 * 86400
|
||||
|
||||
sweepInterval :: Int
|
||||
sweepInterval = 86400 * 1000000
|
||||
|
||||
notRegistered :: Text
|
||||
notRegistered = "The name could not be registered"
|
||||
|
||||
newWorker :: OrdersEnv -> ChainClient -> Sender -> IO Worker
|
||||
newWorker wkOrders wkChain wkSender = Worker wkOrders wkChain wkSender <$> newTVarIO M.empty <*> newTVarIO M.empty
|
||||
|
||||
-- | A thread for each registration in the worker's phases, started by a scan every scanInterval; they stop with it.
|
||||
runWorkers :: Worker -> IO ()
|
||||
runWorkers w@Worker {wkThreads} =
|
||||
forever (safely "names worker: the scan" (startWorkers w) >> threadDelay scanInterval)
|
||||
`finally` (readTVarIO wkThreads >>= mapM_ cancel)
|
||||
|
||||
startWorkers :: Worker -> IO ()
|
||||
startWorkers w@Worker {wkOrders = OrdersEnv {oeStore}, wkThreads} = do
|
||||
running <- readTVarIO wkThreads >>= M.traverseMaybeWithKey (\_ a -> maybe (Just a) (const Nothing) <$> poll a)
|
||||
atomically $ writeTVar wkThreads running
|
||||
active <- withTransaction oeStore activeRegistrations
|
||||
forM_ [nrId | NameRegistration {nrId} <- active, M.notMember nrId running] $ \rid ->
|
||||
mask_ $ asyncWithUnmask (\unmask -> unmask $ work w rid) >>= atomically . modifyTVar' wkThreads . M.insert rid
|
||||
|
||||
work :: Worker -> RegistrationId -> IO ()
|
||||
work w rid@(RegistrationId ridText) = void $ safely ("names worker: registration " <> ridText) $ follow w rid Nothing
|
||||
|
||||
-- | Steps until the registration leaves the worker's phases, logging a lasting failure once.
|
||||
follow :: Worker -> RegistrationId -> Maybe Text -> IO ()
|
||||
follow w@Worker {wkOrders = OrdersEnv {oeStore, oePhases, oeNow}} rid@(RegistrationId ridText) lastFailure =
|
||||
stepRegistration w rid >>= \case
|
||||
Done -> pure ()
|
||||
Again -> follow w rid Nothing
|
||||
WaitUntil v t -> waitFor v t >> follow w rid Nothing
|
||||
Retry v reason -> do
|
||||
when (Just reason /= lastFailure) $ logWarn $ "names worker: registration " <> ridText <> " waits: " <> reason
|
||||
now <- oeNow
|
||||
waitFor v $ addUTCTime retryInterval now
|
||||
follow w rid $ Just reason
|
||||
Await p -> awaitOutcome outcomeWait p >> follow w rid Nothing
|
||||
where
|
||||
waitFor v t = do
|
||||
now <- oeNow
|
||||
let micros = max 0 $ ceiling $ min maxWait (diffUTCTime t now) * 1000000
|
||||
void $ awaitChange oePhases rid (registrationVersion oeStore rid) v micros
|
||||
|
||||
stepRegistration :: Worker -> RegistrationId -> IO Next
|
||||
stepRegistration w@Worker {wkOrders = OrdersEnv {oeStore, oeNow}} rid = do
|
||||
now <- oeNow
|
||||
withTransaction oeStore (`getRegistration` rid) >>= \case
|
||||
Nothing -> pure Done
|
||||
Just r -> case nrPhase r of
|
||||
PQueued -> commit w now r
|
||||
PCommitting -> commit w now r
|
||||
PCommitted -> committed w now r
|
||||
PRegistering -> registering w now r
|
||||
_ -> pure Done
|
||||
|
||||
-- | A commit of the registration in flight is waited for; otherwise commitments(commitment) decides.
|
||||
commit :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
commit w@Worker {wkOrders = OrdersEnv {oeContracts}, wkSender} now r@NameRegistration {nrId, nrCommitment} =
|
||||
inFlightTransaction wkSender (TxCommit nrId) >>= \case
|
||||
Just p -> commitSent w now r p
|
||||
Nothing ->
|
||||
runExceptT ((,) <$> ExceptT (commitmentAges oeContracts) <*> ExceptT (commitmentTime oeContracts nrCommitment)) >>= \case
|
||||
Left e -> pure $ failure r e
|
||||
Right (_, Nothing) -> sendCommit w now r
|
||||
Right (ages, Just t) -> fromCommitment w now r ages t
|
||||
|
||||
-- | At the lapse second a commitment can neither register nor be committed again, so the worker waits a second past it,
|
||||
-- then commits again with the stale fields cleared.
|
||||
fromCommitment :: Worker -> UTCTime -> NameRegistration -> CommitmentAges -> Word64 -> IO Next
|
||||
fromCommitment w@Worker {wkOrders = OrdersEnv {oeConfig}} now r ages t
|
||||
| reusable oeConfig now lapse = toCommitted w now r ages t
|
||||
| lapse >= unixSeconds now = pure $ waitUntil r $ addUTCTime chainSecond $ secondsTime lapse
|
||||
| lapsedFields r /= r = save w now $ lapsedFields r
|
||||
| otherwise = sendCommit w now r
|
||||
where
|
||||
lapse = commitmentLapse ages t
|
||||
|
||||
-- | The revealBefore of a commitment committed at this time.
|
||||
commitmentLapse :: CommitmentAges -> Word64 -> Integer
|
||||
commitmentLapse CommitmentAges {caMax} t = toInteger t + caMax
|
||||
|
||||
-- | A live commitment is reused while revealMargin is left before its lapse, time for the page's reveal and the register to mine.
|
||||
reusable :: NamesConfig -> UTCTime -> Integer -> Bool
|
||||
reusable NamesConfig {nRevealMarginSeconds} now lapse = lapse - unixSeconds now >= toInteger nRevealMarginSeconds
|
||||
|
||||
-- | The intent's deadline is the lapsed revealBefore, so it goes with the old transactions and reveal times; the page signs again.
|
||||
lapsedFields :: NameRegistration -> NameRegistration
|
||||
lapsedFields r =
|
||||
r
|
||||
{ nrReveal = Nothing,
|
||||
nrError = Nothing,
|
||||
nrCommitTx = Nothing,
|
||||
nrCommitBlock = Nothing,
|
||||
nrRegisterTx = Nothing,
|
||||
nrRegisterBlock = Nothing,
|
||||
nrRevealAfter = Nothing,
|
||||
nrRevealBefore = Nothing
|
||||
}
|
||||
|
||||
-- | The reveal times come from the commitment's time. When that is not the block of our commit mined with status 1,
|
||||
-- someone committed it again after a lapse, so our commit and the reveal signed for its window are cleared.
|
||||
toCommitted :: Worker -> UTCTime -> NameRegistration -> CommitmentAges -> Word64 -> IO Next
|
||||
toCommitted w now r@NameRegistration {nrId} CommitmentAges {caMin, caMax} t =
|
||||
unexpiredDone w nrId >> ourCommit w r t >>= \case
|
||||
Left e -> pure $ failure r e
|
||||
Right ours -> save w now (minedBy ours) {nrPhase = PCommitted, nrRevealAfter = Just $ after caMin, nrRevealBefore = Just $ after caMax}
|
||||
where
|
||||
after age = fromInteger $ toInteger t + age
|
||||
minedBy = \case
|
||||
Just (h, block) -> r {nrCommitTx = Just h, nrCommitBlock = Just block}
|
||||
Nothing -> r {nrReveal = Nothing, nrCommitTx = Nothing, nrCommitBlock = Nothing}
|
||||
|
||||
-- | Among the signatures of the commit last sent, the one mined with status 1 at the commitment's time.
|
||||
ourCommit :: Worker -> NameRegistration -> Word64 -> IO (Either ChainError (Maybe (TxHash, Word64)))
|
||||
ourCommit Worker {wkOrders = OrdersEnv {oeStore}, wkChain} NameRegistration {nrId, nrCommitTx} t = do
|
||||
txs <- withTransaction oeStore (`registrationTransactions` nrId)
|
||||
let commits = [tx | tx@NameTransaction {ntFor = TxCommit _} <- txs]
|
||||
lastSent = [ntNonce | NameTransaction {ntHash, ntNonce} <- commits, Just ntHash == nrCommitTx]
|
||||
case [(ntHash, block) | NameTransaction {ntHash, ntNonce, ntStatus = TSMined, ntMinedBlock = Just block} <- commits, ntNonce `elem` lastSent] of
|
||||
(h, block) : _ -> fmap (\Block {blockTimestamp} -> if blockTimestamp == t then Just (h, block) else Nothing) <$> blockAt wkChain block
|
||||
[] -> pure $ Right Nothing
|
||||
|
||||
sendCommit :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
sendCommit w@Worker {wkOrders = OrdersEnv {oeConfig}, wkSender} now r@NameRegistration {nrId, nrCommitment} =
|
||||
submitTransaction wkSender (TxCommit nrId) (cController $ nChain oeConfig) (commitCalldata nrCommitment) >>= \case
|
||||
-- a commit mined since the read, or a latest block still at the lapse second
|
||||
Left (SEChain (ChainReverted (Just RNUnexpiredCommitmentExists))) -> unexpired w now r
|
||||
sendResult -> do
|
||||
unexpiredDone w nrId
|
||||
case sendResult of
|
||||
Right p -> commitSent w now r p
|
||||
Left SEInFlight -> pure Again
|
||||
Left e -> pure $ failure r e
|
||||
|
||||
unexpired :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
unexpired Worker {wkUnexpiredWaits} now r@NameRegistration {nrId = rid@(RegistrationId ridText)} = do
|
||||
waits <- atomically $ stateTVar wkUnexpiredWaits $ \m -> let n = M.findWithDefault 0 rid m + 1 in (n, M.insert rid n m)
|
||||
if waits <= maxUnexpiredWaits
|
||||
then do
|
||||
logWarn $ "names worker: registration " <> ridText <> ": the commit's dry run reverted UnexpiredCommitmentExists, wait " <> tshow waits <> " of a second"
|
||||
pure $ waitUntil r $ addUTCTime chainSecond now
|
||||
else pure $ Retry (nrVersion r) "the commit's dry run keeps reverting UnexpiredCommitmentExists"
|
||||
|
||||
unexpiredDone :: Worker -> RegistrationId -> IO ()
|
||||
unexpiredDone Worker {wkUnexpiredWaits} = atomically . modifyTVar' wkUnexpiredWaits . M.delete
|
||||
|
||||
-- | Recorded with the newest signature's hash, so a fee-bumped replacement is the one the view shows.
|
||||
commitSent :: Worker -> UTCTime -> NameRegistration -> PendingTx -> IO Next
|
||||
commitSent w now r@NameRegistration {nrPhase, nrCommitTx} p
|
||||
| nrPhase == PCommitting && nrCommitTx == Just (ptHash p) = pure $ Await p
|
||||
| otherwise = save w now r {nrPhase = PCommitting, nrCommitTx = Just $ ptHash p, nrCommitBlock = Nothing}
|
||||
|
||||
-- | Waits for the page's reveal until the lapse, and for revealAfter, then registers.
|
||||
committed :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
committed w@Worker {wkSender} now r@NameRegistration {nrId, nrReveal, nrRevealAfter} =
|
||||
inFlightTransaction wkSender (TxRegister nrId) >>= \case
|
||||
Just p -> registerSent w now r p
|
||||
Nothing -> case nrReveal of
|
||||
Nothing -> case lapseMoment r of
|
||||
Just lapse | now <= lapse -> pure $ waitUntil r $ addUTCTime chainSecond lapse
|
||||
_ -> save w now r {nrPhase = PFailed, nrFailedAt = Just now, nrError = Just notSignedInTime}
|
||||
Just rv -> case secondsTime . toInteger <$> nrRevealAfter of
|
||||
Just revealAfter | now < revealAfter -> pure $ waitUntil r revealAfter
|
||||
_ -> sendRegister w now r rv
|
||||
|
||||
-- | The stored intent is checked before its dry run, whose revert decides at once; no label turn is held across the send.
|
||||
sendRegister :: Worker -> UTCTime -> NameRegistration -> NameReveal -> IO Next
|
||||
sendRegister w@Worker {wkOrders = OrdersEnv {oeConfig = cfg@NamesConfig {nChain = ChainConfig {cChainId, cSponsor}}}, wkSender} now r@NameRegistration {nrId = regId@(RegistrationId rid)} rv =
|
||||
verifyRegisterIntent (sponsorDomain cChainId cSponsor) (fromInteger $ unixSeconds now) intent >>= \case
|
||||
-- past its deadline the commitment has lapsed too; a wrong signer follows a change of the chain settings since the reveal
|
||||
Left refusal -> do
|
||||
logWarn $ "names worker: registration " <> rid <> ": its stored intent is refused, " <> T.pack refusal
|
||||
judge w now r Nothing
|
||||
Right () ->
|
||||
submitTransaction wkSender (TxRegister regId) cSponsor (registerWithCreditCalldata intent) >>= \case
|
||||
Right p -> registerSent w now r p
|
||||
Left SEInFlight -> pure Again
|
||||
Left (SEChain (ChainReverted reason)) -> judge w now r reason
|
||||
Left e -> pure $ failure r e
|
||||
where
|
||||
intent = intentOf cfg r rv
|
||||
|
||||
registerSent :: Worker -> UTCTime -> NameRegistration -> PendingTx -> IO Next
|
||||
registerSent w now r@NameRegistration {nrPhase, nrRegisterTx} p
|
||||
| nrPhase == PRegistering && nrRegisterTx == Just (ptHash p) = pure $ Await p
|
||||
| otherwise = save w now r {nrPhase = PRegistering, nrRegisterTx = Just $ ptHash p, nrRegisterBlock = Nothing}
|
||||
|
||||
-- | With none of its register transactions in flight, none can be mined any more: each mined, or its nonce taken.
|
||||
registering :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
registering w@Worker {wkSender} now r@NameRegistration {nrId} =
|
||||
inFlightTransaction wkSender (TxRegister nrId) >>= \case
|
||||
Just p -> registerSent w now r p
|
||||
Nothing -> judge w now r Nothing
|
||||
|
||||
-- | A register that did not go through, by its dry run's revert or once every transaction settled: registered when one mined
|
||||
-- with status 1 or the owner holds the name; committed again on InvalidNonce, for the page to sign again; taken on any revert
|
||||
-- while the name is not available at the latest block; failed otherwise, unless the dry run named the name as gone.
|
||||
judge :: Worker -> UTCTime -> NameRegistration -> Maybe RevertName -> IO Next
|
||||
judge w@Worker {wkOrders = oe@OrdersEnv {oeContracts}} now r@NameRegistration {nrReveal} reason =
|
||||
inTurn w r $
|
||||
confirmLanded oe r >>= \case
|
||||
LRegistered v -> pure (Just v, Again)
|
||||
LChanged -> pure (Nothing, Again)
|
||||
LUnread -> pure (Nothing, Retry (nrVersion r) "the register check's chain read failed")
|
||||
LNotLanded -> case reason of
|
||||
Just RNInvalidNonce -> store w now r {nrReveal = Nothing}
|
||||
Just RNCommitmentTooNew -> pure (Nothing, waitUntil r $ addUTCTime chainSecond now)
|
||||
_ -> case nrReveal of
|
||||
Just NameReveal {rvLabel} ->
|
||||
controllerAvailable oeContracts rvLabel >>= \case
|
||||
Left e -> pure (Nothing, failure r e)
|
||||
Right False -> taken w now r
|
||||
-- the dry run runs on the pending block, where a competitor's register that may never mine can hold the name
|
||||
Right True
|
||||
| reason `elem` map Just [RNNameNotAvailable, RNNameReserved] -> pure (Nothing, Retry (nrVersion r) "the dry run reverted the name as gone, but it reads available")
|
||||
| otherwise -> failed w now r reason
|
||||
Nothing -> failed w now r reason
|
||||
|
||||
-- | The payment is kept, and a code order's code released.
|
||||
taken :: Worker -> UTCTime -> NameRegistration -> IO (Maybe Int64, Next)
|
||||
taken Worker {wkOrders = OrdersEnv {oeStore, oeTurns}} now r@NameRegistration {nrId, nrPayment} = case nrPayment of
|
||||
RPInvoice {} -> written r <$> withTransaction oeStore write
|
||||
RPCode codeId -> written r . fmap fst <$> releaseCode oeTurns oeStore codeId nrId write
|
||||
where
|
||||
write db = versionWritten <$> updateRegistration db r {nrPhase = PTaken, nrError = Nothing} now
|
||||
|
||||
-- | A code order keeps its code for Try again.
|
||||
failed :: Worker -> UTCTime -> NameRegistration -> Maybe RevertName -> IO (Maybe Int64, Next)
|
||||
failed w now r@NameRegistration {nrId = RegistrationId rid} reason = do
|
||||
when (reason == Just RNInsufficientAllowance) $
|
||||
logError $ "names worker: registration " <> rid <> " failed: the registrar's allowance on the sponsor is used up; the guardian raises it with setRegistrarAllowance"
|
||||
store w now r {nrPhase = PFailed, nrFailedAt = Just now, nrError = Just notRegistered}
|
||||
|
||||
-- | A compare-and-set on the version read, in the caller's turn of the name.
|
||||
store :: Worker -> UTCTime -> NameRegistration -> IO (Maybe Int64, Next)
|
||||
store Worker {wkOrders = OrdersEnv {oeStore}} now r = written r <$> withTransaction oeStore (\db -> versionWritten <$> updateRegistration db r now)
|
||||
|
||||
save :: Worker -> UTCTime -> NameRegistration -> IO Next
|
||||
save w now r = inTurn w r $ store w now r
|
||||
|
||||
-- | In the name's turn, as the requests that change an order; the version written is published after the turn.
|
||||
inTurn :: Worker -> NameRegistration -> IO (Maybe Int64, Next) -> IO Next
|
||||
inTurn Worker {wkOrders = OrdersEnv {oeTurns, oePhases}} NameRegistration {nrId, nrLabelHash} action = do
|
||||
(v, next) <- withLabelTurn oeTurns nrLabelHash action
|
||||
mapM_ (atomically . publish oePhases nrId) v
|
||||
pure next
|
||||
|
||||
written :: NameRegistration -> Either WriteRefusal Int64 -> (Maybe Int64, Next)
|
||||
written r = \case
|
||||
Right v -> (Just v, Again)
|
||||
Left WRChanged -> (Nothing, Again)
|
||||
Left (WRRefused refusal) -> (Nothing, Retry (nrVersion r) $ "its write was refused: " <> tshow refusal)
|
||||
|
||||
failure :: Show e => NameRegistration -> e -> Next
|
||||
failure r e = Retry (nrVersion r) $ tshow e
|
||||
|
||||
waitUntil :: NameRegistration -> UTCTime -> Next
|
||||
waitUntil r = WaitUntil (nrVersion r)
|
||||
|
||||
unixSeconds :: UTCTime -> Integer
|
||||
unixSeconds = floor . utcTimeToPOSIXSeconds
|
||||
|
||||
secondsTime :: Integer -> UTCTime
|
||||
secondsTime = posixSecondsToUTCTime . fromInteger
|
||||
|
||||
-- | What Try again did: the version it wrote, the order changed since it was read, or a refusal.
|
||||
data Retried = RTWritten Int64 | RTChanged | RTRefused RetryRefusal
|
||||
|
||||
data RetryRefusal = RFNotFailed | RFLinksRefused | RFCode CodeRefusal | RFUnread
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | Try again, in the caller's turn of the order's name: registered when its register went through; else a code order's code
|
||||
-- must still pay for it; else queued again at no charge, a released code reserved again in the code's turn.
|
||||
retryRegistration :: OrdersEnv -> NameRegistration -> IO Retried
|
||||
retryRegistration oe@OrdersEnv {oeNow} current = do
|
||||
now <- oeNow
|
||||
case registrationAt now current of
|
||||
-- its commitment fixes the refused links, so only another name can register
|
||||
NameRegistration {nrPhase = PFailed, nrLinksRefusedAt = Just _} -> pure $ RTRefused RFLinksRefused
|
||||
r@NameRegistration {nrPhase = PFailed} ->
|
||||
confirmLanded oe r >>= \case
|
||||
LRegistered v -> pure $ RTWritten v
|
||||
LChanged -> pure RTChanged
|
||||
LUnread -> pure $ RTRefused RFUnread
|
||||
LNotLanded -> retryCode oe now r >>= either (pure . RTRefused . RFCode) (requeue oe now r)
|
||||
_ -> pure $ RTRefused RFNotFailed
|
||||
|
||||
-- | The code a code order still holds, though revoked or expired since, or a released one it can reserve again.
|
||||
retryCode :: OrdersEnv -> UTCTime -> NameRegistration -> IO (Either CodeRefusal (Maybe PrepaidCode))
|
||||
retryCode OrdersEnv {oeStore} now NameRegistration {nrId, nrLength, nrPayment} = case nrPayment of
|
||||
RPInvoice {} -> pure $ Right Nothing
|
||||
RPCode codeId ->
|
||||
withTransaction oeStore (`getPrepaidCodeById` codeId) <&> \case
|
||||
Just code -> maybe (Right $ Just code) Left $ codeRefusal now nrId nrLength code
|
||||
Nothing -> Left CRInvalid
|
||||
|
||||
-- | The register's transactions, which confirmLanded has read, are cleared with the failure; the reveal unless its intent still serves.
|
||||
requeue :: OrdersEnv -> UTCTime -> NameRegistration -> Maybe PrepaidCode -> IO Retried
|
||||
requeue oe@OrdersEnv {oeStore, oeTurns} now r@NameRegistration {nrId, nrLength} code =
|
||||
servingReveal oe now r >>= \case
|
||||
Left e -> RTRefused RFUnread <$ logWarn ("names: a retry's chain read failed: " <> tshow e)
|
||||
Right reveal -> do
|
||||
let requeued = r {nrPhase = PQueued, nrReveal = reveal, nrFailedAt = Nothing, nrError = Nothing, nrRegisterTx = Nothing, nrRegisterBlock = Nothing}
|
||||
write db = versionWritten <$> updateRegistration db requeued now
|
||||
outcome <- case code of
|
||||
Nothing -> Right <$> withTransaction oeStore write
|
||||
Just PrepaidCode {pcCodeHash} -> reserveCode oeTurns oeStore now pcCodeHash nrId nrLength write
|
||||
pure $ case outcome of
|
||||
Left refusal -> RTRefused $ RFCode refusal
|
||||
Right (Right v) -> RTWritten v
|
||||
-- a failed order is already live, so only a changed version refuses the write
|
||||
Right (Left _) -> RTChanged
|
||||
|
||||
-- | The stored reveal while its intent can still register: accepted by the worker's check before sending, the sponsor nonce unmoved,
|
||||
-- and the commitment live with revealMargin left in a window the intent's deadline covers. A new commit or another's re-commit
|
||||
-- opens a window the old deadline cannot serve.
|
||||
servingReveal :: OrdersEnv -> UTCTime -> NameRegistration -> IO (Either ChainError (Maybe NameReveal))
|
||||
servingReveal OrdersEnv {oeConfig = cfg@NamesConfig {nChain = ChainConfig {cChainId, cSponsor}}, oeContracts} now r@NameRegistration {nrOwner, nrCommitment, nrReveal} = case nrReveal of
|
||||
Nothing -> pure $ Right Nothing
|
||||
Just rv@NameReveal {rvNonce, rvDeadline} ->
|
||||
verifyRegisterIntent (sponsorDomain cChainId cSponsor) (fromInteger $ unixSeconds now) (intentOf cfg r rv) >>= \case
|
||||
Left _ -> pure $ Right Nothing
|
||||
Right () -> runExceptT $ do
|
||||
nonce <- ExceptT $ sponsorNonce oeContracts nrOwner
|
||||
if nonce /= rvNonce
|
||||
then pure Nothing
|
||||
else do
|
||||
ages <- ExceptT $ commitmentAges oeContracts
|
||||
committedAt <- ExceptT $ commitmentTime oeContracts nrCommitment
|
||||
pure $ case commitmentLapse ages <$> committedAt of
|
||||
Just lapse | reusable cfg now lapse && lapse <= toInteger rvDeadline -> Just rv
|
||||
_ -> Nothing
|
||||
|
||||
-- | Each code order failed for failedCodeHold gives its code back, in its name's and its code's turns, unless it was requeued meanwhile.
|
||||
sweepFailedCodes :: Worker -> IO ()
|
||||
sweepFailedCodes Worker {wkOrders = OrdersEnv {oeStore, oeTurns, oeNow}} = do
|
||||
cutoff <- addUTCTime (negate failedCodeHold) <$> oeNow
|
||||
orders <- withTransaction oeStore (`failedCodeOrdersBefore` cutoff)
|
||||
released <- forM orders $ \(rid@(RegistrationId ridText), codeId) ->
|
||||
safelyWith ("names code sweep: registration " <> ridText) False $
|
||||
withTransaction oeStore (`getRegistration` rid) >>= \case
|
||||
Just NameRegistration {nrLabelHash} -> withLabelTurn oeTurns nrLabelHash $ either (const False) snd <$> releaseCode oeTurns oeStore codeId rid (stillFailed cutoff rid)
|
||||
Nothing -> pure False
|
||||
let count = length $ filter id released
|
||||
when (count > 0) $ logInfo $ "names code sweep: released the codes of " <> tshow count <> " failed order(s)"
|
||||
where
|
||||
stillFailed cutoff rid db =
|
||||
getRegistration db rid <&> \case
|
||||
Just NameRegistration {nrPhase = PFailed, nrFailedAt = Just failedAt} | failedAt < cutoff -> Right ()
|
||||
_ -> Left ()
|
||||
|
||||
runCodeSweep :: Worker -> IO ()
|
||||
runCodeSweep w = forever $ safely "names code sweep" (sweepFailedCodes w) >> threadDelay sweepInterval
|
||||
@@ -42,7 +42,10 @@ import StoreService.Group (GroupEvent, ensureManagedGroup, groupEvent, hasTracke
|
||||
import StoreService.Group.Command (badgeTypeP, codeP, maxMonths, maxNameYears, nameCodeP, prepaidCodeP, textTokenP)
|
||||
import StoreService.Names.Codes (issueNameCode, revokeNameCode)
|
||||
import StoreService.Names.Env (NamesEnv (..), newNamesEnv)
|
||||
import StoreService.Names.Orders (ordersEnv)
|
||||
import StoreService.Names.Sender (runSender)
|
||||
import StoreService.Names.Turns (Turns, newTurns)
|
||||
import StoreService.Names.Worker (newWorker, runCodeSweep, runWorkers)
|
||||
import StoreService.Options
|
||||
import StoreService.Poller (newPollerEnv, newReadHints, runPoller)
|
||||
import StoreService.Providers (Provider)
|
||||
@@ -244,7 +247,8 @@ storeService opts@StoreServiceOpts {serviceConfigFile} cfg env = do
|
||||
webEnv <- newWebEnv chatStore sc ws hints providers namesEnv
|
||||
exportWebapp sc
|
||||
pollerEnv <- newPollerEnv chatStore ws (nePhases <$> namesEnv) hints providers (poll sc)
|
||||
pure [runWebListeners webEnv, runPoller pollerEnv]
|
||||
nameLanes <- maybe (pure []) namesLanes namesEnv
|
||||
pure $ [runWebListeners webEnv, runPoller pollerEnv] <> nameLanes
|
||||
|
||||
startNames :: ChatController -> Turns -> Waiters -> [Provider] -> NamesConfig -> IO NamesEnv
|
||||
startNames cc@ChatController {chatStore} turns waiters providers nc@NamesConfig {nTLD, nCodesOnly, nChain = ChainConfig {cChainId}} =
|
||||
@@ -256,6 +260,12 @@ startNames cc@ChatController {chatStore} turns waiters providers nc@NamesConfig
|
||||
where
|
||||
codesOnlyNote = if nCodesOnly then ", codes only: registration needs a prepaid code" else ""
|
||||
|
||||
-- | The workers resume every registration in their phases, and the code sweep runs at the start and then daily.
|
||||
namesLanes :: NamesEnv -> IO [IO ()]
|
||||
namesLanes ne@NamesEnv {neChain, neSender} = do
|
||||
worker <- newWorker (ordersEnv ne) neChain neSender
|
||||
pure [runSender neSender, runWorkers worker, runCodeSweep worker]
|
||||
|
||||
seedServiceCatalog :: DBStore -> IO ()
|
||||
seedServiceCatalog st = do
|
||||
now <- truncateToSecond <$> getCurrentTime
|
||||
|
||||
@@ -8,11 +8,12 @@
|
||||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
-- Members' messages reach a console over separate connections in no fixed order, so gating is asserted from the bot's store.
|
||||
module Bots.StoreService.GroupIntegrationTests (badgeGroupIntegrationTests, namesBootTests, namesCodesChatTests) where
|
||||
module Bots.StoreService.GroupIntegrationTests (badgeGroupIntegrationTests, namesBootTests, namesCodesChatTests, namesWorkerBootTests) where
|
||||
|
||||
import StoreService.Config (BadgeIssuerKey (..), GroupConfig (..))
|
||||
import StoreService.Group (GroupAction (..), GroupEvent (..), codeInTracker, ensureManagedGroup)
|
||||
import StoreService.Group.Command (maxUses)
|
||||
import StoreService.Names.Abi (selector, uint256)
|
||||
import StoreService.Names.Codes (issueNameCode, reserveCode, revokeNameCode)
|
||||
import StoreService.Names.Phases (RegistrationId (..))
|
||||
import StoreService.Names.Store (PrepaidCode (..), getPrepaidCode, spendPrepaidCode)
|
||||
@@ -21,7 +22,9 @@ import StoreService.Options (StoreServiceOpts (..))
|
||||
import StoreService.Service (ServiceState (..), newServiceState, storeService)
|
||||
import StoreService.Store (ManagedGroup (..), getManagedGroup)
|
||||
import Bots.StoreService.BotTests (badgeBotName, badgeCmdUsage, badgeProfile, badgeTypeOf, credentialOf, entryOf, issueCode, issueRaw, mkStoreServiceOpts, newPurchaseKeys, redeemWithQueue, requestObject, revokeRaw, serviceDbPrefix, shouldAnswerError, statementOf, stopStoreService, testIssuerKeyIdx)
|
||||
import Bots.StoreService.ConfigTests (chainSection, namesSection)
|
||||
import Bots.StoreService.ConfigTests (chainSection, controllerAddr, namesSection)
|
||||
import Bots.StoreService.FakeEthNode (CallAnswer (..), FakeEthNode (..), answerCall, awaitSent, withFakeEthNode)
|
||||
import Bots.StoreService.WebTests (digestFixture)
|
||||
import Bots.StoreService.NamesSearchTests (aliceHex)
|
||||
import Bots.StoreService.NamesStoreTests (order, ownerA, placeCode, placePaid)
|
||||
import ChatClient
|
||||
@@ -139,7 +142,7 @@ badgeGroupIntegrationTests = do
|
||||
namesBootTests :: SpecWith TestParams
|
||||
namesBootTests = do
|
||||
it "boots codes-only, refusing paid name orders 403 codes_only and answering code orders and code checks, the badges shell as built" testNamesBootCodesOnly
|
||||
it "boots with [names] and [chain], routing fixed /api/name paths ahead of a search, registrations to GET, a path no route knows and a taken order's code to 404, and code checks and orders under one limit" testNamesBoot
|
||||
it "boots with [names] and [chain], routing fixed /api/name paths ahead of a search, registrations to GET, a reveal and a retry to POST, a path no route knows and a taken order's code to 404, code checks and orders under one limit, and retries under another" testNamesBoot
|
||||
|
||||
testNamesBoot :: HasCallStack => TestParams -> IO ()
|
||||
testNamesBoot ps = do
|
||||
@@ -163,6 +166,17 @@ testNamesBoot ps = do
|
||||
(statusCode $ responseStatus posted, lookup "Allow" $ responseHeaders posted) `shouldBe` (405, Just "GET")
|
||||
coded <- request "/api/registration/t2Jk8RmQ4vXw9NcP3yLb6g/code"
|
||||
answer coded {method = "POST", requestBody = RequestBodyLBS "{}"} `shouldReturn` (404, Just $ J.object ["error" J..= ("not_found" :: Text)])
|
||||
revealed <- request "/api/registration/t2Jk8RmQ4vXw9NcP3yLb6g/reveal"
|
||||
answer revealed {method = "POST", requestBody = RequestBodyLBS "{}"} `shouldReturn` (404, Just $ J.object ["error" J..= ("not_found" :: Text)])
|
||||
revealedByGet <- httpLbs revealed mgr
|
||||
(statusCode $ responseStatus revealedByGet, lookup "Allow" $ responseHeaders revealedByGet) `shouldBe` (405, Just "POST")
|
||||
retried <- request "/api/registration/t2Jk8RmQ4vXw9NcP3yLb6g/retry"
|
||||
answer retried {method = "POST"} `shouldReturn` (404, Just $ J.object ["error" J..= ("not_found" :: Text)])
|
||||
retriedByGet <- httpLbs retried mgr
|
||||
(statusCode $ responseStatus retriedByGet, lookup "Allow" $ responseHeaders retriedByGet) `shouldBe` (405, Just "POST")
|
||||
-- the retry above took 1 of this client's 5 create requests a minute
|
||||
replicateM_ 4 $ statusCode . responseStatus <$> httpLbs retried {method = "POST"} mgr `shouldReturn` 404
|
||||
statusCode . responseStatus <$> httpLbs retried {method = "POST"} mgr `shouldReturn` 429
|
||||
-- the register above took the first of this client's 10 code requests a minute, which the code check shares
|
||||
let codeRequest path = request path >>= \r -> statusCode . responseStatus <$> httpLbs r {method = "POST", requestBody = RequestBodyLBS "{}"} mgr
|
||||
forM_ (take 9 $ cycle ["/api/code/check", "/api/name/register"]) $ \path -> (path,) <$> codeRequest path `shouldReturn` (path, 400)
|
||||
@@ -194,6 +208,27 @@ testNamesBootCodesOnly ps = do
|
||||
codesOnlyNames :: [Text]
|
||||
codesOnlyNames = namesSection <> ["codes_only = on"]
|
||||
|
||||
namesWorkerBootTests :: SpecWith TestParams
|
||||
namesWorkerBootTests = it "starts the workers with the service, sending the commit of an order queued after the start" testWorkerBoot
|
||||
|
||||
testWorkerBoot :: HasCallStack => TestParams -> IO ()
|
||||
testWorkerBoot ps = withFakeEthNode $ \node@FakeEthNode {fnUrl} -> do
|
||||
svc <- prepareGroupService ps
|
||||
let onFakeNode line = if "rpc_url" `T.isPrefixOf` line then "rpc_url = " <> T.pack fnUrl else line
|
||||
controller = either error id $ strDecode $ encodeUtf8 controllerAddr
|
||||
-- the order's commitment, digestFixture 1, not yet committed
|
||||
forM_ [("minCommitmentAge()", "", 60 :: Int), ("maxCommitmentAge()", "", 86400), ("commitments(bytes32)", digestFixture 1, 0)] $ \(signature, args, value) ->
|
||||
answerCall node controller (selector signature <> args) $ Returns $ uint256 value
|
||||
(t, cc, _, port) <- startGroupService svc {gsIniSections = "" : namesSection <> map onFakeNode chainSection} Nothing
|
||||
flip finally (stopGroupService t cc) $ do
|
||||
pollUntilTrue (listening port)
|
||||
let st = chatStore cc
|
||||
code <- issueNameRaw cc "name 8"
|
||||
row <- fromMaybe (error "no prepaid code row") <$> withTransaction st (`getPrepaidCode` namePrepaidCodeHash code)
|
||||
placeCode st "r1" ownerA (order "alicebakery" 1) row `shouldReturn` Right ()
|
||||
timeout waitLimit (awaitSent node 1) >>= (`shouldSatisfy` isJust)
|
||||
poll t >>= (`shouldSatisfy` isNothing)
|
||||
|
||||
namesCodesChatTests :: SpecWith TestParams
|
||||
namesCodesChatTests = describe "StoreService names: prepaid codes" $ do
|
||||
it "issues prepaid codes on /issue name and /bulk name, answering the usage to years below 2" testGroupNameCodes
|
||||
|
||||
@@ -296,6 +296,7 @@ testNamesConfig node =
|
||||
nYears = 2,
|
||||
nTermStart = termStart,
|
||||
nTermSlackSeconds = slackSeconds,
|
||||
nRevealMarginSeconds = 900,
|
||||
nCodesOnly = False,
|
||||
nChain = fakeChainConfig node
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
module Bots.StoreService.NamesOwnerTests
|
||||
( answerExpiry,
|
||||
answerFree,
|
||||
answerOwner,
|
||||
answerUnowned,
|
||||
byB,
|
||||
codeIn,
|
||||
committedPhase,
|
||||
dynamisOrder,
|
||||
failedPhase,
|
||||
labelBytes,
|
||||
ownerOfSelector,
|
||||
paidIn,
|
||||
rewrite,
|
||||
sentRegister,
|
||||
stored,
|
||||
)
|
||||
where
|
||||
|
||||
import Bots.StoreService.FakeEthNode
|
||||
import Bots.StoreService.NamesContractsTests (registrarOf, unhex)
|
||||
import Bots.StoreService.NamesOrdersTests
|
||||
import Bots.StoreService.WebTests (digestFixture)
|
||||
import Control.Monad (void)
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.IORef (readIORef)
|
||||
import Data.Maybe (fromJust)
|
||||
import Data.Text (Text)
|
||||
import Data.Time.Clock (UTCTime)
|
||||
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
|
||||
import Data.Word (Word64, Word8)
|
||||
import Simplex.Chat.PaymentService.Types (InvoiceStatus (..))
|
||||
import Simplex.Messaging.Agent.Store.Common (withTransaction)
|
||||
import Simplex.Messaging.Eth.Address (Address)
|
||||
import Simplex.Messaging.SimplexName (LabelHash (..), labelHash)
|
||||
import StoreService.Names.Abi (addressWord, uint256)
|
||||
import StoreService.Names.Chain (TxHash (..))
|
||||
import StoreService.Names.Store hiding (findLimit)
|
||||
import Test.Hspec
|
||||
|
||||
seconds :: UTCTime -> Word64
|
||||
seconds = floor . utcTimeToPOSIXSeconds
|
||||
|
||||
byB :: Order -> Order
|
||||
byB o = o {oOwner = ownerBText, oKey = keyB}
|
||||
|
||||
dynamisOrder :: Order
|
||||
dynamisOrder = aliceOrder {oLabel = "dynamis", oCommitment = commitmentOf 3, oSealed = "sealed-dynamis-3", oPrice = 20000}
|
||||
|
||||
-- viem's selectors of nameExpires(uint256), reservedNames(bytes32) and ownerOf(uint256), as in the calls of the contracts tests
|
||||
nameExpiresSelector, reservedNamesSelector, ownerOfSelector :: ByteString
|
||||
nameExpiresSelector = unhex "d6e4fa86"
|
||||
reservedNamesSelector = unhex "389c0748"
|
||||
ownerOfSelector = unhex "6352211e"
|
||||
|
||||
labelBytes :: Text -> ByteString
|
||||
labelBytes label = lh
|
||||
where
|
||||
LabelHash lh = labelHash label
|
||||
|
||||
-- | ownerOf of a name nobody holds: the registrar's bare require, a revert without data.
|
||||
answerUnowned :: Ctx -> Text -> IO ()
|
||||
answerUnowned Ctx {cNode} label = answerCall cNode (registrarOf cNode) (ownerOfSelector <> labelBytes label) $ Reverts ""
|
||||
|
||||
answerOwner :: Ctx -> Text -> Address -> IO ()
|
||||
answerOwner Ctx {cNode} label owner = answerCall cNode (registrarOf cNode) (ownerOfSelector <> labelBytes label) $ Returns $ addressWord owner
|
||||
|
||||
answerExpiry :: Ctx -> Text -> Word64 -> IO ()
|
||||
answerExpiry Ctx {cNode} label expiry = answerCall cNode (registrarOf cNode) (nameExpiresSelector <> labelBytes label) $ Returns $ uint256 expiry
|
||||
|
||||
-- | A register transaction of the order as the sender saves it, then moved to this outcome.
|
||||
sentRegister :: Ctx -> NameRegistration -> Word8 -> TxStatus -> IO TxHash
|
||||
sentRegister Ctx {cStore} NameRegistration {nrId} n status = withTransaction cStore $ \db -> do
|
||||
let h = TxHash $ digestFixture n
|
||||
void $ insertTransaction db (TxRegister nrId) NewTransaction {ntxRaw = "raw", ntxHash = h, ntxNonce = fromIntegral n} orderTime
|
||||
recordTransactionOutcome db h TSPending status (Just 120) `shouldReturn` True
|
||||
pure h
|
||||
|
||||
-- | The chain answers the name as available and held by nobody.
|
||||
answerFree :: Ctx -> Text -> IO ()
|
||||
answerFree ctx label = answerReserved ctx label 0 >> answerUnowned ctx label
|
||||
|
||||
answerReserved :: Ctx -> Text -> Int -> IO ()
|
||||
answerReserved Ctx {cNode} label reason = do
|
||||
answerCall cNode (registrarOf cNode) (nameExpiresSelector <> lh) $ Returns $ uint256 (0 :: Int)
|
||||
answerCall cNode fakeController (reservedNamesSelector <> lh) $ Returns $ uint256 reason
|
||||
where
|
||||
LabelHash lh = labelHash label
|
||||
|
||||
-- | A paid order, settled, then written as the worker would write it.
|
||||
paidIn :: Ctx -> Order -> (NameRegistration -> NameRegistration) -> IO NameRegistration
|
||||
paidIn ctx o change = do
|
||||
(invId, _) <- placed ctx o
|
||||
settleNow ctx invId settled `shouldReturn` Right ISPaid
|
||||
r <- invoiceRegistration ctx invId
|
||||
rewrite ctx r change
|
||||
|
||||
-- | A code order of a code of this length, then written as the worker would write it.
|
||||
codeIn :: Ctx -> Int -> Order -> (NameRegistration -> NameRegistration) -> IO (NameRegistration, PrepaidCode)
|
||||
codeIn ctx len o change = do
|
||||
(text, code) <- issueCode ctx len 2 id
|
||||
r <- registrationById ctx =<< registered ctx text o
|
||||
(,code) <$> rewrite ctx r change
|
||||
|
||||
-- | A write outside the turns, as the worker's, at the stored version.
|
||||
rewrite :: Ctx -> NameRegistration -> (NameRegistration -> NameRegistration) -> IO NameRegistration
|
||||
rewrite Ctx {cStore, cClock} NameRegistration {nrId} change = do
|
||||
t <- readIORef cClock
|
||||
withTransaction cStore $ \db -> do
|
||||
current <- fromJust <$> getRegistration db nrId
|
||||
updateRegistration db (change current) t >>= \case
|
||||
Written _ -> fromJust <$> getRegistration db nrId
|
||||
refused -> error $ "not written: " <> show refused
|
||||
|
||||
stored :: Ctx -> NameRegistration -> IO NameRegistration
|
||||
stored ctx = registrationById ctx . nrId
|
||||
|
||||
failedPhase, committedPhase :: NameRegistration -> NameRegistration
|
||||
failedPhase r = r {nrPhase = PFailed, nrFailedAt = Just orderTime, nrError = Just "The name could not be registered"}
|
||||
committedPhase r =
|
||||
r
|
||||
{ nrPhase = PCommitted,
|
||||
nrCommitTx = Just (TxHash $ digestFixture 8),
|
||||
nrCommitBlock = Just 100,
|
||||
nrRevealAfter = Just (seconds orderTime + 60),
|
||||
nrRevealBefore = Just (seconds orderTime + 86400)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user