Skip to content

Commit

Permalink
Merge #2151
Browse files Browse the repository at this point in the history
2151: Provide slot number (flat slot) in network information r=KtorZ a=rvl

### Issue Number

ADP-418 / #2148

### Overview

Adds absolute slot numbers to API network information endpoint.

### Comments

Example output:
```json
{
    "network_tip": {
        "absolute_slot_number": 6044039,
        "epoch_number": 84,
        "slot_number": 125639
    },
    "node_tip": {
        "absolute_slot_number": 6044039,
        "height": {
            "quantity": 1815959,
            "unit": "block"
        },
        "epoch_number": 84,
        "slot_number": 125639
    },
    "sync_progress": {
        "status": "ready"
    },
    "next_epoch": {
        "epoch_start_time": "2020-09-21T20:20:16Z",
        "epoch_number": 85
    }
}
```


Co-authored-by: Rodney Lorrimar <[email protected]>
  • Loading branch information
iohk-bors[bot] and rvl authored Sep 22, 2020
2 parents 226c1c8 + d043f5c commit 4a8954e
Show file tree
Hide file tree
Showing 22 changed files with 1,293 additions and 1,026 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Data.Maybe
import Data.Time.Clock
( getCurrentTime )
import Test.Hspec
( SpecWith, describe, pendingWith, shouldBe )
( SpecWith, describe, pendingWith, shouldBe, shouldNotBe )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
Expand Down Expand Up @@ -67,6 +67,7 @@ spec = describe "COMMON_NETWORK" $ do
(epochStartTime <$> nextEpoch i) .> Just now
verify r
[ expectField (#syncProgress . #getApiT) (`shouldBe` Ready)
, expectField (#nodeTip . #absoluteSlotNumber . #getApiT) (`shouldNotBe` 0)
]

let Just currentEpochNum = getApiT . (view #epochNumber) <$> (i ^. #networkTip)
Expand All @@ -89,6 +90,8 @@ spec = describe "COMMON_NETWORK" $ do
getFromResponse (#nodeTip . #slotNumber . #getApiT) sync
let blockHeight =
getFromResponse (#nodeTip . #height) sync
let absSlot =
getFromResponse (#nodeTip . #absoluteSlotNumber) sync

res <- request @ApiByronWallet ctx
(Link.getWallet @'Byron w) Default Empty
Expand All @@ -97,6 +100,7 @@ spec = describe "COMMON_NETWORK" $ do
, expectField (#tip . #epochNumber . #getApiT) (`shouldBe` epochNum)
, expectField (#tip . #slotNumber . #getApiT) (`shouldBe` slotNum)
, expectField (#tip . #height) (`shouldBe` blockHeight)
, expectField (#tip . #absoluteSlotNumber) (`shouldBe` absSlot)
]

it "NETWORK_CLOCK - Can query network clock" $ \ctx -> do
Expand Down
6 changes: 5 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,12 +1627,14 @@ getNetworkInformation st nl = do
-- May be unavailible if the node is still syncing.
networkTipInfo :: UTCTime -> MaybeT IO (ApiNetworkTip, ApiEpochInfo)
networkTipInfo now = handle handlePastHorizonException $ do
networkTip <- lift . ti . toSlotId =<< MaybeT (ti $ ongoingSlotAt now)
networkTipSlot <- MaybeT (ti $ ongoingSlotAt now)
networkTip <- lift $ ti $ toSlotId networkTipSlot
let curEpoch = networkTip ^. #epochNumber
nextEpochStart <- lift $ ti $ endTimeOfEpoch curEpoch
let tip = ApiNetworkTip
(ApiT $ networkTip ^. #epochNumber)
(ApiT $ networkTip ^. #slotNumber)
(ApiT networkTipSlot)
let nextEpoch = ApiEpochInfo
(ApiT $ unsafeEpochSucc curEpoch)
(nextEpochStart)
Expand Down Expand Up @@ -1829,6 +1831,7 @@ mkApiTransaction ti txid ins outs ws (meta, timestamp) txMeta setTimeReference =
{ epochNumber = ApiT $ slotId ^. #epochNumber
, slotNumber = ApiT $ slotId ^. #slotNumber
, height = natural (meta ^. #blockHeight)
, absoluteSlotNumber = ApiT $ meta ^. #slotNo
}

toAddressAmount :: TxOut -> AddressAmount (ApiT Address, Proxy n)
Expand Down Expand Up @@ -1868,6 +1871,7 @@ mkApiBlockReference ti tip = do
{ epochNumber = ApiT $ slotId ^. #epochNumber
, slotNumber = ApiT $ slotId ^. #slotNumber
, height = natural $ tip ^. #blockHeight
, absoluteSlotNumber = ApiT $ slotNo tip
}

getWalletTip
Expand Down
8 changes: 8 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ import Cardano.Wallet.Primitive.Types
, ShowFmt (..)
, SlotInEpoch (..)
, SlotLength (..)
, SlotNo (..)
, StakePoolMetadata
, StartTime (..)
, TxIn (..)
Expand Down Expand Up @@ -614,11 +615,13 @@ data ApiBlockReference = ApiBlockReference
{ epochNumber :: !(ApiT EpochNo)
, slotNumber :: !(ApiT SlotInEpoch)
, height :: !(Quantity "block" Natural)
, absoluteSlotNumber :: !(ApiT SlotNo)
} deriving (Eq, Generic, Show)

data ApiNetworkTip = ApiNetworkTip
{ epochNumber :: !(ApiT EpochNo)
, slotNumber :: !(ApiT SlotInEpoch)
, absoluteSlotNumber :: !(ApiT SlotNo)
} deriving (Eq, Generic, Show)

data ApiNetworkInformation = ApiNetworkInformation
Expand Down Expand Up @@ -1223,6 +1226,11 @@ instance FromJSON (ApiT SlotInEpoch) where
instance ToJSON (ApiT SlotInEpoch) where
toJSON (ApiT (SlotInEpoch sn)) = toJSON sn

instance FromJSON (ApiT SlotNo) where
parseJSON = fmap (ApiT . SlotNo) . parseJSON
instance ToJSON (ApiT SlotNo) where
toJSON (ApiT (SlotNo sn)) = toJSON sn

instance FromJSON ApiNetworkTip where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON ApiNetworkTip where
Expand Down
72 changes: 41 additions & 31 deletions lib/core/test/data/Cardano/Wallet/Api/ApiBlockReference.json
Original file line number Diff line number Diff line change
@@ -1,85 +1,95 @@
{
"seed": -2366744487741549495,
"seed": -8884251501313165306,
"samples": [
{
"height": {
"quantity": 18267,
"quantity": 26609,
"unit": "block"
},
"epoch_number": 16540,
"slot_number": 15227
"epoch_number": 29339,
"absolute_slot_number": 8555698,
"slot_number": 3482
},
{
"height": {
"quantity": 7566,
"quantity": 9486,
"unit": "block"
},
"epoch_number": 408,
"slot_number": 28796
"epoch_number": 29485,
"absolute_slot_number": 13869043,
"slot_number": 29579
},
{
"height": {
"quantity": 92,
"quantity": 1596,
"unit": "block"
},
"epoch_number": 32700,
"slot_number": 14992
"epoch_number": 17216,
"absolute_slot_number": 4172286,
"slot_number": 1581
},
{
"height": {
"quantity": 152,
"quantity": 24900,
"unit": "block"
},
"epoch_number": 24914,
"slot_number": 1788
"epoch_number": 8508,
"absolute_slot_number": 8254543,
"slot_number": 2773
},
{
"height": {
"quantity": 11791,
"quantity": 15108,
"unit": "block"
},
"epoch_number": 6604,
"slot_number": 27814
"epoch_number": 15730,
"absolute_slot_number": 949302,
"slot_number": 19528
},
{
"height": {
"quantity": 28780,
"quantity": 3285,
"unit": "block"
},
"epoch_number": 28310,
"slot_number": 27847
"epoch_number": 403,
"absolute_slot_number": 11555203,
"slot_number": 1350
},
{
"height": {
"quantity": 20717,
"quantity": 11618,
"unit": "block"
},
"epoch_number": 19953,
"slot_number": 7805
"epoch_number": 28471,
"absolute_slot_number": 11385630,
"slot_number": 30197
},
{
"height": {
"quantity": 6974,
"quantity": 19871,
"unit": "block"
},
"epoch_number": 29015,
"slot_number": 8064
"epoch_number": 32375,
"absolute_slot_number": 8383769,
"slot_number": 6205
},
{
"height": {
"quantity": 13051,
"quantity": 28463,
"unit": "block"
},
"epoch_number": 30163,
"slot_number": 24402
"epoch_number": 712,
"absolute_slot_number": 3302675,
"slot_number": 17505
},
{
"height": {
"quantity": 23300,
"quantity": 16629,
"unit": "block"
},
"epoch_number": 12611,
"slot_number": 8724
"epoch_number": 2454,
"absolute_slot_number": 15142158,
"slot_number": 1387
}
]
}
Loading

0 comments on commit 4a8954e

Please sign in to comment.