Skip to content

Commit

Permalink
Remove old submissions code, putLocalTxSubmission_
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Feb 8, 2023
1 parent 86d2742 commit 0d717a1
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 148 deletions.
19 changes: 0 additions & 19 deletions lib/wallet/src/Cardano/Wallet/DB.hs
Expand Up @@ -318,15 +318,6 @@ data DBLayer m s k = forall stm. (MonadIO stm, MonadFail stm) => DBLayer
--
-- If the wallet doesn't exist, this operation returns an error.

, putLocalTxSubmission
:: WalletId
-> Hash "Tx"
-> SealedTx
-> SlotNo
-> ExceptT ErrPutLocalTxSubmission stm ()
-- ^ Add or update a transaction in the local submission pool with the
-- most recent submission slot.

, addTxSubmission
:: WalletId
-> (Tx, TxMeta, SealedTx)
Expand Down Expand Up @@ -535,7 +526,6 @@ mkDBLayerFromParts ti DBLayerCollection{..} = DBLayer
(hoistTimeInterpreter liftIO ti)
(mkDecorator_ dbTxHistory) tip
Nothing -> pure Nothing
, putLocalTxSubmission = putLocalTxSubmission_ dbPendingTxs
, addTxSubmission = \wid a b -> wrapNoSuchWallet wid $
addTxSubmission_ dbPendingTxs wid a b
, readLocalTxSubmissionPending = readLocalTxSubmissionPending_ dbPendingTxs
Expand Down Expand Up @@ -757,15 +747,6 @@ data DBPendingTxs stm = DBPendingTxs
-> stm ()
-- ^ Add overwrite an empty submisison pool to the given wallet.

, putLocalTxSubmission_
:: WalletId
-> Hash "Tx"
-> SealedTx
-> SlotNo
-> ExceptT ErrPutLocalTxSubmission stm ()
-- ^ Add or update a transaction in the local submission pool with the
-- most recent submission slot.

, addTxSubmission_
:: WalletId
-> (Tx, TxMeta, SealedTx)
Expand Down
5 changes: 0 additions & 5 deletions lib/wallet/src/Cardano/Wallet/DB/Pure/Layer.hs
Expand Up @@ -24,7 +24,6 @@ import Cardano.Address.Derivation
import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchTransaction (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
)
Expand Down Expand Up @@ -193,10 +192,6 @@ newDBLayer timeInterpreter = do
Pending Tx
-----------------------------------------------------------------------}

, putLocalTxSubmission = \pk txid tx sl -> ExceptT $
alterDB (fmap ErrPutLocalTxSubmissionNoSuchWallet . errNoSuchWallet) db $
mPutLocalTxSubmission pk txid tx sl

, addTxSubmission = error "addTxSubmission not implemented in old design"

, readLocalTxSubmissionPending =
Expand Down
20 changes: 1 addition & 19 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Submissions/Layer.hs
Expand Up @@ -16,11 +16,7 @@ import Prelude hiding
( (.) )

import Cardano.Wallet.DB
( DBPendingTxs (..)
, ErrNoSuchTransaction (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
)
( DBPendingTxs (..), ErrNoSuchTransaction (..), ErrRemoveTx (..) )
import Cardano.Wallet.DB.Sqlite.Types
( TxId (..) )
import Cardano.Wallet.DB.Store.Submissions.Operations
Expand Down Expand Up @@ -85,20 +81,6 @@ mkDbPendingTxs dbvar = DBPendingTxs
updateDBVar dbvar
$ Insert wid $ mkEmpty 0

, putLocalTxSubmission_ = \wid txid tx sl -> do
let errNoSuchWallet = ErrPutLocalTxSubmissionNoSuchWallet $
ErrNoSuchWallet wid
ExceptT $ modifyDBMaybe dbvar $ \ws -> do
case Map.lookup wid ws of
Nothing -> (Nothing, Left errNoSuchWallet)
Just _ ->
let
delta = Just
$ Adjust wid
$ AddSubmission sl (TxId txid, tx)
$ error "pls pass meta to putLocalTxSubmission!"
in (delta, Right ())

, addTxSubmission_ = \wid (_ , meta, sealed) resubmitted -> do
let (expiry, txId) = extractPendingData sealed
updateDBVar dbvar
Expand Down
106 changes: 1 addition & 105 deletions lib/wallet/test/unit/Cardano/WalletSpec.hs
Expand Up @@ -38,11 +38,9 @@ import Cardano.Wallet
, ErrSubmitTx (..)
, ErrUpdatePassphrase (..)
, ErrWithRootKey (..)
, LocalTxSubmissionConfig (..)
, SelectionWithoutChange
, WalletLayer (..)
, migrationPlanToSelectionWithdrawals
, runLocalTxSubmissionPool
, throttle
)
import Cardano.Wallet.DB
Expand Down Expand Up @@ -197,14 +195,10 @@ import Data.Ord
( Down (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text.Class
( ToText (..) )
import Data.Time.Clock
( UTCTime )
import Data.Time.Clock.POSIX
( posixSecondsToUTCTime )
import Data.Word
( Word64 )
import GHC.Generics
( Generic )
import Test.Hspec
Expand Down Expand Up @@ -250,19 +244,8 @@ import Test.QuickCheck.Monadic
( assert, monadicIO, monitor, run )
import Test.Utils.Time
( UniformTime )
import Test.Utils.Trace
( captureLogging' )
import UnliftIO.Concurrent
( MVar
, modifyMVar
, modifyMVar_
, newEmptyMVar
, newMVar
, putMVar
, readMVar
, takeMVar
, threadDelay
)
( MVar, modifyMVar, newEmptyMVar, putMVar, takeMVar, threadDelay )

import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet as W
Expand All @@ -281,7 +264,6 @@ import qualified Data.Foldable as F
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
import qualified Data.Text as T

spec :: Spec
spec = parallel $ describe "Cardano.WalletSpec" $ do
Expand Down Expand Up @@ -330,8 +312,6 @@ spec = parallel $ describe "Cardano.WalletSpec" $ do
(property prop_estimateFee)

describe "LocalTxSubmission" $ do
it "LocalTxSubmission pool retries pending transactions"
(property prop_localTxSubmission)
it "LocalTxSubmission updates are limited in frequency"
(property prop_throttle)

Expand Down Expand Up @@ -651,9 +631,6 @@ data TxRetryTest = TxRetryTest
, retryTestWallet :: (WalletId, WalletName, DummyState)
} deriving (Generic, Show, Eq)

numSlots :: TxRetryTest -> Word64
numSlots = const 100

newtype GenTxHistory = GenTxHistory { getTxHistory :: [(Tx, TxMeta)] }
deriving (Generic, Show, Eq)

Expand Down Expand Up @@ -769,81 +746,6 @@ instance MonadMonotonicTime TxRetryTestM where
instance MonadTime TxRetryTestM where
getCurrentTime = liftIO getCurrentTime

prop_localTxSubmission :: TxRetryTest -> Property
prop_localTxSubmission tc = monadicIO $ do
st <- TxRetryTestState tc 2 <$> newMVar (Time 0)
res <- run $ runTest st $ \ctx@(TxRetryTestCtx DBLayer{..} _ _ wid) -> do
-- Test setup
atomically $ do
let txHistory = getTxHistory (retryTestTxHistory tc)
unsafeRunExceptT $ putTxHistory wid txHistory
forM_ (retryTestPool tc) $ \(LocalTxSubmissionStatus i tx sl) ->
unsafeRunExceptT $ putLocalTxSubmission wid i tx sl

-- Run test
let cfg = LocalTxSubmissionConfig (timeStep st) 10
runLocalTxSubmissionPool @_ @DummyState @ShelleyKey cfg ctx wid

-- Gather state
atomically $ readLocalTxSubmissionPending wid

monitor $ counterexample $ unlines $
[ "posted txs = " ++ show (resSubmittedTxs res)
, "final pool state = " ++ show (resAction res)
, "logs:"
] ++ map (T.unpack . toText) (resLogs res)

-- props:
-- 1. pending transactions in pool are retried
let inPool = (`elem` (submittedTx <$> retryTestPool tc))
assert (all inPool (resSubmittedTxs res))

-- 2. non-pending transactions not retried
let nonPending = map (view #txId . fst)
. filter ((/= Pending) . view #status . snd)
. getTxHistory $ retryTestTxHistory tc
assert (all (`notElem` (map fakeSealedTxId $ resSubmittedTxs res)) nonPending)

-- 3. retries can fail and not break the wallet
assert (not $ null $ resAction res)

where
runTest
:: TxRetryTestState
-> (TxRetryTestCtx -> TxRetryTestM a)
-> IO (TxRetryTestResult a)
runTest st testAction = do
submittedVar <- newMVar []
(msgs, res) <- captureLogging' $ \tr -> do
flip runReaderT st $ unTxRetryTestM $ do
WalletLayerFixture db _wl [wid] _slotNoTime <-
setupFixture $ retryTestWallet tc
let ctx = TxRetryTestCtx db (mockNetwork submittedVar) tr wid

testAction ctx
TxRetryTestResult msgs res <$> readMVar submittedVar

mockNetwork :: MVar [SealedTx] -> NetworkLayer TxRetryTestM Read.Block
mockNetwork var = dummyNetworkLayer
{ currentSlottingParameters = pure (testSlottingParameters tc)
, postTx = \tx -> ExceptT $ do
stash var tx
pure $ case lookup tx (postTxResults tc) of
Just True -> Right ()
Just False -> Left (W.ErrPostTxValidationError "intended")
Nothing -> Left (W.ErrPostTxValidationError "unexpected")
, watchNodeTip = mockNodeTip (numSlots tc) 0
}

mockNodeTip end sl cb
| sl < end = do
let h = Hash ""
void $ cb $ BlockHeader (SlotNo sl) (Quantity (fromIntegral sl)) h (Just h)
mockNodeTip end (sl + 1) cb
| otherwise = pure ()

stash :: MVar [a] -> a -> TxRetryTestM ()
stash var x = modifyMVar_ var (\xs -> pure (x:xs))

{-------------------------------------------------------------------------------
'throttle' Util Function
Expand Down Expand Up @@ -1268,12 +1170,6 @@ fakeSealedTx (tx, wit) = mockSealedTx $ B8.pack repr
where
repr = show (view #txId tx, wit)

fakeSealedTxId :: SealedTx -> Hash "Tx"
fakeSealedTxId = fst . parse . B8.unpack . serialisedTx
where
parse :: String -> (Hash "Tx", [ByteString])
parse = read

mockNetworkLayer :: Monad m => NetworkLayer m block
mockNetworkLayer = dummyNetworkLayer
{ currentNodeTip =
Expand Down

0 comments on commit 0d717a1

Please sign in to comment.