2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Logging.Node.State
|
|
|
|
|
|
|
|
type error +=
|
2017-03-30 15:16:21 +04:00
|
|
|
| Invalid_fitness of { block: Block_hash.t ;
|
|
|
|
expected: Fitness.fitness ;
|
|
|
|
found: Fitness.fitness }
|
|
|
|
| Invalid_operations of { block: Block_hash.t ;
|
|
|
|
expected: Operation_list_list_hash.t ;
|
|
|
|
found: Operation_hash.t list list }
|
2017-02-24 20:17:53 +04:00
|
|
|
| Unknown_network of Net_id.t
|
|
|
|
| Unknown_operation of Operation_hash.t
|
|
|
|
| Unknown_block of Block_hash.t
|
|
|
|
| Unknown_context of Block_hash.t
|
2016-09-08 21:13:10 +04:00
|
|
|
| Unknown_protocol of Protocol_hash.t
|
|
|
|
| Cannot_parse
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Error_monad.register_error_kind
|
2017-02-25 21:01:27 +04:00
|
|
|
`Permanent
|
2016-09-08 21:13:10 +04:00
|
|
|
~id:"state.invalid_fitness"
|
|
|
|
~title:"Invalid fitness"
|
|
|
|
~description:"The computed fitness differs from the fitness found \
|
|
|
|
\ in the block header."
|
2017-03-30 15:16:21 +04:00
|
|
|
~pp:(fun ppf (block, expected, found) ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Format.fprintf ppf
|
2017-03-30 15:16:21 +04:00
|
|
|
"@[<v 2>Invalid fitness for block %a@ \
|
2016-09-08 21:13:10 +04:00
|
|
|
\ expected %a@ \
|
|
|
|
\ found %a"
|
2017-03-30 15:16:21 +04:00
|
|
|
Block_hash.pp_short block
|
2016-09-08 21:13:10 +04:00
|
|
|
Fitness.pp expected
|
|
|
|
Fitness.pp found)
|
2017-03-30 15:16:21 +04:00
|
|
|
Data_encoding.(obj3
|
|
|
|
(req "block" Block_hash.encoding)
|
2016-09-08 21:13:10 +04:00
|
|
|
(req "expected" Fitness.encoding)
|
|
|
|
(req "found" Fitness.encoding))
|
2017-03-30 15:16:21 +04:00
|
|
|
(function Invalid_fitness { block ; expected ; found } ->
|
|
|
|
Some (block, expected, found) | _ -> None)
|
|
|
|
(fun (block, expected, found) ->
|
|
|
|
Invalid_fitness { block ; expected ; found }) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
Error_monad.register_error_kind
|
|
|
|
`Temporary
|
|
|
|
~id:"state.unknown_network"
|
|
|
|
~title:"Unknown network"
|
|
|
|
~description:"TODO"
|
2017-02-24 20:17:53 +04:00
|
|
|
~pp:(fun ppf id ->
|
|
|
|
Format.fprintf ppf "Unknown network %a" Net_id.pp id)
|
2017-03-31 15:04:05 +04:00
|
|
|
Data_encoding.(obj1 (req "net" Net_id.encoding))
|
2016-09-08 21:13:10 +04:00
|
|
|
(function Unknown_network x -> Some x | _ -> None)
|
|
|
|
(fun x -> Unknown_network x) ;
|
|
|
|
|
|
|
|
(** *)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Shared : sig
|
|
|
|
type 'a t
|
|
|
|
val create: 'a -> 'a t
|
|
|
|
val use: 'a t -> ('a -> 'b Lwt.t) -> 'b Lwt.t
|
|
|
|
end = struct
|
|
|
|
type 'a t = {
|
|
|
|
data: 'a ;
|
|
|
|
lock: Lwt_mutex.t ;
|
|
|
|
}
|
|
|
|
let create data = { data ; lock = Lwt_mutex.create () }
|
|
|
|
let use { data ; lock } f =
|
|
|
|
Lwt_mutex.with_lock lock (fun () -> f data)
|
|
|
|
end
|
|
|
|
|
|
|
|
type global_state = {
|
|
|
|
global_data: global_data Shared.t ;
|
|
|
|
protocol_store: Store.Protocol.store Shared.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
and global_data = {
|
|
|
|
nets: net Net_id.Table.t ;
|
|
|
|
global_store: Store.t ;
|
|
|
|
init_index: Net_id.t -> Context.index Lwt.t ;
|
|
|
|
}
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
and net = {
|
2017-03-31 15:04:05 +04:00
|
|
|
id: Net_id.t ;
|
2017-02-24 20:17:53 +04:00
|
|
|
state: net_state Shared.t ;
|
|
|
|
genesis: genesis ;
|
|
|
|
expiration: Time.t option ;
|
|
|
|
forked_network_ttl: Int64.t option ;
|
|
|
|
operation_store: Store.Operation.store Shared.t ;
|
|
|
|
block_header_store: Store.Block_header.store Shared.t ;
|
|
|
|
valid_block_watcher: valid_block Watcher.input ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
and genesis = {
|
|
|
|
time: Time.t ;
|
|
|
|
block: Block_hash.t ;
|
|
|
|
protocol: Protocol_hash.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
and net_state = {
|
|
|
|
mutable current_head: valid_block ;
|
|
|
|
chain_store: Store.Chain.store ;
|
|
|
|
context_index: Context.index ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and valid_block = {
|
2017-02-24 20:17:53 +04:00
|
|
|
net_id: Net_id.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
hash: Block_hash.t ;
|
2017-04-10 15:01:22 +04:00
|
|
|
level: Int32.t ;
|
2017-04-10 14:58:31 +04:00
|
|
|
predecessor: Block_hash.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
timestamp: Time.t ;
|
|
|
|
fitness: Protocol.fitness ;
|
2017-03-30 15:16:21 +04:00
|
|
|
operations_hash: Operation_list_list_hash.t ;
|
|
|
|
operations: Operation_hash.t list list ;
|
2016-09-08 21:13:10 +04:00
|
|
|
discovery_time: Time.t ;
|
|
|
|
protocol_hash: Protocol_hash.t ;
|
|
|
|
protocol: (module Updater.REGISTRED_PROTOCOL) option ;
|
|
|
|
test_protocol_hash: Protocol_hash.t ;
|
|
|
|
test_protocol: (module Updater.REGISTRED_PROTOCOL) option ;
|
2017-02-24 20:17:53 +04:00
|
|
|
test_network: (Net_id.t * Time.t) option ;
|
2016-09-08 21:13:10 +04:00
|
|
|
context: Context.t ;
|
2017-02-24 20:17:53 +04:00
|
|
|
successors: Block_hash.Set.t ;
|
|
|
|
invalid_successors: Block_hash.Set.t ;
|
2017-03-22 20:21:52 +04:00
|
|
|
proto_header: MBytes.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let build_valid_block
|
2017-03-30 15:16:21 +04:00
|
|
|
hash header operations
|
|
|
|
context discovery_time successors invalid_successors =
|
2017-02-24 20:17:53 +04:00
|
|
|
Context.get_protocol context >>= fun protocol_hash ->
|
|
|
|
Context.get_test_protocol context >>= fun test_protocol_hash ->
|
|
|
|
Context.get_test_network context >>= fun test_network ->
|
|
|
|
Context.get_test_network_expiration
|
|
|
|
context >>= fun test_network_expiration ->
|
|
|
|
let test_network =
|
|
|
|
match test_network, test_network_expiration with
|
|
|
|
| None, _ | _, None -> None
|
|
|
|
| Some net_id, Some time -> Some (net_id, time) in
|
|
|
|
let protocol = Updater.get protocol_hash in
|
|
|
|
let test_protocol = Updater.get test_protocol_hash in
|
|
|
|
let valid_block = {
|
2017-03-22 20:21:52 +04:00
|
|
|
net_id = header.Store.Block_header.shell.net_id ;
|
2017-02-24 20:17:53 +04:00
|
|
|
hash ;
|
2017-04-10 15:01:22 +04:00
|
|
|
level = header.shell.level ;
|
2017-04-10 14:58:31 +04:00
|
|
|
predecessor = header.shell.predecessor ;
|
2017-03-22 20:21:52 +04:00
|
|
|
timestamp = header.shell.timestamp ;
|
2017-02-24 20:17:53 +04:00
|
|
|
discovery_time ;
|
2017-04-10 19:06:11 +04:00
|
|
|
operations_hash = header.shell.operations_hash ;
|
2017-03-30 15:16:21 +04:00
|
|
|
operations ;
|
2017-03-22 20:21:52 +04:00
|
|
|
fitness = header.shell.fitness ;
|
2017-02-24 20:17:53 +04:00
|
|
|
protocol_hash ;
|
|
|
|
protocol ;
|
|
|
|
test_protocol_hash ;
|
|
|
|
test_protocol ;
|
|
|
|
test_network ;
|
|
|
|
context ;
|
|
|
|
successors ;
|
|
|
|
invalid_successors ;
|
2017-03-22 20:21:52 +04:00
|
|
|
proto_header = header.Store.Block_header.proto ;
|
2017-02-24 20:17:53 +04:00
|
|
|
} in
|
|
|
|
Lwt.return valid_block
|
|
|
|
|
|
|
|
type t = global_state
|
|
|
|
|
|
|
|
module type DATA_STORE = sig
|
|
|
|
|
|
|
|
type store
|
|
|
|
type key
|
|
|
|
type value
|
|
|
|
|
|
|
|
val known: store -> key -> bool Lwt.t
|
|
|
|
|
|
|
|
(** Read a value in the local database. *)
|
|
|
|
val read: store -> key -> value tzresult Lwt.t
|
|
|
|
val read_opt: store -> key -> value option Lwt.t
|
|
|
|
val read_exn: store -> key -> value Lwt.t
|
|
|
|
|
|
|
|
(** Read a value in the local database (without parsing). *)
|
|
|
|
val read_raw: store -> key -> MBytes.t tzresult Lwt.t
|
|
|
|
val read_raw_opt: store -> key -> MBytes.t option Lwt.t
|
|
|
|
val read_raw_exn: store -> key -> MBytes.t Lwt.t
|
|
|
|
|
|
|
|
(** Read data discovery time (the time when `store` was called). *)
|
|
|
|
val read_discovery_time: store -> key -> Time.t tzresult Lwt.t
|
|
|
|
val read_discovery_time_opt: store -> key -> Time.t option Lwt.t
|
|
|
|
val read_discovery_time_exn: store -> key -> Time.t Lwt.t
|
|
|
|
|
2017-03-28 15:31:41 +04:00
|
|
|
val store: store -> key -> value -> bool Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
val store_raw: store -> key -> MBytes.t -> value option tzresult Lwt.t
|
|
|
|
val remove: store -> key -> bool Lwt.t
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module type INTERNAL_DATA_STORE = sig
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
include DATA_STORE
|
|
|
|
|
|
|
|
val read_full: store -> key -> value tzresult Time.timed_data option Lwt.t
|
|
|
|
|
|
|
|
val mark_valid: store -> key -> bool Lwt.t
|
|
|
|
val mark_invalid: store -> key -> error list -> bool Lwt.t
|
|
|
|
val unmark: store -> key -> bool Lwt.t
|
|
|
|
|
|
|
|
val pending: store -> key -> bool Lwt.t
|
|
|
|
val valid: store -> key -> bool Lwt.t
|
|
|
|
val invalid: store -> key -> error list option Lwt.t
|
|
|
|
|
|
|
|
type key_set
|
|
|
|
val list_invalid: store -> key_set Lwt.t
|
|
|
|
val list_pending: store -> key_set Lwt.t
|
|
|
|
|
|
|
|
val list: store -> key_set Lwt.t
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let wrap_not_found f s k =
|
|
|
|
f s k >>= function
|
|
|
|
| None -> Lwt.fail Not_found
|
|
|
|
| Some v -> Lwt.return v
|
|
|
|
|
|
|
|
module Make_data_store
|
|
|
|
(S : Store.DATA_STORE)
|
|
|
|
(U : sig
|
|
|
|
type store
|
|
|
|
val use: store -> (S.store -> 'a Lwt.t) -> 'a Lwt.t
|
|
|
|
val unknown: S.key -> 'a tzresult Lwt.t
|
|
|
|
end)
|
|
|
|
(Set : Set.S with type elt = S.key and type t = S.key_set) : sig
|
|
|
|
include INTERNAL_DATA_STORE with type store = U.store
|
|
|
|
and type key = S.key
|
|
|
|
and type key_set := Set.t
|
|
|
|
and type value = S.value
|
|
|
|
module Locked : INTERNAL_DATA_STORE with type store = S.store
|
|
|
|
and type key = S.key
|
|
|
|
and type key_set := Set.t
|
|
|
|
and type value = S.value
|
|
|
|
end = struct
|
|
|
|
|
|
|
|
type store = U.store
|
|
|
|
type value = S.value
|
|
|
|
type key = S.key
|
|
|
|
type key_set = Set.t
|
|
|
|
|
|
|
|
let of_bytes = Data_encoding.Binary.of_bytes S.encoding
|
|
|
|
let to_bytes = Data_encoding.Binary.to_bytes S.encoding
|
|
|
|
|
|
|
|
(* FIXME Document and check with a clear mind the invariant in the
|
|
|
|
storage... *)
|
|
|
|
|
|
|
|
module Locked = struct
|
|
|
|
type store = S.store
|
|
|
|
type value = S.value
|
|
|
|
type key = S.key
|
|
|
|
type key_set = Set.t
|
|
|
|
let known s k = S.Discovery_time.known s k
|
|
|
|
let read s k = S.Contents.read (s, k)
|
|
|
|
let read_opt s k = S.Contents.read_opt (s, k)
|
|
|
|
let read_exn s k = S.Contents.read_exn (s, k)
|
|
|
|
let read_raw s k = S.RawContents.read (s, k)
|
|
|
|
let read_raw_opt s k = S.RawContents.read_opt (s, k)
|
|
|
|
let read_raw_exn s k = S.RawContents.read_exn (s, k)
|
|
|
|
let read_discovery_time s k = S.Discovery_time.read s k
|
|
|
|
let read_discovery_time_opt s k = S.Discovery_time.read_opt s k
|
|
|
|
let read_discovery_time_exn s k = S.Discovery_time.read_exn s k
|
|
|
|
let read_full s k =
|
|
|
|
S.Discovery_time.read_opt s k >>= function
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
| Some time ->
|
|
|
|
S.Errors.read_opt s k >>= function
|
|
|
|
| Some exns -> Lwt.return (Some { Time.data = Error exns ; time })
|
|
|
|
| None ->
|
|
|
|
S.Contents.read_opt (s, k) >>= function
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
| Some v -> Lwt.return (Some { Time.data = Ok v ; time })
|
2017-03-28 15:31:41 +04:00
|
|
|
let store s k v =
|
2017-02-24 20:17:53 +04:00
|
|
|
S.Discovery_time.known s k >>= function
|
|
|
|
| true -> Lwt.return_false
|
|
|
|
| false ->
|
|
|
|
let time = Time.now () in
|
2017-03-28 15:31:41 +04:00
|
|
|
S.Contents.store (s, k) v >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
S.Discovery_time.store s k time >>= fun () ->
|
|
|
|
S.Pending.store s k >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
let store_raw s k b =
|
|
|
|
S.Discovery_time.known s k >>= function
|
|
|
|
| true -> return None
|
|
|
|
| false ->
|
|
|
|
match Data_encoding.Binary.of_bytes S.encoding b with
|
|
|
|
| None ->
|
|
|
|
S.Errors.store s k [Cannot_parse] >>= fun () ->
|
|
|
|
fail Cannot_parse
|
|
|
|
| Some v ->
|
|
|
|
let time = Time.now () in
|
|
|
|
S.RawContents.store (s, k) b >>= fun () ->
|
|
|
|
S.Discovery_time.store s k time >>= fun () ->
|
|
|
|
return (Some v)
|
|
|
|
let remove s k =
|
|
|
|
S.Discovery_time.known s k >>= function
|
|
|
|
| false -> Lwt.return_false
|
|
|
|
| true ->
|
|
|
|
S.Discovery_time.remove s k >>= fun () ->
|
|
|
|
S.Contents.remove (s, k) >>= fun () ->
|
|
|
|
S.Validation_time.remove (s, k) >>= fun () ->
|
|
|
|
S.Errors.remove s k >>= fun () ->
|
|
|
|
S.Pending.remove s k >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
let pending s k = S.Pending.known s k
|
|
|
|
let valid s k =
|
|
|
|
S.Validation_time.known (s, k) >>= fun validated ->
|
|
|
|
S.Errors.known s k >>= fun invalid ->
|
|
|
|
Lwt.return (validated && not invalid)
|
|
|
|
let invalid s k =
|
|
|
|
S.Validation_time.known (s, k) >>= fun validated ->
|
|
|
|
if validated then
|
|
|
|
S.Errors.read_opt s k
|
|
|
|
else
|
|
|
|
Lwt.return None
|
|
|
|
let mark_valid s k =
|
|
|
|
S.Pending.known s k >>= fun pending ->
|
|
|
|
if not pending then
|
|
|
|
Lwt.return_false
|
|
|
|
else
|
|
|
|
S.Pending.remove s k >>= fun () ->
|
|
|
|
S.Validation_time.store (s, k) (Time.now ()) >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
let mark_invalid s k e =
|
|
|
|
S.Discovery_time.known s k >>= fun pending ->
|
|
|
|
if not pending then
|
|
|
|
let now = Time.now () in
|
|
|
|
S.Discovery_time.store s k now >>= fun () ->
|
|
|
|
S.Validation_time.store (s, k) now >>= fun () ->
|
|
|
|
S.Errors.store s k e >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
else
|
|
|
|
S.Errors.known s k >>= fun invalid ->
|
|
|
|
if invalid then
|
|
|
|
Lwt.return_false
|
|
|
|
else
|
|
|
|
S.Pending.remove s k >>= fun () ->
|
|
|
|
S.Validation_time.store (s, k) (Time.now ()) >>= fun () ->
|
|
|
|
S.Errors.store s k e >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
let list_invalid s =
|
|
|
|
S.Errors.fold_keys s ~init:Set.empty
|
|
|
|
~f:(fun k acc -> Lwt.return (Set.add k acc))
|
|
|
|
let unmark s k =
|
|
|
|
S.Pending.known s k >>= fun pending ->
|
|
|
|
if not pending then
|
|
|
|
S.Validation_time.remove (s, k) >>= fun () ->
|
|
|
|
S.Errors.remove s k >>= fun () ->
|
|
|
|
S.Pending.store s k >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
else
|
|
|
|
Lwt.return_false
|
|
|
|
let list_pending = S.Pending.read_all
|
|
|
|
let list s =
|
|
|
|
S.Discovery_time.fold_keys s ~init:Set.empty
|
|
|
|
~f:(fun k acc -> Lwt.return (Set.add k acc))
|
|
|
|
end
|
2016-10-21 16:01:20 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let atomic1 f s = U.use s f
|
|
|
|
let atomic2 f s k = U.use s (fun s -> f s k)
|
|
|
|
let atomic3 f s k v = U.use s (fun s -> f s k v)
|
|
|
|
|
|
|
|
let known = atomic2 Locked.known
|
|
|
|
let read = atomic2 Locked.read
|
|
|
|
let read_opt = atomic2 Locked.read_opt
|
|
|
|
let read_exn = atomic2 Locked.read_exn
|
|
|
|
let read_raw = atomic2 Locked.read_raw
|
|
|
|
let read_raw_opt = atomic2 Locked.read_raw_opt
|
|
|
|
let read_raw_exn = atomic2 Locked.read_raw_exn
|
|
|
|
let read_full = atomic2 Locked.read_full
|
|
|
|
let read_discovery_time = atomic2 Locked.read_discovery_time
|
|
|
|
let read_discovery_time_opt = atomic2 Locked.read_discovery_time_opt
|
|
|
|
let read_discovery_time_exn = atomic2 Locked.read_discovery_time_exn
|
2017-03-28 15:31:41 +04:00
|
|
|
let store = atomic3 Locked.store
|
2017-02-24 20:17:53 +04:00
|
|
|
let store_raw = atomic3 Locked.store_raw
|
|
|
|
let remove = atomic2 Locked.remove
|
|
|
|
let mark_valid = atomic2 Locked.mark_valid
|
|
|
|
let mark_invalid = atomic3 Locked.mark_invalid
|
|
|
|
let unmark = atomic2 Locked.unmark
|
|
|
|
let pending = atomic2 Locked.pending
|
|
|
|
let valid = atomic2 Locked.valid
|
|
|
|
let invalid = atomic2 Locked.invalid
|
|
|
|
let list_invalid = atomic1 Locked.list_invalid
|
|
|
|
let list_pending = atomic1 Locked.list_pending
|
|
|
|
let list = atomic1 Locked.list
|
2016-10-21 16:01:20 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2016-10-21 16:01:20 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Raw_operation =
|
|
|
|
Make_data_store
|
|
|
|
(Store.Operation)
|
|
|
|
(struct
|
|
|
|
type store = Store.Operation.store Shared.t
|
|
|
|
let use s = Shared.use s
|
|
|
|
let unknown k = fail (Unknown_operation k)
|
|
|
|
end)
|
|
|
|
(Operation_hash.Set)
|
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
module Raw_operation_list = struct
|
|
|
|
|
|
|
|
module Locked = struct
|
|
|
|
|
|
|
|
let known store (hash, ofs) =
|
|
|
|
Store.Block_header.Operation_list.known (store, hash) ofs
|
|
|
|
let read store (hash, ofs) =
|
|
|
|
Store.Block_header.Operation_list.read
|
|
|
|
(store, hash) ofs >>=? fun ops ->
|
|
|
|
Store.Block_header.Operation_list_path.read
|
|
|
|
(store, hash) ofs >>=? fun path ->
|
|
|
|
return (ops, path)
|
|
|
|
let read_opt store (hash, ofs) =
|
|
|
|
Store.Block_header.Operation_list.read_opt
|
|
|
|
(store, hash) ofs >>= function
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
| Some ops ->
|
|
|
|
Store.Block_header.Operation_list_path.read_exn
|
|
|
|
(store, hash) ofs >>= fun path ->
|
|
|
|
Lwt.return (Some (ops, path))
|
|
|
|
let read_exn store (hash, ofs) =
|
|
|
|
read_opt store (hash, ofs) >>= function
|
|
|
|
| None -> Lwt.fail Not_found
|
|
|
|
| Some (ops, path) -> Lwt.return (ops, path)
|
|
|
|
let store store (hash, ofs) (ops, path) =
|
|
|
|
Store.Block_header.Operation_list.known
|
|
|
|
(store, hash) ofs >>= function
|
|
|
|
| false ->
|
|
|
|
Store.Block_header.Operation_list.store
|
|
|
|
(store, hash) ofs ops >>= fun () ->
|
|
|
|
Store.Block_header.Operation_list_path.store
|
|
|
|
(store, hash) ofs path >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
| true ->
|
|
|
|
Lwt.return_false
|
|
|
|
|
|
|
|
let remove store (hash, ofs) =
|
|
|
|
Store.Block_header.Operation_list.known
|
|
|
|
(store, hash) ofs >>= function
|
|
|
|
| false ->
|
|
|
|
Lwt.return_false
|
|
|
|
| true ->
|
|
|
|
Store.Block_header.Operation_list.remove
|
|
|
|
(store, hash) ofs >>= fun () ->
|
|
|
|
Store.Block_header.Operation_list_path.remove
|
|
|
|
(store, hash) ofs >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
|
|
|
|
let read_count store hash =
|
|
|
|
Store.Block_header.Operation_list_count.read (store, hash)
|
|
|
|
|
|
|
|
let read_count_opt store hash =
|
|
|
|
read_count store hash >>= function
|
|
|
|
| Ok cpt -> Lwt.return (Some cpt)
|
|
|
|
| Error _ -> Lwt.return_none
|
|
|
|
|
|
|
|
let read_count_exn store hash =
|
|
|
|
read_count store hash >>= function
|
|
|
|
| Ok cpt -> Lwt.return cpt
|
|
|
|
| Error _ -> Lwt.fail Not_found
|
|
|
|
|
|
|
|
let store_count store hash count =
|
|
|
|
Store.Block_header.Operation_list_count.store (store, hash) count
|
|
|
|
|
|
|
|
let read_all store hash =
|
|
|
|
Store.Block_header.Operation_list_count.read (store, hash)
|
|
|
|
>>=? fun operation_list_count ->
|
|
|
|
let rec read acc i =
|
|
|
|
if i <= 0 then return acc
|
|
|
|
else
|
|
|
|
Store.Block_header.Operation_list.read
|
|
|
|
(store, hash) (i-1) >>=? fun ops ->
|
|
|
|
read (ops :: acc) (i-1) in
|
|
|
|
read [] operation_list_count
|
|
|
|
|
|
|
|
let read_all_exn store hash =
|
|
|
|
read_all store hash >>= function
|
|
|
|
| Error _ -> Lwt.fail Not_found
|
|
|
|
| Ok ops -> Lwt.return ops
|
|
|
|
|
|
|
|
let store_all store hash op_hashes operations =
|
|
|
|
Store.Block_header.Operation_list_count.store (store, hash)
|
|
|
|
(List.length operations) >>= fun () ->
|
|
|
|
Lwt_list.iteri_p
|
|
|
|
(fun i ops ->
|
|
|
|
Store.Block_header.Operation_list.store
|
|
|
|
(store, hash) i ops >>= fun () ->
|
|
|
|
Store.Block_header.Operation_list_path.store
|
|
|
|
(store, hash) i
|
|
|
|
(Operation_list_list_hash.compute_path op_hashes i)
|
|
|
|
>>= fun () ->
|
|
|
|
Lwt.return_unit)
|
|
|
|
operations >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
let atomic1 f s = Shared.use s f
|
|
|
|
let atomic2 f s k = Shared.use s (fun s -> f s k)
|
|
|
|
let atomic3 f s k v = Shared.use s (fun s -> f s k v)
|
|
|
|
let atomic4 f s k v1 v2 = Shared.use s (fun s -> f s k v1 v2)
|
|
|
|
|
|
|
|
let known = atomic2 Locked.known
|
|
|
|
let read = atomic2 Locked.read
|
|
|
|
let read_opt = atomic2 Locked.read_opt
|
|
|
|
let read_exn = atomic2 Locked.read_exn
|
|
|
|
let store = atomic3 Locked.store
|
|
|
|
let remove = atomic2 Locked.remove
|
|
|
|
|
|
|
|
let store_all = atomic4 Locked.store_all
|
|
|
|
let read_all = atomic2 Locked.read_all
|
|
|
|
let read_all_exn = atomic2 Locked.read_all_exn
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Raw_block_header = struct
|
|
|
|
|
|
|
|
include
|
|
|
|
Make_data_store
|
|
|
|
(Store.Block_header)
|
|
|
|
(struct
|
|
|
|
type store = Store.Block_header.store Shared.t
|
|
|
|
let use s = Shared.use s
|
|
|
|
let unknown k = fail (Unknown_block k)
|
|
|
|
end)
|
|
|
|
(Block_hash.Set)
|
|
|
|
|
|
|
|
let read_pred store k =
|
|
|
|
read_opt store k >>= function
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
| Some { shell = { predecessor } } ->
|
|
|
|
if Block_hash.equal predecessor k then
|
|
|
|
Lwt.return_none
|
|
|
|
else
|
|
|
|
Lwt.return (Some predecessor)
|
|
|
|
let read_pred_exn = wrap_not_found read_pred
|
|
|
|
|
|
|
|
let store_genesis store genesis =
|
|
|
|
let shell : Store.Block_header.shell_header = {
|
2017-03-31 15:04:05 +04:00
|
|
|
net_id = Net_id.of_block_hash genesis.block;
|
2017-04-10 15:01:22 +04:00
|
|
|
level = 0l ;
|
2017-02-24 20:17:53 +04:00
|
|
|
predecessor = genesis.block ;
|
|
|
|
timestamp = genesis.time ;
|
|
|
|
fitness = [] ;
|
2017-04-10 19:06:11 +04:00
|
|
|
operations_hash = Operation_list_list_hash.empty ;
|
2017-02-24 20:17:53 +04:00
|
|
|
} in
|
2017-03-22 20:21:52 +04:00
|
|
|
let header =
|
|
|
|
{ Store.Block_header.shell ; proto = MBytes.create 0 } in
|
2017-02-24 20:17:53 +04:00
|
|
|
let bytes =
|
2017-03-22 20:21:52 +04:00
|
|
|
Data_encoding.Binary.to_bytes Store.Block_header.encoding header in
|
2017-02-24 20:17:53 +04:00
|
|
|
Locked.store_raw store genesis.block bytes >>= fun _created ->
|
2017-03-30 15:16:21 +04:00
|
|
|
Raw_operation_list.Locked.store_all store genesis.block [] [] >>= fun () ->
|
2017-03-22 20:21:52 +04:00
|
|
|
Lwt.return header
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-04-10 15:01:22 +04:00
|
|
|
(* let store_testnet_genesis store genesis = *)
|
|
|
|
(* let shell : Store.Block_header.shell_header = { *)
|
|
|
|
(* net_id = Net_id.of_block_hash genesis.block; *)
|
|
|
|
(* level = 0l ; *)
|
|
|
|
(* predecessor = genesis.block ; *)
|
|
|
|
(* timestamp = genesis.time ; *)
|
|
|
|
(* fitness = [] ; *)
|
|
|
|
(* operations = Operation_list_list_hash.empty ; *)
|
|
|
|
(* } in *)
|
|
|
|
(* let bytes = *)
|
|
|
|
(* Data_encoding.Binary.to_bytes Store.Block_header.encoding { *)
|
|
|
|
(* shell ; *)
|
|
|
|
(* proto = MBytes.create 0 ; *)
|
|
|
|
(* } in *)
|
|
|
|
(* Locked.store_raw store genesis.block bytes >>= fun _created -> *)
|
|
|
|
(* Raw_operation_list.Locked.store_all store genesis.block [] [] >>= fun () -> *)
|
|
|
|
(* Lwt.return shell *)
|
2017-03-28 15:31:41 +04:00
|
|
|
|
2016-10-21 16:01:20 +04:00
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Raw_helpers = struct
|
|
|
|
|
|
|
|
let path store h1 h2 =
|
|
|
|
let rec loop acc h =
|
|
|
|
if Block_hash.equal h h1 then
|
|
|
|
Lwt.return (Some acc)
|
|
|
|
else
|
|
|
|
Raw_block_header.read_opt store h >>= function
|
|
|
|
| Some { shell = header }
|
|
|
|
when not (Block_hash.equal header.predecessor h) ->
|
|
|
|
loop ((h, header) :: acc) header.predecessor
|
|
|
|
| Some _ | None -> Lwt.return_none in
|
|
|
|
loop [] h2
|
|
|
|
|
|
|
|
let rec common_ancestor store hash1 header1 hash2 header2 =
|
|
|
|
if Block_hash.equal hash1 hash2 then
|
|
|
|
Lwt.return (Some (hash1, header1))
|
|
|
|
else if
|
|
|
|
Time.compare
|
|
|
|
header1.Store.Block_header.timestamp
|
|
|
|
header2.Store.Block_header.timestamp <= 0
|
|
|
|
then begin
|
|
|
|
if Block_hash.equal header2.predecessor hash2 then
|
|
|
|
Lwt.return_none
|
|
|
|
else
|
|
|
|
let hash2 = header2.predecessor in
|
|
|
|
Raw_block_header.read_opt store hash2 >>= function
|
|
|
|
| Some { shell = header2 } ->
|
|
|
|
common_ancestor store hash1 header1 hash2 header2
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
end else begin
|
|
|
|
if Block_hash.equal header1.predecessor hash1 then
|
|
|
|
Lwt.return_none
|
|
|
|
else
|
|
|
|
let hash1 = header1.predecessor in
|
|
|
|
Raw_block_header.read_opt store hash1 >>= function
|
|
|
|
| Some { shell = header1 } ->
|
|
|
|
common_ancestor store hash1 header1 hash2 header2
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
end
|
|
|
|
|
|
|
|
let block_locator store sz h =
|
|
|
|
let rec loop acc sz step cpt h =
|
|
|
|
if sz = 0 then Lwt.return (List.rev acc) else
|
|
|
|
Raw_block_header.read_pred store h >>= function
|
|
|
|
| None -> Lwt.return (List.rev (h :: acc))
|
|
|
|
| Some pred ->
|
|
|
|
if cpt = 0 then
|
|
|
|
loop (h :: acc) (sz - 1) (step * 2) (step * 20 - 1) pred
|
|
|
|
else if cpt mod step = 0 then
|
|
|
|
loop (h :: acc) (sz - 1) step (cpt - 1) pred
|
2016-09-22 17:34:38 +04:00
|
|
|
else
|
2017-02-24 20:17:53 +04:00
|
|
|
loop acc sz step (cpt - 1) pred in
|
|
|
|
loop [] sz 1 9 h
|
|
|
|
|
|
|
|
let iter_predecessors
|
|
|
|
(type state)
|
|
|
|
(type t)
|
|
|
|
(compare: t -> t -> int)
|
|
|
|
(predecessor: state -> t -> t option Lwt.t)
|
|
|
|
(date: t -> Time.t)
|
|
|
|
(fitness: t -> Fitness.fitness)
|
|
|
|
state ?max ?min_fitness ?min_date heads ~f =
|
|
|
|
let module Local = struct exception Exit end in
|
|
|
|
let pop, push =
|
|
|
|
(* Poor-man priority queue *)
|
|
|
|
let queue : t list ref = ref [] in
|
|
|
|
let pop () =
|
|
|
|
match !queue with
|
|
|
|
| [] -> None
|
|
|
|
| b :: bs -> queue := bs ; Some b in
|
|
|
|
let push b =
|
|
|
|
let rec loop = function
|
|
|
|
| [] -> [b]
|
|
|
|
| b' :: bs' as bs ->
|
|
|
|
let cmp = compare b b' in
|
|
|
|
if cmp = 0 then
|
|
|
|
bs
|
|
|
|
else if cmp < 0 then
|
|
|
|
b' :: loop bs'
|
|
|
|
else
|
|
|
|
b :: bs in
|
|
|
|
queue := loop !queue in
|
|
|
|
pop, push in
|
|
|
|
let check_count =
|
|
|
|
match max with
|
|
|
|
| None -> (fun () -> ())
|
|
|
|
| Some max ->
|
|
|
|
let cpt = ref 0 in
|
|
|
|
fun () ->
|
|
|
|
if !cpt >= max then raise Local.Exit ;
|
|
|
|
incr cpt in
|
|
|
|
let check_fitness =
|
|
|
|
match min_fitness with
|
|
|
|
| None -> (fun _ -> true)
|
|
|
|
| Some min_fitness ->
|
|
|
|
(fun b -> Fitness.compare min_fitness (fitness b) <= 0) in
|
|
|
|
let check_date =
|
|
|
|
match min_date with
|
|
|
|
| None -> (fun _ -> true)
|
|
|
|
| Some min_date -> (fun b -> Time.compare min_date (date b) <= 0) in
|
|
|
|
let rec loop () =
|
2016-09-22 17:34:38 +04:00
|
|
|
match pop () with
|
|
|
|
| None -> return ()
|
|
|
|
| Some b ->
|
|
|
|
check_count () ;
|
|
|
|
f b >>= fun () ->
|
|
|
|
predecessor state b >>= function
|
|
|
|
| None -> loop ()
|
|
|
|
| Some p ->
|
|
|
|
if check_fitness p && check_date p then push p ;
|
|
|
|
loop () in
|
|
|
|
List.iter push heads ;
|
|
|
|
try loop () with Local.Exit -> return ()
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Block_header = struct
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type shell_header = Store.Block_header.shell_header = {
|
|
|
|
net_id: Net_id.t ;
|
2017-04-10 15:01:22 +04:00
|
|
|
level: Int32.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
predecessor: Block_hash.t ;
|
|
|
|
timestamp: Time.t ;
|
2017-04-10 19:06:11 +04:00
|
|
|
operations_hash: Operation_list_list_hash.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
fitness: MBytes.t list ;
|
|
|
|
}
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
type t = Store.Block_header.t = {
|
2016-09-08 21:13:10 +04:00
|
|
|
shell: shell_header ;
|
|
|
|
proto: MBytes.t ;
|
|
|
|
}
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type block_header = t
|
|
|
|
|
|
|
|
include
|
|
|
|
Make_data_store
|
|
|
|
(Store.Block_header)
|
|
|
|
(struct
|
|
|
|
type store = net
|
|
|
|
let use s = Shared.use s.block_header_store
|
|
|
|
let unknown k = fail (Unknown_block k)
|
|
|
|
end)
|
|
|
|
(Block_hash.Set)
|
|
|
|
|
|
|
|
let read_pred_opt store k =
|
|
|
|
read_opt store k >>= function
|
|
|
|
| Some { shell = { predecessor } }
|
|
|
|
when not (Block_hash.equal predecessor k) ->
|
|
|
|
Lwt.return (Some predecessor)
|
|
|
|
| Some _ | None -> Lwt.return_none
|
|
|
|
let read_pred_exn = wrap_not_found read_pred_opt
|
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
let read_operations s k =
|
|
|
|
Raw_operation_list.read_all s.block_header_store k
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let mark_invalid net hash errors =
|
|
|
|
mark_invalid net hash errors >>= fun marked ->
|
|
|
|
if not marked then
|
|
|
|
Lwt.return_false
|
|
|
|
else begin
|
|
|
|
Raw_block_header.read_opt net.block_header_store hash >>= function
|
|
|
|
| Some { shell = { predecessor } } ->
|
|
|
|
Shared.use net.state begin fun state ->
|
|
|
|
Store.Chain.Valid_successors.remove
|
|
|
|
(state.chain_store, predecessor) hash >>= fun () ->
|
|
|
|
Store.Chain.Invalid_successors.store
|
|
|
|
(state.chain_store, predecessor) hash
|
|
|
|
end >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
| None ->
|
|
|
|
Lwt.return_true
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Helpers = struct
|
|
|
|
|
|
|
|
let check_block state h =
|
|
|
|
known state h >>= function
|
|
|
|
| true -> return ()
|
|
|
|
| false -> failwith "Unknown block %a" Block_hash.pp_short h
|
|
|
|
|
|
|
|
let path state h1 h2 =
|
|
|
|
trace_exn (Failure "State.path") begin
|
|
|
|
check_block state h1 >>=? fun () ->
|
|
|
|
check_block state h2 >>=? fun () ->
|
|
|
|
Raw_helpers.path state.block_header_store h1 h2 >>= function
|
|
|
|
| None -> failwith "not an ancestor"
|
|
|
|
| Some x -> return x
|
|
|
|
end
|
|
|
|
|
|
|
|
let common_ancestor state hash1 hash2 =
|
|
|
|
trace_exn (Failure "State.common_ancestor") begin
|
|
|
|
read_opt state hash1 >>= function
|
|
|
|
| None -> failwith "Unknown_block %a" Block_hash.pp_short hash1
|
|
|
|
| Some { shell = header1 } ->
|
|
|
|
read_opt state hash2 >>= function
|
|
|
|
| None -> failwith "Unknown_block %a" Block_hash.pp_short hash1
|
|
|
|
| Some { shell = header2 } ->
|
|
|
|
Raw_helpers.common_ancestor state.block_header_store
|
|
|
|
hash1 header1 hash2 header2 >>= function
|
|
|
|
| None -> failwith "No common ancestor found"
|
|
|
|
| Some (hash, header) -> return (hash, header)
|
|
|
|
end
|
|
|
|
|
|
|
|
let block_locator state sz h =
|
|
|
|
trace_exn (Failure "State.block_locator") begin
|
|
|
|
check_block state h >>=? fun () ->
|
|
|
|
Raw_helpers.block_locator
|
|
|
|
state.block_header_store sz h >>= fun locator ->
|
|
|
|
return locator
|
|
|
|
end
|
|
|
|
|
|
|
|
let iter_predecessors =
|
|
|
|
let compare b1 b2 =
|
|
|
|
match Fitness.compare b1.shell.fitness b2.shell.fitness with
|
|
|
|
| 0 -> begin
|
|
|
|
match Time.compare b1.shell.timestamp b2.shell.timestamp with
|
|
|
|
| 0 ->
|
|
|
|
Block_hash.compare
|
|
|
|
(Store.Block_header.hash b1) (Store.Block_header.hash b2)
|
|
|
|
| res -> res
|
|
|
|
end
|
|
|
|
| res -> res in
|
|
|
|
let predecessor net b =
|
|
|
|
if Block_hash.equal net.genesis.block b.shell.predecessor then
|
|
|
|
Lwt.return_none
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2017-02-24 20:17:53 +04:00
|
|
|
Raw_block_header.read_opt
|
|
|
|
net.block_header_store b.shell.predecessor in
|
|
|
|
Raw_helpers.iter_predecessors compare predecessor
|
|
|
|
(fun b -> b.shell.timestamp) (fun b -> b.shell.fitness)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
module Operation_list = struct
|
|
|
|
|
|
|
|
type store = net
|
|
|
|
type key = Block_hash.t * int
|
|
|
|
type value = Operation_hash.t list * Operation_list_list_hash.path
|
|
|
|
|
|
|
|
module Locked = Raw_operation_list.Locked
|
|
|
|
|
|
|
|
let atomic1 f s =
|
|
|
|
Shared.use s.block_header_store f
|
|
|
|
let atomic2 f s k =
|
|
|
|
Shared.use s.block_header_store (fun s -> f s k)
|
|
|
|
let atomic3 f s k v =
|
|
|
|
Shared.use s.block_header_store (fun s -> f s k v)
|
|
|
|
let atomic4 f s k v1 v2 =
|
|
|
|
Shared.use s.block_header_store (fun s -> f s k v1 v2)
|
|
|
|
|
|
|
|
let known = atomic2 Locked.known
|
|
|
|
let read = atomic2 Locked.read
|
|
|
|
let read_opt = atomic2 Locked.read_opt
|
|
|
|
let read_exn = atomic2 Locked.read_exn
|
|
|
|
let store = atomic3 Locked.store
|
|
|
|
let remove = atomic2 Locked.remove
|
|
|
|
|
|
|
|
let store_all s k v =
|
|
|
|
Shared.use s.block_header_store begin fun s ->
|
|
|
|
let h = List.map Operation_list_hash.compute v in
|
|
|
|
Locked.store_all s k h v
|
|
|
|
end
|
|
|
|
let read_all = atomic2 Locked.read_all
|
|
|
|
let read_all_exn = atomic2 Locked.read_all_exn
|
|
|
|
|
|
|
|
let read_count = atomic2 Locked.read_count
|
|
|
|
let read_count_opt = atomic2 Locked.read_count_opt
|
|
|
|
let read_count_exn = atomic2 Locked.read_count_exn
|
|
|
|
let store_count = atomic3 Locked.store_count
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Raw_net = struct
|
|
|
|
|
|
|
|
let build
|
|
|
|
~genesis
|
|
|
|
~genesis_block
|
|
|
|
~expiration
|
|
|
|
~forked_network_ttl
|
|
|
|
context_index
|
|
|
|
chain_store
|
|
|
|
block_header_store
|
|
|
|
operation_store =
|
|
|
|
let net_state = {
|
|
|
|
current_head = genesis_block ;
|
|
|
|
chain_store ;
|
|
|
|
context_index ;
|
|
|
|
} in
|
|
|
|
let net = {
|
2017-03-31 15:04:05 +04:00
|
|
|
id = Net_id.of_block_hash genesis.block ;
|
2017-02-24 20:17:53 +04:00
|
|
|
state = Shared.create net_state ;
|
|
|
|
genesis ;
|
|
|
|
expiration ;
|
|
|
|
operation_store = Shared.create operation_store ;
|
|
|
|
forked_network_ttl ;
|
|
|
|
block_header_store = Shared.create block_header_store ;
|
|
|
|
valid_block_watcher = Watcher.create_input ();
|
|
|
|
} in
|
|
|
|
net
|
|
|
|
|
|
|
|
let locked_create
|
|
|
|
data
|
|
|
|
?initial_context ?forked_network_ttl
|
|
|
|
?test_protocol ?expiration genesis =
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = Net_id.of_block_hash genesis.block in
|
|
|
|
let net_store = Store.Net.get data.global_store net_id in
|
2017-02-24 20:17:53 +04:00
|
|
|
let operation_store = Store.Operation.get net_store
|
|
|
|
and block_header_store = Store.Block_header.get net_store
|
|
|
|
and chain_store = Store.Chain.get net_store in
|
2017-03-31 15:04:05 +04:00
|
|
|
Store.Net.Genesis_hash.store net_store genesis.block >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Store.Net.Genesis_time.store net_store genesis.time >>= fun () ->
|
|
|
|
Store.Net.Genesis_protocol.store net_store genesis.protocol >>= fun () ->
|
|
|
|
let test_protocol = Utils.unopt ~default:genesis.protocol test_protocol in
|
|
|
|
Store.Net.Genesis_test_protocol.store net_store test_protocol >>= fun () ->
|
|
|
|
Store.Chain.Current_head.store chain_store genesis.block >>= fun () ->
|
|
|
|
Store.Chain.Known_heads.store chain_store genesis.block >>= fun () ->
|
2017-03-31 15:04:05 +04:00
|
|
|
data.init_index net_id >>= fun context_index ->
|
2017-02-24 20:17:53 +04:00
|
|
|
begin
|
|
|
|
match expiration with
|
|
|
|
| None -> Lwt.return_unit
|
|
|
|
| Some time -> Store.Net.Expiration.store net_store time
|
|
|
|
end >>= fun () ->
|
|
|
|
Raw_block_header.store_genesis
|
2017-03-22 20:21:52 +04:00
|
|
|
block_header_store genesis >>= fun header ->
|
2017-02-24 20:17:53 +04:00
|
|
|
begin
|
|
|
|
match initial_context with
|
|
|
|
| None ->
|
|
|
|
Context.commit_genesis
|
|
|
|
context_index
|
|
|
|
~id:genesis.block
|
|
|
|
~time:genesis.time
|
|
|
|
~protocol:genesis.protocol
|
|
|
|
~test_protocol
|
|
|
|
| Some context ->
|
|
|
|
Lwt.return context
|
|
|
|
end >>= fun context ->
|
|
|
|
build_valid_block
|
2017-03-30 15:16:21 +04:00
|
|
|
genesis.block header [] context genesis.time
|
2017-02-24 20:17:53 +04:00
|
|
|
Block_hash.Set.empty Block_hash.Set.empty >>= fun genesis_block ->
|
|
|
|
Lwt.return @@
|
|
|
|
build
|
|
|
|
~genesis
|
|
|
|
~genesis_block
|
|
|
|
~expiration
|
|
|
|
~forked_network_ttl
|
|
|
|
context_index
|
|
|
|
chain_store
|
|
|
|
block_header_store
|
|
|
|
operation_store
|
2016-09-22 17:34:38 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
module Valid_block = struct
|
|
|
|
|
|
|
|
type t = valid_block = {
|
2017-02-24 20:17:53 +04:00
|
|
|
net_id: Net_id.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
hash: Block_hash.t ;
|
2017-04-10 15:01:22 +04:00
|
|
|
level: Int32.t ;
|
2017-04-10 14:58:31 +04:00
|
|
|
predecessor: Block_hash.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
timestamp: Time.t ;
|
2016-10-21 16:01:20 +04:00
|
|
|
fitness: Fitness.fitness ;
|
2017-03-30 15:16:21 +04:00
|
|
|
operations_hash: Operation_list_list_hash.t ;
|
|
|
|
operations: Operation_hash.t list list ;
|
2016-09-08 21:13:10 +04:00
|
|
|
discovery_time: Time.t ;
|
|
|
|
protocol_hash: Protocol_hash.t ;
|
|
|
|
protocol: (module Updater.REGISTRED_PROTOCOL) option ;
|
|
|
|
test_protocol_hash: Protocol_hash.t ;
|
|
|
|
test_protocol: (module Updater.REGISTRED_PROTOCOL) option ;
|
2017-02-24 20:17:53 +04:00
|
|
|
test_network: (Net_id.t * Time.t) option ;
|
2016-09-08 21:13:10 +04:00
|
|
|
context: Context.t ;
|
2017-02-24 20:17:53 +04:00
|
|
|
successors: Block_hash.Set.t ;
|
|
|
|
invalid_successors: Block_hash.Set.t ;
|
2017-03-22 20:21:52 +04:00
|
|
|
proto_header: MBytes.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
|
|
|
type valid_block = t
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Locked = struct
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let known { context_index } hash =
|
|
|
|
Context.exists context_index hash
|
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
let raw_read block operations time chain_store context_index hash =
|
2017-02-24 20:17:53 +04:00
|
|
|
Context.checkout context_index hash >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| None ->
|
2017-02-24 20:17:53 +04:00
|
|
|
fail (Unknown_context hash)
|
|
|
|
| Some context ->
|
|
|
|
Store.Chain.Valid_successors.read_all (chain_store, hash)
|
|
|
|
>>= fun successors ->
|
|
|
|
Store.Chain.Invalid_successors.read_all (chain_store, hash)
|
|
|
|
>>= fun invalid_successors ->
|
2017-03-30 15:16:21 +04:00
|
|
|
build_valid_block hash block operations
|
|
|
|
context time successors invalid_successors >>= fun block ->
|
2017-02-24 20:17:53 +04:00
|
|
|
return block
|
|
|
|
|
2017-03-30 15:16:21 +04:00
|
|
|
let raw_read_exn block operations time chain_store context_index hash =
|
|
|
|
raw_read block operations time chain_store context_index hash >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| Error _ -> Lwt.fail Not_found
|
|
|
|
| Ok data -> Lwt.return data
|
|
|
|
|
|
|
|
let read net net_state hash =
|
|
|
|
Block_header.read_full net hash >>= function
|
|
|
|
| None | Some { Time.data = Error _ } ->
|
|
|
|
fail (Unknown_block hash)
|
|
|
|
| Some { Time.data = Ok block ; time } ->
|
2017-03-30 15:16:21 +04:00
|
|
|
Block_header.read_operations net hash >>=? fun operations ->
|
|
|
|
raw_read block operations
|
2017-02-24 20:17:53 +04:00
|
|
|
time net_state.chain_store net_state.context_index hash
|
|
|
|
|
|
|
|
let read_opt net net_state hash =
|
|
|
|
read net net_state hash >>= function
|
|
|
|
| Error _ -> Lwt.return_none
|
|
|
|
| Ok data -> Lwt.return (Some data)
|
|
|
|
|
|
|
|
let read_exn net net_state hash =
|
|
|
|
read net net_state hash >>= function
|
|
|
|
| Error _ -> Lwt.fail Not_found
|
|
|
|
| Ok data -> Lwt.return data
|
|
|
|
|
|
|
|
let store
|
|
|
|
block_header_store
|
|
|
|
(net_state: net_state)
|
|
|
|
valid_block_watcher
|
2017-04-10 15:01:22 +04:00
|
|
|
hash { Updater.context ; message ; fitness } ttl =
|
2017-02-24 20:17:53 +04:00
|
|
|
(* Read the block header. *)
|
|
|
|
Raw_block_header.Locked.read
|
|
|
|
block_header_store hash >>=? fun block ->
|
|
|
|
Raw_block_header.Locked.read_discovery_time
|
|
|
|
block_header_store hash >>=? fun discovery_time ->
|
|
|
|
(* Check fitness coherency. *)
|
|
|
|
fail_unless
|
|
|
|
(Fitness.equal fitness block.Store.Block_header.shell.fitness)
|
|
|
|
(Invalid_fitness
|
2017-03-30 15:16:21 +04:00
|
|
|
{ block = hash ;
|
|
|
|
expected = block.Store.Block_header.shell.fitness ;
|
|
|
|
found = fitness ;
|
|
|
|
}) >>=? fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
begin (* Patch context about the associated test network. *)
|
|
|
|
Context.read_and_reset_fork_test_network
|
|
|
|
context >>= fun (fork, context) ->
|
|
|
|
if fork then
|
|
|
|
match ttl with
|
|
|
|
| None ->
|
|
|
|
(* Ignore fork on forked networks. *)
|
|
|
|
Context.del_test_network context >>= fun context ->
|
|
|
|
Context.del_test_network_expiration context
|
|
|
|
| Some ttl ->
|
|
|
|
let eol = Time.(add block.shell.timestamp ttl) in
|
|
|
|
Context.set_test_network
|
2017-03-31 15:04:05 +04:00
|
|
|
context (Net_id.of_block_hash hash) >>= fun context ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Context.set_test_network_expiration
|
|
|
|
context eol >>= fun context ->
|
|
|
|
Lwt.return context
|
|
|
|
else
|
|
|
|
Context.get_test_network_expiration context >>= function
|
|
|
|
| Some eol when Time.(eol <= now ()) ->
|
|
|
|
Context.del_test_network context >>= fun context ->
|
|
|
|
Context.del_test_network_expiration context
|
|
|
|
| None | Some _ ->
|
|
|
|
Lwt.return context
|
|
|
|
end >>= fun context ->
|
|
|
|
Raw_block_header.Locked.mark_valid
|
|
|
|
block_header_store hash >>= fun _marked ->
|
|
|
|
(* TODO fail if the block was previsouly stored ... ??? *)
|
2017-03-30 15:16:21 +04:00
|
|
|
Operation_list.Locked.read_all
|
|
|
|
block_header_store hash >>=? fun operations ->
|
2017-02-24 20:17:53 +04:00
|
|
|
(* Let's commit the context. *)
|
2017-04-10 14:14:11 +04:00
|
|
|
let message =
|
|
|
|
match message with
|
|
|
|
| Some message -> message
|
|
|
|
| None ->
|
2017-04-10 15:01:22 +04:00
|
|
|
Format.asprintf "%a(%ld): %a"
|
2017-04-10 14:14:11 +04:00
|
|
|
Block_hash.pp_short hash
|
2017-04-10 15:01:22 +04:00
|
|
|
block.shell.level
|
2017-04-10 14:14:11 +04:00
|
|
|
Fitness.pp fitness in
|
2017-04-10 15:01:22 +04:00
|
|
|
Context.commit hash block.shell.timestamp message context >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
(* Update the chain state. *)
|
|
|
|
let store = net_state.chain_store in
|
|
|
|
let predecessor = block.shell.predecessor in
|
|
|
|
Store.Chain.Known_heads.remove store predecessor >>= fun () ->
|
|
|
|
Store.Chain.Known_heads.store store hash >>= fun () ->
|
|
|
|
Store.Chain.Valid_successors.store
|
|
|
|
(store, predecessor) hash >>= fun () ->
|
|
|
|
(* Build the `valid_block` value. *)
|
|
|
|
raw_read_exn
|
2017-03-30 15:16:21 +04:00
|
|
|
block operations discovery_time
|
2017-02-24 20:17:53 +04:00
|
|
|
net_state.chain_store net_state.context_index hash >>= fun valid_block ->
|
|
|
|
Watcher.notify valid_block_watcher valid_block ;
|
|
|
|
Lwt.return (Ok valid_block)
|
2016-10-21 16:01:20 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
end
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let atomic1 f net = Shared.use net.state f
|
|
|
|
let atomic2 f net k = Shared.use net.state (fun s -> f s k)
|
|
|
|
let atomic3 f net k v = Shared.use net.state (fun s -> f s k v)
|
2016-09-22 17:34:38 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let known = atomic2 Locked.known
|
|
|
|
let read net hash =
|
|
|
|
Shared.use net.state begin fun state ->
|
|
|
|
Locked.read net state hash
|
|
|
|
end
|
|
|
|
let read_opt net hash =
|
|
|
|
read net hash >>= function
|
|
|
|
| Error _ -> Lwt.return_none
|
|
|
|
| Ok b -> Lwt.return (Some b)
|
|
|
|
let read_exn net hash =
|
|
|
|
read net hash >>= function
|
|
|
|
| Error _ -> Lwt.fail Not_found
|
|
|
|
| Ok b -> Lwt.return b
|
|
|
|
|
2017-04-10 15:01:22 +04:00
|
|
|
let store net hash vcontext =
|
2017-02-24 20:17:53 +04:00
|
|
|
Shared.use net.state begin fun net_state ->
|
|
|
|
Shared.use net.block_header_store begin fun block_header_store ->
|
2017-03-28 15:31:41 +04:00
|
|
|
Context.exists net_state.context_index hash >>= function
|
|
|
|
| true -> return None (* Previously stored context. *)
|
|
|
|
| false ->
|
|
|
|
Raw_block_header.Locked.invalid
|
|
|
|
block_header_store hash >>= function
|
|
|
|
| Some _ -> return None (* Previously invalidated block. *)
|
|
|
|
| None ->
|
|
|
|
Locked.store
|
|
|
|
block_header_store net_state net.valid_block_watcher
|
2017-04-10 15:01:22 +04:00
|
|
|
hash vcontext
|
|
|
|
net.forked_network_ttl >>=? fun valid_block ->
|
2017-03-28 15:31:41 +04:00
|
|
|
return (Some valid_block)
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let watcher net =
|
|
|
|
Watcher.create_stream net.valid_block_watcher
|
|
|
|
|
|
|
|
let fork_testnet state net block expiration =
|
2017-03-31 15:04:05 +04:00
|
|
|
assert (Net_id.equal block.net_id (Net_id.of_block_hash net.genesis.block)) ;
|
2017-02-24 20:17:53 +04:00
|
|
|
let hash = Block_hash.hash_bytes [Block_hash.to_bytes block.hash] in
|
|
|
|
let genesis : genesis = {
|
|
|
|
block = hash ;
|
|
|
|
time = Time.add block.timestamp 1L ;
|
|
|
|
protocol = block.test_protocol_hash ;
|
|
|
|
} in
|
|
|
|
Shared.use state.global_data begin fun data ->
|
2017-03-31 15:04:05 +04:00
|
|
|
if Net_id.Table.mem data.nets (Net_id.of_block_hash hash) then
|
2017-03-02 18:08:42 +04:00
|
|
|
assert false (* This would mean a block is validated twice... *)
|
2017-02-24 20:17:53 +04:00
|
|
|
else
|
|
|
|
Context.init_test_network block.context
|
|
|
|
~time:genesis.time
|
|
|
|
~genesis:genesis.block >>=? fun initial_context ->
|
|
|
|
Raw_net.locked_create data
|
|
|
|
~initial_context
|
|
|
|
~expiration
|
|
|
|
genesis >>= fun net ->
|
|
|
|
return net
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Helpers = struct
|
|
|
|
|
|
|
|
let path net b1 b2 =
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = Net_id.of_block_hash net.genesis.block in
|
|
|
|
if not ( Net_id.equal b1.net_id net_id
|
|
|
|
&& Net_id.equal b2.net_id net_id ) then
|
2017-02-24 20:17:53 +04:00
|
|
|
invalid_arg "State.path" ;
|
|
|
|
Raw_helpers.path net.block_header_store b1.hash b2.hash >>= function
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
| Some blocks ->
|
|
|
|
Lwt_list.map_p
|
|
|
|
(fun (hash, _header) -> read_exn net hash) blocks >>= fun path ->
|
|
|
|
Lwt.return (Some path)
|
|
|
|
|
|
|
|
let common_ancestor net b1 b2 =
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = Net_id.of_block_hash net.genesis.block in
|
|
|
|
if not ( Net_id.equal b1.net_id net_id
|
|
|
|
&& Net_id.equal b2.net_id net_id ) then
|
2017-02-24 20:17:53 +04:00
|
|
|
invalid_arg "State.path" ;
|
2017-03-22 20:21:52 +04:00
|
|
|
Raw_block_header.read_exn (* The blocks are known valid. *)
|
|
|
|
net.block_header_store b1.hash >>= fun { shell = header1 } ->
|
|
|
|
Raw_block_header.read_exn (* The blocks are known valid. *)
|
|
|
|
net.block_header_store b2.hash >>= fun { shell = header2 } ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Raw_helpers.common_ancestor net.block_header_store
|
2017-03-22 20:21:52 +04:00
|
|
|
b1.hash header1 b2.hash header2 >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None -> assert false (* The blocks are known valid. *)
|
|
|
|
| Some (hash, _header) -> read_exn net hash
|
|
|
|
|
|
|
|
let block_locator state sz b =
|
|
|
|
Raw_helpers.block_locator state.block_header_store sz b.hash
|
|
|
|
|
|
|
|
let iter_predecessors =
|
|
|
|
let compare b1 b2 =
|
|
|
|
match Fitness.compare b1.fitness b2.fitness with
|
|
|
|
| 0 -> begin
|
|
|
|
match Time.compare b1.timestamp b2.timestamp with
|
|
|
|
| 0 -> Block_hash.compare b1.hash b2.hash
|
|
|
|
| res -> res
|
|
|
|
end
|
|
|
|
| res -> res in
|
|
|
|
let predecessor state b =
|
2017-04-10 14:58:31 +04:00
|
|
|
if Block_hash.equal b.hash b.predecessor then
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.return None
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2017-04-10 14:58:31 +04:00
|
|
|
read_opt state b.predecessor in
|
2017-02-24 20:17:53 +04:00
|
|
|
Raw_helpers.iter_predecessors compare predecessor
|
|
|
|
(fun b -> b.timestamp) (fun b -> b.fitness)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let known_heads net =
|
|
|
|
Shared.use net.state begin fun net_state ->
|
|
|
|
Store.Chain.Known_heads.elements net_state.chain_store >>= fun hashes ->
|
|
|
|
Lwt_list.map_p (Locked.read_exn net net_state) hashes
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Current = struct
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let genesis net = read_exn net net.genesis.block
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let head net =
|
|
|
|
Shared.use net.state begin fun { current_head } ->
|
|
|
|
Lwt.return current_head
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let protocol net =
|
|
|
|
Shared.use net.state begin fun { current_head } ->
|
|
|
|
match current_head.protocol with
|
|
|
|
| None -> assert false (* TODO PROPER ERROR *)
|
|
|
|
| Some proto -> Lwt.return proto
|
|
|
|
end
|
|
|
|
|
|
|
|
let mem net hash =
|
|
|
|
Shared.use net.state begin fun { chain_store } ->
|
|
|
|
Store.Chain.In_chain_insertion_time.known (chain_store, hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
let find_new net hist sz =
|
|
|
|
let rec common_ancestor hist =
|
|
|
|
match hist with
|
|
|
|
| [] -> Lwt.return net.genesis.block
|
|
|
|
| h :: hist ->
|
|
|
|
mem net h >>= function
|
|
|
|
| false -> common_ancestor hist
|
|
|
|
| true -> Lwt.return h in
|
|
|
|
let rec path sz acc h =
|
|
|
|
if sz <= 0 then return (List.rev acc)
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2017-02-24 20:17:53 +04:00
|
|
|
Shared.use net.state begin fun { chain_store } ->
|
|
|
|
Store.Chain.Successor_in_chain.read_opt (chain_store, h)
|
|
|
|
end >>= function
|
|
|
|
| None -> return (List.rev acc)
|
|
|
|
| Some s -> path (sz-1) (s :: acc) s in
|
|
|
|
common_ancestor hist >>= fun ancestor ->
|
|
|
|
path sz [] ancestor
|
|
|
|
|
|
|
|
let new_blocks store old_block new_block =
|
2017-03-22 20:21:52 +04:00
|
|
|
Raw_block_header.read_exn (* valid block *)
|
|
|
|
store old_block.hash >>= fun { shell = old_header } ->
|
|
|
|
Raw_block_header.read_exn (* valid block *)
|
|
|
|
store new_block.hash >>= fun { shell = new_header } ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Raw_helpers.common_ancestor store
|
2017-03-22 20:21:52 +04:00
|
|
|
old_block.hash old_header
|
|
|
|
new_block.hash new_header >>= function
|
2017-02-24 20:17:53 +04:00
|
|
|
| None -> assert false (* valid block *)
|
|
|
|
| Some (ancestor, _header) ->
|
|
|
|
Raw_helpers.path store ancestor new_block.hash >>= function
|
|
|
|
| None -> assert false (* valid block *)
|
|
|
|
| Some path -> Lwt.return (ancestor, path)
|
|
|
|
|
|
|
|
let locked_set_head block_header_store operation_store state block =
|
|
|
|
let rec pop_blocks ancestor hash =
|
|
|
|
if Block_hash.equal hash ancestor then
|
|
|
|
Lwt.return_unit
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2017-02-24 20:17:53 +04:00
|
|
|
lwt_debug "pop_block %a" Block_hash.pp_short hash >>= fun () ->
|
|
|
|
Raw_block_header.read_exn
|
|
|
|
block_header_store hash >>= fun { shell } ->
|
2017-03-30 15:16:21 +04:00
|
|
|
Raw_operation_list.read_all_exn
|
|
|
|
block_header_store hash >>= fun operations ->
|
|
|
|
let operations = List.concat operations in
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt_list.iter_p
|
|
|
|
(fun h ->
|
|
|
|
Raw_operation.Locked.unmark operation_store h >>= fun _ ->
|
|
|
|
Lwt.return_unit)
|
2017-03-30 15:16:21 +04:00
|
|
|
operations >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Store.Chain.In_chain_insertion_time.remove
|
|
|
|
(state.chain_store, hash) >>= fun () ->
|
|
|
|
Store.Chain.Successor_in_chain.remove
|
|
|
|
(state.chain_store, shell.predecessor) >>= fun () ->
|
|
|
|
pop_blocks ancestor shell.predecessor
|
|
|
|
in
|
|
|
|
let push_block time (hash, shell) =
|
|
|
|
lwt_debug "push_block %a" Block_hash.pp_short hash >>= fun () ->
|
|
|
|
Store.Chain.In_chain_insertion_time.store
|
|
|
|
(state.chain_store, hash) time >>= fun () ->
|
|
|
|
Store.Chain.Successor_in_chain.store
|
|
|
|
(state.chain_store,
|
|
|
|
shell.Store.Block_header.predecessor) hash >>= fun () ->
|
2017-03-30 15:16:21 +04:00
|
|
|
Raw_operation_list.read_all_exn
|
|
|
|
block_header_store hash >>= fun operations ->
|
|
|
|
let operations = List.concat operations in
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt_list.iter_p
|
|
|
|
(fun h ->
|
|
|
|
Raw_operation.Locked.mark_valid operation_store h >>= fun _ ->
|
|
|
|
Lwt.return_unit)
|
2017-03-30 15:16:21 +04:00
|
|
|
operations
|
2017-02-24 20:17:53 +04:00
|
|
|
in
|
|
|
|
let time = Time.now () in
|
|
|
|
new_blocks
|
|
|
|
block_header_store state.current_head block >>= fun (ancestor, path) ->
|
|
|
|
pop_blocks ancestor state.current_head.hash >>= fun () ->
|
|
|
|
Lwt_list.iter_p (push_block time) path >>= fun () ->
|
|
|
|
state.current_head <- block ;
|
|
|
|
Store.Chain.Current_head.store state.chain_store block.hash
|
|
|
|
|
|
|
|
let set_head net block =
|
|
|
|
Shared.use net.state begin fun state ->
|
|
|
|
Shared.use net.operation_store begin fun operation_store ->
|
|
|
|
locked_set_head net.block_header_store operation_store state block
|
|
|
|
end
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let test_and_set_head net ~old block =
|
|
|
|
Shared.use net.state begin fun state ->
|
|
|
|
if not (Block_hash.equal state.current_head.hash old.hash) then
|
|
|
|
Lwt.return_false
|
|
|
|
else
|
|
|
|
Shared.use net.operation_store begin fun operation_store ->
|
|
|
|
locked_set_head
|
|
|
|
net.block_header_store operation_store state block >>= fun () ->
|
|
|
|
Lwt.return_true
|
|
|
|
end
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
let new_blocks net ~from_block ~to_block =
|
|
|
|
new_blocks net.block_header_store from_block to_block
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Net = struct
|
|
|
|
|
|
|
|
type t = net
|
|
|
|
type net = t
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
type nonrec genesis = genesis = {
|
2017-02-24 20:17:53 +04:00
|
|
|
time: Time.t ;
|
|
|
|
block: Block_hash.t ;
|
|
|
|
protocol: Protocol_hash.t ;
|
|
|
|
}
|
|
|
|
let genesis_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
(fun { time ; block ; protocol } -> (time, block, protocol))
|
|
|
|
(fun (time, block, protocol) -> { time ; block ; protocol })
|
|
|
|
(obj3
|
|
|
|
(req "timestamp" Time.encoding)
|
|
|
|
(req "block" Block_hash.encoding)
|
|
|
|
(req "protocol" Protocol_hash.encoding))
|
|
|
|
|
|
|
|
let create state ?test_protocol ?forked_network_ttl genesis =
|
2017-03-31 15:04:05 +04:00
|
|
|
let net_id = Net_id.of_block_hash genesis.block in
|
2017-02-24 20:17:53 +04:00
|
|
|
let forked_network_ttl = map_option Int64.of_int forked_network_ttl in
|
|
|
|
Shared.use state.global_data begin fun data ->
|
2017-03-31 15:04:05 +04:00
|
|
|
if Net_id.Table.mem data.nets net_id then
|
2017-02-24 20:17:53 +04:00
|
|
|
Pervasives.failwith "State.Net.create"
|
|
|
|
else
|
|
|
|
Raw_net.locked_create data
|
|
|
|
?test_protocol ?forked_network_ttl genesis >>= fun net ->
|
2017-03-31 15:04:05 +04:00
|
|
|
Net_id.Table.add data.nets net_id net ;
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.return net
|
|
|
|
end
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
let locked_read data id =
|
2017-02-24 20:17:53 +04:00
|
|
|
let net_store = Store.Net.get data.global_store id in
|
|
|
|
let operation_store = Store.Operation.get net_store
|
|
|
|
and block_header_store = Store.Block_header.get net_store
|
|
|
|
and chain_store = Store.Chain.get net_store in
|
2017-03-31 15:04:05 +04:00
|
|
|
Store.Net.Genesis_hash.read net_store >>=? fun genesis_hash ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Store.Net.Genesis_time.read net_store >>=? fun time ->
|
|
|
|
Store.Net.Genesis_protocol.read net_store >>=? fun protocol ->
|
|
|
|
Store.Net.Expiration.read_opt net_store >>= fun expiration ->
|
|
|
|
Store.Net.Forked_network_ttl.read_opt net_store >>= fun forked_network_ttl ->
|
|
|
|
let genesis = { time ; protocol ; block = genesis_hash } in
|
|
|
|
Store.Chain.Current_head.read chain_store >>=? fun genesis_hash ->
|
|
|
|
data.init_index id >>= fun context_index ->
|
|
|
|
Block_header.Locked.read block_header_store
|
|
|
|
genesis_hash >>=? fun genesis_shell_header ->
|
|
|
|
Block_header.Locked.read_discovery_time block_header_store
|
|
|
|
genesis_hash >>=? fun genesis_discovery_time ->
|
|
|
|
Valid_block.Locked.raw_read
|
2017-03-30 15:16:21 +04:00
|
|
|
genesis_shell_header [] genesis_discovery_time
|
2017-02-24 20:17:53 +04:00
|
|
|
chain_store context_index genesis_hash >>=? fun genesis_block ->
|
|
|
|
return @@
|
|
|
|
Raw_net.build
|
|
|
|
~genesis
|
|
|
|
~genesis_block
|
|
|
|
~expiration
|
|
|
|
~forked_network_ttl
|
|
|
|
context_index
|
|
|
|
chain_store
|
|
|
|
block_header_store
|
|
|
|
operation_store
|
|
|
|
|
|
|
|
let locked_read_all data =
|
|
|
|
Store.Net.list data.global_store >>= fun ids ->
|
|
|
|
iter_p
|
|
|
|
(fun id ->
|
|
|
|
locked_read data id >>=? fun net ->
|
|
|
|
Net_id.Table.add data.nets id net ;
|
|
|
|
return ())
|
|
|
|
ids
|
|
|
|
|
|
|
|
let read_all state =
|
|
|
|
Shared.use state.global_data begin fun data ->
|
|
|
|
locked_read_all data
|
|
|
|
end
|
|
|
|
|
|
|
|
let get state id =
|
|
|
|
Shared.use state.global_data begin fun data ->
|
|
|
|
try return (Net_id.Table.find data.nets id)
|
|
|
|
with Not_found -> fail (Unknown_network id)
|
|
|
|
end
|
|
|
|
|
|
|
|
let all state =
|
|
|
|
Shared.use state.global_data begin fun { nets } ->
|
|
|
|
Lwt.return @@
|
|
|
|
Net_id.Table.fold (fun _ net acc -> net :: acc) nets []
|
|
|
|
end
|
|
|
|
|
2017-03-31 15:04:05 +04:00
|
|
|
let id { id } = id
|
2017-02-24 20:17:53 +04:00
|
|
|
let genesis { genesis } = genesis
|
|
|
|
let expiration { expiration } = expiration
|
|
|
|
let forked_network_ttl { forked_network_ttl } = forked_network_ttl
|
|
|
|
|
|
|
|
let destroy state net =
|
|
|
|
lwt_debug "destroy %a" Net_id.pp (id net) >>= fun () ->
|
|
|
|
Shared.use state.global_data begin fun { global_store ; nets } ->
|
|
|
|
Net_id.Table.remove nets (id net) ;
|
|
|
|
Store.Net.destroy global_store (id net) >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
end
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
(*
|
2016-09-08 21:13:10 +04:00
|
|
|
let () =
|
|
|
|
let open Data_encoding in
|
|
|
|
register_error_kind `Permanent
|
|
|
|
~id:"refusedOperation"
|
|
|
|
~title: "Refused operation"
|
|
|
|
~description:
|
|
|
|
"An operation that will never be accepted (by any protocol version)."
|
|
|
|
~pp:(fun ppf hash ->
|
|
|
|
Format.fprintf ppf "Refused operation %a"
|
|
|
|
Operation_hash.pp_short hash)
|
|
|
|
(obj1 (req "operation_hash" Operation_hash.encoding))
|
|
|
|
(function Exn (Operation.Invalid (hash, _)) -> Some hash | _ -> None)
|
|
|
|
(fun hash -> Exn (Operation.Invalid (hash, [(* TODO *)])))
|
|
|
|
|
|
|
|
let () =
|
|
|
|
let open Data_encoding in
|
|
|
|
register_error_kind `Permanent
|
|
|
|
~id: "invalidBlock"
|
|
|
|
~title: "Invalid block"
|
|
|
|
~description:
|
2017-03-15 19:00:53 +04:00
|
|
|
"The economic protocol refused to validate the block."
|
2016-09-08 21:13:10 +04:00
|
|
|
~pp:(fun ppf block_hash ->
|
|
|
|
Format.fprintf ppf "Cannot validate the block %a"
|
|
|
|
Block_hash.pp_short block_hash)
|
|
|
|
(obj1 (req "block_hash" Block_hash.encoding))
|
|
|
|
(function Exn (Valid_block.Invalid (block_hash, _)) -> Some block_hash
|
|
|
|
| _ -> None)
|
|
|
|
(fun block_hash -> Exn (Valid_block.Invalid (block_hash, [(* TODO *)])))
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
*)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
module Operation = struct
|
|
|
|
|
|
|
|
type shell_header = Store.Operation.shell_header = {
|
|
|
|
net_id: Net_id.t ;
|
2016-09-08 21:13:10 +04:00
|
|
|
}
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
type t = Store.Operation.t = {
|
|
|
|
shell: shell_header ;
|
|
|
|
proto: MBytes.t ;
|
|
|
|
}
|
|
|
|
|
|
|
|
include Make_data_store
|
|
|
|
(Store.Operation)
|
|
|
|
(struct
|
|
|
|
type store = net
|
|
|
|
let use s = Shared.use s.operation_store
|
|
|
|
let unknown k = fail (Unknown_operation k)
|
|
|
|
end)
|
|
|
|
(Operation_hash.Set)
|
|
|
|
|
|
|
|
let in_chain = valid
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Protocol = struct
|
|
|
|
|
|
|
|
type t = Store.Protocol.t
|
|
|
|
|
|
|
|
include Make_data_store
|
|
|
|
(Store.Protocol)
|
|
|
|
(struct
|
|
|
|
type store = global_state
|
|
|
|
let use s = Shared.use s.protocol_store
|
|
|
|
let unknown k = fail (Unknown_protocol k)
|
|
|
|
end)
|
|
|
|
(Protocol_hash.Set)
|
|
|
|
|
|
|
|
(* TODO somehow export `mark_invalid`. *)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
let read
|
|
|
|
?patch_context
|
|
|
|
~store_root
|
|
|
|
~context_root
|
|
|
|
() =
|
|
|
|
Store.init store_root >>=? fun store ->
|
|
|
|
Context.init ?patch_context ~root:context_root >>= fun context_index ->
|
|
|
|
let global_data = {
|
|
|
|
nets = Net_id.Table.create 17 ;
|
|
|
|
global_store = store ;
|
|
|
|
init_index = (fun _ -> Lwt.return context_index) ;
|
|
|
|
} in
|
|
|
|
let state = {
|
|
|
|
global_data = Shared.create global_data ;
|
|
|
|
protocol_store = Shared.create @@ Store.Protocol.get store ;
|
|
|
|
} in
|
|
|
|
Net.read_all state >>=? fun () ->
|
|
|
|
return state
|