mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 08:51:09 +00:00
Polish
This commit is contained in:
@@ -244,7 +244,7 @@ mod tests {
|
||||
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
|
||||
self.storage.insert(key, value);
|
||||
}
|
||||
|
||||
|
||||
fn chain_id(&self) -> u64 { 42 }
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ mod codec;
|
||||
mod support;
|
||||
mod runtime;
|
||||
pub use codec::{endiansensitive, streamreader, joiner, slicable, keyedvec};
|
||||
pub use support::{primitives, function, environment, storage, storagevec};
|
||||
pub use support::{primitives, function, environment, storable};
|
||||
#[cfg(test)]
|
||||
pub use support::{testing, statichex};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use runtime_support::Vec;
|
||||
use storagevec::StorageVec;
|
||||
use storable::StorageVec;
|
||||
use primitives::SessionKey;
|
||||
|
||||
struct AuthorityStorageVec {}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use runtime_support::Vec;
|
||||
use keyedvec::KeyedVec;
|
||||
use storage::Storage;
|
||||
use storagevec::StorageVec;
|
||||
use storable::{kill, Storable, StorageVec};
|
||||
use primitives::{AccountID, SessionKey, BlockNumber};
|
||||
use runtime::{system, staking, consensus};
|
||||
|
||||
@@ -38,7 +37,7 @@ pub fn set_validators(new: &[AccountID]) {
|
||||
|
||||
/// The number of blocks in each session.
|
||||
pub fn length() -> BlockNumber {
|
||||
Storage::into(b"ses\0bps")
|
||||
Storable::lookup_default(b"ses\0bps")
|
||||
}
|
||||
|
||||
/// Hook to be called prior to transaction processing.
|
||||
@@ -62,11 +61,13 @@ pub fn post_transactions() {
|
||||
|
||||
/// Move onto next session: register the new authority set.
|
||||
fn next_session() {
|
||||
// TODO: Call set_authorities() with any new authorities.
|
||||
validators().iter().enumerate().for_each(|(i, v)| {
|
||||
let k = v.to_keyed_vec(b"ses\0nxt\0");
|
||||
if let Some(n) = Storage::try_into(&k) {
|
||||
if let Some(n) = Storable::lookup(&k) {
|
||||
consensus::set_authority(i as u32, &n);
|
||||
kill(&k);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: tests
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use keyedvec::KeyedVec;
|
||||
use storage::Storage;
|
||||
use storable::Storable;
|
||||
use primitives::{BlockNumber, Balance, AccountID};
|
||||
use runtime::{system, session};
|
||||
|
||||
@@ -10,7 +10,7 @@ use runtime::{system, session};
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
pub fn lockup_eras() -> BlockNumber {
|
||||
Storage::into(b"sta\0lpe")
|
||||
Storable::lookup_default(b"sta\0lpe")
|
||||
}
|
||||
|
||||
/// The length of a staking era in blocks.
|
||||
@@ -20,12 +20,12 @@ pub fn era_length() -> BlockNumber {
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
pub fn sessions_per_era() -> BlockNumber {
|
||||
Storage::into(b"sta\0spe")
|
||||
Storable::lookup_default(b"sta\0spe")
|
||||
}
|
||||
|
||||
/// The current era index.
|
||||
pub fn current_era() -> BlockNumber {
|
||||
Storage::into(b"sta\0era")
|
||||
Storable::lookup_default(b"sta\0era")
|
||||
}
|
||||
|
||||
/// The current era index.
|
||||
@@ -35,7 +35,7 @@ pub fn set_current_era(new: BlockNumber) {
|
||||
|
||||
/// The block number at which the era length last changed.
|
||||
pub fn last_era_length_change() -> BlockNumber {
|
||||
Storage::into(b"sta\0lec")
|
||||
Storable::lookup_default(b"sta\0lec")
|
||||
}
|
||||
|
||||
/// Set a new era length. Won't kick in until the next era change (at current length).
|
||||
@@ -51,7 +51,7 @@ fn next_era() {
|
||||
set_current_era(current_era() + 1);
|
||||
|
||||
// Enact era length change.
|
||||
let next_spe: u64 = Storage::into(b"sta\0nse");
|
||||
let next_spe: u64 = Storable::lookup_default(b"sta\0nse");
|
||||
if next_spe > 0 && next_spe != sessions_per_era() {
|
||||
next_spe.store(b"sta\0spe");
|
||||
system::block_number().store(b"sta\0lec");
|
||||
@@ -63,16 +63,16 @@ fn next_era() {
|
||||
|
||||
/// The balance of a given account.
|
||||
pub fn balance_inactive(who: &AccountID) -> Balance {
|
||||
Storage::into(&who.to_keyed_vec(b"sta\0bal\0"))
|
||||
Storable::lookup_default(&who.to_keyed_vec(b"sta\0bal\0"))
|
||||
}
|
||||
|
||||
/// Transfer some unlocked staking balance to another staker.
|
||||
pub fn transfer_inactive(transactor: &AccountID, dest: &AccountID, value: Balance) {
|
||||
let from_key = transactor.to_keyed_vec(b"sta\0bal\0");
|
||||
let from_balance: Balance = Storage::into(&from_key);
|
||||
let from_balance: Balance = Storable::lookup_default(&from_key);
|
||||
assert!(from_balance >= value);
|
||||
let to_key = dest.to_keyed_vec(b"sta\0bal\0");
|
||||
let to_balance: Balance = Storage::into(&to_key);
|
||||
let to_balance: Balance = Storable::lookup_default(&to_key);
|
||||
assert!(to_balance + value > to_balance); // no overflow
|
||||
(from_balance - value).store(&from_key);
|
||||
(to_balance + value).store(&to_key);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use primitives::{Block, BlockNumber, Hash, UncheckedTransaction, TxOrder, Hashable};
|
||||
use runtime_support::{Vec, swap};
|
||||
use storage::Storage;
|
||||
use storable::Storable;
|
||||
use keyedvec::KeyedVec;
|
||||
use environment::with_env;
|
||||
use runtime::session;
|
||||
@@ -12,7 +12,7 @@ pub fn block_number() -> BlockNumber {
|
||||
|
||||
/// Get the block hash of a given block (uses storage).
|
||||
pub fn block_hash(number: BlockNumber) -> Hash {
|
||||
Storage::into(&number.to_keyed_vec(b"sys\0old\0"))
|
||||
Storable::lookup_default(&number.to_keyed_vec(b"sys\0old\0"))
|
||||
}
|
||||
|
||||
/// Deposits a log and ensures it matches the blocks log data.
|
||||
@@ -69,7 +69,7 @@ pub fn execute_transaction(utx: &UncheckedTransaction) {
|
||||
|
||||
// check nonce
|
||||
let nonce_key = tx.signed.to_keyed_vec(b"sys\0non\0");
|
||||
let expected_nonce: TxOrder = Storage::into(&nonce_key);
|
||||
let expected_nonce: TxOrder = Storable::lookup_default(&nonce_key);
|
||||
assert!(tx.nonce == expected_nonce, "All transactions should have the correct nonce");
|
||||
|
||||
// increment nonce in storage
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use primitives::Timestamp;
|
||||
use storage::Storage;
|
||||
use storable::Storable;
|
||||
|
||||
pub fn get() -> Timestamp {
|
||||
Storage::into(b"tim\0val")
|
||||
Storable::lookup_default(b"tim\0val")
|
||||
}
|
||||
|
||||
pub fn set(now: Timestamp) {
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
pub mod primitives;
|
||||
pub mod function;
|
||||
pub mod environment;
|
||||
pub mod storage;
|
||||
pub mod storagevec;
|
||||
pub mod storable;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod statichex;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
use slicable::Slicable;
|
||||
use endiansensitive::EndianSensitive;
|
||||
use keyedvec::KeyedVec;
|
||||
use runtime_support;
|
||||
|
||||
pub trait Storable {
|
||||
fn lookup_default(key: &[u8]) -> Self where Self: Sized + Default { Self::lookup(key).unwrap_or_else(Default::default) }
|
||||
fn lookup(_key: &[u8]) -> Option<Self> where Self: Sized { unimplemented!() }
|
||||
fn store(&self, key: &[u8]);
|
||||
}
|
||||
|
||||
pub fn kill(key: &[u8]) { runtime_support::set_storage(key, b""); }
|
||||
|
||||
impl<T: Default + Sized + EndianSensitive> Storable for T {
|
||||
fn lookup(key: &[u8]) -> Option<Self> {
|
||||
Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len())
|
||||
}
|
||||
fn store(&self, key: &[u8]) {
|
||||
self.as_slice_then(|slice| runtime_support::set_storage(key, slice));
|
||||
}
|
||||
}
|
||||
|
||||
impl Storable for [u8] {
|
||||
fn store(&self, key: &[u8]) {
|
||||
runtime_support::set_storage(key, self)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait to conveniently store a vector of storable data.
|
||||
// TODO: add iterator support
|
||||
pub trait StorageVec {
|
||||
type Item: Default + Sized + Storable;
|
||||
const PREFIX: &'static [u8];
|
||||
|
||||
/// Get the current set of items.
|
||||
fn items() -> Vec<Self::Item> {
|
||||
(0..Self::count()).into_iter().map(Self::item).collect()
|
||||
}
|
||||
|
||||
/// Set the current set of items.
|
||||
fn set_items(items: &[Self::Item]) {
|
||||
Self::set_count(items.len() as u32);
|
||||
items.iter().enumerate().for_each(|(v, ref i)| Self::set_item(v as u32, i));
|
||||
}
|
||||
|
||||
fn set_item(index: u32, item: &Self::Item) {
|
||||
item.store(&index.to_keyed_vec(Self::PREFIX));
|
||||
}
|
||||
|
||||
fn item(index: u32) -> Self::Item {
|
||||
Storable::lookup_default(&index.to_keyed_vec(Self::PREFIX))
|
||||
}
|
||||
|
||||
fn set_count(count: u32) {
|
||||
(count..Self::count()).for_each(|i| Self::set_item(i, &Self::Item::default()));
|
||||
count.store(&b"len".to_keyed_vec(Self::PREFIX));
|
||||
}
|
||||
|
||||
fn count() -> u32 {
|
||||
Storable::lookup_default(&b"len".to_keyed_vec(Self::PREFIX))
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use slicable::Slicable;
|
||||
use endiansensitive::EndianSensitive;
|
||||
use runtime_support;
|
||||
|
||||
pub trait Storage {
|
||||
fn into(key: &[u8]) -> Self where Self: Sized + Default { Self::try_into(key).unwrap_or_else(Default::default) }
|
||||
fn try_into(_key: &[u8]) -> Option<Self> where Self: Sized { unimplemented!() }
|
||||
fn store(&self, key: &[u8]);
|
||||
}
|
||||
|
||||
impl<T: Default + Sized + EndianSensitive> Storage for T {
|
||||
fn try_into(key: &[u8]) -> Option<Self> {
|
||||
Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len())
|
||||
}
|
||||
fn store(&self, key: &[u8]) {
|
||||
self.as_slice_then(|slice| runtime_support::set_storage(key, slice));
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage for [u8] {
|
||||
fn store(&self, key: &[u8]) {
|
||||
runtime_support::set_storage(key, self)
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
use runtime_support::Vec;
|
||||
use keyedvec::KeyedVec;
|
||||
use storage::Storage;
|
||||
|
||||
/// A trait to conveniently store a vector of storable data.
|
||||
// TODO: add iterator support
|
||||
pub trait StorageVec {
|
||||
type Item: Default + Sized + Storage;
|
||||
const PREFIX: &'static [u8];
|
||||
|
||||
/// Get the current set of items.
|
||||
fn items() -> Vec<Self::Item> {
|
||||
(0..Self::count()).into_iter().map(Self::item).collect()
|
||||
}
|
||||
|
||||
/// Set the current set of items.
|
||||
fn set_items(items: &[Self::Item]) {
|
||||
Self::set_count(items.len() as u32);
|
||||
items.iter().enumerate().for_each(|(v, ref i)| Self::set_item(v as u32, i));
|
||||
}
|
||||
|
||||
fn set_item(index: u32, item: &Self::Item) {
|
||||
item.store(&index.to_keyed_vec(Self::PREFIX));
|
||||
}
|
||||
|
||||
fn item(index: u32) -> Self::Item {
|
||||
Storage::into(&index.to_keyed_vec(Self::PREFIX))
|
||||
}
|
||||
|
||||
fn set_count(count: u32) {
|
||||
(count..Self::count()).for_each(|i| Self::set_item(i, &Self::Item::default()));
|
||||
count.store(&b"len".to_keyed_vec(Self::PREFIX));
|
||||
}
|
||||
|
||||
fn count() -> u32 {
|
||||
Storage::into(&b"len".to_keyed_vec(Self::PREFIX))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user