Verified Commit ff468aac authored by Bruno B's avatar Bruno B Committed by Benjamin Canou
Browse files

Client: reorg Michelson test contracts and bash scripts (mini_scenarios, pt2)

parent 2f58e86f
parameter (bytes);
storage (unit);
code {
# Extract parameter from initial stack.
CAR @preimage;
DIP {
# Push contract constants to the stack.
#
# There's a temptation to use @storage to parametrize
# a contract but, in general, there's no reason to encumber
# @storage with immutable values.
PUSH @from (contract unit) "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"; #changeme
PUSH @to (contract unit) "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN"; #changeme
PUSH @target_hash bytes 0x123456; #changeme
PUSH @deadline timestamp "2018-08-08 00:00:00Z"; #changeme
};
# Test if the deadline has passed.
SWAP; NOW;
IFCMPLT
# In case the deadline did pass:
{
# Ignore parameter, just transfer xtz balance back to @from
DROP; DROP; DROP; BALANCE; UNIT; TRANSFER_TOKENS;
}
# In case the deadline hasn't passed yet:
{
# Test length of parameter.
DUP; SIZE;
PUSH @max_length nat 32;
IFCMPLT
{ PUSH string "preimage too long"; FAILWITH; }
{
# Test if it's a preimage of @target_hash.
SHA256 @candidate_hash;
IFCMPNEQ
{ PUSH string "invalid preimage"; FAILWITH; }
{
# Transfer xtz balance to @to.
BALANCE; UNIT; TRANSFER_TOKENS; DIP { DROP };
};
};
};
# Transform single operation into a list.
NIL operation; SWAP; CONS;
UNIT; SWAP; PAIR
}
parameter (or
# First possible action is funding, to create an xcat
(pair %fund
(address %dest)
(pair %settings (bytes %target_hash) (timestamp %deadline)))
# Other possible action is to claim the tokens (or ask a refund)
(or %claim_refund
(bytes %preimage_claim)
(bytes %refund_hash)));
storage (pair
(big_map
bytes # The target hash is used as a key
(pair
# We store in %from the person who funded the xcat
(pair %recipients (address %from) (address %dest))
(pair %settings (mutez %amount) (timestamp %deadline)))
)
unit);
code {
NIL @operations operation; SWAP;
UNPAPAIR @% @% @%; DIP {DUP};
IF_LEFT # Let's fund a new xcat!
{
# Unpack the parameters
UNPAIR @% @%;
# Assert that the destination address is of type unit.
# This costs a bit more gas but limits foot-shooting.
DUP; CONTRACT @dest unit; ASSERT_SOME; DROP;
SWAP; UNPAIR @% @%;
DIP
{
AMOUNT @amount;
SENDER;
DUP; CONTRACT @from unit; ASSERT_SOME; DROP;
DIP { PAIR; SWAP; }; PAIR; PAIR; SOME @xcat;
SWAP;
};
DUP; DIP { MEM; NOT; ASSERT }; # Assert that this target hash isn't already in the map
UPDATE; PAIR @new_storage; SWAP; PAIR;
}
{
# Let's process a claim or a refund
IF_LEFT
{ # It's a claim!
DUP; SIZE; PUSH nat 32; ASSERT_CMPGE;
SHA256 @hash; DUP; DIP {SWAP};
DIIP {
GET; ASSERT_SOME;
# Check deadline and prepare transaction.
DUP; CADR @%; CONTRACT @dest unit; ASSERT_SOME;
SWAP; CDR @%;
UNPAIR @% @%; SWAP;
# The deadline must not have passed
NOW; ASSERT_CMPLT;
# prepare transaction
UNIT; TRANSFER_TOKENS;
};
}
{ # It's a refund!
DUP;
DIP
{
GET; ASSERT_SOME;
DUP; CAAR @%; CONTRACT @from unit; ASSERT_SOME; SWAP; CDR;
UNPAIR @% @%; SWAP;
# The deadline must not HAVE passed
NOW; ASSERT_CMPGE;
UNIT; TRANSFER_TOKENS; SWAP;
};
};
# Clear the big map
NONE @none (pair (pair address address) (pair mutez timestamp));
SWAP; UPDATE @cleared_map; SWAP; DIP { PAIR; SWAP };
CONS; PAIR;
}
}
\ No newline at end of file
......@@ -70,7 +70,7 @@ bake_after $client transfer 10 from bootstrap1 to noop -arg "Unit"
bake_after $client originate contract hardlimit \
for $key1 transferring 1,000 from bootstrap1 \
running file:contracts/hardlimit.tz -init "3"
running file:contracts_mini_scenarios/hardlimit.tz -init "3"
bake_after $client transfer 10 from bootstrap1 to hardlimit -arg "Unit"
bake_after $client transfer 10 from bootstrap1 to hardlimit -arg "Unit"
......
......@@ -30,13 +30,7 @@ if [ ! $NO_TYPECHECK ] ; then
printf "All contracts are well typed\n\n"
fi
# Concatenate all strings of a list into one string
#assert_storage $contract_dir/concat_list.tz '""' '{ "a" ; "b" ; "c" }' '"abc"'
#assert_storage $contract_dir/concat_list.tz '""' '{}' '""'
#assert_storage $contract_dir/concat_list.tz \
# '""' '{ "Hello" ; " " ; "World" ; "!" }' '"Hello World!"'
# FORMAT: assert_output contract_file storage input expected_result
# Typing gas bounds checks
assert_fails $client originate contract first_explosion for bootstrap1 \
......@@ -52,75 +46,6 @@ assert_fails $client run script '{parameter (list int);storage (list (list (lis
and input '{1;2;3;4;5;6;7;8;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1}'
# Test replay prevention
init_with_transfer $contract_dir/replay.tz $key2 Unit 0 bootstrap1
assert_fails $client transfer 0 from bootstrap1 to replay
# Tests create_account
#init_with_transfer $contract_dir/create_account.tz $key2 None 1,000 bootstrap1
#assert_balance create_account "1000 ꜩ"
#created_account=\
#`$client transfer 100 from bootstrap1 to create_account -arg '(Left "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx")' \
#| grep 'New contract' \
#| sed -E 's/.*(KT1[a-zA-Z0-9]+).*/\1/' \
#| head -1`
#bake
#assert_balance $created_account "100 ꜩ"
#assert_balance create_account "1000 ꜩ"
# Creates a contract, transfers data to it and stores the data
#init_with_transfer $contract_dir/create_contract.tz $key2 Unit 1,000 bootstrap1
#assert_balance create_contract "1000 ꜩ"
#created_contract=\
#`$client transfer 0 from bootstrap1 to create_contract -arg '(Left "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx")' \
#| grep 'New contract' \
#| sed -E 's/.*(KT1[a-zA-Z0-9]+).*/\1/' \
#| head -1`
#bake
#assert_storage_contains $created_contract '"abcdefg"'
#assert_balance $created_contract "100 ꜩ"
#assert_balance create_contract "900 ꜩ"
# Test IMPLICIT_ACCOUNT
#init_with_transfer $contract_dir/default_account.tz $key1 \
# Unit 1,000 bootstrap1
#bake_after $client transfer 0 from bootstrap1 to default_account -arg "\"$BOOTSTRAP4_IDENTITY\""
#assert_balance $BOOTSTRAP4_IDENTITY "4000100 ꜩ"
#account=tz1SuakBpFdG9b4twyfrSMqZzruxhpMeSrE5
#bake_after $client transfer 0 from bootstrap1 to default_account -arg "\"$account\""
#assert_balance $account "100 ꜩ"
# Test bytes, SHA256, CHECK_SIGNATURE
init_with_transfer $contract_dir/reveal_signed_preimage.tz bootstrap1 \
'(Pair 0x9995c2ef7bcc7ae3bd15bdd9b02dc6e877c27b26732340d641a4cbc6524813bb "p2pk66uq221795tFxT7jfNmXtBMdjMf6RAaxRTwv1dbuSHbH6yfqGwz")' 1,000 bootstrap1
assert_fails $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972 "p2sigvgDSBnN1bUsfwyMvqpJA1cFhE5s5oi7SetJVQ6LJsbFrU2idPvnvwJhf5v9DhM9ZTX1euS9DgWozVw6BTHiK9VcQVpAU8")'
assert_fails $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972203f "p2sigvgDSBnN1bUsfwyMvqpJA1cFhE5s5oi7SetJVQ6LJsbFrU2idPvnvwJhf5v9DhM9ZTX1euS9DgWozVw6BTHiK9VcQVpAU8")'
assert_success $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972203f "p2sigsceCzcDw2AeYDzUonj4JT341WC9Px4wdhHBxbZcG1FhfqFVuG7f2fGCzrEHSAZgrsrQWpxduDPk9qZRgrpzwJnSHC3gZJ")'
bake
# Test SET_DELEGATE
#b2='tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN'
#b3='tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU'
#b4='tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv'
#b5='tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv'
#init_with_transfer $contract_dir/vote_for_delegate.tz bootstrap1 \
# "(Pair (Pair \"$b3\" None) (Pair \"$b4\" None))" 1,000 bootstrap1
#$client get delegate for vote_for_delegate | assert_in_output none
#assert_fails $client transfer 0 from bootstrap1 to vote_for_delegate -arg None
#assert_fails $client transfer 0 from bootstrap2 to vote_for_delegate -arg None
#bake_after $client transfer 0 from bootstrap3 to vote_for_delegate -arg "(Some \"$b5\")"
#assert_storage_contains vote_for_delegate "\"$b5\""
#$client get delegate for vote_for_delegate | assert_in_output none
#bake_after $client transfer 0 from bootstrap4 to vote_for_delegate -arg "(Some \"$b2\")"
#assert_storage_contains vote_for_delegate "\"$b2\""
#$client get delegate for vote_for_delegate | assert_in_output none
#bake_after $client transfer 0 from bootstrap4 to vote_for_delegate -arg "(Some \"$b5\")"
#$client get delegate for vote_for_delegate | assert_in_output "$b5"
# Test sets and map literals
assert_fails $client typecheck data '{ Elt 0 1 ; Elt 0 1 }' against type '(map nat nat)'
......@@ -128,27 +53,6 @@ assert_fails $client typecheck data '{ Elt 0 1 ; Elt 10 1 ; Elt 5 1 }' against t
assert_fails $client typecheck data '{ "A" ; "C" ; "B" }' against type '(set string)'
assert_fails $client typecheck data '{ "A" ; "B" ; "B" }' against type '(set string)'
# Test hash consistency between Michelson and the CLI
#hash_result=`$client hash data '(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' \
# of type '(pair mutez (pair timestamp int))' | grep Blake2b | sed 's/.*: *//'`
#assert_storage $contract_dir/hash_consistency_checker.tz '0x00' \
# '(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' "$hash_result"
#assert_storage $contract_dir/hash_consistency_checker.tz '0x00' \
# '(Pair 22220000000 (Pair "2017-12-13T04:49:00+00:00" 34))' "$hash_result"
# Test goldenbook
#init_with_transfer $contract_dir/guestbook.tz $key1\
# '{ Elt "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" None }' \
# 100 bootstrap1
#assert_fails $client transfer 0 from bootstrap2 to guestbook -arg '"Pas moi"'
#bake_after $client transfer 0 from bootstrap1 to guestbook -arg '"Coucou"'
#assert_storage_contains guestbook '{ Elt "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" (Some "Coucou") }'
#assert_fails $client transfer 0 from bootstrap3 to guestbook -arg '"Pas moi non plus"'
#assert_fails $client transfer 0 from bootstrap1 to guestbook -arg '"Recoucou ?"'
# Test for issue #262
tee /tmp/bug_262.tz <<EOF
......
......@@ -30,6 +30,15 @@ if [ ! $NO_TYPECHECK ] ; then
printf "All contracts are well typed\n\n"
fi
# FORMAT: assert_output contract_file storage input expected_result
# TODO lockup, originator, parameterized_multisig, reservoir, scrutable_reservoir,
# TODO weather_insurance, xcat_dapp, xcat
# Test replay prevention
init_with_transfer $contract_scenarios_dir/replay.tz $key2 Unit 0 bootstrap1
assert_fails $client transfer 0 from bootstrap1 to replay
# Tests create_account
init_with_transfer $contract_scenarios_dir/create_account.tz $key2 None 1,000 bootstrap1
assert_balance create_account "1000 ꜩ"
......@@ -64,6 +73,17 @@ account=tz1SuakBpFdG9b4twyfrSMqZzruxhpMeSrE5
bake_after $client transfer 0 from bootstrap1 to default_account -arg "\"$account\""
assert_balance $account "100 ꜩ"
# Test bytes, SHA256, CHECK_SIGNATURE
init_with_transfer $contract_scenarios_dir/reveal_signed_preimage.tz bootstrap1 \
'(Pair 0x9995c2ef7bcc7ae3bd15bdd9b02dc6e877c27b26732340d641a4cbc6524813bb "p2pk66uq221795tFxT7jfNmXtBMdjMf6RAaxRTwv1dbuSHbH6yfqGwz")' 1,000 bootstrap1
assert_fails $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972 "p2sigvgDSBnN1bUsfwyMvqpJA1cFhE5s5oi7SetJVQ6LJsbFrU2idPvnvwJhf5v9DhM9ZTX1euS9DgWozVw6BTHiK9VcQVpAU8")'
assert_fails $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972203f "p2sigvgDSBnN1bUsfwyMvqpJA1cFhE5s5oi7SetJVQ6LJsbFrU2idPvnvwJhf5v9DhM9ZTX1euS9DgWozVw6BTHiK9VcQVpAU8")'
assert_success $client transfer 0 from bootstrap1 to reveal_signed_preimage -arg \
'(Pair 0x050100000027566f756c657a2d766f757320636f75636865722061766563206d6f692c20636520736f6972203f "p2sigsceCzcDw2AeYDzUonj4JT341WC9Px4wdhHBxbZcG1FhfqFVuG7f2fGCzrEHSAZgrsrQWpxduDPk9qZRgrpzwJnSHC3gZJ")'
bake
# Test SET_DELEGATE
b2='tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN'
b3='tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU'
......@@ -83,3 +103,8 @@ assert_storage_contains vote_for_delegate "\"$b2\""
$client get delegate for vote_for_delegate | assert_in_output none
bake_after $client transfer 0 from bootstrap4 to vote_for_delegate -arg "(Some \"$b5\")"
$client get delegate for vote_for_delegate | assert_in_output "$b5"
printf "\nEnd of test\n"
show_logs="no"
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