// Copyright 2018 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . //! Auxilliaries to help with managing partial changes to accounts state. use super::{CodeOf, StorageOf, Trait}; use double_map::StorageDoubleMap; use rstd::cell::RefCell; use rstd::collections::btree_map::{BTreeMap, Entry}; use rstd::prelude::*; use runtime_support::StorageMap; use {balances, system}; pub struct ChangeEntry { balance: Option, code: Option>, storage: BTreeMap, Option>>, } // Cannot derive(Default) since it erroneously bounds T by Default. impl Default for ChangeEntry { fn default() -> Self { ChangeEntry { balance: Default::default(), code: Default::default(), storage: Default::default(), } } } pub type ChangeSet = BTreeMap<::AccountId, ChangeEntry>; pub trait AccountDb { fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option>; fn get_code(&self, account: &T::AccountId) -> Vec; fn get_balance(&self, account: &T::AccountId) -> T::Balance; fn commit(&mut self, change_set: ChangeSet); } pub struct DirectAccountDb; impl AccountDb for DirectAccountDb { fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option> { >::get(account.clone(), location.to_vec()) } fn get_code(&self, account: &T::AccountId) -> Vec { >::get(account) } fn get_balance(&self, account: &T::AccountId) -> T::Balance { balances::Module::::free_balance(account) } fn commit(&mut self, s: ChangeSet) { for (address, changed) in s.into_iter() { if let Some(balance) = changed.balance { if let balances::UpdateBalanceOutcome::AccountKilled = balances::Module::::set_free_balance_creating(&address, balance) { // Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback // which will make removal of CodeOf and StorageOf for this account. // In order to avoid writing over the deleted properties we `continue` here. continue; } } if let Some(code) = changed.code { >::insert(&address, &code); } for (k, v) in changed.storage.into_iter() { if let Some(value) = v { >::insert(address.clone(), k, value); } else { >::remove(address.clone(), k); } } } } } pub struct OverlayAccountDb<'a, T: Trait + 'a> { local: RefCell>, underlying: &'a AccountDb, } impl<'a, T: Trait> OverlayAccountDb<'a, T> { pub fn new(underlying: &'a AccountDb) -> OverlayAccountDb<'a, T> { OverlayAccountDb { local: RefCell::new(ChangeSet::new()), underlying, } } pub fn into_change_set(self) -> ChangeSet { self.local.into_inner() } pub fn set_storage( &mut self, account: &T::AccountId, location: Vec, value: Option>, ) { self.local .borrow_mut() .entry(account.clone()) .or_insert(Default::default()) .storage .insert(location, value); } pub fn set_code(&mut self, account: &T::AccountId, code: Vec) { self.local .borrow_mut() .entry(account.clone()) .or_insert(Default::default()) .code = Some(code); } pub fn set_balance(&mut self, account: &T::AccountId, balance: T::Balance) { self.local .borrow_mut() .entry(account.clone()) .or_insert(Default::default()) .balance = Some(balance); } } impl<'a, T: Trait> AccountDb for OverlayAccountDb<'a, T> { fn get_storage(&self, account: &T::AccountId, location: &[u8]) -> Option> { self.local .borrow() .get(account) .and_then(|a| a.storage.get(location)) .cloned() .unwrap_or_else(|| self.underlying.get_storage(account, location)) } fn get_code(&self, account: &T::AccountId) -> Vec { self.local .borrow() .get(account) .and_then(|a| a.code.clone()) .unwrap_or_else(|| self.underlying.get_code(account)) } fn get_balance(&self, account: &T::AccountId) -> T::Balance { self.local .borrow() .get(account) .and_then(|a| a.balance) .unwrap_or_else(|| self.underlying.get_balance(account)) } fn commit(&mut self, s: ChangeSet) { let mut local = self.local.borrow_mut(); for (address, changed) in s.into_iter() { match local.entry(address) { Entry::Occupied(e) => { let mut value = e.into_mut(); if changed.balance.is_some() { value.balance = changed.balance; } if changed.code.is_some() { value.code = changed.code; } value.storage.extend(changed.storage.into_iter()); } Entry::Vacant(e) => { e.insert(changed); } } } } }