From 3d57055ccabec3ffaf2f8d88f0cb7494d2f44b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 9 Apr 2018 16:28:43 +0200 Subject: [PATCH] Alpha: use protocol parameters instead of sandbox to tune constants --- scripts/protocol_parameters.json | 9 +- scripts/sandbox.json | 16 +- src/bin_client/test/protocol_parameters.json | 10 +- src/bin_client/test/sandbox.json | 17 +- src/bin_client/tezos-init-sandboxed-client.sh | 9 +- src/bin_node/tezos-sandboxed-node.sh | 9 +- src/proto_alpha/lib_baking/test/jbuild | 10 +- .../lib_baking/test/node_helpers.ml | 16 +- .../lib_baking/test/node_helpers.mli | 3 +- .../lib_baking/test/proto_alpha_helpers.ml | 51 ++++- .../lib_baking/test/proto_alpha_helpers.mli | 2 +- .../lib_baking/test/sandbox-vote.json | 18 -- src/proto_alpha/lib_baking/test/sandbox.json | 17 -- .../lib_baking/test/test_endorsement.ml | 5 +- src/proto_alpha/lib_baking/test/test_rpc.ml | 5 +- src/proto_alpha/lib_baking/test/test_vote.ml | 5 +- .../lib_protocol/src/constants_repr.ml | 192 ---------------- .../lib_protocol/src/parameters_repr.ml | 206 +++++++++++++++++- .../lib_protocol/src/parameters_repr.mli | 4 +- .../lib_protocol/src/raw_context.ml | 55 +++-- .../lib_protocol/src/raw_context.mli | 3 +- .../lib_protocol/test/helpers/helpers_init.ml | 13 +- 22 files changed, 337 insertions(+), 338 deletions(-) delete mode 100644 src/proto_alpha/lib_baking/test/sandbox-vote.json delete mode 100644 src/proto_alpha/lib_baking/test/sandbox.json diff --git a/scripts/protocol_parameters.json b/scripts/protocol_parameters.json index ab136dde2..92ba62c31 100644 --- a/scripts/protocol_parameters.json +++ b/scripts/protocol_parameters.json @@ -16,5 +16,12 @@ [ "tz1iDPZLxcGf5CqCNpTuuMdtu3zKpJ6HvvFR", "btz1TaSfoSNhFoqwqbPC9iC19rN24KJtB7skD", "71300478465380003" ], [ "tz1i2kbtVu65dP739qLGRpJNujRM8pdpkH3p", "btz1USqQRuvASPXwseXkGTWeWv4dn3VwMVPEk", "283380756728119992" ], [ "tz1LKGCg6ESJLDviHkT8Jc7tUwjw4h3d9MaF", "btz1YwCKMbBLRL1qkBjAHGCwjbWDqiTAEFpbw", "1357762577679880028" ] - ] + ], + "dictator_pubkey": + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", + "time_between_blocks" : [ 1, 0 ], + "blocks_per_roll_snapshot" : 4, + "blocks_per_cycle" : 8, + "preserved_cycles" : 2, + "first_free_baking_slot" : 4 } diff --git a/scripts/sandbox.json b/scripts/sandbox.json index 2838b3595..007ba123e 100644 --- a/scripts/sandbox.json +++ b/scripts/sandbox.json @@ -1,18 +1,4 @@ { "genesis_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "dictator_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "bootstrap_keys": [ - "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", - "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", - "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", - "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", - "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n" - ], - "time_between_blocks" : [ 1, 0 ], - "blocks_per_roll_snapshot" : 4, - "blocks_per_cycle" : 8, - "preserved_cycles" : 2, - "first_free_baking_slot" : 4 + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2" } diff --git a/src/bin_client/test/protocol_parameters.json b/src/bin_client/test/protocol_parameters.json index 3bf19a7bc..cdd9f6b79 100644 --- a/src/bin_client/test/protocol_parameters.json +++ b/src/bin_client/test/protocol_parameters.json @@ -18,5 +18,13 @@ [ "tz1iDPZLxcGf5CqCNpTuuMdtu3zKpJ6HvvFR", "btz1TaSfoSNhFoqwqbPC9iC19rN24KJtB7skD", "71300478465380003" ], [ "tz1i2kbtVu65dP739qLGRpJNujRM8pdpkH3p", "btz1USqQRuvASPXwseXkGTWeWv4dn3VwMVPEk", "283380756728119992" ], [ "tz1LKGCg6ESJLDviHkT8Jc7tUwjw4h3d9MaF", "btz1YwCKMbBLRL1qkBjAHGCwjbWDqiTAEFpbw", "1357762577679880028" ] - ] + ], + "dictator_pubkey": + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", + "time_between_blocks" : [ 1, 0 ], + "blocks_per_cycle" : 128, + "blocks_per_roll_snapshot" : 32, + "preserved_cycles" : 1, + "first_free_baking_slot" : 4, + "proof_of_work_threshold": -1 } diff --git a/src/bin_client/test/sandbox.json b/src/bin_client/test/sandbox.json index 86e739777..007ba123e 100644 --- a/src/bin_client/test/sandbox.json +++ b/src/bin_client/test/sandbox.json @@ -1,19 +1,4 @@ { "genesis_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "dictator_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "bootstrap_keys": [ - "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", - "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", - "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", - "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", - "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n" - ], - "time_between_blocks" : [ 1, 0 ], - "blocks_per_cycle" : 128, - "blocks_per_roll_snapshot" : 32, - "preserved_cycles" : 1, - "first_free_baking_slot" : 4, - "proof_of_work_threshold": -1 + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2" } diff --git a/src/bin_client/tezos-init-sandboxed-client.sh b/src/bin_client/tezos-init-sandboxed-client.sh index 93b744e81..5d19033df 100755 --- a/src/bin_client/tezos-init-sandboxed-client.sh +++ b/src/bin_client/tezos-init-sandboxed-client.sh @@ -27,8 +27,13 @@ init_sandboxed_client() { [ "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", "4000000000000" ], [ "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n", "4000000000000" ] ], - "commitments": [ - ] + "dictator_pubkey": + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", + "time_between_blocks" : [ 1, 0 ], + "blocks_per_roll_snapshot" : 4, + "blocks_per_cycle" : 8, + "preserved_cycles" : 2, + "first_free_baking_slot" : 4 } EOF fi diff --git a/src/bin_node/tezos-sandboxed-node.sh b/src/bin_node/tezos-sandboxed-node.sh index b14bd5c0e..7101931bd 100755 --- a/src/bin_node/tezos-sandboxed-node.sh +++ b/src/bin_node/tezos-sandboxed-node.sh @@ -29,14 +29,7 @@ start_sandboxed_node() { cat > "$sandbox_file" < None + | Some json -> + let file_name, ch = + Filename.open_temp_file "tezos_node_" ".log" in + Printf.fprintf ch "%s%!" + (Data_encoding.Json.to_string json) ; + close_out ch ; + Some file_name in let log_fd = Unix.descr_of_out_channel log_file in let null_fd = Unix.(openfile "/dev/null" [O_RDONLY] 0o644) in let exe = @@ -48,7 +58,6 @@ let fork_node ?exe ?(timeout = 4) ?(port = 18732) ?sandbox () = else path with _ -> Sys.getcwd () // ".." // "bin_node" // "main.exe" in - Format.eprintf "EXE %s@." exe ; let pid = Unix.create_process exe [| "tezos-node" ; @@ -73,7 +82,10 @@ let fork_node ?exe ?(timeout = 4) ?(port = 18732) ?sandbox () = | res -> handle_error res log_file_name end ; - ignore (Sys.command (Printf.sprintf "rm -fr \"%s\"" data_dir)) + ignore (Sys.command (Printf.sprintf "rm -fr \"%s\"" data_dir)) ; + match sandbox with + | None -> () + | Some file -> ignore (Sys.command (Printf.sprintf "rm -f \"%s\"" file)) end ; pid | res -> diff --git a/src/proto_alpha/lib_baking/test/node_helpers.mli b/src/proto_alpha/lib_baking/test/node_helpers.mli index b47811966..babd81833 100644 --- a/src/proto_alpha/lib_baking/test/node_helpers.mli +++ b/src/proto_alpha/lib_baking/test/node_helpers.mli @@ -8,7 +8,8 @@ (**************************************************************************) val fork_node: - ?exe:string -> ?timeout:int -> ?port:int -> ?sandbox:string -> + ?exe:string -> ?timeout:int -> ?port:int -> + ?sandbox:Data_encoding.json -> unit -> int (** [fork_node ()] forks a node in sandbox mode listening to rpc on `localhost:port` (where the default port is 18732) and returns the diff --git a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml index 1d2429356..ddc4f2f90 100644 --- a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml +++ b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.ml @@ -44,6 +44,15 @@ let no_write_context ?(block = `Head 0) config : #Client_context.full = object Format.kasprintf (fun _ -> Lwt.return "") end +let sandbox_parameters = + let json_result = + Data_encoding.Json.from_string {json| +{ "genesis_pubkey": "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2" } +|json} in + match json_result with + | Error err -> raise (Failure err) + | Ok json -> json + let protocol_parameters = let json_result = Data_encoding.Json.from_string {json| @@ -65,7 +74,13 @@ let protocol_parameters = [ "tz1iDPZLxcGf5CqCNpTuuMdtu3zKpJ6HvvFR", "btz1TaSfoSNhFoqwqbPC9iC19rN24KJtB7skD", "71300478465380003" ], [ "tz1i2kbtVu65dP739qLGRpJNujRM8pdpkH3p", "btz1USqQRuvASPXwseXkGTWeWv4dn3VwMVPEk", "283380756728119992" ], [ "tz1LKGCg6ESJLDviHkT8Jc7tUwjw4h3d9MaF", "btz1YwCKMbBLRL1qkBjAHGCwjbWDqiTAEFpbw", "1357762577679880028" ] - ] + ], + "time_between_blocks" : [ 1, 0 ], + "blocks_per_cycle" : 4, + "blocks_per_roll_snapshot" : 2, + "preserved_cycles" : 1, + "first_free_baking_slot" : 4, + "proof_of_work_threshold": -1 } |json} in match json_result with @@ -73,11 +88,37 @@ let protocol_parameters = | Ok json -> Data_encoding.Binary.to_bytes Data_encoding.json json -let activate_alpha () = +let vote_protocol_parameters = + let json_result = + Data_encoding.Json.from_string {json| +{ "bootstrap_accounts": [ + [ "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", "4000000000000" ], + [ "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", "4000000000000" ], + [ "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", "4000000000000" ], + [ "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", "4000000000000" ], + [ "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n", "4000000000000" ] + ], + "time_between_blocks" : [ 1, 0 ], + "blocks_per_cycle" : 4, + "blocks_per_roll_snapshot" : 2, + "preserved_cycles" : 1, + "first_free_baking_slot" : 4, + "blocks_per_voting_period": 2, + "proof_of_work_threshold": -1 +} +|json} in + match json_result with + | Error err -> raise (Failure err) + | Ok json -> + Data_encoding.Binary.to_bytes Data_encoding.json json + +let activate_alpha ?(vote = false) () = let fitness = Fitness_repr.from_int64 0L in let dictator_sk = Client_keys.Secret_key_locator.create ~scheme:"unencrypted" ~location:"edsk31vznjHSSpGExDMHYASz45VZqXN4DPxvsa4hAyY8dHM28cZzp6" in + let protocol_parameters = + if vote then vote_protocol_parameters else protocol_parameters in Tezos_client_genesis.Client_proto_main.bake (no_write_context ~block:(`Head 0) !rpc_config) (`Head 0) (Activate { protocol = Proto_alpha.hash ; @@ -86,7 +127,7 @@ let activate_alpha () = }) dictator_sk -let init ?exe ?(sandbox = "sandbox.json") ?rpc_port () = +let init ?exe ?vote ?rpc_port () = begin match rpc_port with | None -> () @@ -98,9 +139,9 @@ let init ?exe ?(sandbox = "sandbox.json") ?rpc_port () = Node_helpers.fork_node ?exe ~port:!rpc_config.port - ~sandbox + ~sandbox:sandbox_parameters () in - activate_alpha () >>=? fun hash -> + activate_alpha ?vote () >>=? fun hash -> return (pid, hash) let level block = diff --git a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.mli b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.mli index 8959c3d6a..47e2a5293 100644 --- a/src/proto_alpha/lib_baking/test/proto_alpha_helpers.mli +++ b/src/proto_alpha/lib_baking/test/proto_alpha_helpers.mli @@ -12,7 +12,7 @@ open Alpha_context val init : ?exe:string -> - ?sandbox:string -> + ?vote:bool -> ?rpc_port:int -> unit -> (int * Block_hash.t) tzresult Lwt.t (** [init ()] sets up the test environment, and return the PID of diff --git a/src/proto_alpha/lib_baking/test/sandbox-vote.json b/src/proto_alpha/lib_baking/test/sandbox-vote.json deleted file mode 100644 index 41ea8eb9b..000000000 --- a/src/proto_alpha/lib_baking/test/sandbox-vote.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "genesis_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "bootstrap_keys": [ - "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", - "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", - "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", - "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", - "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n" - ], - "time_between_blocks" : [ 1, 0 ], - "blocks_per_cycle" : 4, - "blocks_per_roll_snapshot" : 2, - "preserved_cycles" : 1, - "first_free_baking_slot" : 4, - "blocks_per_voting_period": 2, - "proof_of_work_threshold": -1 -} diff --git a/src/proto_alpha/lib_baking/test/sandbox.json b/src/proto_alpha/lib_baking/test/sandbox.json deleted file mode 100644 index ec8dfeae4..000000000 --- a/src/proto_alpha/lib_baking/test/sandbox.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "genesis_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "bootstrap_keys": [ - "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", - "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", - "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", - "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", - "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n" - ], - "time_between_blocks" : [ 1, 0 ], - "blocks_per_cycle" : 4, - "blocks_per_roll_snapshot" : 2, - "preserved_cycles" : 1, - "first_free_baking_slot" : 4, - "proof_of_work_threshold": -1 -} diff --git a/src/proto_alpha/lib_baking/test/test_endorsement.ml b/src/proto_alpha/lib_baking/test/test_endorsement.ml index 67f8e06c2..3fa68a3d7 100644 --- a/src/proto_alpha/lib_baking/test/test_endorsement.ml +++ b/src/proto_alpha/lib_baking/test/test_endorsement.ml @@ -245,11 +245,10 @@ let run genesis = return () let exe = try Sys.argv.(1) with _ -> "tezos-node" -let sandbox = try Sys.argv.(2) with _ -> "sandbox.json" -let rpc_port = try int_of_string Sys.argv.(3) with _ -> 18100 +let rpc_port = try int_of_string Sys.argv.(2) with _ -> 18100 let main () = - Helpers.init ~exe ~sandbox ~rpc_port () >>=? fun (_node_pid, genesis) -> + Helpers.init ~exe ~rpc_port () >>=? fun (_node_pid, genesis) -> run (`Hash (genesis, 0)) diff --git a/src/proto_alpha/lib_baking/test/test_rpc.ml b/src/proto_alpha/lib_baking/test/test_rpc.ml index 1b7aeb191..7bbf67aad 100644 --- a/src/proto_alpha/lib_baking/test/test_rpc.ml +++ b/src/proto_alpha/lib_baking/test/test_rpc.ml @@ -49,11 +49,10 @@ let run blkid = ) tests let exe = try Sys.argv.(1) with _ -> "tezos-node" -let sandbox = try Sys.argv.(2) with _ -> "sandbox.json" -let rpc_port = try int_of_string Sys.argv.(3) with _ -> 18500 +let rpc_port = try int_of_string Sys.argv.(2) with _ -> 18500 let main () = - Helpers.init ~exe ~sandbox ~rpc_port () >>=? fun (_node_pid, genesis) -> + Helpers.init ~exe ~rpc_port () >>=? fun (_node_pid, genesis) -> run (`Hash (genesis, 0)) let tests = [ diff --git a/src/proto_alpha/lib_baking/test/test_vote.ml b/src/proto_alpha/lib_baking/test/test_vote.ml index a619446b1..67ff0298e 100644 --- a/src/proto_alpha/lib_baking/test/test_vote.ml +++ b/src/proto_alpha/lib_baking/test/test_vote.ml @@ -85,11 +85,10 @@ let run_change_to_demo_proto block return (`Hash (head, 0)) let exe = try Sys.argv.(1) with _ -> "tezos-node" -let sandbox = try Sys.argv.(2) with _ -> "sandbox-vote.json" -let rpc_port = try int_of_string Sys.argv.(3) with _ -> 18400 +let rpc_port = try int_of_string Sys.argv.(2) with _ -> 18400 let change_to_demo_proto () = - init ~exe ~sandbox ~rpc_port () >>=? fun (_node_pid, hash) -> + init ~exe ~vote:true ~rpc_port () >>=? fun (_node_pid, hash) -> run_change_to_demo_proto (`Hash (hash, 0)) Account.bootstrap_accounts >>=? fun _blkh -> return () diff --git a/src/proto_alpha/lib_protocol/src/constants_repr.ml b/src/proto_alpha/lib_protocol/src/constants_repr.ml index 5017853c4..8b3de06b6 100644 --- a/src/proto_alpha/lib_protocol/src/constants_repr.ml +++ b/src/proto_alpha/lib_protocol/src/constants_repr.ml @@ -101,186 +101,6 @@ let default = { module CompareListInt = Compare.List (Compare.Int) -(* This encoding is used to read configuration files (e.g. sandbox.json) - where some fields can be missing, in that case they are replaced by - the default. *) -let sandbox_encoding = - let open Data_encoding in - conv - (fun c -> - let module Compare_time_between_blocks = Compare.List (Period_repr) in - let module Compare_keys = Compare.List (Ed25519.Public_key) in - let opt (=) def v = if def = v then None else Some v in - let preserved_cycles = - opt Compare.Int.(=) - default.preserved_cycles c.preserved_cycles - and blocks_per_cycle = - opt Compare.Int32.(=) - default.blocks_per_cycle c.blocks_per_cycle - and blocks_per_commitment = - opt Compare.Int32.(=) - default.blocks_per_commitment c.blocks_per_commitment - and blocks_per_roll_snapshot = - opt Compare.Int32.(=) - default.blocks_per_roll_snapshot c.blocks_per_roll_snapshot - and blocks_per_voting_period = - opt Compare.Int32.(=) - default.blocks_per_voting_period c.blocks_per_voting_period - and time_between_blocks = - opt Compare_time_between_blocks.(=) - default.time_between_blocks c.time_between_blocks - and first_free_baking_slot = - opt Compare.Int.(=) - default.first_free_baking_slot c.first_free_baking_slot - and endorsers_per_block = - opt Compare.Int.(=) - default.endorsers_per_block c.endorsers_per_block - and max_gas = - opt Compare.Int.(=) - default.max_gas c.max_gas - and proof_of_work_threshold = - opt Compare.Int64.(=) - default.proof_of_work_threshold c.proof_of_work_threshold - and dictator_pubkey = - opt Ed25519.Public_key.(=) - default.dictator_pubkey c.dictator_pubkey - and max_operation_data_length = - opt Compare.Int.(=) - default.max_operation_data_length c.max_operation_data_length - and tokens_per_roll = - opt Tez_repr.(=) - default.tokens_per_roll c.tokens_per_roll - and michelson_maximum_type_size = - opt Compare.Int.(=) - default.michelson_maximum_type_size c.michelson_maximum_type_size - and seed_nonce_revelation_tip = - opt Tez_repr.(=) - default.seed_nonce_revelation_tip c.seed_nonce_revelation_tip - and origination_burn = - opt Tez_repr.(=) - default.origination_burn c.origination_burn - and block_security_deposit = - opt Tez_repr.(=) - default.block_security_deposit c.block_security_deposit - and endorsement_security_deposit = - opt Tez_repr.(=) - default.endorsement_security_deposit c.endorsement_security_deposit - and block_reward = - opt Tez_repr.(=) - default.block_reward c.block_reward - and endorsement_reward = - opt Tez_repr.(=) - default.endorsement_reward c.endorsement_reward - in - ((( preserved_cycles, - blocks_per_cycle, - blocks_per_commitment, - blocks_per_roll_snapshot, - blocks_per_voting_period, - time_between_blocks, - first_free_baking_slot, - endorsers_per_block, - max_gas, - proof_of_work_threshold), - ( dictator_pubkey, - max_operation_data_length, - tokens_per_roll, - michelson_maximum_type_size, - seed_nonce_revelation_tip, - origination_burn, - block_security_deposit, - endorsement_security_deposit, - block_reward, - endorsement_reward)), ()) ) - (fun ((( preserved_cycles, - blocks_per_cycle, - blocks_per_commitment, - blocks_per_roll_snapshot, - blocks_per_voting_period, - time_between_blocks, - first_free_baking_slot, - endorsers_per_block, - max_gas, - proof_of_work_threshold), - ( dictator_pubkey, - max_operation_data_length, - tokens_per_roll, - michelson_maximum_type_size, - seed_nonce_revelation_tip, - origination_burn, - block_security_deposit, - endorsement_security_deposit, - block_reward, - endorsement_reward)), ()) -> - let unopt def = function None -> def | Some v -> v in - { preserved_cycles = - unopt default.preserved_cycles preserved_cycles ; - blocks_per_cycle = - unopt default.blocks_per_cycle blocks_per_cycle ; - blocks_per_commitment = - unopt default.blocks_per_commitment blocks_per_commitment ; - blocks_per_roll_snapshot = - unopt default.blocks_per_roll_snapshot blocks_per_roll_snapshot ; - blocks_per_voting_period = - unopt default.blocks_per_voting_period blocks_per_voting_period ; - time_between_blocks = - unopt default.time_between_blocks @@ - time_between_blocks ; - first_free_baking_slot = - unopt default.first_free_baking_slot first_free_baking_slot ; - endorsers_per_block = - unopt default.endorsers_per_block endorsers_per_block ; - max_gas = - unopt default.max_gas max_gas ; - proof_of_work_threshold = - unopt default.proof_of_work_threshold proof_of_work_threshold ; - dictator_pubkey = - unopt default.dictator_pubkey dictator_pubkey ; - max_operation_data_length = - unopt default.max_operation_data_length max_operation_data_length ; - tokens_per_roll = - unopt default.tokens_per_roll tokens_per_roll ; - michelson_maximum_type_size = - unopt default.michelson_maximum_type_size michelson_maximum_type_size ; - seed_nonce_revelation_tip = - unopt default.seed_nonce_revelation_tip seed_nonce_revelation_tip ; - origination_burn = - unopt default.origination_burn origination_burn ; - block_security_deposit = - unopt default.block_security_deposit block_security_deposit ; - endorsement_security_deposit = - unopt default.endorsement_security_deposit endorsement_security_deposit ; - block_reward = - unopt default.block_reward block_reward ; - endorsement_reward = - unopt default.endorsement_reward endorsement_reward ; - } ) - (merge_objs - (merge_objs - (obj10 - (opt "preserved_cycles" uint8) - (opt "blocks_per_cycle" int32) - (opt "blocks_per_commitment" int32) - (opt "blocks_per_roll_snapshot" int32) - (opt "blocks_per_voting_period" int32) - (opt "time_between_blocks" (list Period_repr.encoding)) - (opt "first_free_baking_slot" uint16) - (opt "endorsers_per_block" uint16) - (opt "instructions_per_transaction" int31) - (opt "proof_of_work_threshold" int64)) - (obj10 - (opt "dictator_pubkey" Ed25519.Public_key.encoding) - (opt "max_operation_data_length" int31) - (opt "tokens_per_roll" Tez_repr.encoding) - (opt "michelson_maximum_type_size" uint16) - (opt "seed_nonce_revelation_tip" Tez_repr.encoding) - (opt "origination_burn" Tez_repr.encoding) - (opt "block_security_deposit" Tez_repr.encoding) - (opt "endorsement_security_deposit" Tez_repr.encoding) - (opt "block_reward" Tez_repr.encoding) - (opt "endorsement_reward" Tez_repr.encoding))) - unit) - let parametric_encoding = let open Data_encoding in conv @@ -383,15 +203,3 @@ let encoding = (merge_objs fixed_encoding parametric_encoding) type error += Constant_read of exn - -let read_sandbox = function - | None -> - return default - | Some json -> - match Data_encoding.Json.(destruct sandbox_encoding json) with - | exception exn -> fail (Constant_read exn) - | c -> - if Compare.Int32.(c.blocks_per_roll_snapshot > c.blocks_per_cycle) then - failwith "Invalid sandbox: 'blocks_per_roll_snapshot > blocks_per_cycle'" - else - return c diff --git a/src/proto_alpha/lib_protocol/src/parameters_repr.ml b/src/proto_alpha/lib_protocol/src/parameters_repr.ml index 7db2c986a..c6ad50f42 100644 --- a/src/proto_alpha/lib_protocol/src/parameters_repr.ml +++ b/src/proto_alpha/lib_protocol/src/parameters_repr.ml @@ -14,10 +14,10 @@ type bootstrap_account = { type t = { bootstrap_accounts : bootstrap_account list ; - commitments : (Unclaimed_public_key_hash.t * Commitment_repr.t) list + commitments : (Unclaimed_public_key_hash.t * Commitment_repr.t) list ; + constants : Constants_repr.parametric ; } - let bootstrap_account_encoding = let open Data_encoding in conv @@ -25,16 +25,198 @@ let bootstrap_account_encoding = (fun (public_key, amount) -> { public_key ; amount }) (tup2 Ed25519.Public_key.encoding Tez_repr.encoding) +(* This encoding is used to read configuration files (e.g. sandbox.json) + where some fields can be missing, in that case they are replaced by + the default. *) +let constants_encoding = + let open Data_encoding in + conv + (fun (c : Constants_repr.parametric) -> + let module Compare_time_between_blocks = Compare.List (Period_repr) in + let module Compare_keys = Compare.List (Ed25519.Public_key) in + let opt (=) def v = if def = v then None else Some v in + let default = Constants_repr.default in + let preserved_cycles = + opt Compare.Int.(=) + default.preserved_cycles c.preserved_cycles + and blocks_per_cycle = + opt Compare.Int32.(=) + default.blocks_per_cycle c.blocks_per_cycle + and blocks_per_commitment = + opt Compare.Int32.(=) + default.blocks_per_commitment c.blocks_per_commitment + and blocks_per_roll_snapshot = + opt Compare.Int32.(=) + default.blocks_per_roll_snapshot c.blocks_per_roll_snapshot + and blocks_per_voting_period = + opt Compare.Int32.(=) + default.blocks_per_voting_period c.blocks_per_voting_period + and time_between_blocks = + opt Compare_time_between_blocks.(=) + default.time_between_blocks c.time_between_blocks + and first_free_baking_slot = + opt Compare.Int.(=) + default.first_free_baking_slot c.first_free_baking_slot + and endorsers_per_block = + opt Compare.Int.(=) + default.endorsers_per_block c.endorsers_per_block + and max_gas = + opt Compare.Int.(=) + default.max_gas c.max_gas + and proof_of_work_threshold = + opt Compare.Int64.(=) + default.proof_of_work_threshold c.proof_of_work_threshold + and dictator_pubkey = + opt Ed25519.Public_key.(=) + default.dictator_pubkey c.dictator_pubkey + and max_operation_data_length = + opt Compare.Int.(=) + default.max_operation_data_length c.max_operation_data_length + and tokens_per_roll = + opt Tez_repr.(=) + default.tokens_per_roll c.tokens_per_roll + and michelson_maximum_type_size = + opt Compare.Int.(=) + default.michelson_maximum_type_size c.michelson_maximum_type_size + and seed_nonce_revelation_tip = + opt Tez_repr.(=) + default.seed_nonce_revelation_tip c.seed_nonce_revelation_tip + and origination_burn = + opt Tez_repr.(=) + default.origination_burn c.origination_burn + and block_security_deposit = + opt Tez_repr.(=) + default.block_security_deposit c.block_security_deposit + and endorsement_security_deposit = + opt Tez_repr.(=) + default.endorsement_security_deposit c.endorsement_security_deposit + and block_reward = + opt Tez_repr.(=) + default.block_reward c.block_reward + and endorsement_reward = + opt Tez_repr.(=) + default.endorsement_reward c.endorsement_reward + in + (( preserved_cycles, + blocks_per_cycle, + blocks_per_commitment, + blocks_per_roll_snapshot, + blocks_per_voting_period, + time_between_blocks, + first_free_baking_slot, + endorsers_per_block, + max_gas, + proof_of_work_threshold), + ( dictator_pubkey, + max_operation_data_length, + tokens_per_roll, + michelson_maximum_type_size, + seed_nonce_revelation_tip, + origination_burn, + block_security_deposit, + endorsement_security_deposit, + block_reward, + endorsement_reward))) + (fun (( preserved_cycles, + blocks_per_cycle, + blocks_per_commitment, + blocks_per_roll_snapshot, + blocks_per_voting_period, + time_between_blocks, + first_free_baking_slot, + endorsers_per_block, + max_gas, + proof_of_work_threshold), + ( dictator_pubkey, + max_operation_data_length, + tokens_per_roll, + michelson_maximum_type_size, + seed_nonce_revelation_tip, + origination_burn, + block_security_deposit, + endorsement_security_deposit, + block_reward, + endorsement_reward)) -> + let unopt def = function None -> def | Some v -> v in + let default = Constants_repr.default in + { Constants_repr.preserved_cycles = + unopt default.preserved_cycles preserved_cycles ; + blocks_per_cycle = + unopt default.blocks_per_cycle blocks_per_cycle ; + blocks_per_commitment = + unopt default.blocks_per_commitment blocks_per_commitment ; + blocks_per_roll_snapshot = + unopt default.blocks_per_roll_snapshot blocks_per_roll_snapshot ; + blocks_per_voting_period = + unopt default.blocks_per_voting_period blocks_per_voting_period ; + time_between_blocks = + unopt default.time_between_blocks @@ + time_between_blocks ; + first_free_baking_slot = + unopt default.first_free_baking_slot first_free_baking_slot ; + endorsers_per_block = + unopt default.endorsers_per_block endorsers_per_block ; + max_gas = + unopt default.max_gas max_gas ; + proof_of_work_threshold = + unopt default.proof_of_work_threshold proof_of_work_threshold ; + dictator_pubkey = + unopt default.dictator_pubkey dictator_pubkey ; + max_operation_data_length = + unopt default.max_operation_data_length max_operation_data_length ; + tokens_per_roll = + unopt default.tokens_per_roll tokens_per_roll ; + michelson_maximum_type_size = + unopt default.michelson_maximum_type_size michelson_maximum_type_size ; + seed_nonce_revelation_tip = + unopt default.seed_nonce_revelation_tip seed_nonce_revelation_tip ; + origination_burn = + unopt default.origination_burn origination_burn ; + block_security_deposit = + unopt default.block_security_deposit block_security_deposit ; + endorsement_security_deposit = + unopt default.endorsement_security_deposit endorsement_security_deposit ; + block_reward = + unopt default.block_reward block_reward ; + endorsement_reward = + unopt default.endorsement_reward endorsement_reward ; + } ) + (merge_objs + (obj10 + (opt "preserved_cycles" uint8) + (opt "blocks_per_cycle" int32) + (opt "blocks_per_commitment" int32) + (opt "blocks_per_roll_snapshot" int32) + (opt "blocks_per_voting_period" int32) + (opt "time_between_blocks" (list Period_repr.encoding)) + (opt "first_free_baking_slot" uint16) + (opt "endorsers_per_block" uint16) + (opt "instructions_per_transaction" int31) + (opt "proof_of_work_threshold" int64)) + (obj10 + (opt "dictator_pubkey" Ed25519.Public_key.encoding) + (opt "max_operation_data_length" int31) + (opt "tokens_per_roll" Tez_repr.encoding) + (opt "michelson_maximum_type_size" uint16) + (opt "seed_nonce_revelation_tip" Tez_repr.encoding) + (opt "origination_burn" Tez_repr.encoding) + (opt "block_security_deposit" Tez_repr.encoding) + (opt "endorsement_security_deposit" Tez_repr.encoding) + (opt "block_reward" Tez_repr.encoding) + (opt "endorsement_reward" Tez_repr.encoding))) + let encoding = let open Data_encoding in conv - (fun { bootstrap_accounts ; commitments } -> - (bootstrap_accounts, commitments)) - (fun ( bootstrap_accounts, commitments ) -> - { bootstrap_accounts ; commitments }) - (obj2 - (req "bootstrap_accounts" (list bootstrap_account_encoding)) - (req "commitments" - (list (merge_tups - (tup1 Unclaimed_public_key_hash.encoding) - Commitment_repr.encoding)))) + (fun { bootstrap_accounts ; commitments ; constants } -> + ((bootstrap_accounts, commitments), constants )) + (fun ( (bootstrap_accounts, commitments), constants ) -> + { bootstrap_accounts ; commitments ; constants }) + (merge_objs + (obj2 + (req "bootstrap_accounts" (list bootstrap_account_encoding)) + (dft "commitments" + (list (merge_tups + (tup1 Unclaimed_public_key_hash.encoding) + Commitment_repr.encoding)) [])) + constants_encoding) diff --git a/src/proto_alpha/lib_protocol/src/parameters_repr.mli b/src/proto_alpha/lib_protocol/src/parameters_repr.mli index 37198a3c8..deaaf3929 100644 --- a/src/proto_alpha/lib_protocol/src/parameters_repr.mli +++ b/src/proto_alpha/lib_protocol/src/parameters_repr.mli @@ -14,7 +14,9 @@ type bootstrap_account = { type t = { bootstrap_accounts : bootstrap_account list ; - commitments : (Unclaimed_public_key_hash.t * Commitment_repr.t) list + commitments : (Unclaimed_public_key_hash.t * Commitment_repr.t) list ; + constants : Constants_repr.parametric ; } val encoding: t Data_encoding.t +val constants_encoding: Constants_repr.parametric Data_encoding.t diff --git a/src/proto_alpha/lib_protocol/src/raw_context.ml b/src/proto_alpha/lib_protocol/src/raw_context.ml index bf3dbcca2..8fc42d6a8 100644 --- a/src/proto_alpha/lib_protocol/src/raw_context.ml +++ b/src/proto_alpha/lib_protocol/src/raw_context.ml @@ -136,9 +136,6 @@ let version_value = "alpha" let version = "v1" let first_level_key = [ version ; "first_level" ] let constants_key = [ version ; "constants" ] - -(* temporary hardcoded key to be removed... *) -let sandbox_param_key = [ "sandbox_parameter" ] let protocol_param_key = [ "protocol_parameters" ] let get_first_level ctxt = @@ -157,31 +154,43 @@ let set_first_level ctxt level = Context.set ctxt first_level_key bytes >>= fun ctxt -> return ctxt -type error += Failed_to_parse_sandbox_parameter of MBytes.t +type error += Failed_to_parse_parameter of MBytes.t +type error += Failed_to_decode_parameter of Data_encoding.json * string let () = register_error_kind `Temporary - ~id:"context.failed_to_parse_sandbox_parameter" - ~title: "Failed to parse sandbox parameter" + ~id:"context.failed_to_parse_parameter" + ~title: "Failed to parse parameter" ~description: - "The sandbox parameter is not a valid JSON string." + "The protocol parameters are not valid JSON." ~pp:begin fun ppf bytes -> Format.fprintf ppf - "@[Cannot parse the sandbox parameter:@ %s@]" + "@[Cannot parse the protocol parameter:@ %s@]" (MBytes.to_string bytes) end Data_encoding.(obj1 (req "contents" bytes)) - (function Failed_to_parse_sandbox_parameter data -> Some data | _ -> None) - (fun data -> Failed_to_parse_sandbox_parameter data) - -let get_sandbox_param c = - Context.get c sandbox_param_key >>= function - | None -> return None - | Some bytes -> - match Data_encoding.Binary.of_bytes Data_encoding.json bytes with - | None -> fail (Failed_to_parse_sandbox_parameter bytes) - | Some json -> return (Some json) + (function Failed_to_parse_parameter data -> Some data | _ -> None) + (fun data -> Failed_to_parse_parameter data) ; + register_error_kind + `Temporary + ~id:"context.failed_to_decode_parameter" + ~title: "Failed to decode parameter" + ~description: + "Unexpected JSON object." + ~pp:begin fun ppf (json, msg) -> + Format.fprintf ppf + "@[Cannot decode the protocol parameter:@ %s@ %a@]" + msg + Data_encoding.Json.pp json + end + Data_encoding.(obj2 + (req "contents" json) + (req "error" string)) + (function + | Failed_to_decode_parameter (json, msg) -> Some (json, msg) + | _ -> None) + (fun (json, msg) -> Failed_to_decode_parameter (json, msg)) let get_proto_param ctxt = Context.get ctxt protocol_param_key >>= function @@ -189,7 +198,7 @@ let get_proto_param ctxt = failwith "Missing protocol parameters." | Some bytes -> match Data_encoding.Binary.of_bytes Data_encoding.json bytes with - | None -> failwith "Invalid json" + | None -> fail (Failed_to_parse_parameter bytes) | Some json -> begin Context.del ctxt protocol_param_key >>= fun ctxt -> match Data_encoding.Json.destruct Parameters_repr.encoding json with @@ -204,7 +213,7 @@ let get_proto_param ctxt = let set_constants ctxt constants = let bytes = Data_encoding.Binary.to_bytes - Constants_repr.parametric_encoding constants in + Parameters_repr.constants_encoding constants in Context.set ctxt constants_key bytes let get_constants ctxt = @@ -213,7 +222,7 @@ let get_constants ctxt = failwith "Internal error: cannot read constants in context." | Some bytes -> match - Data_encoding.Binary.of_bytes Constants_repr.parametric_encoding bytes + Data_encoding.Binary.of_bytes Parameters_repr.constants_encoding bytes with | None -> failwith "Internal error: cannot parse constants in context." @@ -267,12 +276,10 @@ let prepare_first_block ~level ~timestamp ~fitness ctxt = check_first_block ctxt >>=? fun () -> Lwt.return (Raw_level_repr.of_int32 level) >>=? fun first_level -> get_proto_param ctxt >>=? fun (param, ctxt) -> - get_sandbox_param ctxt >>=? fun sandbox_param -> - Constants_repr.read_sandbox sandbox_param >>=? fun constants -> Context.set ctxt version_key (MBytes.of_string version_value) >>= fun ctxt -> set_first_level ctxt first_level >>=? fun ctxt -> - set_constants ctxt constants >>= fun ctxt -> + set_constants ctxt param.constants >>= fun ctxt -> prepare ctxt ~level ~timestamp ~fitness >>=? fun ctxt -> return (param, ctxt) diff --git a/src/proto_alpha/lib_protocol/src/raw_context.mli b/src/proto_alpha/lib_protocol/src/raw_context.mli index 9cc981436..1b6d4cfa1 100644 --- a/src/proto_alpha/lib_protocol/src/raw_context.mli +++ b/src/proto_alpha/lib_protocol/src/raw_context.mli @@ -17,7 +17,8 @@ type storage_error = | Corrupted_data of string list type error += Storage_error of storage_error -type error += Failed_to_parse_sandbox_parameter of MBytes.t +type error += Failed_to_parse_parameter of MBytes.t +type error += Failed_to_decode_parameter of Data_encoding.json * string val storage_error: storage_error -> 'a tzresult Lwt.t diff --git a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml index 2047272d0..350dae552 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/helpers_init.ml @@ -14,12 +14,7 @@ let sandbox_parameters = match Data_encoding.Json.from_string {json| { "genesis_pubkey": - "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", - "slot_durations" : [ 1, 0 ], - "cycle_length" : 4, - "block_per_roll_snapshot" : 2, - "time_before_reward" : 1, - "first_free_baking_slot" : 4 + "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2" } |json} with | Error err -> raise (Failure err) @@ -47,7 +42,11 @@ let protocol_parameters = [ "tz1iDPZLxcGf5CqCNpTuuMdtu3zKpJ6HvvFR", "btz1TaSfoSNhFoqwqbPC9iC19rN24KJtB7skD", "71300478465380003" ], [ "tz1i2kbtVu65dP739qLGRpJNujRM8pdpkH3p", "btz1USqQRuvASPXwseXkGTWeWv4dn3VwMVPEk", "283380756728119992" ], [ "tz1LKGCg6ESJLDviHkT8Jc7tUwjw4h3d9MaF", "btz1YwCKMbBLRL1qkBjAHGCwjbWDqiTAEFpbw", "1357762577679880028" ] - ] + ], + "time_between_blocks" : [ 1, 0 ], + "blocks_per_cycle" : 4, + "blocks_per_roll_snapshot" : 2, + "first_free_baking_slot" : 4 } |json} in match json_result with