diff --git a/apps/simplex-store-service/src/StoreService/Names/Env.hs b/apps/simplex-store-service/src/StoreService/Names/Env.hs index b9c7d07dfd..4cd600989d 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Env.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Env.hs @@ -11,6 +11,7 @@ import StoreService.Names.Chain (ChainClient, newChainClient) import StoreService.Names.Contracts (Contracts, newContracts) import StoreService.Names.Phases (Phases, newPhases) import StoreService.Names.Profiles (Profiles, chatProfile, defaultProfileLimits, monotonicSeconds, newProfiles, systemResolver) +import StoreService.Names.Records (Records, newRecords) import StoreService.Names.Sender (Sender, newSender) import StoreService.Names.Turns (Turns) import StoreService.Providers (Provider) @@ -27,15 +28,17 @@ data NamesEnv = NamesEnv neContracts :: Contracts, neProfiles :: Profiles, neSender :: Sender, + neRecords :: Records, neWaiters :: Waiters, neProviders :: [Provider] } newNamesEnv :: ChatController -> UserId -> NamesConfig -> DBStore -> Turns -> Waiters -> [Provider] -> IO NamesEnv -newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain} neStore neTurns neWaiters neProviders = do +newNamesEnv neChat@ChatController {random} neUserId neConfig@NamesConfig {nTLD, nChain, nRecordEditsPerYear} neStore neTurns neWaiters neProviders = do nePhases <- newPhases neChain <- newChainClient random nChain neContracts <- newContracts nTLD neChain neProfiles <- newProfiles defaultProfileLimits monotonicSeconds (chatProfile neChat neUserId) systemResolver neSender <- newSender random neChain neStore getCurrentTime - pure NamesEnv {neChat, neUserId, neConfig, neStore, nePhases, neTurns, neChain, neContracts, neProfiles, neSender, neWaiters, neProviders} + neRecords <- newRecords neStore neTurns neContracts neChain neSender nTLD nRecordEditsPerYear getCurrentTime + pure NamesEnv {neChat, neUserId, neConfig, neStore, nePhases, neTurns, neChain, neContracts, neProfiles, neSender, neRecords, neWaiters, neProviders} diff --git a/apps/simplex-store-service/src/StoreService/Names/Records.hs b/apps/simplex-store-service/src/StoreService/Names/Records.hs new file mode 100644 index 0000000000..2b35f60101 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Names/Records.hs @@ -0,0 +1,414 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiWayIf #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TupleSections #-} + +-- | Record edits signed by a name's owner, each request relayed as one transaction through the registrar key's sender and followed to its outcome. +module StoreService.Names.Records + ( Records (rsPendingAfter, rsEditsPerYear), + EditRequest (..), + EditAnswer (..), + EditsLeft (..), + newRecords, + nameEditsLeft, + relayEdit, + runRecordEdits, + recordsRound, + ) +where + +import Control.Concurrent (threadDelay) +import Control.Concurrent.STM +import Control.Exception (bracket) +import Control.Monad (forM, forM_, forever, unless, void, when) +import Control.Monad.Except (ExceptT (..), runExceptT) +import Control.Monad.IO.Class (liftIO) +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import Data.Int (Int64) +import Data.List.NonEmpty (NonEmpty (..)) +import qualified Data.List.NonEmpty as L +import Data.Maybe (listToMaybe) +import Data.Set (Set) +import qualified Data.Set as S +import Data.Text (Text) +import Data.Text.Encoding (encodeUtf8) +import Data.Time.Clock (NominalDiffTime, UTCTime, addUTCTime, diffUTCTime) +import Data.Word (Word64) +import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) +import qualified Simplex.Messaging.Agent.Store.DB as DB +import Simplex.Messaging.Crypto (sha256Hash) +import Simplex.Messaging.Eth.Address (Address, recoverAddress) +import Simplex.Messaging.Eth.EIP712 (Eip712Domain, serializeSignature) +import Simplex.Messaging.SimplexName (LabelHash, SimplexTLD) +import Simplex.Messaging.Util (tshow) +import StoreService.Config (ChainConfig (..)) +import StoreService.Log (logWarn, safely) +import StoreService.Names.Chain +import StoreService.Names.Contracts (Contracts, NameStatus (..), RegisteredName (..), nameStatus, resolverNonce, textRecord) +import StoreService.Names.Registration (Node, channelRecordKey, clearRecordsDigest, clearRecordsWithSigCalldata, contactRecordKey, labelNode, multicallCalldata, resolverDomain, setTextDigest, setTextWithSigCalldata) +import StoreService.Names.Revert (RevertName (..), revertText) +import StoreService.Names.Sender (SendError (..), Sender, submitTransaction) +import StoreService.Names.Store +import StoreService.Names.Turns (Turns, withLabelTurn) + +data Records = Records + { rsStore :: DBStore, + rsTurns :: Turns, + rsContracts :: Contracts, + rsChain :: ChainClient, + rsSender :: Sender, + rsTLD :: SimplexTLD, + rsEditsPerYear :: Int, + rsNow :: IO UTCTime, + -- | microseconds a request waits for its edit's outcome + rsPendingAfter :: Int, + -- | the edits a request or the lane is sending or judging, so no edit is sent twice + rsClaimed :: TVar (Set Int64), + -- | the pending edits whose last chain read failed + rsStalled :: TVar (Set Int64), + -- | grows on every settlement and stall, waking the requests that wait for an edit + rsChanges :: TVar Int + } + +-- | The changes of one Save, in nonce order, relayed together or not at all. +data EditRequest = EditRequest + { erLabelHash :: LabelHash, + erChanges :: NonEmpty SignedChange + } + deriving (Eq, Show) + +data EditAnswer + = -- | mined, Nothing when found applied with the applying transaction unknown + EAApplied (Maybe TxHash) + | -- | the revert's name when known; the nonce is free again + EAReverted (Maybe Text) + | -- | not settled within the wait, or the chain stopped answering after the send; the edit goes on + EAPending + | -- | the signers, the deadline, the owner or the nonce + EARefused + | -- | the chain failed before anything was sent, and nothing is stored + EAUnavailable + | -- | the per-name cap, with the seconds until another edit fits + EACapped Int + | -- | the name's yearly limit is used up until this time + EALimited UTCTime + deriving (Eq, Show) + +-- | The edits a name has left in its window, and when the next one returns; Nothing while none counts. +data EditsLeft = EditsLeft {elLeft :: Int, elNextAt :: Maybe UTCTime} + deriving (Eq, Show) + +-- | What the chain shows of an edit none of whose transactions mined. +data Judgment + = -- | one of its own hashes, or Nothing when the nonce passed with the record holding its value + Applied (Maybe TxHash) + | -- | the nonce passed with the record holding another value + NonceTaken + | NonceFree + +-- | Below a proxy's idle timeout; the edit goes on after it. +editPendingAfter :: Int +editPendingAfter = 90000000 + +-- | Relayed edits per name in 'editCapWindow'. +editCap :: Int +editCap = 10 + +editCapWindow :: NominalDiffTime +editCapWindow = 3600 + +-- | The window of the yearly limit, in 365-day years as the names' terms count them. +editLimitWindow :: NominalDiffTime +editLimitWindow = 365 * 86400 + +recordsInterval :: Int +recordsInterval = 4000000 + +newRecords :: DBStore -> Turns -> Contracts -> ChainClient -> Sender -> SimplexTLD -> Int -> IO UTCTime -> IO Records +newRecords rsStore rsTurns rsContracts rsChain rsSender rsTLD rsEditsPerYear rsNow = do + rsClaimed <- newTVarIO S.empty + rsStalled <- newTVarIO S.empty + rsChanges <- newTVarIO 0 + pure Records {rsStore, rsTurns, rsContracts, rsChain, rsSender, rsTLD, rsEditsPerYear, rsNow, rsPendingAfter = editPendingAfter, rsClaimed, rsStalled, rsChanges} + +-- | A pending or mined edit counts. One refused, never sent, or reverted on chain does not. +nameEditsLeft :: DB.Connection -> Int -> LabelHash -> UTCTime -> IO EditsLeft +nameEditsLeft db perYear lh now = do + times <- countedEditTimes db lh $ addUTCTime (negate editLimitWindow) now + let counted = length times + -- the oldest edit returns one, unless a lowered limit is overrun and later ones must go too + pure EditsLeft {elLeft = max 0 $ perYear - counted, elNextAt = addUTCTime editLimitWindow <$> listToMaybe (drop (counted - perYear) times)} + +-- | When the name's yearly limit is used up, the time the next edit fits. +usedUpUntil :: EditsLeft -> Maybe UTCTime +usedUpUntil EditsLeft {elLeft, elNextAt} = if elLeft == 0 then elNextAt else Nothing + +-- | An identical repeat of a sent edit is answered from it, and the yearly limit is checked, before any chain read. +-- The wait for an outcome is bounded from the request's arrival, so the checks and the send count toward it. +relayEdit :: Records -> EditRequest -> IO EditAnswer +relayEdit rs@Records {rsPendingAfter, rsEditsPerYear, rsNow} req@EditRequest {erLabelHash, erChanges} = do + timer <- registerDelay rsPendingAfter + sequence <$> mapM (\(digest, SignedChange {scSignature}) -> recoverAddress scSignature digest) signed >>= \case + Right (signer :| others) | all (== signer) others -> + withStore rs (`getRecordEditByBody` bodyHash) >>= \case + Just RecordEdit {reId} -> awaitEdit rs timer reId + Nothing -> do + now <- rsNow + withStore rs (\db -> usedUpUntil <$> nameEditsLeft db rsEditsPerYear erLabelHash now) + >>= maybe (newEdit rs timer req signer bodyHash) (pure . EALimited) + _ -> pure EARefused + where + signed = L.map (\change -> (changeDigest rs erLabelHash change, change)) erChanges + -- one change hashes as edits stored before requests took two, so their repeats still match + bodyHash = sha256Hash $ B.concat [digest <> serializeSignature scSignature | (digest, SignedChange {scSignature}) <- L.toList signed] + +newEdit :: Records -> TVar Bool -> EditRequest -> Address -> ByteString -> IO EditAnswer +newEdit rs timer req signer bodyHash = + chainAccepts rs req signer >>= \case + Left _ -> pure EAUnavailable + Right False -> pure EARefused + Right True -> + insertEdit rs req signer bodyHash >>= \case + Left answer -> pure answer + Right (Just editId) -> sendNew rs timer editId + -- a pending or mined edit holds the nonce: the identical twin is waited for, any other body refused + Right Nothing -> withStore rs (`getRecordEditByBody` bodyHash) >>= maybe (pure EARefused) (awaitEdit rs timer . reId) + +-- | The earliest deadline after the latest block's time, the signer the registry's owner on the default resolver, the first nonce the owner's next. +chainAccepts :: Records -> EditRequest -> Address -> IO (Either ChainError Bool) +chainAccepts Records {rsChain, rsContracts} EditRequest {erLabelHash, erChanges} signer = runExceptT $ do + Block {blockTimestamp} <- ExceptT $ latestBlock rsChain + if earliestDeadline erChanges <= blockTimestamp + then pure False + else + ExceptT (nameStatus rsContracts erLabelHash) >>= \case + NSRegistered RegisteredName {rnOwner, rnResolver} + | rnOwner == signer && rnResolver == cResolver (ccConfig rsChain) -> (== firstNonce erChanges) <$> ExceptT (resolverNonce rsContracts signer) + _ -> pure False + +-- | Left when the name's yearly limit or hourly cap is used up, checked again under the name's turn against a concurrent edit. +-- Right Nothing when a pending or mined edit holds the first nonce. +insertEdit :: Records -> EditRequest -> Address -> ByteString -> IO (Either EditAnswer (Maybe Int64)) +insertEdit Records {rsStore, rsTurns, rsEditsPerYear, rsNow} EditRequest {erLabelHash, erChanges} signer bodyHash = + withLabelTurn rsTurns erLabelHash $ do + now <- rsNow + withTransaction rsStore $ \db -> do + limited <- usedUpUntil <$> nameEditsLeft db rsEditsPerYear erLabelHash now + times <- relayedEditTimes db erLabelHash (addUTCTime (negate editCapWindow) now) + let relayed = length times + case limited of + Just nextAt -> pure $ Left $ EALimited nextAt + Nothing + | relayed >= editCap -> pure $ Left $ EACapped $ max 1 $ ceiling $ diffUTCTime (addUTCTime editCapWindow $ times !! (relayed - editCap)) now + | otherwise -> Right <$> insertRecordEdit db edit now + where + edit = NewRecordEdit {nreLabelHash = erLabelHash, nreOwner = signer, nreChanges = erChanges, nreBodyHash = bodyHash} + +-- | An edit that was not sent is answered and deleted while still claimed, so the lane cannot send it after. +sendNew :: Records -> TVar Bool -> Int64 -> IO EditAnswer +sendNew rs timer editId = + withClaim rs editId Nothing (withStore rs (`getRecordEdit` editId) >>= maybe (pure Nothing) send) >>= maybe (awaitEdit rs timer editId) pure + where + send edit = + sendEdit rs edit >>= \case + Right () -> pure Nothing + Left e -> do + answer <- case e of + SEChain (ChainReverted name) -> either (const EAUnavailable) (dryRunAnswer name) <$> judge rs edit + _ -> EAUnavailable <$ logWarn ("names records: edit " <> tshow editId <> " not sent: " <> tshow e) + Just answer <$ forget rs editId + -- anyone can relay a signed edit, so a revert may mean another relayer applied it + dryRunAnswer name = \case + Applied tx -> EAApplied tx + NonceTaken -> EAReverted $ revertText RNInvalidNonce + NonceFree -> EAReverted $ revertText =<< name + +-- | Left when nothing was sent: the dry run reverted or the chain failed. +sendEdit :: Records -> RecordEdit -> IO (Either SendError ()) +sendEdit rs@Records {rsSender} edit@RecordEdit {reId} = + submitTransaction rsSender (TxEdit reId) (resolver rs) (editCalldata rs edit) >>= \case + Right _ -> Right () <$ unstall rs reId + Left SEInFlight -> pure $ Right () + Left e -> pure $ Left e + +-- | Only sent edits are stored, so one never sent leaves no row and its repeat is checked anew. +forget :: Records -> Int64 -> IO () +forget rs editId = withStore rs (`deleteUnsentEdit` editId) >> settled rs editId + +-- | Waits until the edit settles, the chain stops answering for it, or the timer fires. +awaitEdit :: Records -> TVar Bool -> Int64 -> IO EditAnswer +awaitEdit rs@Records {rsChanges, rsStalled} timer editId = loop + where + loop = do + -- read before the row, so a change committed after the read still wakes this request + seen <- readTVarIO rsChanges + withStore rs (`getRecordEdit` editId) >>= \case + Nothing -> pure EAUnavailable + Just RecordEdit {reState = ESMined tx} -> pure $ EAApplied tx + Just RecordEdit {reState = ESReverted revert} -> pure $ EAReverted revert + Just RecordEdit {reState = ESPending} -> do + stalled <- S.member editId <$> readTVarIO rsStalled + if stalled + then pure EAPending + else do + changed <- atomically $ (True <$ (readTVar rsChanges >>= check . (/= seen))) `orElse` (False <$ (readTVar timer >>= check)) + if changed then loop else pure EAPending + +runRecordEdits :: Records -> IO () +runRecordEdits rs = forever $ recordsRound rs >> threadDelay recordsInterval + +-- | One pass over the pending edits that no request is sending. A pass or an edit that fails is logged, and the lane goes on. +recordsRound :: Records -> IO () +recordsRound rs@Records {rsChain} = void $ safely "names records: the round" $ do + editIds <- withStore rs pendingEditIds + unless (null editIds) $ + runExceptT (ExceptT (latestBlock rsChain) >>= \tip -> (tip,) <$> ExceptT (deepBlock rsChain tip)) >>= \case + Left e -> mapM_ (\editId -> stall rs editId e) editIds + -- each edit on its own, so a row that fails stops no other edit + Right (tip, deep) -> forM_ editIds $ \editId -> + safely ("names records: edit " <> tshow editId) $ withClaim rs editId () $ withStore rs (`getRecordEdit` editId) >>= mapM_ (follow rs tip deep) + +-- | The newest block at the configured confirmation depth, as the sender counts it. +deepBlock :: ChainClient -> Block -> IO (Either ChainError Block) +deepBlock c tip@Block {blockNumber} + | deep == blockNumber = pure $ Right tip + | otherwise = blockAt c deep + where + deep = confirmedNumber c blockNumber + +-- | One step for a pending edit, from its transactions' statuses as the sender settled them. +follow :: Records -> Block -> Block -> RecordEdit -> IO () +follow rs@Records {rsChain} Block {blockTimestamp} Block {blockTimestamp = deepTime} edit@RecordEdit {reId, reChanges} = do + txs <- withStore rs (`editTransactions` reId) + case txs of + [] -> if ahead then sendUnsent else forget rs reId + _ + | mined : _ <- [ntHash | NameTransaction {ntHash, ntStatus = TSMined} <- txs] -> settle rs reId $ ESMined $ Just mined + | any ((== TSPending) . ntStatus) txs -> if deepTime > reDeadline then expire else unstall rs reId + -- a mined revert carries no data: past the deadline it is SignatureExpired, else unnamed + | block : _ <- [b | NameTransaction {ntStatus = TSReverted, ntMinedBlock = Just b} <- txs] -> + blockAt rsChain block >>= \case + Left e -> stall rs reId e + Right Block {blockTimestamp = minedTime} -> judged rs edit $ settle rs reId $ ESReverted $ if minedTime > reDeadline then signatureExpired else Nothing + -- every transaction dropped: its registrar nonce went to another one + | otherwise -> judged rs edit $ if ahead then resend else expire + where + reDeadline = earliestDeadline reChanges + ahead = reDeadline > blockTimestamp + expire = judged rs edit $ settle rs reId $ ESReverted signatureExpired + sendUnsent = + sendEdit rs edit >>= \case + Left (SEChain (ChainReverted _)) -> forget rs reId + r -> either (stall rs reId) pure r + -- the edit was sent before, so its dry run's revert is its outcome + resend = + sendEdit rs edit >>= \case + Left (SEChain (ChainReverted name)) -> settle rs reId $ ESReverted $ revertText =<< name + r -> either (stall rs reId) pure r + +-- | Settles the edit as the chain shows it, or runs the action when its nonce is still free. +judged :: Records -> RecordEdit -> IO () -> IO () +judged rs edit@RecordEdit {reId} whenFree = + judge rs edit >>= \case + Left e -> stall rs reId e + Right (Applied tx) -> settle rs reId $ ESMined tx + Right NonceTaken -> settle rs reId $ ESReverted $ revertText RNInvalidNonce + Right NonceFree -> whenFree + +-- | Its own transactions first: a status-1 receipt answers its hash. Without one, the owner's nonce and the records. +-- A nonce past the first change but not the last means another body took one of its nonces. +judge :: Records -> RecordEdit -> IO (Either ChainError Judgment) +judge rs@Records {rsChain, rsContracts} RecordEdit {reId, reLabelHash, reOwner, reChanges} = runExceptT $ do + txs <- liftIO $ withStore rs (`editTransactions` reId) + receipts <- forM txs $ \NameTransaction {ntHash} -> (ntHash,) <$> ExceptT (transactionReceipt rsChain ntHash) + case [h | (h, Just Receipt {receiptSuccess = True}) <- receipts] of + h : _ -> pure $ Applied $ Just h + [] -> do + nonce <- ExceptT $ resolverNonce rsContracts reOwner + if + | nonce <= firstNonce reChanges -> pure NonceFree + | nonce <= lastNonce reChanges -> pure NonceTaken + | otherwise -> do + holds <- and <$> mapM (holdsChange . scChange) reChanges + pure $ if holds then Applied Nothing else NonceTaken + where + holdsChange = \case + RCSetText key value -> (== value) <$> record (encodeUtf8 key) + RCClear -> (&&) <$> (unset <$> record contactRecordKey) <*> (unset <$> record channelRecordKey) + record = ExceptT . textRecord rsContracts reLabelHash + unset = (== "") + +settle :: Records -> Int64 -> EditState -> IO () +settle rs editId state = do + void $ withStore rs $ \db -> settleRecordEdit db editId state + settled rs editId + +-- | Wakes the requests waiting for the edit, which read it again. +settled :: Records -> Int64 -> IO () +settled Records {rsStalled, rsChanges} editId = atomically $ modifyTVar' rsStalled (S.delete editId) >> modifyTVar' rsChanges (+ 1) + +-- | Logged once until the edit's next successful step. +stall :: Show e => Records -> Int64 -> e -> IO () +stall Records {rsStalled, rsChanges} editId e = do + new <- atomically $ do + stalled <- readTVar rsStalled + let new = not $ S.member editId stalled + when new $ writeTVar rsStalled (S.insert editId stalled) >> modifyTVar' rsChanges (+ 1) + pure new + when new $ logWarn $ "names records: edit " <> tshow editId <> " waits for the chain: " <> tshow e + +unstall :: Records -> Int64 -> IO () +unstall Records {rsStalled} editId = atomically $ modifyTVar' rsStalled $ S.delete editId + +-- | Runs the action only when nobody else holds the edit, else answers the fallback. +withClaim :: Records -> Int64 -> a -> IO a -> IO a +withClaim Records {rsClaimed} editId fallback action = + bracket claim (\claimed -> when claimed release) $ \claimed -> if claimed then action else pure fallback + where + claim = atomically $ do + held <- readTVar rsClaimed + if S.member editId held then pure False else True <$ writeTVar rsClaimed (S.insert editId held) + release = atomically $ modifyTVar' rsClaimed $ S.delete editId + +changeDigest :: Records -> LabelHash -> SignedChange -> ByteString +changeDigest rs lh SignedChange {scChange, scNonce, scDeadline} = case scChange of + RCSetText key value -> setTextDigest (domain rs) node key value scNonce scDeadline + RCClear -> clearRecordsDigest (domain rs) node scNonce scDeadline + where + node = nodeOf rs lh + +-- | One change is sent as its own call, which keeps the revert's name; two go in one multicall. +editCalldata :: Records -> RecordEdit -> ByteString +editCalldata rs RecordEdit {reLabelHash, reChanges} = case reChanges of + change :| [] -> changeCalldata change + _ -> multicallCalldata $ map changeCalldata $ L.toList reChanges + where + node = nodeOf rs reLabelHash + changeCalldata SignedChange {scChange, scNonce, scDeadline, scSignature} = case scChange of + RCSetText key value -> setTextWithSigCalldata node key value scNonce scDeadline scSignature + RCClear -> clearRecordsWithSigCalldata node scNonce scDeadline scSignature + +firstNonce, lastNonce :: NonEmpty SignedChange -> Word64 +firstNonce = scNonce . L.head +lastNonce = scNonce . L.last + +-- | Past it the edit cannot land whole, so it is SignatureExpired. +earliestDeadline :: NonEmpty SignedChange -> Word64 +earliestDeadline = minimum . L.map scDeadline + +nodeOf :: Records -> LabelHash -> Node +nodeOf Records {rsTLD} = labelNode rsTLD + +domain :: Records -> Eip712Domain +domain rs@Records {rsChain} = resolverDomain (cChainId $ ccConfig rsChain) (resolver rs) + +resolver :: Records -> Address +resolver = cResolver . ccConfig . rsChain + +signatureExpired :: Maybe Text +signatureExpired = revertText RNSignatureExpired + +withStore :: Records -> (DB.Connection -> IO a) -> IO a +withStore Records {rsStore} = withTransaction rsStore diff --git a/apps/simplex-store-service/src/StoreService/Names/Registration.hs b/apps/simplex-store-service/src/StoreService/Names/Registration.hs index a753fd1856..8d986bf5c1 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Registration.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Registration.hs @@ -11,16 +11,23 @@ module StoreService.Names.Registration contactRecordKey, channelRecordKey, sponsorDomain, + resolverDomain, makeCommitment, commitCalldata, registerWithCreditCalldata, registerIntentDigest, verifyRegisterIntent, + setTextDigest, + clearRecordsDigest, + setTextWithSigCalldata, + clearRecordsWithSigCalldata, + multicallCalldata, ) where import Control.Monad (unless, when) import Control.Monad.Except (ExceptT (..), runExceptT, throwError) +import Crypto.Number.Serialize (os2ip) import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Maybe (catMaybes) @@ -66,6 +73,9 @@ data RegisterIntent = RegisterIntent sponsorDomain :: Word64 -> Address -> Eip712Domain sponsorDomain edChainId edVerifyingContract = Eip712Domain {edName = "SimplexSponsor", edVersion = "1", edChainId, edVerifyingContract} +resolverDomain :: Word64 -> Address -> Eip712Domain +resolverDomain edChainId edVerifyingContract = Eip712Domain {edName = "SimplexResolver", edVersion = "1", edChainId, edVerifyingContract} + makeCommitment :: Registration -> ByteString makeCommitment r = keccak256 $ abiTuple [Dynamic $ registrationTuple r] @@ -103,6 +113,44 @@ verifyRegisterIntent domain now RegisterIntent {riRegistration = r@Registration signer <- ExceptT $ recoverAddress riSignature $ registerIntentDigest domain r riNonce riDeadline unless (signer == rOwner) $ throwError "intent is not signed by the owner" +setTextDigest :: Eip712Domain -> Node -> Text -> Text -> Word64 -> Word64 -> ByteString +setTextDigest domain node key value nonce deadline = + hashTypedData + domain + "SetText(bytes32 node,string key,string value,uint256 nonce,uint256 deadline)" + [bytes32 node, VBytes $ encodeUtf8 key, VBytes $ encodeUtf8 value, VUint $ fromIntegral nonce, VUint $ fromIntegral deadline] + +clearRecordsDigest :: Eip712Domain -> Node -> Word64 -> Word64 -> ByteString +clearRecordsDigest domain node nonce deadline = + hashTypedData + domain + "ClearRecords(bytes32 node,uint256 nonce,uint256 deadline)" + [bytes32 node, VUint $ fromIntegral nonce, VUint $ fromIntegral deadline] + +setTextWithSigCalldata :: Node -> Text -> Text -> Word64 -> Word64 -> RecoverableSignature -> ByteString +setTextWithSigCalldata (Node node) key value nonce deadline sig = + selector "setTextWithSig(bytes32,string,string,uint256,uint256,bytes)" + <> abiTuple + [ Static node, + Dynamic $ abiBytes $ encodeUtf8 key, + Dynamic $ abiBytes $ encodeUtf8 value, + Static $ uint256 nonce, + Static $ uint256 deadline, + Dynamic $ abiBytes $ serializeSignature sig + ] + +clearRecordsWithSigCalldata :: Node -> Word64 -> Word64 -> RecoverableSignature -> ByteString +clearRecordsWithSigCalldata (Node node) nonce deadline sig = + selector "clearRecordsWithSig(bytes32,uint256,uint256,bytes)" + <> abiTuple [Static node, Static $ uint256 nonce, Static $ uint256 deadline, Dynamic $ abiBytes $ serializeSignature sig] + +-- | The resolver runs each call on itself in order, and reverts all of them when one fails. +multicallCalldata :: [ByteString] -> ByteString +multicallCalldata calls = selector "multicall(bytes[])" <> abiTuple [Dynamic $ abiArray $ map abiBytes calls] + +bytes32 :: Node -> Eip712Value +bytes32 (Node node) = VUint $ fromInteger $ os2ip node + registrationTuple :: Registration -> ByteString registrationTuple r@Registration {rLabel, rOwner, rDuration, rSecret, rResolver} = abiTuple diff --git a/apps/simplex-store-service/src/StoreService/Names/Revert.hs b/apps/simplex-store-service/src/StoreService/Names/Revert.hs index fc6acd6bd5..4017553265 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Revert.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Revert.hs @@ -1,12 +1,15 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} -module StoreService.Names.Revert (RevertName (..), decodeRevert) where +module StoreService.Names.Revert (RevertName (..), decodeRevert, revertText) where import Data.ByteString (ByteString) import qualified Data.ByteString as B +import qualified Data.ByteString.Char8 as BC import Data.Map.Strict (Map) import qualified Data.Map.Strict as M +import Data.Text (Text) +import Data.Text.Encoding (decodeLatin1) import StoreService.Names.Abi (selector) -- | The custom errors of SimplexController, SimplexSponsor, BaseRegistrarImplementation and SimplexResolver. @@ -107,6 +110,10 @@ revertSignature = \case RNZeroAddress -> Just "ZeroAddress()" RNWithoutData -> Nothing +-- | The error's name as the contracts declare it, Nothing for a revert without data. +revertText :: RevertName -> Maybe Text +revertText = fmap (decodeLatin1 . BC.takeWhile (/= '(')) . revertSignature + revertSelectors :: Map ByteString RevertName revertSelectors = M.fromList [(selector signature, n) | n <- [minBound .. maxBound], Just signature <- [revertSignature n]] diff --git a/apps/simplex-store-service/src/StoreService/Names/Store.hs b/apps/simplex-store-service/src/StoreService/Names/Store.hs index 7de80285fc..9709a32076 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Store.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Store.hs @@ -33,6 +33,11 @@ module StoreService.Names.Store TxStatus (..), NameTransaction (..), NewTransaction (..), + RecordChange (..), + SignedChange (..), + EditState (..), + RecordEdit (..), + NewRecordEdit (..), findLimit, orderHash, orderLive, @@ -47,6 +52,7 @@ module StoreService.Names.Store getLiveOrders, closeOverdue, updateRegistration, + retargetRegistration, findRegistrations, isLabelRegistering, activeRegistrations, @@ -82,6 +88,16 @@ module StoreService.Names.Store recordTransactionOutcome, pendingTransactions, registrationTransactions, + editTransactions, + insertRecordEdit, + getRecordEdit, + getRecordEditByBody, + getRecordEditByNonce, + settleRecordEdit, + pendingEditIds, + deleteUnsentEdit, + relayedEditTimes, + countedEditTimes, ) where @@ -92,6 +108,8 @@ import Data.ByteArray.Encoding (Base (Base16), convertToBase) import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Int (Int64) +import Data.List.NonEmpty (NonEmpty (..)) +import qualified Data.List.NonEmpty as L import Data.Maybe (isJust, listToMaybe) import Data.String (fromString) import Data.Text (Text) @@ -387,7 +405,7 @@ data NewPrepaidCode = NewPrepaidCode data CodeHashTaken = CodeHashTaken deriving (Eq, Show) -data TxFor = TxCommit RegistrationId | TxRegister RegistrationId +data TxFor = TxCommit RegistrationId | TxRegister RegistrationId | TxEdit Int64 deriving (Eq, Show) -- | A dropped transaction has no receipt and its registrar nonce is used. @@ -424,6 +442,41 @@ data NewTransaction = NewTransaction ntxNonce :: Word64 } +-- | A text record set to a value, "" removing it, or every record cleared. +data RecordChange = RCSetText Text Text | RCClear + deriving (Eq, Show) + +data EditState = ESPending | ESMined (Maybe TxHash) | ESReverted (Maybe Text) + deriving (Eq, Show) + +-- | A change the owner signed for the resolver's nonce it names. +data SignedChange = SignedChange + { scChange :: RecordChange, + scNonce :: Word64, + scDeadline :: Word64, + scSignature :: RecoverableSignature + } + deriving (Eq, Show) + +-- | One request's changes, relayed in one transaction, their nonces consecutive in this order. +data RecordEdit = RecordEdit + { reId :: Int64, + reLabelHash :: LabelHash, + reOwner :: Address, + reChanges :: NonEmpty SignedChange, + reBodyHash :: ByteString, + reState :: EditState, + reCreatedAt :: UTCTime + } + deriving (Eq, Show) + +data NewRecordEdit = NewRecordEdit + { nreLabelHash :: LabelHash, + nreOwner :: Address, + nreChanges :: NonEmpty SignedChange, + nreBodyHash :: ByteString + } + findLimit :: Int findLimit = 50 @@ -725,6 +778,34 @@ updateRegistration db r now = do [(version, BI storedLive)] | version == nrVersion -> pure $ if storedLive then LiveOrderExists else NotLive _ -> pure VersionChanged +-- | Another name, or other links, with the terms agreed for it: queued again with the reveal, transactions, reveal times and link refusal cleared. +-- The order hash stays the original order's, so a resend of that order still finds it. +retargetRegistration :: DB.Connection -> NameRegistration -> OrderFields -> Int -> UTCTime -> IO VersionWrite +retargetRegistration db r OrderFields {ofLabelHash, ofLength, ofDuration, ofCommitment, ofSealed, ofAgreedTerms} price = + updateRegistration + db + r + { nrLabelHash = ofLabelHash, + nrLength = ofLength, + nrDuration = ofDuration, + nrCommitment = ofCommitment, + nrSealed = ofSealed, + nrAgreedTerms = ofAgreedTerms, + nrPrice = price, + nrPhase = PQueued, + nrMoves = nrMoves r + 1, + nrReveal = Nothing, + nrCommitTx = Nothing, + nrCommitBlock = Nothing, + nrRegisterTx = Nothing, + nrRegisterBlock = Nothing, + nrRevealAfter = Nothing, + nrRevealBefore = Nothing, + nrLinksRefusedAt = Nothing, + nrFailedAt = Nothing, + nrError = Nothing + } + -- | Newest first: findLimit of them, then every older one holding a balance above 0, since a balance never expires. -- Each with the order holding its balance, as getBalance reads the hold at the given time. findRegistrations :: DB.Connection -> Address -> UTCTime -> IO [(NameRegistration, Maybe RegistrationId)] @@ -1269,23 +1350,24 @@ revokePrepaidCode db codeHash now = do | isJust pcRevokedAt = AlreadyRevoked pcId | otherwise = AlreadyRedeemed -txKind :: TxFor -> (Text, Text) +txKind :: TxFor -> (Text, Maybe Text, Maybe Int64) txKind = \case - TxCommit (RegistrationId rid) -> ("commit", rid) - TxRegister (RegistrationId rid) -> ("register", rid) + TxCommit (RegistrationId rid) -> ("commit", Just rid, Nothing) + TxRegister (RegistrationId rid) -> ("register", Just rid, Nothing) + TxEdit editId -> ("edit", Nothing, Just editId) -- | Saved once signed and before it is sent, so a restart can rebroadcast it. insertTransaction :: DB.Connection -> TxFor -> NewTransaction -> UTCTime -> IO Int64 insertTransaction db for NewTransaction {ntxRaw, ntxHash, ntxNonce} now = do - let (kind, rid) = txKind for + let (kind, rid, editId) = txKind for DB.execute db [sql| INSERT INTO sx_badge_service_name_transactions - (registration_id, kind, raw, tx_hash, registrar_nonce, sent_at, status) - VALUES (?,?,?,?,?,?,?) + (registration_id, name_record_edit_id, kind, raw, tx_hash, registrar_nonce, sent_at, status) + VALUES (?,?,?,?,?,?,?,?) |] - (rid, kind, Binary ntxRaw, txHashField ntxHash, int64 ntxNonce, truncateToSecond now, TSPending) + (rid, editId, kind, Binary ntxRaw, txHashField ntxHash, int64 ntxNonce, truncateToSecond now, TSPending) insertedRowId db -- | Moves a transaction on from the status the caller read, so an outcome is never overwritten unseen; False when it is not in that status. @@ -1300,17 +1382,18 @@ recordTransactionOutcome db hash from status block = transactionColumns :: Query transactionColumns = [sql| - SELECT name_transaction_id, kind, registration_id, raw, tx_hash, registrar_nonce, sent_at, mined_block, status + SELECT name_transaction_id, kind, registration_id, name_record_edit_id, raw, tx_hash, registrar_nonce, sent_at, mined_block, status FROM sx_badge_service_name_transactions |] -type TransactionRow = (Int64, Text, Maybe Text, Binary ByteString, Binary ByteString, Int64, UTCTime, Maybe Int64, TxStatus) +type TransactionRow = (Int64, Text, Maybe Text, Maybe Int64, Binary ByteString, Binary ByteString, Int64, UTCTime, Maybe Int64, TxStatus) toTransaction :: TransactionRow -> Either Text NameTransaction -toTransaction (ntId, kind, rid_, Binary ntRaw, hash, nonce, ntSentAt, block, ntStatus) = do - ntFor <- case (kind, rid_) of - ("commit", Just rid) -> Right $ TxCommit $ RegistrationId rid - ("register", Just rid) -> Right $ TxRegister $ RegistrationId rid +toTransaction (ntId, kind, rid_, editId_, Binary ntRaw, hash, nonce, ntSentAt, block, ntStatus) = do + ntFor <- case (kind, rid_, editId_) of + ("commit", Just rid, Nothing) -> Right $ TxCommit $ RegistrationId rid + ("register", Just rid, Nothing) -> Right $ TxRegister $ RegistrationId rid + ("edit", Nothing, Just editId) -> Right $ TxEdit editId _ -> Left $ "name_transactions.kind: " <> kind pure NameTransaction {ntId, ntFor, ntRaw, ntHash = txHash hash, ntNonce = word64 nonce, ntSentAt, ntMinedBlock = word64 <$> block, ntStatus} @@ -1325,6 +1408,152 @@ registrationTransactions db (RegistrationId rid) = DB.query db (transactionColumns <> " WHERE registration_id = ? ORDER BY name_transaction_id") (Only rid) >>= decodeAll toTransaction +editTransactions :: DB.Connection -> Int64 -> IO [NameTransaction] +editTransactions db editId = + DB.query db (transactionColumns <> " WHERE name_record_edit_id = ? ORDER BY name_transaction_id") (Only editId) + >>= decodeAll toTransaction + +pendingEdit :: Text +pendingEdit = "pending" + +minedEdit :: Text +minedEdit = "mined" + +revertedEdit :: Text +revertedEdit = "reverted" + +-- | Nothing when a pending or mined edit already starts at the owner's first nonce. +insertRecordEdit :: DB.Connection -> NewRecordEdit -> UTCTime -> IO (Maybe Int64) +insertRecordEdit db NewRecordEdit {nreLabelHash = LabelHash lh, nreOwner, nreChanges, nreBodyHash} now = do + inserted <- + executeChanging + db + [sql| + INSERT INTO sx_badge_service_name_record_edits (label_hash, owner, nonce, body_hash, state, created_at) + VALUES (?,?,?,?,?,?) + ON CONFLICT DO NOTHING + |] + (Binary lh, Binary $ unAddress nreOwner, int64 $ scNonce $ L.head nreChanges, Binary nreBodyHash, pendingEdit, truncateToSecond now) + if inserted > 0 + then do + editId <- insertedRowId db + mapM_ (insertChange editId) nreChanges + pure $ Just editId + else pure Nothing + where + insertChange editId SignedChange {scChange, scNonce, scDeadline, scSignature} = + DB.execute + db + "INSERT INTO sx_badge_service_name_record_changes (name_record_edit_id, nonce, record_key, record_value, deadline, signature) VALUES (?,?,?,?,?,?)" + (editId, int64 scNonce, key, value, int64 scDeadline, Binary $ serializeSignature scSignature) + where + (key, value) = case scChange of + RCSetText k v -> (Just k, Just v) + RCClear -> (Nothing, Nothing) + +recordEditColumns :: Query +recordEditColumns = + [sql| + SELECT name_record_edit_id, label_hash, owner, body_hash, state, revert, mined_tx, created_at + FROM sx_badge_service_name_record_edits + |] + +type RecordEditRow = (Int64, Binary ByteString, Binary ByteString, Binary ByteString, Text, Maybe Text, Maybe (Binary ByteString), UTCTime) + +type ChangeRow = (Int64, Maybe Text, Maybe Text, Int64, Binary ByteString) + +-- | An edit's row with its changes, in nonce order. +readRecordEdit :: DB.Connection -> RecordEditRow -> IO RecordEdit +readRecordEdit db row@(reId, _, _, _, _, _, _, _) = do + changes <- DB.query db "SELECT nonce, record_key, record_value, deadline, signature FROM sx_badge_service_name_record_changes WHERE name_record_edit_id = ? ORDER BY nonce" (Only reId) + either (E.throwIO . StoreDecodeError) pure $ toRecordEdit row changes + +toRecordEdit :: RecordEditRow -> [ChangeRow] -> Either Text RecordEdit +toRecordEdit (reId, Binary lh, Binary owner, Binary reBodyHash, state, revert, minedTx, reCreatedAt) changeRows = do + reOwner <- decodeAddress owner + reChanges <- note ("name_record_edits changes: " <> tshow reId) . L.nonEmpty =<< mapM toChange changeRows + reState <- decodeState + pure RecordEdit {reId, reLabelHash = LabelHash lh, reOwner, reChanges, reBodyHash, reState, reCreatedAt} + where + toChange (nonce, key_, value_, deadline, Binary sig) = do + scSignature <- decodeSignature sig + scChange <- case (key_, value_) of + (Just key, Just value) -> Right $ RCSetText key value + (Nothing, Nothing) -> Right RCClear + _ -> Left $ "name_record_changes record: " <> tshow reId + pure SignedChange {scChange, scNonce = word64 nonce, scDeadline = word64 deadline, scSignature} + decodeState + | state == pendingEdit = Right ESPending + | state == minedEdit = Right $ ESMined $ txHash <$> minedTx + | state == revertedEdit = Right $ ESReverted revert + | otherwise = Left $ "name_record_edits.state: " <> state + +firstRecordEdit :: DB.Connection -> IO [RecordEditRow] -> IO (Maybe RecordEdit) +firstRecordEdit db rows = rows >>= mapM (readRecordEdit db) . listToMaybe + +getRecordEdit :: DB.Connection -> Int64 -> IO (Maybe RecordEdit) +getRecordEdit db editId = + firstRecordEdit db $ DB.query db (recordEditColumns <> " WHERE name_record_edit_id = ?") (Only editId) + +-- | The newest edit sent with this signed body. +getRecordEditByBody :: DB.Connection -> ByteString -> IO (Maybe RecordEdit) +getRecordEditByBody db bodyHash = + firstRecordEdit db $ + DB.query db (recordEditColumns <> " WHERE body_hash = ? ORDER BY name_record_edit_id DESC LIMIT 1") (Only $ Binary bodyHash) + +-- | The pending or mined edit holding the owner's nonce among its changes. +getRecordEditByNonce :: DB.Connection -> Address -> Word64 -> IO (Maybe RecordEdit) +getRecordEditByNonce db owner nonce = + firstRecordEdit db $ + DB.query + db + ( recordEditColumns + <> " WHERE owner = ? AND state IN (?,?) AND name_record_edit_id IN (SELECT name_record_edit_id FROM sx_badge_service_name_record_changes WHERE nonce = ?)" + ) + (Binary $ unAddress owner, pendingEdit, minedEdit, int64 nonce) + +-- | Settles a pending edit; False when it was settled already. +settleRecordEdit :: DB.Connection -> Int64 -> EditState -> IO Bool +settleRecordEdit db editId state = + (> 0) + <$> executeChanging + db + "UPDATE sx_badge_service_name_record_edits SET state = ?, revert = ?, mined_tx = ? WHERE name_record_edit_id = ? AND state = ?" + (stateText, revert, txHashField <$> minedTx, editId, pendingEdit) + where + (stateText, revert, minedTx) = case state of + ESPending -> (pendingEdit, Nothing, Nothing) + ESMined tx -> (minedEdit, Nothing, tx) + ESReverted name -> (revertedEdit, name, Nothing) + +pendingEditIds :: DB.Connection -> IO [Int64] +pendingEditIds db = map fromOnly <$> DB.query db "SELECT name_record_edit_id FROM sx_badge_service_name_record_edits WHERE state = ? ORDER BY name_record_edit_id" (Only pendingEdit) + +deleteUnsentEdit :: DB.Connection -> Int64 -> IO () +deleteUnsentEdit db editId = + DB.execute + db + "DELETE FROM sx_badge_service_name_record_edits WHERE name_record_edit_id = ? AND state = ? AND NOT EXISTS (SELECT 1 FROM sx_badge_service_name_transactions WHERE name_record_edit_id = ?)" + (editId, pendingEdit, editId) + +-- | When each edit relayed for the name since the given time was sent, oldest first. +relayedEditTimes :: DB.Connection -> LabelHash -> UTCTime -> IO [UTCTime] +relayedEditTimes db (LabelHash lh) since = + map fromOnly + <$> DB.query + db + "SELECT created_at FROM sx_badge_service_name_record_edits WHERE label_hash = ? AND created_at > ? ORDER BY created_at" + (Binary lh, truncateToSecond since) + +-- | When each pending or mined edit of the name was made since the given time, oldest first: the edits its yearly limit counts. +countedEditTimes :: DB.Connection -> LabelHash -> UTCTime -> IO [UTCTime] +countedEditTimes db (LabelHash lh) since = + map fromOnly + <$> DB.query + db + "SELECT created_at FROM sx_badge_service_name_record_edits WHERE label_hash = ? AND created_at > ? AND state IN (?,?) ORDER BY created_at" + (Binary lh, truncateToSecond since, pendingEdit, minedEdit) + int64 :: Word64 -> Int64 int64 = fromIntegral diff --git a/apps/simplex-store-service/src/StoreService/Service.hs b/apps/simplex-store-service/src/StoreService/Service.hs index 53fb5e6e5c..6c63fb2edc 100644 --- a/apps/simplex-store-service/src/StoreService/Service.hs +++ b/apps/simplex-store-service/src/StoreService/Service.hs @@ -45,6 +45,7 @@ import StoreService.Names.Codes (issueNameCode, revokeNameCode) 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.Store (Balance (..), BalanceHold (..), BalanceLink (..), LateCredit (..), RegistrationPayment (..), balanceByReference, balanceChain, methodText) import StoreService.Names.Turns (Turns, newTurns) @@ -266,9 +267,9 @@ startNames cc@ChatController {chatStore} turns waiters providers nc@NamesConfig -- | The workers resume every registration in their phases, and the code sweep runs at the start and then daily. namesLanes :: NamesEnv -> IO [IO ()] -namesLanes ne@NamesEnv {neChain, neSender} = do +namesLanes ne@NamesEnv {neChain, neSender, neRecords} = do worker <- newWorker (ordersEnv ne) neChain neSender - pure [runSender neSender, runWorkers worker, runCodeSweep worker] + pure [runSender neSender, runRecordEdits neRecords, runWorkers worker, runCodeSweep worker] seedServiceCatalog :: DBStore -> IO () seedServiceCatalog st = do diff --git a/apps/simplex-store-service/src/StoreService/Web/Http.hs b/apps/simplex-store-service/src/StoreService/Web/Http.hs index 649334fe57..a60018f771 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Http.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Http.hs @@ -19,6 +19,8 @@ module StoreService.Web.Http searchLimit, previewLimit, codeLimit, + findLimit, + recordsLimit, holdMicros, jsonResponse, jsonError, @@ -92,12 +94,14 @@ type Respond = Response -> IO ResponseReceived holdMicros :: Int holdMicros = 30 * 1000000 -readLimit, createLimit, searchLimit, previewLimit, codeLimit :: Limit +readLimit, createLimit, searchLimit, previewLimit, codeLimit, findLimit, recordsLimit :: Limit readLimit = Limit {lmName = "read", lmPerMinute = 60} createLimit = Limit {lmName = "create", lmPerMinute = 5} searchLimit = Limit {lmName = "search", lmPerMinute = 30} previewLimit = Limit {lmName = "preview", lmPerMinute = 20} codeLimit = Limit {lmName = "code", lmPerMinute = 10} +findLimit = Limit {lmName = "find", lmPerMinute = 5} +recordsLimit = Limit {lmName = "records", lmPerMinute = 10} maxBodyBytes :: Int maxBodyBytes = 8192 diff --git a/apps/simplex-store-service/src/StoreService/Web/Names.hs b/apps/simplex-store-service/src/StoreService/Web/Names.hs index 684cf6dd07..5183602fe9 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Names.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Names.hs @@ -5,14 +5,17 @@ module StoreService.Web.Names (Route (..), namesRoutes, namesApp) where import Data.Text (Text) +import Data.Time.Clock (getCurrentTime) import Network.HTTP.Types (Method) import Network.Wai (Request, ResponseReceived, pathInfo) import StoreService.Config (ServiceConfig) import StoreService.Names.Env (NamesEnv (..)) import StoreService.Names.Orders (ordersEnv) -import StoreService.Web.Http (HttpEnv, Limit, Respond, codeLimit, createLimit, holdMicros, limited, only, previewLimit, readLimit, searchLimit) +import StoreService.Web.Http (HttpEnv, Limit, Respond, codeLimit, createLimit, findLimit, holdMicros, limited, only, previewLimit, readLimit, recordsLimit, searchLimit) import StoreService.Web.Names.Codes (codeCheckHandler) import StoreService.Web.Names.Orders (codeOrderHandler, spendHandler) +import StoreService.Web.Names.Owner (findHandler, retargetHandler) +import StoreService.Web.Names.Records (recordsHandler) import StoreService.Web.Names.Registration (registrationHandler, retryHandler, revealHandler) import StoreService.Web.Names.Search (previewHandler, searchHandler) @@ -23,13 +26,17 @@ data Route = Route } namesRoutes :: ServiceConfig -> NamesEnv -> [Text] -> Maybe Route -namesRoutes cfg env@NamesEnv {neConfig, neStore, neContracts, neProfiles, neWaiters, neProviders} = \case +namesRoutes cfg env@NamesEnv {neConfig, neStore, neContracts, neProfiles, neRecords, neWaiters, neProviders} = \case + ["api", "name", "records"] -> Just Route {rMethod = "POST", rLimit = recordsLimit, rHandler = recordsHandler neRecords} ["api", "name", "register"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeOrderHandler (ordersEnv env) neProviders neWaiters} ["api", "code", "check"] -> Just Route {rMethod = "POST", rLimit = codeLimit, rHandler = codeCheckHandler neStore} ["api", "link", "preview"] -> Just Route {rMethod = "POST", rLimit = previewLimit, rHandler = previewHandler neProfiles} -- below every fixed /api/name/ route, which it would otherwise match - ["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler neConfig neStore neContracts neProfiles hash} + ["api", "name", hash] -> Just Route {rMethod = "GET", rLimit = searchLimit, rHandler = searchHandler neConfig neStore neContracts neProfiles getCurrentTime hash} + -- above /api/registration/:id, which it would otherwise match + ["api", "registration", "find"] -> Just Route {rMethod = "POST", rLimit = findLimit, rHandler = findHandler (ordersEnv env)} ["api", "registration", rid] -> Just Route {rMethod = "GET", rLimit = readLimit, rHandler = registrationHandler (ordersEnv env) holdMicros rid} + ["api", "registration", rid, "name"] -> Just Route {rMethod = "POST", rLimit = createLimit, rHandler = retargetHandler (ordersEnv env) rid} ["api", "registration", rid, "reveal"] -> Just Route {rMethod = "POST", rLimit = readLimit, rHandler = revealHandler (ordersEnv env) rid} ["api", "registration", rid, "retry"] -> Just Route {rMethod = "POST", rLimit = createLimit, rHandler = retryHandler (ordersEnv env) rid} ["api", "registration", rid, "spend"] -> Just Route {rMethod = "POST", rLimit = createLimit, rHandler = spendHandler (ordersEnv env) cfg neProviders neWaiters rid} diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Records.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Records.hs new file mode 100644 index 0000000000..6010553fd6 --- /dev/null +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Records.hs @@ -0,0 +1,94 @@ +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TupleSections #-} + +module StoreService.Web.Names.Records (EditInvalid (..), recordsHandler, editRequest, editsLeftJSON) where + +import Control.Monad (forM_, unless) +import Data.Aeson ((.:), (.:?), (.=)) +import qualified Data.Aeson as J +import qualified Data.Aeson.Types as JT +import Data.List (nub) +import qualified Data.List.NonEmpty as L +import Data.Maybe (isJust) +import Data.Text (Text) +import Data.Text.Encoding (decodeLatin1) +import Data.Time.Clock (UTCTime) +import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds) +import Data.Word (Word64) +import Network.HTTP.Types (status200, status409, status429, status504) +import Network.Wai (Request, Response, ResponseReceived) +import Simplex.Messaging.SimplexName (LabelHash (..)) +import StoreService.Names.Abi (bytes32FromHex, hex) +import StoreService.Names.Chain (TxHash (..)) +import StoreService.Names.Links (LinkKind (..), linkKindOf) +import StoreService.Names.Records (EditAnswer (..), EditRequest (..), EditsLeft (..), Records, relayEdit) +import StoreService.Names.Registration (channelRecordKey, contactRecordKey) +import StoreService.Names.Signatures (parseSignature) +import StoreService.Names.Store (RecordChange (..), SignedChange (..)) +import StoreService.Web.Http (Respond, badRequest, invalidLink, jsonError, jsonResponse, nameBodyBytes, providerUnavailable, rateLimited, readBoundedBody) + +data EditInvalid = EIBadRequest | EIInvalidLink + deriving (Eq, Show) + +recordsHandler :: Records -> Request -> Respond -> IO ResponseReceived +recordsHandler rs req respond = + readBoundedBody nameBodyBytes req >>= \case + Just body | Just o <- J.decode body -> case editRequest o of + Right edit -> relayEdit rs edit >>= respond . editResponse + Left EIInvalidLink -> respond invalidLink + Left EIBadRequest -> respond badRequest + _ -> respond badRequest + +-- | An edit as sent, before its key, link and signature are checked. +data SentEdit = SentEdit {seChange :: Maybe (Text, Text), seNonce :: Word64, seDeadline :: Word64, seSignature :: Text} + +-- | One link of each kind, so a Save of both links is the most one request carries. +maxEdits :: Int +maxEdits = 2 + +-- | Every edit's format and key, then every link, then every signature's format and low s, as the page's checks run. +editRequest :: J.Object -> Either EditInvalid EditRequest +editRequest o = do + (labelHash, edits) <- maybe (Left EIBadRequest) Right $ JT.parseMaybe fields o + erLabelHash <- maybe (Left EIBadRequest) (Right . LabelHash) $ bytes32FromHex labelHash + sent <- maybe (Left EIBadRequest) Right $ L.nonEmpty edits + unless (length edits <= maxEdits && keysOnce edits && clearAlone edits && consecutive edits) $ Left EIBadRequest + links <- mapM (\SentEdit {seChange} -> mapM linkOf seChange) sent + forM_ links $ mapM_ $ \(kind, value) -> unless (value == "" || linkKindOf value == Just kind) $ Left EIInvalidLink + erChanges <- mapM signedChange sent + pure EditRequest {erLabelHash, erChanges} + where + fields v = (,) <$> v .: "labelHash" <*> (v .: "edits" >>= mapM (J.withObject "edit" sentEdit)) + sentEdit v = do + clear <- v .:? "clear" + seChange <- if clear == Just True then pure Nothing else Just <$> ((,) <$> v .: "key" <*> v .: "value") + SentEdit seChange <$> v .: "nonce" <*> v .: "deadline" <*> v .: "signature" + keysOnce edits = let keys = [k | SentEdit {seChange = Just (k, _)} <- edits] in length keys == length (nub keys) + clearAlone edits = length edits == 1 || all (isJust . seChange) edits + consecutive edits = and $ zipWith (\a b -> seNonce b == seNonce a + 1) edits (drop 1 edits) + linkOf (key, value) = maybe (Left EIBadRequest) (Right . (,value)) $ lookup key recordKinds + signedChange SentEdit {seChange, seNonce, seDeadline, seSignature} = do + scSignature <- either (const $ Left EIBadRequest) Right $ parseSignature seSignature + pure SignedChange {scChange = maybe RCClear (uncurry RCSetText) seChange, scNonce = seNonce, scDeadline = seDeadline, scSignature} + +recordKinds :: [(Text, LinkKind)] +recordKinds = [(decodeLatin1 contactRecordKey, LKContact), (decodeLatin1 channelRecordKey, LKChannel)] + +editResponse :: EditAnswer -> Response +editResponse = \case + EAApplied tx -> jsonResponse status200 $ J.object ["tx" .= ((\(TxHash h) -> hex h) <$> tx)] + EAReverted revert -> jsonResponse status409 $ J.object ["error" .= ("record_reverted" :: Text), "revert" .= revert] + EAPending -> jsonError status504 "edit_pending" + EARefused -> badRequest + EAUnavailable -> providerUnavailable + EACapped seconds -> rateLimited seconds + EALimited nextAt -> jsonResponse status429 $ J.object ["error" .= ("edit_limit" :: Text), "nextAt" .= unixSeconds nextAt] + +-- | The search answer's recordEdits. +editsLeftJSON :: EditsLeft -> J.Value +editsLeftJSON EditsLeft {elLeft, elNextAt} = J.object ["left" .= elLeft, "nextAt" .= (unixSeconds <$> elNextAt)] + +unixSeconds :: UTCTime -> Integer +unixSeconds = floor . utcTimeToPOSIXSeconds diff --git a/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs b/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs index 88216bb695..8e7c4056e0 100644 --- a/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs +++ b/apps/simplex-store-service/src/StoreService/Web/Names/Search.hs @@ -15,6 +15,7 @@ import qualified Data.Aeson.Types as JT import qualified Data.ByteString.Char8 as B import Data.Char (isDigit) import Data.Text (Text) +import Data.Time.Clock (UTCTime) import qualified Data.Text as T import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds) import Network.HTTP.Types (status200) @@ -30,9 +31,11 @@ import StoreService.Names.Community (communityJSON, termsJSON) import StoreService.Names.Contracts import StoreService.Names.Links (LinkKind (..), linkKindOf) import StoreService.Names.Profiles (Entity, Profiles, linkEntity, linkPreview) +import StoreService.Names.Records (nameEditsLeft) import StoreService.Names.Store (isLabelRegistering) import StoreService.Names.View (addressText, tldText) import StoreService.Web.Http (Respond, badRequest, invalidLink, invalidName, jsonResponse, maxBodyBytes, providerUnavailable, readBoundedBody) +import StoreService.Web.Names.Records (editsLeftJSON) import System.Timeout (timeout) import Text.Read (readMaybe) @@ -44,15 +47,15 @@ data Found | FRegistered RegisteredName (Maybe NameRecords) -- | The chain reads of one search share one deadline, so a node that answers slowly fails the search once. -searchHandler :: NamesConfig -> DBStore -> Contracts -> Profiles -> Text -> Request -> Respond -> IO ResponseReceived -searchHandler cfg@NamesConfig {nYears} st ct ps hashText req respond = case LabelHash <$> bytes32FromHex hashText of +searchHandler :: NamesConfig -> DBStore -> Contracts -> Profiles -> IO UTCTime -> Text -> Request -> Respond -> IO ResponseReceived +searchHandler cfg@NamesConfig {nYears} st ct ps clock hashText req respond = case LabelHash <$> bytes32FromHex hashText of Nothing -> respond badRequest Just lh -> case queryLength req of Nothing -> respond invalidName Just len -> do registering <- withTransaction st (`isLabelRegistering` lh) timeout (ctDeadline ct) (runExceptT $ found lh len registering) >>= \case - Just (Right f) -> respond =<< searchAnswer cfg ps lh f + Just (Right f) -> respond =<< searchAnswer cfg st ps clock lh f Just (Left e) -> unavailable $ tshow e Nothing -> unavailable "timed out" where @@ -77,8 +80,8 @@ queryLength req = case join $ lookup "length" $ queryString req of | n >= toInteger minNameLength && n <= toInteger maxNameLength = Just $ fromInteger n | otherwise = Nothing -searchAnswer :: NamesConfig -> Profiles -> LabelHash -> Found -> IO Response -searchAnswer cfg@NamesConfig {nTLD, nYears, nTermStart, nChain = ChainConfig {cChainId, cResolver, cSponsor}} ps lh = \case +searchAnswer :: NamesConfig -> DBStore -> Profiles -> IO UTCTime -> LabelHash -> Found -> IO Response +searchAnswer cfg@NamesConfig {nTLD, nYears, nTermStart, nRecordEditsPerYear, nChain = ChainConfig {cChainId, cResolver, cSponsor}} st ps clock lh = \case FAvailable price -> ok $ ["status" .= ("available" :: Text), "price" .= price, "currency" .= ("usd" :: Text), "years" .= nYears, "termStart" .= termStart] @@ -89,9 +92,11 @@ searchAnswer cfg@NamesConfig {nTLD, nYears, nTermStart, nChain = ChainConfig {cC FReserved -> ok ["status" .= ("reserved" :: Text)] FRegistered RegisteredName {rnOwner, rnResolver, rnExpiresAt, rnGracePeriod} records_ -> do entities <- maybe (pure []) (recordEntities ps) records_ + -- only a name on the default resolver is edited here + edits <- mapM (const editsLeft) records_ ok $ ["status" .= ("registered" :: Text), "entities" .= entities, "records" .= (recordsJSON <$> records_)] - <> ["owner" .= addressText rnOwner, "resolver" .= addressText rnResolver, "recordsNonce" .= (rdNonce <$> records_)] + <> ["owner" .= addressText rnOwner, "resolver" .= addressText rnResolver, "recordsNonce" .= (rdNonce <$> records_), "recordEdits" .= (editsLeftJSON <$> edits)] <> ["expiresAt" .= rnExpiresAt, "gracePeriod" .= rnGracePeriod] <> chain where @@ -100,6 +105,7 @@ searchAnswer cfg@NamesConfig {nTLD, nYears, nTermStart, nChain = ChainConfig {cC chain = ["chainId" .= cChainId, "tld" .= tldText nTLD] termStart = floor (utcTimeToPOSIXSeconds nTermStart) :: Integer recordsJSON NameRecords {rdContact, rdChannel} = J.object ["contact" .= rdContact, "channel" .= rdChannel] + editsLeft = clock >>= \now -> withTransaction st $ \db -> nameEditsLeft db nRecordEditsPerYear lh now -- | Contact entities first. A record may hold several links joined as the app joins them; a value that is not -- a short link of its record's kind makes no entity, and neither does a link past the first few of its kind. diff --git a/apps/simplex-store-service/store_service.ini.example b/apps/simplex-store-service/store_service.ini.example index 2d9fa6ee08..32b7e7899d 100644 --- a/apps/simplex-store-service/store_service.ini.example +++ b/apps/simplex-store-service/store_service.ini.example @@ -114,8 +114,13 @@ idle_seconds = 60 ;term_slack_seconds = 3600 ; a commitment with less than this left before its reveal deadline is not reused ;reveal_margin_seconds = 900 +; how many times one order may move to another name +;max_order_moves = 3 ; on: only prepaid codes register names, and every paid name order answers 403 codes_only ;codes_only = off +; the owner's link edits relayed for one name in any 365 days, each a transaction whose gas the +; service pays; 1 to 100. A request refused, never sent or reverted on chain does not count +;record_edits_per_year = 3 ; ; community names, with [names] only: one line per name, `