From b9c3d954062e90218a10b7921293368a519b9f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 17 Nov 2016 02:08:19 +0100 Subject: [PATCH] Proto: use integer comparison while checking proof-of-work. --- src/client/embedded/bootstrap/client_proto_rpcs.mli | 2 +- src/proto/bootstrap/constants_repr.ml | 9 +++++---- src/proto/bootstrap/mining.ml | 12 +++--------- src/proto/bootstrap/mining.mli | 2 +- src/proto/bootstrap/services.ml | 2 +- src/proto/bootstrap/services_registration.ml | 3 ++- src/proto/bootstrap/tezos_context.mli | 2 +- src/proto/environment/mBytes.mli | 3 --- src/utils/mBytes.ml | 3 --- src/utils/mBytes.mli | 3 --- 10 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/client/embedded/bootstrap/client_proto_rpcs.mli b/src/client/embedded/bootstrap/client_proto_rpcs.mli index 0cbff7de6..d0fe8eaab 100644 --- a/src/client/embedded/bootstrap/client_proto_rpcs.mli +++ b/src/client/embedded/bootstrap/client_proto_rpcs.mli @@ -29,7 +29,7 @@ module Constants : sig val first_free_mining_slot: block -> int32 tzresult Lwt.t val max_signing_slot: block -> int tzresult Lwt.t val instructions_per_transaction: block -> int tzresult Lwt.t - val stamp_threshold: block -> int tzresult Lwt.t + val stamp_threshold: block -> int64 tzresult Lwt.t end module Context : sig diff --git a/src/proto/bootstrap/constants_repr.ml b/src/proto/bootstrap/constants_repr.ml index 605d01df3..8468a376c 100644 --- a/src/proto/bootstrap/constants_repr.ml +++ b/src/proto/bootstrap/constants_repr.ml @@ -39,7 +39,7 @@ type constants = { first_free_mining_slot: int32 ; max_signing_slot: int ; instructions_per_transaction: int ; - proof_of_work_threshold: int ; + proof_of_work_threshold: int64 ; } let default = { @@ -56,7 +56,8 @@ let default = { first_free_mining_slot = 16l ; max_signing_slot = 15 ; instructions_per_transaction = 16 * 1024 ; - proof_of_work_threshold = 8 ; + proof_of_work_threshold = + Int64.(lognot (sub (shift_left 1L 56) 1L)) ; } let opt (=) def v = if def = v then None else Some v @@ -95,7 +96,7 @@ let constants_encoding = opt Int.(=) default.instructions_per_transaction c.instructions_per_transaction and proof_of_work_threshold = - opt Int.(=) + opt Int64.(=) default.proof_of_work_threshold c.proof_of_work_threshold in ( cycle_length, @@ -144,7 +145,7 @@ let constants_encoding = (opt "first_free_mining_slot" int32) (opt "max_signing_slot" int31) (opt "instructions_per_transaction" int31) - (opt "proof_of_work_threshold" int31) + (opt "proof_of_work_threshold" int64) ) type error += Constant_read of exn diff --git a/src/proto/bootstrap/mining.ml b/src/proto/bootstrap/mining.ml index 5a4693bca..70f3b9313 100644 --- a/src/proto/bootstrap/mining.ml +++ b/src/proto/bootstrap/mining.ml @@ -132,16 +132,10 @@ let first_endorsement_slots endorsement_priorities ctxt level >>=? fun delegate_list -> select_delegate delegate delegate_list max_priority - let check_hash hash stamp_threshold = - let bytes = Block_hash.to_bytes hash in - let len = MBytes.length bytes * 8 in - try - for i = len - 1 downto (len - stamp_threshold) do - if MBytes.get_bool bytes i then raise Exit - done; - true - with Exit -> false + let bytes = Block_hash.to_raw hash in + let word = String.get_int64 bytes 0 in + Compare.Uint64.(word < stamp_threshold) let check_header_hash {Block.shell;proto;signature} stamp_threshold = let hash = diff --git a/src/proto/bootstrap/mining.mli b/src/proto/bootstrap/mining.mli index 0e56dbebd..57fe5bae3 100644 --- a/src/proto/bootstrap/mining.mli +++ b/src/proto/bootstrap/mining.mli @@ -70,7 +70,7 @@ val first_endorsement_slots: val check_signature: context -> Block.header -> public_key_hash -> unit tzresult Lwt.t -val check_hash: Block_hash.t -> int -> bool +val check_hash: Block_hash.t -> int64 -> bool val check_proof_of_work_stamp: context -> Block.header -> unit tzresult Lwt.t diff --git a/src/proto/bootstrap/services.ml b/src/proto/bootstrap/services.ml index 7054f08d8..a1b7239bd 100644 --- a/src/proto/bootstrap/services.ml +++ b/src/proto/bootstrap/services.ml @@ -97,7 +97,7 @@ module Constants = struct ~description: "Stamp threshold" ~input: empty ~output: (wrap_tzerror @@ - describe ~title: "proof_of_work threshold" int31) + describe ~title: "proof_of_work threshold" int64) RPC.Path.(custom_root / "constants" / "proof_of_work_threshold") let errors custom_root = diff --git a/src/proto/bootstrap/services_registration.ml b/src/proto/bootstrap/services_registration.ml index edbfeea33..6175cc57a 100644 --- a/src/proto/bootstrap/services_registration.ml +++ b/src/proto/bootstrap/services_registration.ml @@ -79,7 +79,8 @@ let () = let proof_of_work_threshold ctxt = return @@ Constants.proof_of_work_threshold ctxt -let () = register0 Services.Constants.proof_of_work_threshold proof_of_work_threshold +let () = + register0 Services.Constants.proof_of_work_threshold proof_of_work_threshold let () = register1_noctxt Services.Constants.errors diff --git a/src/proto/bootstrap/tezos_context.mli b/src/proto/bootstrap/tezos_context.mli index 2da485bf6..fa8060231 100644 --- a/src/proto/bootstrap/tezos_context.mli +++ b/src/proto/bootstrap/tezos_context.mli @@ -185,7 +185,7 @@ module Constants : sig val first_free_mining_slot: context -> int32 val max_signing_slot: context -> int val instructions_per_transaction: context -> int - val proof_of_work_threshold: context -> int + val proof_of_work_threshold: context -> int64 end diff --git a/src/proto/environment/mBytes.mli b/src/proto/environment/mBytes.mli index 12df8946f..2bcb15be0 100644 --- a/src/proto/environment/mBytes.mli +++ b/src/proto/environment/mBytes.mli @@ -41,9 +41,6 @@ val substring: t -> int -> int -> string val get_char: t -> int -> char (** [get_char buff i] reads 1 byte at offset i as a char *) -val get_bool: t -> int -> bool -(** [get_bool buff i] reads 1 bit at offset i as an unsigned int bit. *) - val get_uint8: t -> int -> int (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 bits. i.e. It returns a value between 0 and 2^8-1 *) diff --git a/src/utils/mBytes.ml b/src/utils/mBytes.ml index 53b7eed6f..3c3a69a9f 100644 --- a/src/utils/mBytes.ml +++ b/src/utils/mBytes.ml @@ -73,9 +73,6 @@ let substring src srcoff len = include EndianBigstring.BigEndian -let get_bool s off = - ((get_uint8 s (off / 8)) lsr (off mod 8)) land 1 = 1 - let of_float f = let buf = create 8 in set_float buf 0 f; diff --git a/src/utils/mBytes.mli b/src/utils/mBytes.mli index 18f5b65ea..93a55267a 100644 --- a/src/utils/mBytes.mli +++ b/src/utils/mBytes.mli @@ -49,9 +49,6 @@ val substring: t -> int -> int -> string val get_char: t -> int -> char (** [get_char buff i] reads 1 byte at offset i as a char *) -val get_bool: t -> int -> bool -(** [get_bool buff i] reads 1 bit at offset i as an unsigned int bit. *) - val get_uint8: t -> int -> int (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 bits. i.e. It returns a value between 0 and 2^8-1 *)