Skip to content

Commit

Permalink
Merge #1890
Browse files Browse the repository at this point in the history
1890: Move slotting to separate module r=Anviking a=Anviking

# Issue Number

ADP-356 #1868, #1869 

# Overview


- [x] I have moved the existing slotting logic (`flatSlot`, `slotAt`, etc.) to a new module `Cardano.Wallet.Primitive.Slotting`
    - It is easier to keep track of remaining uses of static slotting this way
- [x] I started adding a ouroboros-consensus wrapper on top (currently unused)
    - [x] Added property `runQuery epochOf singleEraInterpreter == epochNumber . fromFlatSlot`


# Comments

- I left tests for slotting arithmetic in TypesSpec because the boundary between `Range` tests and slotting tests seemed unclear.
- Next step: Wrap all existing slotting logic using `Qry`/`Interpreter`.
- Could split into two PRs if you want

<!-- Additional comments or screenshots to attach if any -->

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Assign the PR to a corresponding milestone
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Johannes Lund <[email protected]>
  • Loading branch information
iohk-bors[bot] and Anviking authored Jul 14, 2020
2 parents 52966c3 + 1be1ac1 commit 42872e6
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 239 deletions.
4 changes: 2 additions & 2 deletions lib/byron/bench/Restore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Random
( RndState, mkRndState )
import Cardano.Wallet.Primitive.Model
( currentTip, totalUTxO )
import Cardano.Wallet.Primitive.Slotting
( slotAt, slotParams )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..), mkSyncTolerance, syncProgressRelativeToTime )
import Cardano.Wallet.Primitive.Types
Expand All @@ -111,8 +113,6 @@ import Cardano.Wallet.Primitive.Types
, WalletName (..)
, computeUtxoStatistics
, log10
, slotAt
, slotParams
)
import Cardano.Wallet.Unsafe
( unsafeMkMnemonic, unsafeRunExceptT )
Expand Down
6 changes: 4 additions & 2 deletions lib/byron/src/Cardano/Wallet/Byron/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( decodeLegacyAddress )
import Cardano.Wallet.Primitive.Slotting
( flatSlot, fromFlatSlot )
import Cardano.Wallet.Unsafe
( unsafeDeserialiseCbor, unsafeFromHex )
import Data.ByteString
Expand Down Expand Up @@ -306,7 +308,7 @@ toPoint genesisH epLength (W.BlockHeader sid _ h _)

toSlotInEpoch :: W.EpochLength -> W.SlotId -> SlotNo
toSlotInEpoch epLength =
SlotNo . W.flatSlot epLength
SlotNo . flatSlot epLength

-- | SealedTx are the result of rightfully constructed byron transactions so, it
-- is relatively safe to unserialize them from CBOR.
Expand Down Expand Up @@ -388,7 +390,7 @@ fromChainHash genesisHash = \case

fromSlotNo :: W.EpochLength -> SlotNo -> W.SlotId
fromSlotNo epLength (SlotNo sl) =
W.fromFlatSlot epLength sl
fromFlatSlot epLength sl

-- FIXME unsafe conversion (Word64 -> Word32)
fromBlockNo :: BlockNo -> Quantity "block" Word32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ import Cardano.Wallet.Primitive.AddressDerivation
)
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( generateKeyFromHardwareLedger )
import Cardano.Wallet.Primitive.Slotting
( fromFlatSlot )
import Cardano.Wallet.Primitive.Types
( EpochLength (..), Hash (..), SealedTx (..), SlotId (..), fromFlatSlot )
( EpochLength (..), Hash (..), SealedTx (..), SlotId (..) )
import Cardano.Wallet.Unsafe
( unsafeFromHex, unsafeMkSomeMnemonicFromEntropy )
import Control.Monad
Expand Down
13 changes: 7 additions & 6 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ import Cardano.Wallet.Api.Types
, ApiAddress
, ApiByronWallet
, ApiCoinSelection
, ApiEpochInfo (..)
, ApiEpochInfo (ApiEpochInfo)
, ApiFee
, ApiNetworkInformation
, ApiNetworkParameters (..)
Expand Down Expand Up @@ -190,6 +190,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Jormungandr
( generateKeyFromSeed )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.Slotting
( SlotParameters (..), epochStartTime )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
Expand All @@ -202,7 +204,6 @@ import Cardano.Wallet.Primitive.Types
, HistogramBar (..)
, PoolId (..)
, SlotLength (..)
, SlotParameters (..)
, SortOrder (..)
, TxIn (..)
, TxOut (..)
Expand Down Expand Up @@ -1715,10 +1716,10 @@ mkEpochInfo
-> SlotParameters
-- ^ Blockchain slot parameters
-> ApiEpochInfo
mkEpochInfo epochNo sp = ApiEpochInfo
{ epochNumber = ApiT epochNo
, epochStartTime = W.epochStartTime sp epochNo
}
mkEpochInfo epochNo sp =
ApiEpochInfo
(ApiT epochNo)
(epochStartTime sp epochNo)

