fixup! s/contract/program
This commit is contained in:
parent
9bfead2e01
commit
c0e7e43e96
|
@ -107,7 +107,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
|
||||
* `contract_id`, array of 32 bytes representing the program this account has been assigned to
|
||||
* `program_id`, array of 32 bytes representing the program this account has been assigned to
|
||||
* `userdata`, array of bytes representing any userdata associated with the account
|
||||
|
||||
##### Example:
|
||||
|
@ -116,7 +116,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":["FVxxngPx368XvMCoeskdd6U8cZJFsfa1BEtGWqyAxRj4"]}' http://localhost:8899
|
||||
|
||||
// Result
|
||||
{"jsonrpc":"2.0","result":{"contract_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":{"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}
|
||||
```
|
||||
|
||||
---
|
||||
|
|
12
src/bank.rs
12
src/bank.rs
|
@ -5,7 +5,7 @@
|
|||
|
||||
use bincode::deserialize;
|
||||
use bincode::serialize;
|
||||
use budget_program::BudgetProgram;
|
||||
use budget_program::BudgetState;
|
||||
use counter::Counter;
|
||||
use entry::Entry;
|
||||
use hash::{hash, Hash};
|
||||
|
@ -284,7 +284,7 @@ impl Bank {
|
|||
} else {
|
||||
error_counters.account_not_found_leader += 1;
|
||||
}
|
||||
if BudgetProgram::check_id(&tx.program_id) {
|
||||
if BudgetState::check_id(&tx.program_id) {
|
||||
use instruction::Instruction;
|
||||
if let Some(Instruction::NewVote(_vote)) = tx.instruction() {
|
||||
error_counters.account_not_found_vote += 1;
|
||||
|
@ -355,10 +355,10 @@ impl Bank {
|
|||
// It's up to the contract to implement its own rules on moving funds
|
||||
if SystemProgram::check_id(&tx.program_id) {
|
||||
SystemProgram::process_transaction(&tx, accounts)
|
||||
} else if BudgetProgram::check_id(&tx.program_id) {
|
||||
} else if BudgetState::check_id(&tx.program_id) {
|
||||
// TODO: the runtime should be checking read/write access to memory
|
||||
// we are trusting the hard coded contracts not to clobber or allocate
|
||||
BudgetProgram::process_transaction(&tx, accounts)
|
||||
BudgetState::process_transaction(&tx, accounts)
|
||||
} else {
|
||||
return Err(BankError::UnknownContractId(tx.program_id));
|
||||
}
|
||||
|
@ -609,8 +609,8 @@ impl Bank {
|
|||
pub fn read_balance(account: &Account) -> i64 {
|
||||
if SystemProgram::check_id(&account.program_id) {
|
||||
SystemProgram::get_balance(account)
|
||||
} else if BudgetProgram::check_id(&account.program_id) {
|
||||
BudgetProgram::get_balance(account)
|
||||
} else if BudgetState::check_id(&account.program_id) {
|
||||
BudgetState::get_balance(account)
|
||||
} else {
|
||||
account.tokens
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ pub enum BudgetError {
|
|||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
|
||||
pub struct BudgetProgram {
|
||||
pub struct BudgetState {
|
||||
pub initialized: bool,
|
||||
pub pending_budget: Option<Budget>,
|
||||
pub last_error: Option<BudgetError>,
|
||||
|
@ -32,7 +32,7 @@ pub struct BudgetProgram {
|
|||
pub const BUDGET_PROGRAM_ID: [u8; 32] = [
|
||||
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,
|
||||
];
|
||||
impl BudgetProgram {
|
||||
impl BudgetState {
|
||||
fn is_pending(&self) -> bool {
|
||||
self.pending_budget != None
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ impl BudgetProgram {
|
|||
trace!("contract already exists");
|
||||
Err(BudgetError::ContractAlreadyExists(tx.keys[1]))
|
||||
} else {
|
||||
let mut state = BudgetProgram::default();
|
||||
let mut state = BudgetState::default();
|
||||
state.pending_budget = Some(budget);
|
||||
accounts[1].tokens += contract.tokens;
|
||||
state.initialized = true;
|
||||
|
@ -231,7 +231,7 @@ impl BudgetProgram {
|
|||
}
|
||||
|
||||
fn save_error_to_budget_state(e: BudgetError, accounts: &mut [Account]) -> () {
|
||||
if let Ok(mut state) = BudgetProgram::deserialize(&accounts[1].userdata) {
|
||||
if let Ok(mut state) = BudgetState::deserialize(&accounts[1].userdata) {
|
||||
trace!("saved error {:?}", e);
|
||||
state.last_error = Some(e);
|
||||
state.serialize(&mut accounts[1].userdata).unwrap();
|
||||
|
@ -262,7 +262,7 @@ impl BudgetProgram {
|
|||
//TODO the contract needs to provide a "get_balance" introspection call of the userdata
|
||||
pub fn get_balance(account: &Account) -> i64 {
|
||||
if let Ok(state) = deserialize(&account.userdata) {
|
||||
let state: BudgetProgram = state;
|
||||
let state: BudgetState = state;
|
||||
if state.is_pending() {
|
||||
0
|
||||
} else {
|
||||
|
@ -277,26 +277,26 @@ impl BudgetProgram {
|
|||
mod test {
|
||||
use bank::Account;
|
||||
use bincode::serialize;
|
||||
use budget_program::{BudgetError, BudgetProgram};
|
||||
use budget_program::{BudgetError, BudgetState};
|
||||
use chrono::prelude::{DateTime, NaiveDate, Utc};
|
||||
use hash::Hash;
|
||||
use signature::{GenKeys, Keypair, KeypairUtil, Pubkey};
|
||||
use transaction::Transaction;
|
||||
#[test]
|
||||
fn test_serializer() {
|
||||
let mut a = Account::new(0, 512, BudgetProgram::id());
|
||||
let b = BudgetProgram::default();
|
||||
let mut a = Account::new(0, 512, BudgetState::id());
|
||||
let b = BudgetState::default();
|
||||
b.serialize(&mut a.userdata).unwrap();
|
||||
let buf = serialize(&b).unwrap();
|
||||
assert_eq!(a.userdata[8..8 + buf.len()], buf[0..]);
|
||||
let c = BudgetProgram::deserialize(&a.userdata).unwrap();
|
||||
let c = BudgetState::deserialize(&a.userdata).unwrap();
|
||||
assert_eq!(b, c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serializer_userdata_too_small() {
|
||||
let mut a = Account::new(0, 1, BudgetContract::id());
|
||||
let b = BudgetContract::default();
|
||||
let mut a = Account::new(0, 1, BudgetState::id());
|
||||
let b = BudgetState::default();
|
||||
assert_eq!(
|
||||
b.serialize(&mut a.userdata),
|
||||
Err(BudgetError::UserdataTooSmall)
|
||||
|
@ -305,8 +305,8 @@ mod test {
|
|||
#[test]
|
||||
fn test_invalid_instruction() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, BudgetContract::id()),
|
||||
Account::new(0, 512, BudgetContract::id()),
|
||||
Account::new(1, 0, BudgetState::id()),
|
||||
Account::new(0, 512, BudgetState::id()),
|
||||
];
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
|
@ -314,12 +314,12 @@ mod test {
|
|||
let tx = Transaction::new_with_userdata(
|
||||
&from,
|
||||
&[contract.pubkey()],
|
||||
BudgetContract::id(),
|
||||
BudgetState::id(),
|
||||
vec![1, 2, 3], // <== garbage instruction
|
||||
Hash::default(),
|
||||
0,
|
||||
);
|
||||
BudgetContract::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
|
||||
// Success if there was no panic...
|
||||
}
|
||||
|
@ -327,9 +327,9 @@ mod test {
|
|||
#[test]
|
||||
fn test_transfer_on_date() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, BudgetProgram::id()),
|
||||
Account::new(0, 512, BudgetProgram::id()),
|
||||
Account::new(0, 0, BudgetProgram::id()),
|
||||
Account::new(1, 0, BudgetState::id()),
|
||||
Account::new(0, 512, BudgetState::id()),
|
||||
Account::new(0, 0, BudgetState::id()),
|
||||
];
|
||||
let from_account = 0;
|
||||
let contract_account = 1;
|
||||
|
@ -347,10 +347,10 @@ mod test {
|
|||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert_eq!(state.last_error, None);
|
||||
assert!(state.is_pending());
|
||||
|
||||
|
@ -362,12 +362,12 @@ mod test {
|
|||
dt,
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
assert_eq!(accounts[to_account].tokens, 0);
|
||||
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert_eq!(
|
||||
state.last_error,
|
||||
Some(BudgetError::DestinationMissing(to.pubkey()))
|
||||
|
@ -383,20 +383,20 @@ mod test {
|
|||
dt,
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[to_account].tokens, 1);
|
||||
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert!(!state.is_pending());
|
||||
|
||||
// try to replay the timestamp contract
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[to_account].tokens, 1);
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert_eq!(
|
||||
state.last_error,
|
||||
Some(BudgetError::ContractNotPending(contract.pubkey()))
|
||||
|
@ -405,9 +405,9 @@ mod test {
|
|||
#[test]
|
||||
fn test_cancel_transfer() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, BudgetProgram::id()),
|
||||
Account::new(0, 512, BudgetProgram::id()),
|
||||
Account::new(0, 0, BudgetProgram::id()),
|
||||
Account::new(1, 0, BudgetState::id()),
|
||||
Account::new(0, 512, BudgetState::id()),
|
||||
Account::new(0, 0, BudgetState::id()),
|
||||
];
|
||||
let from_account = 0;
|
||||
let contract_account = 1;
|
||||
|
@ -424,10 +424,10 @@ mod test {
|
|||
1,
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert_eq!(state.last_error, None);
|
||||
assert!(state.is_pending());
|
||||
|
||||
|
@ -436,7 +436,7 @@ mod test {
|
|||
Transaction::budget_new_signature(&to, contract.pubkey(), to.pubkey(), Hash::default());
|
||||
// unit test hack, the `from account` is passed instead of the `to` account to avoid
|
||||
// creating more account vectors
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
// nothing should be changed because apply witness didn't finalize a payment
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 1);
|
||||
|
@ -450,7 +450,7 @@ mod test {
|
|||
from.pubkey(),
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[pay_account].tokens, 1);
|
||||
|
@ -462,12 +462,12 @@ mod test {
|
|||
from.pubkey(),
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetProgram::process_transaction(&tx, &mut accounts);
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[from_account].tokens, 0);
|
||||
assert_eq!(accounts[contract_account].tokens, 0);
|
||||
assert_eq!(accounts[pay_account].tokens, 1);
|
||||
|
||||
let state = BudgetProgram::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
let state = BudgetState::deserialize(&accounts[contract_account].userdata).unwrap();
|
||||
assert_eq!(
|
||||
state.last_error,
|
||||
Some(BudgetError::ContractNotPending(contract.pubkey()))
|
||||
|
@ -477,9 +477,9 @@ mod test {
|
|||
#[test]
|
||||
fn test_userdata_too_small() {
|
||||
let mut accounts = vec![
|
||||
Account::new(1, 0, BudgetContract::id()),
|
||||
Account::new(1, 0, BudgetContract::id()), // <== userdata is 0, which is not enough
|
||||
Account::new(1, 0, BudgetContract::id()),
|
||||
Account::new(1, 0, BudgetState::id()),
|
||||
Account::new(1, 0, BudgetState::id()), // <== userdata is 0, which is not enough
|
||||
Account::new(1, 0, BudgetState::id()),
|
||||
];
|
||||
let from = Keypair::new();
|
||||
let contract = Keypair::new();
|
||||
|
@ -493,8 +493,8 @@ mod test {
|
|||
Hash::default(),
|
||||
);
|
||||
|
||||
BudgetContract::process_transaction(&tx, &mut accounts);
|
||||
assert!(BudgetContract::deserialize(&accounts[1].userdata).is_err());
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert!(BudgetState::deserialize(&accounts[1].userdata).is_err());
|
||||
|
||||
let tx = Transaction::budget_new_timestamp(
|
||||
&from,
|
||||
|
@ -503,8 +503,8 @@ mod test {
|
|||
Utc::now(),
|
||||
Hash::default(),
|
||||
);
|
||||
BudgetContract::process_transaction(&tx, &mut accounts);
|
||||
assert!(BudgetContract::deserialize(&accounts[1].userdata).is_err());
|
||||
BudgetState::process_transaction(&tx, &mut accounts);
|
||||
assert!(BudgetState::deserialize(&accounts[1].userdata).is_err());
|
||||
|
||||
// Success if there was no panic...
|
||||
}
|
||||
|
|
|
@ -298,7 +298,7 @@ mod tests {
|
|||
let expected = r#"{
|
||||
"jsonrpc":"2.0",
|
||||
"result":{
|
||||
"contract_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],
|
||||
"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],
|
||||
"tokens": 20,
|
||||
"userdata": []
|
||||
},
|
||||
|
|
|
@ -73,6 +73,7 @@ impl SystemProgram {
|
|||
//bank should be verifying correctness
|
||||
accounts[0].tokens -= tokens;
|
||||
accounts[1].tokens += tokens;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!("Invalid transaction userdata: {:?}", tx.userdata);
|
||||
|
@ -126,7 +127,7 @@ mod test {
|
|||
let mut accounts = vec![Account::default(), Account::default()];
|
||||
let tx =
|
||||
Transaction::system_create(&from, to.pubkey(), Hash::default(), 0, 1, to.pubkey(), 0);
|
||||
SystemContract::process_transaction(&tx, &mut accounts);
|
||||
SystemProgram::process_transaction(&tx, &mut accounts);
|
||||
assert!(accounts[0].userdata.is_empty());
|
||||
assert_eq!(accounts[1].userdata.len(), 1);
|
||||
assert_eq!(accounts[1].program_id, to.pubkey());
|
||||
|
@ -136,7 +137,7 @@ mod test {
|
|||
let from = Keypair::new();
|
||||
let to = Keypair::new();
|
||||
let mut accounts = vec![Account::default(), Account::default()];
|
||||
accounts[1].contract_id = to.pubkey();
|
||||
accounts[1].program_id = to.pubkey();
|
||||
let tx = Transaction::system_create(
|
||||
&from,
|
||||
to.pubkey(),
|
||||
|
@ -146,7 +147,7 @@ mod test {
|
|||
Pubkey::default(),
|
||||
0,
|
||||
);
|
||||
SystemContract::process_transaction(&tx, &mut accounts);
|
||||
SystemProgram::process_transaction(&tx, &mut accounts);
|
||||
assert!(accounts[1].userdata.is_empty());
|
||||
}
|
||||
#[test]
|
||||
|
@ -164,7 +165,7 @@ mod test {
|
|||
Pubkey::default(),
|
||||
0,
|
||||
);
|
||||
SystemContract::process_transaction(&tx, &mut accounts);
|
||||
SystemProgram::process_transaction(&tx, &mut accounts);
|
||||
assert!(accounts[1].userdata.is_empty());
|
||||
}
|
||||
#[test]
|
||||
|
@ -182,7 +183,7 @@ mod test {
|
|||
Pubkey::default(),
|
||||
0,
|
||||
);
|
||||
SystemContract::process_transaction(&tx, &mut accounts);
|
||||
SystemProgram::process_transaction(&tx, &mut accounts);
|
||||
assert_eq!(accounts[1].userdata.len(), 3);
|
||||
}
|
||||
#[test]
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use bincode::{deserialize, serialize};
|
||||
use budget::{Budget, Condition};
|
||||
use budget_program::BudgetProgram;
|
||||
use budget_program::BudgetState;
|
||||
use chrono::prelude::*;
|
||||
use hash::Hash;
|
||||
use instruction::{Contract, Instruction, Vote};
|
||||
|
@ -89,7 +89,7 @@ impl Transaction {
|
|||
Self::new_with_userdata(
|
||||
from_keypair,
|
||||
&[to],
|
||||
BudgetProgram::id(),
|
||||
BudgetState::id(),
|
||||
userdata,
|
||||
last_id,
|
||||
fee,
|
||||
|
@ -114,7 +114,7 @@ impl Transaction {
|
|||
Self::new_with_userdata(
|
||||
from_keypair,
|
||||
&[contract, to],
|
||||
BudgetProgram::id(),
|
||||
BudgetState::id(),
|
||||
userdata,
|
||||
last_id,
|
||||
0,
|
||||
|
@ -133,7 +133,7 @@ impl Transaction {
|
|||
Self::new_with_userdata(
|
||||
from_keypair,
|
||||
&[contract, to],
|
||||
BudgetProgram::id(),
|
||||
BudgetState::id(),
|
||||
userdata,
|
||||
last_id,
|
||||
0,
|
||||
|
@ -146,7 +146,7 @@ impl Transaction {
|
|||
Self::new_with_userdata(
|
||||
from_keypair,
|
||||
&[],
|
||||
BudgetProgram::id(),
|
||||
BudgetState::id(),
|
||||
userdata,
|
||||
last_id,
|
||||
fee,
|
||||
|
@ -172,7 +172,7 @@ impl Transaction {
|
|||
Self::new_with_userdata(
|
||||
from_keypair,
|
||||
&[contract],
|
||||
BudgetProgram::id(),
|
||||
BudgetState::id(),
|
||||
userdata,
|
||||
last_id,
|
||||
0,
|
||||
|
|
Loading…
Reference in New Issue