Rename program_id to owner in system instructions (#10069)
This commit is contained in:
parent
bac4aec16f
commit
bbc549f592
|
@ -150,7 +150,7 @@ mod tests {
|
||||||
SystemInstruction::CreateAccount {
|
SystemInstruction::CreateAccount {
|
||||||
lamports: _,
|
lamports: _,
|
||||||
space,
|
space,
|
||||||
program_id: _,
|
owner: _,
|
||||||
} => space,
|
} => space,
|
||||||
_ => panic!("Not a CreateAccount system instruction"),
|
_ => panic!("Not a CreateAccount system instruction"),
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,9 +32,9 @@ impl Address {
|
||||||
address: &Pubkey,
|
address: &Pubkey,
|
||||||
with_seed: Option<(&Pubkey, &str, &Pubkey)>,
|
with_seed: Option<(&Pubkey, &str, &Pubkey)>,
|
||||||
) -> Result<Self, InstructionError> {
|
) -> Result<Self, InstructionError> {
|
||||||
let base = if let Some((base, seed, program_id)) = with_seed {
|
let base = if let Some((base, seed, owner)) = with_seed {
|
||||||
// re-derive the address, must match the supplied address
|
// re-derive the address, must match the supplied address
|
||||||
if *address != Pubkey::create_with_seed(base, seed, program_id)? {
|
if *address != Pubkey::create_with_seed(base, seed, owner)? {
|
||||||
return Err(SystemError::AddressWithSeedMismatch.into());
|
return Err(SystemError::AddressWithSeedMismatch.into());
|
||||||
}
|
}
|
||||||
Some(*base)
|
Some(*base)
|
||||||
|
@ -86,11 +86,11 @@ fn allocate(
|
||||||
fn assign(
|
fn assign(
|
||||||
account: &mut Account,
|
account: &mut Account,
|
||||||
address: &Address,
|
address: &Address,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
signers: &HashSet<Pubkey>,
|
signers: &HashSet<Pubkey>,
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
// no work to do, just return
|
// no work to do, just return
|
||||||
if account.owner == *program_id {
|
if account.owner == *owner {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,12 +100,12 @@ fn assign(
|
||||||
}
|
}
|
||||||
|
|
||||||
// guard against sysvars being made
|
// guard against sysvars being made
|
||||||
if sysvar::check_id(&program_id) {
|
if sysvar::check_id(&owner) {
|
||||||
debug!("Assign: program id {} invalid", program_id);
|
debug!("Assign: program id {} invalid", owner);
|
||||||
return Err(SystemError::InvalidProgramId.into());
|
return Err(SystemError::InvalidProgramId.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
account.owner = *program_id;
|
account.owner = *owner;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,11 +113,11 @@ fn allocate_and_assign(
|
||||||
to: &mut Account,
|
to: &mut Account,
|
||||||
to_address: &Address,
|
to_address: &Address,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
signers: &HashSet<Pubkey>,
|
signers: &HashSet<Pubkey>,
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
allocate(to, to_address, space, signers)?;
|
allocate(to, to_address, space, signers)?;
|
||||||
assign(to, to_address, program_id, signers)
|
assign(to, to_address, owner, signers)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_account(
|
fn create_account(
|
||||||
|
@ -126,10 +126,10 @@ fn create_account(
|
||||||
to_address: &Address,
|
to_address: &Address,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
signers: &HashSet<Pubkey>,
|
signers: &HashSet<Pubkey>,
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
allocate_and_assign(to, to_address, space, program_id, signers)?;
|
allocate_and_assign(to, to_address, space, owner, signers)?;
|
||||||
transfer(from, to, lamports)
|
transfer(from, to, lamports)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ fn transfer(from: &KeyedAccount, to: &mut Account, lamports: u64) -> Result<(),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_instruction(
|
pub fn process_instruction(
|
||||||
_program_id: &Pubkey,
|
_owner: &Pubkey,
|
||||||
keyed_accounts: &[KeyedAccount],
|
keyed_accounts: &[KeyedAccount],
|
||||||
instruction_data: &[u8],
|
instruction_data: &[u8],
|
||||||
) -> Result<(), InstructionError> {
|
) -> Result<(), InstructionError> {
|
||||||
|
@ -178,7 +178,7 @@ pub fn process_instruction(
|
||||||
SystemInstruction::CreateAccount {
|
SystemInstruction::CreateAccount {
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
program_id,
|
owner,
|
||||||
} => {
|
} => {
|
||||||
let from = next_keyed_account(keyed_accounts_iter)?;
|
let from = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let to = next_keyed_account(keyed_accounts_iter)?;
|
let to = next_keyed_account(keyed_accounts_iter)?;
|
||||||
|
@ -190,7 +190,7 @@ pub fn process_instruction(
|
||||||
&to_address,
|
&to_address,
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
&program_id,
|
&owner,
|
||||||
&signers,
|
&signers,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -199,28 +199,27 @@ pub fn process_instruction(
|
||||||
seed,
|
seed,
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
program_id,
|
owner,
|
||||||
} => {
|
} => {
|
||||||
let from = next_keyed_account(keyed_accounts_iter)?;
|
let from = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let to = next_keyed_account(keyed_accounts_iter)?;
|
let to = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let mut to_account = to.try_account_ref_mut()?;
|
let mut to_account = to.try_account_ref_mut()?;
|
||||||
let to_address =
|
let to_address = Address::create(&to.unsigned_key(), Some((&base, &seed, &owner)))?;
|
||||||
Address::create(&to.unsigned_key(), Some((&base, &seed, &program_id)))?;
|
|
||||||
create_account(
|
create_account(
|
||||||
from,
|
from,
|
||||||
&mut to_account,
|
&mut to_account,
|
||||||
&to_address,
|
&to_address,
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
&program_id,
|
&owner,
|
||||||
&signers,
|
&signers,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
SystemInstruction::Assign { program_id } => {
|
SystemInstruction::Assign { owner } => {
|
||||||
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let mut account = keyed_account.try_account_ref_mut()?;
|
let mut account = keyed_account.try_account_ref_mut()?;
|
||||||
let address = Address::create(keyed_account.unsigned_key(), None)?;
|
let address = Address::create(keyed_account.unsigned_key(), None)?;
|
||||||
assign(&mut account, &address, &program_id, &signers)
|
assign(&mut account, &address, &owner, &signers)
|
||||||
}
|
}
|
||||||
SystemInstruction::Transfer { lamports } => {
|
SystemInstruction::Transfer { lamports } => {
|
||||||
let from = next_keyed_account(keyed_accounts_iter)?;
|
let from = next_keyed_account(keyed_accounts_iter)?;
|
||||||
|
@ -267,29 +266,21 @@ pub fn process_instruction(
|
||||||
base,
|
base,
|
||||||
seed,
|
seed,
|
||||||
space,
|
space,
|
||||||
program_id,
|
owner,
|
||||||
} => {
|
} => {
|
||||||
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let mut account = keyed_account.try_account_ref_mut()?;
|
let mut account = keyed_account.try_account_ref_mut()?;
|
||||||
let address = Address::create(
|
let address =
|
||||||
keyed_account.unsigned_key(),
|
Address::create(keyed_account.unsigned_key(), Some((&base, &seed, &owner)))?;
|
||||||
Some((&base, &seed, &program_id)),
|
allocate_and_assign(&mut account, &address, space, &owner, &signers)
|
||||||
)?;
|
|
||||||
allocate_and_assign(&mut account, &address, space, &program_id, &signers)
|
|
||||||
}
|
}
|
||||||
SystemInstruction::AssignWithSeed {
|
SystemInstruction::AssignWithSeed { base, seed, owner } => {
|
||||||
base,
|
|
||||||
seed,
|
|
||||||
program_id,
|
|
||||||
} => {
|
|
||||||
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
let keyed_account = next_keyed_account(keyed_accounts_iter)?;
|
||||||
let mut account = keyed_account.try_account_ref_mut()?;
|
let mut account = keyed_account.try_account_ref_mut()?;
|
||||||
let address = Address::create(
|
let address =
|
||||||
keyed_account.unsigned_key(),
|
Address::create(keyed_account.unsigned_key(), Some((&base, &seed, &owner)))?;
|
||||||
Some((&base, &seed, &program_id)),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
assign(&mut account, &address, &program_id, &signers)
|
assign(&mut account, &address, &owner, &signers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -369,7 +360,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_account() {
|
fn test_create_account() {
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let to = Pubkey::new_rand();
|
let to = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
|
@ -385,7 +376,7 @@ mod tests {
|
||||||
&bincode::serialize(&SystemInstruction::CreateAccount {
|
&bincode::serialize(&SystemInstruction::CreateAccount {
|
||||||
lamports: 50,
|
lamports: 50,
|
||||||
space: 2,
|
space: 2,
|
||||||
program_id: new_program_owner
|
owner: new_owner
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
),
|
),
|
||||||
|
@ -393,16 +384,16 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(from_account.borrow().lamports, 50);
|
assert_eq!(from_account.borrow().lamports, 50);
|
||||||
assert_eq!(to_account.borrow().lamports, 50);
|
assert_eq!(to_account.borrow().lamports, 50);
|
||||||
assert_eq!(to_account.borrow().owner, new_program_owner);
|
assert_eq!(to_account.borrow().owner, new_owner);
|
||||||
assert_eq!(to_account.borrow().data, [0, 0]);
|
assert_eq!(to_account.borrow().data, [0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_account_with_seed() {
|
fn test_create_account_with_seed() {
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let seed = "shiny pepper";
|
let seed = "shiny pepper";
|
||||||
let to = Pubkey::create_with_seed(&from, seed, &new_program_owner).unwrap();
|
let to = Pubkey::create_with_seed(&from, seed, &new_owner).unwrap();
|
||||||
|
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
let to_account = Account::new_ref(0, 0, &Pubkey::default());
|
let to_account = Account::new_ref(0, 0, &Pubkey::default());
|
||||||
|
@ -419,7 +410,7 @@ mod tests {
|
||||||
seed: seed.to_string(),
|
seed: seed.to_string(),
|
||||||
lamports: 50,
|
lamports: 50,
|
||||||
space: 2,
|
space: 2,
|
||||||
program_id: new_program_owner
|
owner: new_owner
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
),
|
),
|
||||||
|
@ -427,7 +418,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(from_account.borrow().lamports, 50);
|
assert_eq!(from_account.borrow().lamports, 50);
|
||||||
assert_eq!(to_account.borrow().lamports, 50);
|
assert_eq!(to_account.borrow().lamports, 50);
|
||||||
assert_eq!(to_account.borrow().owner, new_program_owner);
|
assert_eq!(to_account.borrow().owner, new_owner);
|
||||||
assert_eq!(to_account.borrow().data, [0, 0]);
|
assert_eq!(to_account.borrow().data, [0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,24 +427,24 @@ mod tests {
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let seed = "dull boy";
|
let seed = "dull boy";
|
||||||
let to = Pubkey::new_rand();
|
let to = Pubkey::new_rand();
|
||||||
let program_id = Pubkey::new_rand();
|
let owner = Pubkey::new_rand();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Address::create(&to, Some((&from, seed, &program_id))),
|
Address::create(&to, Some((&from, seed, &owner))),
|
||||||
Err(SystemError::AddressWithSeedMismatch.into())
|
Err(SystemError::AddressWithSeedMismatch.into())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_account_with_seed_missing_sig() {
|
fn test_create_account_with_seed_missing_sig() {
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let seed = "dull boy";
|
let seed = "dull boy";
|
||||||
let to = Pubkey::create_with_seed(&from, seed, &new_program_owner).unwrap();
|
let to = Pubkey::create_with_seed(&from, seed, &new_owner).unwrap();
|
||||||
|
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
let mut to_account = Account::new(0, 0, &Pubkey::default());
|
let mut to_account = Account::new(0, 0, &Pubkey::default());
|
||||||
let to_address = Address::create(&to, Some((&from, seed, &new_program_owner))).unwrap();
|
let to_address = Address::create(&to, Some((&from, seed, &new_owner))).unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
create_account(
|
create_account(
|
||||||
|
@ -462,7 +453,7 @@ mod tests {
|
||||||
&to_address,
|
&to_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&HashSet::new(),
|
&HashSet::new(),
|
||||||
),
|
),
|
||||||
Err(InstructionError::MissingRequiredSignature)
|
Err(InstructionError::MissingRequiredSignature)
|
||||||
|
@ -474,7 +465,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_with_zero_lamports() {
|
fn test_create_with_zero_lamports() {
|
||||||
// create account with zero lamports tranferred
|
// create account with zero lamports tranferred
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 1, &Pubkey::new_rand()); // not from system account
|
let from_account = Account::new_ref(100, 1, &Pubkey::new_rand()); // not from system account
|
||||||
|
|
||||||
|
@ -488,7 +479,7 @@ mod tests {
|
||||||
&to.into(),
|
&to.into(),
|
||||||
0,
|
0,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[to].iter().cloned().collect::<HashSet<_>>(),
|
&[to].iter().cloned().collect::<HashSet<_>>(),
|
||||||
),
|
),
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -500,14 +491,14 @@ mod tests {
|
||||||
let to_data = to_account.data;
|
let to_data = to_account.data;
|
||||||
assert_eq!(from_lamports, 100);
|
assert_eq!(from_lamports, 100);
|
||||||
assert_eq!(to_lamports, 0);
|
assert_eq!(to_lamports, 0);
|
||||||
assert_eq!(to_owner, new_program_owner);
|
assert_eq!(to_owner, new_owner);
|
||||||
assert_eq!(to_data, [0, 0]);
|
assert_eq!(to_data, [0, 0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_negative_lamports() {
|
fn test_create_negative_lamports() {
|
||||||
// Attempt to create account with more lamports than remaining in from_account
|
// Attempt to create account with more lamports than remaining in from_account
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
|
|
||||||
|
@ -520,7 +511,7 @@ mod tests {
|
||||||
&to.into(),
|
&to.into(),
|
||||||
150,
|
150,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[from, to].iter().cloned().collect::<HashSet<_>>(),
|
&[from, to].iter().cloned().collect::<HashSet<_>>(),
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(SystemError::ResultWithNegativeLamports.into()));
|
assert_eq!(result, Err(SystemError::ResultWithNegativeLamports.into()));
|
||||||
|
@ -570,7 +561,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_already_in_use() {
|
fn test_create_already_in_use() {
|
||||||
// Attempt to create system account in account already owned by another program
|
// Attempt to create system account in account already owned by another program
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
|
|
||||||
|
@ -588,7 +579,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&signers,
|
&signers,
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
||||||
|
@ -606,7 +597,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&signers,
|
&signers,
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
||||||
|
@ -622,7 +613,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&signers,
|
&signers,
|
||||||
);
|
);
|
||||||
assert_eq!(result, Ok(()));
|
assert_eq!(result, Ok(()));
|
||||||
|
@ -633,7 +624,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_unsigned() {
|
fn test_create_unsigned() {
|
||||||
// Attempt to create an account without signing the transfer
|
// Attempt to create an account without signing the transfer
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
|
|
||||||
|
@ -649,7 +640,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
|
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
||||||
|
@ -662,7 +653,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[from].iter().cloned().collect::<HashSet<_>>(),
|
&[from].iter().cloned().collect::<HashSet<_>>(),
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
|
||||||
|
@ -675,7 +666,7 @@ mod tests {
|
||||||
&owned_address,
|
&owned_address,
|
||||||
0,
|
0,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
|
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
|
||||||
);
|
);
|
||||||
assert_eq!(result, Ok(()));
|
assert_eq!(result, Ok(()));
|
||||||
|
@ -710,7 +701,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_data_populated() {
|
fn test_create_data_populated() {
|
||||||
// Attempt to create system account in account with populated data
|
// Attempt to create system account in account with populated data
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let from_account = Account::new_ref(100, 0, &system_program::id());
|
let from_account = Account::new_ref(100, 0, &system_program::id());
|
||||||
|
|
||||||
|
@ -732,7 +723,7 @@ mod tests {
|
||||||
&populated_address,
|
&populated_address,
|
||||||
50,
|
50,
|
||||||
2,
|
2,
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&signers,
|
&signers,
|
||||||
);
|
);
|
||||||
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
|
||||||
|
@ -773,18 +764,13 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_assign() {
|
fn test_assign() {
|
||||||
let new_program_owner = Pubkey::new(&[9; 32]);
|
let new_owner = Pubkey::new(&[9; 32]);
|
||||||
|
|
||||||
let pubkey = Pubkey::new_rand();
|
let pubkey = Pubkey::new_rand();
|
||||||
let mut account = Account::new(100, 0, &system_program::id());
|
let mut account = Account::new(100, 0, &system_program::id());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
assign(
|
assign(&mut account, &pubkey.into(), &new_owner, &HashSet::new()),
|
||||||
&mut account,
|
|
||||||
&pubkey.into(),
|
|
||||||
&new_program_owner,
|
|
||||||
&HashSet::new()
|
|
||||||
),
|
|
||||||
Err(InstructionError::MissingRequiredSignature)
|
Err(InstructionError::MissingRequiredSignature)
|
||||||
);
|
);
|
||||||
// no change, no signature needed
|
// no change, no signature needed
|
||||||
|
@ -803,10 +789,7 @@ mod tests {
|
||||||
process_instruction(
|
process_instruction(
|
||||||
&Pubkey::default(),
|
&Pubkey::default(),
|
||||||
&[KeyedAccount::new(&pubkey, true, &account)],
|
&[KeyedAccount::new(&pubkey, true, &account)],
|
||||||
&bincode::serialize(&SystemInstruction::Assign {
|
&bincode::serialize(&SystemInstruction::Assign { owner: new_owner }).unwrap()
|
||||||
program_id: new_program_owner
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
),
|
),
|
||||||
Ok(())
|
Ok(())
|
||||||
);
|
);
|
||||||
|
@ -814,7 +797,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_assign_to_sysvar() {
|
fn test_assign_to_sysvar() {
|
||||||
let new_program_owner = sysvar::id();
|
let new_owner = sysvar::id();
|
||||||
|
|
||||||
let from = Pubkey::new_rand();
|
let from = Pubkey::new_rand();
|
||||||
let mut from_account = Account::new(100, 0, &system_program::id());
|
let mut from_account = Account::new(100, 0, &system_program::id());
|
||||||
|
@ -823,7 +806,7 @@ mod tests {
|
||||||
assign(
|
assign(
|
||||||
&mut from_account,
|
&mut from_account,
|
||||||
&from.into(),
|
&from.into(),
|
||||||
&new_program_owner,
|
&new_owner,
|
||||||
&[from].iter().cloned().collect::<HashSet<_>>(),
|
&[from].iter().cloned().collect::<HashSet<_>>(),
|
||||||
),
|
),
|
||||||
Err(SystemError::InvalidProgramId.into())
|
Err(SystemError::InvalidProgramId.into())
|
||||||
|
@ -834,7 +817,7 @@ mod tests {
|
||||||
fn test_process_bogus_instruction() {
|
fn test_process_bogus_instruction() {
|
||||||
// Attempt to assign with no accounts
|
// Attempt to assign with no accounts
|
||||||
let instruction = SystemInstruction::Assign {
|
let instruction = SystemInstruction::Assign {
|
||||||
program_id: Pubkey::new_rand(),
|
owner: Pubkey::new_rand(),
|
||||||
};
|
};
|
||||||
let data = serialize(&instruction).unwrap();
|
let data = serialize(&instruction).unwrap();
|
||||||
let result = process_instruction(&system_program::id(), &[], &data);
|
let result = process_instruction(&system_program::id(), &[], &data);
|
||||||
|
@ -916,8 +899,8 @@ mod tests {
|
||||||
let alice_keypair = Keypair::new();
|
let alice_keypair = Keypair::new();
|
||||||
let alice_pubkey = alice_keypair.pubkey();
|
let alice_pubkey = alice_keypair.pubkey();
|
||||||
let seed = "seed";
|
let seed = "seed";
|
||||||
let program_id = Pubkey::new_rand();
|
let owner = Pubkey::new_rand();
|
||||||
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &program_id).unwrap();
|
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
|
||||||
|
|
||||||
bank_client
|
bank_client
|
||||||
.transfer(50, &mint_keypair, &alice_pubkey)
|
.transfer(50, &mint_keypair, &alice_pubkey)
|
||||||
|
@ -929,7 +912,7 @@ mod tests {
|
||||||
&alice_pubkey,
|
&alice_pubkey,
|
||||||
seed,
|
seed,
|
||||||
2,
|
2,
|
||||||
&program_id,
|
&owner,
|
||||||
)],
|
)],
|
||||||
Some(&alice_pubkey),
|
Some(&alice_pubkey),
|
||||||
);
|
);
|
||||||
|
@ -1026,8 +1009,8 @@ mod tests {
|
||||||
let alice_keypair = Keypair::new();
|
let alice_keypair = Keypair::new();
|
||||||
let alice_pubkey = alice_keypair.pubkey();
|
let alice_pubkey = alice_keypair.pubkey();
|
||||||
let seed = "seed";
|
let seed = "seed";
|
||||||
let program_id = Pubkey::new_rand();
|
let owner = Pubkey::new_rand();
|
||||||
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &program_id).unwrap();
|
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
|
||||||
|
|
||||||
bank_client
|
bank_client
|
||||||
.transfer(50, &mint_keypair, &alice_pubkey)
|
.transfer(50, &mint_keypair, &alice_pubkey)
|
||||||
|
@ -1038,7 +1021,7 @@ mod tests {
|
||||||
&alice_with_seed,
|
&alice_with_seed,
|
||||||
&alice_pubkey,
|
&alice_pubkey,
|
||||||
seed,
|
seed,
|
||||||
&program_id,
|
&owner,
|
||||||
)],
|
)],
|
||||||
Some(&alice_pubkey),
|
Some(&alice_pubkey),
|
||||||
);
|
);
|
||||||
|
|
|
@ -73,21 +73,18 @@ impl Pubkey {
|
||||||
pub fn create_with_seed(
|
pub fn create_with_seed(
|
||||||
base: &Pubkey,
|
base: &Pubkey,
|
||||||
seed: &str,
|
seed: &str,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
) -> Result<Pubkey, PubkeyError> {
|
) -> Result<Pubkey, PubkeyError> {
|
||||||
if seed.len() > MAX_SEED_LEN {
|
if seed.len() > MAX_SEED_LEN {
|
||||||
return Err(PubkeyError::MaxSeedLengthExceeded);
|
return Err(PubkeyError::MaxSeedLengthExceeded);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Pubkey::new(
|
Ok(Pubkey::new(
|
||||||
hashv(&[base.as_ref(), seed.as_ref(), program_id.as_ref()]).as_ref(),
|
hashv(&[base.as_ref(), seed.as_ref(), owner.as_ref()]).as_ref(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_program_address(
|
pub fn create_program_address(seeds: &[&str], owner: &Pubkey) -> Result<Pubkey, PubkeyError> {
|
||||||
seeds: &[&str],
|
|
||||||
program_id: &Pubkey,
|
|
||||||
) -> Result<Pubkey, PubkeyError> {
|
|
||||||
let mut hasher = Hasher::default();
|
let mut hasher = Hasher::default();
|
||||||
for seed in seeds.iter() {
|
for seed in seeds.iter() {
|
||||||
if seed.len() > MAX_SEED_LEN {
|
if seed.len() > MAX_SEED_LEN {
|
||||||
|
@ -95,7 +92,7 @@ impl Pubkey {
|
||||||
}
|
}
|
||||||
hasher.hash(seed.as_ref());
|
hasher.hash(seed.as_ref());
|
||||||
}
|
}
|
||||||
hasher.hashv(&[program_id.as_ref(), "ProgramDerivedAddress".as_ref()]);
|
hasher.hashv(&[owner.as_ref(), "ProgramDerivedAddress".as_ref()]);
|
||||||
|
|
||||||
Ok(Pubkey::new(hashv(&[hasher.result().as_ref()]).as_ref()))
|
Ok(Pubkey::new(hashv(&[hasher.result().as_ref()]).as_ref()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,15 +59,15 @@ pub enum SystemInstruction {
|
||||||
/// * Transaction::keys[1] - new account key
|
/// * Transaction::keys[1] - new account key
|
||||||
/// * lamports - number of lamports to transfer to the new account
|
/// * lamports - number of lamports to transfer to the new account
|
||||||
/// * space - number of bytes of memory to allocate
|
/// * space - number of bytes of memory to allocate
|
||||||
/// * program_id - the program id of the new account
|
/// * owner - the program that will own the new account
|
||||||
CreateAccount {
|
CreateAccount {
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: Pubkey,
|
owner: Pubkey,
|
||||||
},
|
},
|
||||||
/// Assign account to a program
|
/// Assign account to a program
|
||||||
/// * Transaction::keys[0] - account to assign
|
/// * Transaction::keys[0] - account to assign
|
||||||
Assign { program_id: Pubkey },
|
Assign { owner: Pubkey },
|
||||||
/// Transfer lamports
|
/// Transfer lamports
|
||||||
/// * Transaction::keys[0] - source
|
/// * Transaction::keys[0] - source
|
||||||
/// * Transaction::keys[1] - destination
|
/// * Transaction::keys[1] - destination
|
||||||
|
@ -79,13 +79,13 @@ pub enum SystemInstruction {
|
||||||
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
||||||
/// * lamports - number of lamports to transfer to the new account
|
/// * lamports - number of lamports to transfer to the new account
|
||||||
/// * space - number of bytes of memory to allocate
|
/// * space - number of bytes of memory to allocate
|
||||||
/// * program_id - the program id of the new account
|
/// * owner - the program that will own the new account
|
||||||
CreateAccountWithSeed {
|
CreateAccountWithSeed {
|
||||||
base: Pubkey,
|
base: Pubkey,
|
||||||
seed: String,
|
seed: String,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: Pubkey,
|
owner: Pubkey,
|
||||||
},
|
},
|
||||||
/// `AdvanceNonceAccount` consumes a stored nonce, replacing it with a successor
|
/// `AdvanceNonceAccount` consumes a stored nonce, replacing it with a successor
|
||||||
///
|
///
|
||||||
|
@ -141,21 +141,21 @@ pub enum SystemInstruction {
|
||||||
/// * Transaction::keys[0] - new account key
|
/// * Transaction::keys[0] - new account key
|
||||||
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
||||||
/// * space - number of bytes of memory to allocate
|
/// * space - number of bytes of memory to allocate
|
||||||
/// * program_id - the program id of the new account
|
/// * owner - the program that will own the new account
|
||||||
AllocateWithSeed {
|
AllocateWithSeed {
|
||||||
base: Pubkey,
|
base: Pubkey,
|
||||||
seed: String,
|
seed: String,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: Pubkey,
|
owner: Pubkey,
|
||||||
},
|
},
|
||||||
/// Assign account to a program based on a seed
|
/// Assign account to a program based on a seed
|
||||||
/// * Transaction::keys[0] - account to assign
|
/// * Transaction::keys[0] - account to assign
|
||||||
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
/// * seed - string of ascii chars, no longer than pubkey::MAX_SEED_LEN
|
||||||
/// * program_id - the program id of the new account
|
/// * owner - the program that will own the new account
|
||||||
AssignWithSeed {
|
AssignWithSeed {
|
||||||
base: Pubkey,
|
base: Pubkey,
|
||||||
seed: String,
|
seed: String,
|
||||||
program_id: Pubkey,
|
owner: Pubkey,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ pub fn create_account(
|
||||||
to_pubkey: &Pubkey,
|
to_pubkey: &Pubkey,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
) -> Instruction {
|
) -> Instruction {
|
||||||
let account_metas = vec![
|
let account_metas = vec![
|
||||||
AccountMeta::new(*from_pubkey, true),
|
AccountMeta::new(*from_pubkey, true),
|
||||||
|
@ -175,7 +175,7 @@ pub fn create_account(
|
||||||
&SystemInstruction::CreateAccount {
|
&SystemInstruction::CreateAccount {
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
program_id: *program_id,
|
owner: *owner,
|
||||||
},
|
},
|
||||||
account_metas,
|
account_metas,
|
||||||
)
|
)
|
||||||
|
@ -185,12 +185,12 @@ pub fn create_account(
|
||||||
// calling create_address_with_seed()
|
// calling create_address_with_seed()
|
||||||
pub fn create_account_with_seed(
|
pub fn create_account_with_seed(
|
||||||
from_pubkey: &Pubkey,
|
from_pubkey: &Pubkey,
|
||||||
to_pubkey: &Pubkey, // must match create_address_with_seed(base, seed, program_id)
|
to_pubkey: &Pubkey, // must match create_address_with_seed(base, seed, owner)
|
||||||
base: &Pubkey,
|
base: &Pubkey,
|
||||||
seed: &str,
|
seed: &str,
|
||||||
lamports: u64,
|
lamports: u64,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
) -> Instruction {
|
) -> Instruction {
|
||||||
let account_metas = vec![
|
let account_metas = vec![
|
||||||
AccountMeta::new(*from_pubkey, true),
|
AccountMeta::new(*from_pubkey, true),
|
||||||
|
@ -205,28 +205,26 @@ pub fn create_account_with_seed(
|
||||||
seed: seed.to_string(),
|
seed: seed.to_string(),
|
||||||
lamports,
|
lamports,
|
||||||
space,
|
space,
|
||||||
program_id: *program_id,
|
owner: *owner,
|
||||||
},
|
},
|
||||||
account_metas,
|
account_metas,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assign(pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
|
pub fn assign(pubkey: &Pubkey, owner: &Pubkey) -> Instruction {
|
||||||
let account_metas = vec![AccountMeta::new(*pubkey, true)];
|
let account_metas = vec![AccountMeta::new(*pubkey, true)];
|
||||||
Instruction::new(
|
Instruction::new(
|
||||||
system_program::id(),
|
system_program::id(),
|
||||||
&SystemInstruction::Assign {
|
&SystemInstruction::Assign { owner: *owner },
|
||||||
program_id: *program_id,
|
|
||||||
},
|
|
||||||
account_metas,
|
account_metas,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assign_with_seed(
|
pub fn assign_with_seed(
|
||||||
address: &Pubkey, // must match create_address_with_seed(base, seed, program_id)
|
address: &Pubkey, // must match create_address_with_seed(base, seed, owner)
|
||||||
base: &Pubkey,
|
base: &Pubkey,
|
||||||
seed: &str,
|
seed: &str,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
) -> Instruction {
|
) -> Instruction {
|
||||||
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
||||||
Instruction::new(
|
Instruction::new(
|
||||||
|
@ -234,7 +232,7 @@ pub fn assign_with_seed(
|
||||||
&SystemInstruction::AssignWithSeed {
|
&SystemInstruction::AssignWithSeed {
|
||||||
base: *base,
|
base: *base,
|
||||||
seed: seed.to_string(),
|
seed: seed.to_string(),
|
||||||
program_id: *program_id,
|
owner: *owner,
|
||||||
},
|
},
|
||||||
account_metas,
|
account_metas,
|
||||||
)
|
)
|
||||||
|
@ -262,11 +260,11 @@ pub fn allocate(pubkey: &Pubkey, space: u64) -> Instruction {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocate_with_seed(
|
pub fn allocate_with_seed(
|
||||||
address: &Pubkey, // must match create_address_with_seed(base, seed, program_id)
|
address: &Pubkey, // must match create_address_with_seed(base, seed, owner)
|
||||||
base: &Pubkey,
|
base: &Pubkey,
|
||||||
seed: &str,
|
seed: &str,
|
||||||
space: u64,
|
space: u64,
|
||||||
program_id: &Pubkey,
|
owner: &Pubkey,
|
||||||
) -> Instruction {
|
) -> Instruction {
|
||||||
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
let account_metas = vec![AccountMeta::new(*address, false)].with_signer(base);
|
||||||
Instruction::new(
|
Instruction::new(
|
||||||
|
@ -275,7 +273,7 @@ pub fn allocate_with_seed(
|
||||||
base: *base,
|
base: *base,
|
||||||
seed: seed.to_string(),
|
seed: seed.to_string(),
|
||||||
space,
|
space,
|
||||||
program_id: *program_id,
|
owner: *owner,
|
||||||
},
|
},
|
||||||
account_metas,
|
account_metas,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue