Skip to content

Commit 320423c

Browse files
committed
fix dbInt65
1 parent c63dbb3 commit 320423c

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

cardano-db/src/Cardano/Db/Statement/Base.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Cardano.Ledger.BaseTypes (SlotNo (..))
1515
import Cardano.Prelude (ByteString, Int64, MonadError (..), MonadIO (..), Proxy (..), Word64, textShow, void)
1616
import Data.Functor.Contravariant (Contravariant (..), (>$<))
1717
import Data.List (partition)
18-
import Data.Maybe (isJust)
18+
import Data.Maybe (isJust, fromMaybe)
1919
import qualified Data.Text as Text
2020
import qualified Data.Text.Encoding as TextEnc
2121
import Data.Time (UTCTime)
@@ -311,8 +311,8 @@ queryMinBlockStmt =
311311

312312
decoder = HsqlD.rowMaybe $ do
313313
blockId <- Id.idDecoder Id.BlockId
314-
blockNo <- HsqlD.column (HsqlD.nonNullable $ fromIntegral <$> HsqlD.int8)
315-
pure (blockId, blockNo)
314+
blockNo <- HsqlD.column (HsqlD.nullable $ fromIntegral <$> HsqlD.int8)
315+
pure (blockId, fromMaybe 0 blockNo)
316316

317317
queryMinBlock :: MonadIO m => DbAction m (Maybe (Id.BlockId, Word64))
318318
queryMinBlock =
@@ -1317,16 +1317,15 @@ queryScriptWithId hash =
13171317
--------------------------------------------------------------------------------
13181318
-- SlotLeader
13191319
--------------------------------------------------------------------------------
1320-
insertSlotLeaderStmt :: HsqlStmt.Statement SCB.SlotLeader (Entity SCB.SlotLeader)
1320+
insertSlotLeaderStmt :: HsqlStmt.Statement SCB.SlotLeader Id.SlotLeaderId
13211321
insertSlotLeaderStmt =
13221322
insert
13231323
SCB.slotLeaderEncoder
1324-
(WithResult $ HsqlD.singleRow SCB.entitySlotLeaderDecoder)
1324+
(WithResult $ Id.idDecoder Id.SlotLeaderId)
13251325

13261326
insertSlotLeader :: MonadIO m => SCB.SlotLeader -> DbAction m Id.SlotLeaderId
13271327
insertSlotLeader slotLeader = do
1328-
entity <- runDbSession (mkCallInfo "insertSlotLeader") $ HsqlSes.statement slotLeader insertSlotLeaderStmt
1329-
pure $ entityKey entity
1328+
runDbSession (mkCallInfo "insertSlotLeader") $ HsqlSes.statement slotLeader insertSlotLeaderStmt
13301329

13311330
--------------------------------------------------------------------------------
13321331
-- TxCbor

cardano-db/src/Cardano/Db/Statement/GovernanceAndVoting.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,11 @@ queryDrepHashSpecialStmt targetValue =
308308
table = tableName (Proxy @a)
309309
rawCol = validateColumn @a "raw"
310310
viewCol = validateColumn @a "view"
311-
idCol = validateColumn @a "id"
312311

313312
sql =
314313
TextEnc.encodeUtf8 $
315314
Text.concat
316-
[ "SELECT "
317-
, idCol
315+
[ "SELECT id"
318316
, " FROM "
319317
, table
320318
, " WHERE "
@@ -417,7 +415,6 @@ setGovActionStateNullStmt columnName =
417415
, " IS NOT NULL AND "
418416
, columnName
419417
, " > $1"
420-
, " RETURNING xmax != 0 AS changed"
421418
]
422419
encoder = HsqlE.param (HsqlE.nonNullable HsqlE.int8)
423420
decoder = HsqlD.rowsAffected

cardano-db/src/Cardano/Db/Statement/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
{-# LANGUAGE TypeFamilyDependencies #-}
1010
{-# LANGUAGE TypeOperators #-}
1111
{-# LANGUAGE UndecidableInstances #-}
12+
{-# LANGUAGE OverloadedStrings #-}
1213

1314
module Cardano.Db.Statement.Types where
1415

@@ -130,7 +131,7 @@ instance GRecordFieldNames (K1 i c) where
130131
-- | Validate a column name against the list of columns in the table.
131132
validateColumn :: forall a. (DbInfo a) => Text -> Text
132133
validateColumn colName =
133-
let cols = NE.toList $ columnNames (Proxy @a)
134+
let cols = "id" : NE.toList (columnNames (Proxy @a))
134135
in if colName `elem` cols
135136
then colName
136137
else

cardano-db/src/Cardano/Db/Types.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,18 @@ dbInt65Encoder = fromDbInt65 >$< HsqlE.int8
221221

222222
-- Helper functions to pack/unpack the sign and value
223223
toDbInt65 :: Int64 -> DbInt65
224-
toDbInt65 n =
225-
DbInt65 $
226-
if n >= 0
227-
then fromIntegral n
228-
else setBit (fromIntegral (abs n)) 63 -- Set sign bit for negative
224+
toDbInt65 n
225+
| n >= 0 = DbInt65 (fromIntegral n)
226+
| n == minBound = DbInt65 (setBit 0 63) -- Special: magnitude 0 + sign bit = minBound
227+
| otherwise = DbInt65 (setBit (fromIntegral (abs n)) 63)
229228

230229
fromDbInt65 :: DbInt65 -> Int64
231230
fromDbInt65 (DbInt65 w) =
232231
if testBit w 63
233-
then negate $ fromIntegral (clearBit w 63) -- Clear sign bit for value
232+
then let magnitude = clearBit w 63
233+
in if magnitude == 0
234+
then minBound -- Special: magnitude 0 + sign bit = minBound
235+
else negate (fromIntegral magnitude)
234236
else fromIntegral w
235237

236238
-- Newtype wrapper around Word64 so we can hand define a PersistentField instance.

cardano-db/test/Test/Property/Cardano/Db/Types.hs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,17 @@ prop_AssetFingerprint =
105105
prop_roundtrip_DbInt65 :: Property
106106
prop_roundtrip_DbInt65 =
107107
H.withTests 5000 . H.property $ do
108-
-- Generate both positive and negative values
109-
posInt64 <- H.forAll $ Gen.int64 (Range.linear 0 maxBound)
110-
negInt64 <- H.forAll $ Gen.int64 (Range.linear minBound (-1))
111-
112-
let i65pos = toDbInt65 posInt64
113-
let i65neg = toDbInt65 negInt64
114-
115-
-- Test roundtrip conversion
116-
runDbInt65Roundtrip i65pos === i65pos
117-
runDbInt65Roundtrip i65neg === i65neg
108+
i64 <- H.forAll $ Gen.int64 (Range.linearFrom 0 minBound maxBound)
109+
let i65 = toDbInt65 i64
110+
fromDbInt65 i65 === i64
111+
112+
prop_DbInt65_edge_cases :: Property
113+
prop_DbInt65_edge_cases = H.property $ do
114+
fromDbInt65 (toDbInt65 minBound) === minBound
115+
fromDbInt65 (toDbInt65 maxBound) === maxBound
116+
fromDbInt65 (toDbInt65 0) === 0
117+
fromDbInt65 (toDbInt65 (-1)) === (-1)
118+
fromDbInt65 (toDbInt65 1) === 1
118119

119120
-- Test DbLovelace roundtrip conversion
120121
prop_roundtrip_DbLovelace :: Property

0 commit comments

Comments
 (0)