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

Commit 9f9d6f8

Browse files
Relax Sized bound for Signers in Transaction and Client (#30568)
* 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
1 parent 38e054f commit 9f9d6f8

File tree

12 files changed

+48
-30
lines changed

12 files changed

+48
-30
lines changed

client/src/connection_cache.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ impl Default for ConnectionCache {
145145
}
146146

147147
macro_rules! dispatch {
148-
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
148+
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident + ?Sized),*>)?(&self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
149149
#[inline]
150150
$(#[$meta])*
151-
$vis fn $name$(<$($t: $cons),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
151+
$vis fn $name$(<$($t: $cons + ?Sized),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
152152
match self {
153153
Self::Quic(this) => this.$name($($arg, )*),
154154
Self::Udp(this) => this.$name($($arg, )*),
155155
}
156156
}
157157
};
158-
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&mut self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
158+
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident + ?Sized),*>)?(&mut self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
159159
#[inline]
160160
$(#[$meta])*
161-
$vis fn $name$(<$($t: $cons),*>)?(&mut self $(, $arg:$ty)*) $(-> $out)? {
161+
$vis fn $name$(<$($t: $cons + ?Sized),*>)?(&mut self $(, $arg:$ty)*) $(-> $out)? {
162162
match self {
163163
Self::Quic(this) => this.$name($($arg, )*),
164164
Self::Udp(this) => this.$name($($arg, )*),

client/src/nonblocking/tpu_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ where
120120
})
121121
}
122122

123-
pub async fn send_and_confirm_messages_with_spinner<T: Signers>(
123+
pub async fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
124124
&self,
125125
messages: &[Message],
126126
signers: &T,

client/src/thin_client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl ThinClient {
124124
tries: usize
125125
) -> TransportResult<Signature>);
126126

127-
dispatch!(pub fn send_and_confirm_transaction<T: Signers>(
127+
dispatch!(pub fn send_and_confirm_transaction<T: Signers + ?Sized>(
128128
&self,
129129
keypairs: &T,
130130
transaction: &mut Transaction,
@@ -172,7 +172,7 @@ impl Client for ThinClient {
172172
}
173173

174174
impl SyncClient for ThinClient {
175-
dispatch!(fn send_and_confirm_message<T: Signers>(
175+
dispatch!(fn send_and_confirm_message<T: Signers + ?Sized>(
176176
&self,
177177
keypairs: &T,
178178
message: Message

client/src/tpu_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ where
111111
})
112112
}
113113

114-
pub fn send_and_confirm_messages_with_spinner<T: Signers>(
114+
pub fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
115115
&self,
116116
messages: &[Message],
117117
signers: &T,

runtime/src/bank_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl AsyncClient for BankClient {
5151
}
5252

5353
impl SyncClient for BankClient {
54-
fn send_and_confirm_message<T: Signers>(
54+
fn send_and_confirm_message<T: Signers + ?Sized>(
5555
&self,
5656
keypairs: &T,
5757
message: Message,

sdk/program/src/example_mocks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub mod solana_sdk {
213213
}
214214

215215
impl VersionedTransaction {
216-
pub fn try_new<T: Signers>(
216+
pub fn try_new<T: Signers + ?Sized>(
217217
message: VersionedMessage,
218218
_keypairs: &T,
219219
) -> std::result::Result<Self, SignerError> {
@@ -230,7 +230,7 @@ pub mod solana_sdk {
230230
}
231231

232232
impl Transaction {
233-
pub fn new<T: Signers>(
233+
pub fn new<T: Signers + ?Sized>(
234234
_from_keypairs: &T,
235235
_message: Message,
236236
_recent_blockhash: Hash,
@@ -252,7 +252,7 @@ pub mod solana_sdk {
252252
}
253253
}
254254

255-
pub fn new_signed_with_payer<T: Signers>(
255+
pub fn new_signed_with_payer<T: Signers + ?Sized>(
256256
instructions: &[Instruction],
257257
payer: Option<&Pubkey>,
258258
signing_keypairs: &T,
@@ -262,9 +262,9 @@ pub mod solana_sdk {
262262
Self::new(signing_keypairs, message, recent_blockhash)
263263
}
264264

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

267-
pub fn try_sign<T: Signers>(
267+
pub fn try_sign<T: Signers + ?Sized>(
268268
&mut self,
269269
_keypairs: &T,
270270
_recent_blockhash: Hash,

sdk/src/client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub trait Client: SyncClient + AsyncClient {
3434
pub trait SyncClient {
3535
/// Create a transaction from the given message, and send it to the
3636
/// server, retrying as-needed.
37-
fn send_and_confirm_message<T: Signers>(
37+
fn send_and_confirm_message<T: Signers + ?Sized>(
3838
&self,
3939
keypairs: &T,
4040
message: Message,
@@ -204,7 +204,7 @@ pub trait AsyncClient {
204204

205205
/// Create a transaction from the given message, and send it to the
206206
/// server, but don't wait for to see if the server accepted it.
207-
fn async_send_message<T: Signers>(
207+
fn async_send_message<T: Signers + ?Sized>(
208208
&self,
209209
keypairs: &T,
210210
message: Message,

sdk/src/transaction/mod.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl Transaction {
346346
/// #
347347
/// # Ok::<(), anyhow::Error>(())
348348
/// ```
349-
pub fn new<T: Signers>(
349+
pub fn new<T: Signers + ?Sized>(
350350
from_keypairs: &T,
351351
message: Message,
352352
recent_blockhash: Hash,
@@ -501,7 +501,7 @@ impl Transaction {
501501
/// #
502502
/// # Ok::<(), anyhow::Error>(())
503503
/// ```
504-
pub fn new_signed_with_payer<T: Signers>(
504+
pub fn new_signed_with_payer<T: Signers + ?Sized>(
505505
instructions: &[Instruction],
506506
payer: Option<&Pubkey>,
507507
signing_keypairs: &T,
@@ -526,7 +526,7 @@ impl Transaction {
526526
///
527527
/// Panics when signing fails. See [`Transaction::try_sign`] and for a full
528528
/// description of failure conditions.
529-
pub fn new_with_compiled_instructions<T: Signers>(
529+
pub fn new_with_compiled_instructions<T: Signers + ?Sized>(
530530
from_keypairs: &T,
531531
keys: &[Pubkey],
532532
recent_blockhash: Hash,
@@ -705,7 +705,7 @@ impl Transaction {
705705
/// #
706706
/// # Ok::<(), anyhow::Error>(())
707707
/// ```
708-
pub fn sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
708+
pub fn sign<T: Signers + ?Sized>(&mut self, keypairs: &T, recent_blockhash: Hash) {
709709
if let Err(e) = self.try_sign(keypairs, recent_blockhash) {
710710
panic!("Transaction::sign failed with error {e:?}");
711711
}
@@ -731,7 +731,7 @@ impl Transaction {
731731
/// handle the error. See the documentation for
732732
/// [`Transaction::try_partial_sign`] for a full description of failure
733733
/// conditions.
734-
pub fn partial_sign<T: Signers>(&mut self, keypairs: &T, recent_blockhash: Hash) {
734+
pub fn partial_sign<T: Signers + ?Sized>(&mut self, keypairs: &T, recent_blockhash: Hash) {
735735
if let Err(e) = self.try_partial_sign(keypairs, recent_blockhash) {
736736
panic!("Transaction::partial_sign failed with error {e:?}");
737737
}
@@ -750,7 +750,7 @@ impl Transaction {
750750
///
751751
/// Panics if signing fails. Use [`Transaction::try_partial_sign_unchecked`]
752752
/// to handle the error.
753-
pub fn partial_sign_unchecked<T: Signers>(
753+
pub fn partial_sign_unchecked<T: Signers + ?Sized>(
754754
&mut self,
755755
keypairs: &T,
756756
positions: Vec<usize>,
@@ -843,7 +843,7 @@ impl Transaction {
843843
/// #
844844
/// # Ok::<(), anyhow::Error>(())
845845
/// ```
846-
pub fn try_sign<T: Signers>(
846+
pub fn try_sign<T: Signers + ?Sized>(
847847
&mut self,
848848
keypairs: &T,
849849
recent_blockhash: Hash,
@@ -906,7 +906,7 @@ impl Transaction {
906906
/// [`PresignerError::VerificationFailure`]: crate::signer::presigner::PresignerError::VerificationFailure
907907
/// [`solana-remote-wallet`]: https://docs.rs/solana-remote-wallet/latest/
908908
/// [`RemoteKeypair`]: https://docs.rs/solana-remote-wallet/latest/solana_remote_wallet/remote_keypair/struct.RemoteKeypair.html
909-
pub fn try_partial_sign<T: Signers>(
909+
pub fn try_partial_sign<T: Signers + ?Sized>(
910910
&mut self,
911911
keypairs: &T,
912912
recent_blockhash: Hash,
@@ -932,7 +932,7 @@ impl Transaction {
932932
/// # Errors
933933
///
934934
/// Returns an error if signing fails.
935-
pub fn try_partial_sign_unchecked<T: Signers>(
935+
pub fn try_partial_sign_unchecked<T: Signers + ?Sized>(
936936
&mut self,
937937
keypairs: &T,
938938
positions: Vec<usize>,
@@ -1668,4 +1668,22 @@ mod tests {
16681668
.unwrap_err();
16691669
assert_eq!(err, SignerError::KeypairPubkeyMismatch);
16701670
}
1671+
1672+
#[test]
1673+
fn test_unsized_signers() {
1674+
fn instructions_to_tx(
1675+
instructions: &[Instruction],
1676+
signers: Box<dyn Signers>,
1677+
) -> Transaction {
1678+
let pubkeys = signers.pubkeys();
1679+
let first_signer = pubkeys.first().expect("should exist");
1680+
let message = Message::new(instructions, Some(first_signer));
1681+
Transaction::new(signers.as_ref(), message, Hash::default())
1682+
}
1683+
1684+
let signer: Box<dyn Signer> = Box::new(Keypair::new());
1685+
let tx = instructions_to_tx(&[], Box::new(vec![signer]));
1686+
1687+
assert!(tx.is_signed());
1688+
}
16711689
}

sdk/src/transaction/versioned/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl From<Transaction> for VersionedTransaction {
6868
impl VersionedTransaction {
6969
/// Signs a versioned message and if successful, returns a signed
7070
/// transaction.
71-
pub fn try_new<T: Signers>(
71+
pub fn try_new<T: Signers + ?Sized>(
7272
message: VersionedMessage,
7373
keypairs: &T,
7474
) -> std::result::Result<Self, SignerError> {

thin-client/src/thin_client.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ where
209209
self.send_and_confirm_transaction(&[keypair], transaction, tries, 0)
210210
}
211211

212-
pub fn send_and_confirm_transaction<T: Signers>(
212+
pub fn send_and_confirm_transaction<T: Signers + ?Sized>(
213213
&self,
214214
keypairs: &T,
215215
transaction: &mut Transaction,
@@ -340,7 +340,7 @@ where
340340
M: ConnectionManager<ConnectionPool = P, NewConnectionConfig = C>,
341341
C: NewConnectionConfig,
342342
{
343-
fn send_and_confirm_message<T: Signers>(
343+
fn send_and_confirm_message<T: Signers + ?Sized>(
344344
&self,
345345
keypairs: &T,
346346
message: Message,

tpu-client/src/nonblocking/tpu_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ where
437437
}
438438

439439
#[cfg(feature = "spinner")]
440-
pub async fn send_and_confirm_messages_with_spinner<T: Signers>(
440+
pub async fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
441441
&self,
442442
messages: &[Message],
443443
signers: &T,

tpu-client/src/tpu_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158
}
159159

160160
#[cfg(feature = "spinner")]
161-
pub fn send_and_confirm_messages_with_spinner<T: Signers>(
161+
pub fn send_and_confirm_messages_with_spinner<T: Signers + ?Sized>(
162162
&self,
163163
messages: &[Message],
164164
signers: &T,

0 commit comments

Comments
 (0)