2016-10-12 17:00:19 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
open Hash
|
|
|
|
open Context
|
|
|
|
|
|
|
|
let (>>=) = Lwt.bind
|
|
|
|
let (>|=) = Lwt.(>|=)
|
|
|
|
let (//) = Filename.concat
|
|
|
|
|
|
|
|
(** Basic blocks *)
|
|
|
|
|
|
|
|
let genesis_block =
|
2017-02-19 21:22:32 +04:00
|
|
|
Block_hash.of_b58check
|
|
|
|
"BLockGenesisGenesisGenesisGenesisGenesisGeneskvg68z"
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let genesis_protocol =
|
2017-02-19 21:22:32 +04:00
|
|
|
Protocol_hash.of_b58check
|
|
|
|
"ProtoDemoDemoDemoDemoDemoDemoDemoDemoDemoDemoD3c8k9"
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let genesis_time =
|
|
|
|
Time.of_seconds 0L
|
|
|
|
|
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-02-24 20:17:53 +04:00
|
|
|
let net_id = State.Net_id.Id genesis_block
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
(** Context creation *)
|
|
|
|
|
|
|
|
let block2 =
|
2017-02-24 20:17:53 +04:00
|
|
|
Block_hash.of_hex_exn
|
2016-09-08 21:13:10 +04:00
|
|
|
"2222222222222222222222222222222222222222222222222222222222222222"
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let faked_block : Store.Block_header.t = {
|
2016-09-08 21:13:10 +04:00
|
|
|
shell = {
|
2017-02-24 20:17:53 +04:00
|
|
|
net_id ;
|
2016-09-08 21:13:10 +04:00
|
|
|
predecessor = genesis_block ;
|
|
|
|
operations = [] ;
|
|
|
|
fitness = [] ;
|
|
|
|
timestamp = Time.of_seconds 0L ;
|
|
|
|
} ;
|
|
|
|
proto = MBytes.of_string "" ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let create_block2 idx =
|
|
|
|
checkout idx genesis_block >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout genesis_block"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
set ctxt ["a"; "b"] (MBytes.of_string "Novembre") >>= fun ctxt ->
|
|
|
|
set ctxt ["a"; "c"] (MBytes.of_string "Juin") >>= fun ctxt ->
|
|
|
|
set ctxt ["version";] (MBytes.of_string "0.0") >>= fun ctxt ->
|
2017-02-24 20:17:53 +04:00
|
|
|
commit faked_block block2 ctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let block3a =
|
2017-02-24 20:17:53 +04:00
|
|
|
Block_hash.of_hex_exn
|
2016-09-08 21:13:10 +04:00
|
|
|
"3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a"
|
|
|
|
|
|
|
|
let create_block3a idx =
|
|
|
|
checkout idx block2 >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout block2"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
del ctxt ["a"; "b"] >>= fun ctxt ->
|
|
|
|
set ctxt ["a"; "d"] (MBytes.of_string "Mars") >>= fun ctxt ->
|
2017-02-24 20:17:53 +04:00
|
|
|
commit faked_block block3a ctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let block3b =
|
2017-02-24 20:17:53 +04:00
|
|
|
Block_hash.of_hex_exn
|
2016-09-08 21:13:10 +04:00
|
|
|
"3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b"
|
|
|
|
|
|
|
|
let block3c =
|
2017-02-24 20:17:53 +04:00
|
|
|
Block_hash.of_hex_exn
|
2016-09-08 21:13:10 +04:00
|
|
|
"3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c3c"
|
|
|
|
|
|
|
|
let create_block3b idx =
|
|
|
|
checkout idx block2 >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout block3b"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
del ctxt ["a"; "c"] >>= fun ctxt ->
|
|
|
|
set ctxt ["a"; "d"] (MBytes.of_string "Février") >>= fun ctxt ->
|
2017-02-24 20:17:53 +04:00
|
|
|
commit faked_block block3b ctxt
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let wrap_context_init f base_dir =
|
|
|
|
let root = base_dir // "context" in
|
|
|
|
Context.init root >>= fun idx ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Context.commit_genesis idx
|
|
|
|
~id:genesis.block
|
|
|
|
~time:genesis.time
|
|
|
|
~protocol:genesis.protocol
|
|
|
|
~test_protocol:genesis.protocol >>= fun _ ->
|
2016-09-08 21:13:10 +04:00
|
|
|
create_block2 idx >>= fun () ->
|
|
|
|
create_block3a idx >>= fun () ->
|
|
|
|
create_block3b idx >>= fun () ->
|
|
|
|
f idx
|
|
|
|
|
|
|
|
(** Simple test *)
|
|
|
|
|
|
|
|
let c = function
|
|
|
|
| None -> None
|
|
|
|
| Some s -> Some (MBytes.to_string s)
|
|
|
|
|
|
|
|
let test_simple idx =
|
|
|
|
checkout idx block2 >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout block2"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["version"] >>= fun version ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (c version) (Some "0.0") ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"b"] >>= fun novembre ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option (Some "Novembre") (c novembre) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"c"] >>= fun juin ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Juin") (c juin) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return ()
|
|
|
|
|
|
|
|
let test_continuation idx =
|
|
|
|
checkout idx block3a >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout block3a"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["version"] >>= fun version ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "0.0") (c version) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"b"] >>= fun novembre ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.is_none ~msg:__LOC__ (c novembre) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"c"] >>= fun juin ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Juin") (c juin) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"d"] >>= fun mars ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Mars") (c mars) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return ()
|
|
|
|
|
|
|
|
let test_fork idx =
|
|
|
|
checkout idx block3b >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout block3b"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["version"] >>= fun version ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "0.0") (c version) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"b"] >>= fun novembre ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Novembre") (c novembre) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"c"] >>= fun juin ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.is_none ~msg:__LOC__ (c juin) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt ["a";"d"] >>= fun mars ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Février") (c mars) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return ()
|
|
|
|
|
|
|
|
let test_replay idx =
|
|
|
|
checkout idx genesis_block >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout genesis_block"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt0 ->
|
2016-09-08 21:13:10 +04:00
|
|
|
set ctxt0 ["version"] (MBytes.of_string "0.0") >>= fun ctxt1 ->
|
|
|
|
set ctxt1 ["a"; "b"] (MBytes.of_string "Novembre") >>= fun ctxt2 ->
|
|
|
|
set ctxt2 ["a"; "c"] (MBytes.of_string "Juin") >>= fun ctxt3 ->
|
|
|
|
set ctxt3 ["a"; "d"] (MBytes.of_string "July") >>= fun ctxt4a ->
|
|
|
|
set ctxt3 ["a"; "d"] (MBytes.of_string "Juillet") >>= fun ctxt4b ->
|
|
|
|
set ctxt4a ["a"; "b"] (MBytes.of_string "November") >>= fun ctxt5a ->
|
|
|
|
get ctxt4a ["a";"b"] >>= fun novembre ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Novembre") (c novembre) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt5a ["a";"b"] >>= fun november ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "November") (c november) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt5a ["a";"d"] >>= fun july ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "July") (c july) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt4b ["a";"b"] >>= fun novembre ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Novembre") (c novembre) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
get ctxt4b ["a";"d"] >>= fun juillet ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_string_option ~msg:__LOC__ (Some "Juillet") (c juillet) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return ()
|
|
|
|
|
|
|
|
let test_list idx =
|
|
|
|
checkout idx genesis_block >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.fail_msg "checkout genesis_block"
|
2017-02-24 20:17:53 +04:00
|
|
|
| Some ctxt ->
|
2016-09-08 21:13:10 +04:00
|
|
|
set ctxt ["a"; "b"] (MBytes.of_string "Novembre") >>= fun ctxt ->
|
|
|
|
set ctxt ["a"; "c"] (MBytes.of_string "Juin") >>= fun ctxt ->
|
|
|
|
set ctxt ["a"; "d"; "e"] (MBytes.of_string "Septembre") >>= fun ctxt ->
|
|
|
|
set ctxt ["f";] (MBytes.of_string "Avril") >>= fun ctxt ->
|
|
|
|
set ctxt ["g"; "h"] (MBytes.of_string "Avril") >>= fun ctxt ->
|
|
|
|
list ctxt [[]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list ~msg:__LOC__ [["a"];["f"];["g"]] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
list ctxt [["a"]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list
|
|
|
|
~msg:__LOC__ [["a";"b"]; ["a";"c"]; ["a";"d"]] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
list ctxt [["f"]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list ~msg:__LOC__ [] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
list ctxt [["g"]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list ~msg:__LOC__ [["g";"h"]] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
list ctxt [["i"]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list ~msg:__LOC__ [] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
list ctxt [["a"];["g"]] >>= fun l ->
|
2016-09-30 13:43:50 +04:00
|
|
|
Assert.equal_persist_list ~msg:__LOC__
|
|
|
|
[["a"; "b"]; ["a"; "c"]; ["a"; "d"]; ["g"; "h"]] l ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return ()
|
|
|
|
|
|
|
|
|
|
|
|
(******************************************************************************)
|
|
|
|
|
|
|
|
let tests : (string * (index -> unit Lwt.t)) list = [
|
|
|
|
"simple", test_simple ;
|
|
|
|
"continuation", test_continuation ;
|
|
|
|
"fork", test_fork ;
|
|
|
|
"replay", test_replay ;
|
|
|
|
"list", test_list ;
|
|
|
|
]
|
|
|
|
|
2016-09-30 13:43:50 +04:00
|
|
|
let () =
|
2016-09-08 21:13:10 +04:00
|
|
|
Test.run "context." (List.map (fun (s, f) -> s, wrap_context_init f) tests)
|