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:
Greg Fitzgerald
2019-03-14 10:48:27 -06:00
committed by GitHub
parent de13082347
commit c1eec0290e
42 changed files with 245 additions and 254 deletions

View File

@ -1,39 +1,36 @@
use crate::pubkey::Pubkey;
use std::{cmp, fmt};
/// An Account with userdata that is stored on chain
/// An Account with data that is stored on chain
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Default, Eq, PartialEq)]
pub struct Account {
/// lamports in the account
pub lamports: u64,
/// data held in this account
pub userdata: Vec<u8>,
pub data: Vec<u8>,
/// the program that owns this account. If executable, the program that loads this account.
pub owner: Pubkey,
/// this account's userdata contains a loaded program (and is now read-only)
/// this account's data contains a loaded program (and is now read-only)
pub executable: bool,
}
impl fmt::Debug for Account {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let userdata_len = cmp::min(64, self.userdata.len());
let userdata_str = if userdata_len > 0 {
format!(
" userdata: {}",
hex::encode(self.userdata[..userdata_len].to_vec())
)
let data_len = cmp::min(64, self.data.len());
let data_str = if data_len > 0 {
format!(" data: {}", hex::encode(self.data[..data_len].to_vec()))
} else {
"".to_string()
};
write!(
f,
"Account {{ lamports: {} userdata.len: {} owner: {} executable: {}{} }}",
"Account {{ lamports: {} data.len: {} owner: {} executable: {}{} }}",
self.lamports,
self.userdata.len(),
self.data.len(),
self.owner,
self.executable,
userdata_str,
data_str,
)
}
}
@ -43,7 +40,7 @@ impl Account {
pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Account {
Account {
lamports,
userdata: vec![0u8; space],
data: vec![0u8; space],
owner: *owner,
executable: false,
}

View File

@ -18,7 +18,7 @@ pub fn create_program_account(name: &str) -> Account {
Account {
lamports: 1,
owner: id(),
userdata: name.as_bytes().to_vec(),
data: name.as_bytes().to_vec(),
executable: true,
}
}

View File

@ -11,11 +11,14 @@ pub enum ProgramError {
/// The arguments provided to a program instruction where invalid
InvalidArgument,
/// An account's userdata contents was invalid
InvalidUserdata,
/// An instruction's data contents was invalid
InvalidInstructionData,
/// An account's userdata was too small
UserdataTooSmall,
/// An account's data contents was invalid
InvalidAccountData,
/// An account's data was too small
AccountDataTooSmall,
/// The account did not have the expected program id
IncorrectProgramId,

View File

@ -32,8 +32,8 @@ pub enum InstructionError {
/// Program spent the lamports of an account that doesn't belong to it
ExternalAccountLamportSpend,
/// Program modified the userdata of an account that doesn't belong to it
ExternalAccountUserdataModified,
/// Program modified the data of an account that doesn't belong to it
ExternalAccountDataModified,
}
impl InstructionError {
@ -52,15 +52,15 @@ pub struct Instruction<P, Q> {
/// Ordered indices into the transaction keys array indicating which accounts to pass to the program
pub accounts: Vec<Q>,
/// The program input data
pub userdata: Vec<u8>,
pub data: Vec<u8>,
}
impl<P, Q> Instruction<P, Q> {
pub fn new<T: Serialize>(program_ids_index: P, userdata: &T, accounts: Vec<Q>) -> Self {
let userdata = serialize(userdata).unwrap();
pub fn new<T: Serialize>(program_ids_index: P, data: &T, accounts: Vec<Q>) -> Self {
let data = serialize(data).unwrap();
Self {
program_ids_index,
userdata,
data,
accounts,
}
}
@ -70,7 +70,7 @@ impl Instruction<u8, u8> {
pub fn serialize_with(mut writer: &mut Cursor<&mut [u8]>, ix: &Self) -> Result<(), Error> {
writer.write_all(&[ix.program_ids_index])?;
serialize_vec_bytes(&mut writer, &ix.accounts[..])?;
serialize_vec_bytes(&mut writer, &ix.userdata[..])?;
serialize_vec_bytes(&mut writer, &ix.data[..])?;
Ok(())
}
@ -79,11 +79,11 @@ impl Instruction<u8, u8> {
reader.read_exact(&mut buf)?;
let program_ids_index = buf[0];
let accounts = deserialize_vec_bytes(&mut reader)?;
let userdata = deserialize_vec_bytes(&mut reader)?;
let data = deserialize_vec_bytes(&mut reader)?;
Ok(Instruction {
program_ids_index,
accounts,
userdata,
data,
})
}
@ -96,7 +96,7 @@ impl Instruction<u8, u8> {
encode_len(&mut wr, len)?;
size += wr.position() as usize + (len * size_of::<u8>());
let len = self.userdata.len();
let len = self.data.len();
wr.set_position(0);
encode_len(&mut wr, len)?;
size += wr.position() as usize + (len * size_of::<u8>());
@ -164,13 +164,13 @@ impl Transaction {
from_keypair: &T,
transaction_keys: &[Pubkey],
program_id: &Pubkey,
userdata: &S,
data: &S,
recent_blockhash: Hash,
fee: u64,
) -> Self {
let program_ids = vec![*program_id];
let accounts = (0..=transaction_keys.len() as u8).collect();
let instructions = vec![Instruction::new(0, userdata, accounts)];
let instructions = vec![Instruction::new(0, data, accounts)];
Self::new_with_instructions(
&[from_keypair],
transaction_keys,
@ -184,13 +184,13 @@ impl Transaction {
from_pubkey: &Pubkey,
transaction_keys: &[Pubkey],
program_id: &Pubkey,
userdata: &T,
data: &T,
recent_blockhash: Hash,
fee: u64,
) -> Self {
let program_ids = vec![*program_id];
let accounts = (0..=transaction_keys.len() as u8).collect();
let instructions = vec![Instruction::new(0, userdata, accounts)];
let instructions = vec![Instruction::new(0, data, accounts)];
let mut keys = vec![*from_pubkey];
keys.extend_from_slice(transaction_keys);
Self::new_with_instructions::<Keypair>(
@ -234,8 +234,8 @@ impl Transaction {
tx.sign(from_keypairs, recent_blockhash);
tx
}
pub fn userdata(&self, instruction_index: usize) -> &[u8] {
&self.instructions[instruction_index].userdata
pub fn data(&self, instruction_index: usize) -> &[u8] {
&self.instructions[instruction_index].data
}
fn key_index(&self, instruction_index: usize, accounts_index: usize) -> Option<usize> {
@ -595,7 +595,7 @@ mod tests {
assert_eq!(req_size, size);
}
/// Detect binary changes in the serialized transaction userdata, which could have a downstream
/// Detect binary changes in the serialized transaction data, which could have a downstream
/// affect on SDKs and DApps
#[test]
fn test_sdk_serialize() {

View File

@ -20,7 +20,7 @@ fn compile_instruction(
let accounts: Vec<_> = ix.accounts.iter().map(|(k, _)| position(keys, k)).collect();
Instruction {
program_ids_index: position(program_ids, &ix.program_ids_index),
userdata: ix.userdata.clone(),
data: ix.data.clone(),
accounts,
}
}