-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c40bbf
commit b990aba
Showing
9 changed files
with
116 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Anoma.Effect.Indexer | ||
( module Anoma.Effect.Indexer.ListUnrevealedCommits, | ||
) | ||
where | ||
|
||
import Anoma.Effect.Indexer.ListUnrevealedCommits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Anoma.Client.Base where | ||
|
||
import Base | ||
import Juvix.Prelude.Pretty | ||
|
||
data TestStep :: Effect where | ||
Step :: Text -> TestStep m () | ||
|
||
makeSem ''TestStep | ||
|
||
runStep :: (Member EmbedIO r) => (Text -> IO ()) -> Sem (TestStep ': r) a -> Sem r a | ||
runStep f = interpret $ \case | ||
Step t -> liftIO $ f t | ||
|
||
pollForOutput :: forall r a. (Members '[Error SimpleError, EmbedIO] r) => Int -> (a -> Bool) -> Sem r a -> Sem r a | ||
pollForOutput timeoutMillis isDataAvailable action = runConcurrent $ do | ||
raceResult <- race timeoutAction go | ||
case raceResult of | ||
Left {} -> throw (SimpleError (mkAnsiText @Text "Operation timed out")) | ||
Right xs -> return xs | ||
where | ||
go :: Sem (Concurrent ': r) a | ||
go = do | ||
res <- inject action | ||
if | ||
| isDataAvailable res -> return res | ||
| otherwise -> threadDelay (50 * 1000) >> go | ||
|
||
timeoutAction :: (Member Concurrent x) => Sem x () | ||
timeoutAction = void (threadDelay (timeoutMillis * 1000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,91 @@ | ||
module Anoma.Client.Positive where | ||
|
||
import Anoma.Effect.Base | ||
import Anoma.Client.Base | ||
import Anoma.Effect | ||
import Base | ||
import Juvix.Compiler.Nockma.Language hiding (Path) | ||
import Juvix.Compiler.Nockma.Translation.FromTree (anomaClosure) | ||
import Juvix.Prelude.Pretty | ||
|
||
root :: Path Abs Dir | ||
root = relToProject $(mkRelDir "tests/Anoma/Client/positive") | ||
|
||
type Check = | ||
Sem | ||
'[ Reader [Term Natural], | ||
EmbedIO | ||
] | ||
root = relToProject $(mkRelDir "tests/Anoma/Client") | ||
|
||
data ClientTest = ClientTest | ||
{ _clientTestNum :: Int, | ||
_clientTestTag :: Text, | ||
{ _clientTestTag :: Text, | ||
_clientRelRoot :: Path Rel Dir, | ||
_clientMainFile :: Path Rel File, | ||
_clientAssertion :: forall r. (Members '[Error SimpleError, Anoma, EmbedIO] r) => Term Natural -> Sem r () | ||
_clientAssertion :: forall r. (Members '[Logger, Error SimpleError, Anoma, EmbedIO, TestStep] r) => Term Natural -> Sem r () | ||
} | ||
|
||
makeLenses ''ClientTest | ||
|
||
clientTestName :: ClientTest -> Text | ||
clientTestName t = numberedTestName (t ^. clientTestNum) (t ^. clientTestTag) | ||
|
||
withRootCopy :: (Path Abs Dir -> IO a) -> IO a | ||
withRootCopy = withRootTmpCopy root | ||
|
||
fromClientTest :: ClientTest -> TestTree | ||
fromClientTest t = testCase (clientTestName t) assertion | ||
fromClientTest t = testCaseSteps (t ^. clientTestTag) assertion | ||
where | ||
assertion :: Assertion | ||
assertion = runM . runProcess . runSimpleErrorHUnit . ignoreLogger $ do | ||
assertion :: (Text -> IO ()) -> Assertion | ||
assertion stepFun = runM . runProcess . runSimpleErrorHUnit . ignoreLogger . runStep stepFun $ do | ||
step "Compiling" | ||
res :: AnomaResult <- liftIO $ withRootCopy (compileMain False (t ^. clientRelRoot) (t ^. clientMainFile)) | ||
let program :: Term Natural = (res ^. anomaClosure) | ||
p <- envAnomaPath | ||
runAnomaEphemeral p ((t ^. clientAssertion) program) | ||
|
||
-- | Run prove with the given arguements and submit the result to the mempool. | ||
-- Returns the traces from the prove endpoint | ||
proveAndSubmit :: | ||
(Members '[Logger, Error SimpleError, Anoma, EmbedIO, TestStep] r) => | ||
Term Natural -> | ||
[Term Natural] -> | ||
Sem r [Term Natural] | ||
proveAndSubmit program proveArgs = do | ||
step "Proving" | ||
resProve <- | ||
runNockma | ||
RunNockmaInput | ||
{ _runNockmaProgram = program, | ||
_runNockmaArgs = proveArgs | ||
} | ||
step "Submitting transaction candidate" | ||
addTransaction | ||
AddTransactionInput | ||
{ _addTransactionInputCandidate = resProve ^. runNockmaResult | ||
} | ||
return (resProve ^. runNockmaTraces) | ||
|
||
isListUnrevealedCommitsAvailable :: ListUnrevealedCommitsResult -> Bool | ||
isListUnrevealedCommitsAvailable l = not (null (l ^. listUnrevealedCommitsResultCommits)) | ||
|
||
clientTests :: [ClientTest] | ||
clientTests = | ||
[ ClientTest | ||
{ _clientTestTag = "Submit swap transaction", | ||
_clientRelRoot = $(mkRelDir "."), | ||
_clientMainFile = $(mkRelFile "Swap.juvix"), | ||
_clientAssertion = \program -> do | ||
proveTraces <- proveAndSubmit program [] | ||
step "fetching unrevealed commits" | ||
resList <- pollForOutput 2000 isListUnrevealedCommitsAvailable listUnrevealedCommits | ||
case (proveTraces, resList ^. listUnrevealedCommitsResultCommits) of | ||
([proveCommitment], [listCommitment]) -> | ||
liftIO $ | ||
assertBool | ||
"expected commitment from prove and list to be equal" | ||
(nockmaEq proveCommitment listCommitment) | ||
_ -> | ||
throw | ||
( SimpleError | ||
( mkAnsiText @Text | ||
"Expected exactly one commitment to be traced by prove and one commitment to be listed by listUnrevealedCommitments" | ||
) | ||
) | ||
} | ||
] | ||
|
||
allTests :: TestTree | ||
allTests = | ||
testGroup | ||
"Anoma Client positive tests" | ||
[] | ||
(map fromClientTest clientTests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.