2017-02-24 20:17:53 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2017-02-24 20:17:53 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Tezos Shell - High-level API for the Gossip network and local storage. *)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type t
|
|
|
|
type db = t
|
|
|
|
|
|
|
|
module Message = Distributed_db_message
|
|
|
|
module Metadata = Distributed_db_metadata
|
|
|
|
|
|
|
|
type p2p = (Message.t, Metadata.t) P2p.net
|
|
|
|
|
|
|
|
val create: State.t -> p2p -> t
|
2017-11-09 14:26:25 +04:00
|
|
|
val state: db -> State.t
|
2017-02-24 20:17:53 +04:00
|
|
|
val shutdown: t -> unit Lwt.t
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** {1 Network database} *)
|
|
|
|
|
|
|
|
(** An instance of the distributed DB for a given network (mainnet,
|
|
|
|
current testnet, ...) *)
|
2017-04-19 23:46:10 +04:00
|
|
|
type net_db
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Activate a given network. The node will notify its neighbours that
|
|
|
|
it now handles the given network and that it expects notification
|
|
|
|
for new head or new operations. *)
|
|
|
|
val activate: t -> State.Net.t -> net_db
|
|
|
|
|
|
|
|
(** Deactivate a given network. The node will notify its neighbours
|
|
|
|
that it does not care anymore about this network. *)
|
|
|
|
val deactivate: net_db -> unit Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
type callback = {
|
2017-11-13 22:06:30 +04:00
|
|
|
notify_branch: P2p.Peer_id.t -> Block_locator.t -> unit ;
|
|
|
|
notify_head: P2p.Peer_id.t -> Block_header.t -> Mempool.t -> unit ;
|
2017-02-24 20:17:53 +04:00
|
|
|
disconnection: P2p.Peer_id.t -> unit ;
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Register all the possible callback from the distributed DB to the
|
|
|
|
validator. *)
|
2017-09-29 20:43:13 +04:00
|
|
|
val set_callback: net_db -> callback -> unit
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Kick a given peer. *)
|
2017-09-29 20:43:13 +04:00
|
|
|
val disconnect: net_db -> P2p.Peer_id.t -> unit Lwt.t
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Various accessors. *)
|
|
|
|
val net_state: net_db -> State.Net.t
|
|
|
|
val db: net_db -> db
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** {1 Sending messages} *)
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
module Request : sig
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Send to a given peer, or to all known active peers for the
|
|
|
|
network, a friendly request "Hey, what's your current branch
|
|
|
|
?". The expected answer is a `Block_locator.t.`. *)
|
|
|
|
val current_branch: net_db -> ?peer:P2p.Peer_id.t -> unit -> unit
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Send to a given peer, or to all known active peers for the
|
|
|
|
given network, a friendly request "Hey, what's your current
|
|
|
|
branch ?". The expected answer is a `Block_locator.t.`. *)
|
|
|
|
val current_head: net_db -> ?peer:P2p.Peer_id.t -> unit -> unit
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
end
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
module Advertise : sig
|
|
|
|
|
|
|
|
(** Notify a given peer, or all known active peers for the
|
|
|
|
network, of a new head and possibly of new operations. *)
|
|
|
|
val current_head:
|
|
|
|
net_db -> ?peer:P2p.Peer_id.t ->
|
|
|
|
?mempool:Mempool.t -> State.Block.t -> unit
|
|
|
|
|
|
|
|
(** Notify a given peer, or all known active peers for the
|
|
|
|
network, of a new head and its sparse history. *)
|
|
|
|
val current_branch:
|
|
|
|
net_db -> ?peer:P2p.Peer_id.t ->
|
|
|
|
State.Block.t -> unit Lwt.t
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
(** {1 Indexes} *)
|
|
|
|
|
|
|
|
(** Generic interface for a "distributed" index.
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
By "distributed", it means that this interface abstract the p2p
|
|
|
|
gossip layer and it is able to fetch missing data from known
|
|
|
|
peers in a "synchronous" interface.
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
*)
|
2017-02-24 20:17:53 +04:00
|
|
|
module type DISTRIBUTED_DB = sig
|
2017-11-13 22:06:30 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type t
|
2017-11-13 22:06:30 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type key
|
2017-11-13 22:06:30 +04:00
|
|
|
(** The index key *)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type value
|
2017-11-13 22:06:30 +04:00
|
|
|
(** The indexed data *)
|
|
|
|
|
|
|
|
(** Is the value known locally? *)
|
2017-02-24 20:17:53 +04:00
|
|
|
val known: t -> key -> bool Lwt.t
|
2017-11-13 22:06:30 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
type error += Missing_data of key
|
2017-11-13 22:06:30 +04:00
|
|
|
|
|
|
|
(** Return the value if it is known locally, otherwise fail with
|
|
|
|
the error [Missing_data]. *)
|
2017-04-19 23:46:10 +04:00
|
|
|
val read: t -> key -> value tzresult Lwt.t
|
2017-11-13 22:06:30 +04:00
|
|
|
|
|
|
|
(** Return the value if it is known locally, otherwise fail with
|
|
|
|
the value [None]. *)
|
2017-04-19 23:46:10 +04:00
|
|
|
val read_opt: t -> key -> value option Lwt.t
|
2017-11-13 22:06:30 +04:00
|
|
|
|
|
|
|
(** Return the value if it is known locally, otherwise fail with
|
|
|
|
the exception [Not_found]. *)
|
2017-02-24 20:17:53 +04:00
|
|
|
val read_exn: t -> key -> value Lwt.t
|
2017-11-13 22:06:30 +04:00
|
|
|
|
|
|
|
type param (** An extra parameter for the network lookup, usually
|
|
|
|
used for prevalidating data. *)
|
|
|
|
|
|
|
|
type error += Timeout of key
|
|
|
|
|
|
|
|
(** Return the value if it is known locally, or block until the data
|
|
|
|
is received from the network. By default, the data will be
|
|
|
|
requested to all the active peers in the network; if the [peer]
|
|
|
|
argument is provided, the data will only be requested to the
|
|
|
|
provided peer. By default, the resulting promise will block
|
|
|
|
forever if the data is never received. If [timeout] is provided
|
|
|
|
the promise will be resolved with the error [Timeout] after the
|
|
|
|
provided amount of seconds.
|
|
|
|
|
|
|
|
A internal scheduler is able to re-send the request with an
|
|
|
|
exponential back-off until the data is received. If the function
|
|
|
|
is called multiple time with the same key but with disctinct
|
|
|
|
peers, the internal scheduler randomly chooses the requested
|
|
|
|
peer (at each retry). *)
|
|
|
|
val fetch:
|
2017-11-10 23:30:29 +04:00
|
|
|
t ->
|
|
|
|
?peer:P2p.Peer_id.t ->
|
|
|
|
?timeout:float ->
|
2017-11-13 22:06:30 +04:00
|
|
|
key -> param -> value tzresult Lwt.t
|
|
|
|
|
|
|
|
(** Same as `fetch` but the call is non-blocking: the data will be
|
|
|
|
stored in the local index when received. *)
|
|
|
|
val prefetch:
|
2017-11-08 15:52:10 +04:00
|
|
|
t ->
|
|
|
|
?peer:P2p.Peer_id.t ->
|
|
|
|
?timeout:float ->
|
2017-11-13 22:06:30 +04:00
|
|
|
key -> param -> unit
|
|
|
|
|
|
|
|
type error += Canceled of key
|
|
|
|
|
|
|
|
(** Remove the data from the local index or cancel all pending
|
|
|
|
request. Any pending [fetch] promises are resolved with the
|
|
|
|
error [Canceled]. *)
|
2017-11-06 18:23:06 +04:00
|
|
|
val clear_or_cancel: t -> key -> unit
|
2017-11-13 22:06:30 +04:00
|
|
|
|
|
|
|
(** Monitor all the fetched data. A given data will appear only
|
|
|
|
once. *)
|
|
|
|
val watch: t -> (key * value) Lwt_stream.t * Watcher.stopper
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** {2 Block index} *)
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Index of block headers. *)
|
|
|
|
module Block_header : sig
|
|
|
|
type t = Block_header.t (* avoid shadowing. *)
|
|
|
|
include DISTRIBUTED_DB with type t := net_db
|
|
|
|
and type key := Block_hash.t
|
|
|
|
and type value := Block_header.t
|
|
|
|
and type param := unit
|
|
|
|
end
|
|
|
|
|
|
|
|
(** Index of all the operations of a given block (per validation pass). *)
|
2017-04-19 23:46:10 +04:00
|
|
|
module Operations :
|
2017-11-13 22:06:30 +04:00
|
|
|
DISTRIBUTED_DB with type t := net_db
|
2017-04-19 23:46:10 +04:00
|
|
|
and type key = Block_hash.t * int
|
|
|
|
and type value = Operation.t list
|
|
|
|
and type param := Operation_list_list_hash.t
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Index of all the hashes of operations of a given block (per
|
|
|
|
validation pass). *)
|
2017-04-19 23:46:10 +04:00
|
|
|
module Operation_hashes :
|
2017-11-13 22:06:30 +04:00
|
|
|
DISTRIBUTED_DB with type t := net_db
|
2017-04-19 23:46:10 +04:00
|
|
|
and type key = Block_hash.t * int
|
|
|
|
and type value = Operation_hash.t list
|
|
|
|
and type param := Operation_list_list_hash.t
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Store on disk all the data associated to a valid block. *)
|
|
|
|
val commit_block:
|
|
|
|
net_db ->
|
|
|
|
Block_hash.t ->
|
|
|
|
Block_header.t -> Operation.t list list ->
|
|
|
|
Updater.validation_result ->
|
|
|
|
State.Block.t option tzresult Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Store on disk all the data associated to an invalid block. *)
|
|
|
|
val commit_invalid_block:
|
|
|
|
net_db ->
|
|
|
|
Block_hash.t -> Block_header.t -> Error_monad.error list ->
|
|
|
|
bool tzresult Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Monitor all the fetched block headers (for all activate networks). *)
|
|
|
|
val watch_block_header:
|
|
|
|
t -> (Block_hash.t * Block_header.t) Lwt_stream.t * Watcher.stopper
|
|
|
|
|
|
|
|
|
|
|
|
(** {2 Operations index} *)
|
|
|
|
|
|
|
|
(** Index of operations (for the mempool). *)
|
|
|
|
module Operation : sig
|
|
|
|
type t = Operation.t (* avoid shadowing. *)
|
|
|
|
include DISTRIBUTED_DB with type t := net_db
|
|
|
|
and type key := Operation_hash.t
|
|
|
|
and type value := Operation.t
|
|
|
|
and type param := unit
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2017-11-11 06:34:12 +04:00
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Inject a new operation in the local index (memory only). *)
|
|
|
|
val inject_operation:
|
|
|
|
net_db -> Operation_hash.t -> Operation.t -> bool tzresult Lwt.t
|
|
|
|
|
|
|
|
(** Monitor all the fetched operations (for all activate networks). *)
|
|
|
|
val watch_operation:
|
|
|
|
t -> (Operation_hash.t * Operation.t) Lwt_stream.t * Watcher.stopper
|
|
|
|
|
|
|
|
(** {2 Protocol index} *)
|
|
|
|
|
|
|
|
(** Index of protocol sources. *)
|
|
|
|
module Protocol : sig
|
|
|
|
type t = Protocol.t (* avoid shadowing. *)
|
|
|
|
include DISTRIBUTED_DB with type t := db
|
|
|
|
and type key := Protocol_hash.t
|
|
|
|
and type value := Protocol.t
|
|
|
|
and type param := unit
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
|
|
|
|
2017-11-13 22:06:30 +04:00
|
|
|
(** Store on disk protocol sources. *)
|
|
|
|
val commit_protocol:
|
|
|
|
db -> Protocol_hash.t -> Protocol.t -> bool tzresult Lwt.t
|
|
|
|
|
|
|
|
(**/**)
|
|
|
|
|
|
|
|
module Raw : sig
|
|
|
|
val encoding: Message.t P2p.Raw.t Data_encoding.t
|
|
|
|
val supported_versions: P2p_types.Version.t list
|
2017-11-11 06:34:12 +04:00
|
|
|
end
|
|
|
|
|