Merge branch 'uint64_comparison_in_proof_of_work' into 'master'

Use unsigned comparison in proof of work

See merge request !114
This commit is contained in:
Grégoire Henry 2016-11-21 09:55:46 +01:00
commit 46cca127a6
17 changed files with 191 additions and 56 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

@ -134,8 +134,16 @@ module Make(Param : sig val name: string end)() = struct
module Compare = Compare module Compare = Compare
module Array = Array module Array = Array
module List = List module List = List
module Bytes = Bytes module Bytes = struct
module String = String include Bytes
include EndianBytes.BigEndian
module LE = EndianBytes.LittleEndian
end
module String = struct
include String
include EndianString.BigEndian
module LE = EndianString.LittleEndian
end
module Set = Set module Set = Set
module Map = Map module Map = Map
module Int32 = Int32 module Int32 = Int32

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

@ -141,23 +141,18 @@ let logsr _ (Int va) (Int vb) =
Int (Int64.shift_right_logical va (Int64.to_int vb)) Int (Int64.shift_right_logical va (Int64.to_int vb))
(* sign aware comparison *) (* sign aware comparison *)
let signed_compare va vb =
Compare.Int64.(if va = vb then 0 else if va > vb then 1 else -1)
let unsigned_compare va vb =
Compare.Int64.(if va >= 0L then if vb >= 0L then signed_compare va vb else -1
else if vb >= 0L then 1 else signed_compare va vb)
let compare let compare
: type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val -> (signed, sixtyfour) int_val : type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val -> (signed, sixtyfour) int_val
= fun kind (Int va) (Int vb) -> = fun kind (Int va) (Int vb) ->
let cmp = match kind with let cmp = match kind with
| Int8 -> signed_compare va vb | Int8 -> Compare.Int64.compare va vb
| Uint8 -> unsigned_compare va vb | Uint8 -> Compare.Uint64.compare va vb
| Int16 -> signed_compare va vb | Int16 -> Compare.Int64.compare va vb
| Uint16 -> unsigned_compare va vb | Uint16 -> Compare.Uint64.compare va vb
| Int32 -> signed_compare va vb | Int32 -> Compare.Int64.compare va vb
| Uint32 -> unsigned_compare va vb | Uint32 -> Compare.Uint64.compare va vb
| Int64 -> signed_compare va vb | Int64 -> Compare.Int64.compare va vb
| Uint64 -> unsigned_compare va vb in | Uint64 -> Compare.Uint64.compare va vb in
Int Compare.Int.(if cmp = 0 then 0L else if cmp > 0 then 1L else -1L) Int Compare.Int.(if cmp = 0 then 0L else if cmp > 0 then 1L else -1L)
let equal kind va vb = let equal kind va vb =
@ -172,9 +167,9 @@ let checked_add : type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val
| Uint16 -> checked_of_int64 Uint16 r | Uint16 -> checked_of_int64 Uint16 r
| Int32 -> checked_of_int64 Int32 r | Int32 -> checked_of_int64 Int32 r
| Uint32 -> checked_of_int64 Uint32 r | Uint32 -> checked_of_int64 Uint32 r
| Int64 when Compare.Int.(signed_compare r va < 0) -> None | Int64 when Compare.Int.(Compare.Int64.compare r va < 0) -> None
| Int64 -> Some (Int r) | Int64 -> Some (Int r)
| Uint64 when Compare.Int.(unsigned_compare r va < 0) -> None | Uint64 when Compare.Int.(Compare.Uint64.compare r va < 0) -> None
| Uint64 -> Some (Int r) | Uint64 -> Some (Int r)
let checked_sub : type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val -> (s, l) int_val option let checked_sub : type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val -> (s, l) int_val option
@ -186,10 +181,10 @@ let checked_sub : type s l. (s, l) int_kind -> (s, l) int_val -> (s, l) int_val
| Int32 -> checked_of_int64 Int32 r | Int32 -> checked_of_int64 Int32 r
| Uint32 -> checked_of_int64 Uint32 r | Uint32 -> checked_of_int64 Uint32 r
| Int64 when Compare.Int64.(vb >= 0L) -> | Int64 when Compare.Int64.(vb >= 0L) ->
if Compare.Int.(signed_compare r va <= 0) then Some (Int r) else None if Compare.Int.(Compare.Int64.compare r va <= 0) then Some (Int r) else None
| Int64 -> | Int64 ->
if Compare.Int.(signed_compare r va >= 0) then Some (Int r) else None if Compare.Int.(Compare.Int64.compare r va >= 0) then Some (Int r) else None
| Uint64 when Compare.Int.(unsigned_compare r va > 0) -> None | Uint64 when Compare.Int.(Compare.Uint64.compare r va > 0) -> None
| Uint64 -> Some (Int r) | Uint64 -> Some (Int r)
let checked_neg : type l. (signed, l) int_kind -> (signed, l) int_val -> (signed, l) int_val option let checked_neg : type l. (signed, l) int_kind -> (signed, l) int_val -> (signed, l) int_val option

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

