Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Relax Sized bound for Signers in Transaction and Client (#30568)
Browse files Browse the repository at this point in the history
* relax Sized bound for Signers in Transaction and Client

* also relax Sized bounds in client, runtime, thin-client and tpu-client

* add tests for using non-sized transaction signers

* fix macro for thin_client vs ?Sized

* move tests to transactions, add Sized relaxation to mut macro

* fix clippy warning

* get rid of unnecessary imports
  • Loading branch information
blackghost1987 authored Mar 30, 2023
1 parent 38e054f commit 9f9d6f8
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions client/src/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ impl Default for ConnectionCache {
}

macro_rules! dispatch {
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident + ?Sized),*>)?(&self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$(#[$meta])*
$vis fn $name$(<$($t: $cons),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
$vis fn $name$(<$($t: $cons + ?Sized),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg, )*),
Self::Udp(this) => this.$name($($arg, )*),
}
}
};
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&mut self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident + ?Sized),*>)?(&mut self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$(#[$meta])*
$vis fn $name$(<$($t: $cons),*>)?(&mut self $(, $arg:$ty)*) $(-> $out)? {
$vis fn $name$(<$($t: $cons + ?Sized),*>)?(&mut self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg, )*),
Self::Udp(this) => this.$name($($arg, )*),
Expand Down
2 changes: 1 addition & 1 deletion client/src/nonblocking/tpu_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ where
})
}

pub async fn send_and_confirm_messages_with_spinner<T: Signers>(
pub async fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
&self,
messages: &[Message],
signers: &T,
Expand Down
4 changes: 2 additions & 2 deletions client/src/thin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl ThinClient {
tries: usize
) -> TransportResult<Signature>);

