Skip to content

Commit

Permalink
revert Message::is_writable breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Apr 12, 2024
1 parent 6112279 commit a4771a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
40 changes: 18 additions & 22 deletions sdk/program/src/message/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -550,21 +550,22 @@ 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
&& i < self.account_keys.len()
- 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<Pubkey>) -> 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)
}

Expand All @@ -583,14 +584,11 @@ impl Message {
}

#[deprecated]
pub fn get_account_keys_by_lock_type(
&self,
reserved_account_keys: &HashSet<Pubkey>,
) -> (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);
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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])
);
}
Expand Down
6 changes: 5 additions & 1 deletion sdk/program/src/message/sanitized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
Self {
message: Cow::Owned(message),
Expand Down

0 comments on commit a4771a9

Please sign in to comment.