@ -184,7 +184,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

@ -420,14 +420,79 @@ let s = Bytes.of_string "hello"
[string] type for this purpose. [string] type for this purpose.
*) *)
(**/**) (** Functions reading and writing bytes *)
(* The following is for system use only. Do not call directly. *) val get_char: t -> int -> char
(** [get_char buff i] reads 1 byte at offset i as a char *)
external unsafe_get : bytes -> int -> char = "%string_unsafe_get" val get_uint8: t -> int -> int
external unsafe_set : bytes -> int -> char -> unit = "%string_unsafe_set" (** [get_uint8 buff i] reads 1 byte at offset i as an unsigned int of 8
external unsafe_blit : bits. i.e. It returns a value between 0 and 2^8-1 *)
bytes -> int -> bytes -> int -> int -> unit
= "caml_blit_string" [@@noalloc] val get_int8: t -> int -> int
external unsafe_fill : (** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8
bytes -> int -> int -> char -> unit = "caml_fill_string" [@@noalloc] bits. i.e. It returns a value between -2^7 and 2^7-1 *)
val set_char: t -> int -> char -> unit
(** [set_char buff i v] writes [v] to [buff] at offset [i] *)
val set_int8: t -> int -> int -> unit
(** [set_int8 buff i v] writes the least significant 8 bits of [v]
to [buff] at offset [i] *)
(** Functions reading according to Big Endian byte order *)
val get_uint16: t -> int -> int
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
val get_int16: t -> int -> int
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
val get_int32: t -> int -> int32
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
val get_int64: t -> int -> int64
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
val set_int16: t -> int -> int -> unit
(** [set_int16 buff i v] writes the least significant 16 bits of [v]
to [buff] at offset [i] *)
val set_int32: t -> int -> int32 -> unit
(** [set_int32 buff i v] writes [v] to [buff] at offset [i] *)
val set_int64: t -> int -> int64 -> unit
(** [set_int64 buff i v] writes [v] to [buff] at offset [i] *)
module LE: sig
(** Functions reading according to Little Endian byte order *)
val get_uint16: t -> int -> int
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
val get_int16: t -> int -> int
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
val get_int32: t -> int -> int32
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
val get_int64: t -> int -> int64
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
val set_int16: t -> int -> int -> unit
(** [set_int16 buff i v] writes the least significant 16 bits of [v]
to [buff] at offset [i] *)
val set_int32: t -> int -> int32 -> unit
(** [set_int32 buff i v] writes [v] to [buff] at offset [i] *)
val set_int64: t -> int -> int64 -> unit
(** [set_int64 buff i v] writes [v] to [buff] at offset [i] *)
end

View File

@ -16,7 +16,9 @@ module Char : S with type t = char
module Bool : S with type t = bool module Bool : S with type t = bool
module Int : S with type t = int module Int : S with type t = int
module Int32 : S with type t = int32 module Int32 : S with type t = int32
module Uint32 : S with type t = int32
module Int64 : S with type t = int64 module Int64 : S with type t = int64
module Uint64 : S with type t = int64
module Float : S with type t = float module Float : S with type t = float
module String : S with type t = string module String : S with type t = string
module List(P : S) : S with type t = P.t list module List(P : S) : S with type t = P.t list

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

@ -238,3 +238,53 @@ val compare: t -> t -> int
{!Pervasives.compare}. Along with the type [t], this function [compare] {!Pervasives.compare}. Along with the type [t], this function [compare]
allows the module [String] to be passed as argument to the functors allows the module [String] to be passed as argument to the functors
{!Set.Make} and {!Map.Make}. *) {!Set.Make} and {!Map.Make}. *)
(** Functions reading bytes *)
val get_char: t -> int -> char
(** [get_char buff i] reads 1 byte at offset i as a char *)
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 *)
val get_int8: t -> int -> int
(** [get_int8 buff i] reads 1 byte at offset i as a signed int of 8
bits. i.e. It returns a value between -2^7 and 2^7-1 *)
(** Functions reading according to Big Endian byte order *)
val get_uint16: t -> int -> int
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
val get_int16: t -> int -> int
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
val get_int32: t -> int -> int32
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
val get_int64: t -> int -> int64
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
module LE: sig
(** Functions reading according to Little Endian byte order *)
val get_uint16: t -> int -> int
(** [get_uint16 buff i] reads 2 bytes at offset i as an unsigned int
of 16 bits. i.e. It returns a value between 0 and 2^16-1 *)
val get_int16: t -> int -> int
(** [get_int16 buff i] reads 2 byte at offset i as a signed int of
16 bits. i.e. It returns a value between -2^15 and 2^15-1 *)
val get_int32: t -> int -> int32
(** [get_int32 buff i] reads 4 bytes at offset i as an int32. *)
val get_int64: t -> int -> int64
(** [get_int64 buff i] reads 8 bytes at offset i as an int64. *)
end

View File

@ -85,6 +85,32 @@ module Int64 = struct
let min x y = if x <= y then x else y let min x y = if x <= y then x else y
end end
module MakeUnsigned(Int : S)(Z : sig val zero : Int.t end) = struct
type t = Int.t
let compare va vb =
Int.(if va >= Z.zero then if vb >= Z.zero then compare va vb else -1
else if vb >= Z.zero then 1 else compare va vb)
let (=) = ((=) : t -> t -> bool)
let (<>) = ((<>) : t -> t -> bool)
let (<) a b =
Int.(if Z.zero <= a then
(a < b || b < Z.zero)
else
(b < Z.zero && a < b))
let (<=) a b =
Int.(if Z.zero <= a then
(a <= b || b < Z.zero)
else
(b < Z.zero && a <= b))
let (>=) a b = (<=) b a
let (>) a b = (<) b a
let max x y = if x >= y then x else y
let min x y = if x <= y then x else y
end
module Uint32 = MakeUnsigned(Int32)(struct let zero = 0l end)
module Uint64 = MakeUnsigned(Int64)(struct let zero = 0L end)
module Float = struct module Float = struct
type t = float type t = float
let (=) = ((=) : t -> t -> bool) let (=) = ((=) : t -> t -> bool)

View File

@ -24,7 +24,9 @@ module Char : S with type t = char
module Bool : S with type t = bool module Bool : S with type t = bool
module Int : S with type t = int module Int : S with type t = int
module Int32 : S with type t = int32 module Int32 : S with type t = int32
module Uint32 : S with type t = int32
module Int64 : S with type t = int64 module Int64 : S with type t = int64
module Uint64 : S with type t = int64
module Float : S with type t = float module Float : S with type t = float
module String : S with type t = string module String : S with type t = string
module List(P : S) : S with type t = P.t list module List(P : S) : S with type t = P.t list

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 *)