ligo/src/node/shell/node.ml

591 lines
20 KiB
OCaml
Raw Normal View History

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 Lwt.Infix
2016-09-08 21:13:10 +04:00
open Logging.Node.Worker
let inject_operation validator ?force bytes =
let t =
match Data_encoding.Binary.of_bytes Store.Operation.encoding bytes with
2016-09-08 21:13:10 +04:00
| None -> failwith "Can't parse the operation"
| Some operation ->
Validator.get
validator operation.shell.net_id >>=? fun net_validator ->
2016-09-08 21:13:10 +04:00
let pv = Validator.prevalidator net_validator in
Prevalidator.inject_operation pv ?force operation in
let hash = Operation_hash.hash_bytes [bytes] in
Lwt.return (hash, t)
2016-10-21 16:01:20 +04:00
let inject_protocol state ?force:_ proto =
let proto_bytes =
Data_encoding.Binary.to_bytes Store.Protocol.encoding proto in
2016-10-21 16:01:20 +04:00
let hash = Protocol_hash.hash_bytes [proto_bytes] in
let validation =
Updater.compile hash proto >>= function
| false ->
failwith
"Compilation failed (%a)"
Protocol_hash.pp_short hash
2016-10-24 16:10:17 +04:00
| true ->
State.Protocol.store state proto >>= function
| false ->
failwith
"Previously registred protocol (%a)"
Protocol_hash.pp_short hash
| true -> return ()
2016-10-24 16:10:17 +04:00
in
Lwt.return (hash, validation)
2016-10-21 16:01:20 +04:00
let inject_block validator ?force bytes =
Validator.inject_block validator ?force bytes >>=? fun (hash, block) ->
return (hash, (block >>=? fun _ -> return ()))
2016-10-21 16:01:20 +04:00
2016-09-08 21:13:10 +04:00
type t = {
state: State.t ;
distributed_db: Distributed_db.t ;
2016-09-08 21:13:10 +04:00
validator: Validator.worker ;
mainnet_db: Distributed_db.net ;
mainnet_net: State.Net.t ;
mainnet_validator: Validator.t ;
2016-09-08 21:13:10 +04:00
inject_block:
?force:bool -> MBytes.t ->
(Block_hash.t * unit tzresult Lwt.t) tzresult Lwt.t ;
2016-09-08 21:13:10 +04:00
inject_operation:
?force:bool -> MBytes.t ->
(Operation_hash.t * unit tzresult Lwt.t) Lwt.t ;
2016-10-21 16:01:20 +04:00
inject_protocol:
?force:bool -> Store.Protocol.t ->
(Protocol_hash.t * unit tzresult Lwt.t) Lwt.t ;
p2p: Distributed_db.p2p ; (* For P2P RPCs *)
2016-09-08 21:13:10 +04:00
shutdown: unit -> unit Lwt.t ;
}
let init_p2p net_params =
match net_params with
| None ->
lwt_log_notice "P2P layer is disabled" >>= fun () ->
Lwt.return P2p.faked_network
2016-09-08 21:13:10 +04:00
| Some (config, limits) ->
lwt_log_notice "bootstraping network..." >>= fun () ->
P2p.create
~config ~limits
Distributed_db_metadata.cfg
Distributed_db_message.cfg >>= fun p2p ->
Lwt.async (fun () -> P2p.maintain p2p) ;
Lwt.return p2p
2017-01-23 14:10:07 +04:00
type config = {
genesis: State.Net.genesis ;
2017-01-23 14:10:07 +04:00
store_root: string ;
context_root: string ;
test_protocol: Protocol_hash.t option ;
patch_context: (Context.t -> Context.t Lwt.t) option ;
p2p: (P2p.config * P2p.limits) option ;
}
2016-09-08 21:13:10 +04:00
2017-01-23 14:10:07 +04:00
let create { genesis ; store_root ; context_root ;
test_protocol ; patch_context ; p2p = net_params } =
2016-09-08 21:13:10 +04:00
init_p2p net_params >>= fun p2p ->
State.read
~store_root ~context_root ?patch_context () >>=? fun state ->
let distributed_db = Distributed_db.create state p2p in
let validator = Validator.create_worker state distributed_db in
State.Net.create state
?test_protocol
~forked_network_ttl:(48 * 3600) (* 2 days *)
genesis >>= fun mainnet_net ->
Validator.activate validator mainnet_net >>= fun mainnet_validator ->
let mainnet_db = Validator.net_db mainnet_validator in
2016-09-08 21:13:10 +04:00
let shutdown () =
P2p.shutdown p2p >>= fun () ->
Validator.shutdown validator >>= fun () ->
Lwt.return_unit
2016-09-08 21:13:10 +04:00
in
return {
state ;
distributed_db ;
2016-09-08 21:13:10 +04:00
validator ;
mainnet_db ;
mainnet_net ;
mainnet_validator ;
inject_block = inject_block validator ;
2016-09-08 21:13:10 +04:00
inject_operation = inject_operation validator ;
2016-10-21 16:01:20 +04:00
inject_protocol = inject_protocol state ;
p2p ;
2016-09-08 21:13:10 +04:00
shutdown ;
}
let shutdown node = node.shutdown ()
module RPC = struct
type block = Node_rpc_services.Blocks.block
type block_info = Node_rpc_services.Blocks.block_info = {
hash: Block_hash.t ;
predecessor: Block_hash.t ;
fitness: MBytes.t list ;
timestamp: Time.t ;
protocol: Protocol_hash.t option ;
operations: Operation_hash.t list option ;
net: Node_rpc_services.Blocks.net ;
test_protocol: Protocol_hash.t option ;
test_network: (Node_rpc_services.Blocks.net * Time.t) option ;
}
let convert (block: State.Valid_block.t) = {
hash = block.hash ;
predecessor = block.pred ;
fitness = block.fitness ;
timestamp = block.timestamp ;
protocol = Some block.protocol_hash ;
operations = Some block.operations ;
net = block.net_id ;
test_protocol = Some block.test_protocol_hash ;
test_network = block.test_network ;
}
let convert_block hash (block: State.Block_header.shell_header) = {
2016-09-08 21:13:10 +04:00
net = block.net_id ;
hash = hash ;
predecessor = block.predecessor ;
fitness = block.fitness ;
timestamp = block.timestamp ;
protocol = None ;
operations = Some block.operations ;
test_protocol = None ;
test_network = None ;
}
let inject_block node = node.inject_block
let inject_operation node = node.inject_operation
2016-10-21 16:01:20 +04:00
let inject_protocol node = node.inject_protocol
2016-09-08 21:13:10 +04:00
let raw_block_info node hash =
Distributed_db.read_block node.distributed_db hash >>= function
| Some (net_db, _block) ->
let net = Distributed_db.state net_db in
State.Valid_block.read_exn net hash >>= fun block ->
Lwt.return (convert block)
| None ->
Lwt.fail Not_found
2016-09-08 21:13:10 +04:00
let prevalidation_hash =
Block_hash.of_b58check
"BLockPrevaLidationPrevaLidationPrevaLidationPrZ4mr6"
2016-09-08 21:13:10 +04:00
let get_net node = function
| `Genesis | `Head _ | `Prevalidation ->
node.mainnet_validator, node.mainnet_db
2016-09-08 21:13:10 +04:00
| `Test_head _ | `Test_prevalidation ->
match Validator.test_validator node.mainnet_validator with
2016-09-08 21:13:10 +04:00
| None -> raise Not_found
| Some v -> v
let get_validator node = function
| `Genesis | `Head _ | `Prevalidation -> node.mainnet_validator
| `Test_head _ | `Test_prevalidation ->
match Validator.test_validator node.mainnet_validator with
| None -> raise Not_found
| Some (v, _) -> v
let get_validator_per_hash node hash =
Distributed_db.read_block_exn
node.distributed_db hash >>= fun (_net_db, block) ->
if State.Net_id.equal
(State.Net.id node.mainnet_net)
block.shell.net_id then
Lwt.return (Some (node.mainnet_validator, node.mainnet_db))
else
match Validator.test_validator node.mainnet_validator with
| Some (test_validator, net_db)
when State.Net_id.equal
(State.Net.id (Validator.net_state test_validator))
block.shell.net_id ->
Lwt.return (Some (node.mainnet_validator, net_db))
| _ -> Lwt.return_none
let read_valid_block node h =
Distributed_db.read_block node.distributed_db h >>= function
| None -> Lwt.return_none
| Some (_net_db, block) ->
State.Net.get node.state block.shell.net_id >>= function
| Error _ -> Lwt.return_none
| Ok net ->
State.Valid_block.read_exn net h >>= fun block ->
Lwt.return (Some block)
let read_valid_block_exn node h =
Distributed_db.read_block_exn
node.distributed_db h >>= fun (net_db, _block) ->
let net = Distributed_db.state net_db in
State.Valid_block.read_exn net h >>= fun block ->
Lwt.return block
let get_pred net_db n (v: State.Valid_block.t) =
let rec loop net_db n h =
if n <= 0 then
Lwt.return h
else
Distributed_db.Block_header.read net_db h >>= function
| None -> Lwt.fail Not_found
| Some { shell = { predecessor } } ->
loop net_db (n-1) predecessor in
if n <= 0 then
Lwt.return v
else
loop net_db n v.hash >>= fun hash ->
let net_state = Distributed_db.state net_db in
State.Valid_block.read_exn net_state hash
2016-09-08 21:13:10 +04:00
let block_info node (block: block) =
match block with
| `Genesis ->
State.Valid_block.Current.genesis node.mainnet_net >|= convert
2016-09-08 21:13:10 +04:00
| ( `Head n | `Test_head n ) as block ->
let validator = get_validator node block in
let net_db = Validator.net_db validator in
let net_state = Validator.net_state validator in
State.Valid_block.Current.head net_state >>= fun head ->
get_pred net_db n head >|= convert
| `Hash h ->
read_valid_block_exn node h >|= convert
2016-09-08 21:13:10 +04:00
| ( `Prevalidation | `Test_prevalidation ) as block ->
let validator = get_validator node block in
2016-09-08 21:13:10 +04:00
let pv = Validator.prevalidator validator in
let net_state = Validator.net_state validator in
State.Valid_block.Current.head net_state >>= fun head ->
2016-09-08 21:13:10 +04:00
let ctxt = Prevalidator.context pv in
Context.get_fitness ctxt >|= fun fitness ->
2016-09-08 21:13:10 +04:00
{ (convert head) with
hash = prevalidation_hash ;
fitness ;
timestamp = Prevalidator.timestamp pv
}
let get_context node block =
match block with
| `Genesis ->
State.Valid_block.Current.genesis node.mainnet_net >>= fun block ->
Lwt.return (Some block.context)
| ( `Head n | `Test_head n ) as block ->
let validator = get_validator node block in
let net_state = Validator.net_state validator in
let net_db = Validator.net_db validator in
State.Valid_block.Current.head net_state >>= fun head ->
get_pred net_db n head >>= fun { context } ->
2016-09-08 21:13:10 +04:00
Lwt.return (Some context)
| `Hash hash-> begin
read_valid_block node hash >|= function
| None -> None
| Some { context } -> Some context
2016-09-08 21:13:10 +04:00
end
| ( `Prevalidation | `Test_prevalidation ) as block ->
let validator, _net = get_net node block in
let pv = Validator.prevalidator validator in
Lwt.return (Some (Prevalidator.context pv))
let operations node block =
match block with
| `Genesis ->
State.Valid_block.Current.genesis node.mainnet_net >>= fun { operations } ->
2016-09-08 21:13:10 +04:00
Lwt.return operations
| ( `Head n | `Test_head n ) as block ->
let validator = get_validator node block in
let net_state = Validator.net_state validator in
let net_db = Validator.net_db validator in
State.Valid_block.Current.head net_state >>= fun head ->
get_pred net_db n head >>= fun { operations } ->
2016-09-08 21:13:10 +04:00
Lwt.return operations
| (`Prevalidation | `Test_prevalidation) as block ->
let validator, _net = get_net node block in
let pv = Validator.prevalidator validator in
let { Updater.applied }, _ = Prevalidator.operations pv in
Lwt.return applied
| `Hash hash->
read_valid_block node hash >|= function
2016-09-08 21:13:10 +04:00
| None -> []
| Some { operations } -> operations
2016-09-08 21:13:10 +04:00
let operation_content node hash =
Distributed_db.read_operation node.distributed_db hash >>= function
| None -> Lwt.return_none
| Some (_, op) -> Lwt.return (Some op)
2016-09-08 21:13:10 +04:00
let pending_operations node (block: block) =
2016-09-08 21:13:10 +04:00
match block with
| ( `Head 0 | `Prevalidation
| `Test_head 0 | `Test_prevalidation ) as block ->
let validator, _net = get_net node block in
let pv = Validator.prevalidator validator in
Lwt.return (Prevalidator.operations pv)
| ( `Head n | `Test_head n ) as block ->
let validator = get_validator node block in
let prevalidator = Validator.prevalidator validator in
let net_state = Validator.net_state validator in
let net_db = Validator.net_db validator in
State.Valid_block.Current.head net_state >>= fun head ->
get_pred net_db n head >>= fun b ->
Prevalidator.pending ~block:b prevalidator >|= fun ops ->
2016-09-08 21:13:10 +04:00
Updater.empty_result, ops
| `Genesis ->
let net = node.mainnet_net in
State.Valid_block.Current.genesis net >>= fun b ->
let validator = get_validator node `Genesis in
let prevalidator = Validator.prevalidator validator in
Prevalidator.pending ~block:b prevalidator >|= fun ops ->
2016-09-08 21:13:10 +04:00
Updater.empty_result, ops
| `Hash h -> begin
get_validator_per_hash node h >>= function
2016-09-08 21:13:10 +04:00
| None ->
Lwt.return (Updater.empty_result, Operation_hash.Set.empty)
| Some (validator, net_db) ->
let net_state = Distributed_db.state net_db in
let prevalidator = Validator.prevalidator validator in
State.Valid_block.read_exn net_state h >>= fun block ->
Prevalidator.pending ~block prevalidator >|= fun ops ->
Updater.empty_result, ops
end
2016-09-08 21:13:10 +04:00
let protocols { state } =
State.Protocol.list state >>= fun set ->
Lwt.return (Protocol_hash.Set.elements set)
2016-10-21 16:01:20 +04:00
let protocol_content node hash =
State.Protocol.read node.state hash
2016-09-08 21:13:10 +04:00
let preapply node block ~timestamp ~sort ops =
begin
match block with
| `Genesis ->
let net = node.mainnet_net in
State.Valid_block.Current.genesis net >>= return
2016-09-08 21:13:10 +04:00
| ( `Head 0 | `Prevalidation
| `Test_head 0 | `Test_prevalidation ) as block ->
let validator = get_validator node block in
let net_state = Validator.net_state validator in
State.Valid_block.Current.head net_state >>= return
2016-09-08 21:13:10 +04:00
| `Head n | `Test_head n as block -> begin
let validator = get_validator node block in
let net_state = Validator.net_state validator in
let net_db = Validator.net_db validator in
State.Valid_block.Current.head net_state >>= fun head ->
get_pred net_db n head >>= return
2016-09-08 21:13:10 +04:00
end
| `Hash hash ->
read_valid_block node hash >>= function
2016-09-08 21:13:10 +04:00
| None -> Lwt.return (error_exn Not_found)
| Some data -> return data
2016-09-08 21:13:10 +04:00
end >>=? fun { hash ; context ; protocol } ->
begin
match protocol with
| None -> failwith "Unknown protocol version"
| Some protocol -> return protocol
end >>=? fun ((module Proto) as protocol) ->
let net_db = Validator.net_db node.mainnet_validator in
2016-09-08 21:13:10 +04:00
Prevalidator.preapply
net_db context protocol hash timestamp sort ops >>=? fun (ctxt, r) ->
Context.get_fitness ctxt >>= fun fitness ->
2016-09-08 21:13:10 +04:00
return (fitness, r)
let complete node ?block str =
match block with
| None ->
Base58.complete str
| Some block ->
get_context node block >>= function
| None -> Lwt.fail Not_found
| Some ctxt ->
Context.get_protocol ctxt >>= fun protocol_hash ->
let (module Proto) = Updater.get_exn protocol_hash in
Base58.complete str >>= fun l1 ->
Proto.complete_b58prefix ctxt str >>= fun l2 ->
Lwt.return (l1 @ l2)
2016-09-08 21:13:10 +04:00
let context_dir node block =
get_context node block >>= function
| None -> Lwt.return None
| Some ctxt ->
Context.get_protocol ctxt >>= fun protocol_hash ->
let (module Proto) = Updater.get_exn protocol_hash in
let dir = RPC.map (fun () -> ctxt) Proto.rpc_services in
Lwt.return (Some (RPC.map (fun _ -> ()) dir))
let heads node =
State.Valid_block.known_heads node.mainnet_net >>= fun heads ->
begin
match Validator.test_validator node.mainnet_validator with
| None -> Lwt.return_nil
| Some (_, net_db) ->
State.Valid_block.known_heads (Distributed_db.state net_db)
end >>= fun test_heads ->
let map =
List.fold_left
(fun map block ->
Block_hash.Map.add
block.State.Valid_block.hash (convert block) map)
Block_hash.Map.empty (test_heads @ heads) in
Lwt.return map
let predecessors node len head =
let rec loop net_db acc len hash (block: State.Block_header.t) =
if Block_hash.equal block.shell.predecessor hash then
Lwt.return (List.rev acc)
else begin
if len = 0 then
Lwt.return (List.rev acc)
else
let hash = block.shell.predecessor in
Distributed_db.Block_header.read_exn net_db hash >>= fun block ->
loop net_db (hash :: acc) (len-1) hash block
end in
try
Distributed_db.read_block_exn
node.distributed_db head >>= fun (net_db, block) ->
loop net_db [] len head block
with Not_found -> Lwt.return_nil
let predecessors_bi state ignored len head =
2016-09-08 21:13:10 +04:00
try
let rec loop acc len hash =
State.Valid_block.read_exn state hash >>= fun block ->
2016-09-08 21:13:10 +04:00
let bi = convert block in
if Block_hash.equal bi.predecessor hash then
Lwt.return (List.rev (bi :: acc))
else begin
if len = 0
|| Block_hash.Set.mem hash ignored then
2016-09-08 21:13:10 +04:00
Lwt.return (List.rev acc)
else
loop (bi :: acc) (len-1) bi.predecessor
end in
loop [] len head
with Not_found -> Lwt.return_nil
let list node len heads =
Lwt_list.fold_left_s
(fun (ignored, acc) head ->
Distributed_db.read_block_exn
node.distributed_db head >>= fun (net_db, _block) ->
let net_state = Distributed_db.state net_db in
predecessors_bi net_state ignored len head >|= fun predecessors ->
2016-09-08 21:13:10 +04:00
let ignored =
List.fold_right
(fun x s -> Block_hash.Set.add x.hash s)
2016-09-08 21:13:10 +04:00
predecessors ignored in
ignored, predecessors :: acc
)
(Block_hash.Set.empty, [])
heads >>= fun (_, blocks) ->
Lwt.return (List.rev blocks)
2016-09-08 21:13:10 +04:00
let block_watcher node =
let stream, shutdown = Distributed_db.watch_block node.distributed_db in
2016-09-08 21:13:10 +04:00
Lwt_stream.map
(fun (hash, block) -> convert_block hash block.Store.Block_header.shell)
2016-09-08 21:13:10 +04:00
stream,
shutdown
let valid_block_watcher node =
let stream, shutdown = Validator.global_watcher node.validator in
Lwt_stream.map (fun block -> convert block) stream,
2016-09-08 21:13:10 +04:00
shutdown
let operation_watcher node =
Distributed_db.watch_operation node.distributed_db
2016-09-08 21:13:10 +04:00
2016-10-21 16:01:20 +04:00
let protocol_watcher node =
Distributed_db.watch_protocol node.distributed_db
2016-10-21 16:01:20 +04:00
2016-09-08 21:13:10 +04:00
let validate node net_id block =
Validator.get node.validator net_id >>=? fun net_v ->
Validator.fetch_block net_v block >>=? fun _ ->
return ()
let bootstrapped node =
let block_stream, stopper =
Validator.new_head_watcher node.mainnet_validator in
let first_run = ref true in
let rec next () =
if !first_run then begin
first_run := false ;
State.Valid_block.Current.head node.mainnet_net >>= fun head ->
Lwt.return (Some (head.hash, head.timestamp))
end else begin
Lwt.pick [
( Lwt_stream.get block_stream >|=
map_option ~f:(fun b -> (b.State.Valid_block.hash, b.timestamp)) ) ;
(Validator.bootstrapped node.mainnet_validator >|= fun () -> None) ;
]
end in
let shutdown () = Watcher.shutdown stopper in
RPC.Answer.{ next ; shutdown }
module Network = struct
let stat (node : t) =
P2p.RPC.stat node.p2p
let watch (node : t) =
P2p.RPC.watch node.p2p
let connect (node : t) =
P2p.RPC.connect node.p2p
module Connection = struct
let info (node : t) =
P2p.RPC.Connection.info node.p2p
let kick (node : t) =
P2p.RPC.Connection.kick node.p2p
let list (node : t) =
P2p.RPC.Connection.list node.p2p
let count (node : t) =
P2p.RPC.Connection.count node.p2p
end
module Point = struct
let info (node : t) =
P2p.RPC.Point.info node.p2p
let infos (node : t) restrict =
P2p.RPC.Point.infos ~restrict node.p2p
let events (node : t) =
P2p.RPC.Point.events node.p2p
let watch (node : t) =
P2p.RPC.Point.watch node.p2p
end
2017-02-24 06:50:33 +04:00
module Peer_id = struct
let info (node : t) =
P2p.RPC.Peer_id.info node.p2p
let infos (node : t) restrict =
P2p.RPC.Peer_id.infos ~restrict node.p2p
let events (node : t) =
P2p.RPC.Peer_id.events node.p2p
let watch (node : t) =
P2p.RPC.Peer_id.watch node.p2p
end
end
2016-09-08 21:13:10 +04:00
end