mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 01:27:27 +00:00
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>
This commit is contained in:
1 parent
ad75fe706f
commit
4aab0ef3a6
12 files changed
+312
-61
No files matched your search
@@ -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
|
||||
@@ -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>` (`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 <n> at max
|
||||
fee <wei>, capped so that its gas limit <gas> costs at most 1 ETH`, and a replacement it caps logs
|
||||
`[NOTE …] names sender: replacing nonce <n> at max fee <wei>, 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> wei per gas, above max_fee_per_gas <cap>;
|
||||
the transaction waits` when fees are above the cap. Nothing is signed until they fall.
|
||||
- `[WARN …] names sender: nonce <n>: not replaced: a replacement would pay over max_fee_per_gas
|
||||
<cap>` when a transaction stuck in the pool cannot be bumped within the cap.
|
||||
- `[WARN …] names sender: the latest block needs <wei> wei per gas, above the fee cap <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 <n>: not replaced: a replacement would pay over the fee cap
|
||||
<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 <n> was signed by <old address>, not by <new address> 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
|
||||
|
||||
@@ -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)).
|
||||
@@ -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 <index>:<secret> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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\"><script>", "pk_live_a-b"] $ \k ->
|
||||
withKey k `shouldReturn` Left "stripe.publishable_key must be pk_live_ or pk_test_ followed by letters and digits"
|
||||
withStripeKeys "rk_test_x" k `shouldReturn` Left "stripe.publishable_key must be pk_live_ or pk_test_ followed by letters and digits"
|
||||
|
||||
withStripeKeys :: T.Text -> T.Text -> IO (Either String ServiceConfig)
|
||||
withStripeKeys secret publishable = parseIni (fullIni <> T.unlines (devKeysStripe secret publishable))
|
||||
|
||||
testStripeKeyModes :: IO ()
|
||||
testStripeKeyModes = do
|
||||
forM_ [("sk_live_x", "pk_live_x"), ("rk_live_x", "pk_live_x"), ("sk_test_x", "pk_test_x"), ("rk_test_x", "pk_test_x")] $ \(s, k) ->
|
||||
(\r -> (s, k, isRight r)) <$> withStripeKeys s k `shouldReturn` (s, k, True)
|
||||
forM_ [("rk_live_x", "pk_test_x"), ("sk_live_x", "pk_test_x"), ("rk_test_x", "pk_live_x"), ("sk_test_x", "pk_live_x"), ("sk_test", "pk_test_x")] $ \(s, k) ->
|
||||
(\r -> (s, k, r)) <$> withStripeKeys s k `shouldReturn` (s, k, Left "stripe.secret_key and stripe.publishable_key must be both live keys or both test keys")
|
||||
|
||||
testStripeDefaults :: IO ()
|
||||
testStripeDefaults = withIni fullStripeIni $ \p -> do
|
||||
@@ -708,6 +718,7 @@ namesConfigTests = describe "StoreService names: config" $ do
|
||||
it "refuses a malformed dev_keys entry or an index named twice, quoting no secret" testDevKeysMalformed
|
||||
it "accepts [issuer] dev_keys beside Stripe test keys and a BTCPay on loopback" testDevKeysTestPayments
|
||||
it "refuses [issuer] dev_keys beside a live Stripe key or a remote BTCPay, naming which and quoting no key" testDevKeysLivePayments
|
||||
it "accepts Stripe test keys with [names] only on chain 31337 or 11155111, and live keys on any chain" testStripeTestKeysChain
|
||||
|
||||
rpcUrl :: T.Text
|
||||
rpcUrl = "https://eth.example.org/v2/rpc-token-value"
|
||||
@@ -1016,3 +1027,13 @@ testDevKeysLivePayments = do
|
||||
devKeysWith (devKeysBTCPay "https://127.0.0.1" <> devKeysStripe "sk_live_secretvalue" "pk_test_x") `shouldReturn` Left liveSecret
|
||||
devKeysWith (devKeysStripe "sk_live_secretvalue" "pk_test_x") >>= either (`shouldNotContain` "secretvalue") (const (expectationFailure "a live key must not boot"))
|
||||
parseIni (localNamesIni (chainSection <> devKeysBTCPay "https://btcpay.example.org" <> devKeysStripe "sk_live_x" "pk_live_x")) >>= (`shouldSatisfy` either (const False) (const True))
|
||||
|
||||
testStripeTestKeysChain :: IO ()
|
||||
testStripeTestKeysChain = do
|
||||
let onChain chainId s k = parseIni (localNamesIni (withChainId chainId <> devKeysStripe s k))
|
||||
refused = "Stripe test keys are accepted with [names] only on a test chain, [chain] chain_id = 31337 or 11155111" :: String
|
||||
forM_ ["31337", "11155111"] $ \chainId ->
|
||||
(\r -> (chainId, isRight r)) <$> onChain chainId "rk_test_x" "pk_test_x" `shouldReturn` (chainId, True)
|
||||
forM_ ["1", "31338", "11155112"] $ \chainId -> do
|
||||
(\r -> (chainId, r)) <$> onChain chainId "sk_test_x" "pk_test_x" `shouldReturn` (chainId, Left refused)
|
||||
(\r -> (chainId, isRight r)) <$> onChain chainId "rk_live_x" "pk_live_x" `shouldReturn` (chainId, True)
|
||||
@@ -10,13 +10,14 @@
|
||||
-- Members' messages reach a console over separate connections in no fixed order, so gating is asserted from the bot's store.
|
||||
module Bots.StoreService.GroupIntegrationTests (badgeGroupIntegrationTests, namesBootTests, namesCodesChatTests, namesRefundChatTests, namesWorkerBootTests) where
|
||||
|
||||
import StoreService.Config (BadgeIssuerKey (..), GroupConfig (..), ServiceConfig (devAcceptUnverifiedStoreReceipts), readServiceConfig)
|
||||
import StoreService.Config (BadgeIssuerKey (..), ChainConfig (cController, cServiceKey), GroupConfig (..), NamesConfig (nChain), ServiceConfig (devAcceptUnverifiedStoreReceipts, names), readServiceConfig)
|
||||
import StoreService.Group (GroupAction (..), GroupEvent (..), codeInTracker, ensureManagedGroup)
|
||||
import StoreService.Group.Command (CmdAction (..), groupCmdAction, maxUses)
|
||||
import StoreService.Names.Abi (selector, uint256)
|
||||
import StoreService.Names.Codes (issueNameCode, reserveCode, revokeNameCode)
|
||||
import StoreService.Names.Phases (RegistrationId (..))
|
||||
import StoreService.Names.Store (PrepaidCode (..), getPrepaidCode, spendLandedPrepaidCode)
|
||||
import StoreService.Names.Chain (SignedTx (..), TxRequest (..), newChainClient, signTransaction)
|
||||
import StoreService.Names.Store (NewTransaction (..), PrepaidCode (..), TxFor (..), getPrepaidCode, insertTransaction, spendLandedPrepaidCode)
|
||||
import StoreService.Names.Turns (newTurns, withCodeTurn)
|
||||
import StoreService.Options (StoreServiceOpts (..))
|
||||
import StoreService.Orders (cancelOpenInvoice)
|
||||
@@ -30,7 +31,7 @@ import Bots.StoreService.FakeEthNode (CallAnswer (..), FakeEthNode (..), Fault (
|
||||
import Bots.StoreService.WebTests (capturingStderr, digestFixture)
|
||||
import Bots.StoreService.NamesSearchTests (aliceHex)
|
||||
import qualified Bots.StoreService.NamesBalancesTests as Balances
|
||||
import Bots.StoreService.NamesStoreTests (order, ownerA, placeCode, placePaid, t0)
|
||||
import Bots.StoreService.NamesStoreTests (issue, order, ownerA, placeCode, placePaid, t0, usable)
|
||||
import ChatClient
|
||||
import ChatTests.DBUtils
|
||||
import ChatTests.Utils
|
||||
@@ -41,6 +42,7 @@ import Control.Concurrent.STM (TQueue, atomically, newTQueueIO, orElse, readTMVa
|
||||
import Control.Exception (IOException, bracket, catch, finally, throwIO, try)
|
||||
import Control.Monad (forM_, guard, mfilter, replicateM_, unless, void, when)
|
||||
import qualified Data.Aeson as J
|
||||
import qualified Data.ByteArray as BA
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as LB
|
||||
import Data.Either (isLeft, isRight)
|
||||
@@ -77,6 +79,7 @@ import Simplex.Messaging.Agent.Protocol (AConnShortLink (..), ConnShortLink (..)
|
||||
import Simplex.Messaging.Agent.Store.Common (withTransaction)
|
||||
import qualified Simplex.Messaging.Agent.Store.DB as DB
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import Simplex.Messaging.Crypto.Secp256k1 (mkPrivateKey)
|
||||
import Simplex.Messaging.Crypto.BBS (bbsKeyGen)
|
||||
import Simplex.Messaging.Encoding.String (strDecode)
|
||||
import Simplex.Messaging.Util (tshow)
|
||||
@@ -86,6 +89,7 @@ import System.FilePath ((</>))
|
||||
import System.Timeout (timeout)
|
||||
#if defined(dbPostgres)
|
||||
import Database.PostgreSQL.Simple (FromRow, Only (..))
|
||||
import qualified Database.PostgreSQL.Simple as PSQL
|
||||
#else
|
||||
import Database.SQLite.Simple (FromRow, Only (..))
|
||||
#endif
|
||||
@@ -151,7 +155,8 @@ namesBootTests = do
|
||||
it "refuses a node serving another chain than chain_id, and dev_keys unless the node answers, only warning without them" testNodeChain
|
||||
it "masks the database password in the start line, in a URI or as a keyword" testRedactPassword
|
||||
it "refuses unverified store receipts with an issuer key clients trust, and takes them with a dev key" testUnverifiedReceiptsKey
|
||||
it "refuses a second instance on the same Postgres database until the first ends, and takes no lock on SQLite" testInstanceLock
|
||||
it "refuses a second instance on the same Postgres database until the first ends, exits when the lock's connection fails, and takes no lock on SQLite" testInstanceLock
|
||||
it "exits before the chat core starts while a pending transaction was signed by another key" testOtherKeyPendingStops
|
||||
it "boots codes-only, refusing paid name orders 403 codes_only and answering code orders and code checks, the badges shell as built" testNamesBootCodesOnly
|
||||
it "boots with [names] and [chain], routing fixed /api/name paths ahead of a search, find ahead of registrations, the community terms and registrations to GET, another name, a reveal, a spend and a retry to POST, a path no route knows and a taken order's code to 404, and each route under its limit, shared by the routes of that limit" testNamesBoot
|
||||
|
||||
@@ -170,13 +175,47 @@ testDevKeysWarned _ = do
|
||||
testInstanceLock :: HasCallStack => TestParams -> IO ()
|
||||
testInstanceLock ps = do
|
||||
Right (_, sk) <- bbsKeyGen
|
||||
let opts = mkStoreServiceOpts ps sk
|
||||
let locked :: IO a -> IO a
|
||||
locked = withInstanceLock 100000 (mkStoreServiceOpts ps sk)
|
||||
#if defined(dbPostgres)
|
||||
withInstanceLock opts (withInstanceLock opts (pure ()) `shouldThrow` (== ExitFailure 1))
|
||||
locked (locked (pure ()) `shouldThrow` (== ExitFailure 1))
|
||||
locked (threadDelay 500000 >> pure True) `shouldReturn` True
|
||||
locked (terminateInstanceLockSession >> threadDelay 5000000) `shouldThrow` (== ExitFailure 1)
|
||||
#else
|
||||
withInstanceLock opts (withInstanceLock opts (pure ()))
|
||||
locked (locked (pure ()))
|
||||
#endif
|
||||
locked (pure True) `shouldReturn` True
|
||||
|
||||
testOtherKeyPendingStops :: HasCallStack => TestParams -> IO ()
|
||||
testOtherKeyPendingStops ps = do
|
||||
svc <- (\s -> s {gsIniSections = "" : namesSection <> chainSection}) <$> prepareGroupService ps
|
||||
(opts, serviceCfg, _) <- groupServiceOpts svc Nothing
|
||||
Just chainCfg <- pure $ nChain <$> names serviceCfg
|
||||
g <- C.newRandom
|
||||
otherKey <- newChainClient g chainCfg {cServiceKey = either error id $ mkPrivateKey $ BA.replicate 32 0x47}
|
||||
Right SignedTx {stRaw, stHash} <-
|
||||
signTransaction g otherKey TxRequest {trTo = cController chainCfg, trData = "", trNonce = 0, trGasLimit = 100000, trMaxFeePerGas = 1, trMaxPriorityFeePerGas = 1}
|
||||
(t, cc, _, _) <- startGroupService svc Nothing
|
||||
flip finally (stopGroupService t cc) $ do
|
||||
code <- issue (chatStore cc) 1 usable
|
||||
placeCode (chatStore cc) "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right ()
|
||||
withTransaction (chatStore cc) $ \db -> void $ insertTransaction db (TxCommit $ RegistrationId "r1") NewTransaction {ntxRaw = stRaw, ntxHash = stHash, ntxNonce = 0} t0
|
||||
env <- newServiceState
|
||||
ended <- timeout waitLimit (try $ storeService opts (Just serviceCfg) (gsCfg svc) env)
|
||||
started <- atomically (tryReadTMVar (serviceCC env))
|
||||
mapM_ stopChat started
|
||||
(isNothing started, ended) `shouldBe` (True, Just (Left (ExitFailure 1)))
|
||||
|
||||
#if defined(dbPostgres)
|
||||
terminateInstanceLockSession :: IO ()
|
||||
terminateInstanceLockSession =
|
||||
bracket (PSQL.connect testDBConnectInfo) PSQL.close $ \db ->
|
||||
PSQL.query
|
||||
db
|
||||
"SELECT pg_terminate_backend(pid) FROM pg_locks WHERE locktype = 'advisory' AND granted AND database = (SELECT oid FROM pg_database WHERE datname = current_database()) AND ((classid::bigint << 32) | objid::bigint) = ?"
|
||||
(Only (0x535853544f5245 :: Int64))
|
||||
`shouldReturn` [Only True]
|
||||
#endif
|
||||
withInstanceLock opts (pure True) `shouldReturn` True
|
||||
|
||||
testUnverifiedReceiptsKey :: HasCallStack => TestParams -> IO ()
|
||||
testUnverifiedReceiptsKey _ = do
|
||||
|
||||
@@ -11,6 +11,7 @@ import Control.Concurrent.Async (concurrently)
|
||||
import Control.Concurrent.STM
|
||||
import Control.Monad (forM_, replicateM_)
|
||||
import Crypto.Random (ChaChaDRG)
|
||||
import qualified Data.ByteArray as BA
|
||||
import Data.ByteArray.Encoding (Base (Base16), convertFromBase)
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as B
|
||||
@@ -22,6 +23,8 @@ import Data.Word (Word64, Word8)
|
||||
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
|
||||
import qualified Simplex.Messaging.Agent.Store.DB as DB
|
||||
import qualified Simplex.Messaging.Crypto as C
|
||||
import Simplex.Messaging.Crypto.Secp256k1 (mkPrivateKey)
|
||||
import Simplex.Messaging.Util (tshow)
|
||||
import StoreService.Config (ChainConfig (..))
|
||||
import StoreService.Names.Chain
|
||||
import StoreService.Names.Phases (RegistrationId (..))
|
||||
@@ -29,6 +32,7 @@ import StoreService.Names.Registration (commitCalldata)
|
||||
import StoreService.Names.Revert (RevertName (..))
|
||||
import StoreService.Names.Sender
|
||||
import StoreService.Names.Store (NameTransaction (..), TxFor (..), TxStatus (..), pendingTransactions, registrationTransactions)
|
||||
import StoreService.Names.View (addressText)
|
||||
import System.Timeout (timeout)
|
||||
import Test.Hspec
|
||||
|
||||
@@ -43,11 +47,13 @@ namesSenderTests = describe "StoreService names: sender" $ do
|
||||
it "keeps a transaction whose send failed, and sends it in the next round" $ testFailedSend g
|
||||
it "replaces a stuck transaction once the market's fees pass its own, and settles it under the new hash" $ testFeeBump g
|
||||
it "rebroadcasts a saved raw transaction after a restart while the node does not know it" $ testRestartRebroadcast g
|
||||
it "refuses a start on another key or chain while a transaction is pending, naming both, and accepts the new key once none is" $ testRotatedKeyStart g
|
||||
it "hands a dropped transaction whose nonce was used, or a mined one whose receipt was pruned, to its owner, who may send again at the next nonce" $ testDroppedNonceUsed g
|
||||
it "logs a round that fails on the store, and settles the transaction in the next round" $ testFailedRound g
|
||||
it "logs a send the node keeps refusing once per error, an underfunded key as an error, in anvil's capitalized form too" $ testRefusedSendLogged g
|
||||
it "refuses a transaction whose block needs more than the fee cap, saving nothing, and signs at most the cap" $ testFeeCapSubmit g
|
||||
it "clamps a replacement at the fee cap, and keeps rebroadcasting once a replacement would pass it" $ testFeeCapReplace g
|
||||
it "caps the max fee of a transaction and of its replacement so that its gas limit costs at most 1 ETH" $ testTxFeeBound g
|
||||
|
||||
data Setup = Setup
|
||||
{ node :: FakeEthNode,
|
||||
@@ -85,6 +91,12 @@ testDecodeSignedTx g = withFakeEthNode $ \node -> do
|
||||
decodeSignedTx stRaw `shouldBe` Right request
|
||||
decodeSignedTx (B.cons 0x01 $ B.drop 1 stRaw) `shouldBe` Left "transaction: not an EIP-1559 transaction"
|
||||
decodeSignedTx (B.take 100 stRaw) `shouldBe` Left "RLP: truncated"
|
||||
transactionSigner (B.take 100 stRaw) `shouldReturn` Left "RLP: truncated"
|
||||
forM_ [0 .. 15] $ \nonce -> do
|
||||
Right SignedTx {stRaw = raw} <- signTransaction g c request {trNonce = nonce}
|
||||
(\signer -> (nonce, signer)) <$> transactionSigner raw `shouldReturn` (nonce, Right (fakeChainId, ccRegistrar c))
|
||||
ccRegistrar c `shouldBe` address "0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F"
|
||||
transactionSigner viemTxYParity1ShortS `shouldReturn` Right (fakeChainId, ccRegistrar c)
|
||||
|
||||
testConcurrentNonces :: TVar ChaChaDRG -> IO ()
|
||||
testConcurrentNonces g = withSetup g $ \s@Setup {node, store} -> do
|
||||
@@ -210,6 +222,27 @@ testRestartRebroadcast g = withSetup g $ \s@Setup {node, store} -> do
|
||||
outcomeOf resumed >>= (`shouldSatisfy` confirmedAs (ptHash p))
|
||||
map (\(_, status, _) -> status) <$> statuses store r1 `shouldReturn` [TSMined]
|
||||
|
||||
testRotatedKeyStart :: TVar ChaChaDRG -> IO ()
|
||||
testRotatedKeyStart g = withSetup g $ \s@Setup {node, store, chain = oldChain} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
let oldConfig = ccConfig oldChain
|
||||
newConfig = oldConfig {cServiceKey = either error id $ mkPrivateKey $ BA.replicate 32 0x47}
|
||||
startOn cfg = checkPendingSignatures g cfg store
|
||||
refusal signedFor configured =
|
||||
Left $ "a pending transaction of nonce 0 was signed " <> signedFor <> ", not " <> configured <> "; start with the key and chain that signed it until no transaction is pending"
|
||||
newRegistrar <- ccRegistrar <$> newChainClient g newConfig
|
||||
startOn oldConfig `shouldReturn` Right ()
|
||||
startOn newConfig `shouldReturn` refusal ("by " <> shown (ccRegistrar oldChain)) ("by " <> shown newRegistrar <> " of chain.service_key")
|
||||
startOn oldConfig {cChainId = 1} `shouldReturn` refusal ("for chain " <> show fakeChainId) "for chain.chain_id 1"
|
||||
mineHash node (ptHash p)
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
outcomeOf p >>= (`shouldSatisfy` confirmedAs (ptHash p))
|
||||
startOn newConfig `shouldReturn` Right ()
|
||||
where
|
||||
shown = T.unpack . addressText
|
||||
|
||||
testDroppedNonceUsed :: TVar ChaChaDRG -> IO ()
|
||||
testDroppedNonceUsed g =
|
||||
forM_ [\node h -> dropTransaction node h >> useNextNonce node, \node h -> mine node [(h, True)] >> pruneReceipt node h] $ \lose ->
|
||||
@@ -316,6 +349,32 @@ testFeeCapReplace g = withCapSetup cap g $ \s@Setup {node} -> do
|
||||
-- above the first max fee and its bump, below the market's headroom fee and a second bump
|
||||
cap = 2 * fakeBaseFee + fakeTip + 1000000000
|
||||
|
||||
testTxFeeBound :: TVar ChaChaDRG -> IO ()
|
||||
testTxFeeBound g = withCapSetup 10000000000000 g $ \s@Setup {node} -> do
|
||||
let gasLimit = fakeGas + fakeGas `div` 5 :: Word64
|
||||
bound = fromInteger $ 1000000000000000000 `div` toInteger gasLimit :: Word64
|
||||
sender <- start s
|
||||
setFees node fakeTip 5000000000000
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEFeeAboveCap (5000000000000 + fakeTip)
|
||||
setFees node fakeTip 1000000000000
|
||||
Right _ <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
[firstRaw] <- sentTransactions node
|
||||
Right first <- pure $ decodeSignedTx firstRaw
|
||||
(trGasLimit first, trMaxFeePerGas first) `shouldBe` (gasLimit, 2 * 1000000000000 + fakeTip)
|
||||
setFees node fakeTip 3000000000000
|
||||
passTime s replaceAfter
|
||||
logged <- capturingStderr $ followTransactions sender
|
||||
logged `shouldSatisfy` T.isInfixOf ("names sender: replacing nonce 0 at max fee " <> tshow bound <> ", capped so that its gas limit " <> tshow gasLimit <> " costs at most 1 ETH")
|
||||
[_, bumpedRaw] <- sentTransactions node
|
||||
decodeSignedTx bumpedRaw `shouldBe` Right first {trMaxPriorityFeePerGas = fakeTip + fakeTip `div` 8 + 1, trMaxFeePerGas = bound}
|
||||
passTime s replaceAfter
|
||||
followTransactions sender
|
||||
length <$> sentTransactions node `shouldReturn` 2
|
||||
Right _ <- submitTransaction sender (TxCommit r2) fakeController (commit 2)
|
||||
[_, _, cappedRaw] <- sentTransactions node
|
||||
(\TxRequest {trGasLimit, trMaxFeePerGas} -> (trGasLimit, trMaxFeePerGas)) <$> decodeSignedTx cappedRaw `shouldBe` Right (gasLimit, bound)
|
||||
toInteger gasLimit * toInteger bound `shouldSatisfy` (<= 1000000000000000000)
|
||||
|
||||
-- fixtures
|
||||
|
||||
r1, r2 :: RegistrationId
|
||||
@@ -357,3 +416,6 @@ refusedWith submit expected =
|
||||
|
||||
unhex :: ByteString -> ByteString
|
||||
unhex = either error id . convertFromBase Base16
|
||||
|
||||
viemTxYParity1ShortS :: ByteString
|
||||
viemTxYParity1ShortS = unhex "02f86f827a693b843b9aca00850737be76008306ddd0945fbdb2315678afecb367f032d93f642f64180aa38082abcdc001a03e704053d6725b7b06476fca04827611f81b637f38b5ab351d62c236252ac06e9f9738e8cce6189740f438c699b4b77b8f9c4a15376ed3b3ef48fea57c459ed8"
|
||||
Reference in new issue
Block a user