pchain-world-state

Crates.iopchain-world-state
lib.rspchain-world-state
version0.4.2
sourcesrc
created_at2024-04-12 06:19:32.868479
updated_at2024-04-12 06:19:32.868479
descriptionDefinitions of structs, functions, and methods for read-write world state from and into a persistent storage in ParallelChain Protocol
homepage
repositoryhttps://github.com/parallelchain-io/pchain-world-state
max_upload_size
id1206030
size131,202
ParallelChain Lab (parallelchainlab)

documentation

README

ParallelChain World State

In ParallelChain, we call the user-visible state that Mainnet maintains the “World State”. The world state is a set of key-value tuples representing the state of every “Account”, including both External accounts and Contract accounts, stored inside a Merkle Patricia Trie (MPT) paritytech/trie-db. This library provides set of functions to read and update world state.

Modules

  • states: definition of WorldState and AccountStorageStage methods to read/write the MPT.
  • network: data formatting scheme to store network-wide state in world state.
  • keys: definition of keys that Parallelchain-F are used to writes into persistent storage.
  • storage: definition of trait for accessing persistent storage from world state, and data structure of the world state changes.
  • error: error handling when accessing the world state.

Basic usage

// Here demonstrates how to create empty world state, update account information 
// and save the world state into database(hashmap).

// This is example address. Don't use this for real transaction.
let address: pchain_types::cryptography::PublicAddress = [200, 49, 188, 70, 13, 208, 8, 5, 148, 104, 28, 81, 229, 202, 203, 180, 220, 187, 48, 162, 53, 122, 83, 233, 166, 97, 173, 217, 25, 172, 106, 53];

// Step 1. prepare database that implement WorldStateStorage trait.
#[derive(Clone)]
struct DummyStorage(HashMap<Key, Value>);

impl WorldStateStorage for DummyStorage{
    fn get(&self, key: &Key) -> Option<Value>{
        match self.0.get(key){
            Some(value) => Some(value.to_owned()),
            None => None
        }
    }
}

impl DummyStorage{
    fn apply_changes(&mut self, changes: WorldStateChanges){
        for key in changes.deletes.iter() {
            self.remove(key);
        }

        for (key, value) in changes.inserts.iter() {
            self.insert(key.clonel(), value.clone());
        }
    }
}

// Create new database
let mut db = DummyStorage(HashMap::new());

// Step 2. Start an empty world state.
let ws = WorldState::initialize(db);

// Step 3. Some world state operations
// 3.1 Operations that immediately commit to the trie.
ws.with_commit().set_balance(address, 100);
ws.with_commit().set_nonce(address, 1);
ws.with_commit().set_storage_value(address, AppKey::new(key), value);
// 3.2 Operations that saved in cached set.
ws.cached().set_balance(address, 100);
ws.cached().set_nonce(address, 1);
ws.cached().set_storage_value(address, AppKey::new(key), value);
ws.commit(); // you can commit the cached changes to MPT

// Step 4. Commit and close the world state to get the WorldStateChanges
// WorldStateChanges contains the trie node changes since opening the world state and the new trie root hash (a.k.a state hash)
let db_changes: WorldStateChanges = ws.commit_and_close();

// Setp 5. Save the WorldStateChanges to database
db.apply_changes(db_changes);

Versioning

The version of this library reflects the version of the ParallelChain Protocol which it implements. For example, the current version is 0.4.2, and this implements protocol version 0.4. Patch version increases are not guaranteed to be non-breaking.

Opening an issue

Open an issue in GitHub if you:

  1. Have a feature request / feature idea,
  2. Have any questions (particularly software related questions),
  3. Think you may have discovered a bug.

Please try to label your issues appropriately.

Commit count: 37

cargo fmt