Rename Account.program_id to Account.owner

This commit is contained in:
Michael Vines 2018-11-12 09:29:17 -08:00
parent 7d05cc8c5d
commit 522876c808
13 changed files with 54 additions and 56 deletions

View File

@ -110,7 +110,7 @@ fn serialize_parameters(keyed_accounts: &mut [KeyedAccount], data: &[u8]) -> Vec
v.write_u64::<LittleEndian>(info.account.userdata.len() as u64)
.unwrap();
v.write_all(&info.account.userdata).unwrap();
v.write_all(info.account.program_id.as_ref()).unwrap();
v.write_all(info.account.owner.as_ref()).unwrap();
}
v.write_u64::<LittleEndian>(data.len() as u64).unwrap();
v.write_all(data).unwrap();
@ -131,7 +131,7 @@ fn deserialize_parameters(keyed_accounts: &mut [KeyedAccount], buffer: &[u8]) {
info.account.userdata.clone_from_slice(&buffer[start..end]);
start += info.account.userdata.len() // skip userdata
+ mem::size_of::<Pubkey>(); // skip program_id
+ mem::size_of::<Pubkey>(); // skip owner
}
}

View File

@ -156,7 +156,7 @@ mod tests {
let alice_pubkey = Pubkey::default();
let bob_pubkey = Pubkey::default();
let program_id = Pubkey::default();
let owner = Pubkey::default();
let mut accounts = [
(
@ -164,13 +164,13 @@ mod tests {
Account {
tokens: 1,
userdata,
program_id,
owner,
executable: true,
loader: Pubkey::default(),
},
),
(alice_pubkey, Account::new(100, 0, program_id)),
(bob_pubkey, Account::new(1, 0, program_id)),
(alice_pubkey, Account::new(100, 0, owner)),
(bob_pubkey, Account::new(1, 0, owner)),
];
let data = serialize(&10u64).unwrap();
process(&mut create_keyed_accounts(&mut accounts), &data);
@ -198,19 +198,19 @@ mod tests {
accounts[3].userdata = serialize({a=1, b=2, c=3}, nil, "s")
"#.as_bytes()
.to_vec();
let program_id = Pubkey::default();
let owner = Pubkey::default();
let program_account = Account {
tokens: 1,
userdata,
program_id,
owner,
executable: true,
loader: Pubkey::default(),
};
let alice_account = Account::new(100, 0, program_id);
let alice_account = Account::new(100, 0, owner);
let serialize_account = Account {
tokens: 100,
userdata: read_test_file("serialize.lua"),
program_id,
owner,
executable: false,
loader: Pubkey::default(),
};
@ -218,7 +218,7 @@ mod tests {
(Pubkey::default(), program_account),
(Pubkey::default(), alice_account),
(Pubkey::default(), serialize_account),
(Pubkey::default(), Account::new(1, 0, program_id)),
(Pubkey::default(), Account::new(1, 0, owner)),
];
let mut keyed_accounts = create_keyed_accounts(&mut accounts);
process(&mut keyed_accounts, &[]);
@ -231,7 +231,7 @@ mod tests {
#[test]
fn test_lua_multisig() {
let program_id = Pubkey::default();
let owner = Pubkey::default();
let alice_pubkey = Pubkey::new(&[0; 32]);
let serialize_pubkey = Pubkey::new(&[1; 32]);
@ -244,7 +244,7 @@ mod tests {
let program_account = Account {
tokens: 1,
userdata: read_test_file("multisig.lua"),
program_id,
owner,
executable: true,
loader: Pubkey::default(),
};
@ -252,7 +252,7 @@ mod tests {
let alice_account = Account {
tokens: 100,
userdata: Vec::new(),
program_id,
owner,
executable: true,
loader: Pubkey::default(),
};
@ -260,7 +260,7 @@ mod tests {
let serialize_account = Account {
tokens: 100,
userdata: read_test_file("serialize.lua"),
program_id,
owner,
executable: true,
loader: Pubkey::default(),
};
@ -269,8 +269,8 @@ mod tests {
(Pubkey::default(), program_account), // Account holding the program
(alice_pubkey, alice_account), // The payer
(serialize_pubkey, serialize_account), // Where the serialize library is stored.
(state_pubkey, Account::new(1, 0, program_id)), // Where program state is stored.
(bob_pubkey, Account::new(1, 0, program_id)), // The payee once M signatures are collected.
(state_pubkey, Account::new(1, 0, owner)), // Where program state is stored.
(bob_pubkey, Account::new(1, 0, owner)), // The payee once M signatures are collected.
];
let mut keyed_accounts = create_keyed_accounts(&mut accounts);

View File

@ -10,7 +10,7 @@ pub struct Account {
/// A transaction can write to its userdata
pub userdata: Vec<u8>,
/// contract id this contract belongs to
pub program_id: Pubkey,
pub owner: Pubkey,
/// this account contains a program (and is strictly read-only)
pub executable: bool,
@ -20,12 +20,12 @@ pub struct Account {
}
impl Account {
// TODO do we want to add executable and leader_program_id even though they should always be false/default?
pub fn new(tokens: u64, space: usize, program_id: Pubkey) -> Account {
// TODO do we want to add executable and leader_owner even though they should always be false/default?
pub fn new(tokens: u64, space: usize, owner: Pubkey) -> Account {
Account {
tokens,
userdata: vec![0u8; space],
program_id,
owner,
executable: false,
loader: Pubkey::default(),
}

View File

@ -726,11 +726,11 @@ impl Bank {
// Verify the transaction
// Make sure that program_id is still the same or this was just assigned by the system call contract
if *pre_program_id != account.program_id && !SystemProgram::check_id(&program_id) {
if *pre_program_id != account.owner && !SystemProgram::check_id(&program_id) {
return Err(BankError::ModifiedContractId(instruction_index as u8));
}
// For accounts unassigned to the contract, the individual balance of each accounts cannot decrease.
if *program_id != account.program_id && pre_tokens > account.tokens {
if *program_id != account.owner && pre_tokens > account.tokens {
return Err(BankError::ExternalAccountTokenSpend(
instruction_index as u8,
));
@ -771,7 +771,7 @@ impl Bank {
let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum();
let pre_data: Vec<_> = program_accounts
.iter_mut()
.map(|a| (a.program_id, a.tokens))
.map(|a| (a.owner, a.tokens))
.collect();
// Call the contract method
@ -1299,9 +1299,9 @@ impl Bank {
}
pub fn read_balance(account: &Account) -> u64 {
if SystemProgram::check_id(&account.program_id) {
if SystemProgram::check_id(&account.owner) {
SystemProgram::get_balance(account)
} else if BudgetState::check_id(&account.program_id) {
} else if BudgetState::check_id(&account.owner) {
BudgetState::get_balance(account)
} else {
account.tokens
@ -2025,7 +2025,7 @@ mod tests {
let string = transport_receiver.poll();
assert!(string.is_ok());
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],"program_id":[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,"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}}}}"#);
assert_eq!(expected, response);
}

View File

@ -16,7 +16,7 @@ pub fn id() -> Pubkey {
pub fn account() -> Account {
Account {
tokens: 0,
program_id: id(),
owner: id(),
userdata: BPF_LOADER_NAME.as_bytes().to_vec(),
executable: true,
loader: native_loader::id(),

View File

@ -46,7 +46,7 @@ impl ComputeLeaderFinalityService {
.filter_map(|account| {
// Filter out any accounts that don't belong to the VoteProgram
// by returning None
if VoteProgram::check_id(&account.program_id) {
if VoteProgram::check_id(&account.owner) {
if let Ok(vote_state) = VoteProgram::deserialize(&account.userdata) {
let validator_stake = bank.get_stake(&vote_state.node_id);
total_stake += validator_stake;

View File

@ -121,7 +121,7 @@ Returns all information associated with the account of provided Pubkey
The result field will be a JSON object with the following sub fields:
* `tokens`, number of tokens assigned to this account, as a signed 64-bit integer
* `program_id`, array of 32 bytes representing the program this account has been assigned to
* `owner`, array of 32 bytes representing the program this account has been assigned to
* `userdata`, array of bytes representing any userdata associated with the account
* `executable`, boolean indicating if the account contains a program (and is strictly read-only)
* `loader`, array of 32 bytes representing the loader for this program (if `executable`), otherwise all
@ -132,7 +132,7 @@ The result field will be a JSON object with the following sub fields:
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0", "id":1, "method":"getAccountInfo", "params":["2gVkYWexTHR5Hb2aLeQN3tnngvWzisFKXDUPrgMHpdST"]}' http://localhost:8899
// Result
{"jsonrpc":"2.0","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],"program_id":[1,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":[3,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,50,48,53,48,45,48,49,45,48,49,84,48,48,58,48,48,58,48,48,90,252,10,7,28,246,140,88,177,98,82,10,227,89,81,18,30,194,101,199,16,11,73,133,20,246,62,114,39,20,113,189,32,50,0,0,0,0,0,0,0,247,15,36,102,167,83,225,42,133,127,82,34,36,224,207,130,109,230,224,188,163,33,213,13,5,117,211,251,65,159,197,51,0,0,0,0,0,0]},"id":1}
{"jsonrpc":"2.0","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":[1,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":[3,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,50,48,53,48,45,48,49,45,48,49,84,48,48,58,48,48,58,48,48,90,252,10,7,28,246,140,88,177,98,82,10,227,89,81,18,30,194,101,199,16,11,73,133,20,246,62,114,39,20,113,189,32,50,0,0,0,0,0,0,0,247,15,36,102,167,83,225,42,133,127,82,34,36,224,207,130,109,230,224,188,163,33,213,13,5,117,211,251,65,159,197,51,0,0,0,0,0,0]},"id":1}
```
---
@ -271,7 +271,7 @@ Subscribe to an account to receive notifications when the userdata for a given a
##### Notification Format:
```bash
{"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],"program_id":[1,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":[3,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,50,48,53,48,45,48,49,45,48,49,84,48,48,58,48,48,58,48,48,90,252,10,7,28,246,140,88,177,98,82,10,227,89,81,18,30,194,101,199,16,11,73,133,20,246,62,114,39,20,113,189,32,50,0,0,0,0,0,0,0,247,15,36,102,167,83,225,42,133,127,82,34,36,224,207,130,109,230,224,188,163,33,213,13,5,117,211,251,65,159,197,51,0,0,0,0,0,0]},"subscription":0}}
{"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":[1,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":[3,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,20,0,0,0,0,0,0,0,50,48,53,48,45,48,49,45,48,49,84,48,48,58,48,48,58,48,48,90,252,10,7,28,246,140,88,177,98,82,10,227,89,81,18,30,194,101,199,16,11,73,133,20,246,62,114,39,20,113,189,32,50,0,0,0,0,0,0,0,247,15,36,102,167,83,225,42,133,127,82,34,36,224,207,130,109,230,224,188,163,33,213,13,5,117,211,251,65,159,197,51,0,0,0,0,0,0]},"subscription":0}}
```
---

View File

@ -294,7 +294,7 @@ impl LeaderScheduler {
.accounts
.values()
.filter_map(|account| {
if VoteProgram::check_id(&account.program_id) {
if VoteProgram::check_id(&account.owner) {
if let Ok(vote_state) = VoteProgram::deserialize(&account.userdata) {
return vote_state
.votes

View File

@ -484,7 +484,7 @@ mod tests {
let expected = r#"{
"jsonrpc":"2.0",
"result":{
"program_id": [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,
"userdata": [],
"executable": false,

View File

@ -485,7 +485,7 @@ mod tests {
"method": "accountNotification",
"params": {
"result": {
"program_id": budget_program_id,
"owner": budget_program_id,
"tokens": 1,
"userdata": expected_userdata,
"executable": executable,
@ -526,7 +526,7 @@ mod tests {
"method": "accountNotification",
"params": {
"result": {
"program_id": budget_program_id,
"owner": budget_program_id,
"tokens": 51,
"userdata": expected_userdata,
"executable": executable,
@ -565,7 +565,7 @@ mod tests {
"method": "accountNotification",
"params": {
"result": {
"program_id": budget_program_id,
"owner": budget_program_id,
"tokens": 1,
"userdata": expected_userdata,
"executable": executable,

View File

@ -73,14 +73,13 @@ impl SystemProgram {
space,
program_id,
} => {
if !Self::check_id(&accounts[0].program_id) {
info!("Invalid account[0] program_id");
if !Self::check_id(&accounts[0].owner) {
info!("Invalid account[0] owner");
Err(Error::InvalidArgument)?;
}
if space > 0
&& (!accounts[1].userdata.is_empty()
|| !Self::check_id(&accounts[1].program_id))
&& (!accounts[1].userdata.is_empty() || !Self::check_id(&accounts[1].owner))
{
info!("Invalid account[1]");
Err(Error::InvalidArgument)?;
@ -91,16 +90,16 @@ impl SystemProgram {
}
accounts[0].tokens -= tokens;
accounts[1].tokens += tokens;
accounts[1].program_id = program_id;
accounts[1].owner = program_id;
accounts[1].userdata = vec![0; space as usize];
accounts[1].executable = false;
accounts[1].loader = Pubkey::default();
}
SystemProgram::Assign { program_id } => {
if !Self::check_id(&accounts[0].program_id) {
if !Self::check_id(&accounts[0].owner) {
Err(Error::AssignOfUnownedAccount)?;
}
accounts[0].program_id = program_id;
accounts[0].owner = program_id;
}
SystemProgram::Move { tokens } => {
//bank should be verifying correctness
@ -112,12 +111,11 @@ impl SystemProgram {
accounts[1].tokens += tokens;
}
SystemProgram::Spawn => {
if !accounts[0].executable || accounts[0].loader != Pubkey::default()
{
if !accounts[0].executable || accounts[0].loader != Pubkey::default() {
Err(Error::AccountNotFinalized)?;
}
accounts[0].loader = accounts[0].program_id;
accounts[0].program_id = tx.account_keys[0];
accounts[0].loader = accounts[0].owner;
accounts[0].owner = tx.account_keys[0];
}
}
Ok(())
@ -171,7 +169,7 @@ mod test {
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].tokens = 1;
accounts[0].program_id = from.pubkey();
accounts[0].owner = from.pubkey();
let tx = Transaction::system_new(&from, to.pubkey(), 1, Hash::default());
if let Ok(()) = process_transaction(&tx, &mut accounts) {
panic!("Account not owned by SystemProgram");
@ -189,14 +187,14 @@ mod test {
process_transaction(&tx, &mut accounts).unwrap();
assert!(accounts[0].userdata.is_empty());
assert_eq!(accounts[1].userdata.len(), 1);
assert_eq!(accounts[1].program_id, to.pubkey());
assert_eq!(accounts[1].owner, to.pubkey());
}
#[test]
fn test_create_allocate_wrong_dest_program() {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[1].program_id = to.pubkey();
accounts[1].owner = to.pubkey();
let tx = Transaction::system_create(
&from,
to.pubkey(),
@ -214,7 +212,7 @@ mod test {
let from = Keypair::new();
let to = Keypair::new();
let mut accounts = vec![Account::default(), Account::default()];
accounts[0].program_id = to.pubkey();
accounts[0].owner = to.pubkey();
let tx = Transaction::system_create(
&from,
to.pubkey(),
@ -252,7 +250,7 @@ mod test {
let mut accounts = vec![Account::default()];
let tx = Transaction::system_assign(&from, Hash::default(), program.pubkey(), 0);
process_transaction(&tx, &mut accounts).unwrap();
assert_eq!(accounts[0].program_id, program.pubkey());
assert_eq!(accounts[0].owner, program.pubkey());
}
#[test]
fn test_move() {

View File

@ -16,7 +16,7 @@ pub fn id() -> Pubkey {
pub fn account() -> Account {
Account {
tokens: 0,
program_id: id(),
owner: id(),
userdata: ERC20_NAME.as_bytes().to_vec(),
executable: true,
loader: native_loader::id(),

View File

@ -105,7 +105,7 @@ impl VoteProgram {
Ok(VoteInstruction::RegisterAccount) => {
// TODO: a single validator could register multiple "vote accounts"
// which would clutter the "accounts" structure. See github issue 1654.
accounts[1].program_id = Self::id();
accounts[1].owner = Self::id();
let mut vote_state = VoteProgram {
votes: VecDeque::new(),
@ -117,7 +117,7 @@ impl VoteProgram {
Ok(())
}
Ok(VoteInstruction::NewVote(vote)) => {
if !Self::check_id(&accounts[0].program_id) {
if !Self::check_id(&accounts[0].owner) {
error!("accounts[0] is not assigned to the VOTE_PROGRAM");
Err(Error::InvalidArguments)?;
}