Use Account::owner as loader for executable accounts

This commit is contained in:
Greg Fitzgerald
2019-02-14 10:57:54 -07:00
parent 3849b8ece4
commit 4a0009365e
7 changed files with 17 additions and 30 deletions

View File

@ -59,7 +59,6 @@ pub fn entrypoint(
keyed_accounts[to].account.owner = program_id; keyed_accounts[to].account.owner = program_id;
keyed_accounts[to].account.userdata = vec![0; space as usize]; keyed_accounts[to].account.userdata = vec![0; space as usize];
keyed_accounts[to].account.executable = false; keyed_accounts[to].account.executable = false;
keyed_accounts[to].account.loader = Pubkey::default();
} }
SystemInstruction::Assign { program_id } => { SystemInstruction::Assign { program_id } => {
if !system_program::check_id(&keyed_accounts[from].account.owner) { if !system_program::check_id(&keyed_accounts[from].account.owner) {
@ -83,11 +82,10 @@ pub fn entrypoint(
} }
SystemInstruction::Spawn => { SystemInstruction::Spawn => {
if !keyed_accounts[from].account.executable if !keyed_accounts[from].account.executable
|| keyed_accounts[from].account.loader != Pubkey::default() || keyed_accounts[from].account.owner != Pubkey::default()
{ {
Err(ProgramError::AccountNotFinalized)?; Err(ProgramError::AccountNotFinalized)?;
} }
keyed_accounts[from].account.loader = keyed_accounts[from].account.owner;
keyed_accounts[from].account.owner = *keyed_accounts[from].signer_key().unwrap(); keyed_accounts[from].account.owner = *keyed_accounts[from].signer_key().unwrap();
} }
} }

View File

@ -8,13 +8,10 @@ pub struct Account {
pub tokens: u64, pub tokens: u64,
/// data held in this account /// data held in this account
pub userdata: Vec<u8>, pub userdata: Vec<u8>,
/// the program that owns this account /// the program that owns this account. If executable, the program that loads this account.
pub owner: Pubkey, pub owner: Pubkey,
/// this account's userdata contains a loaded program (and is now read-only) /// this account's userdata contains a loaded program (and is now read-only)
pub executable: bool, pub executable: bool,
/// the loader for this account
/// (Pubkey::default() if the account is not executable and thus was never 'loaded')
pub loader: Pubkey,
} }
impl Account { impl Account {
@ -25,7 +22,6 @@ impl Account {
userdata: vec![0u8; space], userdata: vec![0u8; space],
owner, owner,
executable: false, executable: false,
loader: Pubkey::default(),
} }
} }
} }

View File

@ -20,6 +20,5 @@ pub fn create_program_account(name: &str) -> Account {
owner: id(), owner: id(),
userdata: name.as_bytes().to_vec(), userdata: name.as_bytes().to_vec(),
executable: true, executable: true,
loader: id(),
} }
} }

View File

