Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forget pending transaction endpoint (Part1) #845

Merged
merged 4 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,10 @@ data WalletClient t = WalletClient
, postExternalTransaction
:: PostExternalTransactionData
-> ClientM ApiTxId
, deleteTransaction
:: ApiT WalletId
-> ApiTxId
-> ClientM NoContent
, listPools
:: ClientM [ApiStakePool]
, networkInformation
Expand Down Expand Up @@ -986,6 +990,7 @@ walletClient =
:<|> _listTransactions
:<|> _postTransactionFee
:<|> _postExternalTransaction
:<|> _deleteTransaction
= transactions

_listPools = pools
Expand All @@ -1003,6 +1008,7 @@ walletClient =
, listTransactions = _listTransactions
, postTransaction = _postTransaction
, postExternalTransaction = _postExternalTransaction
, deleteTransaction = _deleteTransaction
, postTransactionFee = _postTransactionFee
, getWalletUtxoStatistics = _getWalletUtxoStatistics
, listPools = _listPools
Expand Down
23 changes: 23 additions & 0 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module Cardano.Wallet
, updateWalletPassphrase
, ErrWalletAlreadyExists (..)
, ErrNoSuchWallet (..)
, ErrNoSuchTransaction (..)
, ErrListUTxOStatistics (..)
, ErrUpdatePassphrase (..)

Expand All @@ -81,6 +82,7 @@ module Cardano.Wallet
-- ** Transaction
, createUnsignedTx
, estimateTxFee
, forgetPendingTx
, listTransactions
, signTx
, submitExternalTx
Expand All @@ -93,6 +95,7 @@ module Cardano.Wallet
, ErrCoinSelection (..)
, ErrSubmitTx (..)
, ErrSubmitExternalTx (..)
, ErrForgetPendingTx (..)
, ErrPostTx (..)
, ErrDecodeSignedTx (..)
, ErrValidateSelection
Expand All @@ -110,6 +113,7 @@ import Cardano.BM.Trace
( Trace, logDebug, logInfo )
import Cardano.Wallet.DB
( DBLayer
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrWalletAlreadyExists (..)
, PrimaryKey (..)
Expand Down Expand Up @@ -167,6 +171,7 @@ import Cardano.Wallet.Primitive.Types
, Coin (..)
, DefineTx (..)
, Direction (..)
, Hash (..)
, Range (..)
, SlotId (..)
, SlotParameters (..)
Expand Down Expand Up @@ -829,6 +834,17 @@ submitExternalTx ctx bytes = do
nw = ctx ^. networkLayer @t
tl = ctx ^. transactionLayer @t @k

-- | Forget pending transaction.
forgetPendingTx
:: forall ctx s t k.
( HasDBLayer s t k ctx
, DefineTx t
)
=> ctx
-> WalletId
-> Hash "Tx"
-> ExceptT ErrForgetPendingTx IO ()
forgetPendingTx _ctx _wid _tid = undefined

-- | List all transactions and metadata from history for a given wallet.
listTransactions
Expand Down Expand Up @@ -1013,6 +1029,13 @@ data ErrSubmitExternalTx
| ErrSubmitExternalTxDecode ErrDecodeSignedTx
deriving (Show, Eq)

-- | Errors that can occur when trying to change a wallet's passphrase.
data ErrForgetPendingTx
= ErrForgetPendingTxNoSuchWallet ErrNoSuchWallet
| ErrForgetPendingTxNoSuchTransaction ErrNoSuchTransaction
| ErrForgetPendingTxTransactionIsNotPending (Hash "Tx")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Good point 😅

deriving (Show, Eq)

-- | Errors that can occur when trying to change a wallet's passphrase.
data ErrUpdatePassphrase
= ErrUpdatePassphraseNoSuchWallet ErrNoSuchWallet
Expand Down
8 changes: 8 additions & 0 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type Transactions t =
:<|> ListTransactions t
:<|> PostTransactionFee t
:<|> PostExternalTransaction
:<|> DeleteTransaction

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postTransaction
type CreateTransaction t = "wallets"
Expand All @@ -233,6 +234,13 @@ type ListTransactions t = "wallets"
:> QueryParam "order" (ApiT SortOrder)
:> Get '[JSON] [ApiTransaction t]

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteTransaction
type DeleteTransaction = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions"
:> Capture "transactionId" ApiTxId
:> DeleteNoContent '[Any] NoContent

{-------------------------------------------------------------------------------
StakePools

Expand Down
37 changes: 37 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ import Cardano.Wallet
, ErrCreateUnsignedTx (..)
, ErrDecodeSignedTx (..)
, ErrEstimateTxFee (..)
, ErrForgetPendingTx (..)
, ErrListTransactions (..)
, ErrListUTxOStatistics (..)
, ErrMkStdTx (..)
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrPostTx (..)
, ErrSignTx (..)
Expand Down Expand Up @@ -559,6 +561,7 @@ transactions ctx =
:<|> listTransactions ctx
:<|> postTransactionFee ctx
:<|> postExternalTransaction ctx
:<|> deleteTransaction ctx

postTransaction
:: forall ctx s t k.
Expand Down Expand Up @@ -611,6 +614,22 @@ postExternalTransaction ctx (PostExternalTransactionData load) = do
tx <- liftHandler $ W.submitExternalTx @ctx @t @k ctx load
return $ ApiTxId (ApiT (txId @t tx))

deleteTransaction
:: forall ctx s t k.
( ctx ~ ApiLayer s t k
, DefineTx t
)
=> ctx
-> ApiT WalletId
-> ApiTxId
-> Handler NoContent
deleteTransaction ctx (ApiT wid) (ApiTxId (ApiT (tid))) = do
liftHandler $ withWorkerCtx ctx wid liftE $ \wrk ->
W.forgetPendingTx wrk wid tid
return NoContent
where
liftE = throwE . ErrForgetPendingTxNoSuchWallet

listTransactions
:: forall ctx s t k.
( DefineTx t
Expand Down Expand Up @@ -1079,6 +1098,14 @@ instance LiftHandler ErrNoSuchWallet where
, toText wid
]

instance LiftHandler ErrNoSuchTransaction where
handler = \case
ErrNoSuchTransaction tid ->
apiError err404 NoSuchTransaction $ mconcat
[ "I couldn't find a transaction with the given id: "
, toText tid
]

instance LiftHandler ErrWalletAlreadyExists where
handler = \case
ErrWalletAlreadyExists wid ->
Expand Down Expand Up @@ -1236,6 +1263,16 @@ instance LiftHandler ErrSubmitExternalTx where
, errReasonPhrase = errReasonPhrase err400
}

instance LiftHandler ErrForgetPendingTx where
handler = \case
ErrForgetPendingTxNoSuchWallet e -> handler e
ErrForgetPendingTxNoSuchTransaction e -> handler e
ErrForgetPendingTxTransactionIsNotPending tid ->
apiError err404 TransactionNotPending $ mconcat
[ "The transaction with id : ", toText tid,
" cannot be forgotten as it is not pending anymore."
]

instance LiftHandler ErrSubmitTx where
handler = \case
ErrSubmitTxNetwork e -> case e of
Expand Down
10 changes: 10 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ data ApiNetworkInformation = ApiNetworkInformation
-- | Error codes returned by the API, in the form of snake_cased strings
data ApiErrorCode
= NoSuchWallet
| NoSuchTransaction
| TransactionNotPending
| WalletAlreadyExists
| NoRootKey
| WrongEncryptionPassphrase
Expand Down Expand Up @@ -734,6 +736,14 @@ instance MimeUnrender OctetStream PostExternalTransactionData where
instance MimeRender OctetStream PostExternalTransactionData where
mimeRender _ (PostExternalTransactionData val) = BL.fromStrict val

instance FromHttpApiData ApiTxId where
parseUrlPiece txt = case fromText txt of
Left (TextDecodingError err) -> Left $ T.pack err
Right tid -> Right $ ApiTxId $ ApiT tid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the FromText instance is confusing here 🤔 (especially because we have to still do some conversion on top in parseUrlPiece for the error).
I'd suggest to inline this with the definition in FromHttpApiData ApiTxId and return the right error type directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


instance ToHttpApiData ApiTxId where
toUrlPiece (ApiTxId (ApiT tid)) = toText tid

{-------------------------------------------------------------------------------
Aeson Options
-------------------------------------------------------------------------------}
Expand Down
6 changes: 6 additions & 0 deletions lib/core/src/Cardano/Wallet/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Cardano.Wallet.DB
, sparseCheckpoints

-- * Errors
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet(..)
, ErrWalletAlreadyExists(..)
) where
Expand Down Expand Up @@ -185,6 +186,11 @@ newtype ErrNoSuchWallet
= ErrNoSuchWallet WalletId -- Wallet is gone or doesn't exist yet
deriving (Eq, Show)

