From 4aab0ef3a6b570c57ee3e205d9679e490c8488fe Mon Sep 17 00:00:00 2001 From: sh-agents Date: Wed, 11 Nov 2026 08:56:50 +0000 Subject: [PATCH] store: refuse unsafe service starts, exit on a lost instance lock (#8096) * store: restrict Stripe test keys to test chains, refuse mixed modes * store: limit gas_limit_cap times max_fee_per_gas to 1 ETH * store: prevent start while another key's transactions are pending * store: exit when the instance lock's connection fails, restart always * store: explain the fee cap refusal, match the chain checks' form * store: check pending transactions' key and chain before the chat starts * store: print a lost instance lock before the service stops * store: document start refusals, stuck pending rows and the fee bound * store: build the pending signer refusal without an inferred constraint * store: zero the old key's allowance before dropping its pending rows * store: flush the lost lock line before the service stops * store: name the pending check after the signatures it checks * store: test that another key's pending row stops the start * store: read the chain settings in the start test by field * store: limit each transaction's fee to 1 ETH at signing * store: log a fee capped at 1 ETH for replacements too --------- Co-authored-by: sh-agents <350651734+sh-agents@users.noreply.github.com> --- apps/simplex-store-service/Main.hs | 4 +- apps/simplex-store-service/README.md | 69 ++++++++++++++++--- apps/simplex-store-service/docs/names.md | 8 +-- .../src/StoreService/Config.hs | 19 ++++- .../src/StoreService/Names/Chain.hs | 42 ++++++++--- .../src/StoreService/Names/Sender.hs | 50 +++++++++++--- .../src/StoreService/Service.hs | 11 +-- .../src/StoreService/Store/Migrate.hs | 24 +++++-- scripts/store-service/compose.yml | 2 +- tests/Bots/StoreService/ConfigTests.hs | 27 +++++++- .../StoreService/GroupIntegrationTests.hs | 55 ++++++++++++--- tests/Bots/StoreService/NamesSenderTests.hs | 62 +++++++++++++++++ 12 files changed, 312 insertions(+), 61 deletions(-) diff --git a/apps/simplex-store-service/Main.hs b/apps/simplex-store-service/Main.hs index 796f5b9cdf..73f591a5b7 100644 --- a/apps/simplex-store-service/Main.hs +++ b/apps/simplex-store-service/Main.hs @@ -5,7 +5,7 @@ module Main where import StoreService.Config (ServiceConfig (..), defaultLogLevel) import StoreService.Options (StoreServiceOpts (..)) import StoreService.Service -import StoreService.Store.Migrate (withInstanceLock) +import StoreService.Store.Migrate (instanceLockCheckInterval, withInstanceLock) import Control.Logger.Simple (LogConfig (..), setLogLevel, withGlobalLogging) import GHC.IO.Encoding (setLocaleEncoding) import Simplex.Chat.Terminal (terminalChatConfig) @@ -22,7 +22,7 @@ main = do opts@StoreServiceOpts {runCLI, serviceConfigFile} <- welcomeGetOpts serviceCfg <- traverse readConfigOrExit serviceConfigFile mapM_ (setLogLevel . serviceLogLevel) serviceCfg - withInstanceLock opts $ + withInstanceLock instanceLockCheckInterval opts $ if runCLI then storeServiceCLI opts serviceCfg else newServiceState >>= storeService opts serviceCfg terminalChatConfig diff --git a/apps/simplex-store-service/README.md b/apps/simplex-store-service/README.md index 36f09667f8..3f671eab1e 100644 --- a/apps/simplex-store-service/README.md +++ b/apps/simplex-store-service/README.md @@ -251,6 +251,12 @@ absent `[btcpay]` does for Bitcoin and Monero. Its keys: it expired and logs an error. Check such orders in the Stripe Dashboard. After a restart, the hour starts again. +The service refuses to start when `secret_key` and `publishable_key` are not both live keys or both +test keys (`sk_test_` or `rk_test_`, and `pk_test_`). With `[names]`, it also refuses test keys +unless `[chain] chain_id` is 31337, anvil's local chain, or 11155111, Sepolia, so no name on another +chain is registered for a test payment. Without `[names]`, test keys are accepted, as a staging +deployment of badges needs. + The service creates each payment intent with `metadata[order]`, the order's reference. A badge order's is its id, which the page shows the buyer as "Reference" (`invoice_id` in `sx_badge_service_invoices`); a name order's is `name:0x` (`order_hash` in @@ -545,7 +551,11 @@ service account key when `[apple]` and `[google]` are configured. The folder mus On PostgreSQL the service holds an advisory lock on its database for as long as it runs, so a second process on the same database, `--run-cli` included, stops at start with `another store service holds this database`: two would send with the gas key's nonces and run the chat agent's queues together. Stop -the service before the console. +the service before the console. The lock is held by a database connection of its own, and PostgreSQL +releases it when that connection ends, as on a PostgreSQL restart. The service checks the connection +every 30 seconds and exits when it fails, with `Error: the instance lock's database connection +failed: …`. Compose's `restart: unless-stopped` then starts it again, and each start takes the lock +or exits while another process holds it. ### Upgrading from the badge service @@ -1094,11 +1104,18 @@ reads, clears and logs its acceptance, as a community name's did: none. The expected orders × 0.6946 ETH, plus 0.6054 ETH per expected Save of both links (0.3026 ETH for one), pays for all of them even at the cap. A name's link edits are at most `record_edits_per_year` (3) a year, see [Link edits](#link-edits). The figures scale with - `max_fee_per_gas`. + `max_fee_per_gas` until a transaction's gas limit × `max_fee_per_gas` passes 1 ETH; above that its + max fee stays capped, as the last point says. - A Save of both links in one `multicall` uses 26,906 gas less than the same two edits sent as two transactions with links of 4096 bytes, and 28,082 less with short links. -- `gas_limit_cap` (default 8,000,000) must stay above 6,791,417: a transaction whose estimate is +- `gas_limit_cap` (default 8,000,000) must be at least 6,791,417: a transaction whose estimate is above it is not sent. +- Each transaction's max fee is also at most 1 ETH divided by its gas limit, since reth and geth + refuse a transaction whose gas limit × max fee is above `--rpc.txfeecap`, 1 ETH by default, and + every later transaction of the key would wait behind it. The defaults stay under the bound, at + 0.8 ETH for 8,000,000 gas. A transaction it caps logs `[NOTE …] names sender: nonce at max + fee , capped so that its gas limit costs at most 1 ETH`, and a replacement it caps logs + `[NOTE …] names sender: replacing nonce at max fee , capped so that …`. #### Check the balance and transactions in flight @@ -1142,10 +1159,11 @@ psql "postgresql://simplex@127.0.0.1:5432/badge_service" -c "SELECT registrar_no 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. -- `[WARN …] names sender: the latest block needs wei per gas, above max_fee_per_gas ; - the transaction waits` when fees are above the cap. Nothing is signed until they fall. -- `[WARN …] names sender: nonce : not replaced: a replacement would pay over max_fee_per_gas - ` when a transaction stuck in the pool cannot be bumped within the cap. +- `[WARN …] names sender: the latest block needs wei per gas, above the fee cap ; + the transaction waits` when fees are above the fee cap, `max_fee_per_gas` or 1 ETH over the gas + limit. Nothing is signed until they fall. +- `[WARN …] names sender: nonce : not replaced: a replacement would pay over the fee cap + ` when a transaction stuck in the pool cannot be bumped within the fee cap. 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 @@ -1162,9 +1180,16 @@ owner who got the name another way counts as served, and a code order spends its The service resumes its saved transactions at start and gives new ones nonces after theirs. Those are signed by the key that sent them, so started on another key it would give that key's -transactions nonces it has not reached, and they would never be mined. Rotate only with no `pending` -row, at a quiet time: every stop takes the store offline, and each order sends its register about a -minute after its commit. +transactions nonces it has not reached, and they would never be mined. So the service refuses to start +while a `pending` row is signed by another key than `service_key`, or for another chain than +`chain_id`, and names both: + +``` +Error: a pending transaction of nonce was signed by , not by of chain.service_key; start with the key and chain that signed it until no transaction is pending +``` + +Rotate only with no `pending` row, at a quiet time: every stop takes the store offline, and each order +sends its register about a minute after its commit. 1. Keep a copy of the old ini, create the new key in another, fund it, and have the guardian grant it an allowance, as above for `$NEW`: @@ -1210,7 +1235,10 @@ minute after its commit. ``` It prints `$NEW`. After the next order's commit, `cast nonce $NEW --block pending` is above 0, - and `cast nonce $OLD --block pending` stays where it was. + and `cast nonce $OLD --block pending` stays where it was. If `docker compose logs store-service` + shows the error above, a row is still pending and compose keeps restarting the service: stop it + with `docker compose stop store-service`, put `prev/store_service.ini` back in `config/` and + repeat step 2. 4. The guardian zeroes the old key's allowance, sending this calldata as in [Grant the allowance](#grant-the-allowance), then anyone checks it reads 0: @@ -1236,6 +1264,25 @@ minute after its commit. The last balance prints `0`. A negative value, from fees above about 150 gwei, makes cast refuse the sweep; run it again when they fall. +The old key's `pending` rows keep every other key from starting until they settle. A key that +starts but cannot pay for them, as its ETH is used up, settles them once funded, as in +[Fund the key](#fund-the-key). When it is not to be funded, or cannot be started at all, as when its +ini is lost, the guardian first zeroes its allowance, as in step 4, and anyone checks it reads 0, so +that a register of the old key reverts wherever it lands. Then stop the service and mark the rows +dropped: + +``` +docker compose stop store-service +psql "postgresql://simplex@127.0.0.1:5432/badge_service" -c "UPDATE simplex_v1_chat_schema.sx_badge_service_name_transactions SET status = 'dropped' WHERE status = 'pending'" +``` + +The sender no longer follows them, and at the next start each order is judged from the chain, as +after a nonce taken by another transaction: a commit or terms acceptance not on chain is sent again +from the new key, an order whose register did not land fails as after a reverted register and its +buyer can Try again, and a link edit not applied is sent again before its deadline. The marked +transactions stay signed and valid: while the old address holds enough ETH, its commits, terms +acceptances and link edits can still be mined, and its registers revert. + ## Issuing codes Operators issue codes two ways: from the service's own command line in `--run-cli` mode, and from the diff --git a/apps/simplex-store-service/docs/names.md b/apps/simplex-store-service/docs/names.md index c21b716ea0..fb11aaec19 100644 --- a/apps/simplex-store-service/docs/names.md +++ b/apps/simplex-store-service/docs/names.md @@ -99,7 +99,7 @@ How a request flows: | Search handlers | Name status, price, terms, and records with link profiles; the terms text; the link preview. These handlers only read. | [`Web/Names/Search.hs:53-154`](../src/StoreService/Web/Names/Search.hs#L53-L154), [`Names/Profiles.hs:236-274`](../src/StoreService/Names/Profiles.hs#L236-L274) | | Poller | One loop. A pass is run every `waiting_seconds` (3) while a page holds an invoice poll, else every `idle_seconds` (60), and webhook read hints are served between passes. Invoices are settled, overdue ones expired 10 minutes past their window, late money credited, and unpaid orders deleted daily, 30 days after their invoice expired. | [`Poller.hs:62-76`](../src/StoreService/Poller.hs#L62-L76), [`Poller.hs:148-168`](../src/StoreService/Poller.hs#L148-L168), [`Poller.hs:559-598`](../src/StoreService/Poller.hs#L559-L598), [`Orders.hs:95-275`](../src/StoreService/Orders.hs#L95-L275), [`Names/LateCredits.hs:50-72`](../src/StoreService/Names/LateCredits.hs#L50-L72) | | Names worker | A thread is started by a scan every 5 s for each order in `queued`, `committing`, `committed` or `registering`: commit, wait for the reveal, terms acceptance relay, register, and the outcome judged from chain state. A daily sweep releases the codes of code orders failed for 30 days. | [`Names/Worker.hs:115-401`](../src/StoreService/Names/Worker.hs#L115-L401), [`Names/Worker.hs:503-522`](../src/StoreService/Names/Worker.hs#L503-L522) | -| Sender | Signs with `[chain] service_key`, the only signer of that key. Per transaction: dry run, nonce, fees within `max_fee_per_gas`, EIP-1559 signature, a saved row, broadcast. Every 4 s: receipts followed to `confirmations` (default 3), an unknown transaction rebroadcast, and one unmined after 36 s replaced with higher fees when the market's fees have passed its own, within `max_fee_per_gas`. | [`Names/Sender.hs:107-283`](../src/StoreService/Names/Sender.hs#L107-L283) | +| Sender | Signs with `[chain] service_key`, the only signer of that key. Per transaction: dry run, nonce, fees within `max_fee_per_gas` and within 1 ETH over the gas limit, EIP-1559 signature, a saved row, broadcast. Every 4 s: receipts followed to `confirmations` (default 3), an unknown transaction rebroadcast, and one unmined after 36 s replaced with higher fees when the market's fees have passed its own, within the same caps. | [`Names/Sender.hs:109-332`](../src/StoreService/Names/Sender.hs#L109-L332) | | Record edits relay and lane | Owner-signed link edits, each relayed as one resolver transaction through the sender, within `record_edits_per_year` and 10 an hour per name. Pending edits are settled by the lane every 4 s. | [`Names/Records.hs:112-366`](../src/StoreService/Names/Records.hs#L112-L366), [`Web/Names/Records.hs:34-86`](../src/StoreService/Web/Names/Records.hs#L34-L86) | | Contracts reader and chain client | One JSON-RPC client to `[chain] rpc_url`, 10 s per call, redirects refused. Typed `eth_call` reads; the name policy address, the registrar constants and the commitment ages kept once read; terms texts fetched once per hash. | [`Names/Contracts.hs:137-379`](../src/StoreService/Names/Contracts.hs#L137-L379), [`Names/Chain.hs:112-305`](../src/StoreService/Names/Chain.hs#L112-L305) | | In-memory coordination | Waiters: invoice status and payments. Phases: registration versions. Locks: one writer at a time per label hash, prepaid code and balance. Read hints: up to 256 webhook reads queued for the poller. | [`Waiters.hs:14-91`](../src/StoreService/Waiters.hs#L14-L91), [`Names/Phases.hs:15-65`](../src/StoreService/Names/Phases.hs#L15-L65), [`Names/Turns.hs:3-69`](../src/StoreService/Names/Turns.hs#L3-L69), [`Poller.hs:88-108`](../src/StoreService/Poller.hs#L88-L108) | @@ -569,9 +569,9 @@ sequenceDiagram participant V as BTCPay O->>U: docker compose up -d --build U->>U: store_service.ini parsed in full - U->>D: pg_try_advisory_lock, held for the run + U->>D: pg_try_advisory_lock, held for the run, its connection checked every 30 s U->>E: eth_chainId equal to [chain] chain_id - U->>C: start: chat migrations, store migrations in preStartHook + U->>C: start: chat migrations, store migrations and pending transactions' signers in preStartHook C->>C: postStartHook: service requests on, bot address shown or created C->>U: callback with the controller U->>D: badge catalog seeded @@ -585,7 +585,7 @@ sequenceDiagram Notes: - The container runs `simplex-store-service -d BADGE_DB_CONN --create-schema --yes-migrate --service-config /etc/simplex-store-service/store_service.ini` ([`compose.yml:14-20`](../../../scripts/store-service/compose.yml#L14-L20)). -- The process exits on a config error, a held lock, a chain id mismatch, an issuer key mismatch, a failed migration or a missing site build; compose restarts it on failure up to 5 times ([`Main.hs:22-28`](../Main.hs#L22-L28), [`Service.hs:280-285`](../src/StoreService/Service.hs#L280-L285), [`compose.yml:10`](../../../scripts/store-service/compose.yml#L10)). A node that does not answer `eth_chainId` is only warned of, unless `dev_keys` are set ([`Service.hs:169-181`](../src/StoreService/Service.hs#L169-L181)). +- The process exits on a config error (Stripe keys of two modes, or Stripe test keys with `[names]` off chains 31337 and 11155111, among them), a held lock, a chain id mismatch, an issuer key mismatch, a failed migration, a pending transaction signed by another key or for another chain, or a missing site build; while it runs, it exits when the lock's connection fails. Compose restarts it after every exit until it is stopped ([`Main.hs:22-28`](../Main.hs#L22-L28), [`Config.hs:475-479`](../src/StoreService/Config.hs#L475-L479), [`Service.hs:280-285`](../src/StoreService/Service.hs#L280-L285), [`Service.hs:632-635`](../src/StoreService/Service.hs#L632-L635), [`Store/Migrate.hs:53-64`](../src/StoreService/Store/Migrate.hs#L53-L64), [`compose.yml:10`](../../../scripts/store-service/compose.yml#L10)). A node that does not answer `eth_chainId` is only warned of, unless `dev_keys` are set ([`Service.hs:169-181`](../src/StoreService/Service.hs#L169-L181)). - The lanes are started in the chat core's callback, after `postStartHook`, so after the bot address exists, unless `--no-address` is passed ([`Chat/Core.hs:97-106`](../../../src/Simplex/Chat/Core.hs#L97-L106), [`Service.hs:301-313`](../src/StoreService/Service.hs#L301-L313), [`Service.hs:636-645`](../src/StoreService/Service.hs#L636-L645)). - The names lanes are the sender, the record edits lane, the names worker and the code sweep; all lanes are started together with `raceAny_` from simplexmq ([`Service.hs:366-370`](../src/StoreService/Service.hs#L366-L370)). - Under `--run-cli` the binary runs the terminal chat and the badge request lanes only ([`Service.hs:381-404`](../src/StoreService/Service.hs#L381-L404)). diff --git a/apps/simplex-store-service/src/StoreService/Config.hs b/apps/simplex-store-service/src/StoreService/Config.hs index 897da75eb7..403e8ae690 100644 --- a/apps/simplex-store-service/src/StoreService/Config.hs +++ b/apps/simplex-store-service/src/StoreService/Config.hs @@ -280,6 +280,12 @@ publishableKey k = case T.stripPrefix "pk_live_" k <|> T.stripPrefix "pk_test_" Just rest -> not (T.null rest) && T.all (\c -> isAsciiUpper c || isAsciiLower c || isDigit c) rest Nothing -> False +testSecretKey :: Text -> Bool +testSecretKey k = any (`T.isPrefixOf` k) ["sk_test_", "rk_test_"] + +testPublishableKey :: Text -> Bool +testPublishableKey = T.isPrefixOf "pk_test_" + maxCommunityTextLength :: Int maxCommunityTextLength = 200 @@ -296,6 +302,9 @@ minNameYears = 2 localChainId :: Word64 localChainId = 31337 +sepoliaChainId :: Word64 +sepoliaChainId = 11155111 + -- | 100 gwei. defaultMaxFeePerGas :: Word64 defaultMaxFeePerGas = 100000000000 @@ -456,13 +465,18 @@ parseConfig ini = do Left ("issuer.dev_keys is accepted only with [names] on the local chain, [chain] chain_id = " <> show localChainId) unless (null devKeys) $ do forM_ str $ \StripeConfig {sSecretKey, sPublishableKey} -> do - unless (any (`T.isPrefixOf` sSecretKey) ["sk_test_", "rk_test_"]) $ + unless (testSecretKey sSecretKey) $ Left "issuer.dev_keys is refused with a live Stripe key: stripe.secret_key must be a test key, sk_test_ or rk_test_" - unless ("pk_test_" `T.isPrefixOf` sPublishableKey) $ + unless (testPublishableKey sPublishableKey) $ Left "issuer.dev_keys is refused with a live Stripe key: stripe.publishable_key must be a test key, pk_test_" forM_ btc $ \BTCPayConfig {bHost} -> unless (isLoopbackHost (urlHost bHost)) $ Left "issuer.dev_keys is refused with a remote BTCPay: btcpay.host must be on loopback, 127.0.0.1, ::1 or localhost" + forM_ str $ \StripeConfig {sSecretKey, sPublishableKey} -> do + when (testSecretKey sSecretKey /= testPublishableKey sPublishableKey) $ + Left "stripe.secret_key and stripe.publishable_key must be both live keys or both test keys" + when (testSecretKey sSecretKey && maybe False (not . onTestChain) nms) $ + Left ("Stripe test keys are accepted with [names] only on a test chain, [chain] chain_id = " <> show localChainId <> " or " <> show sepoliaChainId) pWaitingSeconds <- cadence "waiting_seconds" 3 pIdleSeconds <- cadence "idle_seconds" 60 apple <- appleSection @@ -554,6 +568,7 @@ parseConfig ini = do pure BadgeIssuerKey {keyIdx, secretKey} _ -> Left "issuer.dev_keys must be : entries separated by commas" onLocalChain NamesConfig {nChain = ChainConfig {cChainId}} = cChainId == localChainId + onTestChain NamesConfig {nChain = ChainConfig {cChainId}} = cChainId `elem` [localChainId, sepoliaChainId] btcpaySection | not (hasSection "btcpay") = Right Nothing | otherwise = do diff --git a/apps/simplex-store-service/src/StoreService/Names/Chain.hs b/apps/simplex-store-service/src/StoreService/Names/Chain.hs index 04c66d4106..82103a1300 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Chain.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Chain.hs @@ -26,6 +26,7 @@ module StoreService.Names.Chain maxPriorityFee, signTransaction, decodeSignedTx, + transactionSigner, sendRawTransaction, fetchText, httpFailureDetail, @@ -60,8 +61,9 @@ import Network.HTTP.Client.TLS (newTlsManager) import Network.HTTP.Types (hContentType, methodPost, statusCode, statusIsSuccessful) import Numeric (readHex, showHex) import Simplex.Messaging.Crypto (keccak256) -import Simplex.Messaging.Eth.Address (Address, addressFromPrivateKey) -import Simplex.Messaging.Eth.RLP (RLPItem (..)) +import Simplex.Messaging.Crypto.Secp256k1 (RecoverableSignature (..)) +import Simplex.Messaging.Eth.Address (Address, addressFromPrivateKey, recoverAddress) +import Simplex.Messaging.Eth.RLP (RLPItem (..), rlpEncode) import Simplex.Messaging.Eth.Transaction (Eip1559Tx (..), signEip1559Tx) import Simplex.Messaging.Util (tshow) import StoreService.Config (ChainConfig (..)) @@ -216,20 +218,42 @@ signTransaction g ChainClient {ccConfig = ChainConfig {cChainId, cServiceKey}} T eip1559TxType :: Word8 eip1559TxType = 2 +signatureScalarBytes :: Int +signatureScalarBytes = 32 + -- | The request a raw transaction from 'signTransaction' was signed from. decodeSignedTx :: ByteString -> Either String TxRequest -decodeSignedTx raw = case B.uncons raw of - Just (txType, encoded) | txType == eip1559TxType -> case rlpItem encoded of - Right (RLPList [_chainId, nonce, tip, maxFee, gas, RLPBytes to, _value, RLPBytes trData, _accessList, _yParity, _r, _s], "") -> do +decodeSignedTx raw = + signedTxItems raw >>= \case + ([_chainId, nonce, tip, maxFee, gas, RLPBytes to, _value, RLPBytes trData, _accessList], _) -> do trTo <- maybe (Left "transaction: not an address") Right $ addressFromBytes to TxRequest trTo trData <$> scalar nonce <*> scalar gas <*> scalar maxFee <*> scalar tip + _ -> Left "transaction: not a signed EIP-1559 transaction" + +scalar :: RLPItem -> Either String Word64 +scalar = \case + RLPBytes b | B.length b <= 8 -> Right $ fromInteger $ os2ip b + _ -> Left "transaction: not a 64-bit scalar" + +transactionSigner :: ByteString -> IO (Either String (Word64, Address)) +transactionSigner raw = case signedTxItems raw of + Right (fields@(chainIdItem : _), (RLPBytes yParity, RLPBytes r, RLPBytes s)) + | B.length yParity <= 1, + Right chainId <- scalar chainIdItem -> + fmap (chainId,) <$> recoverAddress RecoverableSignature {rsR = scalarBytes r, rsS = scalarBytes s, rsRecId = fromInteger $ os2ip yParity} (keccak256 $ B.cons eip1559TxType $ rlpEncode $ RLPList fields) + Right _ -> pure $ Left "transaction: not a chain id and signature" + Left e -> pure $ Left e + where + scalarBytes b = B.replicate (signatureScalarBytes - B.length b) 0 <> b + +signedTxItems :: ByteString -> Either String ([RLPItem], (RLPItem, RLPItem, RLPItem)) +signedTxItems raw = case B.uncons raw of + Just (txType, encoded) | txType == eip1559TxType -> case rlpItem encoded of + Right (RLPList [chainId, nonce, tip, maxFee, gas, to, value, txData, accessList, yParity, r, s], "") -> + Right ([chainId, nonce, tip, maxFee, gas, to, value, txData, accessList], (yParity, r, s)) Right _ -> Left "transaction: not a signed EIP-1559 transaction" Left e -> Left e _ -> Left "transaction: not an EIP-1559 transaction" - where - scalar = \case - RLPBytes b | B.length b <= 8 -> Right $ fromInteger $ os2ip b - _ -> Left "transaction: not a 64-bit scalar" rlpItem :: ByteString -> Either String (RLPItem, ByteString) rlpItem s = case B.uncons s of diff --git a/apps/simplex-store-service/src/StoreService/Names/Sender.hs b/apps/simplex-store-service/src/StoreService/Names/Sender.hs index 9acf7b0c94..cf2c96945a 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Sender.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Sender.hs @@ -12,6 +12,7 @@ module StoreService.Names.Sender ptHash, ptNonce, newSender, + checkPendingSignatures, submitTransaction, inFlightTransaction, awaitOutcome, @@ -43,11 +44,12 @@ import qualified Data.Text as T import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime) import Data.Word (Word64) import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) -import Simplex.Messaging.Eth.Address (Address) +import Simplex.Messaging.Eth.Address (Address, addressFromPrivateKey) import Simplex.Messaging.Util (tshow) import StoreService.Config (ChainConfig (..)) import StoreService.Names.Chain import StoreService.Names.Store (NameTransaction (..), NewTransaction (..), TxFor, TxStatus (..), insertTransaction, pendingTransactions, recordTransactionOutcome) +import StoreService.Names.View (addressText) import StoreService.Safely (safely) import StoreService.Store.Invoices (StoreDecodeError (..)) @@ -148,6 +150,20 @@ newSender snRandom snChain snStore snNow = do } ) +checkPendingSignatures :: TVar ChaChaDRG -> ChainConfig -> DBStore -> IO (Either String ()) +checkPendingSignatures g ChainConfig {cChainId, cServiceKey} st = do + registrar <- addressFromPrivateKey g cServiceKey + saved <- withTransaction st pendingTransactions + runExceptT $ forM_ saved $ \NameTransaction {ntNonce, ntRaw} -> do + (chainId, signer) <- withExceptT ("name_transactions.raw: " <>) $ ExceptT $ transactionSigner ntRaw + when (chainId /= cChainId) $ throwError $ refusal ntNonce ("for chain " <> show chainId) ("for chain.chain_id " <> show cChainId) + when (signer /= registrar) $ throwError $ refusal ntNonce ("by " <> shownAddress signer) ("by " <> shownAddress registrar <> " of chain.service_key") + where + shownAddress = T.unpack . addressText + refusal nonce signedFor configured = + "a pending transaction of nonce " <> show nonce <> " was signed " <> signedFor <> ", not " <> configured + <> "; start with the key and chain that signed it until no transaction is pending" + -- | Dry run, the next nonce, the market's fees, the signature, saved, then sent. A failed send is retried by 'followTransactions'. submitTransaction :: Sender -> TxFor -> Address -> ByteString -> IO (Either SendError PendingTx) submitTransaction s@Sender {snChain = c@ChainClient {ccRegistrar}, snRandom, snStore, snNow, snQueue, snNextNonce, snInFlight} for to calldata = @@ -159,12 +175,15 @@ submitTransaction s@Sender {snChain = c@ChainClient {ccRegistrar}, snRandom, snS nonce <- max used <$> liftIO (readTVarIO snNextNonce) market@(tip, baseFee) <- chain $ marketFees c let needed = baseFee + tip - cap = feeCap c + gasLimit = gasWithHeadroom c gas + (cap, bounded) = feeCap c gasLimit when (needed > cap) $ do - logWarn $ "names sender: the latest block needs " <> tshow needed <> " wei per gas, above max_fee_per_gas " <> tshow cap <> "; the transaction waits" + logWarn $ "names sender: the latest block needs " <> tshow needed <> " wei per gas, above the fee cap " <> tshow cap <> "; the transaction waits" throwError $ SEFeeAboveCap needed + when (bounded && headroomFee market > cap) $ + logNote $ "names sender: nonce " <> tshow nonce <> " at max fee " <> tshow cap <> ", capped so that its gas limit " <> tshow gasLimit <> " costs at most 1 ETH" -- both fit Word64: the tip is at most the needed fee, and the max fee at most the cap - let request = TxRequest {trTo = to, trData = calldata, trNonce = nonce, trGasLimit = gasWithHeadroom c gas, trMaxFeePerGas = fromInteger $ min cap $ headroomFee market, trMaxPriorityFeePerGas = fromInteger tip} + let request = TxRequest {trTo = to, trData = calldata, trNonce = nonce, trGasLimit = gasLimit, trMaxFeePerGas = fromInteger $ min cap $ headroomFee market, trMaxPriorityFeePerGas = fromInteger tip} SignedTx {stRaw, stHash} <- withExceptT SESigning $ ExceptT $ signTransaction snRandom c request liftIO $ do now <- snNow @@ -239,17 +258,18 @@ resend s@Sender {snChain = c, snNow} nonce tx@InFlight {ifRaw, ifHashes = newest Left e -> warnNonce s nonce $ tshow e replace :: Sender -> Word64 -> InFlight -> UTCTime -> IO Bool -replace s@Sender {snChain = c, snRandom, snStore} nonce InFlight {ifFor, ifRequest = old@TxRequest {trMaxFeePerGas, trMaxPriorityFeePerGas}} now = +replace s@Sender {snChain = c, snRandom, snStore} nonce InFlight {ifFor, ifRequest = old@TxRequest {trGasLimit, trMaxFeePerGas, trMaxPriorityFeePerGas}} now = marketFees c >>= \case Left e -> False <$ warnNonce s nonce (tshow e) Right market@(tip, _) | tip <= oldTip && headroomFee market <= oldMax -> False <$ update s nonce (\t -> t {ifReplaceAt = later}) | bumped oldMax > cap -> do update s nonce $ \t -> t {ifReplaceAt = later} - False <$ warnNonce s nonce ("not replaced: a replacement would pay over max_fee_per_gas " <> tshow cap) + False <$ warnNonce s nonce ("not replaced: a replacement would pay over the fee cap " <> tshow cap) | otherwise -> do -- the bumped fees are at most the cap, so the replacement still outbids the old signature - let maxFee' = min cap $ max (headroomFee market) (bumped oldMax) + let wanted = max (headroomFee market) (bumped oldMax) + maxFee' = min cap wanted tip' = min maxFee' $ max tip (bumped oldTip) request = old {trMaxPriorityFeePerGas = fromInteger tip', trMaxFeePerGas = fromInteger maxFee'} signTransaction snRandom c request >>= \case @@ -257,13 +277,15 @@ replace s@Sender {snChain = c, snRandom, snStore} nonce InFlight {ifFor, ifReque Right SignedTx {stRaw, stHash} -> do saveTransaction snStore ifFor stRaw stHash nonce now update s nonce $ \t -> t {ifRequest = request, ifRaw = stRaw, ifHashes = stHash <| ifHashes t, ifReplaceAt = later} - logNote $ "names sender: replacing nonce " <> tshow nonce <> " at max fee " <> tshow maxFee' + logNote $ + "names sender: replacing nonce " <> tshow nonce <> " at max fee " <> tshow maxFee' + <> (if bounded && wanted > cap then ", capped so that its gas limit " <> tshow trGasLimit <> " costs at most 1 ETH" else "") True <$ broadcast s nonce stRaw where later = addUTCTime replaceAfter now oldTip = toInteger trMaxPriorityFeePerGas oldMax = toInteger trMaxFeePerGas - cap = feeCap c + (cap, bounded) = feeCap c trGasLimit -- | The confirmed hash gets its receipt's status; every other hash of the nonce can no longer be mined. settle :: Sender -> Word64 -> InFlight -> TxOutcome -> IO () @@ -300,8 +322,14 @@ headroomFee (tip, baseFee) = baseFeeHeadroom * baseFee + tip gasWithHeadroom :: ChainClient -> Word64 -> Word64 gasWithHeadroom ChainClient {ccConfig = ChainConfig {cGasLimitCap}} gas = min cGasLimitCap (gas + gas `div` 5) -feeCap :: ChainClient -> Integer -feeCap ChainClient {ccConfig = ChainConfig {cMaxFeePerGas}} = toInteger cMaxFeePerGas +feeCap :: ChainClient -> Word64 -> (Integer, Bool) +feeCap ChainClient {ccConfig = ChainConfig {cMaxFeePerGas}} gasLimit = (min configured bound, bound < configured) + where + configured = toInteger cMaxFeePerGas + bound = nodeTxFeeCapWei `div` toInteger (max 1 gasLimit) + +nodeTxFeeCapWei :: Integer +nodeTxFeeCapWei = 1000000000000000000 saveTransaction :: DBStore -> TxFor -> ByteString -> TxHash -> Word64 -> UTCTime -> IO () saveTransaction st for raw hash nonce now = diff --git a/apps/simplex-store-service/src/StoreService/Service.hs b/apps/simplex-store-service/src/StoreService/Service.hs index a4327b02a5..6678440516 100644 --- a/apps/simplex-store-service/src/StoreService/Service.hs +++ b/apps/simplex-store-service/src/StoreService/Service.hs @@ -58,7 +58,7 @@ import StoreService.Names.Env (NamesEnv (..), newNamesEnv) import StoreService.Names.Orders (ordersEnv) import StoreService.Names.Phases (RegistrationId (..)) import StoreService.Names.Records (runRecordEdits) -import StoreService.Names.Sender (runSender) +import StoreService.Names.Sender (checkPendingSignatures, runSender) import StoreService.Names.Store (Balance (..), BalanceHold (..), BalanceLink (..), LateCredit (..), RegistrationPayment (..), balanceByReference, balanceChain, methodText) import StoreService.Names.Turns (Turns, newTurns) import StoreService.Names.View (addressText) @@ -305,7 +305,7 @@ storeService opts serviceCfg baseCfg env = do raceAny_ $ processQueuedRequests key trackerQ_ requestEnv : maybeToList groupLane_ <> lanes chatHooks = defaultChatHooks - { preStartHook = Just $ badgePreStartHook opts, + { preStartHook = Just $ badgePreStartHook opts (serviceCfg >>= names), postStartHook = Just $ badgePostStartHook opts env, preCmdHook = Just $ badgeCmdHook turns } @@ -393,7 +393,7 @@ storeServiceCLI opts serviceCfg = do ev -> pure ev chatHooks = defaultChatHooks - { preStartHook = Just $ badgePreStartHook opts, + { preStartHook = Just $ badgePreStartHook opts Nothing, postStartHook = Just $ badgePostStartHook opts env, preCmdHook = Just $ badgeCmdHook turns, eventHook = Just eventHook @@ -629,9 +629,10 @@ routeLookahead = 60 survive :: T.Text -> IO () -> IO () survive what action = action `catchOwn'` \_ -> logError $ "store service: " <> what <> " failed; the next one will be handled" -badgePreStartHook :: StoreServiceOpts -> ChatController -> IO () -badgePreStartHook opts ChatController {config, chatStore} = +badgePreStartHook :: StoreServiceOpts -> Maybe NamesConfig -> ChatController -> IO () +badgePreStartHook opts namesCfg_ ChatController {config, chatStore, random} = do runStoreServiceMigrations opts config chatStore + forM_ namesCfg_ $ \NamesConfig {nChain} -> checkPendingSignatures random nChain chatStore >>= orExit badgePostStartHook :: StoreServiceOpts -> ServiceState -> ChatController -> IO () badgePostStartHook StoreServiceOpts {noAddress, testing} env cc = do diff --git a/apps/simplex-store-service/src/StoreService/Store/Migrate.hs b/apps/simplex-store-service/src/StoreService/Store/Migrate.hs index 86ca368c4a..0c0f7a214a 100644 --- a/apps/simplex-store-service/src/StoreService/Store/Migrate.hs +++ b/apps/simplex-store-service/src/StoreService/Store/Migrate.hs @@ -7,6 +7,7 @@ module StoreService.Store.Migrate ( runStoreServiceMigrations, withInstanceLock, + instanceLockCheckInterval, ) where @@ -18,13 +19,18 @@ import Simplex.Messaging.Agent.Store.Common import Simplex.Messaging.Agent.Store.Interface (migrateDBSchema) import Simplex.Messaging.Agent.Store.Shared (MigrationConfig (..), MigrationConfirmation (..)) import System.Exit (exitFailure) +import System.IO (hFlush, stdout) #if defined(dbPostgres) +import Control.Concurrent (threadDelay) +import Control.Concurrent.Async (race) import Control.Exception (bracket) +import Control.Monad (void) import qualified Data.ByteString.Char8 as B import Data.Int (Int64) import Database.PostgreSQL.Simple (Only (..)) import qualified Database.PostgreSQL.Simple as PSQL +import Simplex.Messaging.Util (catchOwn') import StoreService.Store.Postgres.Migrations #else import StoreService.Store.SQLite.Migrations @@ -45,20 +51,28 @@ runStoreServiceMigrations opts ChatConfig {confirmMigrations} chatStore = -- | One process per database, as a second would send with the gas key's nonces and run the chat agent's queues too: the -- lock is held on a connection of its own for the whole run. SQLite, used only in development, takes none. -withInstanceLock :: StoreServiceOpts -> IO a -> IO a +withInstanceLock :: Int -> StoreServiceOpts -> IO a -> IO a #if defined(dbPostgres) -withInstanceLock StoreServiceOpts {coreOptions = CoreChatOpts {dbOptions = ChatDbOpts {dbConnstr}}} action = +withInstanceLock checkInterval StoreServiceOpts {coreOptions = CoreChatOpts {dbOptions = ChatDbOpts {dbConnstr}}} action = bracket (PSQL.connectPostgreSQL $ B.pack dbConnstr) PSQL.close $ \db -> PSQL.query db "SELECT pg_try_advisory_lock(?)" (Only instanceLockKey) >>= \case - [Only True] -> action + [Only True] -> either id id <$> race (watchLock db) action _ -> exit "another store service holds this database; stop it before starting this one, --run-cli included" + where + watchLock db = do + threadDelay checkInterval + void (PSQL.query_ db "SELECT 1" :: IO [Only Int]) `catchOwn'` (exit . ("the instance lock's database connection failed: " <>) . show) + watchLock db -- | "SXSTORE" in ASCII, the lock's key in the database's advisory lock space. instanceLockKey :: Int64 instanceLockKey = 0x535853544f5245 #else -withInstanceLock _ action = action +withInstanceLock _ _ action = action #endif +instanceLockCheckInterval :: Int +instanceLockCheckInterval = 30000000 + exit :: String -> IO a -exit err = putStrLn ("Error: " <> err) >> exitFailure +exit err = putStrLn ("Error: " <> err) >> hFlush stdout >> exitFailure diff --git a/scripts/store-service/compose.yml b/scripts/store-service/compose.yml index 5644ee6f96..c211397664 100644 --- a/scripts/store-service/compose.yml +++ b/scripts/store-service/compose.yml @@ -7,7 +7,7 @@ services: args: USER_UID: ${USER_UID:-1000} USER_GID: ${USER_GID:-1000} - restart: on-failure:5 + restart: unless-stopped user: "${USER_UID:-1000}:${USER_GID:-1000}" tty: true network_mode: host diff --git a/tests/Bots/StoreService/ConfigTests.hs b/tests/Bots/StoreService/ConfigTests.hs index bf1a7f79f4..f8394feee4 100644 --- a/tests/Bots/StoreService/ConfigTests.hs +++ b/tests/Bots/StoreService/ConfigTests.hs @@ -39,6 +39,7 @@ badgeConfigTests = describe "store service config" $ do it "disables stripe when the section is absent" testStripeAbsent it "refuses an incomplete stripe section, naming the key" testStripeIncomplete it "takes a publishable key only as pk_live_ or pk_test_ and letters and digits" testStripePublishableKey + it "refuses a Stripe secret key and publishable key of different modes" testStripeKeyModes it "bounds stripe session_minutes to 1-1440" testStripeSessionMinutesRange it "accepts HighSpeed" (testSpeedPolicyAccepted "HighSpeed" HighSpeed) it "refuses HighSpeed beside [names], which settles with no confirmation" testHighSpeedWithNames @@ -201,10 +202,19 @@ fullStripeIni = testStripePublishableKey :: IO () testStripePublishableKey = do - let withKey k = parseIni (T.replace "publishable_key = pk_test_x" ("publishable_key = " <> k) fullStripeIni) - forM_ ["pk_test_51Hx", "pk_live_AbC123"] $ \k -> (\r -> (k, isRight r)) <$> withKey k `shouldReturn` (k, True) + forM_ [("rk_test_x", "pk_test_51Hx"), ("rk_live_x", "pk_live_AbC123")] $ \(s, k) -> (\r -> (k, isRight r)) <$> withStripeKeys s k `shouldReturn` (k, True) forM_ ["pk_test_", "sk_test_x", "pk_test_x\">