-- | Wallet not delegating and not about to join any stake pool.
notDelegating
Expand Down
3 changes: 3 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ library
Cardano.Wallet.Primitive.AddressDerivation.Jormungandr
Cardano.Wallet.Primitive.AddressDerivation.Shelley
Cardano.Wallet.Primitive.AddressDiscovery
Cardano.Wallet.Primitive.Slotting
Cardano.Wallet.Primitive.AddressDiscovery.Random
Cardano.Wallet.Primitive.AddressDiscovery.Sequential
Cardano.Wallet.Primitive.CoinSelection
Expand Down Expand Up @@ -191,6 +192,7 @@ test-suite unit
, cardano-wallet-core
, cardano-wallet-launcher
, cardano-wallet-test-utils
, cardano-slotting
, cborg
, connection
, containers
Expand Down Expand Up @@ -287,6 +289,7 @@ test-suite unit
Cardano.Wallet.Primitive.CoinSelectionSpec
Cardano.Wallet.Primitive.FeeSpec
Cardano.Wallet.Primitive.ModelSpec
Cardano.Wallet.Primitive.SlottingSpec
Cardano.Wallet.Primitive.SyncProgressSpec
Cardano.Wallet.Primitive.TypesSpec
Cardano.Wallet.RegistrySpec
Expand Down
6 changes: 2 additions & 4 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ import Cardano.Wallet.Primitive.Model
, initWallet
, updateState
)
import Cardano.Wallet.Primitive.Slotting
( SlotParameters (..), slotParams, slotRangeFromTimeRange, slotStartTime )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress, SyncTolerance (..), syncProgressRelativeToTime )
import Cardano.Wallet.Primitive.Types
Expand All @@ -275,7 +277,6 @@ import Cardano.Wallet.Primitive.Types
, Range (..)
, SealedTx
, SlotId (..)
, SlotParameters (..)
, SortOrder (..)
, TransactionInfo (..)
, Tx
Expand All @@ -295,9 +296,6 @@ import Cardano.Wallet.Primitive.Types
, dlgCertPoolId
, fromTransactionInfo
, log10
, slotParams
, slotRangeFromTimeRange
, slotStartTime
, wholeRange
)
import Cardano.Wallet.Transaction
Expand Down
5 changes: 2 additions & 3 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ import Cardano.Wallet.Primitive.Types
, UnsignedTx (..)
, WalletId (..)
, WalletMetadata (..)
, slotAt
, slotMinBound
)
import Cardano.Wallet.Registry
( HasWorkerCtx (..)
Expand Down Expand Up @@ -385,6 +383,7 @@ import qualified Cardano.Wallet as W
import qualified Cardano.Wallet.Network as NW
import qualified Cardano.Wallet.Primitive.AddressDerivation.Byron as Byron
import qualified Cardano.Wallet.Primitive.AddressDerivation.Icarus as Icarus
import qualified Cardano.Wallet.Primitive.Slotting as W
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Registry as Registry
import qualified Data.Aeson as Aeson
Expand Down Expand Up @@ -1418,7 +1417,7 @@ getNetworkInformation
getNetworkInformation (_block0, np, st) nl = do
now <- liftIO getCurrentTime
nodeTip <- liftHandler (NW.currentNodeTip nl)
let ntrkTip = fromMaybe slotMinBound (slotAt sp now)
let ntrkTip = fromMaybe W.slotMinBound (W.slotAt sp now)
let nextEpochNo = unsafeEpochSucc (ntrkTip ^. #epochNumber)
pure $ ApiNetworkInformation
{ syncProgress =
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/DB/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import Prelude

import Cardano.Wallet.Primitive.Model
( Wallet, blockchainParameters, currentTip, utxo )
import Cardano.Wallet.Primitive.Slotting
( slotParams, slotStartTime )
import Cardano.Wallet.Primitive.Types
( BlockHeader (blockHeight, slotId)
, Coin (..)
Expand All @@ -91,8 +93,6 @@ import Cardano.Wallet.Primitive.Types
, WalletMetadata (..)
, dlgCertPoolId
, isWithinRange
, slotParams
, slotStartTime
)
import Control.Monad
( when )
Expand Down
8 changes: 5 additions & 3 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ import Cardano.Wallet.Primitive.AddressDerivation
, SoftDerivation (..)
, WalletKey (..)
)
import Cardano.Wallet.Primitive.Slotting
( SlotParameters, slotParams, slotStartTime )
import Control.Concurrent.MVar
( modifyMVar, modifyMVar_, newMVar, readMVar )
import Control.Exception
Expand Down Expand Up @@ -1038,7 +1040,7 @@ mkTxMetaEntity wid txid meta = TxMeta
-- note: TxIn records must already be sorted by order
-- and TxOut records must already be sorted by index
txHistoryFromEntity
:: W.SlotParameters
:: SlotParameters
-> W.BlockHeader
-> [TxMeta]
-> [(TxIn, Maybe TxOut)]
Expand All @@ -1063,7 +1065,7 @@ txHistoryFromEntity sp tip metas ins outs ws =
, W.txInfoDepth =
Quantity $ fromIntegral $ if tipH > txH then tipH - txH else 0
, W.txInfoTime =
W.slotStartTime sp (meta ^. #slotId)
slotStartTime sp (meta ^. #slotId)
}
where
txH = getQuantity (meta ^. #blockHeight)
Expand Down Expand Up @@ -1330,7 +1332,7 @@ selectTxHistory wid minWithdrawal order conditions = do

let wal = checkpointFromEntity cp [] ()
let tip = W.currentTip wal
let slp = W.slotParams $ W.blockchainParameters wal
let slp = slotParams $ W.blockchainParameters wal

return $ txHistoryFromEntity slp tip metas ins outs ws
where
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import Cardano.Wallet.Primitive.AddressDerivation
( AccountingStyle (..), Passphrase (..), PassphraseScheme (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap (..), getAddressPoolGap, mkAddressPoolGap )
import Cardano.Wallet.Primitive.Slotting
( flatSlot, fromFlatSlot )
import Cardano.Wallet.Primitive.Types
( Address (..)
, ChimericAccount (..)
Expand All @@ -43,8 +45,6 @@ import Cardano.Wallet.Primitive.Types
, StakePoolTicker
, TxStatus (..)
, WalletId (..)
, flatSlot
, fromFlatSlot
, isValidCoin
, unsafeEpochNo
)
Expand Down
Loading

0 comments on commit 42872e6

Please sign in to comment.