Skip to content

Proposal: Native Contract for Time-Limited (TTL) Temporary Storage with PostPersist Cleanup #4466

@erikzhang

Description

@erikzhang

Background

One of the long-standing concerns discussed in this repository (e.g. #1187) is state growth and long-term storage bloat.
At the same time, many use cases only require short-lived, temporary data, such as:

  • off-chain coordination / handshakes
  • temporary commitments or proofs
  • message passing, caches, or relay data
  • intermediate results in multi-step workflows

Currently, all contract storage is effectively permanent unless explicitly deleted by users, which makes these use cases inefficient and increases long-term state pressure.


Proposal

Introduce a native contract that provides time-limited (TTL) temporary storage, where each entry is written together with an explicit expiration period.
Expired entries are automatically removed by the system during PostPersist, without requiring any user transaction.

This creates a first-class, protocol-level mechanism for ephemeral storage, instead of overloading permanent contract storage.


High-Level Design

1. Native Contract: TemporaryStorage (name illustrative)

The contract manages temporary key-value entries with an expiration height.

Core methods:

  • Put(byte[] key, byte[] value, uint ttlBlocks)
  • Get(byte[] key) -> byte[] (returns empty / null if expired)
  • Delete(byte[] key) (early removal)
  • (optional) Renew(byte[] key, uint ttlBlocks)

TTL is defined in block height, not wall-clock time.


2. Storage Layout

To enable efficient cleanup in PostPersist, the contract maintains:

  • Data storage

    • D | keyId -> value
  • Metadata

    • E | expireHeight | keyId -> (empty)

This allows the system to directly locate all entries expiring at a given height.


3. PostPersist Cleanup

During PostPersist(currentHeight):

  1. Iterate over keys with prefix E | currentHeight

  2. For each keyId:

    • Remove data entry
    • Remove expiration index entry

To ensure consensus safety:

  • A maximum cleanup limit per block should be enforced.
  • If the limit is reached, cleanup can continue in subsequent blocks using a cursor.

This guarantees bounded execution cost per block.


Advantages

  • Prevents permanent state bloat for short-lived data
  • Enables new classes of applications requiring ephemeral storage
  • System-enforced expiration (no reliance on user cleanup)
  • Aligns with storage-rent and sustainability discussions

Optional Extensions

  • Storage rent based on bytes × ttlBlocks
  • Maximum TTL / size limits enforced by policy
  • Cross-contract data sharing

Summary

This proposal introduces a clean, explicit, and protocol-level solution for temporary storage, reducing long-term state growth while enabling practical real-world use cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DiscussionInitial issue state - proposed but not yet accepted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions