Skip to content

Commit

Permalink
Try #3253:
Browse files Browse the repository at this point in the history
  • Loading branch information
iohk-bors[bot] authored May 4, 2022
2 parents 0fabfd4 + ecedb63 commit d6fbd40
Show file tree
Hide file tree
Showing 28 changed files with 1,024 additions and 429 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ lib/shelley/test/data/balanceTx/**/actual

### Docs build
/_build/
.vscode/settings.json
20 changes: 20 additions & 0 deletions docs/contributing/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ The default log level for log files is Info.
Only Error level logs are shown on stdout during test execution. To
change this, set the `*_MIN_SEVERITY` variables shown above.

#### Common Failures and Resolution

##### No More Wallets

If your test fails with something like:

```
user error (No more faucet wallet available in MVar!)
```

Generate more wallet mnemonics and populate the appropriate list in `lib/core-integration/src/Test/Integration/Faucet.hs`.

Generate new mnemonics with:

```
nix build .#cardano-wallet
# Size may vary depending on which array you need to add to.
./result/bin/cardano-wallet recovery-phrase generate --size 15
```

## Mock servers

Use the `cardano-wallet:mock-token-metadata-server` executable as a
Expand Down
36 changes: 27 additions & 9 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ import Cardano.Wallet.Api.Types
, ApiPostRandomAddressData (..)
, ApiT (..)
, ApiTxId (ApiTxId)
, ApiTxMetadata (..)
, ApiWallet
, Base (Base16)
, ByronWalletPostData (..)
Expand All @@ -162,6 +161,8 @@ import Cardano.Wallet.Api.Types
, WalletPutPassphraseData (..)
, fmtAllowedWords
)
import Cardano.Wallet.Api.Types.SchemaMetadata
( TxMetadataWithSchema )
import Cardano.Wallet.Orphans
()
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -729,6 +730,15 @@ cmdWalletGetUtxoStatistics mkClient =
data TransactionFeatures = NoShelleyFeatures | ShelleyFeatures
deriving (Show, Eq)

data MetadataSchema = SimpleSchema | FullSchema
deriving (Show, Eq)

-- | which json schema to use for output, True is simple
metadataSchemaOption :: Parser MetadataSchema
metadataSchemaOption = flag FullSchema SimpleSchema
$ long "simple-metadata"
<> help "output metadata json in no-schema encoding"

-- | cardano-wallet transaction
cmdTransaction
:: ToJSON wallet
Expand Down Expand Up @@ -760,7 +770,7 @@ data TransactionCreateArgs t = TransactionCreateArgs
{ _port :: Port "Wallet"
, _id :: WalletId
, _payments :: NonEmpty Text
, _metadata :: ApiTxMetadata
, _metadata :: Maybe TxMetadataWithSchema
, _timeToLive :: Maybe (Quantity "second" NominalDiffTime)
}

Expand All @@ -783,7 +793,7 @@ cmdTransactionCreate isShelley mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> whenShelley (ApiTxMetadata Nothing) metadataOption isShelley
<*> whenShelley Nothing metadataOption isShelley
<*> whenShelley Nothing timeToLiveOption isShelley
exec (TransactionCreateArgs wPort wId wAddressAmounts md ttl) = do
wPayments <- either (fail . getTextDecodingError) pure $
Expand Down Expand Up @@ -819,7 +829,7 @@ cmdTransactionFees isShelley mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> whenShelley (ApiTxMetadata Nothing) metadataOption isShelley
<*> whenShelley Nothing metadataOption isShelley
<*> whenShelley Nothing timeToLiveOption isShelley
exec (TransactionCreateArgs wPort wId wAddressAmounts md ttl) = do
wPayments <- either (fail . getTextDecodingError) pure $
Expand All @@ -845,6 +855,7 @@ data TransactionListArgs = TransactionListArgs
, _timeRangeStart :: Maybe Iso8601Time
, _timeRangeEnd :: Maybe Iso8601Time
, _sortOrder :: Maybe SortOrder
, _schema :: MetadataSchema
}

cmdTransactionList
Expand All @@ -860,13 +871,17 @@ cmdTransactionList mkTxClient =
<*> optional timeRangeStartOption
<*> optional timeRangeEndOption
<*> optional sortOrderOption
exec (TransactionListArgs wPort wId mTimeRangeStart mTimeRangeEnd mOrder) =
<*> metadataSchemaOption
exec
(TransactionListArgs
wPort wId mTimeRangeStart mTimeRangeEnd mOrder metadataSchema) =
runClient wPort Aeson.encodePretty $ listTransactions
mkTxClient
(ApiT wId)
mTimeRangeStart
mTimeRangeEnd
(ApiT <$> mOrder)
(metadataSchema == SimpleSchema)

-- | Arguments for 'transaction submit' command
data TransactionSubmitArgs = TransactionSubmitArgs
Expand Down Expand Up @@ -916,6 +931,7 @@ data TransactionGetArgs = TransactionGetArgs
{ _port :: Port "Wallet"
, _wid :: WalletId
, _txid :: TxId
, _schema :: MetadataSchema
}

cmdTransactionGet
Expand All @@ -929,9 +945,11 @@ cmdTransactionGet mkClient =
<$> portOption
<*> walletIdArgument
<*> transactionIdArgument
exec (TransactionGetArgs wPort wId txId) = do
<*> metadataSchemaOption
exec (TransactionGetArgs wPort wId txId metadataSchema ) = do
runClient wPort Aeson.encodePretty $ getTransaction mkClient
(ApiT wId)
(metadataSchema == SimpleSchema)
(ApiTxId $ ApiT $ getTxId txId)

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1435,16 +1453,16 @@ transactionSubmitPayloadArgument = argumentT $ mempty
-- | [--metadata=JSON]
--
-- Note: we decode the JSON just so that we can validate more client-side.
metadataOption :: Parser ApiTxMetadata
metadataOption :: Parser (Maybe TxMetadataWithSchema)
metadataOption = option txMetadataReader $ mempty
<> long "metadata"
<> metavar "JSON"
<> value (ApiTxMetadata Nothing)
<> value Nothing
<> help ("Application-specific transaction metadata as a JSON object. "
<> "The value must match the schema defined in the "
<> "cardano-wallet OpenAPI specification.")

txMetadataReader :: ReadM ApiTxMetadata
txMetadataReader :: ReadM (Maybe TxMetadataWithSchema)
txMetadataReader = eitherReader (Aeson.eitherDecode' . BL8.pack)

-- | [--ttl=DURATION]
Expand Down
5 changes: 4 additions & 1 deletion lib/cli/test/data/Cardano/CLISpec/transaction get --help
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Usage: transaction get [--port INT] WALLET_ID TRANSACTION_ID
Usage: transaction get [--port INT] WALLET_ID TRANSACTION_ID
[--simple-metadata]
Get a transaction with specified id.

Available options:
-h,--help Show this help text
--port INT port used for serving the wallet API.
(default: 8090)
--simple-metadata output metadata json in no-schema
encoding
5 changes: 4 additions & 1 deletion lib/cli/test/data/Cardano/CLISpec/transaction list --help
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Usage: transaction list [--port INT] WALLET_ID [--start TIME]
[--end TIME] [--order ORDER]
[--end TIME] [--order ORDER]
[--simple-metadata]
List the transactions associated with a wallet.

Available options:
Expand All @@ -14,3 +15,5 @@ Available options:
2016-11-21T10:15:00Z).
--order ORDER specifies a sort order, either
'ascending' or 'descending'.
--simple-metadata output metadata json in no-schema
encoding
32 changes: 27 additions & 5 deletions lib/cli/test/unit/Cardano/CLISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import Cardano.Wallet.Api.Client
, transactionClient
, walletClient
)
import Cardano.Wallet.Api.Types
( ApiT (..), ApiTxMetadata (..) )
import Cardano.Wallet.Api.Types.SchemaMetadata
( detailedMetadata, noSchemaMetadata )
import Cardano.Wallet.Primitive.Types
( PoolMetadataSource )
import Cardano.Wallet.Primitive.Types.Tx
Expand Down Expand Up @@ -255,8 +255,9 @@ spec = do
describe "Tx Metadata JSON option" $ do
let parse arg = execParserPure defaultPrefs
(info metadataOption mempty) ["--metadata", arg]
let md = ApiT (TxMetadata (Map.singleton 42 (TxMetaText "hi")))
let ok ex (Success res) = ex == getApiTxMetadata res
let md = detailedMetadata
(TxMetadata (Map.singleton 42 (TxMetaText "hi")))
let ok ex (Success res) = ex == res
ok _ _ = False
let err (Failure _) = True
err _ = False
Expand All @@ -268,7 +269,28 @@ spec = do
, ("invalid", "{ \"json\": true }", err)
, ("null 1", "{ \"0\": null }", err)
, ("null 2", "null", ok Nothing)
, ("null 3", "{ }", ok (Just (ApiT mempty)))
, ("null 3", "{ }", ok (Just (detailedMetadata mempty)))
]
describe "Tx No-Schema Metadata JSON option" $ do
let parse arg = execParserPure
defaultPrefs (info metadataOption mempty) ["--metadata", arg]
let md = noSchemaMetadata
(TxMetadata (Map.singleton 42 (TxMetaText "hi")))
let ok ex (Success res) = ex == res
ok _ _ = False
let err (Failure _) = True
err _ = False
mapM_
(\(desc, arg, tst) -> it desc (parse arg `shouldSatisfy` tst))
[ ("valid", "{ \"42\": \"hi\" }", ok $ Just md)
, ("malformed", "testing", err)
, ("malformed trailing", "{ \"0\": \"\" } arstneio", err)
, ("invalid", "{ \"json\": true }", err)
, ("null 1", "{ \"0\": null }", err)
, ("null 2", "null", ok Nothing)
-- this is the default parsing success:
, ("null 3", "{ }", ok $ Just $ detailedMetadata mempty)

]

