Skip to content

Commit

Permalink
Merge #1860
Browse files Browse the repository at this point in the history
1860: add default migration for 'desired_pool_number' in protocol params table r=KtorZ a=KtorZ

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->

N/A

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- b798abc
  📍 **add default migration for 'desired_pool_number' in protocol params table**
  Defaulting to 50 without which the wallet integrity check fails (genesis parameters have to be identical to the network's parameters).




# Comments

<!-- 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: KtorZ <matthias.benkort@gmail.com>
  • Loading branch information
iohk-bors[bot] and KtorZ committed Jul 6, 2020
2 parents 1736316 + b798abc commit e33c140
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/byron/bench/Restore.hs
Expand Up @@ -426,6 +426,7 @@ withBenchDBLayer tr action =
where
migrationDefaultValues = Sqlite.DefaultFieldValues
{ Sqlite.defaultActiveSlotCoefficient = 1
, Sqlite.defaultDesiredNumberOfPool = 0
}

-- This tweaks the DB support the AnyAddressState.
Expand Down
9 changes: 8 additions & 1 deletion lib/byron/src/Cardano/Wallet/Byron.hs
Expand Up @@ -103,6 +103,7 @@ import Cardano.Wallet.Primitive.Types
, ChimericAccount
, GenesisParameters (..)
, NetworkParameters (..)
, ProtocolParameters (..)
, SyncTolerance
, WalletId
)
Expand Down Expand Up @@ -271,7 +272,13 @@ serveWallet
let params = (block0, np, sTolerance)
db <- Sqlite.newDBFactory
walletDbTracer
(DefaultFieldValues $ getActiveSlotCoefficient gp)
(DefaultFieldValues
{ defaultActiveSlotCoefficient =
getActiveSlotCoefficient gp
, defaultDesiredNumberOfPool =
desiredNumberOfStakePools (protocolParameters np)
}
)
databaseDir
Server.newApiLayer walletEngineTracer params nl' tl db
Server.idleWorker
Expand Down
35 changes: 32 additions & 3 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -141,7 +141,7 @@ import Data.Text.Class
import Data.Typeable
( Typeable )
import Data.Word
( Word32, Word64 )
( Word16, Word32, Word64 )
import Database.Persist.Class
( toPersistValue )
import Database.Persist.Sql
Expand Down Expand Up @@ -328,6 +328,8 @@ migrateManually tr defaultFieldValues =

addActiveSlotCoefficientIfMissing conn

addDesiredPoolNumberIfMissing conn

-- FIXME
-- Temporary migration to fix Daedalus flight wallets. This should
-- really be removed as soon as we have a fix for the cardano-sl:wallet
Expand Down Expand Up @@ -434,12 +436,37 @@ migrateManually tr defaultFieldValues =
Sqlite.finalize addColumn
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded activeSlotCoeff

where
activeSlotCoeff = DBField CheckpointActiveSlotCoeff
value = toText
$ W.unActiveSlotCoefficient
$ defaultActiveSlotCoefficient defaultFieldValues

-- | Adds an 'desired_pool_number' column to the 'protocol_parameters'
-- table if it is missing.
--
addDesiredPoolNumberIfMissing :: Sqlite.Connection -> IO ()
addDesiredPoolNumberIfMissing conn = do
isFieldPresent conn desiredPoolNumber >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded desiredPoolNumber
ColumnMissing -> do
traceWith tr $ MsgManualMigrationNeeded desiredPoolNumber value
addColumn <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName desiredPoolNumber
, "ADD COLUMN", fieldName desiredPoolNumber
, fieldType desiredPoolNumber, "NOT NULL", "DEFAULT", value
, ";"
]
_ <- Sqlite.step addColumn
Sqlite.finalize addColumn
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded desiredPoolNumber
where
desiredPoolNumber = DBField ProtocolParametersDesiredNumberOfPools
value = T.pack $ show $ defaultDesiredNumberOfPool defaultFieldValues

-- | This table became @protocol_parameters@.
removeOldTxParametersTable :: Sqlite.Connection -> IO ()
removeOldTxParametersTable conn = do
Expand Down Expand Up @@ -467,8 +494,10 @@ migrateManually tr defaultFieldValues =
-- | A set of default field values that can be consulted when performing a
-- database migration.
--
newtype DefaultFieldValues = DefaultFieldValues
{ defaultActiveSlotCoefficient :: W.ActiveSlotCoefficient }
data DefaultFieldValues = DefaultFieldValues
{ defaultActiveSlotCoefficient :: W.ActiveSlotCoefficient
, defaultDesiredNumberOfPool :: Word16
}

-- | Sets up a connection to the SQLite database.
--
Expand Down
5 changes: 4 additions & 1 deletion lib/core/test/bench/db/Main.hs
Expand Up @@ -342,7 +342,10 @@ setupDB = do

defaultFieldValues :: DefaultFieldValues
defaultFieldValues = DefaultFieldValues
{ defaultActiveSlotCoefficient = ActiveSlotCoefficient 1.0 }
{ defaultActiveSlotCoefficient = ActiveSlotCoefficient 1.0
, defaultDesiredNumberOfPool = 50
-- NOTE value in the genesis when at the time this migration was needed.
}

cleanupDB :: (FilePath, SqliteContext, DBLayerBench) -> IO ()
cleanupDB (db, ctx, _) = do
Expand Down
4 changes: 3 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Expand Up @@ -727,7 +727,9 @@ temporaryDBFile = emptySystemTempFile "cardano-wallet-SqliteFileMode"

defaultFieldValues :: DefaultFieldValues
defaultFieldValues = DefaultFieldValues
{ defaultActiveSlotCoefficient = ActiveSlotCoefficient 1.0 }
{ defaultActiveSlotCoefficient = ActiveSlotCoefficient 1.0
, defaultDesiredNumberOfPool = 0
}

newDBLayer'
:: PersistState s
Expand Down
9 changes: 7 additions & 2 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr.hs
Expand Up @@ -126,6 +126,7 @@ import Cardano.Wallet.Primitive.Types
, ChimericAccount
, GenesisParameters (..)
, NetworkParameters (..)
, ProtocolParameters (..)
, SyncTolerance
, WalletId
)
Expand Down Expand Up @@ -282,8 +283,12 @@ serveWallet Tracers{..} sTolerance databaseDir hostPref listen backend beforeMai
db <- Sqlite.newDBFactory
walletDbTracer
(DefaultFieldValues
$ getActiveSlotCoefficient
$ genesisParameters np)
{ defaultActiveSlotCoefficient =
getActiveSlotCoefficient (genesisParameters np)
, defaultDesiredNumberOfPool =
desiredNumberOfStakePools (protocolParameters np)
}
)
databaseDir
Server.newApiLayer
walletEngineTracer (toWLBlock block0, np, sTolerance) nl' tl db
Expand Down
9 changes: 8 additions & 1 deletion lib/shelley/src/Cardano/Wallet/Shelley.hs
Expand Up @@ -97,6 +97,7 @@ import Cardano.Wallet.Primitive.Types
, ChimericAccount
, GenesisParameters (..)
, NetworkParameters (..)
, ProtocolParameters (..)
, SyncTolerance
, WalletId
)
Expand Down Expand Up @@ -322,7 +323,13 @@ serveWallet
let params = (block0, np, sTolerance)
db <- Sqlite.newDBFactory
walletDbTracer
(DefaultFieldValues $ getActiveSlotCoefficient gp)
(DefaultFieldValues
{ defaultActiveSlotCoefficient =
getActiveSlotCoefficient gp
, defaultDesiredNumberOfPool =
desiredNumberOfStakePools (protocolParameters np)
}
)
databaseDir
Server.newApiLayer walletEngineTracer params nl' tl db coworker
where
Expand Down

0 comments on commit e33c140

Please sign in to comment.