# Summary

This RFC defines a file format for local snapshots which is compatible with Chrysalis Phase 2.

# Motivation

Nodes create local snapshots to produce ledger representations at a point in time of a given milestone to be able to:

• Start up from a recent milestone instead of having to synchronize from the genesis transaction.
• Delete old transaction data below a given milestone.

Current node implementations use a local snapshot file format which only works with account based ledgers. For Chrysalis Phase 2, this file format has to be assimilated to support a UTXO based ledger.

# Detailed design

Since a UTXO based ledger is much larger in size, this RFC proposes two formats for snapshot files:

• A full format which represents a complete ledger state.
• A delta format which only contains diffs (created and consumed outputs) of milestones from a given milestone index onwards.

This separation allows nodes to swiftly create new delta snapshot files, which then can be distributed with a companion full snapshot file to reconstruct a recent state.

Unlike the current format, these new formats do not include spent addresses since this information is no longer held by nodes.

### Formats​

All types are serialized in little-endian

#### Full Ledger State​

A full ledger snapshot file contains the UTXOs (outputs section) of a node's confirmed milestone (ledger_milestone_index). The diffs contain the diffs to rollback the outputs state to regain the ledger state of the snapshot milestone at (seps_milestone_index).

While the node producing such a full ledger state snapshot could theoretically pre-compute the actual snapshot milestone state, this is deferred to the consumer of the data to speed up local snapshot creation.

#### Delta Ledger State​

A delta ledger state local snapshot only contains the diffs of milestones starting from a given ledger_milestone_index. A node consuming such data must know the state of the ledger at ledger_milestone_index.

#### Schema​

##### Output​

Defines an output.

NameTypeDescription
Message HashArray<byte>[32]The hash of the message in which the transaction was contained which generated this output.
Transaction hashArray<byte>[32]The hash of the transaction which generated this output.
Output indexuint16The index of this output within the transaction.
Output oneOf
SigLockedSingleDeposit
NameTypeDescription
Output TypebyteSet to value 0 to denote a SigLockedSingleDeposit.
Address oneOf
 Name Type Description Address Type byte/varint Set to value 0 to denote an Ed25519 Address. Address ByteArray[32] The raw bytes of the Ed25519 address which is a BLAKE2b-256 hash of the Ed25519 public key.
Amountuint64The amount of tokens this output deposits.
SigLockedDustAllowanceDeposit
NameTypeDescription
Output TypebyteSet to value 1 to denote a SigLockedDustAllowanceDeposit.
Address oneOf
 Name Type Description Address Type byte/varint Set to value 0 to denote an Ed25519 Address. Address ByteArray[32] The raw bytes of the Ed25519 address which is a BLAKE2b-256 hash of the Ed25519 public key.
Amountuint64The amount of tokens this output deposits.
##### Milestone Diff​

Defines the diff a milestone produced by listing the created/consumed outputs and the milestone payload itself.

NameTypeDescription
Treasury Input
only included if milestone contains a receipt
NameTypeDescription
Treasury Input Milestone HashArray<byte>[32]The hash of the milestone this input references.
Treasury Input Amountuint64The amount of this treasury input.
Created Outputs Countuint64The amount of outputs generated with this milestone diff.
Created Outputs anyOf
Output
Consumed Outputs Countuint64The amount of outputs consumed with this milestone diff.
Consumed Outputs anyOf
Output
##### Full snapshot file format​

Defines what a full snapshot file contains.

 Name Type Description Version byte Denotes the version of this file format. Type byte Denotes the type of this file format. Value 0 denotes a full snapshot. Timestamp uint64 The UNIX timestamp in seconds of when this snapshot was produced. Network ID uint64 The ID of the network to which this snapshot is compatible. SEPs milestone index uint64 The milestone index for which the SEPs were generated for. Ledger milestone index uint64 The milestone index of which the UTXOs within the snapshot are from. SEPs count uint64 The amount of SEPs contained within this snapshot. Outputs count uint64 The amount of UTXOs contained within this snapshot. Milestone diffs count uint64 The amount of milestone diffs contained within this snapshot. Treasury Output Milestone Hash Array[32] The milestone hash of the milestone which generated the treasury output. Treasury Output Amount uint64 The amount of funds residing on the treasury output. SEPs SEP Array[32] Outputs Output Milestone Diffs Milestone Diff
##### Delta snapshot file format​

Defines what a delta snapshot contains.

 Name Type Description Version byte Denotes the version of this file format. Type byte Denotes the type of this file format. Value 1 denotes a delta snapshot. Timestamp uint64 The UNIX timestamp in seconds of when this snapshot was produced. Network ID uint64 The ID of the network to which this snapshot is compatible. SEPs milestone index uint64 The milestone index for which the SEPs were generated for. Ledger milestone index uint64 The milestone index up on which this delta snapshot builts up from. SEPs count uint64 The amount of SEPs contained within this snapshot. Milestone diffs count uint64 The amount of milestone diffs contained within this snapshot. SEPs SEP Array[32] Milestone Diffs Milestone Diff

# Drawbacks

Nodes need to support this new format.

# Rationale and alternatives

• In conjunction with a companion full snapshot, a tool or node can "truncate" the data from a delta snapshot back to a single full snapshot. In that case, the ledger_milestone_index and seps_milestone_index would be the same. In the example above, given the full and delta snapshots, one could produce a new full snapshot for milestone 1350.
• Since snapshots may include millions of UTXOs, code generating such files needs to stream data directly onto disk instead of keeping the entire representation in memory. In order to facilitate this, the count denotations for SEPs, UTXOs and diffs are at the beginning of the file. This allows code generating snapshot files to only have to seek back once after the actual count of elements is known.

# Unresolved questions

• Is all the information to startup a node from the local snapshot available with the described format?