From a9214f864d933bb01f01e6cbf68619f3e9887fbe Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Thu, 27 Feb 2020 19:09:14 +0100 Subject: [PATCH] Reviewing again the PascaLIGO contracts. --- src/bin/expect_tests/contract_tests.ml | 22 ++++++--- src/passes/operators/operators.ml | 48 +++++++++--------- src/test/contracts/address.ligo | 6 +-- src/test/contracts/amount.ligo | 5 +- src/test/contracts/amount.mligo | 2 +- src/test/contracts/arithmetic.ligo | 16 ++---- src/test/contracts/assign.ligo | 5 +- src/test/contracts/bad_timestamp.ligo | 2 +- src/test/contracts/balance_constant.ligo | 2 +- src/test/contracts/big_map.ligo | 6 +-- src/test/contracts/boolean_operators.ligo | 14 ++---- src/test/contracts/bytes_arithmetic.ligo | 8 ++- src/test/contracts/bytes_unpack.ligo | 12 ++--- src/test/contracts/chain_id.ligo | 2 +- src/test/contracts/check_signature.ligo | 6 +-- src/test/contracts/coase.ligo | 42 +++++++++++----- src/test/contracts/condition-annot.mligo | 2 +- src/test/contracts/condition-shadowing.mligo | 2 - src/test/contracts/condition.mligo | 2 +- src/test/contracts/counter.ligo | 4 +- src/test/contracts/crypto.ligo | 5 +- src/test/contracts/declarations.ligo | 2 - src/test/contracts/deep_access.ligo | 7 ++- src/test/contracts/dispatch-counter.ligo | 1 - src/test/contracts/entrypoints.ligo | 13 +++-- src/test/contracts/failwith.mligo | 4 +- src/test/contracts/for_fail.ligo | 2 +- src/test/contracts/get_contract.ligo | 19 ++++---- src/test/contracts/heap.ligo | 16 ++++-- src/test/contracts/implicit_account.ligo | 3 +- src/test/contracts/key_hash.ligo | 6 +-- src/test/contracts/key_hash_comparable.ligo | 11 +++-- src/test/contracts/list.ligo | 6 +-- src/test/contracts/loop_bugs.ligo | 4 +- src/test/contracts/map.ligo | 14 ++---- src/test/contracts/multisig-v2.ligo | 45 +++++++++-------- src/test/contracts/multisig.ligo | 8 +-- src/test/integration_tests.ml | 51 +++++++------------- 38 files changed, 209 insertions(+), 216 deletions(-) diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index facee666d..317c8a87a 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -7,7 +7,7 @@ let bad_contract basename = let%expect_test _ = run_ligo_good [ "measure-contract" ; contract "coase.ligo" ; "main" ] ; - [%expect {| 1747 bytes |}] ; + [%expect {| 1870 bytes |}] ; run_ligo_good [ "measure-contract" ; contract "multisig.ligo" ; "main" ] ; [%expect {| 1324 bytes |}] ; @@ -86,7 +86,9 @@ let%expect_test _ = SWAP ; DIP { DUP ; CAR ; CAR } ; GET ; - IF_NONE { PUSH string "MAP FIND" ; FAILWITH } {} ; + IF_NONE + { PUSH string "buy_single: No card pattern." ; FAILWITH } + { DUP ; DIP { DROP } } ; DUP ; CAR ; DIP { DUP ; CDR ; PUSH nat 1 ; ADD } ; @@ -159,7 +161,9 @@ let%expect_test _ = SWAP ; DIP { DUP ; CAR ; CDR } ; GET ; - IF_NONE { PUSH string "MAP FIND" ; FAILWITH } {} ; + IF_NONE + { PUSH string "sell_single: No card." ; FAILWITH } + { DUP ; DIP { DROP } } ; DUP ; CAR ; SENDER ; @@ -173,7 +177,9 @@ let%expect_test _ = CDR ; DIP { DIP 2 { DUP } ; DIG 2 ; CAR ; CAR } ; GET ; - IF_NONE { PUSH string "MAP FIND" ; FAILWITH } {} ; + IF_NONE + { PUSH string "sell_single: No card pattern." ; FAILWITH } + { DUP ; DIP { DROP } } ; DUP ; DIP { DUP } ; SWAP ; @@ -209,7 +215,9 @@ let%expect_test _ = MUL ; SENDER ; CONTRACT unit ; - IF_NONE { PUSH string "bad address for get_contract" ; FAILWITH } {} ; + IF_NONE + { PUSH string "sell_single: No contract." ; FAILWITH } + { DUP ; DIP { DROP } } ; DIP { DUP } ; SWAP ; DIP { DUP } ; @@ -246,7 +254,9 @@ let%expect_test _ = CAR ; DIP { DUP } ; GET ; - IF_NONE { PUSH string "MAP FIND" ; FAILWITH } {} ; + IF_NONE + { PUSH string "transfer_single: No card." ; FAILWITH } + { DUP ; DIP { DROP } } ; DUP ; CAR ; SENDER ; diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index 7cc7f556d..4c72934b2 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -68,30 +68,30 @@ module Simplify = struct (* Tezos module (ex-Michelson) *) | "Tezos.chain_id" -> ok C_CHAIN_ID - | "chain_id" -> ok C_CHAIN_ID (* Deprecated *) - | "get_chain_id" -> ok C_CHAIN_ID (* Deprecated *) + | "chain_id" -> ok C_CHAIN_ID (* Deprecated *) + | "get_chain_id" -> ok C_CHAIN_ID (* Deprecated *) | "Tezos.balance" -> ok C_BALANCE - | "balance" -> ok C_BALANCE (* Deprecated *) + | "balance" -> ok C_BALANCE (* Deprecated *) | "Tezos.now" -> ok C_NOW - | "now" -> ok C_NOW (* Deprecated *) + | "now" -> ok C_NOW (* Deprecated *) | "Tezos.amount" -> ok C_AMOUNT - | "amount" -> ok C_AMOUNT (* Deprecated *) + | "amount" -> ok C_AMOUNT (* Deprecated *) | "Tezos.sender" -> ok C_SENDER - | "sender" -> ok C_SENDER (* Deprecated *) + | "sender" -> ok C_SENDER (* Deprecated *) | "Tezos.address" -> ok C_ADDRESS - | "address" -> ok C_ADDRESS (* Deprecated *) + | "address" -> ok C_ADDRESS (* Deprecated *) | "Tezos.self_address" -> ok C_SELF_ADDRESS - | "self_address" -> ok C_SELF_ADDRESS (* Deprecated *) + | "self_address" -> ok C_SELF_ADDRESS (* Deprecated *) | "Tezos.implicit_account" -> ok C_IMPLICIT_ACCOUNT - | "implicit_account" -> ok C_IMPLICIT_ACCOUNT (* Deprecated *) + | "implicit_account" -> ok C_IMPLICIT_ACCOUNT (* Deprecated *) | "Tezos.source" -> ok C_SOURCE - | "source" -> ok C_SOURCE (* Deprecated *) + | "source" -> ok C_SOURCE (* Deprecated *) | "Tezos.failwith" -> ok C_FAILWITH - | "failwith" -> ok C_FAILWITH + | "failwith" -> ok C_FAILWITH - | "Tezos.transaction" -> ok C_CALL + | "Tezos.transaction" -> ok C_CALL | "transaction" -> ok C_CALL (* Deprecated *) - | "Tezos.set_delegate" -> ok C_SET_DELEGATE + | "Tezos.set_delegate" -> ok C_SET_DELEGATE | "set_delegate" -> ok C_SET_DELEGATE (* Deprecated *) | "get_contract" -> ok C_CONTRACT (* Deprecated *) | "Tezos.get_contract_opt" -> ok C_CONTRACT_OPT @@ -144,7 +144,7 @@ module Simplify = struct | "Bytes.unpack" -> ok C_BYTES_UNPACK | "bytes_unpack" -> ok C_BYTES_UNPACK (* Deprecated *) | "Bytes.length" -> ok C_SIZE - | "Bytes.size" -> ok C_SIZE + | "Bytes.size" -> ok C_SIZE (* Deprecated *) | "bytes_concat" -> ok C_CONCAT (* Deprecated *) | "Bytes.concat" -> ok C_CONCAT | "Bytes.slice" -> ok C_SLICE @@ -165,7 +165,8 @@ module Simplify = struct (* Set module *) - | "Set.size" -> ok C_SIZE + | "Set.cardinal" -> ok C_SIZE + | "Set.size" -> ok C_SIZE (* Deprecated *) | "set_size" -> ok C_SIZE (* Deprecated *) | "set_empty" -> ok C_SET_EMPTY (* Deprecated *) | "Set.mem" -> ok C_SET_MEM @@ -339,14 +340,15 @@ module Simplify = struct (* Set module *) - | "Set.mem" -> ok C_SET_MEM - | "Set.iter" -> ok C_SET_ITER - | "Set.empty" -> ok C_SET_EMPTY - | "Set.literal" -> ok C_SET_LITERAL - | "Set.add" -> ok C_SET_ADD - | "Set.remove" -> ok C_SET_REMOVE - | "Set.fold" -> ok C_SET_FOLD - | "Set.size" -> ok C_SIZE + | "Set.mem" -> ok C_SET_MEM + | "Set.iter" -> ok C_SET_ITER + | "Set.empty" -> ok C_SET_EMPTY + | "Set.literal" -> ok C_SET_LITERAL + | "Set.add" -> ok C_SET_ADD + | "Set.remove" -> ok C_SET_REMOVE + | "Set.fold" -> ok C_SET_FOLD + | "Set.size" -> ok C_SIZE (* Deprecated *) + | "Set.cardinal" -> ok C_SIZE (* Map module *) diff --git a/src/test/contracts/address.ligo b/src/test/contracts/address.ligo index 59d5bff4c..ae863ac81 100644 --- a/src/test/contracts/address.ligo +++ b/src/test/contracts/address.ligo @@ -1,5 +1,3 @@ -// function main (const c : contract (unit)) : address is address (c) - function main (const p : key_hash) : address is block { - const c : contract (unit) = implicit_account (p); -} with address (c) \ No newline at end of file + const c : contract (unit) = Tezos.implicit_account (p); +} with Tezos.address (c) diff --git a/src/test/contracts/amount.ligo b/src/test/contracts/amount.ligo index 9c0263635..c7816fae3 100644 --- a/src/test/contracts/amount.ligo +++ b/src/test/contracts/amount.ligo @@ -1,8 +1,5 @@ function check (const p : unit) : int is block { var result : int := 0; - if Tezos.amount = 100tz then - result := 42 - else - result := 0 + if amount = 100tz then result := 42 else result := 0 } with result diff --git a/src/test/contracts/amount.mligo b/src/test/contracts/amount.mligo index a2db44946..c14228fc4 100644 --- a/src/test/contracts/amount.mligo +++ b/src/test/contracts/amount.mligo @@ -1 +1 @@ -let check = if Tezos.amount > 100tez then 42 else 0 +let check_ (p : unit) : int = if Tezos.amount = 100tz then 42 else 0 diff --git a/src/test/contracts/arithmetic.ligo b/src/test/contracts/arithmetic.ligo index de3dc14ae..a4a6c51cb 100644 --- a/src/test/contracts/arithmetic.ligo +++ b/src/test/contracts/arithmetic.ligo @@ -1,15 +1,9 @@ // Test PascaLIGO arithmetic operators -function mod_op (const n : int) : nat is n mod 42 - -function plus_op (const n : int) : int is n + 42 - +function mod_op (const n : int) : nat is n mod 42 +function plus_op (const n : int) : int is n + 42 function minus_op (const n : int) : int is n - 42 - function times_op (const n : int) : int is n * 42 - -function div_op (const n : int) : int is n / 2 - -function int_op (const n : nat) : int is int (n) - -function neg_op (const n : int) : int is -n +function div_op (const n : int) : int is n / 2 +function int_op (const n : nat) : int is int (n) +function neg_op (const n : int) : int is -n diff --git a/src/test/contracts/assign.ligo b/src/test/contracts/assign.ligo index 6c6de5351..b5181e2fd 100644 --- a/src/test/contracts/assign.ligo +++ b/src/test/contracts/assign.ligo @@ -1,4 +1 @@ -function main (const i : int) : int is - block { - i := i + 1 - } with i +function main (const i : int) : int is block {i := i + 1} with i diff --git a/src/test/contracts/bad_timestamp.ligo b/src/test/contracts/bad_timestamp.ligo index 8be6968e2..dff8f6f24 100644 --- a/src/test/contracts/bad_timestamp.ligo +++ b/src/test/contracts/bad_timestamp.ligo @@ -6,4 +6,4 @@ function main (const p : parameter; const s : storage) : return is block { var stamp : timestamp := ("badtimestamp" : timestamp) } - with ((nil: list(operation)), stamp) + with ((nil : list (operation)), stamp) diff --git a/src/test/contracts/balance_constant.ligo b/src/test/contracts/balance_constant.ligo index 6a5af3fe4..b23ae23e6 100644 --- a/src/test/contracts/balance_constant.ligo +++ b/src/test/contracts/balance_constant.ligo @@ -9,4 +9,4 @@ type storage is tez type return is list (operation) * storage function main (const param : parameter; const store: storage) : return is - ((nil : list (operation)), balance) + ((nil : list (operation)), Tezos.balance) diff --git a/src/test/contracts/big_map.ligo b/src/test/contracts/big_map.ligo index 9b8364ac3..b89fb8f05 100644 --- a/src/test/contracts/big_map.ligo +++ b/src/test/contracts/big_map.ligo @@ -8,7 +8,7 @@ function main (const p : parameter; const s : storage) : return is toto := s.0[23]; s.0[2] := 444 } - with ((nil: list(operation)), s) + with ((nil : list (operation)), s) type foo is big_map (int, int) @@ -16,14 +16,12 @@ function set_ (var n : int; var m : foo) : foo is block { m[23] := n } with m -function add (var n : int ; var m : foo) : foo is set_(n,m) +function add (var n : int ; var m : foo) : foo is set_ (n,m) function rm (var m : foo) : foo is block { remove 42 from map m } with m -function gf (const m : foo) : int is get_force (23, m) - function get (const m : foo) : option (int) is m[42] const empty_big_map : big_map (int,int) = big_map [] diff --git a/src/test/contracts/boolean_operators.ligo b/src/test/contracts/boolean_operators.ligo index 122894d25..d19e5ad72 100644 --- a/src/test/contracts/boolean_operators.ligo +++ b/src/test/contracts/boolean_operators.ligo @@ -1,11 +1,5 @@ -// Test PascaLIGO boolean operators - -function or_true (const b : bool) : bool is b or True - -function or_false (const b : bool) : bool is b or False - -function and_true (const b : bool) : bool is b and True - +function or_true (const b : bool) : bool is b or True +function or_false (const b : bool) : bool is b or False +function and_true (const b : bool) : bool is b and True function and_false (const b : bool) : bool is b and False - -function not_bool (const b : bool) : bool is not b +function not_bool (const b : bool) : bool is not b diff --git a/src/test/contracts/bytes_arithmetic.ligo b/src/test/contracts/bytes_arithmetic.ligo index 6d39dd4c9..820cf6fdc 100644 --- a/src/test/contracts/bytes_arithmetic.ligo +++ b/src/test/contracts/bytes_arithmetic.ligo @@ -1,5 +1,3 @@ -function concat_op (const s : bytes) : bytes is bytes_concat (s, 0x7070) - -function slice_op (const s : bytes) : bytes is bytes_slice (1n, 2n, s) - -function hasherman (const s : bytes) : bytes is sha_256 (s) +function concat_op (const s : bytes) : bytes is Bytes.concat (s, 0x7070) +function slice_op (const s : bytes) : bytes is Bytes.sub (1n, 2n, s) +function hasherman (const s : bytes) : bytes is Crypto.sha256 (s) diff --git a/src/test/contracts/bytes_unpack.ligo b/src/test/contracts/bytes_unpack.ligo index e57ace2ab..feb76f806 100644 --- a/src/test/contracts/bytes_unpack.ligo +++ b/src/test/contracts/bytes_unpack.ligo @@ -1,11 +1,11 @@ function id_string (const p : string) : option (string) is block { - const packed : bytes = bytes_pack (p) -} with (bytes_unpack (packed) : option (string)) + const packed : bytes = Bytes.pack (p) +} with (Bytes.unpack (packed) : option (string)) function id_int (const p : int) : option (int) is block { - const packed : bytes = bytes_pack (p) -} with (bytes_unpack (packed) : option (int)) + const packed : bytes = Bytes.pack (p) +} with (Bytes.unpack (packed) : option (int)) function id_address (const p : address) : option (address) is block { - const packed : bytes = bytes_pack (p) -} with (bytes_unpack (packed) : option (address)) + const packed : bytes = Bytes.pack (p) +} with (Bytes.unpack (packed) : option (address)) diff --git a/src/test/contracts/chain_id.ligo b/src/test/contracts/chain_id.ligo index 353ba4a45..ad9f17395 100644 --- a/src/test/contracts/chain_id.ligo +++ b/src/test/contracts/chain_id.ligo @@ -1 +1 @@ -function chain_id (const tt : chain_id) : chain_id is get_chain_id +function chain_id (const tt : chain_id) : chain_id is Tezos.chain_id diff --git a/src/test/contracts/check_signature.ligo b/src/test/contracts/check_signature.ligo index 97de04bd2..83554433e 100644 --- a/src/test/contracts/check_signature.ligo +++ b/src/test/contracts/check_signature.ligo @@ -1,4 +1,4 @@ -function check_signature (const pk : key; +function check_signature (const pk : key; const signed : signature; - const msg: bytes) : bool -is crypto_check (pk, signed, msg) + const msg : bytes) : bool +is Crypto.check (pk, signed, msg) diff --git a/src/test/contracts/coase.ligo b/src/test/contracts/coase.ligo index 5f5b00644..5897b8686 100644 --- a/src/test/contracts/coase.ligo +++ b/src/test/contracts/coase.ligo @@ -45,27 +45,38 @@ type parameter is | Transfer_single of action_transfer_single function transfer_single (const action : action_transfer_single; - const s : storage) : return is block { + const s : storage) : return is + block { const cards : cards = s.cards; - const card : card = get_force (action.card_to_transfer, cards); + const card : card = + case cards[action.card_to_transfer] of + Some (card) -> card + | None -> (failwith ("transfer_single: No card.") : card) + end; if card.card_owner =/= sender then failwith ("This card doesn't belong to you") else skip; card.card_owner := action.destination; cards[action.card_to_transfer] := card; - s.cards := cards; - const operations : list (operation) = nil - } with (operations, s) + s.cards := cards + } with ((nil : list (operation)), s) function sell_single (const action : action_sell_single; const s : storage) : return is block { - const card : card = get_force (action.card_to_sell, s.cards); + const card : card = + case s.cards[action.card_to_sell] of + Some (card) -> card + | None -> (failwith ("sell_single: No card.") : card) + end; if card.card_owner =/= sender then failwith ("This card doesn't belong to you") else skip; const card_pattern : card_pattern = - get_force (card.card_pattern, s.card_patterns); + case s.card_patterns[card.card_pattern] of + Some (pattern) -> pattern + | None -> (failwith ("sell_single: No card pattern.") : card_pattern) + end; card_pattern.quantity := abs (card_pattern.quantity - 1n); const card_patterns : card_patterns = s.card_patterns; card_patterns[card.card_pattern] := card_pattern; @@ -74,8 +85,12 @@ function sell_single (const action : action_sell_single; remove action.card_to_sell from map cards; s.cards := cards; const price : tez = card_pattern.coefficient * card_pattern.quantity; - const receiver : contract (unit) = get_contract (sender); - const op : operation = transaction (unit, price, receiver); + const receiver : contract (unit) = + case (Tezos.get_contract_opt (Tezos.sender) : option (contract (unit))) of + Some (contract) -> contract + | None -> (failwith ("sell_single: No contract.") : contract (unit)) + end; + const op : operation = Tezos.transaction (unit, price, receiver); const operations : list (operation) = list [op] } with (operations, s) @@ -84,12 +99,13 @@ function buy_single (const action : action_buy_single; block { // Check funds const card_pattern : card_pattern = - get_force (action.card_to_buy, s.card_patterns); + case s.card_patterns[action.card_to_buy] of + Some (pattern) -> pattern + | None -> (failwith ("buy_single: No card pattern.") : card_pattern) + end; const price : tez = card_pattern.coefficient * (card_pattern.quantity + 1n); if price > amount then failwith ("Not enough money") else skip; - // Administrative procedure - const operations : list(operation) = nil; // Increase quantity card_pattern.quantity := card_pattern.quantity + 1n; const card_patterns : card_patterns = s.card_patterns; @@ -103,7 +119,7 @@ function buy_single (const action : action_buy_single; ]; s.cards := cards; s.next_id := s.next_id + 1n - } with (operations, s) + } with ((nil : list (operation)), s) function main (const action : parameter; const s : storage) : return is case action of diff --git a/src/test/contracts/condition-annot.mligo b/src/test/contracts/condition-annot.mligo index 93aece587..1eb1bf0f9 100644 --- a/src/test/contracts/condition-annot.mligo +++ b/src/test/contracts/condition-annot.mligo @@ -1,4 +1,4 @@ -type integer is int +type integer = int let main (i : int) = if (i = 2 : bool) then (42 : int) else (0 : integer) diff --git a/src/test/contracts/condition-shadowing.mligo b/src/test/contracts/condition-shadowing.mligo index 97ee6eb51..64f1ff42f 100644 --- a/src/test/contracts/condition-shadowing.mligo +++ b/src/test/contracts/condition-shadowing.mligo @@ -1,5 +1,3 @@ -(* TODO : make a test using mutation, not shadowing *) - let main (i : int) = let result = 0 in if i = 2 then diff --git a/src/test/contracts/condition.mligo b/src/test/contracts/condition.mligo index 31568e353..4807f36c1 100644 --- a/src/test/contracts/condition.mligo +++ b/src/test/contracts/condition.mligo @@ -1,3 +1,3 @@ // Test conditional in CameLIGO -let main (i: int) = if i = 2 then 42 else 0 +let main (i : int) = if i = 2 then 42 else 0 diff --git a/src/test/contracts/counter.ligo b/src/test/contracts/counter.ligo index 295c540ee..70bd7ed30 100644 --- a/src/test/contracts/counter.ligo +++ b/src/test/contracts/counter.ligo @@ -1,7 +1,7 @@ type t is int -function main (const p : int ; const s : t) : list (operation) * int is +function main (const p : int; const s : t) : list (operation) * int is block { skip } // skip is a do nothing instruction, needed for empty blocks - with ((nil : list(operation)), p + s) + with ((nil : list (operation)), p+s) diff --git a/src/test/contracts/crypto.ligo b/src/test/contracts/crypto.ligo index 08d417241..5c963fe6e 100644 --- a/src/test/contracts/crypto.ligo +++ b/src/test/contracts/crypto.ligo @@ -1,3 +1,2 @@ -function hasherman512 (const s: bytes) : bytes is sha_512 (s) - -function hasherman_blake (const s: bytes) : bytes is blake2b (s) +function hasherman512 (const s : bytes) : bytes is Crypto.sha512 (s) +function hasherman_blake (const s : bytes) : bytes is Crypto.blake2b (s) diff --git a/src/test/contracts/declarations.ligo b/src/test/contracts/declarations.ligo index d6cc5dfb5..b41c36819 100644 --- a/src/test/contracts/declarations.ligo +++ b/src/test/contracts/declarations.ligo @@ -1,5 +1,3 @@ -// Test PascaLIGO top-level declarations - const foo : int = 42 function main (const i : int) : int is i + foo diff --git a/src/test/contracts/deep_access.ligo b/src/test/contracts/deep_access.ligo index 2d9ad32d4..4c4159111 100644 --- a/src/test/contracts/deep_access.ligo +++ b/src/test/contracts/deep_access.ligo @@ -21,9 +21,12 @@ function asymetric_tuple_access (const foo : unit) : int is } with tuple.0 + tuple.1.0 + tuple.1.1.0 + tuple.1.1.1 type nested_record_t is - record [nesty : record [mymap : map (int,string)]] + record [nesty : record [mymap : map (int, string)]] function nested_record (var nee : nested_record_t) : string is block { nee.nesty.mymap[1] := "one" - } with get_force (1, nee.nesty.mymap) + } with case nee.nesty.mymap[1] of + Some (s) -> s + | None -> (failwith ("Should not happen.") : string) + end diff --git a/src/test/contracts/dispatch-counter.ligo b/src/test/contracts/dispatch-counter.ligo index c513381ae..0ef19ab75 100644 --- a/src/test/contracts/dispatch-counter.ligo +++ b/src/test/contracts/dispatch-counter.ligo @@ -7,7 +7,6 @@ type storage is int type return is list (operation) * storage function increment (const i : int; const n : int) : int is i+n - function decrement (const i : int; const n : int) : int is i-n const nop : list (operation) = nil diff --git a/src/test/contracts/entrypoints.ligo b/src/test/contracts/entrypoints.ligo index d1945ac8d..d47a8b78a 100644 --- a/src/test/contracts/entrypoints.ligo +++ b/src/test/contracts/entrypoints.ligo @@ -4,9 +4,12 @@ type return is list (operation) * storage function cb (const a : address; const s : storage) : return is block { - const c : contract (unit) = get_entrypoint ("%cb", a) - } - with (list [transaction (unit, 0mutez, c)], s) + const c : contract (unit) = + case (Tezos.get_entrypoint_opt ("%cb", a) : option (contract (unit))) of + Some (contract) -> contract + | None -> (failwith ("cb: Entrypoint not found.") : contract (unit)) + end + } with (list [Tezos.transaction (unit, 0tez, c)], s) function cbo (const a : address; const s : storage) : return is @@ -14,6 +17,6 @@ function cbo (const a : address; const s : storage) : return is const c : contract (unit) = case (get_entrypoint_opt ("%cbo", a) : option (contract (unit))) of Some (c) -> c - | None -> (failwith ("entrypoint not found") : contract (unit)) + | None -> (failwith ("cbo: Entrypoint not found.") : contract (unit)) end - } with (list [transaction(unit, 0mutez, c)], s) + } with (list [Tezos.transaction (unit, 0tez, c)], s) diff --git a/src/test/contracts/failwith.mligo b/src/test/contracts/failwith.mligo index 2938a703d..9d82ed6e3 100644 --- a/src/test/contracts/failwith.mligo +++ b/src/test/contracts/failwith.mligo @@ -1,4 +1,4 @@ type storage = unit -let main (p : unit; store : storage) : operation list * storage = - if true then failwith "This contract always fails" +let main (p, store : unit * storage) : operation list * storage = + failwith "This contract always fails" diff --git a/src/test/contracts/for_fail.ligo b/src/test/contracts/for_fail.ligo index a8c870843..682c5e295 100644 --- a/src/test/contracts/for_fail.ligo +++ b/src/test/contracts/for_fail.ligo @@ -1,2 +1,2 @@ -function main (const a: int) : int is +function main (const a : int) : int is block { for i := 0 to 100 block { skip } } with i diff --git a/src/test/contracts/get_contract.ligo b/src/test/contracts/get_contract.ligo index 8ace2277d..5a7ca3541 100644 --- a/src/test/contracts/get_contract.ligo +++ b/src/test/contracts/get_contract.ligo @@ -1,19 +1,20 @@ type storage is unit type return is list (operation) * storage -function cb (const s : storage) : return is - block { - const c : contract(unit) = get_contract(sender) - } - with (list [transaction(unit, 0mutez, c)], s) +function cb (const s : storage) : return is block { + const c : contract (unit) = + case (Tezos.get_contract_opt (Tezos.sender) : option (contract (unit))) of + Some (contract) -> contract + | None -> (failwith ("cb: No contract.") : contract (unit)) + end +} with (list [Tezos.transaction (unit, 0tez, c)], s) function cbo (const s : unit) : return is block { const c : contract (unit) = - case (get_contract_opt(sender) : option(contract(unit))) of - Some (c) -> c + case (Tezos.get_contract_opt (Tezos.sender) : option (contract (unit))) of + Some (contract) -> contract | None -> (failwith ("contract not found") : contract (unit)) end - } - with (list [transaction(unit, 0mutez, c)], s) + } with (list [Tezos.transaction (unit, 0tez, c)], s) diff --git a/src/test/contracts/heap.ligo b/src/test/contracts/heap.ligo index 66c7747b6..9ff8f8154 100644 --- a/src/test/contracts/heap.ligo +++ b/src/test/contracts/heap.ligo @@ -10,8 +10,12 @@ function get_top (const h : heap) : heap_elt is get_force (1n, h) function pop_switch (const h : heap) : heap is block { const result : heap_elt = get_top (h); - const s : nat = size (h); - const last : heap_elt = get_force (s, h); + const s : nat = Map.size (h); + const last : heap_elt = + case h[s] of + Some (e) -> e + | None -> (failwith ("No element.") : heap_elt) + end; remove 1n from map h; h[1n] := last } with h @@ -19,8 +23,12 @@ function pop_switch (const h : heap) : heap is function pop_ (const h : heap) : nat is block { const result : heap_elt = get_top (h); - const s : nat = size (h); - var current : heap_elt := get_force (s, h); + const s : nat = Map.size (h); + var current : heap_elt := + case h[s] of + Some (e) -> e + | None -> (failwith ("No element.") : heap_elt) + end; const i : nat = 1n; const left : nat = 2n * i; const right : nat = left + 1n; diff --git a/src/test/contracts/implicit_account.ligo b/src/test/contracts/implicit_account.ligo index ad0a6c54b..c1f8b3649 100644 --- a/src/test/contracts/implicit_account.ligo +++ b/src/test/contracts/implicit_account.ligo @@ -1 +1,2 @@ -function main (const kh: key_hash) : contract (unit) is implicit_account (kh) +function main (const kh: key_hash) : contract (unit) is + Tezos.implicit_account (kh) diff --git a/src/test/contracts/key_hash.ligo b/src/test/contracts/key_hash.ligo index 865abfbb8..41c7fe56b 100644 --- a/src/test/contracts/key_hash.ligo +++ b/src/test/contracts/key_hash.ligo @@ -1,7 +1,5 @@ function check_hash_key (const kh1 : key_hash; const k2 : key) : bool * key_hash is block { - var ret : bool := False; - var kh2 : key_hash := crypto_hash_key (k2); - if kh1 = kh2 then ret := True else skip - } with (ret, kh2) + var kh2 : key_hash := Crypto.hash_key (k2); + } with ((kh1 = kh2), kh2) diff --git a/src/test/contracts/key_hash_comparable.ligo b/src/test/contracts/key_hash_comparable.ligo index 02003b927..7c5a60613 100644 --- a/src/test/contracts/key_hash_comparable.ligo +++ b/src/test/contracts/key_hash_comparable.ligo @@ -1,8 +1,9 @@ -type storage is record - one: map(key_hash, nat); - two: big_map(key_hash, bool); -end +type storage is record [ + one : map (key_hash, nat); + two : big_map (key_hash, bool) +] type return is list (operation) * storage -function main (const a : int; const store : storage) : return is ((nil: list(operation)), store) \ No newline at end of file +function main (const a : int; const store : storage) : return is + ((nil : list (operation)), store) diff --git a/src/test/contracts/list.ligo b/src/test/contracts/list.ligo index 155b80ac5..ae01f1559 100644 --- a/src/test/contracts/list.ligo +++ b/src/test/contracts/list.ligo @@ -17,17 +17,17 @@ const bl : foobar = list [144; 51; 42; 120; 421] function fold_op (const s : list (int)) : int is block { function aggregate (const prec: int; const cur: int) : int is prec+cur - } with list_fold (aggregate, s, 10) + } with List.fold (aggregate, s, 10) function iter_op (const s : list (int)) : int is block { var r : int := 0; function aggregate (const i : int) : unit is block { r := r + i } with unit; - list_iter (aggregate, s) + List.iter (aggregate, s) } with r function map_op (const s : list (int)) : list (int) is block { function increment (const i : int) : int is i+1 - } with list_map (increment, s) + } with List.map (increment, s) diff --git a/src/test/contracts/loop_bugs.ligo b/src/test/contracts/loop_bugs.ligo index 1a18a6758..55b443352 100644 --- a/src/test/contracts/loop_bugs.ligo +++ b/src/test/contracts/loop_bugs.ligo @@ -1,6 +1,6 @@ function shadowing_in_body (var nee : unit) : string is block { var st : string := ""; - var list1 : list(string) := list "to"; "to" end; + var list1 : list (string) := list ["to"; "to"]; for x in list list1 block { const x : string = "ta"; st := st ^ x; @@ -10,7 +10,7 @@ function shadowing_in_body (var nee : unit) : string is block { function shadowing_assigned_in_body (var nee : unit) : string is block { var st : string := ""; - var list1 : list(string) := list "to"; "to" end; + var list1 : list (string) := list ["to"; "to"]; for x in list list1 block { st := st ^ x; var st : string := "ta"; diff --git a/src/test/contracts/map.ligo b/src/test/contracts/map.ligo index eabd8edc7..51339ada4 100644 --- a/src/test/contracts/map.ligo +++ b/src/test/contracts/map.ligo @@ -30,32 +30,28 @@ function patch_ (var m : foobar) : foobar is block { function patch_deep (var m : foobar * nat) : foobar * nat is block { patch m.0 with map [1 -> 9] } with m -function size_ (const m : foobar) : nat is size (m) - -function gf (const m : foobar) : int is get_force (23, m) +function size_ (const m : foobar) : nat is Map.size (m) function get (const m : foobar) : option (int) is m[42] -function get_ (const m : foobar) : option (int) is map_get (42, m) - -function mem (const k: int; const m: foobar) : bool is map_mem (k, m) +function mem (const k: int; const m: foobar) : bool is Map.mem (k, m) function iter_op (const m : foobar) : unit is block { function aggregate (const i : int; const j : int) : unit is block { if i=j then skip else failwith ("fail") } with unit - } with map_iter (aggregate, m) + } with Map.iter (aggregate, m) function map_op (const m : foobar) : foobar is block { function increment (const i : int; const j : int) : int is j+1 - } with map_map (increment, m) + } with Map.map (increment, m) function fold_op (const m : foobar) : int is block { function aggregate (const i : int; const j : int * int) : int is i + j.0 + j.1 - } with map_fold(aggregate, m, 10) + } with Map.fold (aggregate, m, 10) function deep_op (var m : foobar) : foobar is block { diff --git a/src/test/contracts/multisig-v2.ligo b/src/test/contracts/multisig-v2.ligo index cb3292d18..002379827 100644 --- a/src/test/contracts/multisig-v2.ligo +++ b/src/test/contracts/multisig-v2.ligo @@ -37,22 +37,22 @@ function send (const param : send_pt; const s : storage) : return is block { // check sender against the authorized addresses - if not set_mem (sender, s.authorized_addresses) + if not Set.mem (Tezos.sender, s.authorized_addresses) then failwith("Unauthorized address") else skip; // check message size against the stored limit var message : message := param; - const packed_msg : bytes = bytes_pack (message); - if size (packed_msg) > s.max_message_size + const packed_msg : bytes = Bytes.pack (message); + if Bytes.length (packed_msg) > s.max_message_size then failwith ("Message size exceed maximum limit") else skip; (* compute the new set of addresses associated with the message and update counters *) - var new_store : addr_set := set_empty; + var new_store : addr_set := set []; case map_get (packed_msg, s.message_store) of Some (voters) -> @@ -60,26 +60,25 @@ function send (const param : send_pt; const s : storage) : return is (* The message is already stored. Increment the counter only if the sender is not already associated with the message. *) - if set_mem (sender, voters) + if Set.mem (Tezos.sender, voters) then skip - else s.proposal_counters[sender] := - get_force (sender, s.proposal_counters) + 1n; - - new_store := set_add(sender,voters) + else s.proposal_counters[Tezos.sender] := + get_force (Tezos.sender, s.proposal_counters) + 1n; + new_store := Set.add (Tezos.sender,voters) } | None -> block { // the message has never been received before s.proposal_counters[sender] := - get_force (sender, s.proposal_counters) + 1n; - new_store := set [sender] + get_force (Tezos.sender, s.proposal_counters) + 1n; + new_store := set [Tezos.sender] } end; // check sender counters against the maximum number of proposal var sender_proposal_counter : nat := - get_force (sender, s.proposal_counters); + get_force (Tezos.sender, s.proposal_counters); if sender_proposal_counter > s.max_proposal then failwith ("Maximum number of proposal reached") @@ -89,14 +88,14 @@ function send (const param : send_pt; const s : storage) : return is var ret_ops : list (operation) := nil; - if size (new_store) >= s.threshold then { + if Set.cardinal (new_store) >= s.threshold then { remove packed_msg from map s.message_store; ret_ops := message (s.state_hash); // update the state hash - s.state_hash := sha_256 (bytes_concat (s.state_hash, packed_msg)); + s.state_hash := Crypto.sha256 (Bytes.concat (s.state_hash, packed_msg)); // decrement the counters for addr -> ctr in map s.proposal_counters block { - if set_mem(addr,new_store) then + if Set.mem (addr, new_store) then s.proposal_counters[addr] := abs (ctr - 1n) else skip } @@ -106,26 +105,26 @@ function send (const param : send_pt; const s : storage) : return is function withdraw (const param : withdraw_pt; const s : storage) : return is block { var message : message := param; - const packed_msg : bytes = bytes_pack (message); + const packed_msg : bytes = Bytes.pack (message); - case map_get(packed_msg, s.message_store) of + case s.message_store[packed_msg] of Some (voters) -> block { // The message is stored - const new_set : addr_set = set_remove (sender, voters); + const new_set : addr_set = Set.remove (Tezos.sender, voters); (* Decrement the counter only if the sender was already associated with the message *) - if size (voters) =/= size (new_set) - then s.proposal_counters[sender] := - abs (get_force (sender, s.proposal_counters) - 1n) - else skip ; + if Set.cardinal (voters) =/= Set.cardinal (new_set) + then s.proposal_counters[Tezos.sender] := + abs (get_force (Tezos.sender, s.proposal_counters) - 1n) + else skip; (* If the message is left without any associated addresses, remove the corresponding message_store field *) - if size (new_set) = 0n + if Set.cardinal (new_set) = 0n then remove packed_msg from map s.message_store else s.message_store[packed_msg] := new_set } diff --git a/src/test/contracts/multisig.ligo b/src/test/contracts/multisig.ligo index f0cdb636c..19c8fc3c4 100644 --- a/src/test/contracts/multisig.ligo +++ b/src/test/contracts/multisig.ligo @@ -38,7 +38,7 @@ function check_message (const param : check_message_pt; failwith ("Counters does not match") else { const packed_payload : bytes = - bytes_pack ((message, param.counter, s.id, get_chain_id)); + Bytes.pack ((message, param.counter, s.id, Tezos.chain_id)); var valid : nat := 0n; var keys : authorized_keys := s.auth; @@ -47,12 +47,12 @@ function check_message (const param : check_message_pt; nil -> skip | key # tl -> block { keys := tl; - if pkh_sig.0 = crypto_hash_key (key) then - if crypto_check (key, pkh_sig.1, packed_payload) + if pkh_sig.0 = Crypto.hash_key (key) then + if Crypto.check (key, pkh_sig.1, packed_payload) then valid := valid + 1n else failwith ("Invalid signature") else skip - } + } end }; diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index b8270e005..2ad0dfc8f 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1019,21 +1019,11 @@ let map_ type_f path : unit result = let make_expected = e_nat in expect_eq_n_strict_pos_small program "size_" make_input make_expected in - let%bind () = - let make_input = fun n -> ez [(23, n) ; (42, 4)] in - let make_expected = e_int in - expect_eq_n program "gf" make_input make_expected - in let%bind () = let make_input = fun n -> ez [(23, n) ; (42, 4)] in let make_expected = fun _ -> e_some @@ e_int 4 in expect_eq_n program "get" make_input make_expected in - let%bind () = - let make_input = fun n -> ez [(23, n) ; (42, 4)] in - let make_expected = fun _ -> e_some @@ e_int 4 in - expect_eq_n program "get_" make_input make_expected - in let%bind () = let input_map = ez [(23, 10) ; (42, 4)] in expect_eq program "mem" (e_tuple [(e_int 23) ; input_map]) (e_bool true) @@ -1081,11 +1071,6 @@ let big_map_ type_f path : unit result = let lst' = List.map (fun (x, y) -> e_int x, e_int y) lst in (e_typed_big_map lst' t_int t_int) in - let%bind () = - let make_input = fun n -> ez [(23, n) ; (42, 4)] in - let make_expected = e_int in - expect_eq_n program "gf" make_input make_expected - in let%bind () = let make_input = fun n -> let m = ez [(23 , 0) ; (42 , 0)] in @@ -2241,51 +2226,51 @@ let empty_case_religo () : unit result = in ok () -let tuple_type_mligo () : unit result = +let tuple_type_mligo () : unit result = let%bind program = mtype_file "./contracts/tuple_type.mligo" in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 8 in expect_eq_n program "test1" input expected in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 12 in expect_eq_n program "test2" input expected in ok () -let tuple_type_religo () : unit result = +let tuple_type_religo () : unit result = let%bind program = retype_file "./contracts/tuple_type.religo" in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 8 in expect_eq_n program "arguments_test" input expected in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 8 in expect_eq_n program "tuple_test" input expected in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 8 in expect_eq_n program "arguments_test_inline" input expected in - let%bind () = - let input _ = e_int 0 in + let%bind () = + let input _ = e_int 0 in let expected _ = e_int 8 in expect_eq_n program "tuple_test_inline" input expected in ok () -let no_semicolon_religo () : unit result = +let no_semicolon_religo () : unit result = let%bind program = retype_file "./contracts/no_semicolon.religo" in - let%bind () = - let input _ = e_int 2 in + let%bind () = + let input _ = e_int 2 in let expected _ = e_int 3 in expect_eq_n program "a" input expected - in + in ok () let loop_bugs_ligo () : unit result =