File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed
cardano-db/src/Cardano/Db
cardano-db-sync/src/Cardano/DbSync/Plugin/Default/Byron Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -102,7 +102,17 @@ insertABlock
102
102
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m ) ()
103
103
insertABlock tracer blk tip = do
104
104
meta <- liftLookupFail " insertABlock" DB. queryMeta
105
- pbid <- liftLookupFail " insertABlock" $ DB. queryBlockId (Byron. unHeaderHash $ Byron. blockPreviousHash blk)
105
+
106
+ (pbid, mBlock) <- liftLookupFail " insertABOBBoundary" $
107
+ DB. queryBlockIdAndNo (Byron. unHeaderHash $ Byron. blockPreviousHash blk)
108
+ case mBlock of
109
+ Nothing -> pure () -- Previous was an EBB
110
+ Just blockNo ->
111
+ if blockNo < Byron. blockNumber blk
112
+ then pure ()
113
+ else do
114
+ liftIO . logInfo tracer $ " Rollback orphan block number " <> textShow blockNo
115
+ lift . void $ DB. deleteCascadeBlockNo blockNo
106
116
107
117
let slotsPerEpoch = 10 * DB. metaProtocolConst meta
108
118
Original file line number Diff line number Diff line change 1
1
module Cardano.Db.Delete
2
2
( deleteCascadeBlock
3
+ , deleteCascadeBlockNo
3
4
, deleteCascadeSlotNo
4
5
) where
5
6
@@ -23,6 +24,14 @@ deleteCascadeBlock block = do
23
24
mapM_ (deleteCascade . entityKey) keys
24
25
pure $ not (null keys)
25
26
27
+ -- | Delete a block if it exists. Returns 'True' if it did exist and has been
28
+ -- deleted and 'False' if it did not exist.
29
+ deleteCascadeBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Bool
30
+ deleteCascadeBlockNo blockNo = do
31
+ keys <- selectList [ BlockBlockNo ==. Just blockNo ] []
32
+ mapM_ (deleteCascade . entityKey) keys
33
+ pure $ not (null keys)
34
+
26
35
-- | Delete a block if it exists. Returns 'True' if it did exist and has been
27
36
-- deleted and 'False' if it did not exist.
28
37
deleteCascadeSlotNo :: MonadIO m => Word64 -> ReaderT SqlBackend m Bool
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ module Cardano.Db.Query
9
9
, queryBlockCount
10
10
, queryBlockHeight
11
11
, queryBlockId
12
+ , queryBlockIdAndNo
12
13
, queryBlockNo
13
14
, queryMainBlock
14
15
, queryBlockTxCount
@@ -115,6 +116,17 @@ queryBlockId hash = do
115
116
pure $ blk ^. BlockId
116
117
pure $ maybeToEither (DbLookupBlockHash hash) unValue (listToMaybe res)
117
118
119
+ -- | Get the 'BlockId' and the 'BlockNo' associated with the given hash.
120
+ -- This is is usually used with the prevHash for a block so that the back reference
121
+ -- for the block can be obtained. The BlockNo is also returned so any orphaned blocks
122
+ -- can be rolled back.
123
+ queryBlockIdAndNo :: MonadIO m => ByteString -> ReaderT SqlBackend m (Either LookupFail (BlockId , Maybe Word64 ))
124
+ queryBlockIdAndNo hash = do
125
+ res <- select . from $ \ blk -> do
126
+ where_ (blk ^. BlockHash ==. val hash)
127
+ pure $ (blk ^. BlockId , blk ^. BlockBlockNo )
128
+ pure $ maybeToEither (DbLookupBlockHash hash) unValue2 (listToMaybe res)
129
+
118
130
queryBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m (Maybe Block )
119
131
queryBlockNo blkNo = do
120
132
res <- select . from $ \ blk -> do
You can’t perform that action at this time.
0 commit comments