Skip to content
Snippets Groups Projects

Draft: Dh@table of constants

Closed Daniel Hines requested to merge marigold/tzip:dh@table-of-constants into master
+ 100
0
---
title: Table of Micheline feature
status: Draft
author: realD4hines
type: L
created: 2020-01-05
date: 2021-01-05
version: 0
---
## Summary
We propose adding a global table to the protocol for storing Michelson values,
accessible from any contract.
## Motivation
Contracts have a fixed upper size limit. Unfortunately, the current size limit
severely restricts the complexity of smart contracts, forcing users to resort to a
kind of Michelson code golf to compensate.
Our proposed solution to this problem is a shared table of Michelson values that
contracts can reference by hash, allowing users to factor large chunks of code
out of their contract. We expect the primary things to be factored out to be
lambdas and large types.
Factoring out lambdas and types has many benefits:
- Globally available lambdas form libraries of reusable logic, enabling much
more complex applications to fit into the same size.
- As the table becomes more widely adopted it serves as a reference of the
the most frequently reused functions. This supplies us with fine grained data to
further optimize the most heavily used contracts:
- Large types for complex storages or parameters are often repeated throughout a
contract, taking up precious space. Referencing these types via constants
alleviates this problem.
## Specification
- A new manager operation `Register_global` is created, which allows the user to
store a valid Micheline representation of a Michelson value in a
globally-accessible table and returns the hash of this value.
- Expressions registered as constants may contain references to other constants.
However, the depth of a fully-expanded expression is constrained so as to not
exceed the depth of a contract that can be type checked (currently the typechecker
fails after 10,000 iterations.)
- To prevent the need to iterate through deep chains of constants, the depth of
a constant is stored along with its value.
- The storage table is implemented similarly to the existing one for
`big_map`'s, but is shared by all contracts instead of accessible only to
one.
- A new Michelson primitive `constant` is added for referencing these values.
- When applying a transaction, right before type elaboration, we substitute each
occurrence of a constant primitive with the value it points to in storage.
If no value is found, the transaction fails.
- The typed Michelson IR has no knowledge of constants, and the Michelson type
elaborator and interpreter do not change in any way.
- Users will be presented with several new `tezos-client` commands (we may add
others as we see the need):
- `tezos-client register global constant <expression> from <src>`
Registers a new expression in the table.
- `tezos-client get global constant <hash>`
Returns the value of the hash in the current context (if it exists).
- `tezos-client expand constants in <contract>`
Substitutes every constant with its value in the current context (or
fails if a hash is not found).
## Backwards Compatibility
To the author's knowledge, the change does not break any existing functionality
and is only additive.
## Security Considerations
Once contracts are loading libraries, an obvious attack is to try to trick
developers into loading malicious libraries. Users must take care to use the
correct hash and review all the code they load into their contracts.
At the same time, libraries could improve security overall, since code be
audited and certified at the function level rather than the contract level. As a
user, I will find it easier to review 100 lines of logic that uses trusted
libraries than 1000 lines of in-lined code that does the same thing.
## Implementation
The global table of micheline is implemented in MR 2962: https://gitlab.com/tezos/tezos/-/merge_requests/2962
## Appendix
Much credit goes to Arthur Breitman's
[post on the Agora](https://forum.tezosagora.org/t/global-constants/2025)
and Tom Jack's insights and critiques.
## Copyright
Copyright and related rights waived via
[CC0](https://creativecommons.org/publicdomain/zero/1.0/).
Loading