Skip to content

Commit

Permalink
Merge pull request #11577 from MinaProtocol/feature/snarkyjs-compatib…
Browse files Browse the repository at this point in the history
…le-hashing

Fix timestamp precondition
  • Loading branch information
mitschabaude authored Aug 1, 2022
2 parents 8d4db19 + de91795 commit 74f5299
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 67 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ release
.env
.envrc
.direnv/

# pipenv
Pipfile
Pipfile.lock
2 changes: 1 addition & 1 deletion src/app/archive/archive_lib/graphql_query/base_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module Length = Make_numeric (Mina_numbers.Length)
module Block_time = Make_numeric (struct
type t = Block_time.t

let to_string = Block_time.to_string
let to_string = Block_time.to_string_exn

let of_string = Block_time.of_string_exn

Expand Down
12 changes: 8 additions & 4 deletions src/app/archive/archive_lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,12 @@ module Zkapp_timestamp_bounds = struct
let add_if_doesn't_exist (module Conn : CONNECTION)
(timestamp_bounds :
Block_time.t Mina_base.Zkapp_precondition.Closed_interval.t ) =
let timestamp_lower_bound = Block_time.to_string timestamp_bounds.lower in
let timestamp_upper_bound = Block_time.to_string timestamp_bounds.upper in
let timestamp_lower_bound =
Block_time.to_string_exn timestamp_bounds.lower
in
let timestamp_upper_bound =
Block_time.to_string_exn timestamp_bounds.upper
in
let value = { timestamp_lower_bound; timestamp_upper_bound } in
Mina_caqti.select_insert_into_cols ~select:("id", Caqti_type.int)
~table_name ~cols:(Fields.names, typ)
Expand Down Expand Up @@ -2695,7 +2699,7 @@ module Block = struct
|> Unsigned.UInt32.to_int64
; timestamp =
Protocol_state.blockchain_state protocol_state
|> Blockchain_state.timestamp |> Block_time.to_string
|> Blockchain_state.timestamp |> Block_time.to_string_exn
; chain_status
}
in
Expand Down Expand Up @@ -3014,7 +3018,7 @@ module Block = struct
block.global_slot_since_hard_fork |> Unsigned.UInt32.to_int64
; global_slot_since_genesis =
block.global_slot_since_genesis |> Unsigned.UInt32.to_int64
; timestamp = Block_time.to_string block.timestamp
; timestamp = Block_time.to_string_exn block.timestamp
; chain_status = Chain_status.to_string block.chain_status
}
in
Expand Down
2 changes: 1 addition & 1 deletion src/app/cli/src/init/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ let compile_time_constants =
`Assoc
[ ( "genesis_state_timestamp"
, `String
( Block_time.to_time
( Block_time.to_time_exn
consensus_constants.genesis_state_timestamp
|> Core.Time.to_string_iso8601_basic ~zone:Core.Time.Zone.utc
) )
Expand Down
4 changes: 2 additions & 2 deletions src/app/cli/src/tests/coda_processes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let net_configs n =
let offset (consensus_constants : Consensus.Constants.t) =
Core.Time.(
diff (now ())
(Block_time.to_time consensus_constants.genesis_state_timestamp))
(Block_time.to_time_exn consensus_constants.genesis_state_timestamp))

let local_configs ?block_production_interval
?(block_production_keys = Fn.const None)
Expand All @@ -71,7 +71,7 @@ let local_configs ?block_production_interval
Genesis_constants.genesis_timestamp_of_string timestamp
| None ->
(Lazy.force Precomputed_values.compiled_inputs).consensus_constants
.genesis_state_timestamp |> Block_time.to_time
.genesis_state_timestamp |> Block_time.to_time_exn
in
Core.Time.(diff (now ())) genesis_state_timestamp
in
Expand Down
2 changes: 1 addition & 1 deletion src/app/test_executive/snarkyjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module Make (Inputs : Intf.Test.Inputs_intf) = struct
| `Deploy ->
("deploy", "0")
| `Update ->
("update", "2")
("update", "1")
in
(* concurrently make/sign the deploy transaction and wait for the node to be ready *)
[%log info] "Running JS script with command $jscommand"
Expand Down
9 changes: 6 additions & 3 deletions src/lib/block_producer/block_producer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ let run ~logger ~vrf_evaluator ~prover ~verifier ~trust_system
Time.(
Span.to_ms
@@ diff (now ())
@@ Block_time.to_time scheduled_time)) ;
@@ Block_time.to_time_exn scheduled_time)) ;
let%bind.Async.Deferred () =
Strict_pipe.Writer.write transition_writer breadcrumb
in
Expand Down Expand Up @@ -1297,7 +1297,9 @@ let run_precomputed ~logger ~verifier ~trust_system ~time_controller
Mina_metrics.Block_producer.(
Block_production_delay_histogram.observe block_production_delay
Time.(
Span.to_ms @@ diff (now ()) @@ Block_time.to_time scheduled_time)) ;
Span.to_ms
@@ diff (now ())
@@ Block_time.to_time_exn scheduled_time)) ;
let%bind.Async.Deferred () =
Strict_pipe.Writer.write transition_writer breadcrumb
in
Expand Down Expand Up @@ -1352,7 +1354,8 @@ let run_precomputed ~logger ~verifier ~trust_system ~time_controller
| Some (precomputed_block, precomputed_blocks) ->
let new_time_offset =
Time.diff (Time.now ())
(Block_time.to_time precomputed_block.Precomputed.scheduled_time)
(Block_time.to_time_exn
precomputed_block.Precomputed.scheduled_time )
in
[%log info]
"Changing time offset from $old_time_offset to $new_time_offset"
Expand Down
24 changes: 20 additions & 4 deletions src/lib/block_time/block_time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ module Time = struct
let to_time_ns_span s =
Time_ns.Span.of_ms (Int64.to_float (UInt64.to_int64 s))

let of_time_ns_span ns : t =
let int64_ns = ns |> Time_ns.Span.to_int63_ns |> Int63.to_int64 in
(* convert to milliseconds *)
Int64.(int64_ns / 1_000_000L) |> UInt64.of_int64

let to_string_hum s = to_time_ns_span s |> Time_ns.Span.to_string_hum

let to_ms = UInt64.to_int64
Expand Down Expand Up @@ -202,9 +207,11 @@ module Time = struct
UInt64.of_int64
(Int64.of_float (Time.Span.to_ms (Time.to_span_since_epoch t)))

let to_time t =
Time.of_span_since_epoch
(Time.Span.of_ms (Int64.to_float (UInt64.to_int64 t)))
(* TODO: Time.t can't hold the full uint64 range, so this can fail for large t *)
let to_time_exn t =
let t_int64 = UInt64.to_int64 t in
if Int64.(t_int64 < zero) then failwith "converting to negative timestamp" ;
Time.of_span_since_epoch (Time.Span.of_ms (Int64.to_float t_int64))

[%%if time_offsets]

Expand Down Expand Up @@ -249,7 +256,16 @@ module Time = struct

let to_uint64 : t -> UInt64.t = to_span_since_epoch

let to_string = Fn.compose Int64.to_string to_int64
(* TODO: this can fail if the input has more than 63 bits, because it would be serialized to a negative number string *)
let to_string_exn t =
let t_int64 = UInt64.to_int64 t in
if Int64.(t_int64 < zero) then failwith "converting to negative timestamp" ;
Int64.to_string t_int64

let of_time_ns ns : t =
let int64_ns = ns |> Time_ns.to_int63_ns_since_epoch |> Int63.to_int64 in
(* convert to milliseconds *)
Int64.(int64_ns / 1_000_000L) |> UInt64.of_int64

[%%if time_offsets]

Expand Down
8 changes: 6 additions & 2 deletions src/lib/block_time/block_time.mli
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ module Time : sig

val to_time_ns_span : t -> Core_kernel.Time_ns.Span.t

val of_time_ns_span : Core_kernel.Time_ns.Span.t -> t

val to_string_hum : t -> string

val to_ms : t -> Int64.t
Expand Down Expand Up @@ -182,7 +184,7 @@ module Time : sig

val of_time : Time.t -> t

val to_time : t -> Time.t
val to_time_exn : t -> Time.t

val now : Controller.t -> t

Expand All @@ -194,7 +196,9 @@ module Time : sig

val to_uint64 : t -> Unsigned.UInt64.t

val to_string : t -> string
val of_time_ns : Time_ns.t -> t

val to_string_exn : t -> string

(** Strip time offset *)
val to_string_system_time : Controller.t -> t -> string
Expand Down
2 changes: 1 addition & 1 deletion src/lib/daemon_rpcs/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ module Status = struct
float_of_int i |> Time.Span.of_ms |> Time.Span.to_string
in
(* Time.to_string is safe here because this is for display. *)
let time_to_string = Fn.compose Time.to_string Block_time.to_time in
let time_to_string = Fn.compose Time.to_string Block_time.to_time_exn in
let render conf =
let fmt_field name op field = (name, op (Field.get field conf)) in
Consensus.Configuration.Fields.to_list
Expand Down
11 changes: 6 additions & 5 deletions src/lib/mina_graphql/mina_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module Reflection = struct

let nn_time a x =
reflect
(fun t -> Block_time.to_time t |> Time.to_string)
(fun t -> Block_time.to_time_exn t |> Time.to_string)
~typ:(non_null string) a x

let nn_catchup_status a x =
Expand Down Expand Up @@ -239,14 +239,14 @@ module Types = struct
let constants =
(Mina_lib.config coda).precomputed_values.consensus_constants
in
Block_time.to_string @@ C.start_time ~constants global_slot )
Block_time.to_string_exn @@ C.start_time ~constants global_slot )
; field "endTime" ~typ:(non_null string)
~args:Arg.[]
~resolve:(fun { ctx = coda; _ } global_slot ->
let constants =
(Mina_lib.config coda).precomputed_values.consensus_constants
in
Block_time.to_string @@ C.end_time ~constants global_slot )
Block_time.to_string_exn @@ C.end_time ~constants global_slot )
] )

