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

Cli: add a query for a range of pending snark work #16260

Open
wants to merge 2 commits into
base: al/snark-work-graphql
Choose a base branch
from
Open
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
97 changes: 67 additions & 30 deletions src/app/cli/src/init/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,38 @@ let to_signed_fee_exn sign magnitude =
let sgn = match sign with `PLUS -> Sgn.Pos | `MINUS -> Neg in
Currency.Fee.Signed.create ~sgn ~magnitude

let format_pending_snark_work ?(f = Fun.id) graphql_endpoint =
Copy link
Member

Choose a reason for hiding this comment

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

@mrmr1993 has sent me nits,about passing in functions like this instead of piping, but I don't really mind.

Deferred.map
(Graphql_client.query_exn
Graphql_queries.Pending_snark_work.(make @@ makeVariables ())
graphql_endpoint )
~f:(fun response ->
let lst =
[%to_yojson: Cli_lib.Graphql_types.Pending_snark_work.t]
(Array.map
~f:(fun bundle ->
Array.map bundle.workBundle ~f:(fun w ->
let fee_excess_left = w.fee_excess.feeExcessLeft in
{ Cli_lib.Graphql_types.Pending_snark_work.Work.work_id =
w.work_id
; fee_excess =
Currency.Amount.Signed.of_fee
(to_signed_fee_exn fee_excess_left.sign
fee_excess_left.feeMagnitude )
; supply_increase = w.supply_increase
; source_first_pass_ledger_hash =
w.source_first_pass_ledger_hash
; target_first_pass_ledger_hash =
w.target_first_pass_ledger_hash
; source_second_pass_ledger_hash =
w.source_second_pass_ledger_hash
; target_second_pass_ledger_hash =
w.target_second_pass_ledger_hash
} ) )
(f response.pendingSnarkWork) )
in
print_string (Yojson.Safe.to_string lst) )

let pending_snark_work =
let open Command.Param in
Command.async
Expand All @@ -1037,36 +1069,40 @@ let pending_snark_work =
yet"
(Cli_lib.Background_daemon.graphql_init (return ())
~f:(fun graphql_endpoint () ->
Deferred.map
(Graphql_client.query_exn
Graphql_queries.Pending_snark_work.(make @@ makeVariables ())
graphql_endpoint )
~f:(fun response ->
let lst =
[%to_yojson: Cli_lib.Graphql_types.Pending_snark_work.t]
(Array.map
~f:(fun bundle ->
Array.map bundle.workBundle ~f:(fun w ->
let fee_excess_left = w.fee_excess.feeExcessLeft in
{ Cli_lib.Graphql_types.Pending_snark_work.Work
.work_id = w.work_id
; fee_excess =
Currency.Amount.Signed.of_fee
(to_signed_fee_exn fee_excess_left.sign
fee_excess_left.feeMagnitude )
; supply_increase = w.supply_increase
; source_first_pass_ledger_hash =
w.source_first_pass_ledger_hash
; target_first_pass_ledger_hash =
w.target_first_pass_ledger_hash
; source_second_pass_ledger_hash =
w.source_second_pass_ledger_hash
; target_second_pass_ledger_hash =
w.target_second_pass_ledger_hash
} ) )
response.pendingSnarkWork )
in
print_string (Yojson.Safe.to_string lst) ) ) )
format_pending_snark_work graphql_endpoint ) )

let pending_snark_work_range =
let open Command.Param in
let start_idx =
flag "--start-idx" ~aliases:[ "start-idx" ]
~doc:"START_IDX first index of the range (included)"
(required Cli_lib.Arg_type.int16)
Copy link
Member

Choose a reason for hiding this comment

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

making this an int allows this to be negative, ik it's a cli command but still seems a bit foot gunny.

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 kept the int, but it is now checked in the function that the indexes are positive.

in
let end_idx =
flag "--end-idx" ~aliases:[ "end-idx" ]
~doc:"END_IDX first index of the range (excluded)"
(optional Cli_lib.Arg_type.int16)
Copy link
Member

Choose a reason for hiding this comment

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

nit
i still prefer to enforce this as positive through the
type system instead of runtime, but your logic is good so I will approve. I’m also generally more a fan of returning empty list when the user inputs an extremely high end index instead of treating as a none case, but again the logic is fine.

in
Command.async
~summary:
"Range of snark works in JSON format that are not available in the pool \
yet. Returns empty list if one of the indexes is negative."
(Cli_lib.Background_daemon.graphql_init (Args.zip2 start_idx end_idx)
~f:(fun graphql_endpoint (start_idx, end_idx) ->
let f pending_snark_work =
let len_response = Array.length pending_snark_work in
if start_idx < 0 || len_response <= start_idx then [||]
else
match end_idx with
| None ->
Array.subo ~pos:start_idx pending_snark_work
| Some end_idx when 0 <= start_idx && start_idx < end_idx ->
let len = min len_response end_idx - start_idx in
Array.sub ~pos:start_idx ~len pending_snark_work
| _ ->
[||]
in
format_pending_snark_work ~f graphql_endpoint ) )

let start_tracing =
let open Deferred.Let_syntax in
Expand Down Expand Up @@ -2473,6 +2509,7 @@ let advanced ~itn_features =
; ("pooled-zkapp-commands", pooled_zkapp_commands)
; ("snark-pool-list", snark_pool_list)
; ("pending-snark-work", pending_snark_work)
; ("pending-snark-work-range", pending_snark_work_range)
; ("compile-time-constants", compile_time_constants)
; ("node-status", node_status)
; ("visualization", Visualization.command_group)
Expand Down