Commit e1542c85 authored by Andrew Jones's avatar Andrew Jones

Construct storage key in contract

parent a1ba6875
Pipeline #69538 failed with stages
in 21 minutes and 21 seconds
......@@ -9,6 +9,7 @@ ink_abi = { path = "../../../abi", default-features = false, features = ["derive
ink_core = { path = "../../../core", default-features = false }
ink_lang2 = { path = "../../../lang2", default-features = false }
blake2-rfc = { version = "0.2.18", default-features = false }
scale = { package = "parity-scale-codec", version = "1.1", default-features = false, features = ["derive"] }
type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", default-features = false, features = ["derive"], optional = true }
......@@ -24,6 +25,7 @@ crate-type = [
[features]
default = ["test-env"]
std = [
"blake2-rfc/std",
"ink_abi/std",
"ink_core/std",
"scale/std",
......
......@@ -17,53 +17,60 @@
#![feature(proc_macro_hygiene)]
#![cfg_attr(not(feature = "std"), no_std)]
use scale::Encode as _;
use scale::{
Encode as _,
KeyedVec as _,
};
use ink_core::{
memory::format,
storage,
};
use ink_lang2 as ink;
mod crypto {
/// Do a Blake2 256-bit hash and place result in `dest`.
pub fn blake2_256_into(data: &[u8], dest: &mut [u8; 32]) {
dest.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], data).as_bytes());
}
/// Do a Blake2 256-bit hash and return result.
pub fn blake2_256(data: &[u8]) -> [u8; 32] {
let mut r = [0; 32];
blake2_256_into(data, &mut r);
r
}
}
#[ink::contract(version = "0.1.0")]
mod runtime {
/// This simple contract reads a value from runtime storage
#[ink(storage)]
struct RuntimeStorage {
balance_storage_keys: storage::HashMap<AccountId, [u8; 32]>,
}
impl RuntimeStorage {
#[ink(constructor)]
fn new(&mut self) {}
#[ink(message)]
fn add_account_storage_key(&mut self, account: AccountId, key: [u8; 32]) {
self.env().println(&format!("Adding key for account {:?}", account.encode()));
self.balance_storage_keys.insert(account, key);
}
/// Returns the account balance, read directly from runtime storage
#[ink(message)]
fn get_balance(&self, account: AccountId) -> Balance {
self.env().println(&format!("Getting balance for account {:?}", account.encode()));
match self.balance_storage_keys.get(&account) {
Some(key) => {
let result = self.env().get_runtime_storage::<Balance>(&key[..]);
match result {
Ok(balance) => {
self.env().println("get_runtime_storage: Read balance Ok");
balance
},
Err(err) => {
self.env().println(&format!("Error reading runtime storage {:?}", err));
0
},
}
}
None => {
self.env().println(&format!("No key found for account {:?}", account.encode()));
const BALANCE_OF: &[u8] = b"balance:";
let key = crypto::blake2_256(&account.to_keyed_vec(BALANCE_OF));
self.env().println(&format!("Calculated key {:?}", key));
let result = self.env().get_runtime_storage::<Balance>(&key[..]);
match result {
Ok(balance) => {
self.env().println("get_runtime_storage: Read balance Ok");
balance
},
Err(err) => {
self.env().println(&format!("Error reading runtime storage {:?}", err));
0
}
},
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment