Skip to content

Commit dbe5850

Browse files
authored
Merge pull request #3 from input-output-hk/feat/plustan-04
feat: add plustan-04
2 parents edec909 + d72eb4c commit dbe5850

File tree

5 files changed

+93
-9
lines changed

5 files changed

+93
-9
lines changed

src/Stan/Inspection/AntiPattern.hs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23

34
{- |
45
Copyright: (c) 2020 Kowainik
@@ -50,6 +51,7 @@ module Stan.Inspection.AntiPattern
5051
, plustan01
5152
, plustan02
5253
, plustan03
54+
, plustan04
5355
-- * All inspections
5456
, antiPatternInspectionsMap
5557
) where
@@ -60,7 +62,7 @@ import Relude.Extra.Tuple (fmapToFst)
6062
import Stan.Core.Id (Id (..))
6163
import Stan.Inspection (Inspection (..), InspectionAnalysis (..), InspectionsMap, categoryL,
6264
descriptionL, severityL, solutionL)
63-
import Stan.NameMeta (NameMeta (..), baseNameFrom, mkBaseFoldableMeta, mkBaseOldListMeta,
65+
import Stan.NameMeta (ghcPrimNameFrom, NameMeta (..), baseNameFrom, mkBaseFoldableMeta, mkBaseOldListMeta,
6466
primTypeMeta, textNameFrom, unorderedNameFrom, _nameFrom, plutusTxNameFrom)
6567
import Stan.Pattern.Ast (Literal (..), PatternAst (..), anyNamesToPatternAst, app,
6668
namesToPatternAst, opApp, range)
@@ -69,6 +71,8 @@ import Stan.Pattern.Type (PatternType, charPattern, foldableMethodsPatterns, fol
6971
listPattern, stringPattern, textPattern, (|->), (|::))
7072
import Stan.Severity (Severity (..))
7173

74+
import Stan.Core.ModuleName
75+
7276
import qualified Data.List.NonEmpty as NE
7377
import qualified Stan.Category as Category
7478

@@ -95,6 +99,7 @@ antiPatternInspectionsMap = fromList $ fmapToFst inspectionId
9599
, plustan01
96100
, plustan02
97101
, plustan03
102+
, plustan04
98103
]
99104

100105
-- | Smart constructor to create anti-pattern 'Inspection'.
@@ -452,3 +457,40 @@ plustan03 = mkAntiPatternInspection (Id "PLU-STAN-03") "No usage of Optional typ
452457
where
453458
useOfFromMaybe :: NameMeta
454459
useOfFromMaybe = "fromMaybe" `plutusTxNameFrom` "PlutusTx.Maybe"
460+
461+
plustan04 :: Inspection
462+
plustan04 = mkAntiPatternInspection (Id "PLU-STAN-04") "Usage of eq instance of ScriptHash/PublicKeyHash/Credential"
463+
(FindAst pat)
464+
& descriptionL .~ "Usage of eq instance of script-hash / pubkeyhash / payment credential "
465+
& solutionL .~
466+
[ "Potential staking value theft might want to prefer eq comparison of address" ]
467+
& severityL .~ Warning
468+
where
469+
470+
opNames :: [NameMeta]
471+
opNames = map opName ["<", "<=", "==", ">", ">="]
472+
473+
pat = foldl' (\acc x -> acc ||| PatternAstName x fun)
474+
(PatternAstNeg PatternAstAnything) opNames
475+
476+
fun :: PatternType
477+
fun = (publicKeyHashPattern ||| scriptHashPattern ||| credentialPattern)
478+
|-> (?) |-> (?)
479+
480+
opName :: Text -> NameMeta
481+
opName = (`ghcPrimNameFrom` "GHC.Classes")
482+
483+
publicKeyHashPattern :: PatternType
484+
publicKeyHashPattern = ledgerApiTypePattern "PubKeyHash" "Crypto"
485+
486+
scriptHashPattern :: PatternType
487+
scriptHashPattern = ledgerApiTypePattern "ScriptHash" "Scripts"
488+
489+
credentialPattern :: PatternType
490+
credentialPattern = ledgerApiTypePattern "Credential" "Credential"
491+
492+
ledgerApiTypePattern name moduleSuffix = NameMeta
493+
{ nameMetaName = name
494+
, nameMetaModuleName = ModuleName $ "PlutusLedgerApi.V1." <> moduleSuffix
495+
, nameMetaPackage = "plutus-ledger-api"
496+
} |:: []

stan.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ library target
176176
, text
177177
, unordered-containers
178178
, plutus-tx ^>=1.29
179+
, plutus-ledger-api ^>=1.29
179180

180181
exposed-modules: Target.AntiPattern
181182
Target.AntiPattern.Stan0206
@@ -220,6 +221,9 @@ test-suite stan-test
220221
, tomland
221222
, trial
222223
, unordered-containers
224+
-- This should be removed after the reserach is done
225+
, pretty-show
226+
, pretty-simple
223227

224228
ghc-options: -threaded
225229
-rtsopts

target/Target/PlutusTx.hs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import qualified PlutusTx.AssocMap as AssocMap
1616
import PlutusTx (UnsafeFromData(unsafeFromBuiltinData))
1717

1818
-- Place for future imports
19-
--
19+
import PlutusLedgerApi.V1 (PubKeyHash(..),Credential(..),ScriptHash(..))
2020
--
2121
--
2222
--
@@ -36,6 +36,31 @@ unsafeFromBuiltinData :: Integer
3636
unsafeFromBuiltinData =
3737
Tx.unsafeFromBuiltinData (error "we don't care")
3838

39-
fromMaybe01 :: Integer
40-
fromMaybe01 =
41-
Maybe.fromMaybe 2 (Just 1)
39+
usageOfPTxMaybe :: Integer
40+
usageOfPTxMaybe = let
41+
x = Maybe.fromMaybe 0 (Maybe.Just 1)
42+
in x
43+
44+
pubKeyHashEq :: Bool
45+
pubKeyHashEq = pubKeyHash == pubKeyHash
46+
where
47+
pubKeyHash :: PubKeyHash
48+
pubKeyHash = error "we don't care"
49+
50+
scriptHashEq :: Bool
51+
scriptHashEq = scriptHash == scriptHash
52+
where
53+
scriptHash :: ScriptHash
54+
scriptHash = error "we don't care"
55+
56+
credentialHashEq :: Bool
57+
credentialHashEq = credentialHash == credentialHash
58+
where
59+
credentialHash :: Credential
60+
credentialHash = error "we don't care"
61+
62+
credentialHashLe :: Bool
63+
credentialHashLe = credentialHash < credentialHash
64+
where
65+
credentialHash :: Credential
66+
credentialHash = error "we don't care"

test/Test/Stan/Analysis.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import Test.Stan.Analysis.Partial (analysisPartialSpec)
1818
import Test.Stan.Analysis.Style (analysisStyleSpec)
1919

2020
import qualified Data.Set as Set
21+
--import qualified GHC.Prelude as Prel
22+
--import GHC.IO (unsafePerformIO)
23+
--import Text.Pretty.Simple (pPrint)
2124

2225

2326
analysisSpec :: [HieFile] -> Spec
@@ -26,6 +29,7 @@ analysisSpec hieFiles = describe "Static Analysis" $ do
2629
let checksMap = mkDefaultChecks (map hie_hs_file hieFiles)
2730

2831
-- tests without ignorance
32+
--(Just myFile) = find ((== "target/Target/PlutusTx.hs") . hie_hs_file) hieFiles
2933
let analysis = runAnalysis extensionsMap checksMap [] hieFiles
3034
analysisPartialSpec analysis
3135
analysisInfiniteSpec analysis

test/Test/Stan/Analysis/PlutusTx.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,25 @@ analysisPlutusTxSpec :: Analysis -> Spec
1414
analysisPlutusTxSpec analysis = describe "Plutus-Tx" $ do
1515
let checkObservation = observationAssert ["PlutusTx"] analysis
1616

17-
--it "PLU-STAN-0X: no variable named foo" $
18-
--checkObservation AntiPattern.dummyFooStan01 37 3 6
19-
2017
it "PLU-STAN-01: PlutusTx.AssocMap unsafeFromList" $
2118
checkObservation AntiPattern.plustan01 33 12 35
2219

2320
it "PLU-STAN-02: PlutusTx.UnsafeFromData unsafeFromBuiltinData" $
2421
checkObservation AntiPattern.plustan02 37 3 27
2522

2623
it "PLU-STAN-03: No usage of Optional types in on-chain code" $
27-
checkObservation AntiPattern.plustan03 41 3 18
24+
checkObservation AntiPattern.plustan03 41 7 22
25+
26+
it "PLU-STAN-04: == on pubKeyHash" $
27+
checkObservation AntiPattern.plustan04 45 27 29
28+
29+
it "PLU-STAN-04: == on scriptHash" $
30+
checkObservation AntiPattern.plustan04 51 27 29
31+
32+
it "PLU-STAN-04: == on credentialHash" $
33+
checkObservation AntiPattern.plustan04 57 35 37
34+
35+
it "PLU-STAN-04: < on credentialHash" $
36+
checkObservation AntiPattern.plustan04 63 35 36
2837

2938

0 commit comments

Comments
 (0)