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

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

(cherry picked from commit 6abe089740)

# Conflicts:
#	core/src/rpc.rs

* Fix conflicts

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
Co-authored-by: Tyera Eulberg <tyera@solana.com>
This commit is contained in:
mergify[bot]
2021-05-26 21:37:26 +00:00
committed by GitHub
parent f4cf7d2c84
commit 9c549a3ccf
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_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_TRANSACTION_HISTORY_NOT_AVAILABLE: i64 = -32011;
pub enum RpcCustomError {
BlockCleanedUp {
@ -44,6 +45,7 @@ pub enum RpcCustomError {
KeyExcludedFromSecondaryIndex {
index_key: String,
},
TransactionHistoryNotAvailable,
}
#[derive(Debug, Serialize, Deserialize)]
@ -132,6 +134,13 @@ impl From<RpcCustomError> for Error {
),
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

@ -960,6 +960,8 @@ impl JsonRpcRequestProcessor {
}));
}
}
} else {
return Err(RpcCustomError::TransactionHistoryNotAvailable.into());
}
Err(RpcCustomError::BlockNotAvailable { slot }.into())
}
@ -1180,6 +1182,10 @@ impl JsonRpcRequestProcessor {
.unwrap_or(false);
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 {
let status = if let Some(status) = self.get_transaction_status(signature, &bank) {
Some(status)
@ -1315,6 +1321,8 @@ impl JsonRpcRequestProcessor {
}
}
}
} else {
return Err(RpcCustomError::TransactionHistoryNotAvailable.into());
}
Ok(None)
}
@ -1417,7 +1425,7 @@ impl JsonRpcRequestProcessor {
})
.collect())
} else {
Ok(vec![])
Err(RpcCustomError::TransactionHistoryNotAvailable.into())
}
}
@ -5187,7 +5195,7 @@ pub mod tests {
let bob_pubkey = solana_sdk::pubkey::new_rand();
let RpcHandler {
io,
meta,
mut meta,
blockhash,
alice,
confirmed_block_signatures,
@ -5226,7 +5234,7 @@ pub mod tests {
r#"{{"jsonrpc":"2.0","id":1,"method":"getSignatureStatuses","params":[["{}"]]}}"#,
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(
0,
InstructionError::Custom(1),
@ -5236,6 +5244,20 @@ pub mod tests {
serde_json::from_value(json["result"]["value"][0].clone())
.expect("actual response deserialization");
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]
@ -5812,7 +5834,7 @@ pub mod tests {
let bob_pubkey = solana_sdk::pubkey::new_rand();
let RpcHandler {
io,
meta,
mut meta,
confirmed_block_signatures,
blockhash,
..
@ -5864,7 +5886,7 @@ pub mod tests {
}
let req = r#"{"jsonrpc":"2.0","id":1,"method":"getConfirmedBlock","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"))
.expect("actual response deserialization");
let confirmed_block: Option<EncodedConfirmedBlock> =
@ -5905,6 +5927,17 @@ pub mod tests {
}
}
}
// disable rpc-tx-history
meta.config.enable_rpc_transaction_history = false;
let req = r#"{"jsonrpc":"2.0","id":1,"method":"getConfirmedBlock","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]