## Architecture

Tapestry uses state compression and Merkle trees to store social graph data onchain. This is the same technology used for compressed NFTs on Solana.

## State Compression Overview

State compression is a technique that allows for storing large amounts of data off-chain while maintaining a cryptographic proof of that data onchain. Here's how Tapestry implements this:

1. **Off-chain Storage**: Social graph data (user profiles, connections, interactions) is stored in an off-chain graph database. This allows for fast queries and updates without incurring blockchain transaction costs for every operation.

2. **Merkle Trees**: The off-chain data is organized into Merkle trees. A Merkle tree is a data structure where each non-leaf node is a hash of its child nodes, and leaf nodes contain the actual data or hashes of data.

3. **Onchain Root**: The root of the Merkle tree, which represents a cryptographic summary of the entire dataset, is stored on the Solana blockchain. This root serves as an immutable, on-chain anchor for the off-chain data. Any attempt to tamper with the off-chain data will result in a mismatch with the on-chain Merkle root, making it immediately detectable and ensuring the integrity of the entire social graph.

## Parallels with Compressed NFTs

This approach is identical to how compressed NFTs work on Solana:

- **Compressed NFTs**: Store metadata off-chain but keep a Merkle root on-chain for verification.
- **Tapestry Social Graph**: Stores social data off-chain but keeps a Merkle root on-chain for verification.

In both cases, the goal is to reduce on-chain storage costs while maintaining the security and verifiability benefits of blockchain technology.

## Benefits of This Approach

1. **Scalability**: Can handle millions of users and interactions without bloating the blockchain.
2. **Cost-Efficiency**: Reduces on-chain storage costs significantly.
3. **Speed**: Allows for fast reads and writes to the social graph.
4. **Verifiability**: Any piece of social graph data can be cryptographically verified against the on-chain Merkle root.
5. **Decentralization**: While data is stored off-chain, its integrity is ensured by the decentralized Solana network.

## How It Works in Practice

1. When a user updates their profile or makes a new connection, the data is stored in the off-chain database.
2. The Merkle tree is updated to include this new data.
3. The new Merkle root is calculated and stored on-chain in a Solana transaction.
4. To verify any piece of social graph data, a Merkle proof can be generated and checked against the on-chain root.

## Conclusion

By leveraging state compression and Merkle trees, Tapestry achieves a perfect balance between the scalability needs of a social graph and the security guarantees of blockchain technology. This approach ensures that Tapestry is as decentralized as compressed NFTs, while providing the performance and cost-efficiency required for a large-scale social protocol.
