From 76dd996cccd9c4f7f7044876c9f8e2c7f2b3f360 Mon Sep 17 00:00:00 2001 From: Brandon Kase Date: Wed, 26 Aug 2020 18:56:50 +0300 Subject: [PATCH] Extends agent tests to payloads+parse Passes assertion, part of #5680 --- src/app/rosetta/models/coin_change.ml | 2 +- src/app/rosetta/models/coin_identifier.ml | 2 +- src/app/rosetta/models/enums.ml | 2 +- src/app/rosetta/models/operation.ml | 2 +- .../rosetta/models/operation_identifier.ml | 2 +- src/app/rosetta/test-agent/agent.ml | 17 ++++++++ src/app/rosetta/test-agent/offline.ml | 43 +++++++++++++++++++ 7 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/app/rosetta/models/coin_change.ml b/src/app/rosetta/models/coin_change.ml index 43686388a61..a8f21df600b 100644 --- a/src/app/rosetta/models/coin_change.ml +++ b/src/app/rosetta/models/coin_change.ml @@ -7,7 +7,7 @@ *) type t = {coin_identifier: Coin_identifier.t; coin_action: Enums.coinaction} -[@@deriving yojson {strict= false}, show] +[@@deriving yojson {strict= false}, show, eq] (** CoinChange is used to represent a change in state of a some coin identified by a coin_identifier. This object is part of the Operation model and must be populated for UTXO-based blockchains. Coincidentally, this abstraction of UTXOs allows for supporting both account-based transfers and UTXO-based transfers on the same blockchain (when a transfer is account-based, don't populate this model). *) let create (coin_identifier : Coin_identifier.t) diff --git a/src/app/rosetta/models/coin_identifier.ml b/src/app/rosetta/models/coin_identifier.ml index b617799caf7..607c6f43ebe 100644 --- a/src/app/rosetta/models/coin_identifier.ml +++ b/src/app/rosetta/models/coin_identifier.ml @@ -9,7 +9,7 @@ type t = { (* Identifier should be populated with a globally unique identifier of a Coin. In Bitcoin, this identifier would be transaction_hash:index. *) identifier: string } -[@@deriving yojson {strict= false}, show] +[@@deriving yojson {strict= false}, show, eq] (** CoinIdentifier uniquely identifies a Coin. *) let create (identifier : string) : t = {identifier} diff --git a/src/app/rosetta/models/enums.ml b/src/app/rosetta/models/enums.ml index f75c2e3a6fc..5c3bb9b0ff6 100644 --- a/src/app/rosetta/models/enums.ml +++ b/src/app/rosetta/models/enums.ml @@ -2,4 +2,4 @@ type curvetype = (* tweedle *) string [@@deriving yojson, show] type signaturetype = (* schnorr *) string [@@deriving yojson, show] -type coinaction = () [@@deriving yojson, show] +type coinaction = () [@@deriving yojson, show, eq] diff --git a/src/app/rosetta/models/operation.ml b/src/app/rosetta/models/operation.ml index ee6a1277936..81d7c7283b5 100644 --- a/src/app/rosetta/models/operation.ml +++ b/src/app/rosetta/models/operation.ml @@ -18,7 +18,7 @@ type t = ; amount: Amount.t option [@default None] ; coin_change: Coin_change.t option [@default None] ; metadata: Yojson.Safe.t option [@default None] } -[@@deriving yojson {strict= false}, show] +[@@deriving yojson {strict= false}, show, eq] (** Operations contain all balance-changing information within a transaction. They are always one-sided (only affect 1 AccountIdentifier) and can succeed or fail independently from a Transaction. *) let create (operation_identifier : Operation_identifier.t) (_type : string) diff --git a/src/app/rosetta/models/operation_identifier.ml b/src/app/rosetta/models/operation_identifier.ml index 1c517e1aa7d..912acfe2400 100644 --- a/src/app/rosetta/models/operation_identifier.ml +++ b/src/app/rosetta/models/operation_identifier.ml @@ -11,7 +11,7 @@ type t = index: int64 ; (* Some blockchains specify an operation index that is essential for client use. For example, Bitcoin uses a network_index to identify which UTXO was used in a transaction. network_index should not be populated if there is no notion of an operation index in a blockchain (typically most account-based blockchains). *) network_index: int64 option [@default None] } -[@@deriving yojson {strict= false}, show] +[@@deriving yojson {strict= false}, show, eq] (** The operation_identifier uniquely identifies an operation within a transaction. *) let create (index : int64) : t = {index; network_index= None} diff --git a/src/app/rosetta/test-agent/agent.ml b/src/app/rosetta/test-agent/agent.ml index 6ee2ec44a55..3a53946377d 100644 --- a/src/app/rosetta/test-agent/agent.ml +++ b/src/app/rosetta/test-agent/agent.ml @@ -198,6 +198,23 @@ let construction_api_payment_through_mempool ~logger ~rosetta_uri [%log debug] ~metadata:[("res", Construction_metadata_response.to_yojson metadata_res)] "Construction_metadata result $res" ; + let%bind payloads_res = + Offline.Payloads.req ~logger ~rosetta_uri ~network_response ~operations + ~metadata:metadata_res.metadata + in + let%bind parse_res = + Offline.Parse.req ~logger ~rosetta_uri ~network_response + ~transaction: + (`Unsigned + payloads_res.Construction_payloads_response.unsigned_transaction) + in + if not ([%equal: Operation.t list] operations parse_res.operations) then ( + [%log debug] + ~metadata: + [ ("expected", [%to_yojson: Operation.t list] operations) + ; ("actual", [%to_yojson: Operation.t list] parse_res.operations) ] + "Construction_parse : Expected $expected, after payloads+parse $actual" ; + failwith "Operations are not equal before and after payloads+parse" ) ; return () (* TODO: Break up this function in the next PR *) diff --git a/src/app/rosetta/test-agent/offline.ml b/src/app/rosetta/test-agent/offline.ml index 293ea3e1acb..f127607443f 100644 --- a/src/app/rosetta/test-agent/offline.ml +++ b/src/app/rosetta/test-agent/offline.ml @@ -47,3 +47,46 @@ module Preprocess = struct Lift.res r ~logger ~of_yojson:Construction_preprocess_response.of_yojson |> Lift.successfully end + +module Payloads = struct + let req ~rosetta_uri ~logger ~operations ~metadata ~network_response = + let%bind r = + post ~rosetta_uri ~logger + ~body: + Construction_payloads_request.( + { network_identifier= + List.hd_exn + network_response.Network_list_response.network_identifiers + ; operations + ; metadata= Some metadata } + |> to_yojson) + ~path:"construction/payloads" + in + Lift.res r ~logger ~of_yojson:Construction_payloads_response.of_yojson + |> Lift.successfully +end + +module Parse = struct + let req ~rosetta_uri ~logger ~transaction ~network_response = + let signed, transaction = + match transaction with + | `Unsigned txn -> + (false, txn) + | `Signed txn -> + (true, txn) + in + let%bind r = + post ~rosetta_uri ~logger + ~body: + Construction_parse_request.( + { network_identifier= + List.hd_exn + network_response.Network_list_response.network_identifiers + ; transaction + ; signed } + |> to_yojson) + ~path:"construction/parse" + in + Lift.res r ~logger ~of_yojson:Construction_parse_response.of_yojson + |> Lift.successfully +end