let consensus_time_with_global_slot_since_genesis =
Expand Down Expand Up @@ -653,7 +653,7 @@ module Types = struct
let timestamp =
Mina_state.Blockchain_state.timestamp blockchain_state
in
Block_time.to_string timestamp )
Block_time.to_string_exn timestamp )
; field "utcDate" ~typ:(non_null string)
~doc:
(Doc.date
Expand Down Expand Up @@ -2264,7 +2264,8 @@ module Types = struct
with exn -> Error (Exn.to_string exn) )
| _ ->
Error "Expected string for block time" )
~to_json:(function (t : input) -> `String (Block_time.to_string t))
~to_json:(function
| (t : input) -> `String (Block_time.to_string_exn t) )
end

module Length = struct
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_lib/mina_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ let check_and_stop_daemon t ~wait =
| Daemon_rpcs.Types.Status.Next_producer_timing.Check_again tm
| Produce { time = tm; _ }
| Produce_now { time = tm; _ } ->
let tm = Block_time.to_time tm in
let tm = Block_time.to_time_exn tm in
(*Assuming it takes at most 1hr to bootstrap and catchup*)
let next_block =
Time.add tm
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mina_lib/mina_subscriptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ let create ~logger ~constraint_constants ~wallets ~new_blocks
Time.(
Span.to_ms
(diff (now ())
(Block_time.to_time scheduled_time) ))
(Block_time.to_time_exn scheduled_time) ))
)
] ;
Mina_block.Precomputed.to_yojson precomputed_block )
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mina_state/blockchain_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ let display
; local_state = Local_state.display local_state
}
; timestamp =
Time.to_string_trimmed ~zone:Time.Zone.utc (Block_time.to_time timestamp)
Time.to_string_trimmed ~zone:Time.Zone.utc
(Block_time.to_time_exn timestamp)
; body_reference =
Visualization.display_prefix_of_string
@@ Consensus.Body_reference.to_hex body_reference
Expand Down
30 changes: 10 additions & 20 deletions src/lib/network_pool/indexed_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,7 @@ let expired_by_predicate (t : t) :
let predicate = Party.protocol_state_precondition party in
match predicate.timestamp with
| Check { upper; _ } ->
let upper =
Block_time.to_time upper
|> Time_ns.of_time_float_round_nearest_microsecond
in
Time_ns.(upper < expiry_time)
Block_time.(upper < of_time_ns expiry_time)
| _ ->
false ) )
|> Sequence.map ~f:fst
Expand Down Expand Up @@ -960,10 +956,11 @@ let get_highest_fee :
*)

