Proto: use integer comparison while checking proof-of-work.

This commit is contained in:
Grégoire Henry 2016-11-17 02:08:19 +01:00
parent c842140f74
commit b9c3d95406
10 changed files with 14 additions and 27 deletions

View File

@ -29,7 +29,7 @@ module Constants : sig
val first_free_mining_slot: block -> int32 tzresult Lwt.t val first_free_mining_slot: block -> int32 tzresult Lwt.t
val max_signing_slot: block -> int tzresult Lwt.t val max_signing_slot: block -> int tzresult Lwt.t
val instructions_per_transaction: 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 end
module Context : sig module Context : sig

View File

@ -39,7 +39,7 @@ type constants = {
first_free_mining_slot: int32 ; first_free_mining_slot: int32 ;
max_signing_slot: int ; max_signing_slot: int ;
instructions_per_transaction: int ; instructions_per_transaction: int ;
proof_of_work_threshold: int ; proof_of_work_threshold: int64 ;
} }
let default = { let default = {
@ -56,7 +56,8 @@ let default = {
first_free_mining_slot = 16l ; first_free_mining_slot = 16l ;
max_signing_slot = 15 ; max_signing_slot = 15 ;
instructions_per_transaction = 16 * 1024 ; 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 let opt (=) def v = if def = v then None else Some v
@ -95,7 +96,7 @@ let constants_encoding =
opt Int.(=) opt Int.(=)
default.instructions_per_transaction c.instructions_per_transaction default.instructions_per_transaction c.instructions_per_transaction
and proof_of_work_threshold = and proof_of_work_threshold =
opt Int.(=) opt Int64.(=)
default.proof_of_work_threshold c.proof_of_work_threshold default.proof_of_work_threshold c.proof_of_work_threshold
in in
( cycle_length, ( cycle_length,
@ -144,7 +145,7 @@ let constants_encoding =
(opt "first_free_mining_slot" int32) (opt "first_free_mining_slot" int32)
(opt "max_signing_slot" int31) (opt "max_signing_slot" int31)
(opt "instructions_per_transaction" int31) (opt "instructions_per_transaction" int31)
(opt "proof_of_work_threshold" int31) (opt "proof_of_work_threshold" int64)
) )
type error += Constant_read of exn type error += Constant_read of exn

View File

@ -132,16 +132,10 @@ let first_endorsement_slots
endorsement_priorities ctxt level >>=? fun delegate_list -> endorsement_priorities ctxt level >>=? fun delegate_list ->
select_delegate delegate delegate_list max_priority select_delegate delegate delegate_list max_priority
let check_hash hash stamp_threshold = let check_hash hash stamp_threshold =
let bytes = Block_hash.to_bytes hash in let bytes = Block_hash.to_raw hash in
let len = MBytes.length bytes * 8 in let word = String.get_int64 bytes 0 in
try Compare.Uint64.(word < stamp_threshold)
for i = len - 1 downto (len - stamp_threshold) do
if MBytes.get_bool bytes i then raise Exit
done;
true
with Exit -> false
let check_header_hash {Block.shell;proto;signature} stamp_threshold = let check_header_hash {Block.shell;proto;signature} stamp_threshold =
let hash = let hash =

View File

@ -70,7 +70,7 @@ val first_endorsement_slots:
val check_signature: val check_signature:
context -> Block.header -> public_key_hash -> unit tzresult Lwt.t 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: val check_proof_of_work_stamp:
context -> Block.header -> unit tzresult Lwt.t context -> Block.header -> unit tzresult Lwt.t

View File

@ -97,7 +97,7 @@ module Constants = struct
~description: "Stamp threshold" ~description: "Stamp threshold"
~input: empty ~input: empty
~output: (wrap_tzerror @@ ~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") RPC.Path.(custom_root / "constants" / "proof_of_work_threshold")
let errors custom_root = let errors custom_root =

View File

@ -79,7 +79,8 @@ let () =
let proof_of_work_threshold ctxt = let proof_of_work_threshold ctxt =
return @@ Constants.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 () = let () =
register1_noctxt Services.Constants.errors register1_noctxt Services.Constants.errors

View File

@ -185,7 +185,7 @@ module Constants : sig
val first_free_mining_slot: context -> int32 val first_free_mining_slot: context -> int32
val max_signing_slot: context -> int val max_signing_slot: context -> int
val instructions_per_transaction: context -> int val instructions_per_transaction: context -> int
val proof_of_work_threshold: context -> int val proof_of_work_threshold: context -> int64
end end

View File

@ -41,9 +41,6 @@ val substring: t -> int -> int -> string
val get_char: t -> int -> char val get_char: t -> int -> char
(** [get_char buff i] reads 1 byte at offset i as a 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 val get_uint8: t -> int -> int
(** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 (** [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 *) bits. i.e. It returns a value between 0 and 2^8-1 *)

View File

@ -73,9 +73,6 @@ let substring src srcoff len =
include EndianBigstring.BigEndian 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 of_float f =
let buf = create 8 in let buf = create 8 in
set_float buf 0 f; set_float buf 0 f;

View File

@ -49,9 +49,6 @@ val substring: t -> int -> int -> string
val get_char: t -> int -> char val get_char: t -> int -> char
(** [get_char buff i] reads 1 byte at offset i as a 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 val get_uint8: t -> int -> int
(** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8 (** [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 *) bits. i.e. It returns a value between 0 and 2^8-1 *)