GithubHelp home page GithubHelp logo

canister-tools's Introduction

canister-tools

A Rust library for canisters on the internet-computer.

Features

  • Easy simple upgrade strategy.
  • Canister data safety features.
  • Canister methods for the manual download and upload of the global variables in the heap or stable-memory.
  • Take and serialize snapshots of the canister's global variables in the heap, and then download the snapshots.
  • Upload a snapshot of a canister's global variable and load it onto the canister's global variable.

The libarary works with the virtual-memories feature of the ic-stable-structures crate. This way, you can store some canister data directly in a virtual-stable-memory and at the same time keep a global variable on the main heap memory that persists through upgrades.

Upgrade Strategy

thread_local! {
    // canister global data
    static DATA: RefCell<Data> = RefCell::new(Data::default());
}
  
// set a memory-id for the global variable 
const DATA_UPGRADE_MEMORY_ID: MemoryId = MemoryId::new(0);
  
#[init]
fn init() {
    // register the global variable with the memory-id
    canister_tools::init(&DATA, DATA_UPGRADE_MEMORY_ID);
}  
  
#[pre_upgrade]
fn pre_upgrade() {
    // serialize and store the global variables into their memory-ids for the upgrade. 
    canister_tools::pre_upgrade();
}

#[post_upgrade]
fn post_upgrade() {
    // load the data in the memory-id onto the global variable.
    canister_tools::post_upgrade(&DATA, DATA_UPGRADE_MEMORY_ID, None::<fn(OldData) -> Data>);
}

  
  
  

Download snapshots of the canister global variables, and upload snapshots onto the global variables.

This library creates the following canister methods for the state-snapshot management and stable-memory management.

type MemoryId = nat8;
type Offset = nat64;
type Length = nat64;
type StateSnapshotLength = nat64;
type WasmPages = nat64;

service : {
    // Takes a snapshot of the data structure registered at the given MemoryId.
    controller_create_state_snapshot : (MemoryId) -> (StateSnapshotLength);
    
    // Download the snapshot of the data corresponding to the given MemoryId.
    // Download the data in chunks.
    controller_download_state_snapshot : (MemoryId, Offset, Length) -> (blob) query;
    
    // Clears the snapshot of the data corresponding to the given MemoryId.
    // When uploading data onto the data structure, call this method first to clear
    // the snapshot before uploading a customized snapshot.
    controller_clear_state_snapshot : (MemoryId) -> ();
    
    // Upload the serialized data structure for the given MemoryId in chunks that can then be deserialized and loaded onto the canister global variable.   
    controller_append_state_snapshot : (MemoryId, blob) -> ();
    
    // Deserializes the snapshot for the data structure corresponding to the given MemoryId
    // and loads it onto the canister's global variable.
    controller_load_state_snapshot : (MemoryId) -> ();

    // Common stable memory functions as canister methods.
    // Useful when using a custom stable-memory strategy for one or some of the MemoryIds. 
    controller_stable_memory_read : (MemoryId, Offset, Length) -> (blob) query;
    controller_stable_memory_write : (MemoryId, Offset, blob) -> ();
    controller_stable_memory_size : (MemoryId) -> (nat64) query;
    controller_stable_memory_grow : (MemoryId, WasmPages) -> (int64);
}

canister-tools's People

Contributors

levifeldman avatar

Stargazers

hackartist avatar Muhammad Rifqi Fatchurrahman avatar btwl avatar David Dal Busco avatar Dominic Wörner avatar Moritz Fuller avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.