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

@ -12,8 +12,8 @@ pub enum BudgetError {
UninitializedContract,
DestinationMissing,
FailedWitness,
UserdataTooSmall,
UserdataDeserializeFailure,
AccountDataTooSmall,
AccountDataDeserializeFailure,
UnsignedKey,
}
@ -37,7 +37,7 @@ impl BudgetState {
pub fn serialize(&self, output: &mut [u8]) -> Result<(), BudgetError> {
serialize_into(output, self).map_err(|err| match *err {
_ => BudgetError::UserdataTooSmall,
_ => BudgetError::AccountDataTooSmall,
})
}
@ -56,18 +56,18 @@ mod test {
fn test_serializer() {
let mut a = Account::new(0, 512, &id());
let b = BudgetState::default();
b.serialize(&mut a.userdata).unwrap();
let c = BudgetState::deserialize(&a.userdata).unwrap();
b.serialize(&mut a.data).unwrap();
let c = BudgetState::deserialize(&a.data).unwrap();
assert_eq!(b, c);
}
#[test]
fn test_serializer_userdata_too_small() {
fn test_serializer_data_too_small() {
let mut a = Account::new(0, 1, &id());
let b = BudgetState::default();
assert_eq!(
b.serialize(&mut a.userdata),
Err(BudgetError::UserdataTooSmall)
b.serialize(&mut a.data),
Err(BudgetError::AccountDataTooSmall)
);
}
}

View File

@ -151,11 +151,11 @@ impl BudgetTransaction {
}
pub fn system_instruction(tx: &Transaction, index: usize) -> Option<SystemInstruction> {
deserialize(&tx.userdata(index)).ok()
deserialize(&tx.data(index)).ok()
}
pub fn instruction(tx: &Transaction, index: usize) -> Option<BudgetInstruction> {
deserialize(&tx.userdata(index)).ok()
deserialize(&tx.data(index)).ok()
}
/// Verify only the payment plan.
@ -236,9 +236,9 @@ mod tests {
payment.lamports = *lamports; // <-- attack, part 2!
}
}
tx.instructions[1].userdata = serialize(&instruction).unwrap();
tx.instructions[1].data = serialize(&instruction).unwrap();
}
tx.instructions[0].userdata = serialize(&system_instruction).unwrap();
tx.instructions[0].data = serialize(&system_instruction).unwrap();
assert!(BudgetTransaction::verify_plan(&tx));
assert!(!tx.verify_signature());
}
@ -257,7 +257,7 @@ mod tests {
payment.to = thief_keypair.pubkey(); // <-- attack!
}
}
tx.instructions[1].userdata = serialize(&instruction).unwrap();
tx.instructions[1].data = serialize(&instruction).unwrap();
assert!(BudgetTransaction::verify_plan(&tx));
assert!(!tx.verify_signature());
}
@ -274,7 +274,7 @@ mod tests {
payment.lamports = 2; // <-- attack!
}
}
tx.instructions[1].userdata = serialize(&instruction).unwrap();
tx.instructions[1].data = serialize(&instruction).unwrap();
assert!(!BudgetTransaction::verify_plan(&tx));
// Also, ensure all branchs of the plan spend all lamports
@ -284,7 +284,7 @@ mod tests {
payment.lamports = 0; // <-- whoops!
}
}
tx.instructions[1].userdata = serialize(&instruction).unwrap();
tx.instructions[1].data = serialize(&instruction).unwrap();
assert!(!BudgetTransaction::verify_plan(&tx));
}
}