Commit 3e2624dc authored by Romain's avatar Romain Committed by Seb Mondet

Node: add --network command-line option

parent 08e59c44
......@@ -220,46 +220,38 @@ let blockchain_network_encoding : blockchain_network Data_encoding.t =
(list string)
[]))
let builtin_blockchain_networks_with_tags =
[ (1, "zeronet", blockchain_network_zeronet);
(2, "alphanet", blockchain_network_alphanet);
(3, "mainnet", blockchain_network_mainnet);
(4, "babylonnet", blockchain_network_babylonnet);
(5, "sandbox", blockchain_network_sandbox) ]
let builtin_blockchain_networks =
List.map
(fun (_, name, network) -> (name, network))
builtin_blockchain_networks_with_tags
let sugared_blockchain_network_encoding : blockchain_network Data_encoding.t =
let open Data_encoding in
let builtin_encoding (tag, name, network) =
let constructor = String.capitalize_ascii name in
case
(Tag tag)
~title:constructor
(constant constructor)
(fun _ -> None)
(fun () -> network)
in
union
~tag_size:`Uint8
[ case
( case
(Tag 0)
~title:"Custom"
blockchain_network_encoding
(fun x -> Some x)
(fun x -> x);
case
(Tag 1)
~title:"Zeronet"
(constant "Zeronet")
(fun _ -> None)
(fun () -> blockchain_network_zeronet);
case
(Tag 2)
~title:"Alphanet"
(constant "Alphanet")
(fun _ -> None)
(fun () -> blockchain_network_alphanet);
case
(Tag 3)
~title:"Mainnet"
(constant "Mainnet")
(fun _ -> None)
(fun () -> blockchain_network_mainnet);
case
(Tag 4)
~title:"Babylonnet"
(constant "Babylonnet")
(fun _ -> None)
(fun () -> blockchain_network_babylonnet);
case
(Tag 5)
~title:"Sandbox"
(constant "Sandbox")
(fun _ -> None)
(fun () -> blockchain_network_sandbox) ]
(fun x -> x)
:: List.map builtin_encoding builtin_blockchain_networks_with_tags )
type t = {
data_dir : string;
......@@ -1008,7 +1000,7 @@ let update ?data_dir ?min_connections ?expected_connections ?max_connections
?expected_pow ?bootstrap_peers ?listen_addr ?discovery_addr
?(rpc_listen_addrs = []) ?(private_mode = false) ?(disable_mempool = false)
?(disable_testchain = false) ?(cors_origins = []) ?(cors_headers = [])
?rpc_tls ?log_output ?bootstrap_threshold ?history_mode cfg =
?rpc_tls ?log_output ?bootstrap_threshold ?history_mode ?network cfg =
let data_dir = Option.unopt ~default:cfg.data_dir data_dir in
Node_data_version.ensure_data_dir data_dir
>>=? fun () ->
......@@ -1074,7 +1066,12 @@ let update ?data_dir ?min_connections ?expected_connections ?max_connections
history_mode = Option.first_some history_mode cfg.shell.history_mode;
}
in
return {cfg with data_dir; p2p; rpc; log; shell}
(* If --network is specified it overrides the "network" entry of the
configuration file, which itself defaults to mainnet. *)
let blockchain_network =
Option.unopt ~default:cfg.blockchain_network network
in
return {cfg with data_dir; p2p; rpc; log; shell; blockchain_network}
let resolve_addr ~default_addr ?default_port ?(passive = false) peer =
let (addr, port) = P2p_point.Id.parse_addr_port peer in
......
......@@ -39,6 +39,9 @@ type blockchain_network = {
default_bootstrap_peers : string list;
}
(** List of built-in networks with their command-line name. *)
val builtin_blockchain_networks : (string * blockchain_network) list
type t = {
data_dir : string;
p2p : p2p;
......@@ -111,6 +114,7 @@ val update :
?log_output:Lwt_log_sink_unix.Output.t ->
?bootstrap_threshold:int ->
?history_mode:History_mode.t ->
?network:blockchain_network ->
t ->
t tzresult Lwt.t
......
......@@ -32,6 +32,7 @@ let ( // ) = Filename.concat
type t = {
data_dir : string option;
config_file : string;
network : Node_config_file.blockchain_network option;
min_connections : int option;
expected_connections : int option;
max_connections : int option;
......@@ -56,11 +57,11 @@ type t = {
history_mode : History_mode.t option;
}
let wrap data_dir config_file connections max_download_speed max_upload_speed
binary_chunks_size peer_table_size listen_addr discovery_addr peers
no_bootstrap_peers bootstrap_threshold private_mode disable_mempool
disable_testchain expected_pow rpc_listen_addrs rpc_tls cors_origins
cors_headers log_output history_mode =
let wrap data_dir config_file network connections max_download_speed
max_upload_speed binary_chunks_size peer_table_size listen_addr
discovery_addr peers no_bootstrap_peers bootstrap_threshold private_mode
disable_mempool disable_testchain expected_pow rpc_listen_addrs rpc_tls
cors_origins cors_headers log_output history_mode =
let actual_data_dir =
Option.unopt ~default:Node_config_file.default_data_dir data_dir
in
......@@ -104,6 +105,7 @@ let wrap data_dir config_file connections max_download_speed max_upload_speed
{
data_dir;
config_file;
network;
min_connections;
expected_connections;
max_connections;
......@@ -206,6 +208,22 @@ module Term = struct
& opt (some string) None
& info ~docs ~doc ~docv:"FILE" ["config-file"])
let network =
let open Cmdliner in
let doc =
"Select which network to run. Possible values are: "
^ String.concat
", "
(List.map fst Node_config_file.builtin_blockchain_networks)
^ ". Default is mainnet. You cannot specify custom networks on the \
command-line, but you can specify them in the node configuration \
file."
in
Arg.(
value
& opt (some (enum Node_config_file.builtin_blockchain_networks)) None
& info ~docs ~doc ~docv:"NETWORK" ["network"])
(* P2p args *)
let docs = Manpage.p2p_section
......@@ -365,12 +383,12 @@ module Term = struct
let args =
let open Term in
const wrap $ data_dir $ config_file $ connections $ max_download_speed
$ max_upload_speed $ binary_chunks_size $ peer_table_size $ listen_addr
$ discovery_addr $ peers $ no_bootstrap_peers $ bootstrap_threshold
$ private_mode $ disable_mempool $ disable_testchain $ expected_pow
$ rpc_listen_addrs $ rpc_tls $ cors_origins $ cors_headers $ log_output
$ history_mode
const wrap $ data_dir $ config_file $ network $ connections
$ max_download_speed $ max_upload_speed $ binary_chunks_size
$ peer_table_size $ listen_addr $ discovery_addr $ peers
$ no_bootstrap_peers $ bootstrap_threshold $ private_mode $ disable_mempool
$ disable_testchain $ expected_pow $ rpc_listen_addrs $ rpc_tls
$ cors_origins $ cors_headers $ log_output $ history_mode
end
let read_config_file args =
......@@ -411,14 +429,29 @@ let read_and_patch_config_file ?(ignore_bootstrap_peers = false) args =
log_output;
bootstrap_threshold;
history_mode;
network;
config_file = _ } =
args
in
(* Update bootstrap peers must take into account the updated config file
with the [--network] argument, so we cannot use [Node_config_file]. *)
let bootstrap_peers =
if no_bootstrap_peers || ignore_bootstrap_peers then (
log_info "Ignoring bootstrap peers" ;
peers )
else Node_config_file.bootstrap_peers cfg @ peers
else
let cfg_peers =
match cfg.p2p.bootstrap_peers with
| Some peers ->
peers
| None -> (
match network with
| Some network ->
network.default_bootstrap_peers
| None ->
cfg.blockchain_network.default_bootstrap_peers )
in
cfg_peers @ peers
in
Node_config_file.update
?data_dir
......@@ -443,4 +476,5 @@ let read_and_patch_config_file ?(ignore_bootstrap_peers = false) args =
?log_output
?bootstrap_threshold
?history_mode
?network
cfg
......@@ -27,6 +27,7 @@
type t = {
data_dir : string option;
config_file : string;
network : Node_config_file.blockchain_network option;
min_connections : int option;
expected_connections : int option;
max_connections : int option;
......
......@@ -42,6 +42,7 @@ EOF
if ! [ -f "${node_dir}/config.json" ]; then
if [ -n "$USE_TLS" ]; then
$node config init \
--network "sandbox" \
--data-dir "$node_dir" \
--net-addr "127.0.0.1:$port" \
--rpc-addr "127.0.0.1:$rpc" \
......@@ -170,6 +171,7 @@ EOF
else
$node config init \
--network "sandbox" \
--data-dir "$node_dir" \
--net-addr "127.0.0.1:$port" \
--rpc-addr "127.0.0.1:$rpc" \
......@@ -179,7 +181,7 @@ EOF
fi
[ -f "${node_dir}/identity.json" ] || $node identity generate "$expected_pow" --data-dir "$node_dir"
$node run --data-dir "$node_dir" "${peers[@]}" "$sandbox_param" "[email protected]" &
$node run --network "sandbox" --data-dir "$node_dir" "${peers[@]}" "$sandbox_param" "[email protected]" &
node_pids+=("$!")
}
......
......@@ -64,7 +64,7 @@ class Node:
assert node_dir is None or os.path.isdir(node_dir), (f'{node_dir} not '
f'a dir')
if params is None:
params = []
params = ["--network=sandbox"]
self.log_file = log_file
self._temp_dir = node_dir is None
......
......@@ -168,6 +168,7 @@ class Sandbox:
params = ['--connections', '3']
if private:
params = params + ['--private-mode']
params = params + ['--network=sandbox']
peers_rpc = [self.p2p + p for p in peers]
node_bin = self._wrap_path(NODE, branch)
local_admin_client = self._wrap_path(CLIENT_ADMIN, branch)
......@@ -183,10 +184,12 @@ class Sandbox:
assert os.path.isfile(snapshot)
sandboxed_import = [f'--sandbox', self.sandbox_file]
if reconstruct:
node.snapshot_import(snapshot, ['--reconstruct']
+ sandboxed_import)
node.snapshot_import(snapshot,
['--reconstruct', '--network=sandbox'] +
sandboxed_import)
else:
node.snapshot_import(snapshot, sandboxed_import)
node.snapshot_import(snapshot,
['--network=sandbox'] + sandboxed_import)
node.init_id()
node.init_config()
node.run()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment