Shell: keep a reference to 'State.t' in 'State.Net.t'

This commit is contained in:
Grégoire Henry 2017-09-29 18:43:13 +02:00 committed by Benjamin Canou
parent 3909baaedc
commit f7aed9d45d
3 changed files with 20 additions and 14 deletions

View File

@ -48,6 +48,7 @@ and global_data = {
} }
and net_state = { and net_state = {
global_state: global_state ;
net_id: Net_id.t ; net_id: Net_id.t ;
genesis: genesis ; genesis: genesis ;
expiration: Time.t option ; expiration: Time.t option ;
@ -147,7 +148,7 @@ module Net = struct
let allocate let allocate
~genesis ~expiration ~allow_forked_network ~genesis ~expiration ~allow_forked_network
~current_head ~current_head
context_index chain_store block_store = global_state context_index chain_store block_store =
Store.Block.Contents.read_exn Store.Block.Contents.read_exn
(block_store, current_head) >>= fun current_block -> (block_store, current_head) >>= fun current_block ->
let rec chain_state = { let rec chain_state = {
@ -162,6 +163,7 @@ module Net = struct
chain_store ; chain_store ;
} }
and net_state = { and net_state = {
global_state ;
net_id = Net_id.of_block_hash genesis.block ; net_id = Net_id.of_block_hash genesis.block ;
chain_state = { Shared.data = chain_state ; lock = Lwt_mutex.create () } ; chain_state = { Shared.data = chain_state ; lock = Lwt_mutex.create () } ;
genesis ; genesis ;
@ -174,7 +176,7 @@ module Net = struct
Lwt.return net_state Lwt.return net_state
let locked_create let locked_create
data ?expiration ?(allow_forked_network = false) global_state data ?expiration ?(allow_forked_network = false)
net_id genesis commit = net_id genesis commit =
let net_store = Store.Net.get data.global_store net_id in let net_store = Store.Net.get data.global_store net_id in
let block_store = Store.Block.get net_store let block_store = Store.Block.get net_store
@ -202,6 +204,7 @@ module Net = struct
~current_head:genesis.block ~current_head:genesis.block
~expiration ~expiration
~allow_forked_network ~allow_forked_network
global_state
data.context_index data.context_index
chain_store chain_store
block_store block_store
@ -218,12 +221,12 @@ module Net = struct
~time:genesis.time ~time:genesis.time
~protocol:genesis.protocol >>= fun commit -> ~protocol:genesis.protocol >>= fun commit ->
locked_create locked_create
data ?allow_forked_network net_id genesis commit >>= fun net -> state data ?allow_forked_network net_id genesis commit >>= fun net ->
Net_id.Table.add data.nets net_id net ; Net_id.Table.add data.nets net_id net ;
Lwt.return net Lwt.return net
end end
let locked_read data id = let locked_read global_state data id =
let net_store = Store.Net.get data.global_store id in let net_store = Store.Net.get data.global_store id in
let block_store = Store.Block.get net_store let block_store = Store.Block.get net_store
and chain_store = Store.Chain.get net_store in and chain_store = Store.Chain.get net_store in
@ -240,22 +243,23 @@ module Net = struct
~current_head ~current_head
~expiration ~expiration
~allow_forked_network ~allow_forked_network
global_state
data.context_index data.context_index
chain_store chain_store
block_store >>= return block_store >>= return
let locked_read_all data = let locked_read_all global_state data =
Store.Net.list data.global_store >>= fun ids -> Store.Net.list data.global_store >>= fun ids ->
iter_p iter_p
(fun id -> (fun id ->
locked_read data id >>=? fun net -> locked_read global_state data id >>=? fun net ->
Net_id.Table.add data.nets id net ; Net_id.Table.add data.nets id net ;
return ()) return ())
ids ids
let read_all state = let read_all state =
Shared.use state.global_data begin fun data -> Shared.use state.global_data begin fun data ->
locked_read_all data locked_read_all state data
end end
let get state id = let get state id =
@ -274,6 +278,7 @@ module Net = struct
let genesis { genesis } = genesis let genesis { genesis } = genesis
let expiration { expiration } = expiration let expiration { expiration } = expiration
let allow_forked_network { allow_forked_network } = allow_forked_network let allow_forked_network { allow_forked_network } = allow_forked_network
let global_state { global_state } = global_state
let destroy state net = let destroy state net =
lwt_debug "destroy %a" Net_id.pp (id net) >>= fun () -> lwt_debug "destroy %a" Net_id.pp (id net) >>= fun () ->
@ -299,6 +304,7 @@ module Block = struct
let hash { hash } = hash let hash { hash } = hash
let header { contents = { header } } = header let header { contents = { header } } = header
let net_state { net_state } = net_state
let shell_header { contents = { header = { shell } } } = shell let shell_header { contents = { header = { shell } } } = shell
let net_id b = (shell_header b).net_id let net_id b = (shell_header b).net_id
let timestamp b = (shell_header b).timestamp let timestamp b = (shell_header b).timestamp
@ -506,8 +512,8 @@ let read_block_exn t hash =
| None -> Lwt.fail Not_found | None -> Lwt.fail Not_found
| Some b -> Lwt.return b | Some b -> Lwt.return b
let fork_testnet state block protocol expiration = let fork_testnet block protocol expiration =
Shared.use state.global_data begin fun data -> Shared.use block.net_state.global_state.global_data begin fun data ->
Block.context block >>= fun context -> Block.context block >>= fun context ->
Context.set_test_network context Not_running >>= fun context -> Context.set_test_network context Not_running >>= fun context ->
Context.set_protocol context protocol >>= fun context -> Context.set_protocol context protocol >>= fun context ->
@ -519,7 +525,7 @@ let fork_testnet state block protocol expiration =
time = Time.add block.contents.header.shell.timestamp 1L ; time = Time.add block.contents.header.shell.timestamp 1L ;
protocol ; protocol ;
} in } in
Net.locked_create data Net.locked_create block.net_state.global_state data
net_id ~expiration genesis commit >>= fun net -> net_id ~expiration genesis commit >>= fun net ->
return net return net
end end

View File

@ -79,6 +79,7 @@ module Net : sig
- its optional expiration time - its optional expiration time
- the associated global state. *) - the associated global state. *)
val global_state: net_state -> global_state
end end
@ -119,6 +120,7 @@ module Block : sig
val fitness: t -> Fitness.t val fitness: t -> Fitness.t
val operation_list_count: t -> int val operation_list_count: t -> int
val net_id: t -> Net_id.t val net_id: t -> Net_id.t
val net_state: t -> Net.t
val level: t -> Int32.t val level: t -> Int32.t
val message: t -> string val message: t -> string
val max_operations_ttl: t -> int val max_operations_ttl: t -> int
@ -149,8 +151,7 @@ val read_block_exn:
global_state -> Block_hash.t -> Block.t Lwt.t global_state -> Block_hash.t -> Block.t Lwt.t
val fork_testnet: val fork_testnet:
global_state -> Block.t -> Protocol_hash.t -> Time.t -> Block.t -> Protocol_hash.t -> Time.t -> Net.t tzresult Lwt.t
Net.t tzresult Lwt.t
type chain_data = { type chain_data = {
current_head: Block.t ; current_head: Block.t ;

View File

@ -702,8 +702,7 @@ let rec create_validator ?parent worker ?max_child_ttl state db net =
State.Net.get state net_id >>= function State.Net.get state net_id >>= function
| Ok net_store -> return net_store | Ok net_store -> return net_store
| Error _ -> | Error _ ->
State.fork_testnet State.fork_testnet block protocol expiration >>=? fun net_store ->
state block protocol expiration >>=? fun net_store ->
Chain.head net_store >>= fun block -> Chain.head net_store >>= fun block ->
Watcher.notify v.worker.valid_block_input block ; Watcher.notify v.worker.valid_block_input block ;
return net_store return net_store