package tezos-protocol-environment

  1. Overview
  2. Docs
Interface layer between the protocols and the shell

Install

Dune Dependency

Authors

Maintainers

Sources

tezos-16.1.tar.gz
sha256=43723d096307603703a1a89ed1b2eb202b365f5e7824b96b0cbf813b343a6cf7
sha512=b2a637f2e965000d3d49ad85277ca24d6cb07a1a7cf2bc69d296d8b03ad78c3eaa8e21e94b9162e62c2e11649cd03bc845b2a3dafe623b91065df69d47dc8e4f

doc/tezos-protocol-environment.structs/Tezos_protocol_environment_structs/V8/Data_encoding/Encoding/Fixed/index.html

Module Encoding.FixedSource

include module type of struct include Data_encoding.Encoding.Fixed end

add_padding e n is a padded version of the encoding e. In Binary, there are n null bytes (\000) added after the value encoded by e. In JSON, padding is ignored.

list n e is an encoding for lists of exactly n elements. If a list of more or fewer elements is provided, then the encoding fails with the write_error List_invalid_length. For decoding, it can fail with read_error Not_enough_data or read_error Extra_bytes, or it may cause other failures further down the line when the AST traversal becomes out-of-sync with the underlying byte-stream traversal.

The difference of the errors being used when encoding and decoding is because when encoding we have access to the list and we can check the actual length, whereas when decoding we only see bytes, sometimes too many, sometimes not enough.

This encoding has a narrow set of possible applications because it is very restrictive. Still, it can to:

  • mirror static guarantees about the length of some lists,
  • special-case some common lengths of typical input in a union (see example below),
  • other ends.
type expr =
  | Op of string * expr list (* most commonly 1 or 2 operands *)
  | Literal of string
let expr_encoding =
  mu "expr" (fun e ->
    union [
      case ~title:"op-nonary" (Tag 0)
        string
        (function Op (op, []) -> Some op | _ -> None)
        (fun op -> Op (op, []));
      case ~title:"op-unary" (Tag 1)
        (tup2 string (Fixed.list 1 e))
        (function Op (op, ([_]) as operand) -> Some (op, operand) | _ -> None)
        (fun (op, operand) -> Op (op, operand));
      case ~title:"op-binary" (Tag 2)
        (tup2 string (Fixed.list 2 e))
        (function Op (op, ([_;_]) as operand) -> Some (op, operand) | _ -> None)
        (fun (op, operand) -> Op (op, operand));
      case ~title:"op-moreary" (Tag 3)
        (tup2 string (list e))
        (function Op (op, operand) -> Some (op, operand) | _ -> None)
        (fun (op, operand) -> Op (op, operand));
      case ~title:"literal" (Tag 4)
        string
        (function Literal l -> Some l | _ -> None)
        (fun l -> Literal l);
        ]
  )

Interestingly, the cases for known lengths can be generated programmatically.

  • raises Invalid_argument

    if the argument e is a `Variable-size encoding or a zero-byte encoding.

See list above.

  • raises Invalid_argument

    if the argument e is a `Variable-size encoding or a zero-byte encoding.

OCaml

Innovation. Community. Security.