Skip to content

Commit

Permalink
Merge #3253 #3269
Browse files Browse the repository at this point in the history
3253: simple metadata format in some transaction endpoints  r=sevanspowell a=paolino




- We implement support for transaction creation (old and new) 
- we implement a query parameter `simple-metadata` on `get transaction` and `list transactions` to select the new  format in the response 
- we implement a switch in the CLI commands get and list to support the new format in the output 
- we use `oneOf ` openapi combinator to document the possibility of the two formats for the same endpoints


### Issue Number

ADP-1596


3269: Validity interval fix r=paweljakubas a=paweljakubas

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->


- [x] simplyfying interval logic
- [x] enhaning integration testing 

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number
adp-1738
<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Paolo Veronelli <[email protected]>
Co-authored-by: Paolo Veronelli <[email protected]>
Co-authored-by: Samuel Evans-Powell <[email protected]>
Co-authored-by: Johannes Lund <[email protected]>
Co-authored-by: Jonathan Knowles <[email protected]>
Co-authored-by: Pawel Jakubas <[email protected]>
Co-authored-by: Piotr Stachyra <[email protected]>
  • Loading branch information
8 people authored May 11, 2022
3 parents e8eb748 + c1e8181 + ac853c8 commit 6bf3f0c
Show file tree
Hide file tree
Showing 30 changed files with 1,402 additions and 618 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
33 changes: 24 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
( TxMetadataSchema (..), TxMetadataWithSchema )
import Cardano.Wallet.Orphans
()
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -729,6 +730,12 @@ cmdWalletGetUtxoStatistics mkClient =
data TransactionFeatures = NoShelleyFeatures | ShelleyFeatures
deriving (Show, Eq)

-- | Which json schema to use for output
metadataSchemaOption :: Parser TxMetadataSchema
metadataSchemaOption = flag TxMetadataDetailedSchema TxMetadataNoSchema
$ long "simple-metadata"
<> help "output metadata json in no-schema encoding"

-- | cardano-wallet transaction
cmdTransaction
:: ToJSON wallet
Expand Down Expand Up @@ -760,7 +767,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 +790,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 +826,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 +852,7 @@ data TransactionListArgs = TransactionListArgs
, _timeRangeStart :: Maybe Iso8601Time
, _timeRangeEnd :: Maybe Iso8601Time
, _sortOrder :: Maybe SortOrder
, _schema :: TxMetadataSchema
}

cmdTransactionList
Expand All @@ -860,13 +868,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 == TxMetadataNoSchema)

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

cmdTransactionGet
Expand All @@ -929,10 +942,12 @@ 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)
(ApiTxId $ ApiT $ getTxId txId)
metadataSchema

{-------------------------------------------------------------------------------
Commands - 'address'
Expand Down Expand Up @@ -1435,16 +1450,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
Loading

0 comments on commit 6bf3f0c

Please sign in to comment.