Rename userdata to data (#3282)
* Rename userdata to data Instead of saying "userdata", which is ambiguous and imprecise, say "instruction data" or "account data". Also, add `ProgramError::InvalidInstructionData` Fixes #2761
This commit is contained in:
@@ -448,7 +448,7 @@ impl AccountsDB {
|
||||
// when squashing.
|
||||
let acc = &mut account.clone();
|
||||
if account.lamports == 0 {
|
||||
acc.userdata.resize(0, 0);
|
||||
acc.data.resize(0, 0);
|
||||
}
|
||||
|
||||
loop {
|
||||
@@ -1608,7 +1608,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn compare_account(account1: &Account, account2: &Account) -> bool {
|
||||
if account1.userdata != account2.userdata
|
||||
if account1.data != account2.data
|
||||
|| account1.owner != account2.owner
|
||||
|| account1.executable != account2.executable
|
||||
|| account1.lamports != account2.lamports
|
||||
|
@@ -33,7 +33,7 @@ fn get_account_size_static() -> usize {
|
||||
}
|
||||
|
||||
pub fn get_serialized_size(account: &Account) -> usize {
|
||||
get_account_size_static() + account.userdata.len()
|
||||
get_account_size_static() + account.data.len()
|
||||
}
|
||||
|
||||
pub fn serialize_account(dst_slice: &mut [u8], account: &Account, len: usize) {
|
||||
@@ -41,7 +41,7 @@ pub fn serialize_account(dst_slice: &mut [u8], account: &Account, len: usize) {
|
||||
|
||||
write_u64(&mut at, dst_slice, len as u64);
|
||||
write_u64(&mut at, dst_slice, account.lamports);
|
||||
write_bytes(&mut at, dst_slice, &account.userdata);
|
||||
write_bytes(&mut at, dst_slice, &account.data);
|
||||
write_bytes(&mut at, dst_slice, account.owner.as_ref());
|
||||
write_bytes(&mut at, dst_slice, &[account.executable as u8]);
|
||||
}
|
||||
@@ -83,9 +83,9 @@ pub fn deserialize_account(
|
||||
|
||||
let lamports = read_u64(&mut at, &src_slice);
|
||||
|
||||
let userdata_len = len - get_account_size_static();
|
||||
let mut userdata = vec![0; userdata_len];
|
||||
read_bytes(&mut at, &mut userdata, &src_slice, userdata_len);
|
||||
let data_len = len - get_account_size_static();
|
||||
let mut data = vec![0; data_len];
|
||||
read_bytes(&mut at, &mut data, &src_slice, data_len);
|
||||
|
||||
let mut pubkey = vec![0; mem::size_of::<Pubkey>()];
|
||||
read_bytes(&mut at, &mut pubkey, &src_slice, mem::size_of::<Pubkey>());
|
||||
@@ -97,7 +97,7 @@ pub fn deserialize_account(
|
||||
|
||||
Ok(Account {
|
||||
lamports,
|
||||
userdata,
|
||||
data,
|
||||
owner,
|
||||
executable,
|
||||
})
|
||||
@@ -256,7 +256,7 @@ pub mod tests {
|
||||
let v1 = vec![1u8; 32];
|
||||
let mut account1 = Account {
|
||||
lamports: 1,
|
||||
userdata: v1,
|
||||
data: v1,
|
||||
owner: Pubkey::default(),
|
||||
executable: false,
|
||||
};
|
||||
@@ -267,7 +267,7 @@ pub mod tests {
|
||||
let v2 = vec![4u8; 32];
|
||||
let mut account2 = Account {
|
||||
lamports: 1,
|
||||
userdata: v2,
|
||||
data: v2,
|
||||
owner: Pubkey::default(),
|
||||
executable: false,
|
||||
};
|
||||
@@ -277,13 +277,13 @@ pub mod tests {
|
||||
assert_eq!(av.get_account(index2).unwrap(), account2);
|
||||
assert_eq!(av.get_account(index1).unwrap(), account1);
|
||||
|
||||
account2.userdata.iter_mut().for_each(|e| *e *= 2);
|
||||
account2.data.iter_mut().for_each(|e| *e *= 2);
|
||||
let index3 = av.append_account(&account2).unwrap();
|
||||
len += get_serialized_size(&account2) + SIZEOF_U64 as usize;
|
||||
assert_eq!(index3, len as u64);
|
||||
assert_eq!(av.get_account(index3).unwrap(), account2);
|
||||
|
||||
account1.userdata.extend([1, 2, 3, 4, 5, 6].iter().cloned());
|
||||
account1.data.extend([1, 2, 3, 4, 5, 6].iter().cloned());
|
||||
let index4 = av.append_account(&account1).unwrap();
|
||||
len += get_serialized_size(&account2) + SIZEOF_U64 as usize;
|
||||
assert_eq!(index4, len as u64);
|
||||
|
@@ -338,7 +338,7 @@ impl Bank {
|
||||
// will be forced to select it as the leader for height 0
|
||||
let mut bootstrap_leader_vote_account = Account {
|
||||
lamports: bootstrap_leader_stake,
|
||||
userdata: vec![0; VoteState::max_size() as usize],
|
||||
data: vec![0; VoteState::max_size() as usize],
|
||||
owner: solana_vote_api::id(),
|
||||
executable: false,
|
||||
};
|
||||
@@ -346,7 +346,7 @@ impl Bank {
|
||||
let mut vote_state = VoteState::new(&genesis_block.bootstrap_leader_id);
|
||||
vote_state.votes.push_back(Lockout::new(&Vote::new(0)));
|
||||
vote_state
|
||||
.serialize(&mut bootstrap_leader_vote_account.userdata)
|
||||
.serialize(&mut bootstrap_leader_vote_account.data)
|
||||
.unwrap();
|
||||
|
||||
self.accounts().store_slow(
|
||||
@@ -988,12 +988,12 @@ mod tests {
|
||||
let instructions = vec![
|
||||
Instruction {
|
||||
program_ids_index: 0,
|
||||
userdata: serialize(&spend).unwrap(),
|
||||
data: serialize(&spend).unwrap(),
|
||||
accounts: vec![0, 1],
|
||||
},
|
||||
Instruction {
|
||||
program_ids_index: 0,
|
||||
userdata: serialize(&spend).unwrap(),
|
||||
data: serialize(&spend).unwrap(),
|
||||
accounts: vec![0, 2],
|
||||
},
|
||||
];
|
||||
@@ -1541,7 +1541,7 @@ mod tests {
|
||||
accounts
|
||||
.iter()
|
||||
.filter_map(|(pubkey, account)| {
|
||||
if let Ok(vote_state) = VoteState::deserialize(&account.userdata) {
|
||||
if let Ok(vote_state) = VoteState::deserialize(&account.data) {
|
||||
if vote_state.delegate_id == leader_id {
|
||||
Some((*pubkey, true))
|
||||
} else {
|
||||
|
@@ -49,13 +49,13 @@ fn create_path(name: &str) -> PathBuf {
|
||||
pub fn entrypoint(
|
||||
program_id: &Pubkey,
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
ix_userdata: &[u8],
|
||||
ix_data: &[u8],
|
||||
tick_height: u64,
|
||||
) -> Result<(), ProgramError> {
|
||||
if keyed_accounts[0].account.executable {
|
||||
// dispatch it
|
||||
let (names, params) = keyed_accounts.split_at_mut(1);
|
||||
let name = &names[0].account.userdata;
|
||||
let name = &names[0].account.data;
|
||||
let name = match str::from_utf8(name) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
@@ -80,14 +80,14 @@ pub fn entrypoint(
|
||||
return Err(ProgramError::GenericError);
|
||||
}
|
||||
};
|
||||
return entrypoint(program_id, params, ix_userdata, tick_height);
|
||||
return entrypoint(program_id, params, ix_data, tick_height);
|
||||
},
|
||||
Err(e) => {
|
||||
warn!("Unable to load: {:?}", e);
|
||||
return Err(ProgramError::GenericError);
|
||||
}
|
||||
}
|
||||
} else if let Ok(instruction) = deserialize(ix_userdata) {
|
||||
} else if let Ok(instruction) = deserialize(ix_data) {
|
||||
if keyed_accounts[0].signer_key().is_none() {
|
||||
warn!("key[0] did not sign the transaction");
|
||||
return Err(ProgramError::GenericError);
|
||||
@@ -96,16 +96,16 @@ pub fn entrypoint(
|
||||
LoaderInstruction::Write { offset, bytes } => {
|
||||
trace!("NativeLoader::Write offset {} bytes {:?}", offset, bytes);
|
||||
let offset = offset as usize;
|
||||
if keyed_accounts[0].account.userdata.len() < offset + bytes.len() {
|
||||
if keyed_accounts[0].account.data.len() < offset + bytes.len() {
|
||||
warn!(
|
||||
"Error: Overflow, {} < {}",
|
||||
keyed_accounts[0].account.userdata.len(),
|
||||
keyed_accounts[0].account.data.len(),
|
||||
offset + bytes.len()
|
||||
);
|
||||
return Err(ProgramError::GenericError);
|
||||
}
|
||||
// native loader takes a name and we assume it all comes in at once
|
||||
keyed_accounts[0].account.userdata = bytes;
|
||||
keyed_accounts[0].account.data = bytes;
|
||||
}
|
||||
|
||||
LoaderInstruction::Finalize => {
|
||||
@@ -117,7 +117,7 @@ pub fn entrypoint(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
warn!("Invalid userdata in instruction: {:?}", ix_userdata);
|
||||
warn!("Invalid data in instruction: {:?}", ix_data);
|
||||
return Err(ProgramError::GenericError);
|
||||
}
|
||||
Ok(())
|
||||
|
@@ -41,14 +41,14 @@ fn process_instruction(
|
||||
crate::system_program::entrypoint(
|
||||
&program_id,
|
||||
&mut keyed_accounts[1..],
|
||||
&tx.instructions[instruction_index].userdata,
|
||||
&tx.instructions[instruction_index].data,
|
||||
tick_height,
|
||||
)
|
||||
} else {
|
||||
native_loader::entrypoint(
|
||||
&program_id,
|
||||
&mut keyed_accounts,
|
||||
&tx.instructions[instruction_index].userdata,
|
||||
&tx.instructions[instruction_index].data,
|
||||
tick_height,
|
||||
)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ fn verify_instruction(
|
||||
program_id: &Pubkey,
|
||||
pre_program_id: &Pubkey,
|
||||
pre_lamports: u64,
|
||||
pre_userdata: &[u8],
|
||||
pre_data: &[u8],
|
||||
account: &Account,
|
||||
) -> Result<(), ProgramError> {
|
||||
// Verify the transaction
|
||||
@@ -71,12 +71,12 @@ fn verify_instruction(
|
||||
if *program_id != account.owner && pre_lamports > account.lamports {
|
||||
return Err(ProgramError::ExternalAccountLamportSpend);
|
||||
}
|
||||
// For accounts unassigned to the program, the userdata may not change.
|
||||
// For accounts unassigned to the program, the data may not change.
|
||||
if *program_id != account.owner
|
||||
&& !system_program::check_id(&program_id)
|
||||
&& pre_userdata != &account.userdata[..]
|
||||
&& pre_data != &account.data[..]
|
||||
{
|
||||
return Err(ProgramError::ExternalAccountUserdataModified);
|
||||
return Err(ProgramError::ExternalAccountDataModified);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -98,7 +98,7 @@ fn execute_instruction(
|
||||
let pre_total: u64 = program_accounts.iter().map(|a| a.lamports).sum();
|
||||
let pre_data: Vec<_> = program_accounts
|
||||
.iter_mut()
|
||||
.map(|a| (a.owner, a.lamports, a.userdata.clone()))
|
||||
.map(|a| (a.owner, a.lamports, a.data.clone()))
|
||||
.collect();
|
||||
|
||||
process_instruction(
|
||||
@@ -111,14 +111,14 @@ fn execute_instruction(
|
||||
.map_err(verify_error)?;
|
||||
|
||||
// Verify the instruction
|
||||
for ((pre_program_id, pre_lamports, pre_userdata), post_account) in
|
||||
for ((pre_program_id, pre_lamports, pre_data), post_account) in
|
||||
pre_data.iter().zip(program_accounts.iter())
|
||||
{
|
||||
verify_instruction(
|
||||
&program_id,
|
||||
pre_program_id,
|
||||
*pre_lamports,
|
||||
pre_userdata,
|
||||
pre_data,
|
||||
post_account,
|
||||
)?;
|
||||
}
|
||||
@@ -218,10 +218,10 @@ where
|
||||
|
||||
let program_id = tx.program_id(i);
|
||||
if system_program::check_id(&program_id) {
|
||||
crate::system_program::entrypoint(&program_id, &mut keyed_accounts, &ix.userdata, 0)
|
||||
crate::system_program::entrypoint(&program_id, &mut keyed_accounts, &ix.data, 0)
|
||||
.unwrap();
|
||||
} else {
|
||||
process_instruction(&program_id, &mut keyed_accounts, &ix.userdata)?;
|
||||
process_instruction(&program_id, &mut keyed_accounts, &ix.data)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -294,8 +294,8 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_verify_instruction_change_userdata() {
|
||||
fn change_userdata(program_id: &Pubkey) -> Result<(), ProgramError> {
|
||||
fn test_verify_instruction_change_data() {
|
||||
fn change_data(program_id: &Pubkey) -> Result<(), ProgramError> {
|
||||
let alice_program_id = Keypair::new().pubkey();
|
||||
let account = Account::new(0, 0, &alice_program_id);
|
||||
verify_instruction(&program_id, &alice_program_id, 0, &[42], &account)
|
||||
@@ -305,14 +305,14 @@ mod tests {
|
||||
let mallory_program_id = Keypair::new().pubkey();
|
||||
|
||||
assert_eq!(
|
||||
change_userdata(&system_program_id),
|
||||
change_data(&system_program_id),
|
||||
Ok(()),
|
||||
"system program should be able to change the userdata"
|
||||
"system program should be able to change the data"
|
||||
);
|
||||
assert_eq!(
|
||||
change_userdata(&mallory_program_id),
|
||||
Err(ProgramError::ExternalAccountUserdataModified),
|
||||
"malicious Mallory should not be able to change the account userdata"
|
||||
change_data(&mallory_program_id),
|
||||
Err(ProgramError::ExternalAccountDataModified),
|
||||
"malicious Mallory should not be able to change the account data"
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -28,7 +28,7 @@ fn create_system_account(
|
||||
Err(SystemError::SourceNotSystemAccount)?;
|
||||
}
|
||||
|
||||
if !keyed_accounts[TO_ACCOUNT_INDEX].account.userdata.is_empty()
|
||||
if !keyed_accounts[TO_ACCOUNT_INDEX].account.data.is_empty()
|
||||
|| !system_program::check_id(&keyed_accounts[TO_ACCOUNT_INDEX].account.owner)
|
||||
{
|
||||
info!(
|
||||
@@ -47,7 +47,7 @@ fn create_system_account(
|
||||
keyed_accounts[FROM_ACCOUNT_INDEX].account.lamports -= lamports;
|
||||
keyed_accounts[TO_ACCOUNT_INDEX].account.lamports += lamports;
|
||||
keyed_accounts[TO_ACCOUNT_INDEX].account.owner = *program_id;
|
||||
keyed_accounts[TO_ACCOUNT_INDEX].account.userdata = vec![0; space as usize];
|
||||
keyed_accounts[TO_ACCOUNT_INDEX].account.data = vec![0; space as usize];
|
||||
keyed_accounts[TO_ACCOUNT_INDEX].account.executable = false;
|
||||
Ok(())
|
||||
}
|
||||
@@ -110,8 +110,8 @@ pub fn entrypoint(
|
||||
SystemInstruction::Move { lamports } => move_lamports(keyed_accounts, lamports),
|
||||
}
|
||||
} else {
|
||||
info!("Invalid transaction instruction userdata: {:?}", data);
|
||||
Err(ProgramError::InvalidUserdata)
|
||||
info!("Invalid instruction data: {:?}", data);
|
||||
Err(ProgramError::InvalidInstructionData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,11 +138,11 @@ mod tests {
|
||||
let from_lamports = from_account.lamports;
|
||||
let to_lamports = to_account.lamports;
|
||||
let to_owner = to_account.owner;
|
||||
let to_userdata = to_account.userdata.clone();
|
||||
let to_data = to_account.data.clone();
|
||||
assert_eq!(from_lamports, 50);
|
||||
assert_eq!(to_lamports, 50);
|
||||
assert_eq!(to_owner, new_program_owner);
|
||||
assert_eq!(to_userdata, [0, 0]);
|
||||
assert_eq!(to_data, [0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -191,8 +191,8 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_userdata_populated() {
|
||||
// Attempt to create system account in account with populated userdata
|
||||
fn test_create_data_populated() {
|
||||
// Attempt to create system account in account with populated data
|
||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
||||
let from = Keypair::new().pubkey();
|
||||
let mut from_account = Account::new(100, 0, &system_program::id());
|
||||
@@ -200,7 +200,7 @@ mod tests {
|
||||
let populated_key = Keypair::new().pubkey();
|
||||
let mut populated_account = Account {
|
||||
lamports: 0,
|
||||
userdata: vec![0, 1, 2, 3],
|
||||
data: vec![0, 1, 2, 3],
|
||||
owner: Pubkey::default(),
|
||||
executable: false,
|
||||
};
|
||||
|
Reference in New Issue
Block a user