From 6e63f446186d5838f1f5e9c4aafbb5573c88649f Mon Sep 17 00:00:00 2001 From: kderme Date: Fri, 24 Aug 2018 20:25:49 +0300 Subject: [PATCH] fixing names and comments --- .../src/Cardano/Wallet/Kernel/DB/Resolved.hs | 12 +++---- .../src/Cardano/Wallet/Kernel/Pending.hs | 15 +++++---- .../src/Cardano/Wallet/Kernel/Transactions.hs | 33 ++++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/wallet-new/src/Cardano/Wallet/Kernel/DB/Resolved.hs b/wallet-new/src/Cardano/Wallet/Kernel/DB/Resolved.hs index 0e6aaa38aa5..cf0d704ffe2 100644 --- a/wallet-new/src/Cardano/Wallet/Kernel/DB/Resolved.hs +++ b/wallet-new/src/Cardano/Wallet/Kernel/DB/Resolved.hs @@ -61,12 +61,12 @@ data ResolvedTx = ResolvedTx { } -- | This is used when apply block is called, during prefiltering, so related inputs --- and outputs to the HDAccount are known. --- @inCoin@ is the coins from input addresses of the account. --- @outCoin@ is the coins from output addresses of the account. +-- and outputs to the HDAccount are known to the caller. +-- @spentInputsCoins@ is the coins from input addresses of the account. They reduce the balance. +-- @gainedOutputsCoins@ is the coins from output addresses of the account. They increase the balance. -- @allOurs@ indictes if all inputs and outputs addresses belong to the account. resolvedToTxMeta :: ResolvedTx -> Coin -> Coin -> Bool -> HD.HdAccountId -> TxMeta -resolvedToTxMeta ResolvedTx{..} inCoin outCoin allOurs accountId = +resolvedToTxMeta ResolvedTx{..} spentInputsCoins gainedOutputsCoins allOurs accountId = fromMaybe (error "Invalid ResolvedTx") mbMeta where mbMeta = do @@ -75,12 +75,12 @@ resolvedToTxMeta ResolvedTx{..} inCoin outCoin allOurs accountId = let (txId, timestamp) = _fromDb _rtxMeta return TxMeta { _txMetaId = txId - , _txMetaAmount = absCoin inCoin outCoin + , _txMetaAmount = absCoin spentInputsCoins gainedOutputsCoins , _txMetaInputs = inps , _txMetaOutputs = outs , _txMetaCreationAt = timestamp , _txMetaIsLocal = allOurs - , _txMetaIsOutgoing = outCoin < inCoin + , _txMetaIsOutgoing = gainedOutputsCoins < spentInputsCoins -- it's outgoing if gained is less than spent. , _txMetaWalletId = _fromDb $ HD.getHdRootId (accountId ^. HD.hdAccountIdParent) , _txMetaAccountIx = HD.getHdAccountIx $ accountId ^. HD.hdAccountIdIx } diff --git a/wallet-new/src/Cardano/Wallet/Kernel/Pending.hs b/wallet-new/src/Cardano/Wallet/Kernel/Pending.hs index ef8992a2c14..905eb7dc091 100644 --- a/wallet-new/src/Cardano/Wallet/Kernel/Pending.hs +++ b/wallet-new/src/Cardano/Wallet/Kernel/Pending.hs @@ -41,8 +41,11 @@ import Pos.Wallet.Web.Tracking.Decrypt (WalletDecrCredentialsKey (..), Submit pending transactions -------------------------------------------------------------------------------} - -type PartialTxMeta = Bool -> Coin -> IO TxMeta +-- | When we create a new Transaction, we don`t yet know which outputs belong to us +-- (it may not be just the change addresses change we create, but also addresses the user specifies). +-- This check happenes in @newTx@. Until then we move around this partial TxMetadata. +-- @Bool@ indicates if all outputs are ours and @Coin@ the sum of the coin of our outputs. +type PartialTxMeta = Bool -> Coin -> TxMeta -- | Submit a new pending transaction -- @@ -69,7 +72,7 @@ newForeign :: ActiveWallet -> TxMeta -> IO (Either NewForeignError ()) newForeign w accountId tx meta = do - map void <$> newTx w accountId tx (\_ _ -> return meta) $ \ourAddrs -> + map void <$> newTx w accountId tx (\_ _ -> meta) $ \ourAddrs -> update' ((walletPassive w) ^. wallets) $ NewForeign accountId (InDb tx) ourAddrs -- | Submit a new transaction @@ -90,15 +93,15 @@ newTx :: forall e. ActiveWallet newTx ActiveWallet{..} accountId tx partialMeta upd = do -- run the update allOurs' <- allOurs <$> getWalletCredentials walletPassive - let (addrsOurs',coinsOurs) = unzip allOurs' - outCoins = sumCoinsUnsafe coinsOurs + let (addrsOurs',ourOutputCoins) = unzip allOurs' + gainedOutputCoins = sumCoinsUnsafe ourOutputCoins allOutsOurs = length allOurs' == length txOut res <- upd $ addrsOurs' case res of Left e -> return (Left e) Right () -> do -- process transaction on success - meta <- partialMeta allOutsOurs outCoins + let meta = partialMeta allOutsOurs gainedOutputCoins putTxMeta (walletPassive ^. walletMeta) meta submitTx return (Right meta) diff --git a/wallet-new/src/Cardano/Wallet/Kernel/Transactions.hs b/wallet-new/src/Cardano/Wallet/Kernel/Transactions.hs index d6323d90d39..41eab65920c 100644 --- a/wallet-new/src/Cardano/Wallet/Kernel/Transactions.hs +++ b/wallet-new/src/Cardano/Wallet/Kernel/Transactions.hs @@ -212,9 +212,9 @@ newTransaction ActiveWallet{..} spendingPassword options accountId payees = runE -- STEP 4: Compute metadata let txId = hash . taTx $ txAux -- This is the sum of inputs coins. - let inCoins = paymentAmount (toaOut . snd <$> inputs) + let spentInputCoins = paymentAmount (toaOut . snd <$> inputs) -- partially applied, because we don`t know here which outputs are ours - let partialMeta = createNewMeta accountId txId inputs (toaOut <$> outputs) True inCoins + partialMeta <- liftIO $ createNewMeta accountId txId inputs (toaOut <$> outputs) True spentInputCoins return (txAux, partialMeta, availableUtxo) where -- Generate an initial seed for the random generator using the hash of @@ -256,22 +256,23 @@ newTransaction ActiveWallet{..} spendingPassword options accountId payees = runE walletPassive -- | This is called when we create a new Pending Transaction. -createNewMeta :: HdAccountId -> TxId -> NonEmpty (TxIn, TxOutAux) -> NonEmpty TxOut -> Bool -> Coin -> Bool -> Coin -> IO TxMeta -createNewMeta hdId txId inp out allInOurs inCoin allOutOurs outCoin = do +-- This actually returns a function because we don`t know yet our outputs. +createNewMeta :: HdAccountId -> TxId -> NonEmpty (TxIn, TxOutAux) -> NonEmpty TxOut -> Bool -> Coin -> IO PartialTxMeta +createNewMeta hdId txId inp out allInOurs spentInputsCoins = do time <- liftIO getCurrentTimestamp - metaForNewTx time hdId txId inp out allInOurs inCoin allOutOurs outCoin + return $ metaForNewTx time hdId txId inp out allInOurs spentInputsCoins + -- ^ this partially applied function indicates the lack of all TxMeta at this stage. -metaForNewTx :: Monad m => Core.Timestamp -> - HdAccountId -> TxId -> NonEmpty (TxIn, TxOutAux) -> NonEmpty TxOut -> Bool -> Coin -> Bool -> Coin -> m TxMeta -metaForNewTx time accountId txId inputs outputs allInpOurs inCoin allOutOurs outCoin = - return $ TxMeta { +metaForNewTx :: Core.Timestamp -> HdAccountId -> TxId -> NonEmpty (TxIn, TxOutAux) -> NonEmpty TxOut -> Bool -> Coin -> Bool -> Coin -> TxMeta +metaForNewTx time accountId txId inputs outputs allInpOurs spentInputsCoins allOutOurs gainedOutputsCoins = + TxMeta { _txMetaId = txId - , _txMetaAmount = absCoin inCoin outCoin + , _txMetaAmount = absCoin spentInputsCoins gainedOutputsCoins , _txMetaInputs = inputsForMeta , _txMetaOutputs = aux <$> outputs , _txMetaCreationAt = time , _txMetaIsLocal = allInpOurs && allOutOurs - , _txMetaIsOutgoing = outCoin < inCoin -- it`s outgoing if our inputs used are more than the new utxo. + , _txMetaIsOutgoing = gainedOutputsCoins < spentInputsCoins -- it`s outgoing if our inputs spent are more than the new utxo. , _txMetaWalletId = _fromDb $ getHdRootId (accountId ^. hdAccountIdParent) , _txMetaAccountIx = getHdAccountIx $ accountId ^. hdAccountIdIx } @@ -284,17 +285,17 @@ metaForNewTx time accountId txId inputs outputs allInpOurs inCoin allOutOurs out in (txid, index, addr, coins) TxInUnknown _ _ -> error "Tried to create TxMeta with unknown input" --- | Different wraper for @metaForNewTx@ mainly for testing. -toMeta :: Monad m => Core.Timestamp -> HdAccountId -> RawResolvedTx -> Bool -> Coin -> m TxMeta -toMeta time accountId UnsafeRawResolvedTx{..} allOutOurs outCoin = do +-- | Different wraper for @metaForNewTx@ mainly for testing Only NewPending Transactions. +toMeta :: Core.Timestamp -> HdAccountId -> RawResolvedTx -> PartialTxMeta +toMeta time accountId UnsafeRawResolvedTx{..} allOutOurs outCoin = let allInpOurs = True txId = hash . taTx $ rawResolvedTx (txIn :: NonEmpty TxIn) = _txInputs $ taTx rawResolvedTx (inputsRes :: NonEmpty TxOutAux) = rawResolvedTxInputs - inpCoin = paymentAmount $ toaOut <$> inputsRes + spentInputCoins = paymentAmount $ toaOut <$> inputsRes inputs = NonEmpty.zip txIn inputsRes txOut = _txOutputs $ taTx rawResolvedTx - metaForNewTx time accountId txId inputs txOut allInpOurs inpCoin allOutOurs outCoin + in metaForNewTx time accountId txId inputs txOut allInpOurs spentInputCoins allOutOurs outCoin -- | Special monad used to process the payments, which randomness is derived -- from a fixed seed obtained from hashing the payees. This guarantees that