describe "Tx TTL option" $ do
Expand Down
20 changes: 20 additions & 0 deletions lib/core-integration/src/Test/Integration/Faucet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,26 @@ seqMnemonics = unsafeMkMnemonic <$>
, "drive", "grant", "tumble", "catalog", "plastic"
, "hello", "stomach", "utility", "safe", "cradle"
]
, [ "garage" , "warm" , "blouse" , "peanut" , "pair"
, "trend" , "fine" , "hybrid" , "risk" , "mail"
, "fan" , "damage" , "push" , "shrug" , "boost"
]
, [ "sleep" , "sword" , "chaos" , "taste" , "emotion"
, "achieve" , "prosper" , "scrap" , "delay" , "wing"
, "ketchup" , "neglect" , "foam" , "muffin" , "brisk"
]
, [ "lawn" , "bring" , "diamond" , "network" , "remember"
, "slight" , "auto" , "select" , "vivid" , "silent"
, "letter" , "usual" , "pen" , "yellow" , "equip"
]
, [ "coast" , "under" , "relax" , "vault" , "hip"
, "plastic" , "boat" , "dice" , "genuine" , "inject"
, "have" , "swim" , "miss" , "expire" , "tornado"
]
, [ "damp" , "fabric" , "witness" , "height" , "hurdle"
, "office" , "midnight" , "gaze" , "engage" , "eagle"
, "into" , "cup" , "about" , "velvet" , "acquire"
]
]

maMnemonics :: [Mnemonic 24]
Expand Down
8 changes: 6 additions & 2 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2803,11 +2803,13 @@ postTransactionFeeViaCLI ctx args = cardanoWalletCLI $ join
listTransactionsViaCLI
:: forall r s m. (CmdResult r, HasType (Port "wallet") s, MonadIO m)
=> s
-> Bool
-> [String]
-> m r
listTransactionsViaCLI ctx args = cardanoWalletCLI $ join
listTransactionsViaCLI ctx metadataSchema args = cardanoWalletCLI $ join
[ ["transaction", "list"]
, ["--port", show (ctx ^. typed @(Port "wallet"))]
, ["--simple-metadata" | metadataSchema]
, args
]

Expand Down Expand Up @@ -2838,10 +2840,12 @@ getTransactionViaCLI
=> s
-> String
-> String
-> Bool
-> m r
getTransactionViaCLI ctx wid tid = cardanoWalletCLI $ join
getTransactionViaCLI ctx wid tid metadataSchema = cardanoWalletCLI $ join
[ ["transaction", "get"]
, ["--port", show (ctx ^. typed @(Port "wallet")), wid, tid]
, ["--simple-metadata" | metadataSchema]
]

