Write account path impl ReadableAccount (#16779)

This commit is contained in:
Jeff Washington (jwash) 2021-04-28 15:29:22 -05:00 committed by GitHub
parent 783bd79e9d
commit f533d3be77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 10 deletions

View File

@ -3338,7 +3338,7 @@ impl AccountsDb {
slot: Slot,
hashes: &[impl Borrow<Hash>],
mut storage_finder: F,
accounts_and_meta_to_store: &[(StoredMeta, Option<&AccountSharedData>)],
accounts_and_meta_to_store: &[(StoredMeta, Option<&impl ReadableAccount>)],
) -> Vec<AccountInfo> {
assert_eq!(hashes.len(), accounts_and_meta_to_store.len());
let mut infos: Vec<AccountInfo> = Vec::with_capacity(accounts_and_meta_to_store.len());
@ -3725,7 +3725,7 @@ impl AccountsDb {
&self,
slot: Slot,
hashes: Option<&[impl Borrow<Hash>]>,
accounts_and_meta_to_store: &[(StoredMeta, Option<&AccountSharedData>)],
accounts_and_meta_to_store: &[(StoredMeta, Option<&impl ReadableAccount>)],
) -> Vec<AccountInfo> {
let len = accounts_and_meta_to_store.len();
let hashes = hashes.map(|hashes| {
@ -3739,8 +3739,9 @@ impl AccountsDb {
.map(|(i, (meta, account))| {
let hash = hashes.map(|hashes| hashes[i].borrow());
let account = account.cloned().unwrap_or_default();
let account = account
.map(|account| account.to_account_shared_data())
.unwrap_or_default();
let account_info = AccountInfo {
store_id: CACHE_VIRTUAL_STORAGE_ID,
offset: CACHE_VIRTUAL_OFFSET,
@ -3767,13 +3768,13 @@ impl AccountsDb {
>(
&self,
slot: Slot,
accounts: &[(&Pubkey, &AccountSharedData)],
accounts: &[(&Pubkey, &impl ReadableAccount)],
hashes: Option<&[impl Borrow<Hash>]>,
storage_finder: F,
mut write_version_producer: P,
is_cached_store: bool,
) -> Vec<AccountInfo> {
let accounts_and_meta_to_store: Vec<(StoredMeta, Option<&AccountSharedData>)> = accounts
let accounts_and_meta_to_store: Vec<_> = accounts
.iter()
.map(|(pubkey, account)| {
self.read_only_accounts_cache.remove(pubkey, slot);
@ -4322,7 +4323,7 @@ impl AccountsDb {
&self,
slot: Slot,
infos: Vec<AccountInfo>,
accounts: &[(&Pubkey, &AccountSharedData)],
accounts: &[(&Pubkey, &impl ReadableAccount)],
) -> SlotList<AccountInfo> {
let mut reclaims = SlotList::<AccountInfo>::with_capacity(infos.len() * 2);
for (info, pubkey_account) in infos.into_iter().zip(accounts.iter()) {
@ -4743,7 +4744,7 @@ impl AccountsDb {
fn store_accounts_frozen<'a>(
&'a self,
slot: Slot,
accounts: &[(&Pubkey, &AccountSharedData)],
accounts: &[(&Pubkey, &impl ReadableAccount)],
hashes: Option<&[impl Borrow<Hash>]>,
storage_finder: Option<StorageFinder<'a>>,
write_version_producer: Option<Box<dyn Iterator<Item = u64>>>,
@ -4767,7 +4768,7 @@ impl AccountsDb {
fn store_accounts_custom<'a>(
&'a self,
slot: Slot,
accounts: &[(&Pubkey, &AccountSharedData)],
accounts: &[(&Pubkey, &impl ReadableAccount)],
hashes: Option<&[impl Borrow<Hash>]>,
storage_finder: Option<StorageFinder<'a>>,
write_version_producer: Option<Box<dyn Iterator<Item = u64>>>,

View File

@ -462,7 +462,7 @@ impl AppendVec {
/// and will be available to other threads.
pub fn append_accounts(
&self,
accounts: &[(StoredMeta, Option<&AccountSharedData>)],
accounts: &[(StoredMeta, Option<&impl ReadableAccount>)],
hashes: &[impl Borrow<Hash>],
) -> Vec<usize> {
let _lock = self.append_lock.lock().unwrap();

View File

@ -116,6 +116,15 @@ pub trait ReadableAccount: Sized {
fn owner(&self) -> &Pubkey;
fn executable(&self) -> bool;
fn rent_epoch(&self) -> Epoch;
fn to_account_shared_data(&self) -> AccountSharedData {
AccountSharedData::create(
self.lamports(),
self.data().to_vec(),
*self.owner(),
self.executable(),
self.rent_epoch(),
)
}
}
impl ReadableAccount for Account {
@ -668,6 +677,17 @@ pub mod tests {
account2.serialize_data(&"hello world").unwrap();
}
#[test]
fn test_to_account_shared_data() {
let key = Pubkey::new_unique();
let (account1, account2) = make_two_accounts(&key);
assert!(accounts_equal(&account1, &account2));
let account3 = account1.to_account_shared_data();
let account4 = account2.to_account_shared_data();
assert!(accounts_equal(&account1, &account3));
assert!(accounts_equal(&account1, &account4));
}
#[test]
fn test_account_shared_data() {
let key = Pubkey::new_unique();