Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3480 from input-output-hk/kderme/CBR-390
Browse files Browse the repository at this point in the history
fixing names and comments
  • Loading branch information
edsko authored Aug 25, 2018
2 parents cbf87eb + 6e63f44 commit beae68f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
12 changes: 6 additions & 6 deletions wallet-new/src/Cardano/Wallet/Kernel/DB/Resolved.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
15 changes: 9 additions & 6 deletions wallet-new/src/Cardano/Wallet/Kernel/Pending.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand All @@ -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
Expand All @@ -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)
Expand Down
33 changes: 17 additions & 16 deletions wallet-new/src/Cardano/Wallet/Kernel/Transactions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit beae68f

Please sign in to comment.