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

Commit

Permalink
[CSE-241] Allow not resetting DB for getUtxoSum (#1687)
Browse files Browse the repository at this point in the history
* [CSE-241] Recompute utxo sum if it's not in DB

* [CSE-241] Get rid of putGenesisUtxoSum

* [CSE-241] Remove redundancy
  • Loading branch information
Pastafarianist authored and domenkozar committed Oct 1, 2017
1 parent c563017 commit dd2dd10
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions node/src/Pos/Explorer/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import System.Wlog (WithLogger, logError)

import Pos.Binary.Class (UnsignedVarInt (..), serialize')
import Pos.Core (Address, Coin, EpochIndex, HeaderHash,
sumCoins, unsafeAddCoin)
coinToInteger, unsafeAddCoin)
import Pos.Core.Configuration (HasConfiguration)
import Pos.DB (DBError (..), DBIteratorClass (..),
DBTag (GStateDB), MonadDB,
Expand Down Expand Up @@ -98,19 +98,21 @@ getLastTransactions = gsGetBi lastTxsPrefix
----------------------------------------------------------------------------

prepareExplorerDB :: (MonadReader ctx m, MonadDB m) => m ()
prepareExplorerDB =
unlessM areBalancesInitialized $ do
prepareExplorerDB = do
unlessM balancesInitializedM $ do
let GenesisUtxo utxo = genesisUtxo
addressCoinPairs = utxoToAddressCoinPairs utxo
putGenesisBalances addressCoinPairs
putGenesisUtxoSum addressCoinPairs
putInitFlag
-- Smooth migration for CSE-228.
unlessM utxoSumInitializedM $ do
putCurrentUtxoSum

balancesInitFlag :: ByteString
balancesInitFlag = "e/init/"

areBalancesInitialized :: MonadDBRead m => m Bool
areBalancesInitialized = isJust <$> dbGet GStateDB balancesInitFlag
balancesInitializedM :: MonadDBRead m => m Bool
balancesInitializedM = isJust <$> dbGet GStateDB balancesInitFlag

putInitFlag :: MonadDB m => m ()
putInitFlag = gsPutBi balancesInitFlag True
Expand All @@ -121,10 +123,19 @@ putGenesisBalances addressCoinPairs = writeBatchGState putAddrBalancesOp
putAddrBalancesOp :: [ExplorerOp]
putAddrBalancesOp = map (uncurry PutAddrBalance) addressCoinPairs

putGenesisUtxoSum :: MonadDB m => [(Address, Coin)] -> m ()
putGenesisUtxoSum addressCoinPairs = do
let utxoSum = sumCoins $ map snd addressCoinPairs
utxoSumInitializedM :: MonadDBRead m => m Bool
utxoSumInitializedM = isJust <$> dbGet GStateDB utxoSumPrefix

putCurrentUtxoSum :: MonadDB m => m ()
putCurrentUtxoSum = do
utxoSum <- computeUtxoSum
writeBatchGState [PutUtxoSum utxoSum]
where
computeUtxoSum :: MonadDBRead m => m Integer
computeUtxoSum = do
let txOutValueSource =
mapOutput (coinToInteger . txOutValue . toaOut . snd) utxoSource
runConduitRes $ txOutValueSource .| CL.fold (+) 0

----------------------------------------------------------------------------
-- Batch operations
Expand Down

0 comments on commit dd2dd10

Please sign in to comment.