dispatch!(pub fn send_and_confirm_transaction<T: Signers>(
dispatch!(pub fn send_and_confirm_transaction<T: Signers + ?Sized>(
&self,
keypairs: &T,
transaction: &mut Transaction,
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Client for ThinClient {
}

impl SyncClient for ThinClient {
dispatch!(fn send_and_confirm_message<T: Signers>(
dispatch!(fn send_and_confirm_message<T: Signers + ?Sized>(
&self,
keypairs: &T,
message: Message
Expand Down
2 changes: 1 addition & 1 deletion client/src/tpu_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
})
}

pub fn send_and_confirm_messages_with_spinner<T: Signers>(
pub fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
&self,
messages: &[Message],
signers: &T,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl AsyncClient for BankClient {
}

impl SyncClient for BankClient {
fn send_and_confirm_message<T: Signers>(
fn send_and_confirm_message<T: Signers + ?Sized>(
&self,
keypairs: &T,
message: Message,
Expand Down
10 changes: 5 additions & 5 deletions sdk/program/src/example_mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub mod solana_sdk {
}

impl VersionedTransaction {
pub fn try_new<T: Signers>(
pub fn try_new<T: Signers + ?Sized>(
message: VersionedMessage,
_keypairs: &T,
) -> std::result::Result<Self, SignerError> {
Expand All @@ -230,7 +230,7 @@ pub mod solana_sdk {
}

impl Transaction {
pub fn new<T: Signers>(
pub fn new<T: Signers + ?Sized>(
_from_keypairs: &T,
_message: Message,
_recent_blockhash: Hash,
Expand All @@ -252,7 +252,7 @@ pub mod solana_sdk {
}
}

pub fn new_signed_with_payer<T: Signers>(
pub fn new_signed_with_payer<T: Signers + ?Sized>(
instructions: &[Instruction],
payer: Option<&Pubkey>,
signing_keypairs: &T,
Expand All @@ -262,9 +262,9 @@ pub mod solana_sdk {
Self::new(signing_keypairs, message, recent_blockhash)
}

pub fn sign<T: Signers>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}

pub fn try_sign<T: Signers>(
pub fn try_sign<T: Signers + ?Sized>(
&mut self,
_keypairs: &T,
_recent_blockhash: Hash,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait Client: SyncClient + AsyncClient {
pub trait SyncClient {
/// Create a transaction from the given message, and send it to the
/// server, retrying as-needed.
fn send_and_confirm_message<T: Signers>(
fn send_and_confirm_message<T: Signers + ?Sized>(
&self,
keypairs: &T,
message: Message,
Expand Down Expand Up @@ -204,7 +204,7 @@ pub trait AsyncClient {

/// Create a transaction from the given message, and send it to the
/// server, but don't wait for to see if the server accepted it.
fn async_send_message<T: Signers>(
fn async_send_message<T: Signers + ?Sized>(
&self,
keypairs: &T,
message: Message,
Expand Down
36 changes: 27 additions & 9 deletions sdk/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Transaction {
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new<T: Signers>(
pub fn new<T: Signers + ?Sized>(
from_keypairs: &T,
message: Message,
recent_blockhash: Hash,
Expand Down Expand Up @@ -501,7 +501,7 @@ impl Transaction {
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn new_signed_with_payer<T: Signers>(
pub fn new_signed_with_payer<T: Signers + ?Sized>(
instructions: &[Instruction],
payer: Option<&Pubkey>,
signing_keypairs: &T,
Expand All @@ -526,7 +526,7 @@ impl Transaction {
///
/// Panics when signing fails. See [`Transaction::try_sign`] and for a full
/// description of failure conditions.
pub fn new_with_compiled_instructions<T: Signers>(
pub fn new_with_compiled_instructions<T: Signers + ?Sized>(
from_keypairs: &T,
keys: &[Pubkey],
recent_blockhash: Hash,
Expand Down Expand Up @@ -705,7 +705,7 @@ impl Transaction {
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
pub fn sign<T: Signers + ?Sized>(&mut self, keypairs: &T, recent_blockhash: Hash) {
if let Err(e) = self.try_sign(keypairs, recent_blockhash) {
panic!("Transaction::sign failed with error {e:?}");
}
Expand All @@ -731,7 +731,7 @@ impl Transaction {
/// handle the error. See the documentation for
/// [`Transaction::try_partial_sign`] for a full description of failure
/// conditions.
pub fn partial_sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
pub fn partial_sign<T: Signers + ?Sized>(&mut self, keypairs: &T, recent_blockhash: Hash) {
if let Err(e) = self.try_partial_sign(keypairs, recent_blockhash) {
panic!("Transaction::partial_sign failed with error {e:?}");
}
Expand All @@ -750,7 +750,7 @@ impl Transaction {
///
/// Panics if signing fails. Use [`Transaction::try_partial_sign_unchecked`]
/// to handle the error.
pub fn partial_sign_unchecked<T: Signers>(
pub fn partial_sign_unchecked<T: Signers + ?Sized>(
&mut self,
keypairs: &T,
positions: Vec<usize>,
Expand Down Expand Up @@ -843,7 +843,7 @@ impl Transaction {
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn try_sign<T: Signers>(
pub fn try_sign<T: Signers + ?Sized>(
&mut self,
keypairs: &T,
recent_blockhash: Hash,
Expand Down Expand Up @@ -906,7 +906,7 @@ impl Transaction {
/// [`PresignerError::VerificationFailure`]: crate::signer::presigner::PresignerError::VerificationFailure
/// [`solana-remote-wallet`]: https://docs.rs/solana-remote-wallet/latest/
/// [`RemoteKeypair`]: https://docs.rs/solana-remote-wallet/latest/solana_remote_wallet/remote_keypair/struct.RemoteKeypair.html
pub fn try_partial_sign<T: Signers>(
pub fn try_partial_sign<T: Signers + ?Sized>(
&mut self,
keypairs: &T,
recent_blockhash: Hash,
Expand All @@ -932,7 +932,7 @@ impl Transaction {
/// # Errors
///
/// Returns an error if signing fails.
pub fn try_partial_sign_unchecked<T: Signers>(
pub fn try_partial_sign_unchecked<T: Signers + ?Sized>(
&mut self,
keypairs: &T,
positions: Vec<usize>,
Expand Down Expand Up @@ -1668,4 +1668,22 @@ mod tests {
.unwrap_err();
assert_eq!(err, SignerError::KeypairPubkeyMismatch);
}

#[test]
fn test_unsized_signers() {
fn instructions_to_tx(
instructions: &[Instruction],
signers: Box<dyn Signers>,
) -> Transaction {
let pubkeys = signers.pubkeys();
let first_signer = pubkeys.first().expect("should exist");
let message = Message::new(instructions, Some(first_signer));
Transaction::new(signers.as_ref(), message, Hash::default())
}

let signer: Box<dyn Signer> = Box::new(Keypair::new());
let tx = instructions_to_tx(&[], Box::new(vec![signer]));

assert!(tx.is_signed());
}
}
2 changes: 1 addition & 1 deletion sdk/src/transaction/versioned/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl From<Transaction> for VersionedTransaction {
impl VersionedTransaction {
/// Signs a versioned message and if successful, returns a signed
/// transaction.
pub fn try_new<T: Signers>(
pub fn try_new<T: Signers + ?Sized>(
message: VersionedMessage,
keypairs: &T,
) -> std::result::Result<Self, SignerError> {
Expand Down
4 changes: 2 additions & 2 deletions thin-client/src/thin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
self.send_and_confirm_transaction(&[keypair], transaction, tries, 0)
}

pub fn send_and_confirm_transaction<T: Signers>(
pub fn send_and_confirm_transaction<T: Signers + ?Sized>(
&self,
keypairs: &T,
transaction: &mut Transaction,
Expand Down Expand Up @@ -340,7 +340,7 @@ where
M: ConnectionManager<ConnectionPool = P, NewConnectionConfig = C>,
C: NewConnectionConfig,
{
fn send_and_confirm_message<T: Signers>(
fn send_and_confirm_message<T: Signers + ?Sized>(
&self,
keypairs: &T,
message: Message,
Expand Down
2 changes: 1 addition & 1 deletion tpu-client/src/nonblocking/tpu_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ where
}

#[cfg(feature = "spinner")]
pub async fn send_and_confirm_messages_with_spinner<T: Signers>(
pub async fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
&self,
messages: &[Message],
signers: &T,
Expand Down
2 changes: 1 addition & 1 deletion tpu-client/src/tpu_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where
}

#[cfg(feature = "spinner")]
pub fn send_and_confirm_messages_with_spinner<T: Signers>(
pub fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
&self,
messages: &[Message],
signers: &T,
Expand Down

0 comments on commit 9f9d6f8

Please sign in to comment.