Crates.io | sn_registers |
lib.rs | sn_registers |
version | 0.4.3 |
source | src |
created_at | 2023-06-04 22:53:28.128518 |
updated_at | 2024-11-12 19:00:05.358106 |
description | Safe Network Register Logic |
homepage | https://maidsafe.net |
repository | https://github.com/maidsafe/safe_network |
max_upload_size | |
id | 882592 |
size | 55,715 |
Provides utilities for working with registers on the Safe Network.
Registers are a fundamental data structure in the Safe Network, designed for storing and managing mutable data with strong consistency guarantees. They are particularly useful for scenarios requiring atomic updates and conflict resolution in a distributed environment.
A register consists of:
name
of the registerPermissions
showing the who can mutate the registerRegisters are:
The sn_registers
crate provides a high-level API for interacting with registers:
Basic workflow:
MerkleReg is a CRDT that maintains a single value but keeps track of all the changes (mutations) made to that value. It uses a Merkle tree to store and verify the history of mutations. This allows for efficient verification of the state of the register and the history of changes, which is particularly useful in distributed systems where you may need to prove the integrity of data.
The MerkleReg CRDT typically consists of: * Value: The current value stored in the register. * History: A Merkle tree that stores the history of all previous values. Each mutation adds a new node to the tree, which is cryptographically linked to its predecessors, forming a secure chain of updates.
When you mutate the MerkleReg, the following happens: * The current value is replaced with the new value. * The mutation is recorded in the Merkle tree by creating a new node that includes a cryptographic hash of the new value and the hash of the previous state (root of the Merkle tree).
Like other CRDTs, MerkleReg resolves conflicts automatically. If two or more replicas concurrently update the register with different values, the CRDT framework handles merging these changes. The Merkle tree structure helps in efficiently reconciling these updates by comparing the histories.
To show the mutation history in a MerkleReg, you can traverse the Merkle tree, listing all previous values and their associated metadata (such as timestamps or versions). Here’s how you might approach this in practice:
Here are some simple scenarios using the sn_registers
crate:
// `permissions` defines the owner of the register
let mut register = Register::new(owner.pub_key, meta, permissions);
let entry = Entry::new("Hello, Safe Network!".as_bytes().to_vec());
// Children being an empty list for a newly created register
register.write(entry, children, owner.priv_key).await?;
/// Note this reads the root content (i.e. the last entry) of the inner crdt.
/// It will return with multiple `roots`, when there are branches of the inner crdt.
let root_contents = register.read().await?;
for content in root_contents {
println!("content: {:?}", String::from_utf8(content.value)?);
}
/// Note two registers are only mergeable when they are for the same address and permissions.
/// And it is the inner crdt to be merged.
pub fn merge(&mut self, other: &Register) -> Result<()> {
self.verify_is_mergeable(other)?;
self.crdt.merge(other.crdt.clone());
Ok(())
}