Skip to content

Commit

Permalink
Contracts may have balance = 0 && nonce = 0, so check for _this_addre…
Browse files Browse the repository at this point in the history
…ss (#981)

* Contracts may have balance = 0 && nonce = 0, so check for _this_address

* fmt
  • Loading branch information
jjcnn authored Apr 20, 2021
1 parent dfb9551 commit 5615cc2
Show file tree
Hide file tree
Showing 12 changed files with 652 additions and 36 deletions.
80 changes: 47 additions & 33 deletions src/eval/EvalUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,17 @@ module EvalTypecheck = struct
open MonadUtil
open Result.Let_syntax
let typecheck_remote_field_types ~caddr fts_opt =
let open EvalType in
(* First check that the address is in use: balance > 0 || nonce > 0 *)
(* Checks that _this_address is defined *)
let is_contract_addr ~caddr =
let this_id = EvalIdentifier.mk_loc_id this_address_label in
let%bind _, this_typ_opt =
StateService.external_fetch ~caddr ~fname:this_id ~keys:[] ~ignoreval:true
in
pure @@ Option.is_some this_typ_opt
(* Checks that balance > 0 || nonce > 0 *)
let is_user_addr ~caddr =
(* First check if the address is a user address with balance > 0 || nonce > 0 *)
let balance_id = EvalIdentifier.mk_loc_id balance_label in
let nonce_id = EvalIdentifier.mk_loc_id nonce_label in
let%bind balance_lit, _ =
Expand All @@ -513,34 +521,40 @@ module EvalTypecheck = struct
match (balance_lit, nonce_lit) with
| Some (UintLit (Uint128L balance)), Some (UintLit (Uint64L nonce))
when Uint128.compare balance Uint128.zero > 0
|| Uint64.compare nonce Uint64.zero > 0 -> (
match fts_opt with
| None ->
(* Non-contract address - all fields checked *)
pure true
| Some fts ->
let this_id = EvalIdentifier.mk_loc_id this_address_label in
let%bind _, this_typ_opt =
StateService.external_fetch ~caddr ~fname:this_id ~keys:[]
~ignoreval:true
in
if Option.is_none this_typ_opt then
fail0
@@ sprintf "No contract found at address %s"
(EvalLiteral.Bystrx.hex_encoding caddr)
else
(* Check that all fields are defined at caddr, and that their types are assignable to what is expected *)
allM fts ~f:(fun (f, t) ->
let%bind res =
StateService.external_fetch ~caddr ~fname:f ~keys:[]
~ignoreval:true
in
match res with
| _, Some ext_typ ->
pure @@ type_assignable ~expected:t ~actual:ext_typ
| _, None -> pure false))
| _ ->
fail0
@@ sprintf "Address %s not in use."
(EvalLiteral.Bystrx.hex_encoding caddr)
|| Uint64.compare nonce Uint64.zero > 0 ->
pure true
| _ -> pure false
let typecheck_remote_field_types ~caddr fts_opt =
let open EvalType in
match fts_opt with
| None ->
(* True if the address is in use, false otherwise *)
let%bind user_addr = is_user_addr ~caddr in
if not user_addr then
let%bind contract_addr = is_contract_addr ~caddr in
if not contract_addr then
fail0
@@ sprintf "Address %s not in use."
(EvalLiteral.Bystrx.hex_encoding caddr)
else pure true
else pure true
| Some fts ->
(* True if the address contains a contract with the appropriate fields, false otherwise *)
let%bind contract_addr = is_contract_addr ~caddr in
if not contract_addr then
fail0
@@ sprintf "No contract found at address %s"
(EvalLiteral.Bystrx.hex_encoding caddr)
else
(* Check that all fields are defined at caddr, and that their types are assignable to what is expected *)
allM fts ~f:(fun (f, t) ->
let%bind res =
StateService.external_fetch ~caddr ~fname:f ~keys:[]
~ignoreval:true
in
match res with
| _, Some ext_typ ->
pure @@ type_assignable ~expected:t ~actual:ext_typ
| _, None -> pure false)
end
2 changes: 1 addition & 1 deletion tests/runner/Testcontracts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ let contract_tests env =
"init_address_type" ~is_library:false ~ipc_mode:true;
"remote_state_reads"
>::: build_contract_tests env "remote_state_reads" fail_code 101
129 [];
131 [];
"map_as_cparam"
>: build_contract_init_test env fail_code "map_as_cparam"
"init_illegal_key" ~is_library:false ~ipc_mode:true;
Expand Down
1 change: 1 addition & 0 deletions tests/runner/remote_state_reads/blockchain_130.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "vname": "BLOCKNUMBER", "type": "BNum", "value": "100" } ]
1 change: 1 addition & 0 deletions tests/runner/remote_state_reads/blockchain_131.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ { "vname": "BLOCKNUMBER", "type": "BNum", "value": "100" } ]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"errors": [
{
"error_message":
"Address 0x1234567890123456789012345678901234567890 not in use.",
"StateIPCClient: Error in IPC access: (code:0, message:Fetching state value failed).",
"start_location": { "file": "", "line": 0, "column": 0 },
"end_location": { "file": "", "line": 0, "column": 0 }
}
Expand Down
108 changes: 108 additions & 0 deletions tests/runner/remote_state_reads/message_130.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"_tag": "RemoteReadsADTTest",
"_amount": "0",
"_sender": "0xabfeccdc9012345678901234567890f777564322",
"params": [
{
"vname" : "list1",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564324",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564325",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
]
}
]
}
},
{
"vname" : "list2",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
},
{
"vname" : "list3",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564324",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
]
}
},
{
"vname" : "pair1",
"type" : "Pair ByStr20 0x3620c286757a29985cee194eb90064270fb65414.AddressADT",
"value" : {
"constructor" : "Pair",
"argtypes" : [ "ByStr20", "0x3620c286757a29985cee194eb90064270fb65414.AddressADT" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "0x3620c286757a29985cee194eb90064270fb65414.Address2",
"argtypes" : [],
"arguments" : [ "0xabfeccdc9012345678901234567890f777564327" ]
}
]
}
},
{
"vname" : "adt1",
"type" : "0x3620c286757a29985cee194eb90064270fb65414.AddressADT",
"value" :
{
"constructor" : "0x3620c286757a29985cee194eb90064270fb65414.Address2",
"argtypes" : [],
"arguments" : [ "0xabfeccdc9012345678901234567890f777564324" ]
}
},
{
"vname" : "remote1",
"type" : "ByStr20",
"value" : "0xabfeccdc9012345678901234567890f777564325"
}
],
"_origin": "0xabfeccdc9012345678901234567890f777564322"
}
108 changes: 108 additions & 0 deletions tests/runner/remote_state_reads/message_131.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"_tag": "RemoteReadsADTTest",
"_amount": "0",
"_sender": "0xabfeccdc9012345678901234567890f777564322",
"params": [
{
"vname" : "list1",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564324",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564325",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
]
}
]
}
},
{
"vname" : "list2",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
},
{
"vname" : "list3",
"type" : "List ByStr20",
"value" : {
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564324",
{
"constructor" : "Cons",
"argtypes" : [ "ByStr20" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "Nil",
"argtypes" : [ "ByStr20" ],
"arguments" : []
}
]
}
]
}
},
{
"vname" : "pair1",
"type" : "Pair ByStr20 0x3620c286757a29985cee194eb90064270fb65414.AddressADT",
"value" : {
"constructor" : "Pair",
"argtypes" : [ "ByStr20", "0x3620c286757a29985cee194eb90064270fb65414.AddressADT" ],
"arguments" : [
"0xabfeccdc9012345678901234567890f777564323",
{
"constructor" : "0x3620c286757a29985cee194eb90064270fb65414.Address2",
"argtypes" : [],
"arguments" : [ "0xabfeccdc9012345678901234567890f777564327" ]
}
]
}
},
{
"vname" : "adt1",
"type" : "0x3620c286757a29985cee194eb90064270fb65414.AddressADT",
"value" :
{
"constructor" : "0x3620c286757a29985cee194eb90064270fb65414.Address2",
"argtypes" : [],
"arguments" : [ "0xabfeccdc9012345678901234567890f777564324" ]
}
},
{
"vname" : "remote1",
"type" : "ByStr20",
"value" : "0xabfeccdc9012345678901234567890f777564325"
}
],
"_origin": "0xabfeccdc9012345678901234567890f777564322"
}
2 changes: 1 addition & 1 deletion tests/runner/remote_state_reads/output_122.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"errors": [
{
"error_message":
"Address 0xabfeccdc9012345678901234567890f777564327 not in use.",
"No contract found at address 0xabfeccdc9012345678901234567890f777564327",
"start_location": { "file": "", "line": 0, "column": 0 },
"end_location": { "file": "", "line": 0, "column": 0 }
}
Expand Down
12 changes: 12 additions & 0 deletions tests/runner/remote_state_reads/output_130.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"gas_remaining": "7487",
"errors": [
{
"error_message":
"No contract found at address 0xabfeccdc9012345678901234567890f777564327",
"start_location": { "file": "", "line": 0, "column": 0 },
"end_location": { "file": "", "line": 0, "column": 0 }
}
],
"warnings": []
}
12 changes: 12 additions & 0 deletions tests/runner/remote_state_reads/output_131.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"gas_remaining": "7487",
"errors": [
{
"error_message":
"No contract found at address 0xabfeccdc9012345678901234567890f777564327",
"start_location": { "file": "", "line": 0, "column": 0 },
"end_location": { "file": "", "line": 0, "column": 0 }
}
],
"warnings": []
}
Loading

0 comments on commit 5615cc2

Please sign in to comment.