Skip to content

Commit

Permalink
Add new TransactionError
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Sep 7, 2021
1 parent ee28a52 commit a9fdc52
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
24 changes: 12 additions & 12 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ impl Accounts {
&& message.is_writable(i, demote_program_write_locks)
&& !is_upgradeable_loader_present
{
error_counters.invalid_account_index += 1;
return Err(TransactionError::InvalidAccountIndex);
error_counters.invalid_writable_account += 1;
return Err(TransactionError::InvalidWritableAccount);
}

if bpf_loader_upgradeable::check_id(account.owner()) {
Expand Down Expand Up @@ -315,8 +315,8 @@ impl Accounts {
if let Ok(UpgradeableLoaderState::ProgramData { .. }) =
account.state()
{
error_counters.invalid_account_index += 1;
return Err(TransactionError::InvalidAccountIndex);
error_counters.invalid_writable_account += 1;
return Err(TransactionError::InvalidWritableAccount);
}
}
}
Expand Down Expand Up @@ -1744,11 +1744,11 @@ mod tests {
let tx = Transaction::new(&[&keypair], message.clone(), Hash::default());
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
assert_eq!(
loaded_accounts[0],
(Err(TransactionError::InvalidAccountIndex), None)
(Err(TransactionError::InvalidWritableAccount), None)
);

// Solution 1: include bpf_loader_upgradeable account
Expand All @@ -1765,7 +1765,7 @@ mod tests {
let loaded_accounts =
load_accounts(tx, &accounts_with_upgradeable_loader, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
let result = loaded_accounts[0].0.as_ref().unwrap();
assert_eq!(result.accounts[..2], accounts_with_upgradeable_loader[..2]);
Expand All @@ -1780,7 +1780,7 @@ mod tests {
let tx = Transaction::new(&[&keypair], message, Hash::default());
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
let result = loaded_accounts[0].0.as_ref().unwrap();
assert_eq!(result.accounts[..2], accounts[..2]);
Expand Down Expand Up @@ -1827,11 +1827,11 @@ mod tests {
let tx = Transaction::new(&[&keypair], message.clone(), Hash::default());
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
assert_eq!(
loaded_accounts[0],
(Err(TransactionError::InvalidAccountIndex), None)
(Err(TransactionError::InvalidWritableAccount), None)
);

// Solution 1: include bpf_loader_upgradeable account
Expand All @@ -1848,7 +1848,7 @@ mod tests {
let loaded_accounts =
load_accounts(tx, &accounts_with_upgradeable_loader, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
let result = loaded_accounts[0].0.as_ref().unwrap();
assert_eq!(result.accounts[..2], accounts_with_upgradeable_loader[..2]);
Expand All @@ -1863,7 +1863,7 @@ mod tests {
let tx = Transaction::new(&[&keypair], message, Hash::default());
let loaded_accounts = load_accounts(tx, &accounts, &mut error_counters);

assert_eq!(error_counters.invalid_account_index, 1);
assert_eq!(error_counters.invalid_writable_account, 1);
assert_eq!(loaded_accounts.len(), 1);
let result = loaded_accounts[0].0.as_ref().unwrap();
assert_eq!(result.accounts[..2], accounts[..2]);
Expand Down
1 change: 1 addition & 0 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub struct ErrorCounters {
pub invalid_account_index: usize,
pub invalid_program_for_execution: usize,
pub not_allowed_during_cluster_maintenance: usize,
pub invalid_writable_account: usize,
}

#[derive(Default, Debug)]
Expand Down
8 changes: 7 additions & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl ExecuteTimings {
}

type BankStatusCache = StatusCache<Result<()>>;
#[frozen_abi(digest = "GT81Hdwrh73i55ScQvFqmzeHfUL42yxuavZods8VyzGc")]
#[frozen_abi(digest = "5Br3PNyyX1L7XoS4jYLt5JTeMXowLSsu7v9LhokC8vnq")]
pub type BankSlotDelta = SlotDelta<Result<()>>;
type TransactionAccountRefCells = Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>;
type TransactionLoaderRefCells = Vec<Vec<(Pubkey, Rc<RefCell<AccountSharedData>>)>>;
Expand Down Expand Up @@ -3119,6 +3119,12 @@ impl Bank {
error_counters.not_allowed_during_cluster_maintenance
);
}
if 0 != error_counters.invalid_writable_account {
inc_new_counter_info!(
"bank-process_transactions-error-invalid_writable_account",
error_counters.invalid_writable_account
);
}
}

/// Converts Accounts into RefCell<AccountSharedData>, this involves moving
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub enum TransactionError {
/// Transaction version is unsupported
#[error("Transaction version is unsupported")]
UnsupportedVersion,

/// Transaction loads a writable account that cannot be written
#[error("Transaction loads a writable account that cannot be written")]
InvalidWritableAccount,
}

pub type Result<T> = result::Result<T, TransactionError>;
Expand Down
1 change: 1 addition & 0 deletions storage-proto/proto/transaction_by_addr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum TransactionErrorType {
ACCOUNT_BORROW_OUTSTANDING_TX = 16;
WOULD_EXCEED_MAX_BLOCK_COST_LIMIT = 17;
UNSUPPORTED_VERSION = 18;
INVALID_WRITABLE_ACCOUNT = 19;
}

message InstructionError {
Expand Down
4 changes: 4 additions & 0 deletions storage-proto/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ impl TryFrom<tx_by_addr::TransactionError> for TransactionError {
16 => TransactionError::AccountBorrowOutstanding,
17 => TransactionError::WouldExceedMaxBlockCostLimit,
18 => TransactionError::UnsupportedVersion,
19 => TransactionError::InvalidWritableAccount,
_ => return Err("Invalid TransactionError"),
})
}
Expand Down Expand Up @@ -614,6 +615,9 @@ impl From<TransactionError> for tx_by_addr::TransactionError {
TransactionError::UnsupportedVersion => {
tx_by_addr::TransactionErrorType::UnsupportedVersion
}
TransactionError::InvalidWritableAccount => {
tx_by_addr::TransactionErrorType::InvalidWritableAccount
}
} as i32,
instruction_error: match transaction_error {
TransactionError::InstructionError(index, ref instruction_error) => {
Expand Down

0 comments on commit a9fdc52

Please sign in to comment.