2016-10-12 17:00:19 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2016-10-12 17:00:19 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let (//) = Filename.concat
|
|
|
|
|
|
|
|
(** Basic blocks *)
|
|
|
|
|
|
|
|
let genesis_block =
|
2017-04-05 11:54:21 +04:00
|
|
|
Block_hash.of_b58check_exn
|
2017-02-19 21:22:32 +04:00
|
|
|
"BLockGenesisGenesisGenesisGenesisGenesisGeneskvg68z"
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let genesis_protocol =
|
2017-04-05 11:54:21 +04:00
|
|
|
Protocol_hash.of_b58check_exn
|
2017-02-19 21:22:32 +04:00
|
|
|
"ProtoDemoDemoDemoDemoDemoDemoDemoDemoDemoDemoD3c8k9"
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let genesis_time =
|
|
|
|
Time.of_seconds 0L
|
|
|
|
|
2017-10-09 12:55:12 +04:00
|
|
|
module Proto = (val State.Registred_protocol.get_exn genesis_protocol)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let genesis : State.Net.genesis = {
|
|
|
|
time = genesis_time ;
|
2016-09-08 21:13:10 +04:00
|
|
|
block = genesis_block ;
|
|
|
|
protocol = genesis_protocol ;
|
|
|
|
}
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = Net_id.of_block_hash genesis_block
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let incr_fitness fitness =
|
|
|
|
let new_fitness =
|
|
|
|
match fitness with
|
2017-04-10 14:14:11 +04:00
|
|
|
| [ fitness ] ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Pervasives.(
|
|
|
|
Data_encoding.Binary.of_bytes Data_encoding.int64 fitness
|
2017-11-27 09:13:12 +04:00
|
|
|
|> Option.unopt ~default:0L
|
2016-09-30 13:43:50 +04:00
|
|
|
|> Int64.succ
|
|
|
|
|> Data_encoding.Binary.to_bytes Data_encoding.int64
|
|
|
|
)
|
2016-09-08 21:13:10 +04:00
|
|
|
| _ -> Data_encoding.Binary.to_bytes Data_encoding.int64 1L
|
|
|
|
in
|
2017-04-10 14:14:11 +04:00
|
|
|
[ new_fitness ]
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let incr_timestamp timestamp =
|
2017-02-24 20:17:53 +04:00
|
|
|
Time.add timestamp (Int64.add 1L (Random.int64 10L))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let operation op =
|
2017-04-19 21:21:23 +04:00
|
|
|
let op : Operation.t = {
|
2017-11-14 05:41:37 +04:00
|
|
|
shell = { branch = genesis_block } ;
|
2016-09-30 13:43:50 +04:00
|
|
|
proto = MBytes.of_string op ;
|
2016-09-08 21:13:10 +04:00
|
|
|
} in
|
2017-04-19 21:21:23 +04:00
|
|
|
Operation.hash op,
|
2016-09-08 21:13:10 +04:00
|
|
|
op,
|
2017-04-19 21:21:23 +04:00
|
|
|
Data_encoding.Binary.to_bytes Operation.encoding op
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 21:21:23 +04:00
|
|
|
let block _state ?(operations = []) pred_hash pred name : Block_header.t =
|
2017-04-10 19:06:11 +04:00
|
|
|
let operations_hash =
|
2017-03-30 15:16:21 +04:00
|
|
|
Operation_list_list_hash.compute
|
|
|
|
[Operation_list_hash.compute operations] in
|
2017-04-19 21:21:23 +04:00
|
|
|
let fitness = incr_fitness pred.Block_header.shell.fitness in
|
2017-02-24 20:17:53 +04:00
|
|
|
let timestamp = incr_timestamp pred.shell.timestamp in
|
2016-09-08 21:13:10 +04:00
|
|
|
{ shell = {
|
2017-04-10 15:01:22 +04:00
|
|
|
level = Int32.succ pred.shell.level ;
|
2017-04-12 20:22:40 +04:00
|
|
|
proto_level = pred.shell.proto_level ;
|
2016-09-08 21:13:10 +04:00
|
|
|
predecessor = pred_hash ;
|
2017-09-29 20:43:13 +04:00
|
|
|
validation_passes = 1 ;
|
2017-04-10 19:06:11 +04:00
|
|
|
timestamp ; operations_hash ; fitness } ;
|
2016-09-08 21:13:10 +04:00
|
|
|
proto = MBytes.of_string name ;
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
let equal_operation ?msg op1 op2 =
|
|
|
|
let msg = Assert.format_msg msg in
|
|
|
|
let eq op1 op2 =
|
|
|
|
match op1, op2 with
|
|
|
|
| None, None -> true
|
|
|
|
| Some op1, Some op2 ->
|
2017-04-19 21:21:23 +04:00
|
|
|
Operation.equal op1 op2
|
2017-04-05 20:24:26 +04:00
|
|
|
| _ -> false in
|
|
|
|
let prn = function
|
|
|
|
| None -> "none"
|
2017-11-27 09:13:12 +04:00
|
|
|
| Some op -> Operation_hash.to_hex (Operation.hash op) in
|
2017-04-05 20:24:26 +04:00
|
|
|
Assert.equal ?msg ~prn ~eq op1 op2
|
|
|
|
|
|
|
|
let equal_block ?msg st1 st2 =
|
|
|
|
let msg = Assert.format_msg msg in
|
|
|
|
let eq st1 st2 =
|
|
|
|
match st1, st2 with
|
|
|
|
| None, None -> true
|
2017-04-19 21:21:23 +04:00
|
|
|
| Some st1, Some st2 -> Block_header.equal st1 st2
|
2017-04-05 20:24:26 +04:00
|
|
|
| _ -> false in
|
|
|
|
let prn = function
|
|
|
|
| None -> "none"
|
2017-11-27 09:13:12 +04:00
|
|
|
| Some st -> Block_hash.to_hex (Block_header.hash st) in
|
2017-04-05 20:24:26 +04:00
|
|
|
Assert.equal ?msg ~prn ~eq st1 st2
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let block _state ?(operations = []) (pred: State.Block.t) name
|
2017-04-19 21:21:23 +04:00
|
|
|
: Block_header.t =
|
2017-04-10 19:06:11 +04:00
|
|
|
let operations_hash =
|
2017-03-30 15:16:21 +04:00
|
|
|
Operation_list_list_hash.compute
|
|
|
|
[Operation_list_hash.compute operations] in
|
2017-04-19 23:46:10 +04:00
|
|
|
let pred_header = State.Block.shell_header pred in
|
|
|
|
let fitness = incr_fitness pred_header.fitness in
|
|
|
|
let timestamp = incr_timestamp pred_header.timestamp in
|
2017-11-14 06:14:26 +04:00
|
|
|
{ shell = { level = Int32.succ pred_header.level ;
|
2017-04-19 23:46:10 +04:00
|
|
|
proto_level = pred_header.proto_level ;
|
|
|
|
predecessor = State.Block.hash pred ;
|
2017-09-29 20:43:13 +04:00
|
|
|
validation_passes = 1 ;
|
2017-04-10 19:06:11 +04:00
|
|
|
timestamp ; operations_hash ; fitness } ;
|
2016-09-08 21:13:10 +04:00
|
|
|
proto = MBytes.of_string name ;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let build_valid_chain state vtbl pred names =
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt_list.fold_left_s
|
|
|
|
(fun pred name ->
|
|
|
|
begin
|
2017-04-05 20:24:26 +04:00
|
|
|
let oph, op, _bytes = operation name in
|
2016-09-08 21:13:10 +04:00
|
|
|
let block = block state ~operations:[oph] pred name in
|
2017-04-19 23:46:10 +04:00
|
|
|
let hash = Block_header.hash block in
|
|
|
|
let pred_header = State.Block.header pred in
|
|
|
|
State.Block.context pred >>= fun predecessor_context ->
|
2016-10-20 20:54:16 +04:00
|
|
|
begin
|
|
|
|
Proto.begin_application
|
2017-04-19 23:46:10 +04:00
|
|
|
~predecessor_context
|
|
|
|
~predecessor_timestamp: pred_header.shell.timestamp
|
|
|
|
~predecessor_fitness: pred_header.shell.fitness
|
2016-10-20 20:54:16 +04:00
|
|
|
block >>=? fun vstate ->
|
|
|
|
(* no operations *)
|
|
|
|
Proto.finalize_block vstate
|
|
|
|
end >>=? fun ctxt ->
|
2017-04-19 23:46:10 +04:00
|
|
|
State.Block.store state block [[op]] ctxt >>=? fun _vblock ->
|
|
|
|
State.Block.read state hash >>=? fun vblock ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Hashtbl.add vtbl name vblock ;
|
|
|
|
return vblock
|
|
|
|
end >>= function
|
|
|
|
| Ok v -> Lwt.return v
|
|
|
|
| Error err ->
|
|
|
|
Error_monad.pp_print_error Format.err_formatter err ;
|
|
|
|
assert false)
|
|
|
|
pred
|
|
|
|
names >>= fun _ ->
|
|
|
|
Lwt.return ()
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let build_example_tree net =
|
2016-09-08 21:13:10 +04:00
|
|
|
let vtbl = Hashtbl.create 23 in
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.genesis net >>= fun genesis ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Hashtbl.add vtbl "Genesis" genesis ;
|
2016-09-08 21:13:10 +04:00
|
|
|
let chain = [ "A1" ; "A2" ; "A3" ; "A4" ; "A5" ; "A6" ; "A7" ; "A8" ] in
|
2017-04-19 23:46:10 +04:00
|
|
|
build_valid_chain net vtbl genesis chain >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
let a3 = Hashtbl.find vtbl "A3" in
|
|
|
|
let chain = [ "B1" ; "B2" ; "B3" ; "B4" ; "B5" ; "B6" ; "B7" ; "B8" ] in
|
2017-04-19 23:46:10 +04:00
|
|
|
build_valid_chain net vtbl a3 chain >>= fun () ->
|
|
|
|
Lwt.return vtbl
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
type state = {
|
2017-04-19 23:46:10 +04:00
|
|
|
vblock: (string, State.Block.t) Hashtbl.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
state: State.t ;
|
|
|
|
net: State.Net.t ;
|
2017-02-24 20:17:53 +04:00
|
|
|
init: unit -> State.t tzresult Lwt.t;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let vblock s = Hashtbl.find s.vblock
|
|
|
|
|
|
|
|
exception Found of string
|
|
|
|
|
|
|
|
let vblocks s =
|
2017-02-24 20:17:53 +04:00
|
|
|
Hashtbl.fold (fun k v acc -> (k,v) :: acc) s.vblock []
|
|
|
|
|> List.sort Pervasives.compare
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let wrap_state_init f base_dir =
|
|
|
|
begin
|
|
|
|
let store_root = base_dir // "store" in
|
|
|
|
let context_root = base_dir // "context" in
|
|
|
|
let init () =
|
|
|
|
State.read
|
|
|
|
~store_root
|
|
|
|
~context_root
|
|
|
|
() in
|
2017-02-24 20:17:53 +04:00
|
|
|
init () >>=? fun state ->
|
|
|
|
State.Net.create state genesis >>= fun net ->
|
2017-04-19 23:46:10 +04:00
|
|
|
build_example_tree net >>= fun vblock ->
|
|
|
|
f { state ; net ; vblock ; init } >>=? fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return ()
|
2017-03-07 12:51:11 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-05 20:24:26 +04:00
|
|
|
let test_init (_ : state) =
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** State.Block.read *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let test_read_block (s: state) =
|
2017-04-19 23:46:10 +04:00
|
|
|
Lwt_list.iter_s (fun (name, vblock) ->
|
|
|
|
let hash = State.Block.hash vblock in
|
|
|
|
State.Block.read s.net hash >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| Error _ ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Assert.fail_msg "Error while reading valid block %s" name
|
2017-02-24 20:17:53 +04:00
|
|
|
| Ok _vblock' ->
|
2017-04-19 23:46:10 +04:00
|
|
|
(* FIXME COMPARE read operations ??? *)
|
|
|
|
Lwt.return_unit
|
|
|
|
) (vblocks s) >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain_traversal.path *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let rec compare_path p1 p2 = match p1, p2 with
|
|
|
|
| [], [] -> true
|
|
|
|
| h1 :: p1, h2 :: p2 -> Block_hash.equal h1 h2 && compare_path p1 p2
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let test_path (s: state) =
|
|
|
|
let check_path h1 h2 p2 =
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain_traversal.path (vblock s h1) (vblock s h2) >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "cannot compute path %s -> %s" h1 h2 ;
|
2017-04-19 23:46:10 +04:00
|
|
|
| Some (p: State.Block.t list) ->
|
|
|
|
let p = List.map State.Block.hash p in
|
|
|
|
let p2 = List.map (fun b -> State.Block.hash (vblock s b)) p2 in
|
2016-09-30 13:43:50 +04:00
|
|
|
if not (compare_path p p2) then
|
|
|
|
Assert.fail_msg "bad path %s -> %s" h1 h2 ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return_unit in
|
2017-02-24 20:17:53 +04:00
|
|
|
check_path "Genesis" "Genesis" [] >>= fun () ->
|
|
|
|
check_path "A1" "A1" [] >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
check_path "A2" "A6" ["A3"; "A4"; "A5"; "A6"] >>= fun () ->
|
|
|
|
check_path "B2" "B6" ["B3"; "B4"; "B5"; "B6"] >>= fun () ->
|
|
|
|
check_path "A1" "B3" ["A2"; "A3"; "B1"; "B2"; "B3"] >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain_traversal.common_ancestor *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let test_ancestor s =
|
|
|
|
let check_ancestor h1 h2 expected =
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain_traversal.common_ancestor
|
|
|
|
(vblock s h1) (vblock s h2) >>= fun a ->
|
|
|
|
if not (Block_hash.equal (State.Block.hash a) (State.Block.hash expected)) then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "bad ancestor %s %s" h1 h2 ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return_unit in
|
2017-04-19 23:46:10 +04:00
|
|
|
check_ancestor "Genesis" "Genesis" (vblock s "Genesis") >>= fun () ->
|
|
|
|
check_ancestor "Genesis" "A3" (vblock s "Genesis") >>= fun () ->
|
|
|
|
check_ancestor "A3" "Genesis" (vblock s "Genesis") >>= fun () ->
|
|
|
|
check_ancestor "A1" "A1" (vblock s "A1") >>= fun () ->
|
|
|
|
check_ancestor "A1" "A3" (vblock s "A1") >>= fun () ->
|
|
|
|
check_ancestor "A3" "A1" (vblock s "A1") >>= fun () ->
|
|
|
|
check_ancestor "A6" "B6" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "B6" "A6" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "A4" "B1" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "B1" "A4" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "A3" "B1" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "B1" "A3" (vblock s "A3") >>= fun () ->
|
|
|
|
check_ancestor "A2" "B1" (vblock s "A2") >>= fun () ->
|
|
|
|
check_ancestor "B1" "A2" (vblock s "A2") >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain_traversal.block_locator *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let test_locator s =
|
|
|
|
let check_locator h1 expected =
|
2017-09-29 20:43:13 +04:00
|
|
|
Block_locator.compute
|
2017-04-19 23:46:10 +04:00
|
|
|
(vblock s h1) (List.length expected) >>= fun l ->
|
2017-11-11 06:34:12 +04:00
|
|
|
let _, l = (l : Block_locator.t :> _ * _) in
|
2016-09-08 21:13:10 +04:00
|
|
|
if List.length l <> List.length expected then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg
|
|
|
|
"Invalid locator length %s (found: %d, expected: %d)"
|
2016-09-08 21:13:10 +04:00
|
|
|
h1 (List.length l) (List.length expected) ;
|
|
|
|
List.iter2
|
|
|
|
(fun h h2 ->
|
2017-04-19 23:46:10 +04:00
|
|
|
if not (Block_hash.equal h (State.Block.hash @@ vblock s h2)) then
|
2017-11-11 06:34:12 +04:00
|
|
|
Assert.fail_msg "Invalid locator %s (expected: %s)" h1 h2)
|
2016-09-08 21:13:10 +04:00
|
|
|
l expected ;
|
|
|
|
Lwt.return_unit in
|
2017-11-11 06:34:12 +04:00
|
|
|
check_locator "A8" ["A7";"A6";"A5";"A4";"A3";"A2"] >>= fun () ->
|
|
|
|
check_locator "B8" ["B7";"B6";"B5";"B4";"B3";"B2";"B1";"A3"] >>= fun () ->
|
|
|
|
check_locator "B8" ["B7";"B6";"B5";"B4"] >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain.known_heads *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let compare s name heads l =
|
2017-02-24 20:17:53 +04:00
|
|
|
if List.length heads <> List.length l then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg
|
|
|
|
"unexpected known_heads size (%s: %d %d)"
|
2017-02-24 20:17:53 +04:00
|
|
|
name (List.length heads) (List.length l) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
List.iter
|
|
|
|
(fun bname ->
|
2017-04-19 23:46:10 +04:00
|
|
|
let hash = State.Block.hash (vblock s bname) in
|
|
|
|
if not (List.exists (fun b -> Block_hash.equal hash (State.Block.hash b)) heads) then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "missing block in known_heads (%s: %s)" name bname)
|
2016-09-08 21:13:10 +04:00
|
|
|
l
|
|
|
|
|
|
|
|
let test_known_heads s =
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.known_heads s.net >>= fun heads ->
|
2016-09-08 21:13:10 +04:00
|
|
|
compare s "initial" heads ["A8";"B8"] ;
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain.head/set_head *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let test_head s =
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.head s.net >>= fun head ->
|
|
|
|
if not (Block_hash.equal (State.Block.hash head) genesis_block) then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "unexpected head" ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "A6") >>= fun _ ->
|
|
|
|
Chain.head s.net >>= fun head ->
|
|
|
|
if not (Block_hash.equal (State.Block.hash head) (State.Block.hash @@ vblock s "A6")) then
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "unexpected head" ;
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain.mem *)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let test_mem s =
|
|
|
|
let mem s x =
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.mem s.net (State.Block.hash @@ vblock s x) in
|
2016-09-08 21:13:10 +04:00
|
|
|
let test_mem s x =
|
|
|
|
mem s x >>= function
|
|
|
|
| true -> Lwt.return_unit
|
2016-09-30 13:43:50 +04:00
|
|
|
| false -> Assert.fail_msg "mem %s" x in
|
2016-09-08 21:13:10 +04:00
|
|
|
let test_not_mem s x =
|
|
|
|
mem s x >>= function
|
|
|
|
| false -> Lwt.return_unit
|
2016-09-30 13:43:50 +04:00
|
|
|
| true -> Assert.fail_msg "not (mem %s)" x in
|
2016-09-08 21:13:10 +04:00
|
|
|
test_not_mem s "A3" >>= fun () ->
|
|
|
|
test_not_mem s "A6" >>= fun () ->
|
|
|
|
test_not_mem s "A8" >>= fun () ->
|
|
|
|
test_not_mem s "B1" >>= fun () ->
|
|
|
|
test_not_mem s "B6" >>= fun () ->
|
|
|
|
test_not_mem s "B8" >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "A8") >>= fun _ ->
|
2016-09-08 21:13:10 +04:00
|
|
|
test_mem s "A3" >>= fun () ->
|
|
|
|
test_mem s "A6" >>= fun () ->
|
|
|
|
test_mem s "A8" >>= fun () ->
|
|
|
|
test_not_mem s "B1" >>= fun () ->
|
|
|
|
test_not_mem s "B6" >>= fun () ->
|
|
|
|
test_not_mem s "B8" >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "A6") >>= fun _ ->
|
2016-09-08 21:13:10 +04:00
|
|
|
test_mem s "A3" >>= fun () ->
|
|
|
|
test_mem s "A6" >>= fun () ->
|
|
|
|
test_not_mem s "A8" >>= fun () ->
|
|
|
|
test_not_mem s "B1" >>= fun () ->
|
|
|
|
test_not_mem s "B6" >>= fun () ->
|
|
|
|
test_not_mem s "B8" >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "B6") >>= fun _ ->
|
2016-09-08 21:13:10 +04:00
|
|
|
test_mem s "A3" >>= fun () ->
|
|
|
|
test_not_mem s "A4" >>= fun () ->
|
|
|
|
test_not_mem s "A6" >>= fun () ->
|
|
|
|
test_not_mem s "A8" >>= fun () ->
|
|
|
|
test_mem s "B1" >>= fun () ->
|
|
|
|
test_mem s "B6" >>= fun () ->
|
|
|
|
test_not_mem s "B8" >>= fun () ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "B8") >>= fun _ ->
|
2016-09-08 21:13:10 +04:00
|
|
|
test_mem s "A3" >>= fun () ->
|
|
|
|
test_not_mem s "A4" >>= fun () ->
|
|
|
|
test_not_mem s "A6" >>= fun () ->
|
|
|
|
test_not_mem s "A8" >>= fun () ->
|
|
|
|
test_mem s "B1" >>= fun () ->
|
|
|
|
test_mem s "B6" >>= fun () ->
|
|
|
|
test_mem s "B8" >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(** Chain_traversal.new_blocks *)
|
|
|
|
|
|
|
|
let test_new_blocks s =
|
|
|
|
let test s head h expected_ancestor expected =
|
|
|
|
let to_block = vblock s head
|
|
|
|
and from_block = vblock s h in
|
|
|
|
Chain_traversal.new_blocks ~from_block ~to_block >>= fun (ancestor, blocks) ->
|
|
|
|
if not (Block_hash.equal (State.Block.hash ancestor) (State.Block.hash @@ vblock s expected_ancestor)) then
|
2017-11-11 06:34:12 +04:00
|
|
|
Assert.fail_msg "Invalid ancestor %s -> %s (expected: %s)" head h expected_ancestor ;
|
2017-04-19 23:46:10 +04:00
|
|
|
if List.length blocks <> List.length expected then
|
|
|
|
Assert.fail_msg
|
|
|
|
"Invalid locator length %s (found: %d, expected: %d)"
|
|
|
|
h (List.length blocks) (List.length expected) ;
|
|
|
|
List.iter2
|
|
|
|
(fun h1 h2 ->
|
|
|
|
if not (Block_hash.equal (State.Block.hash h1) (State.Block.hash @@ vblock s h2)) then
|
2017-11-11 06:34:12 +04:00
|
|
|
Assert.fail_msg "Invalid new blocks %s -> %s (expected: %s)" head h h2)
|
2017-04-19 23:46:10 +04:00
|
|
|
blocks expected ;
|
|
|
|
Lwt.return_unit
|
|
|
|
in
|
|
|
|
test s "A6" "A6" "A6" [] >>= fun () ->
|
|
|
|
test s "A8" "A6" "A6" ["A7";"A8"] >>= fun () ->
|
|
|
|
test s "A8" "B7" "A3" ["A4";"A5";"A6";"A7";"A8"] >>= fun () ->
|
|
|
|
return ()
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
(****************************************************************************)
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
(** Block_locator.find_new *)
|
2017-04-19 23:46:10 +04:00
|
|
|
|
|
|
|
let test_find_new s =
|
2016-09-08 21:13:10 +04:00
|
|
|
let test s h expected =
|
2017-09-29 20:43:13 +04:00
|
|
|
Block_locator.compute (vblock s h) 50 >>= fun loc ->
|
|
|
|
Block_locator.find_new s.net loc (List.length expected) >>= fun blocks ->
|
2017-04-19 23:46:10 +04:00
|
|
|
if List.length blocks <> List.length expected then
|
|
|
|
Assert.fail_msg
|
2017-11-11 06:34:12 +04:00
|
|
|
"Invalid find new length %s (found: %d, expected: %d)"
|
2017-04-19 23:46:10 +04:00
|
|
|
h (List.length blocks) (List.length expected) ;
|
|
|
|
List.iter2
|
|
|
|
(fun h1 h2 ->
|
|
|
|
if not (Block_hash.equal h1 (State.Block.hash @@ vblock s h2)) then
|
2017-11-11 06:34:12 +04:00
|
|
|
Assert.fail_msg "Invalid find new %s.%d (expected: %s)" h (List.length expected) h2)
|
2017-04-19 23:46:10 +04:00
|
|
|
blocks expected ;
|
|
|
|
Lwt.return_unit
|
2016-09-08 21:13:10 +04:00
|
|
|
in
|
2017-04-19 23:46:10 +04:00
|
|
|
Chain.set_head s.net (vblock s "A8") >>= fun _ ->
|
2017-11-11 06:34:12 +04:00
|
|
|
test s "A6" [] >>= fun () ->
|
2016-09-08 21:13:10 +04:00
|
|
|
test s "A6" ["A7";"A8"] >>= fun () ->
|
|
|
|
test s "A6" ["A7"] >>= fun () ->
|
|
|
|
test s "B4" ["A4"] >>= fun () ->
|
|
|
|
test s "B7" ["A4";"A5";"A6";"A7"] >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
(****************************************************************************)
|
|
|
|
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let tests : (string * (state -> unit tzresult Lwt.t)) list = [
|
2016-09-08 21:13:10 +04:00
|
|
|
"init", test_init ;
|
|
|
|
"read_block", test_read_block ;
|
|
|
|
"path", test_path ;
|
|
|
|
"ancestor", test_ancestor ;
|
|
|
|
"locator", test_locator ;
|
|
|
|
"known_heads", test_known_heads ;
|
|
|
|
"head", test_head ;
|
|
|
|
"mem", test_mem ;
|
2017-04-19 23:46:10 +04:00
|
|
|
"new_blocks", test_new_blocks ;
|
|
|
|
"find_new", test_find_new ;
|
2016-09-08 21:13:10 +04:00
|
|
|
]
|
|
|
|
|
2016-09-30 13:43:50 +04:00
|
|
|
let () =
|
2016-09-08 21:13:10 +04:00
|
|
|
Test.run "state." (List.map (fun (s, f) -> s, wrap_state_init f) tests)
|