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 b0e1db9 commit 9321e88
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
25 changes: 13 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 @@ -316,8 +316,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);
}
}
}
Expand Down Expand Up @@ -1746,11 +1746,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 @@ -1767,7 +1767,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 @@ -1782,7 +1782,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 @@ -1829,11 +1829,12 @@ 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);
println!("{:?}", error_counters);
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 @@ -1850,7 +1851,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 @@ -1865,7 +1866,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
6 changes: 6 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
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 9321e88

Please sign in to comment.