-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
functional.rs
207 lines (187 loc) · 7.22 KB
/
functional.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![cfg(feature = "test-sbf")]
use {
solana_instruction::{error::InstructionError, AccountMeta, Instruction},
solana_program_test::*,
solana_pubkey::Pubkey,
solana_sdk::{
signature::{Keypair, Signer},
transaction::{Transaction, TransactionError},
},
spl_memo::*,
};
fn program_test() -> ProgramTest {
ProgramTest::new("spl_memo", id(), processor!(processor::process_instruction))
}
#[tokio::test]
async fn test_memo_signing() {
let memo = "🐆".as_bytes();
let (banks_client, payer, recent_blockhash) = program_test().start().await;
let keypairs = vec![Keypair::new(), Keypair::new(), Keypair::new()];
let pubkeys: Vec<Pubkey> = keypairs.iter().map(|keypair| keypair.pubkey()).collect();
// Test complete signing
let signer_key_refs: Vec<&Pubkey> = pubkeys.iter().collect();
let mut transaction =
Transaction::new_with_payer(&[build_memo(memo, &signer_key_refs)], Some(&payer.pubkey()));
let mut signers = vec![&payer];
for keypair in keypairs.iter() {
signers.push(keypair);
}
transaction.sign(&signers, recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
// Test unsigned memo
let mut transaction =
Transaction::new_with_payer(&[build_memo(memo, &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
// Demonstrate success on signature provided, regardless of specific memo
// AccountMeta
let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new_readonly(keypairs[0].pubkey(), true),
AccountMeta::new_readonly(keypairs[1].pubkey(), true),
AccountMeta::new_readonly(payer.pubkey(), false),
],
data: memo.to_vec(),
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer, &keypairs[0], &keypairs[1]], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
// Test missing signer(s)
let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new_readonly(keypairs[0].pubkey(), true),
AccountMeta::new_readonly(keypairs[1].pubkey(), false),
AccountMeta::new_readonly(keypairs[2].pubkey(), true),
],
data: memo.to_vec(),
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer, &keypairs[0], &keypairs[2]], recent_blockhash);
assert_eq!(
banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature)
);
let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id: id(),
accounts: vec![
AccountMeta::new_readonly(keypairs[0].pubkey(), false),
AccountMeta::new_readonly(keypairs[1].pubkey(), false),
AccountMeta::new_readonly(keypairs[2].pubkey(), false),
],
data: memo.to_vec(),
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
assert_eq!(
banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature)
);
// Test invalid utf-8; demonstrate log
let invalid_utf8 = [0xF0, 0x9F, 0x90, 0x86, 0xF0, 0x9F, 0xFF, 0x86];
let mut transaction =
Transaction::new_with_payer(&[build_memo(&invalid_utf8, &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
assert_eq!(
banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::InvalidInstructionData)
);
}
#[tokio::test]
#[ignore]
async fn test_memo_compute_limits() {
let (banks_client, payer, recent_blockhash) = program_test().start().await;
// Test memo length
let mut memo = vec![];
for _ in 0..1000 {
let mut vec = vec![0x53, 0x4F, 0x4C];
memo.append(&mut vec);
}
let mut transaction =
Transaction::new_with_payer(&[build_memo(&memo[..450], &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
let mut transaction =
Transaction::new_with_payer(&[build_memo(&memo[..600], &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
let err = banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap();
let failed_to_complete =
TransactionError::InstructionError(0, InstructionError::ProgramFailedToComplete);
let computational_budget_exceeded =
TransactionError::InstructionError(0, InstructionError::ComputationalBudgetExceeded);
assert!(err == failed_to_complete || err == computational_budget_exceeded);
let mut memo = vec![];
for _ in 0..100 {
let mut vec = vec![0xE2, 0x97, 0x8E];
memo.append(&mut vec);
}
let mut transaction =
Transaction::new_with_payer(&[build_memo(&memo[..60], &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
let mut transaction =
Transaction::new_with_payer(&[build_memo(&memo[..63], &[])], Some(&payer.pubkey()));
transaction.sign(&[&payer], recent_blockhash);
let err = banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap();
assert!(err == failed_to_complete || err == computational_budget_exceeded);
// Test num signers with 32-byte memo
let memo = Pubkey::new_unique().to_bytes();
let mut keypairs = vec![];
for _ in 0..20 {
keypairs.push(Keypair::new());
}
let pubkeys: Vec<Pubkey> = keypairs.iter().map(|keypair| keypair.pubkey()).collect();
let signer_key_refs: Vec<&Pubkey> = pubkeys.iter().collect();
let mut signers = vec![&payer];
for keypair in keypairs[..12].iter() {
signers.push(keypair);
}
let mut transaction = Transaction::new_with_payer(
&[build_memo(&memo, &signer_key_refs[..12])],
Some(&payer.pubkey()),
);
transaction.sign(&signers, recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
let mut signers = vec![&payer];
for keypair in keypairs[..15].iter() {
signers.push(keypair);
}
let mut transaction = Transaction::new_with_payer(
&[build_memo(&memo, &signer_key_refs[..15])],
Some(&payer.pubkey()),
);
transaction.sign(&signers, recent_blockhash);
let err = banks_client
.process_transaction(transaction)
.await
.unwrap_err()
.unwrap();
assert!(err == failed_to_complete || err == computational_budget_exceeded);
}