-- | Can't perform given operation because there's no transaction
newtype ErrNoSuchTransaction
= ErrNoSuchTransaction (Hash "Tx")
deriving (Eq, Show)

-- | Forbidden operation was executed on an already existing wallet
newtype ErrWalletAlreadyExists
= ErrWalletAlreadyExists WalletId -- Wallet already exists in db
Expand Down
33 changes: 33 additions & 0 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,15 @@ x-parametersWalletId: &parametersWalletId
maxLength: 40
minLength: 40

x-parametersTransactionId: &parametersTransactionId
in: path
name: transactionId
required: true
type: string
format: hex
maxLength: 64
minLength: 64

x-parametersStakePoolId: &parametersStakePoolId
in: path
name: stakePoolId
Expand Down Expand Up @@ -866,6 +875,16 @@ x-responsesPutWalletPassphrase: &responsesPutWalletPassphrase
204:
description: No Content

x-responsesDeleteTransaction: &responsesDeleteTransaction
<<: *responsesErr400
<<: *responsesErr403
<<: *responsesErr404
<<: *responsesErr405
<<: *responsesErr406
<<: *responsesErr415
204:
description: No Content

x-responsesListTransactions: &responsesListTransactions
<<: *responsesErr404
<<: *responsesErr405
Expand Down Expand Up @@ -1155,6 +1174,20 @@ paths:
schema: *ApiPostTransactionFeeData
responses: *responsesPostTransactionFee

/wallets/{walletId}/transactions/{transactionId}:
delete:
operationId: deleteTransaction
tags: ["Transactions"]
summary: Delete
description: |
<p align="right">status: <strong>under development</strong></p>

Forget pending transaction.
parameters:
- *parametersWalletId
- *parametersTransactionId
responses: *responsesDeleteTransaction

/wallets/{walletId}/addresses:
get:
operationId: listAddresses
Expand Down