Skip to content

Draft: Proto/Michelson: `big_map` caching

Alistair O'Brien requested to merge ajob410@proto-michelson-big-map-caching into master

Why

Context: Adding references to Michelson

Presently, fetching values for a big map will always involve deserializing a Micheline value from the Alpha_context. This poses an issue for references since fetching the same value would not result in the same reference.

# string (ref int) big_map : 'S
DUP
# string (ref int) big_map : string (ref int) big_map : 'S
# fetch ref for "Alistair"
PUSH string "Alistair"
GET
ASSERT_SOME
# ref int : string (ref int) big_map : 'S
# fetch ref for "Alistair" again
SWAP
PUSH STRING "Alistair" 
GET
ASSERT_SOME
# ref int : ref int : 'S
# += 1 the first ref 
DUP 
DUP 
GET_REF
PUSH int 1
ADD
SET_REF
# ref int : ref int : 'S
ASSERT_NEQ

What

This MR aims to solve the above issue by maintaining a cache for each big map, stored in the big map cache context.

How

We introduce a big map cache with the interface:

type ('key, 'value) t = ('key, 'value) Script_typed_ir.big_map_cache

type ('k, 'v, 'vc) tid = Big_map.Id.t * 'k comparable_ty * ('v, 'vc) ty

val find_and_cache_by_hash :
  context ->
  Script_expr_hash.t ->
  ('k, 'v) t ->
  ('v option * ('k, 'v) t * context) tzresult Lwt.t

module Context : sig
  type t = Script_typed_ir.big_map_cache_context

  val init : t

  val find_and_cache_by_hash :
    context ->
    ('k, 'v, _) tid ->
    Script_expr_hash.t ->
    t ->
    ('v option * t * context) tzresult Lwt.t
end

The context is passed around monadically in the Script_interpreter.step function. Script_big_map.get (and get_and_update) is modified to lookup the key in the cache first before looking up the key in the Alpha_context.

Manually testing the MR

make test-proto-unit

Checklist

  • Document the interface of any function added or modified (see the coding guidelines)
  • Document any change to the user interface, including configuration parameters (see node configuration)
  • Provide automatic testing (see the testing guide).
  • For new features and bug fixes, add an item in the appropriate changelog (docs/protocols/alpha.rst for the protocol and the environment, CHANGES.rst at the root of the repository for everything else).
  • Select suitable reviewers using the Reviewers field below.
  • Select as Assignee the next person who should take action on that MR
Edited by Alistair O'Brien

Merge request reports