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

Standalone snarkwork process to submit proofs via graphql #16367

Open
wants to merge 2 commits into
base: feature/graphql-add-snark-work
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions graphql_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
},
{
"kind": "SCALAR",
"name": "ProofBundle",
"name": "ProofBundleInput",
"description": "Proof bundle for a given spec in json format",
"fields": null,
"inputFields": null,
Expand Down Expand Up @@ -5514,7 +5514,7 @@
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ProofBundle",
"name": "ProofBundleInput",
"ofType": null
}
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/cli_lib/arg_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ let receipt_chain_hash =
let peer : Host_and_port.t Command.Arg_type.t =
Command.Arg_type.create (fun s -> Host_and_port.of_string s)

let uri : Uri.t Command.Arg_type.t = Command.Arg_type.create Uri.of_string

let global_slot =
Command.Arg_type.map Command.Param.int
~f:Mina_numbers.Global_slot_since_genesis.of_int
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_graphql/mina_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ module Mutations = struct
| Ok
( `Broadcasted
, Network_pool.Snark_pool.Resource_pool.Diff.Add_solved_work _
, _ ) ->
, () ) ->
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
, () ) ->
, _ ) ->

It looks like this change triggers an error when building:

File "src/lib/mina_graphql/mina_graphql.ml", line 814, characters 14-16:
814 |             , () ) ->
                    ^^
Error: This pattern matches values of type unit
       but a pattern was expected which matches values of type
         Network_pool.Snark_pool.Resource_pool.Diff.rejected

Ok "Accepted"
| Error err ->
Error (Error.to_string_hum err)
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mina_graphql/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,8 @@ module Input = struct
type input = Ledger_proof.t Snark_work_lib.Work.Result_without_metrics.t

let arg_typ =
scalar "ProofBundle" ~doc:"Proof bundle for a given spec in json format"
scalar "ProofBundleInput"
~doc:"Proof bundle for a given spec in json format"
~coerce:(fun json ->
let json = Utils.to_yojson json in
Snark_work_lib.Work.Result_without_metrics.of_yojson
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snark_work_lib/work.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ module Result_without_metrics = struct
; prover : Signature_lib.Public_key.Compressed.t
; fee : Currency.Fee.t
}
[@@deriving yojson]
[@@deriving yojson, sexp]
end
6 changes: 5 additions & 1 deletion src/lib/snark_worker/standalone/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
async_unix
async_kernel
sexplib0
uri
;; local libraries
mina_base
currency
Expand All @@ -20,6 +21,9 @@
genesis_constants
signature_lib
transaction_snark
graphql_lib
mina_graphql
)
(instrumentation (backend bisect_ppx))
(preprocess (pps ppx_let ppx_custom_printf ppx_version)))
(preprocessor_deps ../../../graphql-ppx-config.inc ../../../../graphql_schema.json)
(preprocess (pps ppx_let ppx_custom_printf ppx_version graphql_ppx -- %{read-lines:../../../graphql-ppx-config.inc})))
147 changes: 134 additions & 13 deletions src/lib/snark_worker/standalone/run_snark_worker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,82 @@ open Core_kernel
open Async
module Prod = Snark_worker__Prod.Inputs

module Graphql_client = Graphql_lib.Client.Make (struct
let preprocess_variables_string = Fn.id

let headers = String.Map.empty
end)

module Encoders = Mina_graphql.Types.Input
module Scalars = Graphql_lib.Scalars

module Send_proof_mutation =
[%graphql
({|
mutation ($input: ProofBundleInput!) @encoders(module: "Encoders"){
sendProofBundle(input: $input)
}
|}
[@encoders Encoders] )]

let submit_graphql input graphql_endpoint =
let obj = Send_proof_mutation.(make @@ makeVariables ~input ()) in
match%bind Graphql_client.query obj graphql_endpoint with
| Ok _s ->
Caml.Format.printf "Successfully generated proof bundle mutation" ;
exit 0
| Error (`Failed_request s) ->
Caml.Format.printf !"Request failed: %s" s ;
exit 1
| Error (`Graphql_error s) ->
Caml.Format.printf "Graphql error: %s" s ;
exit 1

let perform (s : Prod.Worker_state.t) ~fee ~public_key
(spec :
(Transaction_witness.t, Ledger_proof.t) Snark_work_lib.Work.Single.Spec.t
One_or_two.t ) =
One_or_two.Deferred_result.map spec ~f:(fun w ->
let open Deferred.Or_error.Let_syntax in
let%map proof, time =
Prod.perform_single s
~message:(Mina_base.Sok_message.create ~fee ~prover:public_key)
w
in
( proof
, (time, match w with Transition _ -> `Transition | Merge _ -> `Merge) ) )
|> Deferred.Or_error.map ~f:(function
| `One (proof1, _) ->
{ Snark_work_lib.Work.Result_without_metrics.proofs = `One proof1
; statements =
One_or_two.map spec ~f:Snark_work_lib.Work.Single.Spec.statement
; prover = public_key
; fee
}
| `Two ((proof1, _), (proof2, _)) ->
{ Snark_work_lib.Work.Result_without_metrics.proofs =
`Two (proof1, proof2)
; statements =
One_or_two.map spec ~f:Snark_work_lib.Work.Single.Spec.statement
; prover = public_key
; fee
} )
Comment on lines +49 to +64
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
|> Deferred.Or_error.map ~f:(function
| `One (proof1, _) ->
{ Snark_work_lib.Work.Result_without_metrics.proofs = `One proof1
; statements =
One_or_two.map spec ~f:Snark_work_lib.Work.Single.Spec.statement
; prover = public_key
; fee
}
| `Two ((proof1, _), (proof2, _)) ->
{ Snark_work_lib.Work.Result_without_metrics.proofs =
`Two (proof1, proof2)
; statements =
One_or_two.map spec ~f:Snark_work_lib.Work.Single.Spec.statement
; prover = public_key
; fee
} )
|> Deferred.Or_error.map ~f:(fun proofs_and_time ->
{ Snark_work_lib.Work.Result_without_metrics.proofs =
One_or_two.map proofs_and_time ~f:fst
; statements =
One_or_two.map spec ~f:Snark_work_lib.Work.Single.Spec.statement
; prover = public_key
; fee
} )


let command =
let open Command.Let_syntax in
Command.async ~summary:"Run snark worker directly"
(let%map_open spec =
flag "--spec-sexp" ~doc:""
(required (sexp_conv Prod.single_spec_of_sexp))
(let%map_open spec_json =
flag "--spec-json"
~doc:
"Snark work spec in json format (preferred over sexp format if both \
are passed)"
(optional string)
and spec_sexp =
flag "--spec-sexp"
~doc:
"Snark work spec in sexp format(json format is preferred over sexp \
if both are passed)"
(optional string)
and config_file = Cli_lib.Flag.config_files
and cli_proof_level =
flag "--proof-level" ~doc:""
Expand All @@ -17,6 +87,23 @@ let command =
; ("Check", Check)
; ("None", No_check)
] ) )
and snark_work_fee =
flag "--snark-worker-fee" ~aliases:[ "snark-worker-fee" ]
~doc:
(sprintf
"FEE Amount a worker wants to get compensated for generating a \
snark proof" )
(optional Cli_lib.Arg_type.txn_fee)
and snark_worker_key =
flag "--snark-worker-public-key"
~aliases:[ "snark-worker-public-key" ]
~doc:
(sprintf "PUBLICKEY Run the SNARK worker with this public key. %s"
Cli_lib.Default.receiver_key_warning )
(optional Cli_lib.Arg_type.public_key_compressed)
and proof_submission_graphql_endpoint =
flag "--graphql-uri" ~doc:"Graphql endpoint to submit proofs"
(optional Cli_lib.Arg_type.uri)
in
fun () ->
let open Async in
Expand All @@ -32,17 +119,51 @@ let command =
let%bind worker_state =
Prod.Worker_state.create ~constraint_constants ~proof_level ()
in
let public_key = fst Key_gen.Sample_keypairs.genesis_winner in
let fee = Currency.Fee.of_nanomina_int_exn 10 in
let message = Mina_base.Sok_message.create ~fee ~prover:public_key in
match%bind Prod.perform_single worker_state ~message spec with
| Ok (proof, time) ->
let spec =
match spec_json with
| Some json -> (
match
Yojson.Safe.from_string json
|> One_or_two.of_yojson
(Snark_work_lib.Work.Single.Spec.of_yojson
Transaction_witness.of_yojson Ledger_proof.of_yojson )
with
| Ok spec ->
spec
| Error e ->
failwith (sprintf "Failed to read json spec. Error: %s" e) )
| None -> (
match spec_sexp with
| Some spec ->
One_or_two.t_of_sexp
(Snark_work_lib.Work.Single.Spec.t_of_sexp
Transaction_witness.t_of_sexp Ledger_proof.t_of_sexp )
(Sexp.of_string spec)
| None ->
failwith "Provide a spec either in json or sexp format" )
in
let public_key =
Option.value
~default:(fst Key_gen.Sample_keypairs.genesis_winner)
snark_worker_key
in
let fee =
Option.value
~default:(Currency.Fee.of_nanomina_int_exn 10)
snark_work_fee
in
match%bind perform worker_state ~fee ~public_key spec with
| Ok result -> (
Caml.Format.printf
!"@[<v>Successfully proved in %{sexp: Time.Span.t}.@,\
Proof was:@,\
%{sexp: Transaction_snark.t}@]@."
time proof ;
exit 0
!"@[<v>Successfully proved. Result: \n\
\ %{sexp: Ledger_proof.t \
Snark_work_lib.Work.Result_without_metrics.t}@]@."
result ;
match proof_submission_graphql_endpoint with
| Some endpoint ->
submit_graphql result endpoint
| _ ->
Deferred.unit )
| Error err ->
Caml.Format.printf
!"Proving failed with error: %s@."
Expand Down