Add custom error for tx-history queries when node does not support (#17494)

This commit is contained in:
Tyera Eulberg 2021-05-26 13:27:41 -06:00 committed by GitHub
parent 3f9e3c7375
commit 6abe089740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 5 deletions

View File

@ -16,6 +16,7 @@ pub const JSON_RPC_SERVER_ERROR_SLOT_SKIPPED: i64 = -32007;
pub const JSON_RPC_SERVER_ERROR_NO_SNAPSHOT: i64 = -32008; pub const JSON_RPC_SERVER_ERROR_NO_SNAPSHOT: i64 = -32008;
pub const JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: i64 = -32009; pub const JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: i64 = -32009;
pub const JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: i64 = -32010; pub const JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: i64 = -32010;
pub const JSON_RPC_SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE: i64 = -32011;
pub enum RpcCustomError { pub enum RpcCustomError {
BlockCleanedUp { BlockCleanedUp {
@ -44,6 +45,7 @@ pub enum RpcCustomError {
KeyExcludedFromSecondaryIndex { KeyExcludedFromSecondaryIndex {
index_key: String, index_key: String,
}, },
TransactionHistoryNotAvailable,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -132,6 +134,13 @@ impl From<RpcCustomError> for Error {
), ),
data: None, data: None,
}, },
RpcCustomError::TransactionHistoryNotAvailable => Self {
code: ErrorCode::ServerError(
JSON_RPC_SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE,
),
message: "Transaction history is not available from this node".to_string(),
data: None,
},
} }
} }
} }

View File

@ -958,6 +958,8 @@ impl JsonRpcRequestProcessor {
})); }));
} }
} }
} else {
return Err(RpcCustomError::TransactionHistoryNotAvailable.into());
} }
Err(RpcCustomError::BlockNotAvailable { slot }.into()) Err(RpcCustomError::BlockNotAvailable { slot }.into())
} }
@ -1178,6 +1180,10 @@ impl JsonRpcRequestProcessor {
.unwrap_or(false); .unwrap_or(false);
let bank = self.bank(Some(CommitmentConfig::processed())); let bank = self.bank(Some(CommitmentConfig::processed()));
if search_transaction_history && !self.config.enable_rpc_transaction_history {
return Err(RpcCustomError::TransactionHistoryNotAvailable.into());
}
for signature in signatures { for signature in signatures {
let status = if let Some(status) = self.get_transaction_status(signature, &bank) { let status = if let Some(status) = self.get_transaction_status(signature, &bank) {
Some(status) Some(status)
@ -1313,6 +1319,8 @@ impl JsonRpcRequestProcessor {
} }
} }
} }
} else {
return Err(RpcCustomError::TransactionHistoryNotAvailable.into());
} }
Ok(None) Ok(None)
} }
@ -1415,7 +1423,7 @@ impl JsonRpcRequestProcessor {
}) })
.collect()) .collect())
} else { } else {
Ok(vec![]) Err(RpcCustomError::TransactionHistoryNotAvailable.into())
} }
} }
@ -5132,7 +5140,7 @@ pub mod tests {
let bob_pubkey = solana_sdk::pubkey::new_rand(); let bob_pubkey = solana_sdk::pubkey::new_rand();
let RpcHandler { let RpcHandler {
io, io,
meta, mut meta,
blockhash, blockhash,
alice, alice,
confirmed_block_signatures, confirmed_block_signatures,
@ -5171,7 +5179,7 @@ pub mod tests {
r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatuses","params":[["{}"]]}}"#, r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatuses","params":[["{}"]]}}"#,
confirmed_block_signatures[1] confirmed_block_signatures[1]
); );
let res = io.handle_request_sync(&req, meta); let res = io.handle_request_sync(&req, meta.clone());
let expected_res: transaction::Result<()> = Err(TransactionError::InstructionError( let expected_res: transaction::Result<()> = Err(TransactionError::InstructionError(
0, 0,
InstructionError::Custom(1), InstructionError::Custom(1),
@ -5181,6 +5189,20 @@ pub mod tests {
serde_json::from_value(json["result"]["value"][0].clone()) serde_json::from_value(json["result"]["value"][0].clone())
.expect("actual response deserialization"); .expect("actual response deserialization");
assert_eq!(expected_res, result.as_ref().unwrap().status); assert_eq!(expected_res, result.as_ref().unwrap().status);
// disable rpc-tx-history, but attempt historical query
meta.config.enable_rpc_transaction_history = false;
let req = format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatuses","params":[["{}"], {{"searchTransactionHistory": true}}]}}"#,
confirmed_block_signatures[1]
);
let res = io.handle_request_sync(&req, meta);
assert_eq!(
res,
Some(
r#"{"jsonrpc":"2.0","error":{"code":-32011,"message":"Transaction history is not available from this node"},"id":1}"#.to_string(),
)
);
} }
#[test] #[test]
@ -5757,7 +5779,7 @@ pub mod tests {
let bob_pubkey = solana_sdk::pubkey::new_rand(); let bob_pubkey = solana_sdk::pubkey::new_rand();
let RpcHandler { let RpcHandler {
io, io,
meta, mut meta,
confirmed_block_signatures, confirmed_block_signatures,
blockhash, blockhash,
.. ..
@ -5809,7 +5831,7 @@ pub mod tests {
} }
let req = r#"{"jsonrpc":"2.0","id":1,"method":"getBlock","params":[0,"binary"]}"#; let req = r#"{"jsonrpc":"2.0","id":1,"method":"getBlock","params":[0,"binary"]}"#;
let res = io.handle_request_sync(&req, meta); let res = io.handle_request_sync(&req, meta.clone());
let result: Value = serde_json::from_str(&res.expect("actual response")) let result: Value = serde_json::from_str(&res.expect("actual response"))
.expect("actual response deserialization"); .expect("actual response deserialization");
let confirmed_block: Option<EncodedConfirmedBlock> = let confirmed_block: Option<EncodedConfirmedBlock> =
@ -5850,6 +5872,17 @@ pub mod tests {
} }
} }
} }
// disable rpc-tx-history
meta.config.enable_rpc_transaction_history = false;
let req = r#"{"jsonrpc":"2.0","id":1,"method":"getBlock","params":[0]}"#;
let res = io.handle_request_sync(&req, meta);
assert_eq!(
res,
Some(
r#"{"jsonrpc":"2.0","error":{"code":-32011,"message":"Transaction history is not available from this node"},"id":1}"#.to_string(),
)
);
} }
#[test] #[test]