2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
open Tezos_hash
|
|
|
|
|
|
|
|
(* Tezos protocol "bootstrap" - untyped script representation *)
|
|
|
|
|
|
|
|
type location = int
|
|
|
|
|
|
|
|
let location_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
def
|
|
|
|
"scriptLocation" @@
|
|
|
|
describe
|
|
|
|
~title:
|
|
|
|
"Script location"
|
|
|
|
~description:
|
|
|
|
"The location of a node in a script (code, data or type) \
|
|
|
|
as its index in the expression tree in prefix order, with \
|
|
|
|
zero being the root and adding one for every basic node, \
|
|
|
|
sequence and primitive application." @@
|
|
|
|
int31
|
|
|
|
|
|
|
|
type expr = (* TODO: turn the location into an alpha ? *)
|
|
|
|
| Int of location * string
|
|
|
|
| String of location * string
|
|
|
|
| Prim of location * string * expr list
|
|
|
|
| Seq of location * expr list
|
|
|
|
|
|
|
|
let expr_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
let int_encoding =
|
|
|
|
obj1 (req "int" string) in
|
|
|
|
let string_encoding =
|
|
|
|
obj1 (req "string" string) in
|
|
|
|
let prim_encoding expr_encoding =
|
2016-09-09 19:04:55 +04:00
|
|
|
let json =
|
|
|
|
union
|
|
|
|
[ case string
|
|
|
|
(function (v, []) -> Some v | _ -> None)
|
|
|
|
(fun v -> (v, [])) ;
|
|
|
|
case (assoc (list expr_encoding))
|
|
|
|
(fun (v, args) -> Some [ (v, args) ])
|
|
|
|
(function
|
|
|
|
| [ (v, args) ] -> (v, args)
|
|
|
|
| _ -> Json.cannot_destruct "invalid script expression") ] in
|
|
|
|
let binary =
|
|
|
|
obj2 (req "prim" string) (req "args" (list expr_encoding)) in
|
|
|
|
splitted ~json ~binary in
|
2016-09-08 21:13:10 +04:00
|
|
|
let seq_encoding expr_encoding =
|
|
|
|
list expr_encoding in
|
|
|
|
mu "tezosScriptExpression" (fun expr_encoding ->
|
|
|
|
describe
|
|
|
|
~title: "Script expression (data, type or code)" @@
|
2016-11-15 04:36:06 +04:00
|
|
|
union ~tag_size:`Uint8
|
2016-09-08 21:13:10 +04:00
|
|
|
[ case ~tag:0 int_encoding
|
|
|
|
(function Int (_, v) -> Some v | _ -> None)
|
|
|
|
(fun v -> Int (-1, v)) ;
|
2016-11-08 22:31:01 +04:00
|
|
|
case ~tag:1 string_encoding
|
2016-09-08 21:13:10 +04:00
|
|
|
(function String (_, v) -> Some v | _ -> None)
|
|
|
|
(fun v -> String (-1, v)) ;
|
2016-11-08 22:31:01 +04:00
|
|
|
case ~tag:2 (prim_encoding expr_encoding)
|
2016-09-08 21:13:10 +04:00
|
|
|
(function
|
2016-09-09 19:04:55 +04:00
|
|
|
| Prim (_, v, args) -> Some (v, args)
|
2016-09-08 21:13:10 +04:00
|
|
|
| _ -> None)
|
2016-09-09 19:04:55 +04:00
|
|
|
(function (prim, args) -> Prim (-1, prim, args)) ;
|
2016-11-08 22:31:01 +04:00
|
|
|
case ~tag:3 (seq_encoding expr_encoding)
|
2016-09-08 21:13:10 +04:00
|
|
|
(function Seq (_, v) -> Some v | _ -> None)
|
|
|
|
(fun args -> Seq (-1, args)) ])
|
|
|
|
|
|
|
|
let update_locations ir =
|
|
|
|
let rec update_locations i = function
|
|
|
|
| Int (_, v) -> (Int (i, v), succ i)
|
|
|
|
| String (_, v) -> (String (i, v), succ i)
|
|
|
|
| Prim (_, name, args) ->
|
|
|
|
let (nargs, ni) =
|
|
|
|
List.fold_left (fun (nargs, ni) arg ->
|
|
|
|
let narg, ni = update_locations ni arg in
|
|
|
|
(narg :: nargs, ni))
|
|
|
|
([], succ i) args in
|
|
|
|
(Prim (i, name, List.rev nargs), ni)
|
|
|
|
| Seq (_, args) ->
|
|
|
|
let (nargs, ni) =
|
|
|
|
List.fold_left (fun (nargs, ni) arg ->
|
|
|
|
let narg, ni = update_locations ni arg in
|
|
|
|
(narg :: nargs, ni))
|
|
|
|
([], succ i) args in
|
|
|
|
(Seq (i, List.rev nargs), ni) in
|
2016-11-10 19:25:31 +04:00
|
|
|
fst (update_locations 1 ir)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let expr_encoding =
|
|
|
|
Data_encoding.conv
|
|
|
|
(fun to_write -> to_write)
|
|
|
|
(fun just_read -> update_locations just_read)
|
|
|
|
expr_encoding
|
|
|
|
|
|
|
|
type code =
|
|
|
|
{ code : expr ;
|
|
|
|
arg_type : expr ;
|
|
|
|
ret_type : expr ;
|
|
|
|
storage_type : expr }
|
|
|
|
|
|
|
|
type storage =
|
|
|
|
{ storage : expr ;
|
|
|
|
storage_type : expr }
|
|
|
|
|
|
|
|
open Data_encoding
|
|
|
|
|
|
|
|
let storage_encoding =
|
|
|
|
conv
|
|
|
|
(fun { storage ; storage_type } -> (storage, storage_type))
|
|
|
|
(fun (storage, storage_type) -> { storage ; storage_type })
|
|
|
|
(obj2
|
|
|
|
(req "storage" expr_encoding)
|
|
|
|
(req "storageType" expr_encoding))
|
|
|
|
|
|
|
|
let code_encoding =
|
|
|
|
conv
|
|
|
|
(fun { code; arg_type; ret_type; storage_type } ->
|
|
|
|
(code, arg_type, ret_type, storage_type))
|
|
|
|
(fun (code, arg_type, ret_type, storage_type) ->
|
|
|
|
{ code; arg_type; ret_type; storage_type })
|
|
|
|
(obj4
|
|
|
|
(req "code" expr_encoding)
|
|
|
|
(req "argType" expr_encoding)
|
|
|
|
(req "retType" expr_encoding)
|
|
|
|
(req "storageType" expr_encoding))
|
|
|
|
|
|
|
|
let hash_expr data =
|
|
|
|
let bytes = Data_encoding.Binary.to_bytes expr_encoding data in
|
2017-02-19 21:22:32 +04:00
|
|
|
Script_expr_hash.(hash_bytes [ bytes ] |> to_b58check)
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
type t =
|
2017-03-09 22:17:13 +04:00
|
|
|
{ code : code ;
|
|
|
|
storage : storage }
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let encoding =
|
|
|
|
let open Data_encoding in
|
2017-03-09 22:17:13 +04:00
|
|
|
conv
|
|
|
|
(function { code ; storage } -> (code, storage))
|
|
|
|
(fun (code, storage) -> { code ; storage })
|
|
|
|
(obj2
|
|
|
|
(req "code" code_encoding)
|
|
|
|
(req "storage" storage_encoding))
|