-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from input-output-hk/ch1bo/modularize-headlogic
Move multiple types of out HeadLogic
- Loading branch information
Showing
28 changed files
with
448 additions
and
410 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
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
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,79 @@ | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Hydra.ClientInput where | ||
|
||
import Data.Aeson (object, withObject, (.:), (.=)) | ||
import qualified Data.Aeson as Aeson | ||
import Hydra.Ledger (Tx, UTxO) | ||
import Hydra.Prelude | ||
|
||
data ClientInput tx | ||
= Init | ||
| Abort | ||
| Commit (UTxO tx) | ||
| NewTx tx | ||
| GetUtxo | ||
| Close | ||
| Contest | ||
deriving (Generic) | ||
|
||
deriving instance Tx tx => Eq (ClientInput tx) | ||
deriving instance Tx tx => Show (ClientInput tx) | ||
deriving instance Tx tx => Read (ClientInput tx) | ||
|
||
instance (Arbitrary tx, Arbitrary (UTxO tx)) => Arbitrary (ClientInput tx) where | ||
arbitrary = genericArbitrary | ||
|
||
-- NOTE: Somehow, can't use 'genericShrink' here as GHC is complaining about | ||
-- Overlapping instances with 'UTxO tx' even though for a fixed `tx`, there | ||
-- should be only one 'UTxO tx' | ||
shrink = \case | ||
Init -> [] | ||
Abort -> [] | ||
Commit xs -> Commit <$> shrink xs | ||
NewTx tx -> NewTx <$> shrink tx | ||
GetUtxo -> [] | ||
Close -> [] | ||
Contest -> [] | ||
|
||
instance Tx tx => ToJSON (ClientInput tx) where | ||
toJSON = \case | ||
Init -> | ||
object [tagFieldName .= s "init"] | ||
Abort -> | ||
object [tagFieldName .= s "abort"] | ||
Commit u -> | ||
object [tagFieldName .= s "commit", "utxo" .= u] | ||
NewTx tx -> | ||
object [tagFieldName .= s "newTransaction", "transaction" .= tx] | ||
GetUtxo -> | ||
object [tagFieldName .= s "getUtxo"] | ||
Close -> | ||
object [tagFieldName .= s "close"] | ||
Contest -> | ||
object [tagFieldName .= s "contest"] | ||
where | ||
s = Aeson.String | ||
tagFieldName = "input" | ||
|
||
instance Tx tx => FromJSON (ClientInput tx) where | ||
parseJSON = withObject "ClientInput" $ \obj -> do | ||
tag <- obj .: "input" | ||
case tag of | ||
"init" -> | ||
pure Init | ||
"abort" -> | ||
pure Abort | ||
"commit" -> | ||
Commit <$> (obj .: "utxo") | ||
"newTransaction" -> | ||
NewTx <$> (obj .: "transaction") | ||
"getUtxo" -> | ||
pure GetUtxo | ||
"close" -> | ||
pure Close | ||
"contest" -> | ||
pure Contest | ||
_ -> | ||
fail $ "unknown input type: " <> toString @Text tag |
Oops, something went wrong.