From a4771a96325a91bcca37d79cfbdb0320f8423145 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Fri, 12 Apr 2024 08:01:57 +0000 Subject: [PATCH] revert Message::is_writable breaking change --- sdk/program/src/message/legacy.rs | 40 +++++++++++++--------------- sdk/program/src/message/sanitized.rs | 6 ++++- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/sdk/program/src/message/legacy.rs b/sdk/program/src/message/legacy.rs index ae5a5fd184cccf..3d20e980828269 100644 --- a/sdk/program/src/message/legacy.rs +++ b/sdk/program/src/message/legacy.rs @@ -22,7 +22,7 @@ use { short_vec, system_instruction, system_program, sysvar, wasm_bindgen, }, lazy_static::lazy_static, - std::{collections::HashSet, convert::TryFrom, str::FromStr}, + std::{convert::TryFrom, str::FromStr}, }; lazy_static! { @@ -550,7 +550,7 @@ impl Message { /// Returns true if the account at the specified index was requested to be /// writable. This method should not be used directly. - fn is_writable_index(&self, i: usize) -> bool { + pub(super) fn is_writable_index(&self, i: usize) -> bool { i < (self.header.num_required_signatures - self.header.num_readonly_signed_accounts) as usize || (i >= self.header.num_required_signatures as usize @@ -558,13 +558,14 @@ impl Message { - self.header.num_readonly_unsigned_accounts as usize) } - /// Returns true if the account at the specified index should be write - /// locked when loaded for transaction processing in the runtime. This - /// method differs from `is_maybe_writable` because it is aware of the - /// latest reserved accounts which are not allowed to be write locked. - pub fn is_writable(&self, i: usize, reserved_account_keys: &HashSet) -> bool { + /// Returns true if the account at the specified index is writable by the + /// instructions in this message. Since the dynamic set of reserved accounts + /// isn't used here to demote write locks, this shouldn't be used in the + /// runtime. + #[deprecated(since = "2.0.0", note = "Please use `is_maybe_writable` instead")] + pub fn is_writable(&self, i: usize) -> bool { (self.is_writable_index(i)) - && !reserved_account_keys.contains(&self.account_keys[i]) + && !is_builtin_key_or_sysvar(&self.account_keys[i]) && !self.demote_program_id(i) } @@ -583,14 +584,11 @@ impl Message { } #[deprecated] - pub fn get_account_keys_by_lock_type( - &self, - reserved_account_keys: &HashSet, - ) -> (Vec<&Pubkey>, Vec<&Pubkey>) { + pub fn get_account_keys_by_lock_type(&self) -> (Vec<&Pubkey>, Vec<&Pubkey>) { let mut writable_keys = vec![]; let mut readonly_keys = vec![]; for (i, key) in self.account_keys.iter().enumerate() { - if self.is_writable(i, reserved_account_keys) { + if self.is_maybe_writable(i) { writable_keys.push(key); } else { readonly_keys.push(key); @@ -770,14 +768,12 @@ mod tests { recent_blockhash: Hash::default(), instructions: vec![], }; - - let reserved_keys = HashSet::from_iter([key2, key3]); - assert!(message.is_writable(0, &reserved_keys)); - assert!(!message.is_writable(1, &reserved_keys)); - assert!(!message.is_writable(2, &reserved_keys)); - assert!(!message.is_writable(3, &reserved_keys)); - assert!(message.is_writable(4, &reserved_keys)); - assert!(!message.is_writable(5, &reserved_keys)); + assert!(message.is_writable(0)); + assert!(!message.is_writable(1)); + assert!(!message.is_writable(2)); + assert!(message.is_writable(3)); + assert!(message.is_writable(4)); + assert!(!message.is_writable(5)); } #[test] @@ -805,7 +801,7 @@ mod tests { Some(&id1), ); assert_eq!( - message.get_account_keys_by_lock_type(&HashSet::default()), + message.get_account_keys_by_lock_type(), (vec![&id1, &id0], vec![&id3, &program_id, &id2]) ); } diff --git a/sdk/program/src/message/sanitized.rs b/sdk/program/src/message/sanitized.rs index ecf7590c28ef75..3f7b49662546f5 100644 --- a/sdk/program/src/message/sanitized.rs +++ b/sdk/program/src/message/sanitized.rs @@ -36,7 +36,11 @@ impl<'a> LegacyMessage<'a> { .account_keys .iter() .enumerate() - .map(|(i, _key)| message.is_writable(i, reserved_account_keys)) + .map(|(i, _key)| { + message.is_writable_index(i) + && !reserved_account_keys.contains(&message.account_keys[i]) + && !message.demote_program_id(i) + }) .collect::>(); Self { message: Cow::Owned(message),