Skip to content

Commit

Permalink
Merge #834 #840 #845
Browse files Browse the repository at this point in the history
834: DaedalusIPC: Fix isWindows function r=KtorZ a=rvl

# Issue Number

None.

# Overview

Fixes a typo in the OS detection code for NodeIPC.

# Comments

I again looked at the possibility of importing the cardano-shell package for this functionality. But it is pulling in more dependencies. Also the `daedalusIPC` function there had the logging replaced with `putTextLn`. Branch is [rvl/daedalus-ipc-cardano-shell](rvl/daedalus-ipc-fix...rvl/daedalus-ipc-cardano-shell) for reference though.


840: Make Jörmungandr NetworkLayer use raw blocks r=KtorZ a=Anviking


# Issue Number

#711 

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- [x] I changed the constructors in `Cardano.Wallet.Jormungandr.Network` to create NetworkLayers with raw jörmungadnr-specific blocks, and later used `toWLBlock <$> nl`. This way we can later use `toSPBlock <$> nl` for stake-pools.


# Comments

<!-- Additional comments or screenshots to attach if any -->

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Acknowledge any changes required to the Wiki
-->


845: Add forget pending transaction endpoint (Part1) r=KtorZ a=paweljakubas

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->
#836 

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- [x] I have added forget pending transaction endpoint, errors and types
- [x] I have updated swagger accordingly


# Comments

<!-- Additional comments or screenshots to attach if any -->

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Rodney Lorrimar <rodney.lorrimar@iohk.io>
Co-authored-by: Johannes Lund <johannes.lund@iohk.io>
Co-authored-by: Pawel Jakubas <pawel.jakubas@iohk.io>
  • Loading branch information
4 people authored Oct 16, 2019
4 parents fcabaf9 + 4069d92 + b41e6b8 + c65982c commit d525e85
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 17 deletions.
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")
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

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
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/DaedalusIPC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import GHC.IO.Handle.FD
import System.Environment
( lookupEnv )
import System.Info
( arch )
( os )
import System.IO
( Handle, hFlush, hGetLine, hSetNewlineMode, noNewlineTranslation )
import System.IO.Error
Expand Down Expand Up @@ -214,7 +214,7 @@ readMessage :: Handle -> IO BL.ByteString
readMessage = if isWindows then windowsReadMessage else posixReadMessage

isWindows :: Bool
isWindows = arch == "windows"
isWindows = os == "windows"

windowsReadMessage :: Handle -> IO BL.ByteString
windowsReadMessage handle = do
Expand Down
15 changes: 9 additions & 6 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import System.Exit
import qualified Cardano.BM.Configuration.Model as CM
import qualified Cardano.Wallet.Api.Server as Server
import qualified Cardano.Wallet.DB.Sqlite as Sqlite
import qualified Cardano.Wallet.Jormungandr.Binary as J
import qualified Data.Text as T
import qualified Network.Wai.Handler.Warp as Warp

Expand Down Expand Up @@ -128,21 +129,21 @@ serveWallet (cfg, sb, tr) databaseDir listen lj beforeMainLoop = do
let nPort = Port $ baseUrlPort $ _restApi cp
waitForService "Jörmungandr" (sb, tr) nPort $
waitForNetwork nl defaultRetryPolicy
rndApi <- apiLayer tr nl
seqApi <- apiLayer tr nl
startServer tr nPort nl rndApi seqApi
let (_, bp) = staticBlockchainParameters nl
rndApi <- apiLayer tr (toWLBlock <$> nl)
seqApi <- apiLayer tr (toWLBlock <$> nl)
startServer tr nPort bp rndApi seqApi
pure ExitSuccess
Left e -> handleNetworkStartupError e
where
startServer
:: Trace IO Text
-> Port "node"
-> NetworkLayer IO t (Block Tx)
-> BlockchainParameters
-> ApiLayer (RndState t) t RndKey
-> ApiLayer (SeqState t) t SeqKey
-> IO ()
startServer tracer nPort nl rndWallet seqWallet = do
let (_, bp) = staticBlockchainParameters nl
startServer tracer nPort bp rndWallet seqWallet = do
Server.withListeningSocket listen $ \(wPort, socket) -> do
let tracerIPC = appendName "daedalus-ipc" tracer
let tracerApi = appendName "api" tracer
Expand All @@ -153,6 +154,8 @@ serveWallet (cfg, sb, tr) databaseDir listen lj beforeMainLoop = do
Server.start settings tracerApi socket rndWallet seqWallet
race_ ipcServer apiServer

toWLBlock = J.convertBlock

apiLayer
:: forall s k .
( KeyToAddress (Jormungandr 'Testnet) k
Expand Down
17 changes: 8 additions & 9 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Cardano.Wallet.Jormungandr.Network
JormungandrBackend (..)
, JormungandrConnParams (..)
, withNetworkLayer
, newNetworkLayer

-- * Launching the node backend
, JormungandrConfig (..)
Expand Down Expand Up @@ -86,7 +87,7 @@ import Cardano.Wallet.Jormungandr.Api.Client
, postMessage
)
import Cardano.Wallet.Jormungandr.Binary
( convertBlock, runGetOrFail )
( runGetOrFail )
import Cardano.Wallet.Jormungandr.BlockHeaders
( BlockHeaders (..)
, appendBlockHeaders
Expand All @@ -100,16 +101,14 @@ import Cardano.Wallet.Jormungandr.BlockHeaders
)
import Cardano.Wallet.Jormungandr.Compatibility
( Jormungandr, genConfigFile, localhostBaseUrl )
import Cardano.Wallet.Jormungandr.Primitive.Types
( Tx )
import Cardano.Wallet.Network
( Cursor, NetworkLayer (..), NextBlocksResult (..), defaultRetryPolicy )
import Cardano.Wallet.Network.Ports
( PortNumber, getRandomPort, waitForPort )
import Cardano.Wallet.Primitive.Model
( BlockchainParameters (..) )
import Cardano.Wallet.Primitive.Types
( Block (..), BlockHeader (..), Hash (..), SlotId (..) )
( BlockHeader (..), Hash (..), SlotId (..) )
import Control.Concurrent.MVar.Lifted
( MVar, modifyMVar, newMVar, readMVar )
import Control.Exception
Expand Down Expand Up @@ -179,7 +178,7 @@ withNetworkLayer
-- ^ Logging
-> JormungandrBackend
-- ^ How Jörmungandr is started.
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t (Block Tx)) -> IO a)
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t J.Block) -> IO a)
-- ^ The action to run. It will be passed the connection parameters used,
-- and a network layer if startup was successful.
-> IO a
Expand All @@ -192,7 +191,7 @@ withNetworkLayerLaunch
-- ^ Logging of node startup.
-> JormungandrConfig
-- ^ Configuration for starting Jörmungandr.
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t (Block Tx)) -> IO a)
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t J.Block) -> IO a)
-- ^ The action to run. It will be passed the connection parameters used,
-- and a network layer if startup was successful.
-> IO a
Expand All @@ -204,7 +203,7 @@ withNetworkLayerConn
:: forall n a t. (t ~ Jormungandr n)
=> JormungandrConnParams
-- ^ Parameters for connecting to Jörmungandr node which is already running.
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t (Block Tx)) -> IO a)
-> (Either ErrStartup (JormungandrConnParams, NetworkLayer IO t J.Block) -> IO a)
-- ^ Action to run with the network layer.
-> IO a
withNetworkLayerConn cp@(JormungandrConnParams block0H baseUrl) action =
Expand All @@ -219,13 +218,13 @@ newNetworkLayer
:: forall n t. (t ~ Jormungandr n)
=> BaseUrl
-> Hash "Genesis"
-> ExceptT ErrGetBlockchainParams IO (NetworkLayer IO t (Block Tx))
-> ExceptT ErrGetBlockchainParams IO (NetworkLayer IO t J.Block)
newNetworkLayer baseUrl block0H = do
mgr <- liftIO $ newManager defaultManagerSettings
st <- newMVar emptyBlockHeaders
let jor = mkJormungandrClient mgr baseUrl
g0 <- getInitialBlockchainParameters jor (coerce block0H)
return (convertBlock <$> mkRawNetworkLayer g0 st jor)
return (mkRawNetworkLayer g0 st jor)

-- | Wrap a Jormungandr client into a 'NetworkLayer' common interface.
--
Expand Down
Loading

0 comments on commit d525e85

Please sign in to comment.