From a6807b4d13b64b03b584d7ab776f9779bb32ed32 Mon Sep 17 00:00:00 2001 From: Pierre Chambart Date: Mon, 18 Jun 2018 23:46:22 +0200 Subject: [PATCH] Alpha: allow injecting a contract as bootstrap --- scripts/protocol_parameters.json | 35 ++++++++++++++- src/bin_client/test/contracts/faucet.tz | 7 +++ src/bin_client/tezos-init-sandboxed-client.sh | 45 ++++++++++++++++--- .../lib_protocol/src/bootstrap_storage.ml | 25 ++++++++--- .../lib_protocol/src/parameters_repr.ml | 30 ++++++++++--- .../lib_protocol/src/parameters_repr.mli | 1 + .../lib_protocol/test/helpers/block.ml | 2 +- 7 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 src/bin_client/test/contracts/faucet.tz diff --git a/scripts/protocol_parameters.json b/scripts/protocol_parameters.json index e561eb46e..166d29ee7 100644 --- a/scripts/protocol_parameters.json +++ b/scripts/protocol_parameters.json @@ -3,8 +3,39 @@ [ "edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9", "4000000000000" ], [ "edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV", "4000000000000" ], [ "edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU", "4000000000000" ], - [ "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n", "4000000000000" ] - ], + [ "edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n", "4000000000000" ], + [ "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav", "10000000", + { "address": "TZ1Yj9xA3jp8xy9maok89VB6HtBUtrK77tFk", + "script": + { "code": + [ { "prim": "parameter", + "args": [ { "prim": "key_hash" } ] }, + { "prim": "storage", + "args": [ { "prim": "timestamp" } ] }, + { "prim": "code", + "args": + [ [ [ [ { "prim": "DUP" }, { "prim": "CAR" }, + { "prim": "DIP", "args": [ [ { "prim": "CDR" } ] ] } ] ], + { "prim": "SWAP" }, + { "prim": "PUSH", "args": [ { "prim": "int" }, { "int": "300" } ] }, + { "prim": "ADD", "annots": [ "@FIVE_MINUTES_LATER" ] }, + { "prim": "NOW" }, + [ [ { "prim": "COMPARE" }, { "prim": "GE" } ], + { "prim": "IF", + "args": + [ [], + [ [ { "prim": "UNIT" }, + { "prim": "FAILWITH" } ] ] ] } ], + { "prim": "IMPLICIT_ACCOUNT" }, + { "prim": "PUSH", "args": [ { "prim": "mutez" }, { "int": "1000000" } ] }, + { "prim": "UNIT" }, + { "prim": "TRANSFER_TOKENS" }, + { "prim": "NIL", "args": [ { "prim": "operation" } ] }, + { "prim": "SWAP" }, + { "prim": "CONS" }, + { "prim": "DIP", "args": [ [ { "prim": "NOW" } ] ] }, + { "prim": "PAIR" } ] ] } ], + "storage": { "int": "0" } } } ] ], "dictator_pubkey": "edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2", "time_between_blocks" : [ "1", "0" ], diff --git a/src/bin_client/test/contracts/faucet.tz b/src/bin_client/test/contracts/faucet.tz new file mode 100644 index 000000000..0c92a0744 --- /dev/null +++ b/src/bin_client/test/contracts/faucet.tz @@ -0,0 +1,7 @@ +parameter key_hash ; +storage timestamp ; +code { UNPAIR ; SWAP ; + PUSH int 300 ; ADD @FIVE_MINUTES_LATER ; + NOW ; ASSERT_CMPGE ; + IMPLICIT_ACCOUNT ; PUSH mutez 1000000 ; UNIT ; TRANSFER_TOKENS ; + NIL operation ; SWAP ; CONS ; DIP { NOW } ; PAIR } \ No newline at end of file diff --git a/src/bin_client/tezos-init-sandboxed-client.sh b/src/bin_client/tezos-init-sandboxed-client.sh index 0f94d19b7..60a3e2c8b 100755 --- a/src/bin_client/tezos-init-sandboxed-client.sh +++ b/src/bin_client/tezos-init-sandboxed-client.sh @@ -32,20 +32,51 @@ init_sandboxed_client() { if ! [ -f "$parameters_file" ]; then cat > "$parameters_file" <>=? fun ctxt -> - Contract_storage.reveal_manager_key ctxt contract account.public_key >>=? fun ctxt -> - Delegate_storage.set ctxt contract (Some public_key_hash) >>=? fun ctxt -> - return ctxt +let init_account ctxt ({ public_key; amount; script }: Parameters_repr.bootstrap_account) = + let public_key_hash = Signature.Public_key.hash public_key in + match script with + | None -> + let contract = Contract_repr.implicit_contract public_key_hash in + Contract_storage.credit ctxt contract amount >>=? fun ctxt -> + Contract_storage.reveal_manager_key ctxt contract public_key >>=? fun ctxt -> + Delegate_storage.set ctxt contract (Some public_key_hash) >>=? fun ctxt -> + return ctxt + | Some (contract, script) -> + Contract_storage.originate ctxt contract + ~balance:amount + ~manager:Signature.Public_key_hash.zero + ~script:(script, None) + ~delegate:(Some public_key_hash) + ~spendable:false + ~delegatable:false >>=? fun ctxt -> + return ctxt let init ctxt ?ramp_up_cycles ?no_reward_cycles accounts = fold_left_s init_account ctxt accounts >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/src/parameters_repr.ml b/src/proto_alpha/lib_protocol/src/parameters_repr.ml index fcca1d7c4..e357edf0f 100644 --- a/src/proto_alpha/lib_protocol/src/parameters_repr.ml +++ b/src/proto_alpha/lib_protocol/src/parameters_repr.ml @@ -10,6 +10,7 @@ type bootstrap_account = { public_key : Signature.Public_key.t ; amount : Tez_repr.t ; + script : (Contract_repr.t * Script_repr.t) option ; } type t = { @@ -22,11 +23,30 @@ type t = { let bootstrap_account_encoding = let open Data_encoding in - conv - (fun { public_key ; amount } -> (public_key, amount)) - (fun (public_key, amount) -> { public_key ; amount }) - (tup2 Signature.Public_key.encoding Tez_repr.encoding) - + union + [ case (Tag 0) ~title:"Non_scripted" + (tup2 + Signature.Public_key.encoding + Tez_repr.encoding) + (function + | { public_key ; amount ; script = None } -> + Some (public_key, amount) + | { script = Some _ } -> None) + (fun (public_key, amount) -> + { public_key ; amount ; script = None }) ; + case (Tag 1) ~title:"Scripted" + (tup3 + Signature.Public_key.encoding + Tez_repr.encoding + (obj2 + (req "address" Contract_repr.encoding) + (req "script" Script_repr.encoding))) + (function + | { public_key ; amount ; script = Some script } -> + Some (public_key, amount, script) + | { script = None } -> None) + (fun (public_key, amount, script) -> + { public_key ; amount ; script = Some script }) ] (* 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 diff --git a/src/proto_alpha/lib_protocol/src/parameters_repr.mli b/src/proto_alpha/lib_protocol/src/parameters_repr.mli index 7cc092270..fe68676b3 100644 --- a/src/proto_alpha/lib_protocol/src/parameters_repr.mli +++ b/src/proto_alpha/lib_protocol/src/parameters_repr.mli @@ -10,6 +10,7 @@ type bootstrap_account = { public_key : Signature.Public_key.t ; amount : Tez_repr.t ; + script : (Contract_repr.t * Script_repr.t) option ; } type t = { diff --git a/src/proto_alpha/lib_protocol/test/helpers/block.ml b/src/proto_alpha/lib_protocol/test/helpers/block.ml index 4f9840e75..78de1cf5b 100644 --- a/src/proto_alpha/lib_protocol/test/helpers/block.ml +++ b/src/proto_alpha/lib_protocol/test/helpers/block.ml @@ -185,7 +185,7 @@ let initial_context = let bootstrap_accounts = List.map (fun (Account.{ pk = public_key ; _ }, amount) -> - Parameters_repr.{ public_key ; amount } + Parameters_repr.{ public_key ; amount; script = None } ) initial_accounts in let json =