Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-to-end agent down to submit #5772

Merged
merged 7 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/app/rosetta/lib/construction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ module Metadata = struct
in
let nonce =
Option.map
~f:(fun nonce -> Unsigned.UInt32.(of_string nonce |> add one))
~f:(fun nonce -> Unsigned.UInt32.of_string nonce)
account#nonce
|> Option.value ~default:Unsigned.UInt32.one
|> Option.value ~default:Unsigned.UInt32.zero
in
{ Construction_metadata_response.metadata=
Metadata_data.create ~sender:options.Options.sender
Expand Down Expand Up @@ -355,7 +355,7 @@ module Combine = struct
let%bind signature =
match req.signatures with
| s :: _ ->
M.return @@ s.signing_payload.hex_bytes
M.return @@ s.hex_bytes
| _ ->
M.fail (Errors.create `Signature_missing)
in
Expand Down
45 changes: 42 additions & 3 deletions src/app/rosetta/test-agent/agent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,58 @@ let construction_api_payment_through_mempool ~logger ~rosetta_uri
Offline.Payloads.req ~logger ~rosetta_uri ~network_response ~operations
~metadata:metadata_res.metadata
in
let%bind parse_res =
let%bind payloads_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 (
if not ([%equal: Operation.t list] operations payloads_parse_res.operations)
then (
[%log debug]
~metadata:
[ ("expected", [%to_yojson: Operation.t list] operations)
; ("actual", [%to_yojson: Operation.t list] parse_res.operations) ]
; ( "actual"
, [%to_yojson: Operation.t list] payloads_parse_res.operations ) ]
"Construction_parse : Expected $expected, after payloads+parse $actual" ;
failwith "Operations are not equal before and after payloads+parse" ) ;
let%bind signature =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you remove the %bind and the Deferred.return ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm binding here with the Deferred.return to bind over the Result part (we're in the Deferred.Result monad)

Signer.sign ~keys
~unsigned_transaction_string:payloads_res.unsigned_transaction
|> Deferred.return
in
let%bind combine_res =
Offline.Combine.req ~logger ~rosetta_uri ~network_response ~signature
~unsigned_transaction:payloads_res.unsigned_transaction
~public_key_hex_bytes:keys.public_key_hex_bytes
~address:derive_res.address
in
let%bind combine_parse_res =
Offline.Parse.req ~logger ~rosetta_uri ~network_response
~transaction:
(`Signed combine_res.Construction_combine_response.signed_transaction)
in
if not ([%equal: Operation.t list] operations combine_parse_res.operations)
then (
[%log debug]
~metadata:
[ ("expected", [%to_yojson: Operation.t list] operations)
; ( "actual"
, [%to_yojson: Operation.t list] combine_parse_res.operations ) ]
"Construction_combine : Expected $expected, after combine+parse $actual" ;
failwith "Operations are not equal before and after combine+parse" ) ;
let%bind hash_res =
Offline.Hash.req ~logger ~rosetta_uri ~network_response
~signed_transaction:combine_res.signed_transaction
in
let%bind submit_res =
Peek.Construction.submit ~logger ~rosetta_uri ~network_response
~signed_transaction:combine_res.signed_transaction
in
assert (
String.equal hash_res.Construction_hash_response.transaction_hash
submit_res.transaction_identifier.hash ) ;
[%log debug] "Construction_submit is finalize" ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"finalized"

return ()

(* TODO: Break up this function in the next PR *)
Expand Down
62 changes: 47 additions & 15 deletions src/app/rosetta/test-agent/offline.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
open Async
open Core_kernel
open Models
open Lib
module Lift = Peek.Lift

let post = Peek.post

let net_id = Peek.net_id

open Deferred.Result.Let_syntax

module Derive = struct
Expand All @@ -14,9 +15,7 @@ module Derive = struct
post ~rosetta_uri ~logger
~body:
Construction_derive_request.(
{ network_identifier=
List.hd_exn
network_response.Network_list_response.network_identifiers
{ network_identifier= net_id network_response
; public_key=
{ Public_key.hex_bytes= public_key_hex_bytes
; curve_type= "tweedle" }
Expand All @@ -34,9 +33,7 @@ module Preprocess = struct
post ~rosetta_uri ~logger
~body:
Construction_preprocess_request.(
{ network_identifier=
List.hd_exn
network_response.Network_list_response.network_identifiers
{ network_identifier= net_id network_response
; max_fee= [Amount_of.coda max_fee]
; operations
; suggested_fee_multiplier= None
Expand All @@ -54,9 +51,7 @@ module Payloads = struct
post ~rosetta_uri ~logger
~body:
Construction_payloads_request.(
{ network_identifier=
List.hd_exn
network_response.Network_list_response.network_identifiers
{ network_identifier= net_id network_response
; operations
; metadata= Some metadata }
|> to_yojson)
Expand All @@ -79,14 +74,51 @@ module Parse = struct
post ~rosetta_uri ~logger
~body:
Construction_parse_request.(
{ network_identifier=
List.hd_exn
network_response.Network_list_response.network_identifiers
; transaction
; signed }
{network_identifier= net_id network_response; transaction; signed}
|> to_yojson)
~path:"construction/parse"
in
Lift.res r ~logger ~of_yojson:Construction_parse_response.of_yojson
|> Lift.successfully
end

module Combine = struct
let req ~rosetta_uri ~logger ~unsigned_transaction ~signature ~address
~public_key_hex_bytes ~network_response =
let%bind r =
post ~rosetta_uri ~logger
~body:
Construction_combine_request.(
{ network_identifier= net_id network_response
; unsigned_transaction
; signatures=
[ (* TODO: How important is it to fill in all these details properly? *)
{ Signature.signing_payload=
{ Signing_payload.address
; hex_bytes= "TODO"
; signature_type= None }
; public_key=
{ Public_key.hex_bytes= public_key_hex_bytes
; curve_type= "tweedle" }
; signature_type= "schnorr"
; hex_bytes= signature } ] }
|> to_yojson)
~path:"construction/combine"
in
Lift.res r ~logger ~of_yojson:Construction_combine_response.of_yojson
|> Lift.successfully
end

module Hash = struct
let req ~rosetta_uri ~logger ~signed_transaction ~network_response =
let%bind r =
post ~rosetta_uri ~logger
~body:
Construction_hash_request.(
{network_identifier= net_id network_response; signed_transaction}
|> to_yojson)
~path:"construction/hash"
in
Lift.res r ~logger ~of_yojson:Construction_hash_response.of_yojson
|> Lift.successfully
end
13 changes: 13 additions & 0 deletions src/app/rosetta/test-agent/peek.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,17 @@ module Construction = struct
in
Lift.res ~logger res ~of_yojson:Construction_metadata_response.of_yojson
|> Lift.successfully

(* This is really a poke, but collocating it here because it goes through rosetta *)
let submit ~rosetta_uri ~network_response ~logger ~signed_transaction =
let%bind res =
post ~rosetta_uri ~logger
~body:
Construction_submit_request.(
{network_identifier= net_id network_response; signed_transaction}
|> to_yojson)
~path:"construction/submit"
in
Lift.res ~logger res ~of_yojson:Construction_submit_response.of_yojson
|> Lift.successfully
end
25 changes: 14 additions & 11 deletions src/app/rosetta/test-agent/signer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open Core_kernel
open Signature_lib
open Lib
module Signature = Coda_base.Signature
module User_command = Coda_base.User_command

module Keys = struct
type t = {keypair: Keypair.t; public_key_hex_bytes: string}
Expand Down Expand Up @@ -32,23 +33,25 @@ let sign ~(keys : Keys.t) ~unsigned_transaction_string =
try return (Yojson.Safe.from_string unsigned_transaction_string)
with _ -> Result.fail (Errors.create (`Json_parse None))
in
let%bind unsigned_transaction =
let%map unsigned_transaction =
Transaction.Unsigned.Rendered.of_yojson json
|> Result.map_error ~f:(fun e -> Errors.create (`Json_parse (Some e)))
|> Result.bind ~f:Transaction.Unsigned.of_rendered
in
let user_command_payload =
User_command_info.Partial.to_user_command_payload
~nonce:unsigned_transaction.nonce
unsigned_transaction.Transaction.Unsigned.command
|> Result.ok |> Option.value_exn
in
let signature =
Schnorr.sign keys.keypair.private_key
unsigned_transaction.random_oracle_input
|> Signature.Raw.encode
in
let encoded_signature = Signature.Raw.encode signature in
let signed_transaction =
{ Transaction.Signed.nonce= unsigned_transaction.nonce
; signature= encoded_signature
; command= unsigned_transaction.command }
in
let%map rendered_signed_transaction =
Transaction.Signed.render signed_transaction
let signature' =
User_command.sign_payload keys.keypair.private_key user_command_payload
|> Signature.Raw.encode
in
Transaction.Signed.Rendered.to_yojson rendered_signed_transaction
|> Yojson.Safe.to_string
[%test_eq: string] signature signature' ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be better to compare the signatures at type Signature.t, then you only have to encode the one you're returning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this originally but it's a bit complicated because we need to derive sexp and compare on too many things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can follow up and fix this in another PR if you can see an easy way to do so

signature
2 changes: 1 addition & 1 deletion src/lib/rosetta_lib/user_command_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module Partial = struct
let%map body =
match t.kind with
| `Payment ->
let%bind source_pk = pk_to_public_key ~context:"Source" t.receiver in
let%bind source_pk = pk_to_public_key ~context:"Source" t.source in
let%map amount =
Result.of_option t.amount
~error:
Expand Down