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

Commit bcaeb32

Browse files
committed
Add vote init instruction for cleaner tests
1 parent 72aeb30 commit bcaeb32

16 files changed

+240
-84
lines changed
File renamed without changes.
File renamed without changes.

stake-pool/py/actions/stake_pool.py renamed to stake-pool/py/stake_pool/actions.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from stake_pool.state import STAKE_POOL_LAYOUT, ValidatorList, Fee, StakePool
1616
import stake_pool.instructions as sp
1717

18-
import actions.stake
19-
import actions.token
18+
from stake.actions import create_stake
19+
from spl_token.actions import create_mint, create_associated_token_account
2020

2121

2222
async def create(client: AsyncClient, manager: Keypair,
@@ -87,12 +87,12 @@ async def create_all(client: AsyncClient, manager: Keypair, fee: Fee, referral_f
8787
STAKE_POOL_PROGRAM_ID, stake_pool.public_key)
8888

8989
reserve_stake = Keypair()
90-
await actions.stake.create_stake(client, manager, reserve_stake, pool_withdraw_authority)
90+
await create_stake(client, manager, reserve_stake, pool_withdraw_authority)
9191

9292
pool_mint = Keypair()
93-
await actions.token.create_mint(client, manager, pool_mint, pool_withdraw_authority)
93+
await create_mint(client, manager, pool_mint, pool_withdraw_authority)
9494

95-
manager_fee_account = await actions.token.create_associated_token_account(
95+
manager_fee_account = await create_associated_token_account(
9696
client,
9797
manager,
9898
manager.public_key,

stake-pool/py/system/__init__.py

Whitespace-only changes.
File renamed without changes.

stake-pool/py/tests/conftest.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import shutil
55
import tempfile
66
import time
7-
from typing import Iterator
8-
from subprocess import run, Popen
7+
from typing import Iterator, List
8+
from subprocess import Popen
99

10+
from solana.keypair import Keypair
1011
from solana.publickey import PublicKey
1112
from solana.rpc.async_api import AsyncClient
1213
from solana.rpc.commitment import Confirmed
1314

15+
from vote.actions import create_vote
16+
from system.actions import airdrop
17+
1418

1519
@pytest.fixture(scope="session")
1620
def solana_test_validator():
@@ -27,24 +31,16 @@ def solana_test_validator():
2731

2832

2933
@pytest.fixture
30-
def validators(async_client):
34+
def validators(event_loop, async_client, payer) -> List[PublicKey]:
3135
num_validators = 3
3236
validators = []
37+
futures = []
3338
for i in range(num_validators):
34-
tf = tempfile.NamedTemporaryFile()
35-
identity = f"{tf.name}-identity-{i}.json"
36-
run(["solana-keygen", "new", "-s", "-o", identity])
37-
vote = f"{tf.name}-vote-{i}.json"
38-
run(["solana-keygen", "new", "-s", "-o", vote])
39-
withdrawer = f"{tf.name}-withdrawer-{i}.json"
40-
run(["solana-keygen", "new", "-s", "-o", withdrawer])
41-
run(["solana", "create-vote-account",
42-
vote, identity, withdrawer,
43-
"--commission", "1",
44-
"--commitment", "confirmed",
45-
"-ul"])
46-
output = run(["solana-keygen", "pubkey", vote], capture_output=True)
47-
validators.append(PublicKey(output.stdout.decode('utf-8').strip()))
39+
vote = Keypair()
40+
node = Keypair()
41+
futures.append(create_vote(async_client, payer, vote, node, payer.public_key, payer.public_key, 10))
42+
validators.append(vote.public_key)
43+
event_loop.run_until_complete(asyncio.gather(*futures))
4844
return validators
4945

5046

@@ -68,3 +64,11 @@ def async_client(event_loop, solana_test_validator) -> Iterator[AsyncClient]:
6864
time.sleep(1)
6965
yield async_client
7066
event_loop.run_until_complete(async_client.close())
67+
68+
69+
@pytest.fixture
70+
def payer(event_loop, async_client) -> Keypair:
71+
payer = Keypair()
72+
airdrop_lamports = 10_000_000_000
73+
event_loop.run_until_complete(airdrop(async_client, payer.public_key, airdrop_lamports))
74+
return payer

stake-pool/py/tests/test_add_remove.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import pytest
2-
from solana.keypair import Keypair
32
from solana.publickey import PublicKey
43
from solana.rpc.commitment import Confirmed
54

6-
import actions.system
7-
import actions.stake_pool
85
from vote.constants import VOTE_PROGRAM_ID
96
from stake_pool.state import Fee, ValidatorList, StakeStatus
7+
from stake_pool.actions import create_all, add_validator_to_pool, remove_validator_from_pool
108

119

1210
@pytest.mark.asyncio
13-
async def test_add_validator(async_client, validators):
14-
manager = Keypair()
15-
airdrop_lamports = 1_000_000_000_000
16-
await actions.system.airdrop(async_client, manager.public_key, airdrop_lamports)
17-
11+
async def test_add_validator(async_client, validators, payer):
1812
fee = Fee(numerator=1, denominator=1000)
1913
referral_fee = 20
20-
(stake_pool, validator_list_address) = await actions.stake_pool.create_all(async_client, manager, fee, referral_fee)
14+
(stake_pool, validator_list_address) = await create_all(async_client, payer, fee, referral_fee)
2115
for validator in validators:
2216
resp = await async_client.get_account_info(validator, commitment=Confirmed)
2317
assert PublicKey(resp['result']['value']['owner']) == VOTE_PROGRAM_ID
24-
await actions.stake_pool.add_validator_to_pool(async_client, manager, stake_pool, validator)
18+
await add_validator_to_pool(async_client, payer, stake_pool, validator)
2519

2620
resp = await async_client.get_account_info(validator_list_address, commitment=Confirmed)
2721
data = resp['result']['value']['data']
@@ -33,7 +27,7 @@ async def test_add_validator(async_client, validators):
3327
assert validator_info.transient_stake_lamports == 0
3428
assert validator_info.last_update_epoch == 0
3529
assert validator_info.status == StakeStatus.ACTIVE
36-
await actions.stake_pool.remove_validator_from_pool(async_client, manager, stake_pool, validator)
30+
await remove_validator_from_pool(async_client, payer, stake_pool, validator)
3731

3832
resp = await async_client.get_account_info(validator_list_address, commitment=Confirmed)
3933
data = resp['result']['value']['data']

stake-pool/py/tests/test_create.py

+13-51
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,42 @@
66
from stake_pool.constants import find_withdraw_authority_program_address, STAKE_POOL_PROGRAM_ID
77
from stake_pool.state import StakePool, Fee
88

9-
import actions.system
10-
import actions.stake
11-
import actions.stake_pool
12-
import actions.token
9+
from stake.actions import create_stake
10+
from stake_pool.actions import create
11+
from spl_token.actions import create_mint, create_associated_token_account
1312

1413

1514
@pytest.mark.asyncio
16-
async def test_airdrop(async_client):
17-
manager = Keypair()
18-
airdrop_lamports = 1_000_000
19-
await actions.system.airdrop(async_client, manager.public_key, airdrop_lamports)
20-
resp = await async_client.get_balance(manager.public_key, commitment=Confirmed)
21-
assert resp['result']['value'] == airdrop_lamports
22-
23-
24-
@pytest.mark.asyncio
25-
async def test_create_stake(async_client):
26-
owner = Keypair()
27-
reserve_stake = Keypair()
28-
airdrop_lamports = 1_000_000_000
29-
await actions.system.airdrop(async_client, owner.public_key, airdrop_lamports)
30-
await actions.stake.create_stake(async_client, owner, reserve_stake, owner.public_key)
31-
32-
33-
@pytest.mark.asyncio
34-
async def test_create_mint(async_client):
35-
owner = Keypair()
36-
airdrop_lamports = 1_000_000_000
37-
await actions.system.airdrop(async_client, owner.public_key, airdrop_lamports)
38-
pool_mint = Keypair()
39-
await actions.token.create_mint(async_client, owner, pool_mint, owner.public_key)
40-
await actions.token.create_associated_token_account(
41-
async_client,
42-
owner,
43-
owner.public_key,
44-
pool_mint.public_key,
45-
)
46-
47-
48-
@pytest.mark.asyncio
49-
async def test_create_stake_pool(async_client):
50-
manager = Keypair()
51-
airdrop_lamports = 1_000_000_000_000
52-
await actions.system.airdrop(async_client, manager.public_key, airdrop_lamports)
53-
15+
async def test_create_stake_pool(async_client, payer):
5416
stake_pool = Keypair()
5517
validator_list = Keypair()
5618
(pool_withdraw_authority, seed) = find_withdraw_authority_program_address(
5719
STAKE_POOL_PROGRAM_ID, stake_pool.public_key)
5820

5921
reserve_stake = Keypair()
60-
await actions.stake.create_stake(async_client, manager, reserve_stake, pool_withdraw_authority)
22+
await create_stake(async_client, payer, reserve_stake, pool_withdraw_authority)
6123

6224
pool_mint = Keypair()
63-
await actions.token.create_mint(async_client, manager, pool_mint, pool_withdraw_authority)
25+
await create_mint(async_client, payer, pool_mint, pool_withdraw_authority)
6426

65-
manager_fee_account = await actions.token.create_associated_token_account(
27+
manager_fee_account = await create_associated_token_account(
6628
async_client,
67-
manager,
68-
manager.public_key,
29+
payer,
30+
payer.public_key,
6931
pool_mint.public_key,
7032
)
7133

7234
fee = Fee(numerator=1, denominator=1000)
7335
referral_fee = 20
74-
await actions.stake_pool.create(
75-
async_client, manager, stake_pool, validator_list, pool_mint.public_key,
36+
await create(
37+
async_client, payer, stake_pool, validator_list, pool_mint.public_key,
7638
reserve_stake.public_key, manager_fee_account, fee, referral_fee)
7739
resp = await async_client.get_account_info(stake_pool.public_key, commitment=Confirmed)
7840
assert resp['result']['value']['owner'] == str(STAKE_POOL_PROGRAM_ID)
7941
data = resp['result']['value']['data']
8042
pool_data = StakePool.decode(data[0], data[1])
81-
assert pool_data.manager == manager.public_key
82-
assert pool_data.staker == manager.public_key
43+
assert pool_data.manager == payer.public_key
44+
assert pool_data.staker == payer.public_key
8345
assert pool_data.stake_withdraw_bump_seed == seed
8446
assert pool_data.validator_list == validator_list.public_key
8547
assert pool_data.reserve_stake == reserve_stake.public_key

stake-pool/py/tests/test_stake.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
from solana.keypair import Keypair
3+
4+
from stake.actions import create_stake
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_create_stake(async_client, payer):
9+
reserve_stake = Keypair()
10+
await create_stake(async_client, payer, reserve_stake, payer.public_key)

stake-pool/py/tests/test_system.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
from solana.keypair import Keypair
3+
from solana.rpc.commitment import Confirmed
4+
5+
import system.actions
6+
7+
8+
@pytest.mark.asyncio
9+
async def test_airdrop(async_client):
10+
manager = Keypair()
11+
airdrop_lamports = 1_000_000
12+
await system.actions.airdrop(async_client, manager.public_key, airdrop_lamports)
13+
resp = await async_client.get_balance(manager.public_key, commitment=Confirmed)
14+
assert resp['result']['value'] == airdrop_lamports

stake-pool/py/tests/test_token.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
from solana.keypair import Keypair
3+
4+
from spl_token.actions import create_mint, create_associated_token_account
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_create_mint(async_client, payer):
9+
pool_mint = Keypair()
10+
await create_mint(async_client, payer, pool_mint, payer.public_key)
11+
await create_associated_token_account(
12+
async_client,
13+
payer,
14+
payer.public_key,
15+
pool_mint.public_key,
16+
)

stake-pool/py/tests/test_vote.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
from solana.keypair import Keypair
3+
4+
from vote.actions import create_vote
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_create_vote(async_client, payer):
9+
vote = Keypair()
10+
node = Keypair()
11+
await create_vote(async_client, payer, vote, node, payer.public_key, payer.public_key, 10)

stake-pool/py/vote/actions.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from solana.publickey import PublicKey
2+
from solana.keypair import Keypair
3+
from solana.rpc.async_api import AsyncClient
4+
from solana.rpc.commitment import Confirmed
5+
from solana.rpc.types import TxOpts
6+
from solana.sysvar import SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY
7+
from solana.transaction import Transaction
8+
import solana.system_program as sys
9+
10+
from vote.constants import VOTE_PROGRAM_ID, VOTE_STATE_LEN
11+
from vote.instructions import initialize, InitializeParams
12+
13+
14+
async def create_vote(
15+
client: AsyncClient, payer: Keypair, vote: Keypair, node: Keypair,
16+
voter: PublicKey, withdrawer: PublicKey, commission: int):
17+
print(f"Creating vote account {vote.public_key}")
18+
resp = await client.get_minimum_balance_for_rent_exemption(VOTE_STATE_LEN)
19+
txn = Transaction()
20+
txn.add(
21+
sys.create_account(
22+
sys.CreateAccountParams(
23+
from_pubkey=payer.public_key,
24+
new_account_pubkey=vote.public_key,
25+
lamports=resp['result'],
26+
space=VOTE_STATE_LEN,
27+
program_id=VOTE_PROGRAM_ID,
28+
)
29+
)
30+
)
31+
txn.add(
32+
initialize(
33+
InitializeParams(
34+
vote=vote.public_key,
35+
rent_sysvar=SYSVAR_RENT_PUBKEY,
36+
clock_sysvar=SYSVAR_CLOCK_PUBKEY,
37+
node=node.public_key,
38+
authorized_voter=voter,
39+
authorized_withdrawer=withdrawer,
40+
commission=commission,
41+
)
42+
)
43+
)
44+
await client.send_transaction(
45+
txn, payer, vote, node, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed))

stake-pool/py/vote/constants.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
VOTE_PROGRAM_ID = PublicKey("Vote111111111111111111111111111111111111111")
55
"""Program id for the native vote program."""
6+
7+
VOTE_STATE_LEN: int = 3731
8+
"""Size of vote account."""

0 commit comments

Comments
 (0)