-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathtest_http_client.py
540 lines (452 loc) · 20.8 KB
/
test_http_client.py
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
"""Tests for the HTTP API Client."""
from typing import Tuple
import pytest
import solders.system_program as sp
from solders.keypair import Keypair
from solders.message import MessageV0, Message
from solders.pubkey import Pubkey
from solders.rpc.errors import SendTransactionPreflightFailureMessage
from solders.rpc.requests import GetBlockHeight, GetFirstAvailableBlock
from solders.rpc.responses import GetBlockHeightResp, GetFirstAvailableBlockResp, Resp
from solders.transaction import VersionedTransaction
from solana.constants import VOTE_PROGRAM_ID
from solana.rpc.api import Client
from solana.rpc.commitment import Confirmed, Finalized, Processed
from solana.rpc.core import RPCException, TransactionExpiredBlockheightExceededError
from solana.rpc.types import DataSliceOpts, TxOpts
from solders.transaction import Transaction
from spl.token.constants import WRAPPED_SOL_MINT
from ..utils import AIRDROP_AMOUNT, assert_valid_response
@pytest.mark.integration
def test_request_air_drop(stubbed_sender: Keypair, stubbed_receiver: Pubkey, test_http_client: Client):
"""Test air drop to stubbed_sender and stubbed_receiver."""
# Airdrop to stubbed_sender
resp = test_http_client.request_airdrop(stubbed_sender.pubkey(), AIRDROP_AMOUNT)
assert_valid_response(resp)
test_http_client.confirm_transaction(resp.value)
balance = test_http_client.get_balance(stubbed_sender.pubkey())
assert balance.value == AIRDROP_AMOUNT
# Airdrop to stubbed_receiver
resp = test_http_client.request_airdrop(stubbed_receiver, AIRDROP_AMOUNT)
assert_valid_response(resp)
test_http_client.confirm_transaction(resp.value)
balance = test_http_client.get_balance(stubbed_receiver)
assert balance.value == AIRDROP_AMOUNT
@pytest.mark.integration
def test_request_air_drop_prefetched_blockhash(
stubbed_sender_prefetched_blockhash, stubbed_receiver_prefetched_blockhash, test_http_client: Client
):
"""Test air drop to stubbed_sender and stubbed_receiver."""
# Airdrop to stubbed_sender
resp = test_http_client.request_airdrop(stubbed_sender_prefetched_blockhash.pubkey(), AIRDROP_AMOUNT)
assert_valid_response(resp)
test_http_client.confirm_transaction(resp.value)
balance = test_http_client.get_balance(stubbed_sender_prefetched_blockhash.pubkey())
assert balance.value == AIRDROP_AMOUNT
# Airdrop to stubbed_receiver
resp = test_http_client.request_airdrop(stubbed_receiver_prefetched_blockhash, AIRDROP_AMOUNT)
assert_valid_response(resp)
test_http_client.confirm_transaction(resp.value)
balance = test_http_client.get_balance(stubbed_receiver_prefetched_blockhash)
assert balance.value == AIRDROP_AMOUNT
@pytest.mark.integration
def test_send_transaction_and_get_balance(stubbed_sender, stubbed_receiver, test_http_client: Client):
"""Test sending a transaction to localnet."""
# Create transfer tx to transfer lamports from stubbed sender to stubbed_receiver
blockhash = test_http_client.get_latest_blockhash().value.blockhash
ixs = [
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
]
msg = Message.new_with_blockhash(ixs, stubbed_sender.pubkey(), blockhash)
transfer_tx = Transaction([stubbed_sender], msg, blockhash)
sim_resp = test_http_client.simulate_transaction(transfer_tx)
assert_valid_response(sim_resp)
resp = test_http_client.send_transaction(transfer_tx)
assert_valid_response(resp)
# Confirm transaction
test_http_client.confirm_transaction(resp.value)
# Check balances
bal_resp = test_http_client.get_balance(stubbed_sender.pubkey())
assert_valid_response(bal_resp)
assert bal_resp.value == 9999994000
bal_resp2 = test_http_client.get_balance(stubbed_receiver)
assert_valid_response(bal_resp2)
assert bal_resp2.value == 10000001000
@pytest.mark.integration
def test_send_versioned_transaction_and_get_balance(random_funded_keypair: Keypair, test_http_client: Client):
"""Test sending a transaction to localnet."""
receiver = Keypair()
amount = 1_000_000
transfer_ix = sp.transfer(
sp.TransferParams(from_pubkey=random_funded_keypair.pubkey(), to_pubkey=receiver.pubkey(), lamports=amount)
)
recent_blockhash = test_http_client.get_latest_blockhash().value.blockhash
msg = MessageV0.try_compile(
payer=random_funded_keypair.pubkey(),
instructions=[transfer_ix],
address_lookup_table_accounts=[],
recent_blockhash=recent_blockhash,
)
transfer_tx = VersionedTransaction(msg, [random_funded_keypair])
resp = test_http_client.send_transaction(transfer_tx)
assert_valid_response(resp)
# Confirm transaction
test_http_client.confirm_transaction(resp.value)
# Check balances
sender_balance_resp = test_http_client.get_balance(random_funded_keypair.pubkey())
assert_valid_response(sender_balance_resp)
assert sender_balance_resp.value == AIRDROP_AMOUNT - amount - 5000
receiver_balance_resp = test_http_client.get_balance(receiver.pubkey())
assert_valid_response(receiver_balance_resp)
assert receiver_balance_resp.value == amount
@pytest.mark.integration
def test_send_bad_transaction(stubbed_receiver: Pubkey, test_http_client: Client):
"""Test sending a transaction that errors."""
poor_account = Keypair()
airdrop_amount = 1000000
airdrop_resp = test_http_client.request_airdrop(poor_account.pubkey(), airdrop_amount)
assert_valid_response(airdrop_resp)
test_http_client.confirm_transaction(airdrop_resp.value)
balance = test_http_client.get_balance(poor_account.pubkey())
assert balance.value == airdrop_amount
# Create transfer tx to transfer lamports from stubbed sender to stubbed_receiver
blockhash = test_http_client.get_latest_blockhash().value.blockhash
ixs = [
sp.transfer(
sp.TransferParams(
from_pubkey=poor_account.pubkey(), to_pubkey=stubbed_receiver, lamports=airdrop_amount + 1
)
)
]
msg = Message.new_with_blockhash(ixs, poor_account.pubkey(), blockhash)
transfer_tx = Transaction([poor_account], msg, blockhash)
with pytest.raises(RPCException) as exc_info:
test_http_client.send_transaction(transfer_tx)
err = exc_info.value.args[0]
assert isinstance(err, SendTransactionPreflightFailureMessage)
assert err.data.logs
@pytest.mark.integration
def test_send_transaction_prefetched_blockhash(
stubbed_sender_prefetched_blockhash, stubbed_receiver_prefetched_blockhash, test_http_client
):
"""Test sending a transaction to localnet."""
# Create transfer tx to transfer lamports from stubbed sender to stubbed_receiver
recent_blockhash = test_http_client.parse_recent_blockhash(test_http_client.get_latest_blockhash())
ixs = [
sp.transfer(
sp.TransferParams(
from_pubkey=stubbed_sender_prefetched_blockhash.pubkey(),
to_pubkey=stubbed_receiver_prefetched_blockhash,
lamports=1000,
)
)
]
msg = Message.new_with_blockhash(ixs, stubbed_sender_prefetched_blockhash.pubkey(), recent_blockhash)
transfer_tx = Transaction([stubbed_sender_prefetched_blockhash], msg, recent_blockhash)
resp = test_http_client.send_transaction(transfer_tx)
assert_valid_response(resp)
# Confirm transaction
test_http_client.confirm_transaction(resp.value)
# Check balances
resp = test_http_client.get_balance(stubbed_sender_prefetched_blockhash.pubkey())
assert_valid_response(resp)
assert resp.value == 9999994000
resp = test_http_client.get_balance(stubbed_receiver_prefetched_blockhash)
assert_valid_response(resp)
assert resp.value == 10000001000
@pytest.mark.integration
def test_send_raw_transaction_and_get_balance(stubbed_sender, stubbed_receiver, test_http_client: Client):
"""Test sending a raw transaction to localnet."""
# Get a recent blockhash
resp = test_http_client.get_latest_blockhash()
assert_valid_response(resp)
recent_blockhash = resp.value.blockhash
assert recent_blockhash is not None
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
blockhash = test_http_client.get_latest_blockhash().value.blockhash
ixs = [
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
]
msg = Message.new_with_blockhash(ixs, stubbed_sender.pubkey(), blockhash)
transfer_tx = Transaction([stubbed_sender], msg, blockhash)
# Send raw transaction
tx_resp = test_http_client.send_raw_transaction(bytes(transfer_tx))
assert_valid_response(tx_resp)
# Confirm transaction
test_http_client.confirm_transaction(tx_resp.value)
# Check balances
bal_resp = test_http_client.get_balance(stubbed_sender.pubkey())
assert_valid_response(bal_resp)
assert bal_resp.value == 9999988000
bal_resp2 = test_http_client.get_balance(stubbed_receiver)
assert_valid_response(bal_resp2)
assert bal_resp2.value == 10000002000
@pytest.mark.integration
def test_send_raw_transaction_and_get_balance_using_latest_blockheight(
stubbed_sender, stubbed_receiver, test_http_client
):
"""Test sending a raw transaction to localnet using latest blockhash."""
# Get a recent blockhash
resp = test_http_client.get_latest_blockhash(Finalized)
assert_valid_response(resp)
recent_blockhash = resp.value.blockhash
assert recent_blockhash is not None
last_valid_block_height = resp.value.last_valid_block_height
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
blockhash = test_http_client.get_latest_blockhash().value.blockhash
ixs = [
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
]
msg = Message.new_with_blockhash(ixs, stubbed_sender.pubkey(), blockhash)
transfer_tx = Transaction([stubbed_sender], msg, blockhash)
# Send raw transaction
resp = test_http_client.send_raw_transaction(
bytes(transfer_tx),
opts=TxOpts(preflight_commitment=Processed, last_valid_block_height=last_valid_block_height),
)
assert_valid_response(resp)
# Confirm transaction
test_http_client.confirm_transaction(resp.value, last_valid_block_height=last_valid_block_height)
# Check balances
resp = test_http_client.get_balance(stubbed_sender.pubkey())
assert_valid_response(resp)
assert resp.value == 9999982000
resp = test_http_client.get_balance(stubbed_receiver)
assert_valid_response(resp)
assert resp.value == 10000003000
@pytest.mark.integration
def test_confirm_expired_transaction(stubbed_sender, stubbed_receiver, test_http_client: Client):
"""Test that RPCException is raised when trying to confirm a transaction that exceeded last valid block height."""
# Get a recent blockhash
resp = test_http_client.get_latest_blockhash()
recent_blockhash = resp.value.blockhash
assert recent_blockhash is not None
last_valid_block_height = resp.value.last_valid_block_height - 330
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
ixs = [
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
]
msg = Message.new_with_blockhash(ixs, stubbed_sender.pubkey(), recent_blockhash)
transfer_tx = Transaction([stubbed_sender], msg, recent_blockhash)
# Send raw transaction
tx_resp = test_http_client.send_raw_transaction(
bytes(transfer_tx), opts=TxOpts(skip_confirmation=True, skip_preflight=True)
)
assert_valid_response(tx_resp)
# Confirm transaction
with pytest.raises(TransactionExpiredBlockheightExceededError) as exc_info:
test_http_client.confirm_transaction(tx_resp.value, Finalized, last_valid_block_height=last_valid_block_height)
err_object = exc_info.value.args[0]
assert "block height exceeded" in err_object
@pytest.mark.integration
def test_get_fee_for_transaction(stubbed_sender, stubbed_receiver, test_http_client: Client):
"""Test that gets a fee for a transaction using get_fee_for_message."""
# Get a recent blockhash
resp = test_http_client.get_latest_blockhash()
recent_blockhash = resp.value.blockhash
assert recent_blockhash is not None
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
ixs = [
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
]
msg = Message.new_with_blockhash(ixs, stubbed_sender.pubkey(), recent_blockhash)
# get fee for transaction
fee_resp = test_http_client.get_fee_for_message(msg)
assert_valid_response(fee_resp)
assert fee_resp.value is not None
@pytest.mark.integration
def test_get_fee_for_versioned_message(stubbed_sender: Keypair, stubbed_receiver: Pubkey, test_http_client: Client):
"""Test that gets a fee for a transaction using get_fee_for_message."""
# Get a recent blockhash
resp = test_http_client.get_latest_blockhash()
recent_blockhash = resp.value.blockhash
assert recent_blockhash is not None
msg = MessageV0.try_compile(
payer=stubbed_sender.pubkey(),
instructions=[
sp.transfer(
sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000)
)
],
address_lookup_table_accounts=[],
recent_blockhash=recent_blockhash,
)
# get fee for transaction
fee_resp = test_http_client.get_fee_for_message(msg)
assert_valid_response(fee_resp)
assert fee_resp.value is not None
@pytest.mark.integration
def test_get_block_commitment(test_http_client: Client):
"""Test get block commitment."""
resp = test_http_client.get_block_commitment(5)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_block_time(test_http_client: Client):
"""Test get block time."""
resp = test_http_client.get_block_time(5)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_cluster_nodes(test_http_client: Client):
"""Test get cluster nodes."""
resp = test_http_client.get_cluster_nodes()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_block(test_http_client: Client):
"""Test get confirmed block."""
resp = test_http_client.get_block(2)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_block_with_encoding(test_http_client: Client):
"""Test get confrimed block with encoding."""
resp = test_http_client.get_block(2, encoding="base64")
assert_valid_response(resp)
@pytest.mark.integration
def test_get_block_height(test_http_client: Client):
"""Test get height."""
resp = test_http_client.get_block_height()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_blocks(test_http_client: Client):
"""Test get confirmed blocks."""
resp = test_http_client.get_blocks(5, 10)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_signatures_for_address(test_http_client: Client):
"""Test get signatures for addresses."""
resp = test_http_client.get_signatures_for_address(VOTE_PROGRAM_ID, limit=1, commitment=Confirmed)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_epoch_info(test_http_client: Client):
"""Test get epoch info."""
resp = test_http_client.get_epoch_info()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_epoch_schedule(test_http_client: Client):
"""Test get epoch schedule."""
resp = test_http_client.get_epoch_schedule()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_latest_blockhash(test_http_client: Client):
"""Test get latest blockhash."""
resp = test_http_client.get_latest_blockhash()
assert_valid_response(resp)
assert resp.value.blockhash is not None
assert resp.value.last_valid_block_height is not None
@pytest.mark.integration
def test_get_slot(test_http_client: Client):
"""Test get slot."""
resp = test_http_client.get_slot()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_first_available_block(test_http_client: Client):
"""Test get first available block."""
resp = test_http_client.get_first_available_block()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_genesis_hash(test_http_client: Client):
"""Test get genesis hash."""
resp = test_http_client.get_genesis_hash()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_identity(test_http_client: Client):
"""Test get identity."""
resp = test_http_client.get_genesis_hash()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_inflation_governor(test_http_client: Client):
"""Test get inflation governor."""
resp = test_http_client.get_inflation_governor()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_inflation_rate(test_http_client: Client):
"""Test get inflation rate."""
resp = test_http_client.get_inflation_rate()
assert_valid_response(resp)
# XXX: Block not available for slot on local cluster
@pytest.mark.skip
@pytest.mark.integration
def test_get_inflation_reward(stubbed_sender, test_http_client: Client):
"""Test get inflation reward."""
resp = test_http_client.get_inflation_reward([stubbed_sender.pubkey()], commitment=Confirmed)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_largest_accounts(test_http_client: Client):
"""Test get largest accounts."""
resp = test_http_client.get_largest_accounts()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_leader_schedule(test_http_client: Client):
"""Test get leader schedule."""
resp = test_http_client.get_leader_schedule()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_minimum_balance_for_rent_exemption(test_http_client: Client):
"""Test get minimum balance for rent exemption."""
resp = test_http_client.get_minimum_balance_for_rent_exemption(50)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_slot_leader(test_http_client: Client):
"""Test get slot leader."""
resp = test_http_client.get_slot_leader()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_supply(test_http_client: Client):
"""Test get slot leader."""
resp = test_http_client.get_supply()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_transaction_count(test_http_client: Client):
"""Test get transactinon count."""
resp = test_http_client.get_transaction_count()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_version(test_http_client: Client):
"""Test get version."""
resp = test_http_client.get_version()
assert_valid_response(resp)
@pytest.mark.integration
def test_get_account_info(stubbed_sender, test_http_client: Client):
"""Test get_account_info."""
resp = test_http_client.get_account_info(stubbed_sender.pubkey())
assert_valid_response(resp)
resp = test_http_client.get_account_info(stubbed_sender.pubkey(), encoding="jsonParsed")
assert_valid_response(resp)
resp = test_http_client.get_account_info(stubbed_sender.pubkey(), data_slice=DataSliceOpts(1, 1))
assert_valid_response(resp)
@pytest.mark.integration
def test_get_multiple_accounts(stubbed_sender, test_http_client: Client):
"""Test get_multiple_accounts."""
pubkeys = [stubbed_sender.pubkey()] * 2
resp = test_http_client.get_multiple_accounts(pubkeys)
assert_valid_response(resp)
resp = test_http_client.get_multiple_accounts(pubkeys, encoding="jsonParsed")
assert_valid_response(resp)
resp = test_http_client.get_multiple_accounts(pubkeys, data_slice=DataSliceOpts(1, 1))
assert_valid_response(resp)
@pytest.mark.integration
def test_get_token_largest_accounts(test_http_client: Client):
"""Test get token largest accounts."""
resp = test_http_client.get_token_largest_accounts(WRAPPED_SOL_MINT)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_token_supply(test_http_client: Client):
"""Test get token supply."""
resp = test_http_client.get_token_supply(WRAPPED_SOL_MINT)
assert_valid_response(resp)
@pytest.mark.integration
def test_get_vote_accounts(test_http_client: Client):
"""Test get vote accounts."""
resp = test_http_client.get_vote_accounts()
assert_valid_response(resp)
@pytest.mark.integration
def test_batch_request(test_http_client: Client):
"""Test get vote accounts."""
reqs = (GetBlockHeight(), GetFirstAvailableBlock())
parsers = (GetBlockHeightResp, GetFirstAvailableBlockResp)
resp: Tuple[Resp[GetBlockHeightResp], Resp[GetFirstAvailableBlockResp]] = (
test_http_client._provider.make_batch_request( # pylint: disable=protected-access
reqs, parsers
)
)
assert_valid_response(resp[0])
assert_valid_response(resp[1])