mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-21 20:57:15 +00:00
store: add the names transaction sender
This commit is contained in:
1 parent
71f19aac27
commit
33a393736a
9 files changed
+908
-32
No files matched your search
@@ -159,6 +159,7 @@ data NamesConfig = NamesConfig
|
||||
nYears :: Int,
|
||||
nTermStart :: UTCTime,
|
||||
nTermSlackSeconds :: Int,
|
||||
nRevealMarginSeconds :: Int,
|
||||
-- | Only prepaid codes pay for names: every order that would take money for one is refused.
|
||||
nCodesOnly :: Bool,
|
||||
nChain :: ChainConfig
|
||||
@@ -173,7 +174,10 @@ data ChainConfig = ChainConfig
|
||||
cBaseRegistrar :: Address,
|
||||
cResolver :: Address,
|
||||
cServiceKey :: Secp256k1PrivateKey,
|
||||
cGasLimitCap :: Word64
|
||||
cConfirmations :: Int,
|
||||
cGasLimitCap :: Word64,
|
||||
-- | wei; no transaction is signed with a higher max fee per gas
|
||||
cMaxFeePerGas :: Word64
|
||||
}
|
||||
deriving (Eq)
|
||||
|
||||
@@ -237,6 +241,10 @@ requireHttps u
|
||||
minNameYears :: Int
|
||||
minNameYears = 2
|
||||
|
||||
-- | 100 gwei.
|
||||
defaultMaxFeePerGas :: Word64
|
||||
defaultMaxFeePerGas = 100000000000
|
||||
|
||||
defaultTermStart :: String
|
||||
defaultTermStart = "2027-01-01T00:00:00Z"
|
||||
|
||||
@@ -308,8 +316,8 @@ knownSettings =
|
||||
("group", ["display_name", "description"]),
|
||||
("dev", ["accept_unverified_store_receipts", "check_store_during_coverage", "credit_test_purchases"]),
|
||||
("issuer", ["index", "private_key"]),
|
||||
("names", ["tld", "years", "term_start", "term_slack_seconds", "codes_only"]),
|
||||
("chain", ["rpc_url", "chain_id", "controller", "sponsor", "base_registrar", "resolver", "service_key", "gas_limit_cap"])
|
||||
("names", ["tld", "years", "term_start", "term_slack_seconds", "reveal_margin_seconds", "codes_only"]),
|
||||
("chain", ["rpc_url", "chain_id", "controller", "sponsor", "base_registrar", "resolver", "service_key", "confirmations", "gas_limit_cap", "max_fee_per_gas"])
|
||||
]
|
||||
|
||||
unknownKeys :: Ini -> [Text]
|
||||
@@ -508,9 +516,10 @@ parseConfig ini = do
|
||||
when (nYears < minNameYears) $ Left ("names.years must be at least " <> show minNameYears)
|
||||
nTermStart <- termStart
|
||||
nTermSlackSeconds <- atLeast 0 "names" "term_slack_seconds" 3600
|
||||
nRevealMarginSeconds <- atLeast 0 "names" "reveal_margin_seconds" 900
|
||||
nCodesOnly <- bool "names" "codes_only" False
|
||||
nChain <- chainSection
|
||||
pure (Just NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nCodesOnly, nChain})
|
||||
pure (Just NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nRevealMarginSeconds, nCodesOnly, nChain})
|
||||
tldSetting =
|
||||
required "names" "tld" >>= \case
|
||||
"simplex" -> Right TLDSimplex
|
||||
@@ -531,8 +540,10 @@ parseConfig ini = do
|
||||
cBaseRegistrar <- address "base_registrar"
|
||||
cResolver <- address "resolver"
|
||||
cServiceKey <- serviceKey
|
||||
cConfirmations <- atLeast 0 "chain" "confirmations" 3
|
||||
cGasLimitCap <- positiveWord "gas_limit_cap" (Just 8000000)
|
||||
pure ChainConfig {cRpcUrl, cChainId, cController, cSponsor, cBaseRegistrar, cResolver, cServiceKey, cGasLimitCap}
|
||||
cMaxFeePerGas <- positiveWord "max_fee_per_gas" (Just defaultMaxFeePerGas)
|
||||
pure ChainConfig {cRpcUrl, cChainId, cController, cSponsor, cBaseRegistrar, cResolver, cServiceKey, cConfirmations, cGasLimitCap, cMaxFeePerGas}
|
||||
rpcUrl = do
|
||||
v <- required "chain" "rpc_url"
|
||||
unless (any (`T.isPrefixOf` v) ["http://", "https://"]) $ Left "chain.rpc_url must be an http or https URL"
|
||||
|
||||
@@ -18,6 +18,8 @@ module StoreService.Names.Chain
|
||||
ethCall,
|
||||
ethCallAt,
|
||||
latestBlock,
|
||||
blockAt,
|
||||
confirmedNumber,
|
||||
transactionReceipt,
|
||||
transactionByHash,
|
||||
transactionCount,
|
||||
@@ -141,6 +143,14 @@ ethCallAt c tag to calldata = rpc c "eth_call" [J.object ["to" .= to, "data" .=
|
||||
latestBlock :: ChainClient -> IO (Either ChainError Block)
|
||||
latestBlock c = blockByNumber c "latest"
|
||||
|
||||
-- | A block the node does not have is a failed read.
|
||||
blockAt :: ChainClient -> Word64 -> IO (Either ChainError Block)
|
||||
blockAt c = blockByNumber c . blockTagParam . AtBlock
|
||||
|
||||
-- | The newest block at the configured confirmation depth below this latest one, as the sender counts it.
|
||||
confirmedNumber :: ChainClient -> Word64 -> Word64
|
||||
confirmedNumber ChainClient {ccConfig = ChainConfig {cConfirmations}} latest = latest - min latest (fromIntegral (max 1 cConfirmations) - 1)
|
||||
|
||||
blockByNumber :: ChainClient -> J.Value -> IO (Either ChainError Block)
|
||||
blockByNumber c tag =
|
||||
rpc c "eth_getBlockByNumber" [tag, J.Bool False] $ J.withObject "block" $ \b ->
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
-- | Every transaction of the registrar key, sent once and in nonce order.
|
||||
module StoreService.Names.Sender
|
||||
( Sender,
|
||||
SendError (..),
|
||||
TxOutcome (..),
|
||||
PendingTx,
|
||||
ptHash,
|
||||
ptNonce,
|
||||
newSender,
|
||||
submitTransaction,
|
||||
inFlightTransaction,
|
||||
awaitOutcome,
|
||||
followTransactions,
|
||||
runSender,
|
||||
senderRound,
|
||||
replaceAfter,
|
||||
)
|
||||
where
|
||||
|
||||
import Control.Concurrent (threadDelay)
|
||||
import Control.Concurrent.MVar (MVar, newMVar, withMVar)
|
||||
import Control.Concurrent.STM
|
||||
import Control.Exception (throwIO)
|
||||
import Control.Monad (forM, forM_, forever, unless, void, when)
|
||||
import Control.Monad.Except (ExceptT (..), runExceptT, throwError, withExceptT)
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import Crypto.Random (ChaChaDRG)
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.List (find)
|
||||
import Data.List.NonEmpty (NonEmpty (..), (<|))
|
||||
import qualified Data.List.NonEmpty as L
|
||||
import Data.Map.Strict (Map)
|
||||
import qualified Data.Map.Strict as M
|
||||
import Data.Maybe (fromMaybe, isNothing)
|
||||
import Data.Text (Text)
|
||||
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.Util (tshow)
|
||||
import StoreService.Config (ChainConfig (..))
|
||||
import StoreService.Log (logError, logInfo, logWarn, safely)
|
||||
import StoreService.Names.Chain
|
||||
import StoreService.Names.Store (NameTransaction (..), NewTransaction (..), TxFor, TxStatus (..), insertTransaction, pendingTransactions, recordTransactionOutcome)
|
||||
import StoreService.Store.Invoices (StoreDecodeError (..))
|
||||
|
||||
data Sender = Sender
|
||||
{ snChain :: ChainClient,
|
||||
snRandom :: TVar ChaChaDRG,
|
||||
snStore :: DBStore,
|
||||
snNow :: IO UTCTime,
|
||||
-- | MVar wakes its waiters in FIFO order, so new transactions take their nonces in the order they came.
|
||||
snQueue :: MVar (),
|
||||
-- | Above every nonce this sender saved.
|
||||
snNextNonce :: TVar Word64,
|
||||
snInFlight :: TVar (Map Word64 InFlight)
|
||||
}
|
||||
|
||||
-- | The signatures for one registrar nonce: the first, then each fee-bumped replacement.
|
||||
data InFlight = InFlight
|
||||
{ ifFor :: TxFor,
|
||||
ifRequest :: TxRequest,
|
||||
ifRaw :: ByteString,
|
||||
-- | newest first
|
||||
ifHashes :: NonEmpty TxHash,
|
||||
ifReplaceAt :: UTCTime,
|
||||
-- | the block at which the nonce was first seen used with no receipt for any hash
|
||||
ifUsedSince :: Maybe Word64,
|
||||
-- | the last warning logged for this nonce, so a lasting failure is logged once
|
||||
ifLastWarning :: Maybe Text,
|
||||
ifOutcome :: TMVar TxOutcome
|
||||
}
|
||||
|
||||
-- | Nothing was signed or saved.
|
||||
data SendError
|
||||
= -- | the dry run reverted, its gas is above the cap, or the chain did not answer
|
||||
SEChain ChainError
|
||||
| SESigning String
|
||||
| -- | this registration's or edit's transaction of this kind is still in flight
|
||||
SEInFlight
|
||||
| -- | the wei per gas the latest block needs is above the configured max fee per gas
|
||||
SEFeeAboveCap Integer
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | Settled at the configured confirmation depth.
|
||||
data TxOutcome
|
||||
= TOConfirmed TxHash Receipt
|
||||
| -- | The registrar nonce is used and none of its hashes has a receipt: a pruned receipt or another transaction.
|
||||
-- The sender signs nothing more for it; its owner judges it from chain state.
|
||||
TONonceUsed
|
||||
deriving (Eq, Show)
|
||||
|
||||
data PendingTx = PendingTx
|
||||
{ -- | the newest signature's hash
|
||||
ptHash :: TxHash,
|
||||
ptNonce :: Word64,
|
||||
ptOutcome :: TMVar TxOutcome
|
||||
}
|
||||
|
||||
-- | 15 blocks: a transaction still not mined by then is replaced if the market's fees have passed its own.
|
||||
replaceAfter :: NominalDiffTime
|
||||
replaceAfter = 180
|
||||
|
||||
followInterval :: Int
|
||||
followInterval = 4000000
|
||||
|
||||
-- | The max fee covers six full blocks in a row, each raising the base fee by 12.5%.
|
||||
baseFeeHeadroom :: Integer
|
||||
baseFeeHeadroom = 2
|
||||
|
||||
-- | Nodes accept a replacement only with both fees at least 10% higher.
|
||||
bumped :: Integer -> Integer
|
||||
bumped fee = fee + fee `div` 8 + 1
|
||||
|
||||
-- | Resumes every saved transaction without a receipt. Owners resuming after a restart find theirs with 'inFlightTransaction'.
|
||||
newSender :: TVar ChaChaDRG -> ChainClient -> DBStore -> IO UTCTime -> IO Sender
|
||||
newSender snRandom snChain snStore snNow = do
|
||||
saved <- withTransaction snStore pendingTransactions
|
||||
inFlight <- M.fromList <$> mapM resumed (L.groupWith ntNonce saved)
|
||||
snQueue <- newMVar ()
|
||||
snNextNonce <- newTVarIO $ maybe 0 ((+ 1) . fst) $ M.lookupMax inFlight
|
||||
snInFlight <- newTVarIO inFlight
|
||||
pure Sender {snChain, snRandom, snStore, snNow, snQueue, snNextNonce, snInFlight}
|
||||
where
|
||||
resumed txs = do
|
||||
let NameTransaction {ntFor, ntNonce, ntRaw, ntSentAt} = L.last txs
|
||||
request <- either (throwIO . StoreDecodeError . ("name_transactions.raw: " <>) . T.pack) pure $ decodeSignedTx ntRaw
|
||||
outcome <- newEmptyTMVarIO
|
||||
pure
|
||||
( ntNonce,
|
||||
InFlight
|
||||
{ ifFor = ntFor,
|
||||
ifRequest = request,
|
||||
ifRaw = ntRaw,
|
||||
ifHashes = L.reverse $ L.map ntHash txs,
|
||||
ifReplaceAt = addUTCTime replaceAfter ntSentAt,
|
||||
ifUsedSince = Nothing,
|
||||
ifLastWarning = Nothing,
|
||||
ifOutcome = outcome
|
||||
}
|
||||
)
|
||||
|
||||
-- | 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 =
|
||||
withMVar snQueue $ \_ -> runExceptT $ do
|
||||
busy <- any ((== for) . ifFor) <$> liftIO (readTVarIO snInFlight)
|
||||
when busy $ throwError SEInFlight
|
||||
gas <- chain $ estimateGas c to calldata
|
||||
used <- chain $ transactionCount c ccRegistrar Latest
|
||||
nonce <- max used <$> liftIO (readTVarIO snNextNonce)
|
||||
market@(tip, baseFee) <- chain $ marketFees c
|
||||
let needed = baseFee + tip
|
||||
cap = feeCap c
|
||||
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"
|
||||
throwError $ SEFeeAboveCap needed
|
||||
-- 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 = gas, trMaxFeePerGas = fromInteger $ min cap $ headroomFee market, trMaxPriorityFeePerGas = fromInteger tip}
|
||||
SignedTx {stRaw, stHash} <- withExceptT SESigning $ ExceptT $ signTransaction snRandom c request
|
||||
liftIO $ do
|
||||
now <- snNow
|
||||
saveTransaction snStore for stRaw stHash nonce now
|
||||
outcome <- newEmptyTMVarIO
|
||||
atomically $ do
|
||||
writeTVar snNextNonce $ nonce + 1
|
||||
modifyTVar' snInFlight $
|
||||
M.insert nonce InFlight {ifFor = for, ifRequest = request, ifRaw = stRaw, ifHashes = stHash :| [], ifReplaceAt = addUTCTime replaceAfter now, ifUsedSince = Nothing, ifLastWarning = Nothing, ifOutcome = outcome}
|
||||
broadcast s nonce stRaw
|
||||
pure PendingTx {ptHash = stHash, ptNonce = nonce, ptOutcome = outcome}
|
||||
where
|
||||
chain = withExceptT SEChain . ExceptT
|
||||
|
||||
inFlightTransaction :: Sender -> TxFor -> IO (Maybe PendingTx)
|
||||
inFlightTransaction Sender {snInFlight} for =
|
||||
fmap pending . find ((== for) . ifFor . snd) . M.toList <$> readTVarIO snInFlight
|
||||
where
|
||||
pending (nonce, InFlight {ifHashes, ifOutcome}) = PendingTx {ptHash = L.head ifHashes, ptNonce = nonce, ptOutcome = ifOutcome}
|
||||
|
||||
-- | Nothing when the transaction is not settled within the microseconds.
|
||||
awaitOutcome :: Int -> PendingTx -> IO (Maybe TxOutcome)
|
||||
awaitOutcome micros PendingTx {ptOutcome} = do
|
||||
timer <- registerDelay micros
|
||||
atomically $ (Just <$> readTMVar ptOutcome) `orElse` (Nothing <$ (readTVar timer >>= check))
|
||||
|
||||
runSender :: Sender -> IO ()
|
||||
runSender s = forever $ senderRound s >> threadDelay followInterval
|
||||
|
||||
-- | A round that fails is logged, so the lane goes on to the next one.
|
||||
senderRound :: Sender -> IO ()
|
||||
senderRound s = void $ safely "names sender: the round" (followTransactions s)
|
||||
|
||||
-- | One round over the transactions in flight, lowest nonce first. Only one thread may run it.
|
||||
followTransactions :: Sender -> IO ()
|
||||
followTransactions s@Sender {snChain = c@ChainClient {ccRegistrar}, snInFlight} = do
|
||||
inFlight <- readTVarIO snInFlight
|
||||
unless (M.null inFlight) $
|
||||
runExceptT ((,) <$> ExceptT (latestBlock c) <*> ExceptT (transactionCount c ccRegistrar Latest)) >>= \case
|
||||
Left e -> logWarn $ "names sender: " <> tshow e
|
||||
Right (Block {blockNumber}, used) -> forM_ (M.toAscList inFlight) $ uncurry $ follow s blockNumber used
|
||||
|
||||
follow :: Sender -> Word64 -> Word64 -> Word64 -> InFlight -> IO ()
|
||||
follow s@Sender {snChain = c@ChainClient {ccConfig = ChainConfig {cConfirmations}}} latest used nonce tx@InFlight {ifHashes, ifUsedSince} =
|
||||
runExceptT (forM (L.toList ifHashes) $ \h -> (h,) <$> ExceptT (transactionReceipt c h)) >>= \case
|
||||
Left e -> warnNonce s nonce $ tshow e
|
||||
Right receipts -> case [(h, r) | (h, Just r) <- receipts] of
|
||||
(h, r@Receipt {receiptBlock}) : _
|
||||
| deep receiptBlock -> settle s nonce tx $ TOConfirmed h r
|
||||
| otherwise -> update s nonce $ \t -> t {ifUsedSince = Nothing}
|
||||
[]
|
||||
| nonce < used -> do
|
||||
let since = fromMaybe latest ifUsedSince
|
||||
if deep since then settle s nonce tx TONonceUsed else update s nonce $ \t -> t {ifUsedSince = Just since}
|
||||
| otherwise -> do
|
||||
update s nonce $ \t -> t {ifUsedSince = Nothing}
|
||||
resend s nonce tx
|
||||
where
|
||||
deep block = latest + 1 >= block + fromIntegral cConfirmations
|
||||
|
||||
-- | A replacement when one is due and the market asks for it; otherwise the newest signature again if the node does not know it.
|
||||
resend :: Sender -> Word64 -> InFlight -> IO ()
|
||||
resend s@Sender {snChain = c, snNow} nonce tx@InFlight {ifRaw, ifHashes = newest :| _, ifReplaceAt, ifLastWarning} = do
|
||||
now <- snNow
|
||||
replaced <- if now >= ifReplaceAt then replace s nonce tx now else pure False
|
||||
unless replaced $
|
||||
transactionByHash c newest >>= \case
|
||||
Right Nothing -> do
|
||||
when (isNothing ifLastWarning) $ logInfo $ "names sender: rebroadcasting nonce " <> tshow nonce
|
||||
broadcast s nonce ifRaw
|
||||
Right (Just _) -> pure ()
|
||||
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 =
|
||||
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)
|
||||
| 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)
|
||||
tip' = min maxFee' $ max tip (bumped oldTip)
|
||||
request = old {trMaxPriorityFeePerGas = fromInteger tip', trMaxFeePerGas = fromInteger maxFee'}
|
||||
signTransaction snRandom c request >>= \case
|
||||
Left e -> False <$ logError ("names sender: replacement for nonce " <> tshow nonce <> " not signed: " <> T.pack e)
|
||||
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}
|
||||
logInfo $ "names sender: replacing nonce " <> tshow nonce <> " at max fee " <> tshow maxFee'
|
||||
True <$ broadcast s nonce stRaw
|
||||
where
|
||||
later = addUTCTime replaceAfter now
|
||||
oldTip = toInteger trMaxPriorityFeePerGas
|
||||
oldMax = toInteger trMaxFeePerGas
|
||||
cap = feeCap c
|
||||
|
||||
-- | 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 ()
|
||||
settle Sender {snStore, snInFlight} nonce InFlight {ifHashes, ifOutcome} outcome = do
|
||||
withTransaction snStore $ \db -> forM_ ifHashes $ \h -> recordTransactionOutcome db h TSPending (status h) (block h)
|
||||
when (outcome == TONonceUsed) $
|
||||
logWarn $ "names sender: nonce " <> tshow nonce <> " used with no receipt for its transactions; its owner judges it from chain state"
|
||||
atomically $ do
|
||||
modifyTVar' snInFlight $ M.delete nonce
|
||||
putTMVar ifOutcome outcome
|
||||
where
|
||||
status h = case outcome of
|
||||
TOConfirmed mined Receipt {receiptSuccess} | mined == h -> if receiptSuccess then TSMined else TSReverted
|
||||
_ -> TSDropped
|
||||
block h = case outcome of
|
||||
TOConfirmed mined Receipt {receiptBlock} | mined == h -> Just receiptBlock
|
||||
_ -> Nothing
|
||||
|
||||
update :: Sender -> Word64 -> (InFlight -> InFlight) -> IO ()
|
||||
update Sender {snInFlight} nonce f = atomically $ modifyTVar' snInFlight $ M.adjust f nonce
|
||||
|
||||
-- | The tip the node suggests and the latest base fee, as Integer so that no fee arithmetic wraps.
|
||||
marketFees :: ChainClient -> IO (Either ChainError (Integer, Integer))
|
||||
marketFees c = runExceptT $ do
|
||||
tip <- ExceptT $ maxPriorityFee c
|
||||
Block {blockBaseFee} <- ExceptT $ latestBlock c
|
||||
pure (toInteger tip, toInteger blockBaseFee)
|
||||
|
||||
headroomFee :: (Integer, Integer) -> Integer
|
||||
headroomFee (tip, baseFee) = baseFeeHeadroom * baseFee + tip
|
||||
|
||||
feeCap :: ChainClient -> Integer
|
||||
feeCap ChainClient {ccConfig = ChainConfig {cMaxFeePerGas}} = toInteger cMaxFeePerGas
|
||||
|
||||
saveTransaction :: DBStore -> TxFor -> ByteString -> TxHash -> Word64 -> UTCTime -> IO ()
|
||||
saveTransaction st for raw hash nonce now =
|
||||
withTransaction st $ \db -> void $ insertTransaction db for NewTransaction {ntxRaw = raw, ntxHash = hash, ntxNonce = nonce} now
|
||||
|
||||
broadcast :: Sender -> Word64 -> ByteString -> IO ()
|
||||
broadcast s@Sender {snChain} nonce raw =
|
||||
sendRawTransaction snChain raw >>= \case
|
||||
Left e -> warnNonce s nonce $ "not sent: " <> tshow e
|
||||
Right _ -> update s nonce $ \t -> t {ifLastWarning = Nothing}
|
||||
|
||||
-- | Logged when it differs from the nonce's last warning. An underfunded key is an error, since only the operator can fund it.
|
||||
warnNonce :: Sender -> Word64 -> Text -> IO ()
|
||||
warnNonce Sender {snInFlight} nonce warning = do
|
||||
changed <- atomically $ do
|
||||
inFlight <- readTVar snInFlight
|
||||
case M.lookup nonce inFlight of
|
||||
Just tx@InFlight {ifLastWarning}
|
||||
| ifLastWarning == Just warning -> pure False
|
||||
| otherwise -> True <$ writeTVar snInFlight (M.insert nonce tx {ifLastWarning = Just warning} inFlight)
|
||||
Nothing -> pure True
|
||||
when changed $ (if underfundedMessage `T.isInfixOf` T.toLower warning then logError else logWarn) message
|
||||
where
|
||||
message = "names sender: nonce " <> tshow nonce <> ": " <> warning
|
||||
|
||||
-- | How geth, reth and erigon refuse a transaction whose key cannot pay gas × fee + value, in lower case; anvil capitalizes it.
|
||||
underfundedMessage :: Text
|
||||
underfundedMessage = "insufficient funds"
|
||||
@@ -108,11 +108,13 @@ idle_seconds = 60
|
||||
;term_start = 2027-01-01T00:00:00Z
|
||||
; how far an order's duration may be from the one the service computes
|
||||
;term_slack_seconds = 3600
|
||||
; a commitment with less than this left before its reveal deadline is not reused
|
||||
;reveal_margin_seconds = 900
|
||||
; on: only prepaid codes register names, and every paid name order answers 403 codes_only
|
||||
;codes_only = off
|
||||
;
|
||||
; the chain the names are registered on. rpc_url can carry a provider's API key and service_key
|
||||
; is the registrar key: keep both secret. Neither is ever logged.
|
||||
; pays for every commit and register: keep both secret. Neither is ever logged.
|
||||
;[chain]
|
||||
;rpc_url = http://127.0.0.1:8545
|
||||
;chain_id = 31337
|
||||
@@ -120,10 +122,15 @@ idle_seconds = 60
|
||||
;sponsor = 0x...
|
||||
;base_registrar = 0x...
|
||||
;resolver = 0x...
|
||||
; 0x and 64 hex digits: the registrar key the service sends transactions with
|
||||
; 0x and 64 hex digits: the registrar key the service sends transactions with; create, fund and
|
||||
; rotate it as apps/simplex-store-service/README.md, "The gas key", says
|
||||
;service_key = 0x...
|
||||
; blocks after a register's receipt before a name counts as registered
|
||||
;confirmations = 3
|
||||
; the most gas any one transaction may use
|
||||
;gas_limit_cap = 8000000
|
||||
; the most wei per gas any transaction may pay (100 gwei); a transaction that needs more waits
|
||||
;max_fee_per_gas = 100000000000
|
||||
|
||||
; local testing only, in both run modes: accepts any well-formed App Store or Play receipt without
|
||||
; verifying it, so anyone who can reach this service can mint badges. The service refuses to start
|
||||
|
||||
@@ -94,11 +94,13 @@ private_key = replace-me
|
||||
;term_start = 2027-01-01T00:00:00Z
|
||||
; how far an order's duration may be from the one the service computes
|
||||
;term_slack_seconds = 3600
|
||||
; a commitment with less than this left before its reveal deadline is not reused
|
||||
;reveal_margin_seconds = 900
|
||||
; on: only prepaid codes register names, and every paid name order answers 403 codes_only
|
||||
;codes_only = off
|
||||
;
|
||||
; the chain the names are registered on. rpc_url can carry a provider's API key and service_key
|
||||
; is the registrar key: keep both secret. Neither is ever logged.
|
||||
; pays for every commit and register: keep both secret. Neither is ever logged.
|
||||
;[chain]
|
||||
; the reth node
|
||||
;rpc_url = http://127.0.0.1:8545
|
||||
@@ -108,10 +110,15 @@ private_key = replace-me
|
||||
;sponsor = 0x...
|
||||
;base_registrar = 0x...
|
||||
;resolver = 0x...
|
||||
; 0x and 64 hex digits: the registrar key the service sends transactions with
|
||||
; 0x and 64 hex digits: the registrar key the service sends transactions with; create, fund and
|
||||
; rotate it as apps/simplex-store-service/README.md, "The gas key", says
|
||||
;service_key = 0x...
|
||||
; blocks after a register's receipt before a name counts as registered
|
||||
;confirmations = 3
|
||||
; the most gas any one transaction may use
|
||||
;gas_limit_cap = 8000000
|
||||
; the most wei per gas any transaction may pay (100 gwei); a transaction that needs more waits
|
||||
;max_fee_per_gas = 100000000000
|
||||
|
||||
; local testing only, in both run modes: accepts any well-formed App Store or Play receipt without
|
||||
; verifying it, so anyone who can reach this service can mint badges. The service refuses to start
|
||||
|
||||
@@ -710,31 +710,36 @@ address t = either error id (strDecode (B.pack (T.unpack t)))
|
||||
|
||||
testNamesDefaults :: IO ()
|
||||
testNamesDefaults = do
|
||||
NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nChain} <- parsedNames namesSection chainSection
|
||||
NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nRevealMarginSeconds, nChain} <- parsedNames namesSection chainSection
|
||||
nTLD `shouldBe` TLDTesting
|
||||
nYears `shouldBe` 2
|
||||
nTermStart `shouldBe` UTCTime (fromGregorian 2027 1 1) 0
|
||||
nTermSlackSeconds `shouldBe` 3600
|
||||
let ChainConfig {cRpcUrl, cChainId, cController, cSponsor, cBaseRegistrar, cResolver, cServiceKey, cGasLimitCap} = nChain
|
||||
nRevealMarginSeconds `shouldBe` 900
|
||||
let ChainConfig {cRpcUrl, cChainId, cController, cSponsor, cBaseRegistrar, cResolver, cServiceKey, cConfirmations, cGasLimitCap, cMaxFeePerGas} = nChain
|
||||
cRpcUrl `shouldBe` T.unpack rpcUrl
|
||||
cChainId `shouldBe` 31337
|
||||
(cController, cSponsor, cBaseRegistrar, cResolver) `shouldBe` (address controllerAddr, address sponsorAddr, address registrarAddr, address resolverAddr)
|
||||
Right expectedKey <- pure (mkPrivateKey (BA.pack [0xac, 0x09, 0x74, 0xbe, 0xc3, 0x9a, 0x17, 0xe3, 0x6b, 0xa4, 0xa6, 0xb4, 0xd2, 0x38, 0xff, 0x94, 0x4b, 0xac, 0xb4, 0x78, 0xcb, 0xed, 0x5e, 0xfc, 0xae, 0x78, 0x4d, 0x7b, 0xf4, 0xf2, 0xff, 0x80]))
|
||||
(cServiceKey == expectedKey) `shouldBe` True
|
||||
cConfirmations `shouldBe` 3
|
||||
cGasLimitCap `shouldBe` 8000000
|
||||
cMaxFeePerGas `shouldBe` 100000000000
|
||||
|
||||
testNamesWrittenOut :: IO ()
|
||||
testNamesWrittenOut = do
|
||||
let ns = ["[names]", "tld = simplex", "years = 5", "term_start = 2027-03-01T12:00:00Z", "term_slack_seconds = 60"]
|
||||
cs = chainSection <> ["gas_limit_cap = 500000"]
|
||||
NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nChain = ChainConfig {cGasLimitCap}} <- parsedNames ns cs
|
||||
let ns = ["[names]", "tld = simplex", "years = 5", "term_start = 2027-03-01T12:00:00Z", "term_slack_seconds = 60", "reveal_margin_seconds = 0"]
|
||||
cs = chainSection <> ["confirmations = 0", "gas_limit_cap = 500000", "max_fee_per_gas = 30000000000"]
|
||||
NamesConfig {nTLD, nYears, nTermStart, nTermSlackSeconds, nRevealMarginSeconds, nChain = ChainConfig {cConfirmations, cGasLimitCap, cMaxFeePerGas}} <- parsedNames ns cs
|
||||
nTLD `shouldBe` TLDSimplex
|
||||
nYears `shouldBe` 5
|
||||
nTermStart `shouldBe` UTCTime (fromGregorian 2027 3 1) (12 * 3600)
|
||||
nTermSlackSeconds `shouldBe` 60
|
||||
cGasLimitCap `shouldBe` 500000
|
||||
(nTermSlackSeconds, nRevealMarginSeconds) `shouldBe` (60, 0)
|
||||
(cConfirmations, cGasLimitCap, cMaxFeePerGas) `shouldBe` (0, 500000, 30000000000)
|
||||
namesRefusal ns (chainSection <> ["confirmations = -1"]) `shouldReturn` "chain.confirmations must be at least 0"
|
||||
namesRefusal ns (chainSection <> ["gas_limit_cap = 0"]) `shouldReturn` "chain.gas_limit_cap must be a positive whole number"
|
||||
namesRefusal ns (chainSection <> ["gas_limit_cap = 18446744073709551616"]) `shouldReturn` "chain.gas_limit_cap must be a positive whole number"
|
||||
namesRefusal ns (chainSection <> ["max_fee_per_gas = 0"]) `shouldReturn` "chain.max_fee_per_gas must be a positive whole number"
|
||||
namesRefusal ns (chainSection <> ["max_fee_per_gas = 18446744073709551616"]) `shouldReturn` "chain.max_fee_per_gas must be a positive whole number"
|
||||
namesRefusal ns (filter (not . ("chain_id" `T.isPrefixOf`)) chainSection) `shouldReturn` "chain.chain_id is required"
|
||||
|
||||
testNamesListenerKeys :: IO ()
|
||||
@@ -830,7 +835,7 @@ testNamesSecretsHidden = do
|
||||
e <- namesRefusal namesSection (withUrl secretUrl)
|
||||
e `shouldBe` "chain.rpc_url must be an http or https URL"
|
||||
e `shouldNotContain` "rpc-token-value"
|
||||
other <- namesRefusal namesSection (chainSection <> ["gas_limit_cap = many"])
|
||||
other <- namesRefusal namesSection (chainSection <> ["confirmations = many"])
|
||||
other `shouldNotContain` "rpc-token-value"
|
||||
other `shouldNotContain` T.unpack (T.drop 2 serviceKeyHex)
|
||||
Right cfg <- parseIni (namesIni namesSection chainSection)
|
||||
|
||||
@@ -6,14 +6,24 @@ module Bots.StoreService.FakeEthNode
|
||||
( FakeEthNode (..),
|
||||
CallAnswer (..),
|
||||
Fault (..),
|
||||
Gate,
|
||||
withFakeEthNode,
|
||||
fakeChainConfig,
|
||||
answerCall,
|
||||
answerCallOnce,
|
||||
answerCallFrom,
|
||||
setFault,
|
||||
clearFault,
|
||||
releaseGate,
|
||||
pausedAt,
|
||||
setBlock,
|
||||
setFees,
|
||||
mine,
|
||||
dropTransaction,
|
||||
pruneReceipt,
|
||||
useNextNonce,
|
||||
sentTransactions,
|
||||
awaitSent,
|
||||
revertFault,
|
||||
fakeChainId,
|
||||
fakeController,
|
||||
@@ -24,10 +34,12 @@ module Bots.StoreService.FakeEthNode
|
||||
fakeGas,
|
||||
fakeTip,
|
||||
fakeBaseFee,
|
||||
fakeMaxFeePerGas,
|
||||
)
|
||||
where
|
||||
|
||||
import Control.Concurrent (threadDelay)
|
||||
import Control.Concurrent.Async (Async, async, cancel)
|
||||
import Control.Concurrent.STM
|
||||
import Control.Exception (finally)
|
||||
import Data.Aeson ((.:), (.=))
|
||||
@@ -45,7 +57,7 @@ import Data.Word (Word64)
|
||||
import Network.HTTP.Types (badGateway502, badRequest400, hContentType, ok200)
|
||||
import Network.Wai (Application, responseLBS, strictRequestBody)
|
||||
import qualified Network.Wai.Handler.Warp as Warp
|
||||
import Numeric (showHex)
|
||||
import Numeric (readHex, showHex)
|
||||
import Simplex.Messaging.Crypto (keccak256)
|
||||
import qualified Simplex.Messaging.Crypto.Secp256k1 as S
|
||||
import Simplex.Messaging.Encoding.String (strDecode)
|
||||
@@ -54,6 +66,7 @@ import Simplex.Messaging.Util (eitherToMaybe)
|
||||
import StoreService.Config (ChainConfig (..))
|
||||
import StoreService.Names.Abi (hex)
|
||||
import StoreService.Names.Chain (TxRequest (..), decodeSignedTx)
|
||||
import System.Timeout (timeout)
|
||||
|
||||
data FakeEthNode = FakeEthNode
|
||||
{ fnUrl :: String,
|
||||
@@ -72,9 +85,17 @@ data Fault
|
||||
| Answers J.Value
|
||||
| -- | answers as usual after this many microseconds
|
||||
Slow Int
|
||||
| -- | the next request waits at the gate until it is released, then answers as usual; later ones pass
|
||||
Gated Gate
|
||||
|
||||
data Gate = Gate {gArrived :: TMVar (), gRelease :: TMVar ()}
|
||||
|
||||
data FakeState = FakeState
|
||||
{ fsCalls :: Map (ByteString, ByteString) CallAnswer,
|
||||
-- | answered once, before 'fsCalls'
|
||||
fsCallsOnce :: Map (ByteString, ByteString) CallAnswer,
|
||||
-- | answered from the block on, before 'fsCalls'
|
||||
fsCallsFrom :: Map (ByteString, ByteString) (Word64, CallAnswer),
|
||||
fsFaults :: Map Text Fault,
|
||||
-- | every raw transaction received, newest first
|
||||
fsSent :: [ByteString],
|
||||
@@ -84,15 +105,18 @@ data FakeState = FakeState
|
||||
-- | the key's transaction count at the latest block
|
||||
fsUsed :: Word64,
|
||||
fsBlock :: (Word64, Word64),
|
||||
-- | the times of the blocks mined or set, by number
|
||||
fsBlockTimes :: Map Word64 Word64,
|
||||
-- | the suggested tip and the base fee
|
||||
fsFees :: (Word64, Word64),
|
||||
fsClosed :: Bool
|
||||
}
|
||||
|
||||
fakeGas, fakeTip, fakeBaseFee :: Num a => a
|
||||
fakeGas, fakeTip, fakeBaseFee, fakeMaxFeePerGas :: Num a => a
|
||||
fakeGas = 200000
|
||||
fakeTip = 1000000000
|
||||
fakeBaseFee = 2000000000
|
||||
fakeMaxFeePerGas = 100000000000
|
||||
|
||||
fakeChainId :: Word64
|
||||
fakeChainId = 31337
|
||||
@@ -106,7 +130,7 @@ fakeResolver = address "0x2279b7a0a67db372996a5fab50d91eaa73d2ebe6"
|
||||
fakeRegistrarKey :: S.Secp256k1PrivateKey
|
||||
fakeRegistrarKey = either error id $ S.mkPrivateKey $ BA.replicate 32 0x46
|
||||
|
||||
-- | 2026-01-01, block 1; each mined block is this many seconds later.
|
||||
-- | 2026-01-01, block 1 after the genesis block 0; each mined block is this many seconds later.
|
||||
genesisTime, blockSeconds :: Word64
|
||||
genesisTime = 1767225600
|
||||
blockSeconds = 12
|
||||
@@ -117,12 +141,15 @@ withFakeEthNode action = do
|
||||
newTVarIO
|
||||
FakeState
|
||||
{ fsCalls = M.empty,
|
||||
fsCallsOnce = M.empty,
|
||||
fsCallsFrom = M.empty,
|
||||
fsFaults = M.empty,
|
||||
fsSent = [],
|
||||
fsPool = M.empty,
|
||||
fsReceipts = M.empty,
|
||||
fsUsed = 0,
|
||||
fsBlock = (1, genesisTime),
|
||||
fsBlockTimes = M.fromList [(0, genesisTime - blockSeconds), (1, genesisTime)],
|
||||
fsFees = (fakeTip, fakeBaseFee),
|
||||
fsClosed = False
|
||||
}
|
||||
@@ -140,12 +167,22 @@ fakeChainConfig FakeEthNode {fnUrl} =
|
||||
cBaseRegistrar = fakeBaseRegistrar,
|
||||
cResolver = fakeResolver,
|
||||
cServiceKey = fakeRegistrarKey,
|
||||
cGasLimitCap = 8000000
|
||||
cConfirmations = 3,
|
||||
cGasLimitCap = 8000000,
|
||||
cMaxFeePerGas = fakeMaxFeePerGas
|
||||
}
|
||||
|
||||
answerCall :: FakeEthNode -> Address -> ByteString -> CallAnswer -> IO ()
|
||||
answerCall FakeEthNode {fnState} to calldata a = atomically $ modifyTVar' fnState $ \s -> s {fsCalls = M.insert (unAddress to, calldata) a $ fsCalls s}
|
||||
|
||||
-- | The next call only is answered this way, then the call answers as before.
|
||||
answerCallOnce :: FakeEthNode -> Address -> ByteString -> CallAnswer -> IO ()
|
||||
answerCallOnce FakeEthNode {fnState} to calldata a = atomically $ modifyTVar' fnState $ \s -> s {fsCallsOnce = M.insert (unAddress to, calldata) a $ fsCallsOnce s}
|
||||
|
||||
-- | At this block and later ones the call answers this way, at earlier blocks as before.
|
||||
answerCallFrom :: FakeEthNode -> Word64 -> Address -> ByteString -> CallAnswer -> IO ()
|
||||
answerCallFrom FakeEthNode {fnState} block to calldata a = atomically $ modifyTVar' fnState $ \s -> s {fsCallsFrom = M.insert (unAddress to, calldata) (block, a) $ fsCallsFrom s}
|
||||
|
||||
-- | Every request for the method fails this way until 'clearFault'.
|
||||
setFault :: FakeEthNode -> Text -> Fault -> IO ()
|
||||
setFault FakeEthNode {fnState} method f = atomically $ modifyTVar' fnState $ \s -> s {fsFaults = M.insert method f $ fsFaults s}
|
||||
@@ -153,12 +190,39 @@ setFault FakeEthNode {fnState} method f = atomically $ modifyTVar' fnState $ \s
|
||||
clearFault :: FakeEthNode -> Text -> IO ()
|
||||
clearFault FakeEthNode {fnState} method = atomically $ modifyTVar' fnState $ \s -> s {fsFaults = M.delete method $ fsFaults s}
|
||||
|
||||
-- | The next request for the method stops at the gate it returns.
|
||||
gateNext :: FakeEthNode -> Text -> IO Gate
|
||||
gateNext node method = do
|
||||
gate <- Gate <$> newEmptyTMVarIO <*> newEmptyTMVarIO
|
||||
gate <$ setFault node method (Gated gate)
|
||||
|
||||
-- | Returns once a request is held at the gate.
|
||||
awaitGate :: Gate -> IO ()
|
||||
awaitGate Gate {gArrived} = atomically $ readTMVar gArrived
|
||||
|
||||
releaseGate :: Gate -> IO ()
|
||||
releaseGate Gate {gRelease} = atomically $ putTMVar gRelease ()
|
||||
|
||||
-- | Far longer than a request takes to reach its first chain read.
|
||||
gateArrival :: Int
|
||||
gateArrival = 5000000
|
||||
|
||||
-- | The action, run until its first request for the method is held at the gate.
|
||||
pausedAt :: FakeEthNode -> Text -> IO a -> IO (Async a, Gate)
|
||||
pausedAt node method action = do
|
||||
gate <- gateNext node method
|
||||
inFlight <- async action
|
||||
timeout gateArrival (awaitGate gate) >>= maybe (cancel inFlight >> fail ("no " <> T.unpack method <> " reached the gate")) (const $ pure (inFlight, gate))
|
||||
|
||||
revertFault :: ByteString -> Fault
|
||||
revertFault = RpcError 3 "execution reverted" . Just
|
||||
|
||||
-- | Sets the latest block's number and time.
|
||||
setBlock :: FakeEthNode -> Word64 -> Word64 -> IO ()
|
||||
setBlock FakeEthNode {fnState} number time = atomically $ modifyTVar' fnState $ \s -> s {fsBlock = (number, time)}
|
||||
setBlock FakeEthNode {fnState} number time = atomically $ modifyTVar' fnState $ \s -> s {fsBlock = (number, time), fsBlockTimes = M.insert number time $ fsBlockTimes s}
|
||||
|
||||
setFees :: FakeEthNode -> Word64 -> Word64 -> IO ()
|
||||
setFees FakeEthNode {fnState} tip baseFee = atomically $ modifyTVar' fnState $ \s -> s {fsFees = (tip, baseFee)}
|
||||
|
||||
-- | Mines a new block with these transaction hashes and receipt statuses, and returns its number.
|
||||
-- A mined transaction of the pool uses its nonce, and the pool loses every transaction below it.
|
||||
@@ -168,13 +232,32 @@ mine FakeEthNode {fnState} txs = atomically $ do
|
||||
let number' = number + 1
|
||||
mined = M.fromList [(h, (ok, number')) | (h, ok) <- txs]
|
||||
used = maximum $ fsUsed : [nonce + 1 | (nonce, raw) <- M.toList fsPool, keccak256 raw `M.member` mined]
|
||||
writeTVar fnState s {fsBlock = (number', time + blockSeconds), fsReceipts = M.union mined fsReceipts, fsPool = M.dropWhileAntitone (< used) fsPool, fsUsed = used}
|
||||
writeTVar fnState s {fsBlock = (number', time + blockSeconds), fsBlockTimes = M.insert number' (time + blockSeconds) $ fsBlockTimes s, fsReceipts = M.union mined fsReceipts, fsPool = M.dropWhileAntitone (< used) fsPool, fsUsed = used}
|
||||
pure number'
|
||||
|
||||
-- | The node forgets a transaction it has not mined, as after its own restart.
|
||||
dropTransaction :: FakeEthNode -> ByteString -> IO ()
|
||||
dropTransaction FakeEthNode {fnState} hash = atomically $ modifyTVar' fnState $ \s -> s {fsPool = M.filter ((/= hash) . keccak256) $ fsPool s}
|
||||
|
||||
-- | The node forgets a receipt, as a pruned node does; the nonce stays used.
|
||||
pruneReceipt :: FakeEthNode -> ByteString -> IO ()
|
||||
pruneReceipt FakeEthNode {fnState} hash = atomically $ modifyTVar' fnState $ \s -> s {fsReceipts = M.delete hash $ fsReceipts s}
|
||||
|
||||
-- | The key's next nonce goes to a transaction the node shows nothing of, and the pool loses its own at that nonce.
|
||||
useNextNonce :: FakeEthNode -> IO ()
|
||||
useNextNonce FakeEthNode {fnState} = atomically $ modifyTVar' fnState $ \s@FakeState {fsUsed, fsPool} -> s {fsUsed = fsUsed + 1, fsPool = M.delete fsUsed fsPool}
|
||||
|
||||
-- | The raw transactions in the order they were sent.
|
||||
sentTransactions :: FakeEthNode -> IO [ByteString]
|
||||
sentTransactions FakeEthNode {fnState} = reverse . fsSent <$> readTVarIO fnState
|
||||
|
||||
-- | Waits until the node has received at least this many raw transactions, and returns them in the order they were sent.
|
||||
awaitSent :: FakeEthNode -> Int -> IO [ByteString]
|
||||
awaitSent FakeEthNode {fnState} n = atomically $ do
|
||||
sent <- fsSent <$> readTVar fnState
|
||||
check $ length sent >= n
|
||||
pure $ reverse sent
|
||||
|
||||
fakeApp :: TVar FakeState -> Application
|
||||
fakeApp st req respond = do
|
||||
body <- strictRequestBody req
|
||||
@@ -189,30 +272,51 @@ fakeApp st req respond = do
|
||||
Just NotJson -> respond $ responseLBS ok200 [] "<html>Sign in to provider-api-key</html>"
|
||||
Just (Answers v) -> reply ["result" .= v]
|
||||
Just (Slow delay) -> threadDelay delay >> answered method params
|
||||
Just (Gated Gate {gArrived, gRelease}) -> do
|
||||
atomically $ putTMVar gArrived ()
|
||||
atomically $ takeTMVar gRelease `orElse` (readTVar st >>= check . fsClosed)
|
||||
answered method params
|
||||
Nothing -> answered method params
|
||||
Nothing -> respond $ responseLBS badRequest400 [] "not a JSON-RPC request"
|
||||
where
|
||||
request = J.withObject "request" $ \o -> (,) <$> o .: "method" <*> o .: "params"
|
||||
fault method = M.lookup method . fsFaults <$> readTVarIO st
|
||||
-- a gate holds one request only
|
||||
fault method = atomically $ do
|
||||
s <- readTVar st
|
||||
case M.lookup method $ fsFaults s of
|
||||
f@(Just Gated {}) -> f <$ writeTVar st s {fsFaults = M.delete method $ fsFaults s}
|
||||
f -> pure f
|
||||
answered method params = reply =<< atomically (answer st method params)
|
||||
reply r = respond $ responseLBS ok200 [(hContentType, "application/json")] $ J.encode $ J.object $ ["jsonrpc" .= ("2.0" :: Text), "id" .= (1 :: Int)] <> r
|
||||
|
||||
answer :: TVar FakeState -> Text -> [J.Value] -> STM [JT.Pair]
|
||||
answer st method params = do
|
||||
s@FakeState {fsCalls, fsSent, fsPool, fsReceipts, fsUsed, fsBlock = (number, time), fsFees = (tip, baseFee)} <- readTVar st
|
||||
s@FakeState {fsCalls, fsCallsOnce, fsCallsFrom, fsSent, fsPool, fsReceipts, fsUsed, fsBlock = (number, time), fsBlockTimes, fsFees = (tip, baseFee)} <- readTVar st
|
||||
case (method, params) of
|
||||
("eth_call", [call, _])
|
||||
| Just (to, calldata) <- JT.parseMaybe callTarget call ->
|
||||
pure $ case M.lookup (unAddress to, calldata) fsCalls of
|
||||
Just (Returns ret) -> result $ hex ret
|
||||
Just (Reverts revertData) -> rpcError 3 "execution reverted" (Just revertData)
|
||||
Nothing -> rpcError (-32000) "no fake answer for this call" Nothing
|
||||
("eth_call", [call, J.String tag])
|
||||
| Just (to, calldata) <- JT.parseMaybe callTarget call,
|
||||
Just at <- if tag == "latest" then Just number else fromQuantity tag ->
|
||||
let key = (unAddress to, calldata)
|
||||
callAnswer = \case
|
||||
Just (Returns ret) -> result $ hex ret
|
||||
Just (Reverts revertData) -> rpcError 3 "execution reverted" (Just revertData)
|
||||
Nothing -> rpcError (-32000) "no fake answer for this call" Nothing
|
||||
in case M.lookup key fsCallsOnce of
|
||||
Just a -> callAnswer (Just a) <$ writeTVar st s {fsCallsOnce = M.delete key fsCallsOnce}
|
||||
Nothing -> pure $ callAnswer $ case M.lookup key fsCallsFrom of
|
||||
Just (from, a) | at >= from -> Just a
|
||||
_ -> M.lookup key fsCalls
|
||||
("eth_estimateGas", _) -> pure $ result $ quantity fakeGas
|
||||
("eth_getTransactionCount", [_, J.String "latest"]) -> pure $ result $ quantity $ toInteger fsUsed
|
||||
("eth_getTransactionCount", [_, J.String "pending"]) ->
|
||||
pure $ result $ quantity $ toInteger $ fsUsed + fromIntegral (length $ takeWhile (`M.member` fsPool) [fsUsed ..])
|
||||
("eth_maxPriorityFeePerGas", _) -> pure $ result $ quantity $ toInteger tip
|
||||
("eth_getBlockByNumber", [J.String "latest", _]) -> pure $ result $ blockJson baseFee number time
|
||||
-- a block not mined or set here has the time of the last one before it
|
||||
("eth_getBlockByNumber", [J.String n, _])
|
||||
| Just at <- fromQuantity n -> pure $ result $ case M.lookupLE at fsBlockTimes of
|
||||
Just (_, t) | at <= number -> blockJson baseFee at t
|
||||
_ -> J.Null
|
||||
("eth_getTransactionReceipt", [J.String h]) | Just hash <- fromHex h -> pure $ result $ case M.lookup hash fsReceipts of
|
||||
Just (ok, block) -> J.object ["transactionHash" .= h, "status" .= quantity (if ok then 1 else 0), "blockNumber" .= quantity (toInteger block)]
|
||||
Nothing -> J.Null
|
||||
@@ -248,6 +352,11 @@ rpcError code message revertData = ["error" .= J.object (["code" .= code, "messa
|
||||
quantity :: Integer -> Text
|
||||
quantity n = "0x" <> T.pack (showHex n "")
|
||||
|
||||
fromQuantity :: Text -> Maybe Word64
|
||||
fromQuantity t = case T.stripPrefix "0x" t of
|
||||
Just h | [(n, "")] <- readHex (T.unpack h) -> Just n
|
||||
_ -> Nothing
|
||||
|
||||
fromHex :: Text -> Maybe ByteString
|
||||
fromHex t = T.stripPrefix "0x" t >>= eitherToMaybe . convertFromBase Base16 . encodeUtf8
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ namesChainTests = describe "StoreService names: chain" $ do
|
||||
describe "reads" $ do
|
||||
it "answers eth_call from the node by contract and calldata" $ testEthCall g
|
||||
it "reads the latest block's number, time and base fee" $ testLatestBlock g
|
||||
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
|
||||
@@ -224,6 +225,14 @@ testLatestBlock g = withChain g $ \node c -> do
|
||||
setBlock node 21000000 1798761600
|
||||
latestBlock c `shouldReturn` Right Block {blockNumber = 21000000, blockTimestamp = 1798761600, blockBaseFee = fakeBaseFee}
|
||||
|
||||
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
|
||||
|
||||
@@ -0,0 +1,393 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Bots.StoreService.NamesSenderTests (namesSenderTests) where
|
||||
|
||||
import Bots.StoreService.FakeEthNode
|
||||
import Bots.StoreService.NamesStoreTests (issue, order, ownerA, placeCode, t0, usable)
|
||||
import Bots.StoreService.WebTests (capturingStderr, digestFixture, withServiceStore)
|
||||
import Control.Concurrent.Async (concurrently)
|
||||
import Control.Concurrent.STM
|
||||
import Control.Monad (forM_, replicateM_)
|
||||
import Crypto.Random (ChaChaDRG)
|
||||
import Data.ByteArray.Encoding (Base (Base16), convertFromBase)
|
||||
import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString as B
|
||||
import Data.List (sort)
|
||||
import Data.String (fromString)
|
||||
import qualified Data.Text as T
|
||||
import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime)
|
||||
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 StoreService.Config (ChainConfig (..))
|
||||
import StoreService.Names.Abi (selector, uint256)
|
||||
import StoreService.Names.Chain
|
||||
import StoreService.Names.Phases (RegistrationId (..))
|
||||
import StoreService.Names.Registration (commitCalldata)
|
||||
import StoreService.Names.Revert (RevertName (..))
|
||||
import StoreService.Names.Sender
|
||||
import StoreService.Names.Store (NameTransaction (..), TxFor (..), TxStatus (..), pendingTransactions, registrationTransactions)
|
||||
import Test.Hspec
|
||||
|
||||
namesSenderTests :: Spec
|
||||
namesSenderTests = describe "StoreService names: sender" $ do
|
||||
g <- runIO C.newRandom
|
||||
it "reads a signed transaction's request back from its raw bytes" $ testDecodeSignedTx g
|
||||
it "gives concurrent transactions their own nonces, each saved before it is sent" $ testConcurrentNonces g
|
||||
it "saves nothing and takes no nonce when the dry run reverts or the chain does not answer" $ testNothingSaved g
|
||||
it "refuses a second transaction for work with one in flight" $ testOneInFlight g
|
||||
it "settles a receipt only once it is confirmations deep, with its status" $ testConfirmationDepth g
|
||||
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 "hands a dropped transaction whose nonce was used to its owner, who judges it from chain state before any new signature" $ testDroppedNonceUsed g
|
||||
it "hands a mined transaction whose receipt was pruned to its owner, signing nothing more" $ testPrunedReceipt 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" $ testRefusedSendLogged g
|
||||
it "logs anvil's capitalized underfunded refusal as an error once" $ testCapitalizedUnderfundedLogged 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
|
||||
|
||||
data Setup = Setup
|
||||
{ node :: FakeEthNode,
|
||||
store :: DBStore,
|
||||
clock :: TVar UTCTime,
|
||||
chain :: ChainClient,
|
||||
random :: TVar ChaChaDRG
|
||||
}
|
||||
|
||||
withSetup :: TVar ChaChaDRG -> (Setup -> IO a) -> IO a
|
||||
withSetup = withCapSetup fakeMaxFeePerGas
|
||||
|
||||
-- | Two code orders, r1 and r2, two confirmations, and this max fee per gas.
|
||||
withCapSetup :: Word64 -> TVar ChaChaDRG -> (Setup -> IO a) -> IO a
|
||||
withCapSetup cap random action = withFakeEthNode $ \node -> withServiceStore $ \store -> do
|
||||
forM_ [(1, "r1", "alicebakery"), (2, "r2", "bobbakery")] $ \(n, rid, label) -> do
|
||||
code <- issue store n usable
|
||||
placeCode store rid ownerA (order label n) code `shouldReturn` Right ()
|
||||
clock <- newTVarIO t0
|
||||
chain <- newChainClient random (fakeChainConfig node) {cConfirmations = 2, cMaxFeePerGas = cap}
|
||||
action Setup {node, store, clock, chain, random}
|
||||
|
||||
start :: Setup -> IO Sender
|
||||
start Setup {random, chain, store, clock} = newSender random chain store (readTVarIO clock)
|
||||
|
||||
passTime :: Setup -> NominalDiffTime -> IO ()
|
||||
passTime Setup {clock} t = atomically $ modifyTVar' clock (addUTCTime t)
|
||||
|
||||
testDecodeSignedTx :: TVar ChaChaDRG -> IO ()
|
||||
testDecodeSignedTx g = withFakeEthNode $ \node -> do
|
||||
c <- newChainClient g $ fakeChainConfig node
|
||||
-- calldata over 55 bytes takes a long RLP length
|
||||
let request = TxRequest {trTo = fakeSponsor, trData = B.replicate 300 0xab, trNonce = 70000, trGasLimit = 450000, trMaxFeePerGas = 31000000000, trMaxPriorityFeePerGas = 0}
|
||||
Right SignedTx {stRaw} <- signTransaction g c request
|
||||
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"
|
||||
|
||||
testConcurrentNonces :: TVar ChaChaDRG -> IO ()
|
||||
testConcurrentNonces g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
-- both dry runs pass before either reads the registrar's nonce
|
||||
setFault node "eth_getTransactionCount" $ Slow 300000
|
||||
(Right a, Right b) <- concurrently (submitTransaction sender (TxCommit r1) fakeController $ commit 1) (submitTransaction sender (TxCommit r2) fakeController $ commit 2)
|
||||
clearFault node "eth_getTransactionCount"
|
||||
sort [ptNonce a, ptNonce b] `shouldBe` [0, 1]
|
||||
saved <- withTransaction store pendingTransactions
|
||||
map (\NameTransaction {ntFor, ntNonce, ntHash} -> (ntFor, ntNonce, ntHash)) saved
|
||||
`shouldMatchList` [(TxCommit r1, ptNonce a, ptHash a), (TxCommit r2, ptNonce b, ptHash b)]
|
||||
sentTransactions node `shouldReturn` map ntRaw saved
|
||||
mapM_ (mineHash node) [ptHash a, ptHash b]
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
forM_ [a, b] $ \p -> outcomeOf p >>= (`shouldSatisfy` confirmedAs (ptHash p))
|
||||
Right c <- submitTransaction sender (TxRegister r1) fakeSponsor $ commit 3
|
||||
ptNonce c `shouldBe` 2
|
||||
|
||||
testNothingSaved :: TVar ChaChaDRG -> IO ()
|
||||
testNothingSaved g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
setFault node "eth_estimateGas" $ revertFault insufficientAllowance
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEChain (ChainReverted $ Just RNInsufficientAllowance)
|
||||
clearFault node "eth_estimateGas"
|
||||
setFault node "eth_maxPriorityFeePerGas" HttpFailure
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEChain (ChainUnavailable "eth_maxPriorityFeePerGas: HTTP 502")
|
||||
clearFault node "eth_maxPriorityFeePerGas"
|
||||
withTransaction store pendingTransactions `shouldReturn` []
|
||||
sentTransactions node `shouldReturn` []
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
ptNonce p `shouldBe` 0
|
||||
|
||||
testOneInFlight :: TVar ChaChaDRG -> IO ()
|
||||
testOneInFlight g = withSetup g $ \s@Setup {node} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEInFlight
|
||||
Right other <- submitTransaction sender (TxRegister r1) fakeSponsor (commit 2)
|
||||
ptNonce other `shouldBe` 1
|
||||
(fmap ptHash <$> inFlightTransaction sender (TxCommit r1)) `shouldReturn` Just (ptHash p)
|
||||
length <$> sentTransactions node `shouldReturn` 2
|
||||
|
||||
testConfirmationDepth :: TVar ChaChaDRG -> IO ()
|
||||
testConfirmationDepth g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
let TxHash h = ptHash p
|
||||
block <- mine node [(h, False)]
|
||||
followTransactions sender
|
||||
settled sender (TxCommit r1) `shouldReturn` False
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
outcomeOf p `shouldReturn` TOConfirmed (ptHash p) Receipt {receiptSuccess = False, receiptBlock = block}
|
||||
settled sender (TxCommit r1) `shouldReturn` True
|
||||
statuses store r1 `shouldReturn` [(ptHash p, TSReverted, Just block)]
|
||||
|
||||
testFailedSend :: TVar ChaChaDRG -> IO ()
|
||||
testFailedSend g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
setFault node "eth_sendRawTransaction" HttpFailure
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
clearFault node "eth_sendRawTransaction"
|
||||
sentTransactions node `shouldReturn` []
|
||||
[NameTransaction {ntRaw, ntHash}] <- withTransaction store pendingTransactions
|
||||
ntHash `shouldBe` ptHash p
|
||||
followTransactions sender
|
||||
sentTransactions node `shouldReturn` [ntRaw]
|
||||
transactionByHash' s (ptHash p) `shouldReturn` Just TxPending
|
||||
|
||||
testFeeBump :: TVar ChaChaDRG -> IO ()
|
||||
testFeeBump g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
[firstRaw] <- sentTransactions node
|
||||
Right first@TxRequest {trMaxFeePerGas = firstMax, trMaxPriorityFeePerGas = firstTip} <- pure $ decodeSignedTx firstRaw
|
||||
(firstTip, firstMax) `shouldBe` (fakeTip, 2 * fakeBaseFee + fakeTip)
|
||||
passTime s $ replaceAfter - 1
|
||||
followTransactions sender
|
||||
passTime s 1
|
||||
followTransactions sender
|
||||
length <$> sentTransactions node `shouldReturn` 1
|
||||
setFees node fakeTip (2 * fakeBaseFee)
|
||||
followTransactions sender
|
||||
length <$> sentTransactions node `shouldReturn` 1
|
||||
passTime s replaceAfter
|
||||
followTransactions sender
|
||||
[_, bumpedRaw] <- sentTransactions node
|
||||
let bumpedHash = TxHash $ C.keccak256 bumpedRaw
|
||||
decodeSignedTx bumpedRaw `shouldBe` Right first {trMaxPriorityFeePerGas = fakeTip + fakeTip `div` 8 + 1, trMaxFeePerGas = 4 * fakeBaseFee + fakeTip}
|
||||
transactionByHash' s (ptHash p) `shouldReturn` Nothing
|
||||
transactionByHash' s bumpedHash `shouldReturn` Just TxPending
|
||||
map (\(h, status, _) -> (h, status)) <$> statuses store r1 `shouldReturn` [(ptHash p, TSPending), (bumpedHash, TSPending)]
|
||||
mineHash node bumpedHash
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
TOConfirmed minedHash Receipt {receiptSuccess = True, receiptBlock} <- outcomeOf p
|
||||
minedHash `shouldBe` bumpedHash
|
||||
statuses store r1 `shouldReturn` [(ptHash p, TSDropped, Nothing), (bumpedHash, TSMined, Just receiptBlock)]
|
||||
|
||||
testRestartRebroadcast :: TVar ChaChaDRG -> IO ()
|
||||
testRestartRebroadcast g = withSetup g $ \s@Setup {node, store} -> do
|
||||
crashed <- start s
|
||||
Right p <- submitTransaction crashed (TxCommit r1) fakeController (commit 1)
|
||||
[raw] <- sentTransactions node
|
||||
let TxHash h = ptHash p
|
||||
dropTransaction node h
|
||||
restarted <- start s
|
||||
Just resumed <- inFlightTransaction restarted (TxCommit r1)
|
||||
(ptHash resumed, ptNonce resumed) `shouldBe` (ptHash p, 0)
|
||||
followTransactions restarted
|
||||
sentTransactions node `shouldReturn` [raw, raw]
|
||||
followTransactions restarted
|
||||
sentTransactions node `shouldReturn` [raw, raw]
|
||||
Right next <- submitTransaction restarted (TxCommit r2) fakeController (commit 2)
|
||||
ptNonce next `shouldBe` 1
|
||||
_ <- mine node [(h, True)]
|
||||
_ <- mine node []
|
||||
followTransactions restarted
|
||||
outcomeOf resumed >>= (`shouldSatisfy` confirmedAs (ptHash p))
|
||||
map (\(_, status, _) -> status) <$> statuses store r1 `shouldReturn` [TSMined]
|
||||
|
||||
testDroppedNonceUsed :: TVar ChaChaDRG -> IO ()
|
||||
testDroppedNonceUsed g = withSetup g $ \s@Setup {node, store, chain} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
let TxHash h = ptHash p
|
||||
dropTransaction node h
|
||||
useNextNonce node
|
||||
followTransactions sender
|
||||
settled sender (TxCommit r1) `shouldReturn` False
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
outcomeOf p `shouldReturn` TONonceUsed
|
||||
statuses store r1 `shouldReturn` [(ptHash p, TSDropped, Nothing)]
|
||||
length <$> sentTransactions node `shouldReturn` 1
|
||||
-- the owner: the commitment reads 0, so the commit never mined and is sent again
|
||||
answerCall node fakeController commitmentsCall $ Returns $ uint256 (0 :: Int)
|
||||
ethCall chain fakeController commitmentsCall `shouldReturn` Right (uint256 (0 :: Int))
|
||||
Right again <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
ptNonce again `shouldBe` 1
|
||||
length <$> sentTransactions node `shouldReturn` 2
|
||||
|
||||
testPrunedReceipt :: TVar ChaChaDRG -> IO ()
|
||||
testPrunedReceipt g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
let TxHash h = ptHash p
|
||||
_ <- mine node [(h, True)]
|
||||
pruneReceipt node h
|
||||
followTransactions sender
|
||||
settled sender (TxCommit r1) `shouldReturn` False
|
||||
_ <- mine node []
|
||||
followTransactions sender
|
||||
outcomeOf p `shouldReturn` TONonceUsed
|
||||
statuses store r1 `shouldReturn` [(ptHash p, TSDropped, Nothing)]
|
||||
followTransactions sender
|
||||
length <$> sentTransactions node `shouldReturn` 1
|
||||
withTransaction store pendingTransactions `shouldReturn` []
|
||||
|
||||
testFailedRound :: TVar ChaChaDRG -> IO ()
|
||||
testFailedRound g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
mineHash node (ptHash p)
|
||||
_ <- mine node []
|
||||
renameTransactions store "sx_badge_service_name_transactions" "name_transactions_away"
|
||||
senderRound sender
|
||||
settled sender (TxCommit r1) `shouldReturn` False
|
||||
renameTransactions store "name_transactions_away" "sx_badge_service_name_transactions"
|
||||
senderRound sender
|
||||
outcomeOf p >>= (`shouldSatisfy` confirmedAs (ptHash p))
|
||||
map (\(_, status, _) -> status) <$> statuses store r1 `shouldReturn` [TSMined]
|
||||
where
|
||||
renameTransactions store from to = withTransaction store $ \db -> DB.execute_ db $ fromString $ "ALTER TABLE " <> from <> " RENAME TO " <> to
|
||||
|
||||
testRefusedSendLogged :: TVar ChaChaDRG -> IO ()
|
||||
testRefusedSendLogged g = withSetup g $ \s@Setup {node} -> do
|
||||
sender <- start s
|
||||
-- the specs run in parallel, so this one's lines are told apart by a nonce no other spec reaches
|
||||
replicateM_ 9 $ useNextNonce node
|
||||
let underfunded = "insufficient funds for gas * price + value: balance 0, tx cost 4600000000000000, overshot 4600000000000000"
|
||||
logged <- capturingStderr $ do
|
||||
setFault node "eth_sendRawTransaction" $ RpcError (-32000) underfunded Nothing
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
ptNonce p `shouldBe` 9
|
||||
followTransactions sender
|
||||
followTransactions sender
|
||||
setFault node "eth_sendRawTransaction" HttpFailure
|
||||
followTransactions sender
|
||||
followTransactions sender
|
||||
clearFault node "eth_sendRawTransaction"
|
||||
followTransactions sender
|
||||
let linesWith tag message = length $ filter (\l -> tag `T.isPrefixOf` l && message `T.isInfixOf` l) $ T.lines logged
|
||||
linesWith "[ERROR" "names sender: nonce 9: not sent: ChainUnavailable \"eth_sendRawTransaction: error -32000: insufficient funds" `shouldBe` 1
|
||||
linesWith "[WARN" "names sender: nonce 9: not sent: ChainUnavailable \"eth_sendRawTransaction: HTTP 502\"" `shouldBe` 1
|
||||
linesWith "[" "names sender: nonce 9" `shouldBe` 2
|
||||
linesWith "[" "names sender: rebroadcasting nonce 9" `shouldBe` 0
|
||||
length <$> sentTransactions node `shouldReturn` 1
|
||||
|
||||
testCapitalizedUnderfundedLogged :: TVar ChaChaDRG -> IO ()
|
||||
testCapitalizedUnderfundedLogged g = withSetup g $ \s@Setup {node} -> do
|
||||
sender <- start s
|
||||
-- a nonce no other spec reaches, as in testRefusedSendLogged
|
||||
replicateM_ 13 $ useNextNonce node
|
||||
logged <- capturingStderr $ do
|
||||
setFault node "eth_sendRawTransaction" $ RpcError (-32003) "Insufficient funds for gas * price + value" Nothing
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
ptNonce p `shouldBe` 13
|
||||
followTransactions sender
|
||||
followTransactions sender
|
||||
let linesWith :: T.Text -> T.Text -> Int
|
||||
linesWith tag message = length $ filter (\l -> tag `T.isPrefixOf` l && message `T.isInfixOf` l) $ T.lines logged
|
||||
linesWith "[ERROR" "names sender: nonce 13: not sent: ChainUnavailable \"eth_sendRawTransaction: error -32003: Insufficient funds" `shouldBe` 1
|
||||
linesWith "[" "names sender: nonce 13" `shouldBe` 1
|
||||
|
||||
testFeeCapSubmit :: TVar ChaChaDRG -> IO ()
|
||||
testFeeCapSubmit g = withSetup g $ \s@Setup {node, store} -> do
|
||||
sender <- start s
|
||||
setFees node fakeTip (fakeMaxFeePerGas - fakeTip + 1)
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEFeeAboveCap (fakeMaxFeePerGas + 1)
|
||||
-- twice the base fee is past Word64, which would wrap to a near-zero fee
|
||||
let hugeBaseFee = maxBound `div` 2 + 1 :: Word64
|
||||
setFees node fakeTip hugeBaseFee
|
||||
submitTransaction sender (TxCommit r1) fakeController (commit 1) `refusedWith` SEFeeAboveCap (toInteger hugeBaseFee + fakeTip)
|
||||
withTransaction store pendingTransactions `shouldReturn` []
|
||||
sentTransactions node `shouldReturn` []
|
||||
let baseFee = fakeMaxFeePerGas `div` 2
|
||||
setFees node fakeTip baseFee
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
ptNonce p `shouldBe` 0
|
||||
[raw] <- sentTransactions node
|
||||
(\TxRequest {trMaxPriorityFeePerGas, trMaxFeePerGas} -> (trMaxPriorityFeePerGas, trMaxFeePerGas)) <$> decodeSignedTx raw `shouldBe` Right (fakeTip, fakeMaxFeePerGas)
|
||||
|
||||
testFeeCapReplace :: TVar ChaChaDRG -> IO ()
|
||||
testFeeCapReplace g = withCapSetup cap g $ \s@Setup {node} -> do
|
||||
sender <- start s
|
||||
Right p <- submitTransaction sender (TxCommit r1) fakeController (commit 1)
|
||||
[firstRaw] <- sentTransactions node
|
||||
Right first <- pure $ decodeSignedTx firstRaw
|
||||
trMaxFeePerGas first `shouldBe` 2 * fakeBaseFee + fakeTip
|
||||
setFees node fakeTip (2 * fakeBaseFee)
|
||||
passTime s replaceAfter
|
||||
followTransactions sender
|
||||
[_, bumpedRaw] <- sentTransactions node
|
||||
decodeSignedTx bumpedRaw `shouldBe` Right first {trMaxPriorityFeePerGas = fakeTip + fakeTip `div` 8 + 1, trMaxFeePerGas = cap}
|
||||
passTime s replaceAfter
|
||||
followTransactions sender
|
||||
length <$> sentTransactions node `shouldReturn` 2
|
||||
let bumpedHash = C.keccak256 bumpedRaw
|
||||
dropTransaction node bumpedHash
|
||||
followTransactions sender
|
||||
sentTransactions node `shouldReturn` [firstRaw, bumpedRaw, bumpedRaw]
|
||||
(fmap ptHash <$> inFlightTransaction sender (TxCommit r1)) `shouldReturn` Just (TxHash bumpedHash)
|
||||
ptNonce p `shouldBe` 0
|
||||
where
|
||||
-- above the first max fee and its bump, below the market's headroom fee and a second bump
|
||||
cap = 2 * fakeBaseFee + fakeTip + 1000000000
|
||||
|
||||
-- fixtures
|
||||
|
||||
r1, r2 :: RegistrationId
|
||||
r1 = RegistrationId "r1"
|
||||
r2 = RegistrationId "r2"
|
||||
|
||||
commit :: Word8 -> ByteString
|
||||
commit = commitCalldata . digestFixture
|
||||
|
||||
commitmentsCall :: ByteString
|
||||
commitmentsCall = selector "commitments(bytes32)" <> digestFixture 1
|
||||
|
||||
-- | viem's encodeErrorResult of InsufficientAllowance(10000, 2500)
|
||||
insufficientAllowance :: ByteString
|
||||
insufficientAllowance = unhex "2a1b2dd8000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000009c4"
|
||||
|
||||
mineHash :: FakeEthNode -> TxHash -> IO ()
|
||||
mineHash node (TxHash h) = () <$ mine node [(h, True)]
|
||||
|
||||
confirmedAs :: TxHash -> TxOutcome -> Bool
|
||||
confirmedAs hash = \case
|
||||
TOConfirmed h Receipt {receiptSuccess = True} -> h == hash
|
||||
_ -> False
|
||||
|
||||
outcomeOf :: PendingTx -> IO TxOutcome
|
||||
outcomeOf p = awaitOutcome 2000000 p >>= maybe (fail "the transaction is not settled") pure
|
||||
|
||||
settled :: Sender -> TxFor -> IO Bool
|
||||
settled sender for = null <$> inFlightTransaction sender for
|
||||
|
||||
statuses :: DBStore -> RegistrationId -> IO [(TxHash, TxStatus, Maybe Word64)]
|
||||
statuses store rid = withTransaction store $ \db -> map (\NameTransaction {ntHash, ntStatus, ntMinedBlock} -> (ntHash, ntStatus, ntMinedBlock)) <$> registrationTransactions db rid
|
||||
|
||||
transactionByHash' :: Setup -> TxHash -> IO (Maybe TxState)
|
||||
transactionByHash' Setup {chain} h = either (error . show) id <$> transactionByHash chain h
|
||||
|
||||
refusedWith :: IO (Either SendError PendingTx) -> SendError -> Expectation
|
||||
refusedWith submit expected =
|
||||
submit >>= \case
|
||||
Left e -> e `shouldBe` expected
|
||||
Right p -> expectationFailure $ "sent with nonce " <> show (ptNonce p)
|
||||
|
||||
unhex :: ByteString -> ByteString
|
||||
unhex = either error id . convertFromBase Base16
|
||||
Reference in new issue
Block a user