store: register names across a commitment lapse without signing again (#8145)

* store: register names across a commitment lapse without signing again

* store: count every commit toward Try again and moves

* store: update the names webapp submodule

* store: update the names webapp submodule

---------

Co-authored-by: sh-agents <350651734+sh-agents@users.noreply.github.com>
This commit is contained in:
sh-agentsandsh-agents authored and GitHub committed 2026-11-13 13:55:33 +00:00
1 parent 4c4a2aaaee
commit 5becf05d6e
7 files changed
+134 -65

No files matched your search

+11 -5
View File
@@ -965,7 +965,10 @@ reveal's deadline, and the service relays it with `commitUnderGeneralTermsWithSi
register refused for the acceptance (new terms, a used nonce), an acceptance of the other kind of
terms, or one signed for the name policy before a `setNamePolicy`, clears the reveal, and the page
signs again. A relay that reverts while the policy holds that acceptance for the commitment, as when
another sent it first, is followed by the register.
another sent it first, is followed by the register. The page signs the reveal and the acceptance
with a deadline a week ahead, never before the commitment lapses, and the service keeps them across a
lapse while the deadline has not passed and the sponsor nonce is unchanged: it commits again and
registers with no new signature.
### Codes-only launch
@@ -1238,9 +1241,10 @@ psql "postgresql://simplex@127.0.0.1:5432/badge_service" -c "SELECT registrar_no
key is enough and needs no restart. The line is logged again only when the node's message changes.
- `[ERROR …] names worker: registration <id> waits: the registrar's allowance on the sponsor is
used up; the guardian raises it with setRegistrarAllowance` when the sponsor refuses a register
with `InsufficientAllowance`. The order keeps its phase and its register is tried again every 15
seconds, so a top-up before its commitment lapses registers it with no restart. The line is
logged once per order. An order whose commitment lapses first fails, and the buyer can Try again.
with `InsufficientAllowance`. The order keeps its phase and its register's dry run is tried again
every 15 seconds, also past its commitment's lapse, so a top-up registers it with no restart, the
commitment sent again first when it lapsed. The line is logged once per order. An order whose
signature expires first, a week after the page signed it, fails, and the buyer can Try again.
This holds when the register's dry run is refused. A register that passes its dry run and reverts
once mined, as two registers sent while the allowance covers one can, fails its order with no
such line; the next order's dry run logs it, and the buyer can Try again.
@@ -1255,7 +1259,9 @@ psql "postgresql://simplex@127.0.0.1:5432/badge_service" -c "SELECT registrar_no
An order sends at most 5 commits, 5 terms acceptances and 5 registers, its moves included, since an
owner who changes their terms acceptance on chain can make each acceptance and register revert at the
service's gas. Past that the order fails, Try again and a move answer `400 bad_request`, and support closes it
service's gas. The service's own commits after a lapse, which wait for it, count against that only when
they revert, so a signature kept for its week registers over several commitment windows; Try again and
a move count every commit. Past that the order fails, Try again and a move answer `400 bad_request`, and support closes it
with `//close` (see [Closing a failed order](#closing-a-failed-order)).
The log line is `[WARN …] names worker: registration <id> failed: it sent <n> transactions of one kind`.
+8 -5
View File
@@ -231,7 +231,7 @@ sequenceDiagram
Notes:
- Every transaction here is sent through the sender as the commit is, with value 0 and its gas paid by the registrar key; each kind is sent at most 5 times per order, moves included ([`Names/Chain.hs:199-214`](../src/StoreService/Names/Chain.hs#L199-L214), [`Names/Worker.hs:428-449`](../src/StoreService/Names/Worker.hs#L428-L449)). By the code comments and the log line of an `InsufficientAllowance` revert, the rent is spent from the registrar key's allowance on the sponsor ([`Names/Contracts.hs:179-181`](../src/StoreService/Names/Contracts.hs#L179-L181), [`Names/Worker.hs:341`](../src/StoreService/Names/Worker.hs#L341)).
- Every transaction here is sent through the sender as the commit is, with value 0 and its gas paid by the registrar key; each kind is sent at most 5 times per order, moves included; the worker's own sends do not count a commit mined with status 1, while Try again and a move count every commit ([`Names/Chain.hs:199-214`](../src/StoreService/Names/Chain.hs#L199-L214), [`Names/Worker.hs:443-468`](../src/StoreService/Names/Worker.hs#L443-L468)). By the code comments and the log line of an `InsufficientAllowance` revert, the rent is spent from the registrar key's allowance on the sponsor ([`Names/Contracts.hs:179-181`](../src/StoreService/Names/Contracts.hs#L179-L181), [`Names/Worker.hs:351`](../src/StoreService/Names/Worker.hs#L351)).
- `RegisterName` is signed in the EIP-712 domain `SimplexSponsor`, the acceptance in `SimplexNamePolicy`, as `AcceptGeneralTerms` or `AcceptCommunityTerms` ([`Names/Registration.hs:80-111`](../src/StoreService/Names/Registration.hs#L80-L111)).
- The reveal is checked in the contract's order: the label, its hash and length, at most one link per key, the commitment rebuilt from the stored owner, duration and default resolver; the intent's deadline not before `revealBefore`, its signer and the sponsor nonce; the acceptance against the current terms and policy nonce; the links' kinds last ([`Names/Reveal.hs:103-145`](../src/StoreService/Names/Reveal.hs#L103-L145)).
- A reveal whose link is not a short link of its record's kind is answered `400 invalid_link`, and the order is marked `linksRefused`. The links are part of the commitment, so only a move (3.6) continues the order ([`Web/Names/Registration.hs:95-109`](../src/StoreService/Web/Names/Registration.hs#L95-L109), [`Names/Orders.hs:315-320`](../src/StoreService/Names/Orders.hs#L315-L320)).
@@ -334,7 +334,7 @@ Notes:
### 3.5 A register transaction that fails
After a register transaction reverts, the order is judged from chain state: its reveal is cleared for the page to sign again, it waits, it is set `taken` with its price kept as a balance, or it is set `failed`.
After a register transaction reverts, the order is judged from chain state: its reveal is cleared for the page to sign again, it waits, it is committed again with its reveal, it is set `taken` with its price kept as a balance, or it is set `failed`.
```mermaid
sequenceDiagram
@@ -349,6 +349,8 @@ sequenceDiagram
K->>D: reveal cleared, phase stays committed, the page signs again
else InsufficientAllowance
K->>K: error logged, retried every 15 s
else CommitmentTooOld from the dry run
K->>D: phase committing, reveal kept, then committed again and registered with it
else name taken at the latest block and at the confirmation depth
K->>D: phase taken, a paid order's price kept as its balance
else name available
@@ -359,7 +361,8 @@ sequenceDiagram
Notes:
- Whether the name was registered after all is checked first: on a register mined with status 1, or with the name held by the owner at the confirmation depth, the order is set `registered` ([`Names/Orders.hs:365-400`](../src/StoreService/Names/Orders.hs#L365-L400), [`Names/Worker.hs:329-336`](../src/StoreService/Names/Worker.hs#L329-L336)).
- An `InsufficientAllowance` revert waits for the registrar key's allowance on the sponsor to be raised outside the service ([`Names/Worker.hs:341`](../src/StoreService/Names/Worker.hs#L341)).
- An `InsufficientAllowance` revert waits for the registrar key's allowance on the sponsor to be raised outside the service, also past the commitment's lapse, as the sponsor checks the allowance before the commitment. The order fails once its intent's deadline passes ([`Names/Worker.hs:351`](../src/StoreService/Names/Worker.hs#L351), [`Names/Worker.hs:303-318`](../src/StoreService/Names/Worker.hs#L303-L318)).
- `CommitmentTooOld` is reverted only after the intent's and the allowance's checks. The order is set `committing` with its reveal kept, or, while `revealBefore` has not passed by the service's clock, the dry run is tried again a second later; the commit is sent again, and the register follows once it has aged, with no new signature. The names page signs the intent and the acceptance with a deadline a week ahead, never before `revealBefore` ([`Names/Worker.hs:348-350`](../src/StoreService/Names/Worker.hs#L348-L350), [`Names/Worker.hs:201-212`](../src/StoreService/Names/Worker.hs#L201-L212)).
- On `CommitmentTooNew` the register is retried 1 s later. While the name is taken only above the confirmation depth, or is reported gone by the dry run while `available(label)` is true, the order is retried every 15 s ([`Names/Worker.hs:340-352`](../src/StoreService/Names/Worker.hs#L340-L352)).
- A code order set `taken` has its code released instead of a balance credited ([`Names/Worker.hs:361-368`](../src/StoreService/Names/Worker.hs#L361-L368)).
@@ -377,7 +380,7 @@ sequenceDiagram
participant D as PostgreSQL
alt Try again
P->>W: POST /api/registration/ID/retry
W->>E: registered after all? reveal valid for the nonce and window?
W->>E: registered after all? reveal valid for the nonce and deadline?
W->>D: phase queued, failure cleared, reveal kept or cleared
W-->>P: 200 the order's view: queued, or registered after all
else another name
@@ -392,7 +395,7 @@ sequenceDiagram
Notes:
- Try again is sent with the registration id alone. It is refused for an order whose links were refused, one with 5 sends of a kind, and a code order whose code no longer pays. The reveal is kept while the sponsor nonce is unchanged and the commitment has `reveal_margin_seconds` (default 900) left within the intent's deadline ([`Web/Names/Registration.hs:116-140`](../src/StoreService/Web/Names/Registration.hs#L116-L140), [`Names/Worker.hs:411-501`](../src/StoreService/Names/Worker.hs#L411-L501), [`Config.hs:654`](../src/StoreService/Config.hs#L654)). At the next scan a commitment with that margin left is reused; otherwise a new commit is sent, a second past the old commitment's lapse when one is on chain ([`Names/Worker.hs:175-206`](../src/StoreService/Names/Worker.hs#L175-L206)).
- Try again is sent with the registration id alone. It is refused for an order whose links were refused, one with 5 sends of a kind, and a code order whose code no longer pays. The reveal is kept while the sponsor nonce is unchanged and the intent's deadline is `reveal_margin_seconds` (default 900) or more away ([`Web/Names/Registration.hs:116-140`](../src/StoreService/Web/Names/Registration.hs#L116-L140), [`Names/Worker.hs:424-508`](../src/StoreService/Names/Worker.hs#L424-L508), [`Config.hs:673`](../src/StoreService/Config.hs#L673)). At the next scan a commitment with that margin left is reused; otherwise a new commit is sent, a second past the old commitment's lapse when one is on chain, and a kept reveal registers after it ([`Names/Worker.hs:171-212`](../src/StoreService/Names/Worker.hs#L171-L212)).
- A move is accepted for a failed order, or for one whose links were refused, up to `max_order_moves` (default 3). For a failed order the service checks first whether its name was registered after all, then refuses a move after 5 sends of a kind ([`Web/Names/Owner.hs:113-126`](../src/StoreService/Web/Names/Owner.hs#L113-L126), [`Config.hs:655`](../src/StoreService/Config.hs#L655)).
- Both names' locks are taken in hash order, and the first order's hash is kept. A paid order moved to a cheaper name is credited the difference; a code order is moved at price 0, its code reserved again ([`Names/Turns.hs:27-32`](../src/StoreService/Names/Turns.hs#L27-L32), [`Web/Names/Owner.hs:128-135`](../src/StoreService/Web/Names/Owner.hs#L128-L135), [`Names/Orders.hs:340-356`](../src/StoreService/Names/Orders.hs#L340-L356), [`Names/Store.hs:787-813`](../src/StoreService/Names/Store.hs#L787-L813)).
- A paid order's move reads the new name's price as an order does: a price under `min_price_cents_per_year` times the paid years is answered `503 name_check_unavailable`, and nothing is credited ([`Names/Contracts.hs:190-202`](../src/StoreService/Names/Contracts.hs#L190-L202)).
@@ -194,16 +194,15 @@ fromCommitment w@Worker {wkOrders = OrdersEnv {oeConfig}} now r ages t
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.
-- | A live commitment, or a stored intent, is reused while revealMargin is left before its end, 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
reusable NamesConfig {nRevealMarginSeconds} now end = end - 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.
-- | The old commitment's transactions and reveal times; the reveal stays for the next commitment.
lapsedFields :: NameRegistration -> NameRegistration
lapsedFields r =
r
{ nrReveal = Nothing,
nrError = Nothing,
{ nrError = Nothing,
nrCommitTx = Nothing,
nrCommitBlock = Nothing,
nrRegisterTx = Nothing,
@@ -213,7 +212,7 @@ lapsedFields r =
}
-- | 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.
-- someone committed it again after a lapse, so our commit is cleared.
toCommitted :: Worker -> UTCTime -> NameRegistration -> CommitmentAges -> Word64 -> IO Next
toCommitted w now r CommitmentAges {caMin, caMax} t =
ourCommit w r t >>= \case
@@ -223,7 +222,7 @@ toCommitted w now r CommitmentAges {caMin, caMax} t =
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}
Nothing -> r {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)))
@@ -251,7 +250,7 @@ commitSent w now r@NameRegistration {nrPhase, nrCommitTx} p
-- | Waits for the page's reveal until the lapse, has its acceptance held by the name policy, waits for revealAfter, then registers.
committed :: Worker -> UTCTime -> NameRegistration -> IO Next
committed w@Worker {wkSender} now r@NameRegistration {nrId, nrReveal, nrRevealAfter, nrRevealBefore} =
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
@@ -259,15 +258,15 @@ committed w@Worker {wkSender} now r@NameRegistration {nrId, nrReveal, nrRevealAf
Just lapse | now <= lapse -> pure $ waitUntil r $ addUTCTime chainSecond lapse
_ -> save w now r {nrPhase = PFailed, nrFailedAt = Just now, nrError = Just notSignedInTime}
Just rv@NameReveal {rvAcceptance, rvDeadline} ->
(pure (if unlapsed then rvAcceptance else Nothing) $>>= relayAcceptance w now r rvDeadline) >>= \case
-- past the commitment's lapse no acceptance is relayed, as the policy would commit again; the register's dry run then decides
(pure (if unlapsed now r then rvAcceptance else Nothing) $>>= relayAcceptance w now r rvDeadline) >>= \case
Just next -> pure next
Nothing -> case secondsTime . toInteger <$> nrRevealAfter of
Just revealAfter | now < revealAfter -> pure $ waitUntil r revealAfter
_ -> sendRegister w now r rv
where
-- past the commitment's lapse no acceptance is relayed, as the policy would commit again; the register's dry run then
-- fails the order as it does a plain name's
unlapsed = all ((now <=) . secondsTime . toInteger) nrRevealBefore
unlapsed :: UTCTime -> NameRegistration -> Bool
unlapsed now NameRegistration {nrRevealBefore} = all ((now <=) . secondsTime . toInteger) nrRevealBefore
-- | The relay of the reveal's acceptance to the name policy, sent without waiting for revealAfter; Nothing once the policy
-- stores that acceptance. One signed for another policy, before a setNamePolicy, or for the other kind of terms, clears the
@@ -304,7 +303,7 @@ relayAcceptance w@Worker {wkOrders = OrdersEnv {oeContracts, oeConfig = NamesCon
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
-- past its deadline the intent can no longer register; 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
@@ -331,7 +330,8 @@ registering w@Worker {wkSender} now r@NameRegistration {nrId} =
-- | 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 or a refused terms acceptance, for the page to sign again, the name
-- policy read again on TermsNotAccepted; kept waiting while the sponsor allowance is used up; taken on any revert while the name is not
-- policy read again on TermsNotAccepted; committing again with its reveal on CommitmentTooOld, which the sponsor reverts only after the
-- intent's and the allowance's checks; kept waiting while the sponsor allowance is used up; taken on any revert while the name is not
-- available at the confirmation depth; 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 =
@@ -345,6 +345,9 @@ judge w@Worker {wkOrders = oe@OrdersEnv {oeContracts}} now r@NameRegistration {n
when (e == RNTermsNotAccepted) $ forgetNamePolicy oeContracts
store w now r {nrReveal = Nothing}
Just RNCommitmentTooNew -> pure (Nothing, waitUntil r $ addUTCTime chainSecond now)
Just RNCommitmentTooOld
| unlapsed now r -> pure (Nothing, waitUntil r $ addUTCTime chainSecond now)
| otherwise -> store w now r {nrPhase = PCommitting}
Just RNInsufficientAllowance -> pure (Nothing, Stalled (nrVersion r) "the registrar's allowance on the sponsor is used up; the guardian raises it with setRegistrarAllowance")
_ -> case nrReveal of
Just NameReveal {rvLabel} ->
@@ -445,7 +448,9 @@ maxSendsPerKind = 5
-- | The send while the order has sent fewer than maxSendsPerKind transactions of its kind, else the order fails.
withinSends :: Worker -> UTCTime -> NameRegistration -> (TxFor -> Bool) -> IO Next -> IO Next
withinSends w@Worker {wkOrders} now r@NameRegistration {nrId = RegistrationId rid} ofKind send = do
sent <- sendsOf wkOrders r ofKind
txs <- orderTransactions wkOrders r
let minedCommits = [ntNonce | NameTransaction {ntFor = TxCommit _, ntNonce, ntStatus = TSMined} <- txs]
sent = sendCount ofKind [tx | tx@NameTransaction {ntNonce} <- txs, ntNonce `notElem` minedCommits]
if sent < maxSendsPerKind
then send
else do
@@ -453,12 +458,14 @@ withinSends w@Worker {wkOrders} now r@NameRegistration {nrId = RegistrationId ri
save w now r {nrPhase = PFailed, nrFailedAt = Just now, nrError = Just notRegistered}
sendsUsedUp :: OrdersEnv -> NameRegistration -> IO Bool
sendsUsedUp oe r = any (>= maxSendsPerKind) <$> mapM (sendsOf oe r) [isCommit, isAccept, isRegister]
sendsUsedUp oe r = orderTransactions oe r <&> \txs -> any (\ofKind -> sendCount ofKind txs >= maxSendsPerKind) [isCommit, isAccept, isRegister]
-- | One per nonce, as a fee replacement is the same send.
sendsOf :: OrdersEnv -> NameRegistration -> (TxFor -> Bool) -> IO Int
sendsOf OrdersEnv {oeStore} NameRegistration {nrId} ofKind =
length . nubOrd . map ntNonce . filter (ofKind . ntFor) <$> withTransaction oeStore (`registrationTransactions` nrId)
sendCount :: (TxFor -> Bool) -> [NameTransaction] -> Int
sendCount ofKind = length . nubOrd . map ntNonce . filter (ofKind . ntFor)
orderTransactions :: OrdersEnv -> NameRegistration -> IO [NameTransaction]
orderTransactions OrdersEnv {oeStore} NameRegistration {nrId} = withTransaction oeStore (`registrationTransactions` nrId)
isCommit, isAccept, isRegister :: TxFor -> Bool
isCommit = \case TxCommit _ -> True; _ -> False
@@ -492,25 +499,15 @@ requeue oe@OrdersEnv {oeStore, oeTurns} now r@NameRegistration {nrId, nrLength}
-- 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.
-- | The stored reveal while its intent can still register: accepted by the worker's check before sending, revealMargin left
-- before its deadline, and the sponsor nonce unmoved.
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
servingReveal OrdersEnv {oeConfig = cfg@NamesConfig {nChain = ChainConfig {cChainId, cSponsor}}, oeContracts} now r@NameRegistration {nrOwner, 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
Right () | reusable cfg now (toInteger rvDeadline) -> fmap (\nonce -> if nonce == rvNonce then Just rv else Nothing) <$> sponsorNonce oeContracts nrOwner
_ -> pure $ Right Nothing
data Closed = CLRegistered RegistrationId | CLRefunded (Maybe UTCTime) RefundRecord | CLRefused CloseRefusal
deriving (Eq, Show)
@@ -110,7 +110,7 @@ idle_seconds = 60
;term_start = 2027-01-01T00:00:00Z
; how far an order's duration may be from the one the service computes
;term_slack_seconds = 3600
; a commitment with less than this left before its reveal deadline is not reused
; a commitment, or a signed reveal on Try again, with less than this left before its deadline is not reused
;reveal_margin_seconds = 900
; how many times one order may move to another name
;max_order_moves = 3
@@ -96,7 +96,7 @@ private_key = replace-me
;term_start = 2027-01-01T00:00:00Z
; how far an order's duration may be from the one the service computes
;term_slack_seconds = 3600
; a commitment with less than this left before its reveal deadline is not reused
; a commitment, or a signed reveal on Try again, with less than this left before its deadline is not reused
;reveal_margin_seconds = 900
; how many times one order may move to another name
;max_order_moves = 3
+80 -17
View File
@@ -12,7 +12,7 @@ import Bots.StoreService.NamesOrdersTests
import Bots.StoreService.NamesOwnerTests (answerExpiry, answerFree, answerOwner, answerUnowned, byB, codeIn, committedPhase, dynamisOrder, failedPhase, labelBytes, move, moveTo, ownerOfSelector, paidIn, rewrite, sentRegister, stored)
import Bots.StoreService.NamesPhasesTests (held, prompt, registrationPath, woken)
import Bots.StoreService.NamesStoreTests (btc, insertInvoiceRow, ownerA, paidOrder, reveal)
import Bots.StoreService.WebTests (capturingStderr, jsonObject, statusOf, stubCalls, webGet, withApp)
import Bots.StoreService.WebTests (capturingStderr, digestFixture, jsonObject, statusOf, stubCalls, webGet, withApp)
import Control.Applicative ((<|>))
import Control.Arrow ((&&&))
import Control.Concurrent (threadDelay)
@@ -31,7 +31,7 @@ import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
import Data.Word (Word64)
import Data.Word (Word8, Word64)
import Network.HTTP.Client (responseBody)
import Simplex.Chat.PaymentService.Types (InvoiceId (..), InvoiceStatus (..))
import Simplex.Messaging.Agent.Store.Common (withTransaction)
@@ -81,8 +81,9 @@ workerTests = describe "StoreService names: worker" $ do
it "goes to committed when the commit reverts UnexpiredCommitmentExists, in its dry run or mined, with the commitment's times" testUnexpiredCommitment
it "sends a commit mined out of gas again" testCommitOutOfGas
it "waits a second at the lapse second, retries as usual when the dry run still reverts, then commits" testLapseSecond
it "clears a requeued order's stale intent before committing again after a lapse, and a third party's commitment's" testStaleIntent
it "keeps a requeued order's intent while it commits again after a lapse, and over a third party's commitment" testIntentAcrossCommits
it "keeps the phase while the latest block needs more than the fee cap, then commits" testFeeAboveCap
it "commits again after commits mined with status 1 on every lapse while Try again counts them, and fails an order whose commits reverted the most times an order may" testMinedCommitsUncounted
describe "writes" $ do
it "writes nothing when the registration changed since it was read" testLostRace
it "writes in the name's turn" testInNameTurn
@@ -93,6 +94,7 @@ workerTests = describe "StoreService names: worker" $ do
it "keeps the phase on NameNotAvailable while the name still reads available, as for a pending competitor" testPendingCompetitor
it "ends failed on a revert while the name is available, keeping a code" testRevertFailed
it "keeps an order committed while the sponsor allowance is used up, and registers it after a top-up" testAllowanceUsedUp
it "keeps an order waiting on the allowance past its commitment's lapse, sending nothing, then commits again and registers with its stored intent" testAllowanceAcrossLapse
it "fails an order that sent the most registers an order may, sending nothing, refuses its Try again and a move, and registers it on a move once its owner holds the name" testSendsCapped
it "clears the reveal on InvalidNonce or TermsNotAccepted, staying committed, and reads the name policy again on TermsNotAccepted" testInvalidNonce
it "relays no intent past its deadline, ending failed" testExpiredIntent
@@ -128,10 +130,11 @@ workerTests = describe "StoreService names: worker" $ do
it "answers a released code's error while it cannot pay again, before any read for the intent, and reserves it again while unused" testRetryReleasedCode
it "requeues clearing failed_at, error and the register's hashes, waking a held poll" testRetryRequeue
it "keeps the intent while our commitment is live with revealMargin left, going straight to committed" testRetryLiveCommitment
it "clears an intent whose deadline ends before our live commitment lapses" testRetryShortDeadline
it "clears an intent with less than revealMargin left before its deadline, while our live commitment has more" testRetryShortDeadline
it "keeps an intent within its deadline after our commitment lapsed, committing again with it" testRetryLapsedIntent
it "clears the label and intent when the sponsor nonce moved, our live commit keeping its hashes" testRetryNonceMoved
it "clears the label and an intent the worker would refuse, while our commitment is live with revealMargin left" testRetryRefusedIntent
it "clears the intent of a third party's live re-commit, and the worker the commit's hashes" testRetryThirdParty
it "clears an intent past its deadline at a third party's live re-commit, and the worker the commit's hashes" testRetryThirdParty
it "clears the intent with less than revealMargin left, committing again a second after revealBefore" testRetryMargin
it "requeues a lapsed committed order, which reads failed, answering an old version at once after it" testRetryLapsed
it "answers registration_changed with the current view when the order changed during its checks, writing nothing" testRetryLostRace
@@ -449,21 +452,21 @@ testLapseSecond = withWorker $ \w@W {wCtx = ctx} -> do
nrPhase <$> stored ctx r `shouldReturn` PCommitting
length <$> sent w `shouldReturn` 1
testStaleIntent :: IO ()
testStaleIntent = withWorker $ \w@W {wCtx = ctx} -> do
testIntentAcrossCommits :: IO ()
testIntentAcrossCommits = withWorker $ \w@W {wCtx = ctx} -> do
-- requeued by Try again with its intent, its commitment lapsed
r <- paidIn ctx aliceOrder $ \o -> (openPhase o) {nrPhase = PQueued, nrReveal = Just reveal}
answerCommitment w r (orderSeconds - maxAge - 1)
void $ steps w r >>= awaiting
x <- stored ctx r
commitTx <- newestHash w
(nrPhase x, nrReveal x, nrRevealAfter x, nrRevealBefore x, nrCommitTx x, nrVersion x) `shouldBe` (PCommitting, Nothing, Nothing, Nothing, Just commitTx, nrVersion r + 2)
-- requeued with its intent while a third party's commit of the commitment is live: not ours, so the intent goes
(nrPhase x, nrReveal x, nrRevealAfter x, nrRevealBefore x, nrCommitTx x, nrVersion x) `shouldBe` (PCommitting, Just reveal, Nothing, Nothing, Just commitTx, nrVersion r + 2)
-- requeued with its intent while a third party's commit of the commitment is live: not ours, so only our commit goes
r2 <- paidIn ctx (byB aliceOrder) {oCommitment = commitmentOf 2} $ \o -> (openPhase o) {nrPhase = PQueued, nrReveal = Just reveal}
answerCommitment w r2 (orderSeconds - 60)
steps w r2 >>= waitingUntil >>= (`shouldBe` at (orderSeconds - 60 + maxAge + 1))
answerCommitment w r2 (orderSeconds - 30)
steps w r2 >>= waitingUntil >>= (`shouldBe` at (orderSeconds - 30 + minAge))
x2 <- stored ctx r2
(nrPhase x2, nrReveal x2, nrCommitTx x2, nrRevealBefore x2) `shouldBe` (PCommitted, Nothing, Nothing, Just (orderSeconds - 60 + maxAge))
(nrPhase x2, nrReveal x2, nrCommitTx x2, nrRevealBefore x2) `shouldBe` (PCommitted, Just reveal, Nothing, Just (orderSeconds - 30 + maxAge))
length <$> sent w `shouldReturn` 1
testFeeAboveCap :: IO ()
@@ -478,6 +481,28 @@ testFeeAboveCap = withWorker $ \w@W {wCtx = ctx@Ctx {cNode}} -> do
void $ steps w r >>= awaiting
nrPhase <$> stored ctx r `shouldReturn` PCommitting
sentCommits :: Ctx -> NameRegistration -> TxStatus -> [Word8] -> IO ()
sentCommits Ctx {cStore} NameRegistration {nrId} status nonces = withTransaction cStore $ \db -> forM_ nonces $ \n -> do
let h = TxHash $ digestFixture n
void $ insertTransaction db (TxCommit nrId) NewTransaction {ntxRaw = "raw", ntxHash = h, ntxNonce = fromIntegral n} orderTime
recordTransactionOutcome db h TSPending status (Just 120) `shouldReturn` True
testMinedCommitsUncounted :: IO ()
testMinedCommitsUncounted = withWorker $ \w@W {wCtx = ctx@Ctx {cOrders}} -> do
r <- paidIn ctx aliceOrder id
sentCommits ctx r TSMined [1 .. fromIntegral maxSendsPerKind]
sendsUsedUp cOrders r `shouldReturn` True
answerCommitment w r 0
void $ steps w r >>= awaiting
nrPhase <$> stored ctx r `shouldReturn` PCommitting
r2 <- paidIn ctx (byB aliceOrder) {oCommitment = commitmentOf 2} id
sentCommits ctx r2 TSReverted [11 .. 10 + fromIntegral maxSendsPerKind]
answerCommitment w r2 0
steps w r2 >>= done
x2 <- stored ctx r2
(nrPhase x2, nrError x2) `shouldBe` (PFailed, Just "The name could not be registered")
length <$> sent w `shouldReturn` 1
testLostRace :: IO ()
testLostRace = withWorker $ \w@W {wCtx = ctx@Ctx {cNode}, wWorker} -> do
r <- paidIn ctx aliceOrder openPhase >>= revealed ctx keyA "alicebakery"
@@ -678,6 +703,33 @@ testAllowanceUsedUp = withWorker $ \w@W {wCtx = ctx@Ctx {cNode}} -> do
void $ steps w r >>= awaiting
length <$> sent w `shouldReturn` 1
testAllowanceAcrossLapse :: IO ()
testAllowanceAcrossLapse = withWorker $ \w@W {wCtx = ctx@Ctx {cNode, cOrders}} -> do
let lapse = orderSeconds + 10
r <- paidIn ctx aliceOrder (\o -> (openPhase o) {nrRevealBefore = Just lapse}) >>= revealed ctx keyA "alicebakery"
rv <- maybe (fail "no reveal") pure $ nrReveal r
let registerCall = registerWithCreditCalldata $ intentOf (oeConfig cOrders) r rv
answerAvailable w True
answerCommitment w r (lapse - maxAge)
revertDryRun cNode registerCall $ Just $ selector "InsufficientAllowance(uint256,uint256)"
void $ steps w r >>= stalled
advance ctx 11
void $ steps w r >>= stalled
stored ctx r `shouldReturn` r
revertDryRun cNode registerCall $ Just $ selector "CommitmentTooOld(bytes32,uint256,uint256)"
void $ steps w r >>= awaiting
committing <- stored ctx r
(nrPhase committing, nrReveal committing, nrRevealBefore committing) `shouldBe` (PCommitting, Just rv, Nothing)
t <- mineNewest w True >>= blockTime w
answerCommitment w r t
revertDryRun cNode registerCall Nothing
steps w r >>= waitingUntil >>= (`shouldBe` at (t + minAge))
advance ctx $ fromIntegral (t + minAge - lapse - 1)
void $ steps w r >>= awaiting
x <- stored ctx r
(nrPhase x, nrReveal x, nrRevealBefore x) `shouldBe` (PRegistering, Just rv, Just (t + maxAge))
length <$> sent w `shouldReturn` 2
testSendsCapped :: IO ()
testSendsCapped = withWorker $ \w@W {wCtx = ctx} -> do
r <- paidIn ctx aliceOrder openPhase >>= revealed ctx keyA "alicebakery"
@@ -1068,18 +1120,16 @@ testRetryLanded = withWorker $ \W {wCtx = ctx} -> do
(field "phase" v', field "registerTx" v', field "expiresAt" v') `shouldBe` (Just "registered", Just J.Null, Just (J.toJSON expiry))
testRetryUnread :: IO ()
testRetryUnread = withWorker $ \w@W {wCtx = ctx@Ctx {cNode}} -> do
testRetryUnread = withWorker $ \W {wCtx = ctx@Ctx {cNode}} -> do
r <- paidIn ctx aliceOrder failedPhase
-- a revert with data is a failed read, not an unowned name
answerCall cNode (registrarOf cNode) (ownerOfSelector <> labelBytes "alicebakery") $ Reverts $ unhex "37ed32e8"
retryOf ctx r `shouldReturn` refusedWith 503 "provider_unavailable"
stored ctx r `shouldReturn` r
-- with an intent stored, the sponsor nonce and then the commitment unanswered
-- with an intent stored, the sponsor nonce unanswered
p <- paidIn ctx dynamisOrder openPhase >>= revealed ctx keyA "dynamis" >>= \x -> rewrite ctx x failedPhase
answerUnowned ctx "dynamis"
retryOf ctx p `shouldReturn` refusedWith 503 "provider_unavailable"
answerNonce w $ rvNonce reveal
retryOf ctx p `shouldReturn` refusedWith 503 "provider_unavailable"
stored ctx p `shouldReturn` p
testRetryReleasedCode :: IO ()
@@ -1143,13 +1193,26 @@ testRetryShortDeadline :: IO ()
testRetryShortDeadline = withWorker $ \w@W {wCtx = ctx} -> do
(r, _) <- paidIn ctx aliceOrder id >>= committedByUs w
f <- failedAfterRegister w r
rv <- signedReveal ctx keyA reveal {rvDeadline = fromJust (nrRevealBefore r) - 1} r
rv <- signedReveal ctx keyA reveal {rvDeadline = orderSeconds + 899} r
short <- rewrite ctx f $ \o -> o {nrReveal = Just rv}
answerNonce w $ rvNonce rv
v <- retried ctx short
(field "phase" v, field "label" v) `shouldBe` (Just "queued", Just J.Null)
nrReveal <$> stored ctx short `shouldReturn` Nothing
testRetryLapsedIntent :: IO ()
testRetryLapsedIntent = withWorker $ \w@W {wCtx = ctx} -> do
(r, t) <- paidIn ctx aliceOrder id >>= committedByUs w
rv <- signedReveal ctx keyA reveal r
f <- failedAfterRegister w r >>= \x -> rewrite ctx x (\o -> o {nrReveal = Just rv})
answerNonce w $ rvNonce rv
advance ctx $ fromIntegral (t + maxAge + 1 - orderSeconds)
field "label" <$> retried ctx f `shouldReturn` Just "alicebakery"
void $ steps w f >>= awaiting
x <- stored ctx f
(nrPhase x, nrReveal x, nrRevealBefore x) `shouldBe` (PCommitting, Just rv, Nothing)
length <$> sent w `shouldReturn` 2
testRetryNonceMoved :: IO ()
testRetryNonceMoved = withWorker $ \w@W {wCtx = ctx} -> do
(r, _) <- paidIn ctx aliceOrder id >>= committedByUs w