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

Commit dfefe8d

Browse files
committed
clean up un-needed code
remove some redundant fields
1 parent 213c02e commit dfefe8d

File tree

3 files changed

+36
-41
lines changed

3 files changed

+36
-41
lines changed

explorer/src/Pos/Explorer/Web/Server.hs

+24-40
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import Pos.Infra.Diffusion.Types (Diffusion)
5959
import Pos.Binary.Class (biSize)
6060
import Pos.Chain.Block (Block, Blund, HeaderHash, MainBlock, Undo,
6161
gbHeader, gbhConsensus, mainBlockSlot, mainBlockTxPayload,
62-
mcdSlot)
62+
mcdSlot, headerHash)
6363
import Pos.Chain.Genesis as Genesis (Config (..), GenesisHash,
6464
configEpochSlots)
6565
import Pos.Chain.Txp (Tx (..), TxAux, TxId, TxIn (..), TxMap,
@@ -324,8 +324,8 @@ getBlockSummary
324324
-> CHash
325325
-> m CBlockSummary
326326
getBlockSummary genesisConfig cHash = do
327-
headerHash <- unwrapOrThrow $ fromCHash cHash
328-
mainBlund <- getMainBlund (configGenesisHash genesisConfig) headerHash
327+
hh <- unwrapOrThrow $ fromCHash cHash
328+
mainBlund <- getMainBlund (configGenesisHash genesisConfig) hh
329329
toBlockSummary (configEpochSlots genesisConfig) mainBlund
330330

331331

@@ -450,46 +450,27 @@ getBlockRange genesisConfig start stop = do
450450
let txId = hash tx
451451
txExtra <- getTxExtraOrFail txId
452452

453-
-- Return transaction extra (txExtra) fields
454-
let mBlockchainPlace = teBlockchainPlace txExtra -- TODO, remove this, we already know which block
455-
blockchainPlace <- maybeThrow (Internal "No blockchain place.") mBlockchainPlace
456-
457-
let headerHashBP = fst blockchainPlace
458-
let txIndexInBlock = snd blockchainPlace
459-
460453
blkSlotStart <- getBlkSlotStart mb
461454

462-
let blockHeight = fromIntegral $ mb ^. difficultyL
463-
let receivedTime = teReceivedTime txExtra
464-
let blockTime = timestampToPosix <$> blkSlotStart
465-
466-
-- Get block epoch and slot index
467-
let blkHeaderSlot = mb ^. mainBlockSlot
468-
let epochIndex = getEpochIndex $ siEpoch blkHeaderSlot
469-
let slotIndex = getSlotIndex $ siSlot blkHeaderSlot
470-
let blkHash = toCHash headerHashBP
471-
472-
tx <- maybeThrow (Internal "TxExtra return tx index that is out of bounds") $
473-
atMay (toList $ mb ^. mainBlockTxPayload . txpTxs) (fromIntegral txIndexInBlock)
474-
475-
let inputOutputsMB = map (fmap toaOut) $ NE.toList $ teInputOutputs txExtra
476-
let txOutputs = convertTxOutputs . NE.toList $ _txOutputs tx
477-
478-
let totalInputMB = unsafeIntegerToCoin . sumCoins . map txOutValue <$> sequence inputOutputsMB
479-
let totalOutput = unsafeIntegerToCoin $ sumCoins $ map snd txOutputs
455+
let
456+
blockTime = timestampToPosix <$> blkSlotStart
457+
inputOutputsMB = map (fmap toaOut) $ NE.toList $ teInputOutputs txExtra
458+
txOutputs = convertTxOutputs . NE.toList $ _txOutputs tx
459+
totalInputMB = unsafeIntegerToCoin . sumCoins . map txOutValue <$> sequence inputOutputsMB
460+
totalOutput = unsafeIntegerToCoin $ sumCoins $ map snd txOutputs
480461

481462
-- Verify that strange things don't happen with transactions
482463
whenJust totalInputMB $ \totalInput -> when (totalOutput > totalInput) $
483464
throwM $ Internal "Detected tx with output greater than input"
484465

485466
pure $ CTxSummary
486467
{ ctsId = toCTxId txId
487-
, ctsTxTimeIssued = timestampToPosix <$> receivedTime
468+
, ctsTxTimeIssued = timestampToPosix <$> teReceivedTime txExtra
488469
, ctsBlockTimeIssued = blockTime
489-
, ctsBlockHeight = Just blockHeight
490-
, ctsBlockEpoch = Just epochIndex
491-
, ctsBlockSlot = Just slotIndex
492-
, ctsBlockHash = Just blkHash
470+
, ctsBlockHeight = Nothing
471+
, ctsBlockEpoch = Nothing
472+
, ctsBlockSlot = Nothing
473+
, ctsBlockHash = Just $ toCHash $ headerHash mb
493474
, ctsRelayedBy = Nothing
494475
, ctsTotalInput = mkCCoinMB totalInputMB
495476
, ctsTotalOutput = mkCCoin totalOutput
@@ -499,7 +480,7 @@ getBlockRange genesisConfig start stop = do
499480
}
500481
genesisHash = configGenesisHash genesisConfig
501482
go :: ExplorerMode ctx m => HeaderHash -> CBlockRange -> m CBlockRange
502-
go hh state = do
483+
go hh state1 = do
503484
maybeBlund <- getBlund genesisHash hh
504485
newState <- case maybeBlund of
505486
Just (Right blk', undo) -> do
@@ -508,13 +489,13 @@ getBlockRange genesisConfig start stop = do
508489
txs = blk' ^. mainBlockTxPayload . txpTxs
509490
blockSum <- toBlockSummary (configEpochSlots genesisConfig) (blk',undo)
510491
let
511-
state2 = state { cbrBlocks = blockSum : (cbrBlocks state) }
492+
state2 = state1 { cbrBlocks = blockSum : (cbrBlocks state1) }
512493
iterateTx :: ExplorerMode ctx m => CBlockRange -> Tx -> m CBlockRange
513494
iterateTx stateIn tx = do
514495
txSummary <- getTxSummaryFromBlock blk' tx
515496
pure $ stateIn { cbrTransactions = txSummary : (cbrTransactions stateIn) }
516497
foldM iterateTx state2 txs
517-
_ -> pure state
498+
_ -> pure state1
518499
if hh == stopHeaderHash then
519500
pure newState
520501
else do
@@ -523,7 +504,11 @@ getBlockRange genesisConfig start stop = do
523504
Nothing -> do
524505
pure newState
525506
Just nextHh' -> go nextHh' newState
526-
go startHeaderHash (CBlockRange [] [])
507+
backwards <- go startHeaderHash (CBlockRange [] [])
508+
pure $ CBlockRange
509+
{ cbrBlocks = reverse $ cbrBlocks backwards
510+
, cbrTransactions = reverse $ cbrTransactions backwards
511+
}
527512

528513

529514
-- | Get transaction summary from transaction id. Looks at both the database
@@ -979,9 +964,8 @@ getBlundOrThrow
979964
:: ExplorerMode ctx m
980965
=> HeaderHash
981966
-> m Blund
982-
getBlundOrThrow headerHash =
983-
getBlundFromHHCSLI headerHash >>=
984-
maybeThrow (Internal "Blund with hash cannot be found!")
967+
getBlundOrThrow hh =
968+
getBlundFromHHCSLI hh >>= maybeThrow (Internal "Blund with hash cannot be found!")
985969

986970

987971
-- | Deserialize Cardano or RSCoin address and convert it to Cardano address.

explorer/src/Pos/Explorer/Web/TestServer.hs

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Pos.Explorer.Web.ClientTypes (Byte, CAda (..), CAddress (..),
2424
CAddressSummary (..), CAddressType (..),
2525
CAddressesFilter (..), CBlockEntry (..),
2626
CBlockSummary (..), CGenesisAddressInfo (..),
27+
CBlockRange (..),
2728
CGenesisSummary (..), CHash (..), CTxBrief (..),
2829
CTxEntry (..), CTxId (..), CTxSummary (..), CUtxo (..),
2930
mkCCoin)
@@ -50,7 +51,7 @@ explorerHandlers :: Server ExplorerApi
5051
explorerHandlers =
5152
toServant (ExplorerApiRecord
5253
{ _totalAda = testTotalAda
53-
, _dumpBlockRange = undefined
54+
, _dumpBlockRange = testDumpBlockRange
5455
, _blocksPages = testBlocksPages
5556
, _blocksPagesTotal = testBlocksPagesTotal
5657
, _blocksSummary = testBlocksSummary
@@ -115,6 +116,15 @@ testBlocksPagesTotal
115116
-> Handler Integer
116117
testBlocksPagesTotal _ = pure 10
117118

119+
testDumpBlockRange :: CHash -> CHash -> Handler CBlockRange
120+
testDumpBlockRange start _ = do
121+
dummyBlock <- testBlocksSummary start
122+
dummyTx <- testTxsSummary cTxId
123+
pure $ CBlockRange
124+
{ cbrBlocks = [ dummyBlock ]
125+
, cbrTransactions = [ dummyTx ]
126+
}
127+
118128
testBlocksPages
119129
:: Maybe Word
120130
-> Maybe Word

explorer/src/documentation/Main.hs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ instance ToSchema C.CAddress
8484
instance ToParamSchema C.CAddress
8585
instance ToParamSchema C.EpochIndex
8686
instance ToSchema C.CTxSummary
87+
instance ToSchema C.CBlockRange
8788
instance ToSchema C.CTxEntry
8889
instance ToSchema C.CTxBrief
8990
instance ToSchema C.CUtxo

0 commit comments

Comments
 (0)