@ -192,7 +192,7 @@ impl AccountsDB {
return Err(BankError::AccountNotFound); return Err(BankError::AccountNotFound);
} }
}; };
if !program.executable || program.loader == Pubkey::default() { if !program.executable || program.owner == Pubkey::default() {
error_counters.account_not_found += 1; error_counters.account_not_found += 1;
return Err(BankError::AccountNotFound); return Err(BankError::AccountNotFound);
} }
@ -200,7 +200,7 @@ impl AccountsDB {
// add loader to chain // add loader to chain
accounts.insert(0, (program_id, program.clone())); accounts.insert(0, (program_id, program.clone()));
program_id = program.loader; program_id = program.owner;
} }
Ok(accounts) Ok(accounts)
} }
@ -616,32 +616,32 @@ mod tests {
let mut account = Account::new(40, 1, Pubkey::default()); let mut account = Account::new(40, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = native_loader::id(); account.owner = native_loader::id();
accounts.push((key1, account)); accounts.push((key1, account));
let mut account = Account::new(41, 1, Pubkey::default()); let mut account = Account::new(41, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key1; account.owner = key1;
accounts.push((key2, account)); accounts.push((key2, account));
let mut account = Account::new(42, 1, Pubkey::default()); let mut account = Account::new(42, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key2; account.owner = key2;
accounts.push((key3, account)); accounts.push((key3, account));
let mut account = Account::new(43, 1, Pubkey::default()); let mut account = Account::new(43, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key3; account.owner = key3;
accounts.push((key4, account)); accounts.push((key4, account));
let mut account = Account::new(44, 1, Pubkey::default()); let mut account = Account::new(44, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key4; account.owner = key4;
accounts.push((key5, account)); accounts.push((key5, account));
let mut account = Account::new(45, 1, Pubkey::default()); let mut account = Account::new(45, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key5; account.owner = key5;
accounts.push((key6, account)); accounts.push((key6, account));
let instructions = vec![Instruction::new(0, &(), vec![0])]; let instructions = vec![Instruction::new(0, &(), vec![0])];
@ -675,7 +675,7 @@ mod tests {
let mut account = Account::new(40, 1, Pubkey::default()); let mut account = Account::new(40, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = Pubkey::default(); account.owner = Pubkey::default();
accounts.push((key1, account)); accounts.push((key1, account));
let instructions = vec![Instruction::new(0, &(), vec![0])]; let instructions = vec![Instruction::new(0, &(), vec![0])];
@ -708,7 +708,7 @@ mod tests {
accounts.push((key0, account)); accounts.push((key0, account));
let mut account = Account::new(40, 1, Pubkey::default()); let mut account = Account::new(40, 1, Pubkey::default());
account.loader = native_loader::id(); account.owner = native_loader::id();
accounts.push((key1, account)); accounts.push((key1, account));
let instructions = vec![Instruction::new(0, &(), vec![0])]; let instructions = vec![Instruction::new(0, &(), vec![0])];
@ -744,17 +744,17 @@ mod tests {
let mut account = Account::new(40, 1, Pubkey::default()); let mut account = Account::new(40, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = native_loader::id(); account.owner = native_loader::id();
accounts.push((key1, account)); accounts.push((key1, account));
let mut account = Account::new(41, 1, Pubkey::default()); let mut account = Account::new(41, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key1; account.owner = key1;
accounts.push((key2, account)); accounts.push((key2, account));
let mut account = Account::new(42, 1, Pubkey::default()); let mut account = Account::new(42, 1, Pubkey::default());
account.executable = true; account.executable = true;
account.loader = key2; account.owner = key2;
accounts.push((key3, account)); accounts.push((key3, account));
let instructions = vec![ let instructions = vec![

View File

@ -191,7 +191,6 @@ impl Bank {
userdata: vec![0; vote_program::get_max_size() as usize], userdata: vec![0; vote_program::get_max_size() as usize],
owner: vote_program::id(), owner: vote_program::id(),
executable: false, executable: false,
loader: Pubkey::default(),
}; };
let mut vote_state = vote_program::VoteState::new( let mut vote_state = vote_program::VoteState::new(

View File

@ -599,8 +599,7 @@ mod tests {
"owner": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], "owner": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
"tokens": 20, "tokens": 20,
"userdata": [], "userdata": [],
"executable": false, "executable": false
"loader": [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
}, },
"id":1} "id":1}
"#; "#;

View File

@ -507,7 +507,6 @@ mod tests {
let contract_funds = Keypair::new(); let contract_funds = Keypair::new();
let contract_state = Keypair::new(); let contract_state = Keypair::new();
let budget_program_id = budget_program::id(); let budget_program_id = budget_program::id();
let loader = Pubkey::default(); // TODO
let executable = false; // TODO let executable = false; // TODO
let bank = Bank::new(&genesis_block); let bank = Bank::new(&genesis_block);
let arc_bank = Arc::new(bank); let arc_bank = Arc::new(bank);
@ -565,7 +564,6 @@ mod tests {
"tokens": 1, "tokens": 1,
"userdata": expected_userdata, "userdata": expected_userdata,
"executable": executable, "executable": executable,
"loader": loader,
}, },
"subscription": 0, "subscription": 0,
@ -605,7 +603,6 @@ mod tests {
"tokens": 51, "tokens": 51,
"userdata": expected_userdata, "userdata": expected_userdata,
"executable": executable, "executable": executable,
"loader": loader,
}, },
"subscription": 0, "subscription": 0,
} }
@ -644,7 +641,6 @@ mod tests {
"tokens": 1, "tokens": 1,
"userdata": expected_userdata, "userdata": expected_userdata,
"executable": executable, "executable": executable,
"loader": loader,
}, },
"subscription": 0, "subscription": 0,
} }
@ -736,7 +732,7 @@ mod tests {
subscriptions.check_account(&alice.pubkey(), &account); subscriptions.check_account(&alice.pubkey(), &account);
let string = transport_receiver.poll(); let string = transport_receiver.poll();
if let Async::Ready(Some(response)) = string.unwrap() { if let Async::Ready(Some(response)) = string.unwrap() {
let expected = format!(r#"{{"jsonrpc":"2.0","method":"accountNotification","params":{{"result":{{"executable":false,"loader":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"owner":[129,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"tokens":1,"userdata":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},"subscription":0}}}}"#); let expected = format!(r#"{{"jsonrpc":"2.0","method":"accountNotification","params":{{"result":{{"executable":false,"owner":[129,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"tokens":1,"userdata":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},"subscription":0}}}}"#);
assert_eq!(expected, response); assert_eq!(expected, response);
} }