module Add_from_gossip_exn (M : Writer_result.S) = struct
let check_timestamp_predicate expiry_ns (user_command : User_command.t) : bool
=
let current_time = Time_ns.now () in
let expiry_time = Time_ns.(sub current_time expiry_ns) in
let check_timestamp_predicate (expiry_ns : Time_ns.Span.t)
(user_command : User_command.t) : bool =
let expiry = Block_time.Span.of_time_ns_span expiry_ns in
let current_time = Block_time.of_time_ns @@ Time_ns.now () in
let expiry_time = Block_time.sub current_time expiry in
match user_command with
| User_command.Signed_command _ ->
true
Expand All @@ -973,21 +970,14 @@ module Add_from_gossip_exn (M : Writer_result.S) = struct
let predicate = Party.protocol_state_precondition party in
match predicate.timestamp with
| Check { lower; upper } ->
let lower =
Block_time.to_time lower
|> Time_ns.of_time_float_round_nearest_microsecond
in
let upper =
Block_time.to_time upper
|> Time_ns.of_time_float_round_nearest_microsecond
in
(*Timestamp bounds are compared against slot start time of
the most recent block and that could be any number of slots
old. So give the transaction more time to be included*)
Time_ns.(upper > expiry_time)
&& Time_ns.(lower < add current_time expiry_ns)
Block_time.(upper <= expiry_time)
|| Block_time.(lower >= add current_time expiry)
| _ ->
true )
false )
|> not

let rec add_from_gossip_exn :
config:Config.t
Expand Down
2 changes: 1 addition & 1 deletion src/lib/network_pool/transaction_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2364,7 +2364,7 @@ let%test_module _ =
of_time_exn ~constants:consensus_constants current_time
|> end_time ~constants:consensus_constants)
in
at (Block_time.to_time slot_end)
at (Block_time.to_time_exn slot_end)
in
let curr_slot = current_global_slot () in
let slot_padding = Mina_numbers.Global_slot.of_int padding in
Expand Down
4 changes: 2 additions & 2 deletions src/lib/node_status_service/node_status_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ let start ~logger ~node_status_url ~transition_frontier ~sync_status ~network
{ hash
; sender
; received_at =
Time.to_string (Block_time.to_time received_at)
Time.to_string (Block_time.to_time_exn received_at)
; is_valid = false
; reason_for_rejection = Some reason_for_rejection
} )
Expand All @@ -345,7 +345,7 @@ let start ~logger ~node_status_url ~transition_frontier ~sync_status ~network
{ hash
; sender
; received_at =
Time.to_string (Block_time.to_time received_at)
Time.to_string (Block_time.to_time_exn received_at)
; is_valid = true
; reason_for_rejection = None
} )
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snarky_js_bindings/snarkyjs
2 changes: 1 addition & 1 deletion src/lib/snarky_js_bindings/test_module/simple-zkapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let [command, zkappKeyBase58, feePayerKeyBase58, feePayerNonce] =
process.argv.slice(2);
zkappKeyBase58 ||= PrivateKey.random().toBase58();
feePayerKeyBase58 ||= PrivateKey.random().toBase58();
feePayerNonce ||= command === "update" ? "2" : "0";
feePayerNonce ||= command === "update" ? "1" : "0";
console.log(
`simple-zkapp.js: Running "${command}" with zkapp key ${zkappKeyBase58}, fee payer key ${feePayerKeyBase58} and fee payer nonce ${feePayerNonce}`
);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/transition_handler/block_sink.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ let push sink (`Transition e, `Time_received tm, `Valid_cb cb) =
let%bind () = on_push () in
Mina_metrics.(Counter.inc_one Network.gossip_messages_received) ;
let state = Envelope.Incoming.data e in
let processing_start_time = Block_time.(now time_controller |> to_time) in
let processing_start_time =
Block_time.(now time_controller |> to_time_exn)
in
don't_wait_for
( match%map Mina_net2.Validation_callback.await cb with
| Some `Accept ->
let processing_time_span =
Time.diff
Block_time.(now time_controller |> to_time)
Block_time.(now time_controller |> to_time_exn)
processing_start_time
in
Mina_metrics.Block_latency.(
Expand All @@ -68,11 +70,11 @@ let push sink (`Transition e, `Time_received tm, `Valid_cb cb) =
() ) ;
Perf_histograms.add_span ~name:"external_transition_latency"
(Core.Time.abs_diff
Block_time.(now time_controller |> to_time)
Block_time.(now time_controller |> to_time_exn)
Mina_block.(
header state |> Header.protocol_state
|> Protocol_state.blockchain_state |> Blockchain_state.timestamp
|> Block_time.to_time) ) ;
|> Block_time.to_time_exn) ) ;
Mina_metrics.(Gauge.inc_one Network.new_state_received) ;
if log_gossip_heard then
[%str_log info]
Expand Down
Loading

0 comments on commit 74f5299

Please sign in to comment.