-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunpodman
executable file
·225 lines (179 loc) · 7.14 KB
/
runpodman
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env stack
{- stack
script
--ghc-options -freverse-errors
--ghc-options -fno-warn-tabs
--resolver nightly-2022-05-29
--package aeson
--package bytestring
--package conduit
--package interpolatedstring-perl6
--package mtl
--package shell-conduit
--package string-conv
--package transformers
--package unliftio-core
--package yaml
-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE QuasiQuotes #-}
import Control.Concurrent (threadDelay)
import Control.Exception (assert)
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Monad.Reader
import Data.Aeson
import Data.Aeson.Types
import Data.Conduit
import Data.Conduit.Shell
import Data.Conduit.Shell.Segments
import Data.String.Conv (toS)
import GHC.Generics (Generic)
import Data.Yaml (decodeFileThrow)
import System.Environment
import System.Exit (exitFailure)
import Text.InterpolatedString.Perl6 (qc)
imageName :: String
imageName = "symbiont-assembly-sdk"
containerName :: String
containerName = "assembly-sdk"
contractDir :: String
contractDir = "workspace/secretaccounts"
main :: IO ()
main = getArgs >>= \case
["buildimage"] -> run $ do
podman "build" "-t" imageName "."
["start"] -> run $ do
pwdE <- liftIO $ getEnv "PWD"
displayE <- liftIO $ getEnv "DISPLAY"
xhost "+" -- FIXME this is insecure
podman "run" "-d" "--rm" "-it"
"--name" containerName
"-v" "dot-symbiont:/home/work/.symbiont"
"-v" "assembly-sdk-dot-local:/home/work/.local"
"-v" (pwdE <> "/workspace:/home/work/workspace")
"--detach-keys=ctrl-q,ctrl-q,ctrl-q,ctrl-q,ctrl-q"
"-v" "/tmp/.X11-unix:/tmp/.X11-unix"
"-e" ("DISPLAY=" <> displayE)
"-p" "8880-8890:8880-8890"
imageName
"/usr/bin/tail" "-f" "/dev/null"
-- podman "exec" containerName "sym" "local-network" "stop"
podman "exec" containerName "sym" "local-network" "start"
createKa
["stop"] -> run $ do
podman "exec" containerName "sym" "local-network" "stop"
podman "rm" "-f" containerName
["shell"] -> run $ do
podman "exec" "-it"
"--detach-keys=ctrl-q,ctrl-q,ctrl-q,ctrl-q,ctrl-q"
containerName
"/bin/bash"
["dashboard"] -> run $ do
podman "exec" "-it" containerName "sym-dash"
["create-ka"] -> run createKa
["deploy"] -> run $ do
podman "exec" containerName "sym" "network" "publish-contract" "--contract-dir" contractDir
-- {{{ contract functions
["c-mine-coin"] -> run (mineCoin $| jq)
["c-get-all-transfers"] -> run (getAllTransfers $| jq)
["c-get-balance"] -> run (getBalance $| jq)
["c-send", recipient, amountStr] -> run (send recipient (read amountStr) $| jq)
-- }}}
-- {{{ test cases
["t-validate-transfer"] -> do
[newKeyAlias1] <- run (strings createKa)
[newKeyAlias2] <- run (strings createKa)
putStrLn $ "new KAs: " <> show [newKeyAlias1, newKeyAlias2]
runWithKA newKeyAlias1 $ do
mineCoin $| jq
send newKeyAlias2 1 $| jq
threadDelay 1_000_000 -- wait to ensure transaction got processed
putStrLn $ "sending one coin from: " <> newKeyAlias1 <> " to: " <> newKeyAlias2
putStrLn "target transfers"
targetBalance <- fmap (parseBalanceJson . head) .runWithKA newKeyAlias2 $ do
getAllTransfers $| jq
strings $ getBalance
putStrLn "source transfers"
sourceBalance <- fmap (parseBalanceJson . head) . runWithKA newKeyAlias1 $ do
getAllTransfers $| jq
strings $ getBalance
putStrLn $ "Target balance after transfer: " <> show targetBalance
assert (targetBalance == 1) $ pure ()
putStrLn $ "Source balance after transfer: " <> show sourceBalance
assert (sourceBalance == 0) $ pure ()
["t-send-swap"] -> do
[newKeyAlias1] <- run (strings createKa)
[newKeyAlias2] <- run (strings createKa)
putStrLn $ "new KAs: " <> show [newKeyAlias1, newKeyAlias2]
runWithKA newKeyAlias1 $ do
mineCoin $| jq
swap newKeyAlias2 1 $| jq
threadDelay 1_000_000 -- wait to ensure transaction got processed
putStrLn $ "swapping one coin from: " <> newKeyAlias1 <> " to: " <> newKeyAlias2 <> " and back"
putStrLn "target transfers"
targetBalance <- fmap (parseBalanceJson . head) .runWithKA newKeyAlias2 $ do
getAllTransfers $| jq
strings $ getBalance
putStrLn "source transfers"
sourceBalance <- fmap (parseBalanceJson . head) . runWithKA newKeyAlias1 $ do
getAllTransfers $| jq
strings $ getBalance
putStrLn $ "Target balance after transfer: " <> show targetBalance
assert (targetBalance == 0) $ pure ()
putStrLn $ "Source balance after transfer: " <> show sourceBalance
assert (sourceBalance == 1) $ pure ()
-- }}}
args -> putStrLn ("unknown arguments: " <> show args) >> exitFailure
mineCoin, getAllTransfers, getBalance :: (MonadUnliftIO m, HasKeyAlias (Segment m)) => Segment m ()
mineCoin = callContractF "mine_coin" "{}"
getAllTransfers = callContractF "get_all_transfers" "{}"
getBalance = callContractF "get_balance" "{}"
createKa :: MonadUnliftIO m => Segment m ()
createKa = podman "exec" containerName "sym" "network" "create-ka"
send :: (MonadUnliftIO m, HasKeyAlias (Segment m)) => KeyAlias -> Int -> Segment m ()
send recipient amount = callContractF "send" [qc|\{ "amount": {show amount}, "recipient": "{recipient}" }|]
swap :: (MonadUnliftIO m, HasKeyAlias (Segment m)) => KeyAlias -> Int -> Segment m ()
swap recipient amount = callContractF "swap" [qc|\{ "amount": {show amount}, "recipient": "{recipient}" }|]
callContractF :: (MonadUnliftIO m, HasKeyAlias (Segment m)) => String -> String -> Segment m ()
callContractF functionName payload = do
keyAlias <- getKa
ContractMeta{name, version, language} <- getContractMeta
liftIO $ print name
podman "exec" containerName "curl" "-X" "POST" "-s" ("http://localhost:8888/api/v1/contracts/" <> name <> "/" <> (show language) <> "-" <> version <> "/" <> functionName)
"-H" "Content-Type: application/json"
"-H" ("Symbiont-Key-Alias: " <> keyAlias)
"-d" payload
parseBalanceJson :: String -> Int
parseBalanceJson responseJson =
let Just responseValue = decode' (toS responseJson) :: Maybe Value
Just (String balanceStr) :: Maybe Value = flip parseMaybe responseValue $
withObject "r1" $ \o -> do
v1 <- o .: "data"
withObject "r2" (.: "result") v1
in read (toS balanceStr)
devNull :: Monad m => Segment m ()
devNull = conduit (void await)
-- {{{ Contract Metadata
getContractMeta :: MonadIO m => m ContractMeta
getContractMeta = decodeFileThrow (contractDir <> "/contract.yaml")
data ContractMeta = ContractMeta
{ name :: String
, version :: String
, language :: Int
} deriving (Show, Generic, FromJSON)
-- }}}
-- {{{ Key Alias reading
type KeyAlias = String
runWithKA :: MonadIO m => KeyAlias -> Segment (ReaderT KeyAlias m) a -> m a
runWithKA keyAlias = flip runReaderT keyAlias . run
class MonadIO m => HasKeyAlias m where
getKa :: m String
instance HasKeyAlias (Segment IO) where
getKa = liftIO $ getEnv "KA"
instance MonadIO m => HasKeyAlias (Segment (ReaderT KeyAlias m)) where
getKa = conduit (ask <* (yield mempty))
--- }}}
-- vim: set filetype=haskell: