Skip to content

Commit

Permalink
Add RestorationMode type
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Jan 2, 2024
1 parent 3b80540 commit 2a3e154
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 516 deletions.
1 change: 1 addition & 0 deletions lib/wallet/api/http/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ cmdWalletCreateFromMnemonic mkClient =
(ApiT wName)
(ApiT wPwd)
Nothing
Nothing

-- | Arguments for 'wallet create from-public-key' command
data WalletCreateFromPublicKeyArgs = WalletCreateFromPublicKeyArgs
Expand Down
4 changes: 4 additions & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ import Cardano.Wallet.Api.Types.MintBurn
, ApiTokenAmountFingerprint (..)
, ApiTokens (..)
)
import Cardano.Wallet.Api.Types.RestorationMode
( RestorationMode
)
import Cardano.Wallet.Api.Types.SchemaMetadata
( TxMetadataWithSchema
)
Expand Down Expand Up @@ -1062,6 +1065,7 @@ data WalletPostData = WalletPostData
, name :: !(ApiT WalletName)
, passphrase :: !(ApiT (Passphrase "user"))
, oneChangeAddressMode :: !(Maybe Bool)
, restorationMode :: Maybe RestorationMode
}
deriving (FromJSON, ToJSON) via DefaultRecord WalletPostData
deriving (Eq, Generic, Show)
Expand Down
90 changes: 90 additions & 0 deletions lib/wallet/api/http/Cardano/Wallet/Api/Types/RestorationMode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.Wallet.Api.Types.RestorationMode
( RestorationMode (..)
) where

import Prelude

import Data.Aeson
( FromJSON (parseJSON)
, KeyValue ((.=))
, ToJSON (toJSON)
, Value (..)
, object
, (.:)
)
import Data.Aeson.Types
( Parser
)
import Data.Quantity
( Quantity (Quantity)
)
import Data.Time
( UTCTime
)
import Data.Time.Format.ISO8601
( iso8601ParseM
, iso8601Show
)
import Data.Word
( Word32
)
import GHC.Generics
( Generic
)

import qualified Data.Text as T

data RestorationMode
= RestoreFromGenesis
| RestoreFromSlot (Quantity "slot" Word32)
| RestoreFromTip
| RestoreFromDate UTCTime
| RestoreFromBlock (Quantity "block" Word32)
deriving (Eq, Show, Generic)

instance ToJSON RestorationMode where
toJSON :: RestorationMode -> Value
toJSON = \case
RestoreFromGenesis -> object [ "unit" .= String "genesis" ]
RestoreFromSlot (Quantity s) ->
object
[ "unit" .= String "slot"
, "quantity" .= s
]
RestoreFromTip -> object [ "unit" .= String "tip" ]
RestoreFromDate t -> object
[ "unit" .= String "time"
, "value" .= String (T.pack $ iso8601Show t)
]
RestoreFromBlock (Quantity b) ->
object
[ "unit" .= String "block"
, "quantity" .= b
]

instance FromJSON RestorationMode where
parseJSON :: Value -> Parser RestorationMode
parseJSON = \case
Object o -> do
unit <- o .: "unit"
case unit of
String "genesis" -> pure RestoreFromGenesis
String "slot" -> do
s <- o .: "quantity"
pure $ RestoreFromSlot (Quantity s)
String "tip" -> pure RestoreFromTip
String "time" -> do
t <- o .: "value"
case iso8601ParseM (T.unpack t) of
Nothing -> fail "Invalid date format"
Just t' -> pure $ RestoreFromDate t'
String "block" -> do
b <- o .: "quantity"
pure $ RestoreFromBlock (Quantity b)
_ -> fail "Invalid restoration unit"
_ -> fail "Invalid restoration mode"
1 change: 1 addition & 0 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ library cardano-wallet-api-http
Cardano.Wallet.Api.Types.Key
Cardano.Wallet.Api.Types.MintBurn
Cardano.Wallet.Api.Types.Primitive
Cardano.Wallet.Api.Types.RestorationMode
Cardano.Wallet.Api.Types.SchemaMetadata
Cardano.Wallet.Api.Types.Transaction
Cardano.Wallet.Api.Types.WalletAsset
Expand Down
14 changes: 14 additions & 0 deletions lib/wallet/test/data/Cardano/Wallet/Api/RestorationMode.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ import Cardano.Wallet.Api.Types.Error
, ApiErrorSharedWalletNoSuchCosigner (..)
, ApiErrorTxOutputLovelaceInsufficient (..)
)
import Cardano.Wallet.Api.Types.RestorationMode
( RestorationMode (..)
)
import Cardano.Wallet.Api.Types.SchemaMetadata
( TxMetadataSchema (..)
, TxMetadataWithSchema (..)
Expand Down Expand Up @@ -527,6 +530,7 @@ import Data.OpenApi
, NamedSchema (..)
, Schema
, ToSchema (..)
, validateToJSON
)
import Data.OpenApi.Declare
( Declare
Expand Down Expand Up @@ -619,6 +623,7 @@ import Test.QuickCheck
, Arbitrary1 (..)
, Gen
, InfiniteList (..)
, Property
, applyArbitrary2
, applyArbitrary3
, arbitraryBoundedEnum
Expand Down Expand Up @@ -700,6 +705,9 @@ import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Yaml as Yaml
import qualified Prelude
import Test.Hspec.QuickCheck
( prop
)
import qualified Test.Utils.Roundtrip as Utils

type T0 = 'Testnet 0
Expand Down Expand Up @@ -846,6 +854,7 @@ spec = do
jsonTest @(ApiRewardAccount T0)
jsonTest @(ApiExternalCertificate T0)
jsonTest @ApiVoteAction
jsonTest @RestorationMode

describe "ApiEra roundtrip" $
it "toApiEra . fromApiEra == id" $ property $ \era -> do
Expand Down Expand Up @@ -909,6 +918,17 @@ spec = do
ReqBody '[JSON] WalletPostData :> PostNoContent
)

describe "Verify that schema and json instances agrees on writing values for" $
do
let check
:: forall v
. (ToJSON v, ToSchema v, Show v, Arbitrary v) => Property
check = forAll arbitrary $ \(v :: v) -> do
let es = validateToJSON v
counterexample (show (v, es)) $ length es `shouldBe` 0
prop "WalletPostData" $ check @WalletPostData
prop "ApiVoteAction" $ check @ApiVoteAction

describe
"verify that every path specified by the servant server matches an \
\existing path in the specification" $
Expand Down Expand Up @@ -2699,6 +2719,11 @@ instance Arbitrary (Hash "BlockHeader") where
instance Arbitrary ApiBlockHeader where
arbitrary = genericArbitrary
shrink = genericShrink

instance Arbitrary RestorationMode where
arbitrary = genericArbitrary
shrink = genericShrink

{-------------------------------------------------------------------------------
Specification / Servant-Swagger Machinery
Expand Down
85 changes: 0 additions & 85 deletions test/manual/ConnectionLostWithTheNode.md

This file was deleted.

Loading

0 comments on commit 2a3e154

Please sign in to comment.