Skip to content

Commit 69d18bb

Browse files
committed
Fix script procedure
1 parent 143f370 commit 69d18bb

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

cardano-db-sync/src/Cardano/DbSync/Fix/PlutusDataBytes.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Control.Monad.Trans.Control (MonadBaseControl)
3737
import Control.Monad.Trans.Reader (ReaderT)
3838
import Data.ByteString (ByteString)
3939
import qualified Data.ByteString.Short as SBS
40+
import Data.Either.Extra (mapLeft)
4041
import Data.Foldable (toList)
4142
import Data.Int (Int64)
4243
import Data.Map (Map)
@@ -108,7 +109,7 @@ getWrongPlutusData tracer = do
108109
(fmap f . DB_V_13_0.querydatumInfo . entityKey)
109110
(DB_V_13_0.datumHash . entityVal)
110111
(Just . getDatumBytes)
111-
(hashPlutusData . getDatumBytes)
112+
(mapLeft Just . hashPlutusData . getDatumBytes)
112113
redeemerDataList <-
113114
findWrongPlutusData
114115
tracer
@@ -118,7 +119,7 @@ getWrongPlutusData tracer = do
118119
(fmap f . DB_V_13_0.queryRedeemerDataInfo . entityKey)
119120
(DB_V_13_0.redeemerDataHash . entityVal)
120121
(Just . getRedeemerDataBytes)
121-
(hashPlutusData . getRedeemerDataBytes)
122+
(mapLeft Just . hashPlutusData . getRedeemerDataBytes)
122123
pure $ FixData datumList redeemerDataList
123124
where
124125
f queryRes = do
@@ -144,7 +145,7 @@ findWrongPlutusData ::
144145
(a -> m (Maybe CardanoPoint)) -> -- get previous block point
145146
(a -> ByteString) -> -- get the hash
146147
(a -> Maybe ByteString) -> -- get the stored bytes
147-
(a -> Either String ByteString) -> -- hash the stored bytes
148+
(a -> Either (Maybe String) ByteString) -> -- hash the stored bytes
148149
m [FixPlutusInfo]
149150
findWrongPlutusData tracer tableName qCount qPage qGetInfo getHash getBytes hashBytes = do
150151
liftIO $
@@ -197,7 +198,8 @@ findWrongPlutusData tracer tableName qCount qPage qGetInfo getHash getBytes hash
197198

198199
checkValidBytes :: a -> m Bool
199200
checkValidBytes a = case hashBytes a of
200-
Left msg -> do
201+
Left Nothing -> pure True
202+
Left (Just msg) -> do
201203
liftIO $
202204
logWarning tracer $
203205
Text.concat ["Invalid Binary Data for hash ", textShow actualHash, ": ", Text.pack msg]

cardano-db-sync/src/Cardano/DbSync/Fix/PlutusScripts.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,18 @@ findWrongPlutusScripts tracer =
109109
prevPoint <- convertToPoint (SlotNo prevSlotNo) prevBlockHsh
110110
Just prevPoint
111111

112+
hashPlutusScript :: DB_V_13_0.Script -> Either (Maybe String) ByteString
112113
hashPlutusScript dbScript = do
113-
bytes <- maybeToEither "No bytes found for plutus script" id $ DB_V_13_0.scriptBytes dbScript
114-
let script :: AlonzoScript StandardAlonzo = PlutusScript (AlonzoPlutusV1 (Plutus $ PlutusBinary $ SBS.toShort bytes))
115-
let hsh :: Ledger.ScriptHash StandardCrypto = Ledger.hashScript @StandardAlonzo script
116-
Right $ Generic.unScriptHash hsh
114+
bytes <- maybeToEither (Just "No bytes found for plutus script") id $ DB_V_13_0.scriptBytes dbScript
115+
case DB_V_13_0.scriptType dbScript of
116+
PlutusV1 -> do
117+
-- The bug only affected Alonzo script
118+
let script :: AlonzoScript StandardAlonzo = PlutusScript (AlonzoPlutusV1 (Plutus $ PlutusBinary $ SBS.toShort bytes))
119+
let hsh :: Ledger.ScriptHash StandardCrypto = Ledger.hashScript @StandardAlonzo script
120+
Right $ Generic.unScriptHash hsh
121+
PlutusV2 -> Left Nothing
122+
PlutusV3 -> Left Nothing
123+
_ -> Left $ Just "Non plutus script found where it shouldn't."
117124

118125
fixPlutusScripts :: MonadIO m => Trace IO Text -> CardanoBlock -> FixPlutusScripts -> ReaderT SqlBackend m ()
119126
fixPlutusScripts tracer cblk fpss = do

0 commit comments

Comments
 (0)