mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 15:38:23 +00:00
* core: add prepaid name codes and length bounds * store: run the names migration after badges * store: issue and revoke prepaid name codes * store: document issuing prepaid name codes * store: move the ABI helpers to Names.Abi * store: read the [names] and [chain] settings * store: add the names chain client and reads * store: refuse group name codes while names are off * store: search names and preview links * store: serve each site on its own port * scripts: deploy the badges and names sites * store: document search and the two sites * store: add the drop_taken_codes migration * store: read the names codes_only setting * store: add the names store and order signatures * store: take name orders by card, crypto or code * store: document name orders and codes-only * store: add the names transaction sender * store: commit and register queued orders * store: reveal and retry an owner's order * store: document the gas key * store: add the name balance migrations * store: read crypto units, rates and confirmations * store: keep a taken order's money as a balance * store: spend a balance on another name * store: record refunds with //refund * store: document balances and refunds * store: add the agreed terms migration * store: read the terms and community names * store: answer community and terms in search * store: sign and store the agreed terms * store: document the terms and community names * store: add the invoiced units migration * store: read the crypto an invoice asks * store: credit late and overpaid crypto by share * store: document crediting late crypto by share * store: add the record changes migration * store: find an owner's orders and move one * store: relay owners' link edits on chain * store: document link edits * store: move isLoopbackHost to Config * store: trust dev issuer keys on the local chain * scripts: build the names site from a submodule * scripts: add the dev setup and contract run * store: document the dev setup and names image * store: send a Stripe idempotency key for names * tests: wait until a relay lets go of its edit * store: refuse a link edit deadline under 10 min * store: count reverted link edits in the year limit * store: require https for a remote chain node * store: refuse zero chain confirmations * store: set up the dev stack from the names branch * store: rerun a failed unpaid cleanup next pass * store: never replace a succeeded Stripe intent * store: refuse an ini setting written as key: value * store: note the webapp submodule's pinned branch * store: document confirmLanded judging by ownerOf * store: update the names webapp submodule * store-service: register names under the name policy, every name accepting its terms on chain (#7889) * store: accept each name's terms on chain under the name policy * ci: run the reduced build on sh/names-deploy * store: address review: policy re-read, terms reads and startup log * store: never credit an unconfirmed extra * store: alert money sent to a paid name invoice * store: refuse to start on another chain's node * store: keep orders while the allowance is used up * store: replace a stalled transaction after 3 blocks * store: judge a name taken at confirmation depth * store: bind the template's listener to loopback * store: scan only the ids of active name orders * store: refuse unknown [names] and [chain] settings * store: fetch the webapp submodule in upgrade steps * store: delete definitions only tests use * store: lock only name invoices at settlement * store: retry an unexpired commitment as usual * store: share the helpers defined twice * store: test route limits on the running service * store: fix tests that could not fail * ci: test the store service on Postgres * store: count IPv6 clients by their /64 * store: sweep invoices read alone past a list cap * store: cap checkouts of all clients together * store: cap the long polls held at once * store: stop the old badge project at upgrade * store: keep search lookups free of previews * store: read the ini as UTF-8 in any locale * ci: run Postgres store tests in a UTF-8 locale * store: cap the transactions one order sends * store: cancel a card intent after three declines * store: refuse a name price under a cent * store: send each transaction with gas headroom * store: cap searches and read terms once per block * store: never log the group join link * store: mask the database password at startup * store: run one instance per database * store: limit name codes to admins, 100 a day * store: refuse HighSpeed settlement with names * store: no unverified receipts with a trusted key * store: cap the log lines of refused webhooks * store: check Apple certificates at the signed date * store: refuse a malformed Stripe publishable key * store: check the owner before a refund * store: fold the names migrations into one * store: read onion name links through the proxy * store: refund money after paid once confirmed * store: limit the allowance wait to a dry run * store: bind loopback when upgrading * ci: cache and widen the store Postgres job * store: fix stale and duplicate names tests * store: fix the submodule path in the upgrade * store: refuse unpaid name codes on the CLI * store: apply the review's names test list * store: finish the review's names test list * store: restore the base registration tests * store: pass link hosts that do not resolve here * store: refuse a move once its sends are used up * store: take the shared checkout at the provider * store: hold a search slot for chain reads only * store: state which refunds are safe on a reference * store: check a failed order's name before refusing its move (#8060) * store: test and document the shared checkout cap for name orders * store: check a failed order's name on chain before refusing its move * store: test name links whose host lookup fails * store: test that a search holds its slot through the chain reads only * store: restore the signed transaction check in the chain tests * store: remove the chain tests' copy of the signed transaction check * store: correct the search slot texts and the README's balance endpoints * docs: system and sequence diagrams of the names service (#8068) --------- Co-authored-by: sh-agents <350651734+sh-agents@users.noreply.github.com> Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com> Co-authored-by: brenzi-agent <brenzi-agent@brenzi.ch> Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
246 lines
12 KiB
Haskell
246 lines
12 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Bots.StoreService.NamesChainTests (namesChainTests) where
|
|
|
|
import Bots.StoreService.FakeEthNode
|
|
import Control.Concurrent.STM (TVar)
|
|
import Control.Exception (toException, try)
|
|
import Control.Monad (forM_)
|
|
import Crypto.Random (ChaChaDRG)
|
|
import qualified Data.Aeson as J
|
|
import Data.ByteArray.Encoding (Base (Base16), convertFromBase)
|
|
import Data.ByteString (ByteString)
|
|
import qualified Data.ByteString as B
|
|
import qualified Data.Text as T
|
|
import Data.Time.Clock (diffUTCTime, getCurrentTime)
|
|
import Data.Word (Word64)
|
|
import qualified Network.HTTP.Client as HTTP
|
|
import qualified Simplex.Messaging.Crypto as C
|
|
import StoreService.Config (ChainConfig (..))
|
|
import StoreService.Names.Abi (uint256)
|
|
import StoreService.Names.Chain
|
|
import StoreService.Names.Revert
|
|
import Test.Hspec
|
|
|
|
namesChainTests :: Spec
|
|
namesChainTests = describe "StoreService names: chain" $ do
|
|
g <- runIO C.newRandom
|
|
describe "reverts" $ do
|
|
it "names every contract error by the selector viem encodes it with" testRevertSelectors
|
|
it "writes a revert's name as the contracts declare it, none for a revert without data" testRevertText
|
|
it "reads a dry run's revert with data by its name" $ testDryRunRevertNamed g
|
|
describe "failures" $ do
|
|
it "reads eth_call faults: a bare revert as one without data, a revert reason as unknown, an RPC error without the URL, a 2xx answer not JSON without quoting it, and a long message cut" $ testCallFaults g
|
|
it "gives up on a node that does not answer within the call's timeout" $ testTimeout g
|
|
it "reads a refused connection as unavailable, without the URL" $ testRefused g
|
|
it "refuses an answer over the size bound" $ testOversized g
|
|
it "refuses a quantity over 256 bits, and a long one within the call's timeout" $ testLongQuantity g
|
|
it "keeps an HTTP failure's detail for the log, with the URL's host, path and query redacted" testFailureDetail
|
|
describe "reads" $ do
|
|
it "reads a block by its number, and one the node does not have as a failed read" $ testBlockAt g
|
|
it "follows a transaction from sent to mined with its receipt and the nonces" $ testFollowTransaction g
|
|
describe "transactions" $ do
|
|
it "signs a transaction whose hash is the node's keccak256 of the raw bytes" $ testSignedHash g
|
|
it "refuses a gas estimate above the cap" $ testGasCap g
|
|
|
|
-- | The custom errors the service tells apart, from the contracts' compiled ABIs, with viem's encodeErrorResult selectors.
|
|
viemSelectors :: [(ByteString, RevertName)]
|
|
viemSelectors =
|
|
[ ("5289653e", RNAlreadyFrozen),
|
|
("836588c9", RNCommitmentNotFound),
|
|
("74480cc9", RNCommitmentTooNew),
|
|
("256e2216", RNCommitmentTooOld),
|
|
("9a71997b", RNDurationTooShort),
|
|
("a8cab3d1", RNFrozen),
|
|
("2a1b2dd8", RNInsufficientAllowance),
|
|
("11011294", RNInsufficientValue),
|
|
("8d666f60", RNInvalidEVMAddress),
|
|
("756688fe", RNInvalidNonce),
|
|
("8baa579f", RNInvalidSignature),
|
|
("21134988", RNLabelTooLong),
|
|
("2d0c6794", RNMaxCommitmentAgeTooHigh),
|
|
("3e5aa838", RNMaxCommitmentAgeTooLow),
|
|
("ace8f2ce", RNMinCharLengthCanOnlyDecrease),
|
|
("ce47b48d", RNMinCharLengthZero),
|
|
("6357b884", RNMinCommitmentAgeZero),
|
|
("477707e8", RNNameNotAvailable),
|
|
("d17ee357", RNNameNotReserved),
|
|
("e1bf11bf", RNNameReserved),
|
|
("c4295eb6", RNNameTooShort),
|
|
("70f8a4c9", RNNftGateCanOnlyBeDisabled),
|
|
("6b1578c1", RNNftRequired),
|
|
("c30734cc", RNNodeNotOwnedByRegistrar),
|
|
("644d871f", RNNotBeneficiary),
|
|
("42f058b4", RNNotNameOwner),
|
|
("ff43b88f", RNNotOffered),
|
|
("4632a8bf", RNNotOwnerOrBeneficiary),
|
|
("df4795c7", RNNotSponsor),
|
|
("9a0aeacd", RNNotSubnameRegistrar),
|
|
("8a3c1cfb", RNOffsetOutOfBoundsError),
|
|
("a9efcc18", RNOwnerMustBeCaller),
|
|
("40866499", RNPublicSalesClosed),
|
|
("bc7fd331", RNReasonRequired),
|
|
("2bea818f", RNRecipientMustBeCaller),
|
|
("37ed32e8", RNReentrantCall),
|
|
("d3f605c4", RNResolverRequiredWhenDataSupplied),
|
|
("27af646c", RNReverseRecordNotSupported),
|
|
("0819bdcd", RNSignatureExpired),
|
|
("c4f189e2", RNTermsMismatch),
|
|
("bcdf753d", RNTermsNotAccepted),
|
|
("90b8ec18", RNTransferFailed),
|
|
("bf2f44f5", RNTransferToSelf),
|
|
("0a059d71", RNUnexpiredCommitmentExists),
|
|
("d92e233d", RNZeroAddress)
|
|
]
|
|
|
|
testRevertSelectors :: IO ()
|
|
testRevertSelectors = do
|
|
map snd viemSelectors `shouldBe` filter (/= RNWithoutData) [minBound .. maxBound]
|
|
[(n, decodeRevert $ unhex sel <> uint256 (0 :: Int)) | (sel, n) <- viemSelectors] `shouldBe` [(n, Just n) | (_, n) <- viemSelectors]
|
|
decodeRevert (B.take 3 $ unhex "74480cc9") `shouldBe` Nothing
|
|
decodeRevert "" `shouldBe` Just RNWithoutData
|
|
|
|
testRevertText :: IO ()
|
|
testRevertText = do
|
|
revertText RNInvalidNonce `shouldBe` Just "InvalidNonce"
|
|
revertText RNSignatureExpired `shouldBe` Just "SignatureExpired"
|
|
revertText RNWithoutData `shouldBe` Nothing
|
|
|
|
testDryRunRevertNamed :: TVar ChaChaDRG -> IO ()
|
|
testDryRunRevertNamed g = withChain g $ \node c -> do
|
|
setFault node "eth_estimateGas" $ revertFault insufficientAllowance
|
|
estimateGas c fakeSponsor "\x01\x02\x03\x04" `shouldReturn` Left (ChainReverted $ Just RNInsufficientAllowance)
|
|
sentTransactions node `shouldReturn` []
|
|
|
|
-- | A reason kept in the message names a revert that had data, so it is not read as one without.
|
|
testCallFaults :: TVar ChaChaDRG -> IO ()
|
|
testCallFaults g = withChain g $ \node c ->
|
|
forM_
|
|
[ (RpcError (-32000) "execution reverted" Nothing, ChainReverted $ Just RNWithoutData),
|
|
(RpcError 3 "execution reverted: ERC721: invalid token ID" Nothing, ChainReverted Nothing),
|
|
(RpcError (-32000) "header not found" Nothing, ChainUnavailable "eth_call: error -32000: header not found"),
|
|
(NotJson, ChainUnavailable "eth_call: not a JSON-RPC response"),
|
|
(RpcError (-32000) (T.replicate 1000 "x") Nothing, ChainUnavailable $ "eth_call: error -32000: " <> T.replicate 200 "x")
|
|
]
|
|
$ \(fault, err) -> do
|
|
setFault node "eth_call" fault
|
|
ethCallAt c Latest fakeController availableCall `shouldReturn` Left err
|
|
|
|
testTimeout :: TVar ChaChaDRG -> IO ()
|
|
testTimeout g = withChain g $ \node c -> do
|
|
ccTimeout c `shouldBe` 10000000
|
|
setFault node "eth_getBlockByNumber" Hang
|
|
let short = c {ccTimeout = 300000}
|
|
start <- getCurrentTime
|
|
r <- latestBlock short
|
|
elapsed <- (`diffUTCTime` start) <$> getCurrentTime
|
|
r `shouldBe` Left (ChainUnavailable "eth_getBlockByNumber: timed out")
|
|
elapsed `shouldSatisfy` (< 2)
|
|
|
|
testRefused :: TVar ChaChaDRG -> IO ()
|
|
testRefused g = withFakeEthNode $ \node -> do
|
|
-- port 1 is privileged and nothing listens on it in the test container
|
|
c <- newChainClient g (fakeChainConfig node) {cRpcUrl = "http://127.0.0.1:1" <> secretPath}
|
|
latestBlock c `shouldReturn` Left (ChainUnavailable "eth_getBlockByNumber: connection failed")
|
|
|
|
testOversized :: TVar ChaChaDRG -> IO ()
|
|
testOversized g = withChain g $ \node c -> do
|
|
answerCall node fakeController availableCall $ Returns $ B.replicate (512 * 1024) 0
|
|
ethCallAt c Latest fakeController availableCall `shouldReturn` Left (ChainUnavailable "eth_call: response over 1048576 bytes")
|
|
|
|
testLongQuantity :: TVar ChaChaDRG -> IO ()
|
|
testLongQuantity g = withChain g $ \node c -> do
|
|
let fee digits = setFault node "eth_maxPriorityFeePerGas" (Answers $ J.String $ "0x" <> digits) >> maxPriorityFee c {ccTimeout = 300000}
|
|
notQuantity = Left (ChainUnavailable "eth_maxPriorityFeePerGas: not a JSON-RPC response: Error in $: not a quantity")
|
|
fee (T.replicate 63 "0" <> "7") `shouldReturn` Right 7
|
|
fee (T.replicate 64 "0" <> "7") `shouldReturn` notQuantity
|
|
start <- getCurrentTime
|
|
fee (T.replicate 1000000 "f") `shouldReturn` notQuantity
|
|
elapsed <- (`diffUTCTime` start) <$> getCurrentTime
|
|
elapsed `shouldSatisfy` (< 1)
|
|
|
|
testFailureDetail :: IO ()
|
|
testFailureDetail = do
|
|
let url = "https://provider.example/v2/provider-api-key?key=abc"
|
|
req <- HTTP.parseRequest url
|
|
let detail = httpFailureDetail url . HTTP.HttpExceptionRequest req
|
|
redacted t = (t, filter (`T.isInfixOf` t) ["provider.example", "provider-api-key", "key=abc"]) `shouldBe` (t, [])
|
|
redacted $ detail $ HTTP.InvalidDestinationHost "provider.example"
|
|
redacted $ detail $ HTTP.ConnectionFailure $ toException $ userError $ "connect to " <> url
|
|
redacted $ httpFailureDetail url $ HTTP.InvalidUrlException url "Invalid URL"
|
|
manager <- HTTP.newManager HTTP.defaultManagerSettings
|
|
-- port 1 is privileged and nothing listens on it in the test container
|
|
let refusedUrl = "http://127.0.0.1:1" <> secretPath
|
|
refused <- try $ HTTP.parseRequest refusedUrl >>= (`HTTP.httpLbs` manager)
|
|
case refused of
|
|
Left e -> httpFailureDetail refusedUrl e `shouldSatisfy` ("ConnectionFailure" `T.isPrefixOf`)
|
|
Right _ -> expectationFailure "connected to port 1"
|
|
|
|
testBlockAt :: TVar ChaChaDRG -> IO ()
|
|
testBlockAt g = withChain g $ \node c -> do
|
|
setBlock node 100 1798761600
|
|
mined <- mine node []
|
|
blockAt c 100 `shouldReturn` Right Block {blockNumber = 100, blockTimestamp = 1798761600, blockBaseFee = fakeBaseFee}
|
|
blockAt c mined `shouldReturn` Right Block {blockNumber = 101, blockTimestamp = 1798761612, blockBaseFee = fakeBaseFee}
|
|
blockAt c 102 >>= (`shouldSatisfy` \case Left (ChainUnavailable _) -> True; _ -> False)
|
|
|
|
testFollowTransaction :: TVar ChaChaDRG -> IO ()
|
|
testFollowTransaction g = withChain g $ \node c@ChainClient {ccRegistrar} -> do
|
|
let unknown = TxHash $ B.replicate 32 0xee
|
|
transactionKnown c unknown `shouldReturn` Right False
|
|
transactionReceipt c unknown `shouldReturn` Right Nothing
|
|
SignedTx {stRaw, stHash = hash@(TxHash h)} <- signed g c 0
|
|
sendRawTransaction c stRaw `shouldReturn` Right hash
|
|
transactionKnown c hash `shouldReturn` Right True
|
|
transactionReceipt c hash `shouldReturn` Right Nothing
|
|
transactionCount c ccRegistrar Latest `shouldReturn` Right 0
|
|
transactionCount c ccRegistrar Pending `shouldReturn` Right 1
|
|
block <- mine node [(h, False)]
|
|
transactionKnown c hash `shouldReturn` Right True
|
|
transactionReceipt c hash `shouldReturn` Right (Just Receipt {receiptSuccess = False, receiptBlock = block})
|
|
transactionCount c ccRegistrar Latest `shouldReturn` Right 1
|
|
|
|
testSignedHash :: TVar ChaChaDRG -> IO ()
|
|
testSignedHash g = withChain g $ \node c -> do
|
|
SignedTx {stRaw, stHash} <- signed g c 7
|
|
stHash `shouldBe` TxHash (C.keccak256 stRaw)
|
|
sendRawTransaction c stRaw `shouldReturn` Right stHash
|
|
sentTransactions node `shouldReturn` [stRaw]
|
|
|
|
testGasCap :: TVar ChaChaDRG -> IO ()
|
|
testGasCap g = withFakeEthNode $ \node -> do
|
|
let config = fakeChainConfig node
|
|
atCap <- newChainClient g config {cGasLimitCap = fakeGas}
|
|
estimateGas atCap fakeController availableCall `shouldReturn` Right fakeGas
|
|
belowCap <- newChainClient g config {cGasLimitCap = fakeGas - 1}
|
|
estimateGas belowCap fakeController availableCall `shouldReturn` Left (ChainGasAboveCap fakeGas)
|
|
|
|
-- fixtures
|
|
|
|
withChain :: TVar ChaChaDRG -> (FakeEthNode -> ChainClient -> IO a) -> IO a
|
|
withChain g action = withFakeEthNode $ \node -> do
|
|
c <- newChainClient g (fakeChainConfig node) {cRpcUrl = fnUrl node <> secretPath}
|
|
action node c
|
|
|
|
-- | Where a provider puts its API key.
|
|
secretPath :: String
|
|
secretPath = "/v2/provider-api-key"
|
|
|
|
signed :: TVar ChaChaDRG -> ChainClient -> Word64 -> IO SignedTx
|
|
signed g c nonce =
|
|
either error id
|
|
<$> signTransaction g c TxRequest {trTo = fakeController, trData = availableCall, trNonce = nonce, trGasLimit = fakeGas, trMaxFeePerGas = 2 * fakeBaseFee + fakeTip, trMaxPriorityFeePerGas = fakeTip}
|
|
|
|
-- | controller.available("bakery1"), viem's encodeFunctionData
|
|
availableCall :: ByteString
|
|
availableCall = unhex "aeb8ce9b0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000762616b6572793100000000000000000000000000000000000000000000000000"
|
|
|
|
-- | viem's encodeErrorResult of InsufficientAllowance(10000, 2500)
|
|
insufficientAllowance :: ByteString
|
|
insufficientAllowance = unhex "2a1b2dd8000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000009c4"
|
|
|
|
unhex :: ByteString -> ByteString
|
|
unhex = either error id . convertFromBase Base16
|