Skip to content

Commit a6d8d30

Browse files
committed
feat: filter out the offchain plutus observations
1 parent dbe5850 commit a6d8d30

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ tags
5353
.dir-locals.el
5454
TAGS
5555

56+
# debugging
57+
output.txt
58+
5659
# other
5760
.DS_Store

.stan.toml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1+
#[[remove]]
2+
#directory = "target/"
3+
4+
[[remove]]
5+
directory = "src/"
6+
7+
[[remove]]
8+
directory = "app/"
9+
10+
[[remove]]
11+
directory = "test/"
12+
13+
[[remove]]
14+
directory = "target/Target/AntiPattern/"
15+
16+
[[remove]]
17+
file = "target/Target/AntiPattern.hs"
18+
19+
[[remove]]
20+
file = "target/Target/Partial.hs"
21+
22+
[[remove]]
23+
file = "target/Target/Infinite.hs"
24+
125
[[remove]]
2-
directory = "target/"
26+
file = "target/Target/Style.hs"
327

428
[[check]]
529
type = "Exclude"

src/Stan.hs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ import Stan.Severity (Severity (Error))
4545
import Stan.Toml (configCodec, getTomlConfig, usedTomlFiles)
4646

4747
import qualified Toml
48+
import qualified Slist as Slist
49+
import qualified Data.Set as Set
50+
import qualified Data.Text as T
51+
import Language.Haskell.Exts
52+
import Stan.FileInfo (FileInfo(..))
4853

4954

5055
run :: IO ()
@@ -83,13 +88,56 @@ getAnalysis StanArgs{..} notJson config hieFiles = do
8388
-- show what observations are ignored
8489
pure analysis
8590

91+
isPlutusObservations :: Observation -> Bool
92+
isPlutusObservations Observation{..} =
93+
-- observationInspectionId includes PLU-STAN
94+
"PLU-STAN" `T.isInfixOf` unId observationInspectionId
95+
96+
isOnchainObservations :: Set FilePath -> Observation -> Bool
97+
isOnchainObservations files obs = Set.member (observationFile obs) files
98+
99+
isFileOnchainContract :: FilePath -> IO Bool
100+
isFileOnchainContract file = do
101+
result <- parseFile file
102+
pure $ case result of
103+
ParseOk (Module _ _ _ _ decls) -> any isOnchainModuleAnn decls
104+
_otherwise -> False
105+
106+
isOnchainModuleAnn :: Decl SrcSpanInfo -> Bool
107+
isOnchainModuleAnn (AnnPragma _ (ModuleAnn _ (Lit _ (String _ "onchain-contract" _)))) = True
108+
isOnchainModuleAnn (AnnPragma _ (ModuleAnn _ (Paren _ (ExpTypeSig _ (Lit _ (String _ "onchain-contract" _)) _)))) = True
109+
isOnchainModuleAnn _ = False
110+
111+
onchainFiles :: [HieFile] -> IO (Set FilePath)
112+
onchainFiles hieFiles = do
113+
let files = map hie_hs_file hieFiles
114+
fromList <$> filterM isFileOnchainContract files
115+
116+
onchainCondition :: Set FilePath -> Observation -> Bool
117+
onchainCondition contracts obs = not (isPlutusObservations obs) || isOnchainObservations contracts obs
118+
119+
filterForOnchain :: Set FilePath -> FileInfo -> FileInfo
120+
filterForOnchain contracts info@FileInfo{..}= info {
121+
fileInfoObservations = Slist.filter (onchainCondition contracts) fileInfoObservations }
122+
123+
removeOffchain :: [HieFile] ->Analysis -> IO Analysis
124+
removeOffchain hieFiles analysis = do
125+
contracts <- onchainFiles hieFiles
126+
pure analysis {
127+
analysisObservations = Slist.filter (onchainCondition contracts) (analysisObservations analysis)
128+
, analysisFileMap = fmap (filterForOnchain contracts) (analysisFileMap analysis)
129+
-- TODO: we might want to add those filtered observations to the ignored list
130+
-- but i'm not sure if it's a good idea
131+
}
132+
86133
runStan :: StanArgs -> IO ()
87134
runStan stanArgs@StanArgs{..} = do
88135
let notJson = not stanArgsJsonOut
89136
(configTrial, useDefConfig, env) <- getStanConfig stanArgs notJson
90137
whenResult_ configTrial $ \warnings config -> do
91138
hieFiles <- readHieFiles stanArgsHiedir
92-
analysis <- getAnalysis stanArgs notJson config hieFiles
139+
--NOTE: this filter is applied only for CLI, hls will still show all observations
140+
analysis <- getAnalysis stanArgs notJson config hieFiles >>= removeOffchain hieFiles
93141
-- show what observations are ignored
94142
when notJson $ putText $ indent $ prettyShowIgnoredObservations
95143
(configIgnored config)

stan.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ library
161161
, trial ^>= 0.0.0.0
162162
, trial-optparse-applicative ^>= 0.0.0.0
163163
, trial-tomland ^>= 0.0.0.0
164+
, haskell-src-exts
165+
-- This should be removed after the research is done
166+
, pretty-show
167+
, pretty-simple
164168

165169
executable stan
166170
import: common-options
@@ -221,7 +225,7 @@ test-suite stan-test
221225
, tomland
222226
, trial
223227
, unordered-containers
224-
-- This should be removed after the reserach is done
228+
-- This should be removed after the research is done
225229
, pretty-show
226230
, pretty-simple
227231

target/Target/PlutusTx.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import PlutusTx (UnsafeFromData(unsafeFromBuiltinData))
1717

1818
-- Place for future imports
1919
import PlutusLedgerApi.V1 (PubKeyHash(..),Credential(..),ScriptHash(..))
20-
--
20+
{-# ANN module ("onchain-contract" :: String) #-}
2121
--
2222
--
2323
--

0 commit comments

Comments
 (0)