s/contract/program/
This commit is contained in:
parent
655ee1a64b
commit
903a9bfd05
21
src/bank.rs
21
src/bank.rs
|
@ -1,5 +1,5 @@
|
||||||
//! The `bank` module tracks client accounts and the progress of smart
|
//! The `bank` module tracks client accounts and the progress of on-chain
|
||||||
//! contracts. It offers a high-level API that signs transactions
|
//! programs. It offers a high-level API that signs transactions
|
||||||
//! on behalf of the caller, and a low-level API for when they have
|
//! on behalf of the caller, and a low-level API for when they have
|
||||||
//! already been signed and verified.
|
//! already been signed and verified.
|
||||||
|
|
||||||
|
@ -86,9 +86,6 @@ pub enum BankError {
|
||||||
/// The program returned an error
|
/// The program returned an error
|
||||||
ProgramError(u8, ProgramError),
|
ProgramError(u8, ProgramError),
|
||||||
|
|
||||||
/// Contract id is unknown
|
|
||||||
UnknownContractId(u8),
|
|
||||||
|
|
||||||
/// Recoding into PoH failed
|
/// Recoding into PoH failed
|
||||||
RecordFailure,
|
RecordFailure,
|
||||||
|
|
||||||
|
@ -274,7 +271,7 @@ impl Checkpoint for Accounts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Manager for the state of all accounts and contracts after processing its entries.
|
/// Manager for the state of all accounts and programs after processing its entries.
|
||||||
pub struct Bank {
|
pub struct Bank {
|
||||||
/// A map of account public keys to the balance in that account.
|
/// A map of account public keys to the balance in that account.
|
||||||
pub accounts: RwLock<Accounts>,
|
pub accounts: RwLock<Accounts>,
|
||||||
|
@ -642,8 +639,8 @@ impl Bank {
|
||||||
return Err(BankError::LastIdNotFound);
|
return Err(BankError::LastIdNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no way to predict what contract will execute without an error
|
// There is no way to predict what program will execute without an error
|
||||||
// If a fee can pay for execution then the contract will be scheduled
|
// If a fee can pay for execution then the program will be scheduled
|
||||||
let err =
|
let err =
|
||||||
Self::reserve_signature_with_last_id(last_ids, &tx.last_id, &tx.signatures[0]);
|
Self::reserve_signature_with_last_id(last_ids, &tx.last_id, &tx.signatures[0]);
|
||||||
if let Err(BankError::LastIdNotFound) = err {
|
if let Err(BankError::LastIdNotFound) = err {
|
||||||
|
@ -1209,15 +1206,15 @@ impl Bank {
|
||||||
account.tokens
|
account.tokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Each contract would need to be able to introspect its own state
|
/// Each program would need to be able to introspect its own state
|
||||||
/// this is hard coded to the budget contract language
|
/// this is hard-coded to the Budget language
|
||||||
pub fn get_balance(&self, pubkey: &Pubkey) -> u64 {
|
pub fn get_balance(&self, pubkey: &Pubkey) -> u64 {
|
||||||
self.get_account(pubkey)
|
self.get_account(pubkey)
|
||||||
.map(|x| Self::read_balance(&x))
|
.map(|x| Self::read_balance(&x))
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO: Need to implement a real staking contract to hold node stake.
|
/// TODO: Need to implement a real staking program to hold node stake.
|
||||||
/// Right now this just gets the account balances. See github issue #1655.
|
/// Right now this just gets the account balances. See github issue #1655.
|
||||||
pub fn get_stake(&self, pubkey: &Pubkey) -> u64 {
|
pub fn get_stake(&self, pubkey: &Pubkey) -> u64 {
|
||||||
self.get_balance(pubkey)
|
self.get_balance(pubkey)
|
||||||
|
@ -1547,7 +1544,7 @@ mod tests {
|
||||||
let bank = Bank::new(&mint);
|
let bank = Bank::new(&mint);
|
||||||
let dest = Keypair::new();
|
let dest = Keypair::new();
|
||||||
|
|
||||||
// source with 0 contract context
|
// source with 0 program context
|
||||||
let tx = Transaction::system_create(
|
let tx = Transaction::system_create(
|
||||||
&mint.keypair(),
|
&mint.keypair(),
|
||||||
dest.pubkey(),
|
dest.pubkey(),
|
||||||
|
|
|
@ -26,8 +26,8 @@ fn process_instruction(
|
||||||
) -> Result<(), ProgramError> {
|
) -> Result<(), ProgramError> {
|
||||||
let program_id = tx.program_id(instruction_index);
|
let program_id = tx.program_id(instruction_index);
|
||||||
|
|
||||||
// Call the contract method
|
// Call the program method
|
||||||
// It's up to the contract to implement its own rules on moving funds
|
// It's up to the program to implement its own rules on moving funds
|
||||||
if is_legacy_program(&program_id) {
|
if is_legacy_program(&program_id) {
|
||||||
if system_program::check_id(&program_id) {
|
if system_program::check_id(&program_id) {
|
||||||
system_program::process(&tx, instruction_index, program_accounts)?;
|
system_program::process(&tx, instruction_index, program_accounts)?;
|
||||||
|
@ -71,11 +71,11 @@ fn verify_instruction(
|
||||||
) -> Result<(), ProgramError> {
|
) -> Result<(), ProgramError> {
|
||||||
// Verify the transaction
|
// Verify the transaction
|
||||||
|
|
||||||
// Make sure that program_id is still the same or this was just assigned by the system call contract
|
// Make sure that program_id is still the same or this was just assigned by the system program
|
||||||
if *pre_program_id != account.owner && !system_program::check_id(&program_id) {
|
if *pre_program_id != account.owner && !system_program::check_id(&program_id) {
|
||||||
return Err(ProgramError::ModifiedProgramId);
|
return Err(ProgramError::ModifiedProgramId);
|
||||||
}
|
}
|
||||||
// For accounts unassigned to the contract, the individual balance of each accounts cannot decrease.
|
// For accounts unassigned to the program, the individual balance of each accounts cannot decrease.
|
||||||
if *program_id != account.owner && pre_tokens > account.tokens {
|
if *program_id != account.owner && pre_tokens > account.tokens {
|
||||||
return Err(ProgramError::ExternalAccountTokenSpend);
|
return Err(ProgramError::ExternalAccountTokenSpend);
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ pub fn execute_instruction(
|
||||||
) -> Result<(), ProgramError> {
|
) -> Result<(), ProgramError> {
|
||||||
let program_id = tx.program_id(instruction_index);
|
let program_id = tx.program_id(instruction_index);
|
||||||
// TODO: the runtime should be checking read/write access to memory
|
// TODO: the runtime should be checking read/write access to memory
|
||||||
// we are trusting the hard coded contracts not to clobber or allocate
|
// we are trusting the hard-coded programs not to clobber or allocate
|
||||||
let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum();
|
let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum();
|
||||||
let pre_data: Vec<_> = program_accounts
|
let pre_data: Vec<_> = program_accounts
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
|
|
Loading…
Reference in New Issue