proc' :: FilePath -> [String] -> CreateProcess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,10 @@ spec = describe "SHELLEY_ADDRESSES" $ do
-- 4. Wait for transaction from A -> B to no longer be pending
eventually "Transaction from A -> B is discovered on B" $ do
request @(ApiTransaction n) ctx
(Link.getTransaction @'Shelley wA rtx) Default Empty
(Link.getTransaction @'Shelley wA rtx False) Default Empty
>>= expectField #status (`shouldBe` ApiT InLedger)
request @(ApiTransaction n) ctx
(Link.getTransaction @'Shelley wB rtx) Default Empty
(Link.getTransaction @'Shelley wB rtx False) Default Empty
>>= expectField #status (`shouldBe` ApiT InLedger)

-- 5. Check that there's one more used and total addresses on the wallets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ import Data.Text
( Text )
import Data.Text.Class
( showT, toText )
import Data.Tuple.Extra
( both )
import Numeric.Natural
( Natural )
import Test.Hspec
Expand Down Expand Up @@ -154,8 +156,6 @@ import qualified Cardano.Wallet.Api.Link as Link
import qualified Data.ByteString as BS
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Tuple.Extra
( both )
import qualified Network.HTTP.Types.Status as HTTP

spec :: forall n.
Expand Down Expand Up @@ -238,7 +238,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
eventually "Wallet has joined pool and deposit info persists" $ do
rJoin' <- request @(ApiTransaction n) ctx
(Link.getTransaction @'Shelley src
(getFromResponse Prelude.id rJoin))
(getFromResponse Prelude.id rJoin) False)
Default Empty
verify rJoin'
[ expectResponseCode HTTP.status200
Expand All @@ -249,14 +249,14 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
]

let txId = getFromResponse #id rJoin
let link = Link.getTransaction @'Shelley src (ApiTxId txId)
let link = Link.getTransaction @'Shelley src (ApiTxId txId) False
eventually "delegation transaction is in ledger" $ do
rSrc <- request @(ApiTransaction n) ctx link Default Empty
verify rSrc
[ expectResponseCode HTTP.status200
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#metadata . #getApiTxMetadata) (`shouldBe` Nothing)
, expectField #metadata (`shouldBe` Nothing)
, expectField #inputs $ \inputs' -> do
inputs' `shouldSatisfy` all (isJust . source)
]
Expand Down Expand Up @@ -302,7 +302,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
expectResponseCode HTTP.status202 r1
eventually "Wallet has not consumed rewards" $ do
let linkSrc = Link.getTransaction @'Shelley
src (getFromResponse Prelude.id r1)
src (getFromResponse Prelude.id r1) False
request @(ApiTransaction n) ctx linkSrc Default Empty
>>= flip verify
[ expectField
Expand Down Expand Up @@ -377,7 +377,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
eventually "There's at least one outgoing transaction with a withdrawal" $ do
rWithdrawal <- request @(ApiTransaction n) ctx
(Link.getTransaction @'Shelley src
(getFromResponse Prelude.id rTx))
(getFromResponse Prelude.id rTx) False)
Default Empty
verify rWithdrawal
[ expectResponseCode HTTP.status200
Expand Down Expand Up @@ -413,7 +413,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
let quitFeeAmt = getFromResponse #amount rq

eventually "Certificates are inserted after quitting a pool" $ do
let epg = Link.getTransaction @'Shelley src txid
let epg = Link.getTransaction @'Shelley src txid False
rlg <- request @(ApiTransaction n) ctx epg Default Empty
verify rlg
[ expectField
Expand Down Expand Up @@ -934,14 +934,14 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
]

let txId = getFromResponse #id rQuit
let link = Link.getTransaction @'Shelley w (ApiTxId txId)
let link = Link.getTransaction @'Shelley w (ApiTxId txId) False
eventually "quit transaction is in ledger" $ do
rSrc <- request @(ApiTransaction n) ctx link Default Empty
verify rSrc
[ expectResponseCode HTTP.status200
, expectField (#direction . #getApiT) (`shouldBe` Incoming)
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#metadata . #getApiTxMetadata) (`shouldBe` Nothing)
, expectField #metadata (`shouldBe` Nothing)
, expectField #inputs $ \inputs' -> do
inputs' `shouldSatisfy` all (isJust . source)
]
Expand Down
Loading

0 comments on commit d6fbd40

Please sign in to comment.