2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type t = Raw_store.t
|
|
|
|
type global_store = t
|
|
|
|
|
|
|
|
(**************************************************************************
|
2018-02-16 04:26:24 +04:00
|
|
|
* Net store under "chain/"
|
2017-02-24 20:17:53 +04:00
|
|
|
**************************************************************************)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
module Chain = struct
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
type store = global_store * Chain_id.t
|
2017-02-24 20:17:53 +04:00
|
|
|
let get s id = (s, id)
|
|
|
|
|
|
|
|
module Indexed_store =
|
|
|
|
Store_helpers.Make_indexed_substore
|
2018-02-16 04:26:24 +04:00
|
|
|
(Store_helpers.Make_substore(Raw_store)(struct let name = ["chain"] end))
|
|
|
|
(Chain_id)
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let destroy = Indexed_store.remove_all
|
|
|
|
let list t =
|
|
|
|
Indexed_store.fold_indexes t ~init:[]
|
|
|
|
~f:(fun h acc -> Lwt.return (h :: acc))
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
module Genesis_hash =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["genesis" ; "hash"] end)
|
|
|
|
(Store_helpers.Make_value(Block_hash))
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Genesis_time =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["genesis" ; "time"] end)
|
|
|
|
(Store_helpers.Make_value(Time))
|
|
|
|
|
|
|
|
module Genesis_protocol =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["genesis" ; "protocol"] end)
|
|
|
|
(Store_helpers.Make_value(Protocol_hash))
|
|
|
|
|
|
|
|
module Genesis_test_protocol =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["genesis" ; "test_protocol"] end)
|
|
|
|
(Store_helpers.Make_value(Protocol_hash))
|
|
|
|
|
|
|
|
module Expiration =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["expiration"] end)
|
|
|
|
(Store_helpers.Make_value(Time))
|
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
module Allow_forked_chain =
|
|
|
|
Indexed_store.Make_set (struct let name = ["allow_forked_chain"] end)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
(**************************************************************************
|
2018-02-16 04:26:24 +04:00
|
|
|
* Block_header store under "chain/<id>/blocks/"
|
2017-02-24 20:17:53 +04:00
|
|
|
**************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Block = struct
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
type store = Chain.store
|
2017-02-24 20:17:53 +04:00
|
|
|
let get x = x
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Indexed_store =
|
|
|
|
Store_helpers.Make_indexed_substore
|
2017-02-24 20:17:53 +04:00
|
|
|
(Store_helpers.Make_substore
|
2018-02-16 04:26:24 +04:00
|
|
|
(Chain.Indexed_store.Store)
|
2017-02-24 20:17:53 +04:00
|
|
|
(struct let name = ["blocks"] end))
|
|
|
|
(Block_hash)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
type contents = {
|
|
|
|
header: Block_header.t ;
|
2017-12-05 18:18:05 +04:00
|
|
|
message: string option ;
|
2017-04-18 13:29:14 +04:00
|
|
|
max_operations_ttl: int ;
|
2017-11-19 17:38:36 +04:00
|
|
|
max_operation_data_length: int;
|
2017-12-05 18:17:54 +04:00
|
|
|
context: Context_hash.t ;
|
2017-04-19 23:46:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
module Contents =
|
2017-03-30 15:16:21 +04:00
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
2017-04-19 23:46:10 +04:00
|
|
|
(struct let name = ["contents"] end)
|
2017-03-30 15:16:21 +04:00
|
|
|
(Store_helpers.Make_value(struct
|
2017-04-19 23:46:10 +04:00
|
|
|
type t = contents
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
2017-11-19 17:38:36 +04:00
|
|
|
(fun { header ; message ; max_operations_ttl ;
|
2018-01-31 13:04:35 +04:00
|
|
|
max_operation_data_length ; context } ->
|
2017-11-19 17:38:36 +04:00
|
|
|
(message, max_operations_ttl,
|
2018-01-31 13:04:35 +04:00
|
|
|
max_operation_data_length, context, header))
|
2017-11-19 17:38:36 +04:00
|
|
|
(fun (message, max_operations_ttl,
|
2018-01-31 13:04:35 +04:00
|
|
|
max_operation_data_length, context, header) ->
|
2017-11-19 17:38:36 +04:00
|
|
|
{ header ; message ; max_operations_ttl ;
|
2018-01-31 13:04:35 +04:00
|
|
|
max_operation_data_length ;
|
2017-11-19 17:38:36 +04:00
|
|
|
context })
|
2018-01-31 13:04:35 +04:00
|
|
|
(obj5
|
2017-12-05 18:18:05 +04:00
|
|
|
(opt "message" string)
|
2017-04-18 13:29:14 +04:00
|
|
|
(req "max_operations_ttl" uint16)
|
2017-11-19 17:38:36 +04:00
|
|
|
(req "max_operation_data_length" uint16)
|
2017-12-05 18:17:54 +04:00
|
|
|
(req "context" Context_hash.encoding)
|
2017-04-19 23:46:10 +04:00
|
|
|
(req "header" Block_header.encoding))
|
2017-03-30 15:16:21 +04:00
|
|
|
end))
|
|
|
|
|
|
|
|
module Operations_index =
|
|
|
|
Store_helpers.Make_indexed_substore
|
|
|
|
(Store_helpers.Make_substore
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["operations"] end))
|
|
|
|
(Store_helpers.Integer_index)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Operation_hashes =
|
2017-03-30 15:16:21 +04:00
|
|
|
Operations_index.Make_map
|
2017-04-19 23:46:10 +04:00
|
|
|
(struct let name = ["hashes"] end)
|
2017-03-30 15:16:21 +04:00
|
|
|
(Store_helpers.Make_value(struct
|
|
|
|
type t = Operation_hash.t list
|
|
|
|
let encoding = Data_encoding.list Operation_hash.encoding
|
|
|
|
end))
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Operation_path =
|
2017-03-30 15:16:21 +04:00
|
|
|
Operations_index.Make_map
|
|
|
|
(struct let name = ["path"] end)
|
|
|
|
(Store_helpers.Make_value(struct
|
|
|
|
type t = Operation_list_list_hash.path
|
|
|
|
let encoding = Operation_list_list_hash.path_encoding
|
|
|
|
end))
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Operations =
|
|
|
|
Operations_index.Make_map
|
|
|
|
(struct let name = ["contents"] end)
|
|
|
|
(Store_helpers.Make_value(struct
|
|
|
|
type t = Operation.t list
|
|
|
|
let encoding = Data_encoding.(list (dynamic_size Operation.encoding))
|
|
|
|
end))
|
|
|
|
|
|
|
|
type invalid_block = {
|
|
|
|
level: int32 ;
|
2017-10-31 20:59:02 +04:00
|
|
|
errors: Error_monad.error list ;
|
2017-04-19 23:46:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
module Invalid_block =
|
|
|
|
Store_helpers.Make_map
|
|
|
|
(Store_helpers.Make_substore
|
2018-02-16 04:26:24 +04:00
|
|
|
(Chain.Indexed_store.Store)
|
2017-04-19 23:46:10 +04:00
|
|
|
(struct let name = ["invalid_blocks"] end))
|
|
|
|
(Block_hash)
|
|
|
|
(Store_helpers.Make_value(struct
|
|
|
|
type t = invalid_block
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
2017-10-31 20:59:02 +04:00
|
|
|
(fun { level ; errors } -> (level, errors))
|
|
|
|
(fun (level, errors) -> { level ; errors })
|
|
|
|
(tup2 int32 (list Error_monad.error_encoding))
|
2017-04-19 23:46:10 +04:00
|
|
|
end))
|
|
|
|
|
2017-03-30 19:19:59 +04:00
|
|
|
let register s =
|
|
|
|
Base58.register_resolver Block_hash.b58check_encoding begin fun str ->
|
|
|
|
let pstr = Block_hash.prefix_path str in
|
2018-02-16 04:26:24 +04:00
|
|
|
Chain.Indexed_store.fold_indexes s ~init:[]
|
|
|
|
~f:begin fun chain acc ->
|
|
|
|
Indexed_store.resolve_index (s, chain) pstr >>= fun l ->
|
2017-03-30 19:19:59 +04:00
|
|
|
Lwt.return (List.rev_append l acc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-22 21:16:26 +04:00
|
|
|
module Predecessors =
|
|
|
|
Store_helpers.Make_map
|
|
|
|
(Store_helpers.Make_substore
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["predecessors"] end))
|
|
|
|
(Store_helpers.Integer_index)
|
|
|
|
(Store_helpers.Make_value(Block_hash))
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
(**************************************************************************
|
|
|
|
* Blockchain data
|
|
|
|
**************************************************************************)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
module Chain_data = struct
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
type store = Chain.store
|
2017-02-24 20:17:53 +04:00
|
|
|
let get s = s
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Known_heads =
|
|
|
|
Store_helpers.Make_buffered_set
|
|
|
|
(Store_helpers.Make_substore
|
2018-02-16 04:26:24 +04:00
|
|
|
(Chain.Indexed_store.Store)
|
2017-02-24 20:17:53 +04:00
|
|
|
(struct let name = ["known_heads"] end))
|
|
|
|
(Block_hash)
|
|
|
|
(Block_hash.Set)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Current_head =
|
|
|
|
Store_helpers.Make_single_store
|
2018-02-16 04:26:24 +04:00
|
|
|
(Chain.Indexed_store.Store)
|
2017-02-24 20:17:53 +04:00
|
|
|
(struct let name = ["current_head"] end)
|
|
|
|
(Store_helpers.Make_value(Block_hash))
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2018-02-16 04:26:24 +04:00
|
|
|
module In_main_branch =
|
2017-02-24 20:17:53 +04:00
|
|
|
Store_helpers.Make_single_store
|
2017-04-19 23:46:10 +04:00
|
|
|
(Block.Indexed_store.Store)
|
|
|
|
(struct let name = ["in_chain"] end)
|
|
|
|
(Store_helpers.Make_value(Block_hash)) (* successor *)
|
2016-10-21 16:01:20 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
(**************************************************************************
|
|
|
|
* Protocol store under "protocols/"
|
|
|
|
**************************************************************************)
|
2016-10-21 16:01:20 +04:00
|
|
|
|
|
|
|
module Protocol = struct
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type store = global_store
|
|
|
|
let get x = x
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module Indexed_store =
|
|
|
|
Store_helpers.Make_indexed_substore
|
2017-02-24 20:17:53 +04:00
|
|
|
(Store_helpers.Make_substore
|
|
|
|
(Raw_store)
|
|
|
|
(struct let name = ["protocols"] end))
|
|
|
|
(Protocol_hash)
|
2017-04-19 23:46:10 +04:00
|
|
|
|
|
|
|
module Contents =
|
|
|
|
Indexed_store.Make_map
|
|
|
|
(struct let name = ["contents"] end)
|
2017-04-19 21:21:23 +04:00
|
|
|
(Store_helpers.Make_value(Protocol))
|
2017-04-19 23:46:10 +04:00
|
|
|
|
|
|
|
module RawContents =
|
|
|
|
Store_helpers.Make_single_store
|
|
|
|
(Indexed_store.Store)
|
|
|
|
(struct let name = ["contents"] end)
|
|
|
|
(Store_helpers.Raw_value)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-03-30 19:19:59 +04:00
|
|
|
let register s =
|
|
|
|
Base58.register_resolver Protocol_hash.b58check_encoding begin fun str ->
|
|
|
|
let pstr = Protocol_hash.prefix_path str in
|
|
|
|
Indexed_store.resolve_index s pstr
|
|
|
|
end
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-03-30 19:19:59 +04:00
|
|
|
let init dir =
|
|
|
|
Raw_store.init dir >>=? fun s ->
|
2017-04-19 23:46:10 +04:00
|
|
|
Block.register s ;
|
2017-03-30 19:19:59 +04:00
|
|
|
Protocol.register s ;
|
|
|
|
return s
|
|
|
|
|
2017-05-31 20:27:11 +04:00
|
|
|
let close = Raw_store.close
|