From 6a8ae41d0d8080ec8f1bbfa381831d67cd80da5e Mon Sep 17 00:00:00 2001 From: sh-agents Date: Wed, 11 Nov 2026 13:10:05 +0000 Subject: [PATCH] store: refuse name prices under a configured floor (#8093) * store: refuse name prices under a configured floor * store: correct the price floor comment on the allowance * store: log the configured price floor at start * store: test the price floor for spends, 3 years and the ini value * store: document the price floor for searches, moves and spends * store: reuse withRpcUrl in the price floor boot test, tidy imports --------- Co-authored-by: sh-agents <350651734+sh-agents@users.noreply.github.com> --- apps/simplex-store-service/README.md | 16 ++++++++ apps/simplex-store-service/docs/names.md | 9 +++-- .../src/StoreService/Config.hs | 9 ++++- .../src/StoreService/Names/Contracts.hs | 18 +++++---- .../src/StoreService/Names/Env.hs | 4 +- .../src/StoreService/Service.hs | 4 +- .../store_service.ini.example | 4 ++ .../store-service/store_service.ini.example | 4 ++ tests/Bots/StoreService/ConfigTests.hs | 16 ++++++++ .../StoreService/GroupIntegrationTests.hs | 28 ++++++++++++- .../Bots/StoreService/NamesContractsTests.hs | 39 +++++++++++++------ tests/Bots/StoreService/NamesOrdersTests.hs | 16 +++++--- tests/Bots/StoreService/NamesOwnerTests.hs | 11 +++--- tests/Bots/StoreService/NamesRecordsTests.hs | 3 +- tests/Bots/StoreService/NamesSearchTests.hs | 4 +- tests/Bots/StoreService/NamesSpendTests.hs | 6 ++- 16 files changed, 146 insertions(+), 45 deletions(-) diff --git a/apps/simplex-store-service/README.md b/apps/simplex-store-service/README.md index 77e6abcda6..77dafbc927 100644 --- a/apps/simplex-store-service/README.md +++ b/apps/simplex-store-service/README.md @@ -931,6 +931,22 @@ ini and a restart, with the same build. Codes for investors come from the group [From the group](#from-the-group)) or from `//issue name` in `--run-cli` (see [From the command line](#from-the-command-line)). +### Price floor + +Prices come from the chain, as the controller's `rentPriceUSD` answers them, with one exception: +`[names] min_price_cents_per_year` (default 5000, at least 1, read at startup) is the lowest price, in +cents a year, at which the service sells a name. A chain price under it for the name's paid years +fails the price read, and the service logs a warning with the price: + +- a search answers `503 provider_unavailable`; +- an order or a spend answers `503 name_check_unavailable` before any payment; +- a move answers `503 name_check_unavailable` and credits nothing. + +Code orders and their moves read no price, so the floor does not apply to them. The default is the +launch price of a name of 8 letters or more, $100 for 2 years. One floor bounds every length, so a +node answering the floor price for a 6- or 7-letter name passes the check even when those names +cost more on chain. To lower prices on chain below the floor, lower the setting and restart first. + ### Link edits Each "Edit links" save is one request, relayed by the service as one transaction whose gas it pays diff --git a/apps/simplex-store-service/docs/names.md b/apps/simplex-store-service/docs/names.md index 97df9d7a1c..1673d24201 100644 --- a/apps/simplex-store-service/docs/names.md +++ b/apps/simplex-store-service/docs/names.md @@ -185,7 +185,7 @@ sequenceDiagram Notes: -- The checks are run in the contract's order: timestamp within 600 s, duration (paid years plus the free days until `term_start`), current terms, a name available on chain and not in `registering` here, then the chain price equal to the order's ([`Web/Names/Orders.hs:104-118`](../src/StoreService/Web/Names/Orders.hs#L104-L118), [`Names/Orders.hs:266-310`](../src/StoreService/Names/Orders.hs#L266-L310)). Refusals are listed in [`Web/Names/Orders.hs:344-357`](../src/StoreService/Web/Names/Orders.hs#L344-L357). +- The checks are run in the contract's order: timestamp within 600 s, duration (paid years plus the free days until `term_start`), current terms, a name available on chain and not in `registering` here, then the chain price, at least `min_price_cents_per_year` times the paid years and equal to the order's ([`Web/Names/Orders.hs:104-118`](../src/StoreService/Web/Names/Orders.hs#L104-L118), [`Names/Orders.hs:266-310`](../src/StoreService/Names/Orders.hs#L266-L310), [`Names/Contracts.hs:190-202`](../src/StoreService/Names/Contracts.hs#L190-L202)). Refusals are listed in [`Web/Names/Orders.hs:344-357`](../src/StoreService/Web/Names/Orders.hs#L344-L357). - The terms text is read from the database, or fetched from git.simplex.chat or GitHub on the first use of its hash and stored; either is checked against the sha256 in its link when the link has one. While it cannot be read, the order is answered `503 terms_unavailable` ([`Names/Orders.hs:284-291`](../src/StoreService/Names/Orders.hs#L284-L291), [`Names/Contracts.hs:239-290`](../src/StoreService/Names/Contracts.hs#L239-L290), [`Web/Names/Orders.hs:356`](../src/StoreService/Web/Names/Orders.hs#L356)). - The name lock is held across the provider call, and the answer is sent after it. Overdue unpaid orders of the same wallet and name are closed in the order's transaction ([`Web/Names/Orders.hs:76-100`](../src/StoreService/Web/Names/Orders.hs#L76-L100), [`Names/Orders.hs:207-243`](../src/StoreService/Names/Orders.hs#L207-L243)). - When the order is not written, its provider invoice is cancelled. Provider checkouts are capped at 30 a minute for all clients together ([`Web/Names/Orders.hs:139-173`](../src/StoreService/Web/Names/Orders.hs#L139-L173), [`Web/Http.hs:151-154`](../src/StoreService/Web/Http.hs#L151-L154)). @@ -323,6 +323,7 @@ Notes: - Only the label's hash and length are sent ([`Web/Names/Search.hs:53-57`](../src/StoreService/Web/Names/Search.hs#L53-L57)). 30 a minute per client are allowed for searches and terms reads together; chain reads are run for at most 16 searches at once, for all clients together, and a 17th is answered `429` ([`Web/Names.hs:34-36`](../src/StoreService/Web/Names.hs#L34-L36), [`Web/Http.hs:128-149`](../src/StoreService/Web/Http.hs#L128-L149)). - The owner and resolver are read from the registry for a name registered or in its grace period (`nameExpires` plus `GRACE_PERIOD` not before the latest block's time); for any other name `reservedNames` is read ([`Names/Contracts.hs:149-162`](../src/StoreService/Names/Contracts.hs#L149-L162)). +- An available name's price is `rentPriceUSD` for its paid years; a price under `min_price_cents_per_year` times those years fails the search with `503 provider_unavailable` ([`Names/Contracts.hs:190-202`](../src/StoreService/Names/Contracts.hs#L190-L202)). - A name shown available on chain while an order here is in `registering` is answered `registered` with no entities; a reserved name is answered `reserved` ([`Web/Names/Search.hs:66-72`](../src/StoreService/Web/Names/Search.hs#L66-L72), [`Web/Names/Search.hs:97-98`](../src/StoreService/Web/Names/Search.hs#L97-L98)). - Records, their nonce and the edits left are read only for a name on the default resolver, and at most 5 links of each record ([`Names/Contracts.hs:164-177`](../src/StoreService/Names/Contracts.hs#L164-L177), [`Web/Names/Search.hs:99-130`](../src/StoreService/Web/Names/Search.hs#L99-L130)). - A link whose host is, or resolves to, a non-public address is refused before it is read, and each DNS lookup is run in a thread of its own. Profiles are kept 600 s; 4 fetch slots are used by searches and 2 by previews ([`Names/Profiles.hs:83-95`](../src/StoreService/Names/Profiles.hs#L83-L95), [`Names/Profiles.hs:326-371`](../src/StoreService/Names/Profiles.hs#L326-L371)). @@ -382,7 +383,7 @@ sequenceDiagram else another name P->>W: POST /api/registration/ID/name, signed new name W->>E: registered after all? refused after 5 sends of a kind - W->>E: terms, name available, a paid order's rentPriceUSD at most its price + W->>E: terms, name available, a paid order's rentPriceUSD at least the floor and at most its price W->>D: under both names' locks: new name queued, moves + 1 W-->>P: 200 the order's view, or 409 registration_changed when registered after all end @@ -394,6 +395,7 @@ 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)). - 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)). --- @@ -436,7 +438,7 @@ sequenceDiagram W->>W: its owner's signature checked, name and balance locks taken W->>E: terms of the name's kind W->>D: balance equal to the amount read and not held - W->>E: name status, rentPriceUSD equal to the price + W->>E: name status, rentPriceUSD at least the floor and equal to the price alt balance covers the price, no method sent W->>D: one transaction: registration queued, balance lowered by the price W-->>P: 200: registrationId @@ -455,6 +457,7 @@ sequenceDiagram Notes: - `method` is present exactly when the price is above the balance, and a card difference under 50 cents is refused ([`Web/Names/Orders.hs:280-306`](../src/StoreService/Web/Names/Orders.hs#L280-L306)). +- The chain price is checked as an order's: a price under `min_price_cents_per_year` times the paid years is answered `503 name_check_unavailable` before any payment ([`Names/Contracts.hs:190-202`](../src/StoreService/Names/Contracts.hs#L190-L202)). - The spend is signed `simplex.domains spend SOURCE ...` by the source order's owner, who owns the new order ([`Web/Names/Orders.hs:249-257`](../src/StoreService/Web/Names/Orders.hs#L249-L257), [`Names/Signatures.hs:104-106`](../src/StoreService/Names/Signatures.hs#L104-L106)). - A balance amount other than the one read, or a hold, is answered `409 balance_changed`. A hold by an overdue order is treated as none, and that order is closed in the spend's transaction ([`Names/Balances.hs:79-87`](../src/StoreService/Names/Balances.hs#L79-L87), [`Names/Orders.hs:236-243`](../src/StoreService/Names/Orders.hs#L236-L243)). - The whole balance read is held by the difference order. At settlement the hold is spent; when it cannot be spent, the order is kept `queued` and an error is logged ([`Names/Store.hs:514-532`](../src/StoreService/Names/Store.hs#L514-L532), [`Orders.hs:200-204`](../src/StoreService/Orders.hs#L200-L204), [`Orders.hs:232-236`](../src/StoreService/Orders.hs#L232-L236)). diff --git a/apps/simplex-store-service/src/StoreService/Config.hs b/apps/simplex-store-service/src/StoreService/Config.hs index 403e8ae690..91644f1308 100644 --- a/apps/simplex-store-service/src/StoreService/Config.hs +++ b/apps/simplex-store-service/src/StoreService/Config.hs @@ -177,6 +177,7 @@ data NamesConfig = NamesConfig nCodesOnly :: Bool, -- | The owner record edits relayed for one name in any 365 days, each a transaction whose gas the service pays. nRecordEditsPerYear :: Int, + nMinPriceCentsPerYear :: Int, -- | What each community name is for, keyed by the label hash's bytes, as search receives a name. nCommunity :: Map ByteString Text, nChain :: ChainConfig @@ -294,6 +295,9 @@ defaultRecordEditsPerYear = 3 minRecordEditsPerYear = 1 maxRecordEditsPerYear = 100 +defaultMinPriceCentsPerYear :: Int +defaultMinPriceCentsPerYear = 5000 + -- | The controller refuses a registration under 730 days. minNameYears :: Int minNameYears = 2 @@ -416,7 +420,7 @@ knownSettings = ("dev", ["accept_unverified_store_receipts", "check_store_during_coverage", "credit_test_purchases"]), ("issuer", ["index", "private_key", "dev_keys"]), ("log", ["level"]), - ("names", ["tld", "years", "term_start", "term_slack_seconds", "reveal_margin_seconds", "max_order_moves", "codes_only", "record_edits_per_year"]), + ("names", ["tld", "years", "term_start", "term_slack_seconds", "reveal_margin_seconds", "max_order_moves", "codes_only", "record_edits_per_year", "min_price_cents_per_year"]), ("chain", ["rpc_url", "chain_id", "controller", "sponsor", "base_registrar", "resolver", "service_key", "confirmations", "gas_limit_cap", "max_fee_per_gas"]) ] @@ -670,9 +674,10 @@ parseConfig ini = do nMaxOrderMoves <- atLeast 0 "names" "max_order_moves" 3 nCodesOnly <- bool "names" "codes_only" False nRecordEditsPerYear <- recordEditsPerYear + nMinPriceCentsPerYear <- atLeast 1 "names" "min_price_cents_per_year" defaultMinPriceCentsPerYear nCommunity <- communityList nTLD nChain <- chainSection - pure (Just NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nRevealMarginSeconds, nMaxOrderMoves, nCodesOnly, nRecordEditsPerYear, nCommunity, nChain}) + pure (Just NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nRevealMarginSeconds, nMaxOrderMoves, nCodesOnly, nRecordEditsPerYear, nMinPriceCentsPerYear, nCommunity, nChain}) tldSetting = required "names" "tld" >>= \case "simplex" -> Right TLDSimplex diff --git a/apps/simplex-store-service/src/StoreService/Names/Contracts.hs b/apps/simplex-store-service/src/StoreService/Names/Contracts.hs index 5a92f094de..6a5391859d 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Contracts.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Contracts.hs @@ -83,6 +83,7 @@ data Contracts = Contracts { ctChain :: ChainClient, ctStore :: DBStore, ctTLD :: SimplexTLD, + ctMinPriceCentsPerYear :: Int, ctRegistrar :: IORef (Maybe RegistrarConstants), ctCommitmentAges :: IORef (Maybe CommitmentAges), ctNamePolicy :: IORef (Maybe Address), @@ -145,8 +146,8 @@ termsFetchInterval :: Int termsFetchInterval = 3600 * 1000000 -- | Reads nothing: the registrar's constants, the commitment ages and the name policy are read on first use and kept once read. -newContracts :: DBStore -> SimplexTLD -> ChainClient -> IO Contracts -newContracts ctStore ctTLD ctChain = do +newContracts :: DBStore -> SimplexTLD -> Int -> ChainClient -> IO Contracts +newContracts ctStore ctTLD ctMinPriceCentsPerYear ctChain = do ctRegistrar <- newIORef Nothing ctCommitmentAges <- newIORef Nothing ctNamePolicy <- newIORef Nothing @@ -154,7 +155,7 @@ newContracts ctStore ctTLD ctChain = do ctTermsLinks <- newIORef Nothing ctFetchAfter <- newIORef =<< getCurrentTime ctFetching <- newMVar () - pure Contracts {ctChain, ctStore, ctTLD, ctRegistrar, ctCommitmentAges, ctNamePolicy, ctTermsTexts, ctTermsLinks, ctFetchAfter, ctFetching, ctDeadline = nameReadsDeadline, ctTermsBackoff = termsBackoffSeconds} + pure Contracts {ctChain, ctStore, ctTLD, ctMinPriceCentsPerYear, ctRegistrar, ctCommitmentAges, ctNamePolicy, ctTermsTexts, ctTermsLinks, ctFetchAfter, ctFetching, ctDeadline = nameReadsDeadline, ctTermsBackoff = termsBackoffSeconds} -- | Registered while @nameExpires + GRACE_PERIOD@ is not before the latest block's time. nameStatus :: Contracts -> LabelHash -> IO (Either ChainError NameStatus) @@ -187,15 +188,18 @@ textRecord :: Contracts -> LabelHash -> ByteString -> IO (Either ChainError Text textRecord ct lh key = call ct (resolver ct) ("text(bytes32,string)", abiTuple [Static $ nodeArg ct lh, Dynamic $ abiBytes key]) decodeString -- | Cents for a label of this length and these years. The oracle prices by length only, --- so a placeholder label of that length keeps the label from the RPC node. A price under a cent is refused: a node --- answering one would sell names for nothing while the sponsor allowance pays their rent. +-- so a placeholder label of that length keeps the label from the RPC node. A price under the floor for these years is refused: a node +-- answering one would sell names below it while the sponsor allowance pays their rent. chainPriceCents :: Contracts -> Int -> Int -> IO (Either ChainError Int) -chainPriceCents ct len years = call ct (controller ct) ("rentPriceUSD(string,uint256)", args) $ \ret -> do +chainPriceCents ct@Contracts {ctMinPriceCentsPerYear} len years = fmap (>>= checkFloor) . call ct (controller ct) ("rentPriceUSD(string,uint256)", args) $ \ret -> do base <- decodeUint256 ret let cents = base `div` attoUsdPerCent - if cents >= 1 && cents <= toInteger (maxBound :: Int) then Right (fromInteger cents) else Left AERange + if cents <= toInteger (maxBound :: Int) then Right (fromInteger cents) else Left AERange where args = abiTuple [Dynamic $ abiBytes $ B.replicate len 'a', Static $ uint256 $ toInteger years * secondsPerYear] + checkFloor cents + | toInteger cents >= toInteger ctMinPriceCentsPerYear * toInteger years = Right cents + | otherwise = Left $ ChainUnavailable $ "rentPriceUSD answered " <> tshow cents <> " cents for " <> tshow years <> " years, under names.min_price_cents_per_year" sponsorNonce :: Contracts -> Address -> IO (Either ChainError Word64) sponsorNonce ct owner = call ct (cSponsor $ chainConfig ct) ("nonces(address)", addressWord owner) decodeWord64 diff --git a/apps/simplex-store-service/src/StoreService/Names/Env.hs b/apps/simplex-store-service/src/StoreService/Names/Env.hs index 084c736cd1..ed252d0195 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Env.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Env.hs @@ -34,10 +34,10 @@ data NamesEnv = NamesEnv } newNamesEnv :: ChatController -> UserId -> NamesConfig -> DBStore -> Turns -> Waiters -> [Provider] -> IO NamesEnv -newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain, nRecordEditsPerYear} neStore neTurns neWaiters neProviders = do +newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain, nRecordEditsPerYear, nMinPriceCentsPerYear} neStore neTurns neWaiters neProviders = do nePhases <- newPhases neChain <- newChainClient random nChain - neContracts <- newContracts neStore nTLD neChain + neContracts <- newContracts neStore nTLD nMinPriceCentsPerYear neChain neProfiles <- newProfiles defaultProfileLimits monotonicSeconds (chatProfile neChat neUserId) systemResolver neSender <- newSender random neChain neStore getCurrentTime neRecords <- newRecords neStore neTurns neContracts neChain neSender nTLD nRecordEditsPerYear getCurrentTime diff --git a/apps/simplex-store-service/src/StoreService/Service.hs b/apps/simplex-store-service/src/StoreService/Service.hs index 2dc3c3e9ba..e853069620 100644 --- a/apps/simplex-store-service/src/StoreService/Service.hs +++ b/apps/simplex-store-service/src/StoreService/Service.hs @@ -328,11 +328,11 @@ storeService opts serviceCfg baseCfg env = do 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}} = +startNames cc@ChatController {chatStore} turns waiters providers nc@NamesConfig {nTLD, nCodesOnly, nMinPriceCentsPerYear, nChain = ChainConfig {cChainId}} = readTVarIO (currentUser cc) >>= \case Nothing -> putStrLn "No current user" >> exitFailure Just User {userId} -> do - logNote $ "names: on, TLD " <> safeDecodeUtf8 (strEncode nTLD) <> ", chain " <> tshow cChainId <> codesOnlyNote + logNote $ "names: on, TLD " <> safeDecodeUtf8 (strEncode nTLD) <> ", chain " <> tshow cChainId <> ", price floor " <> tshow nMinPriceCentsPerYear <> " cents a year" <> codesOnlyNote ne@NamesEnv {neContracts} <- newNamesEnv cc userId nc chatStore turns waiters providers logNamePolicy nc neContracts pure ne diff --git a/apps/simplex-store-service/store_service.ini.example b/apps/simplex-store-service/store_service.ini.example index 296d1e81c7..150c236515 100644 --- a/apps/simplex-store-service/store_service.ini.example +++ b/apps/simplex-store-service/store_service.ini.example @@ -117,6 +117,10 @@ idle_seconds = 60 ; the owner's link edits relayed for one name in any 365 days, each a transaction whose gas the ; service pays; 1 to 100. A request refused, never sent or reverted on chain does not count ;record_edits_per_year = 3 +; the lowest price a name is sold at, in cents a year, at least 1. Prices come from the chain, and +; this floor is the one exception: a chain price under it for a name's paid years answers 503 to the +; search, order, spend or move. Lower it before lowering prices on chain below it +;min_price_cents_per_year = 5000 ; ; what each community name is for, with [names] only (the name policy on chain lists the names): ; one line per name, `