Skip to content

Commit 6270c82

Browse files
authored
Merge pull request #338 from michaelhly/remove-blockhash
Remove Blockhash, TransactionSignature, SIG_LENGTH and SigPubkeyPair
2 parents a1c37c4 + eb54304 commit 6270c82

File tree

13 files changed

+49
-72
lines changed

13 files changed

+49
-72
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
## Added
66

77
- Add VersionedTransaction support to `send_transaction` and `simulate_transaction` methods [(#334)](https://github.com/michaelhly/solana-py/pull/334)
8-
- Support VersionedMessage in `get_fee_for_message` methods [(#336)](https://github.com/michaelhly/solana-py/pull/336)
8+
- Support VersionedMessage in `get_fee_for_message` methods [(#337)](https://github.com/michaelhly/solana-py/pull/337)
99

1010
## Changed
1111

12-
- Remove redundant classes and modules ([#329](https://github.com/michaelhly/solana-py/pull/329) and [#335](https://github.com/michaelhly/solana-py/pull/335)):
12+
- Remove redundant classes and modules ([#329](https://github.com/michaelhly/solana-py/pull/329), [#335](https://github.com/michaelhly/solana-py/pull/335) and [#338](https://github.com/michaelhly/solana-py/pull/338)):
1313
- Remove `PublicKey`, in favour of `solders.pubkey.Pubkey`.
1414
- Remove `AccountMeta` in favour of `solders.instruction.AccountMeta`.
1515
- Remove `TransactionInstruction` in favour of `solders.instruction.Instruction`.
@@ -22,6 +22,10 @@
2222
- Remove `Message` in favour of `solders.message.Message`.
2323
- Remove `system_program` in favour of `solders.system_program`. Note: where previously a params object like `AssignParams` called a field `program_id`, it now calls it `owner`.
2424
- Remove `sysvar` in favour of `solders.sysvar`. The constants in `solders.sysvar` have short names, so instead of `solana.sysvar.SYSVAR_RENT_PUBKEY` you'll use `solders.sysvar.RENT`.
25+
- Remove `solana.blockhash.Blockhash` in favour of `solders.hash.Hash`. Note: `Blockhash(my_str)` -> `Hash.from_str(my_str)`.
26+
- Remove `solana.transaction.TransactionSignature` newtype. This was unused - solana-py is already using `solders.signature.Signature`.
27+
- Remove constant `solana.transaction.SIG_LENGTH` in favour of `solders.signature.Signature.LENGTH`.
28+
- Remove unused `solana.transaction.SigPubkeyPair`.
2529
- Use latest solders [(#334)](https://github.com/michaelhly/solana-py/pull/334)
2630
- Use new `solders.rpc.requests.SendRawTransasction` in `send_raw_transaction` methods
2731

src/solana/blockhash.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
"""Blockhash.
2-
3-
Example:
4-
>>> # An arbitrary base58 encoded blockhash:
5-
>>> Blockhash("EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k")
6-
'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'
7-
"""
8-
from typing import NewType
1+
"""Blockhash utils."""
92

103
from cachetools import TTLCache
11-
12-
Blockhash = NewType("Blockhash", str)
13-
"""Type for blockhash."""
4+
from solders.hash import Hash
145

156

167
class BlockhashCache:
@@ -26,11 +17,11 @@ def __init__(self, ttl: int = 60) -> None:
2617
self.unused_blockhashes: TTLCache = TTLCache(maxsize=maxsize, ttl=ttl)
2718
self.used_blockhashes: TTLCache = TTLCache(maxsize=maxsize, ttl=ttl)
2819

29-
def set(self, blockhash: Blockhash, slot: int, used_immediately: bool = False) -> None:
20+
def set(self, blockhash: Hash, slot: int, used_immediately: bool = False) -> None:
3021
"""Update the cache.
3122
3223
Args:
33-
blockhash: new Blockhash value.
24+
blockhash: new blockhash value.
3425
slot: the slot which the blockhash came from.
3526
used_immediately: whether the client used the blockhash immediately after fetching it.
3627
@@ -43,11 +34,11 @@ def set(self, blockhash: Blockhash, slot: int, used_immediately: bool = False) -
4334
return
4435
self.unused_blockhashes[slot] = blockhash
4536

46-
def get(self) -> Blockhash:
47-
"""Get the cached Blockhash. Raises KeyError if cache has expired.
37+
def get(self) -> Hash:
38+
"""Get the cached blockhash. Raises KeyError if cache has expired.
4839
4940
Returns:
50-
cached Blockhash.
41+
cached blockhash.
5142
5243
"""
5344
try:

src/solana/rpc/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import sleep, time
55
from typing import Dict, List, Optional, Sequence, Union
66

7+
from solders.hash import Hash as Blockhash
78
from solders.keypair import Keypair
89
from solders.message import VersionedMessage
910
from solders.pubkey import Pubkey
@@ -61,7 +62,7 @@
6162
from solders.signature import Signature
6263
from solders.transaction import VersionedTransaction
6364

64-
from solana.blockhash import Blockhash, BlockhashCache
65+
from solana.blockhash import BlockhashCache
6566
from solana.rpc import types
6667
from solana.transaction import Transaction
6768

src/solana/rpc/async_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from time import time
44
from typing import Dict, List, Optional, Sequence, Union
55

6+
from solders.hash import Hash as Blockhash
67
from solders.keypair import Keypair
78
from solders.message import VersionedMessage
89
from solders.pubkey import Pubkey
@@ -59,7 +60,7 @@
5960
from solders.signature import Signature
6061
from solders.transaction import VersionedTransaction
6162

62-
from solana.blockhash import Blockhash, BlockhashCache
63+
from solana.blockhash import BlockhashCache
6364
from solana.rpc import types
6465
from solana.transaction import Transaction
6566

src/solana/rpc/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from solders.account_decoder import UiAccountEncoding, UiDataSliceConfig
1111
from solders.commitment_config import CommitmentLevel
12+
from solders.hash import Hash as Blockhash
1213
from solders.message import MessageV0, VersionedMessage
1314
from solders.pubkey import Pubkey
1415
from solders.rpc.config import (
@@ -83,7 +84,7 @@
8384
from solders.transaction import VersionedTransaction
8485
from solders.transaction_status import UiTransactionEncoding
8586

86-
from solana.blockhash import Blockhash, BlockhashCache
87+
from solana.blockhash import BlockhashCache
8788
from solana.rpc import types
8889
from solana.transaction import Transaction
8990

@@ -518,7 +519,7 @@ def _post_send(resp: SendTransactionResp) -> SendTransactionResp:
518519
@staticmethod
519520
def parse_recent_blockhash(blockhash_resp: GetLatestBlockhashResp) -> Blockhash:
520521
"""Extract blockhash from JSON RPC result."""
521-
return Blockhash(str(blockhash_resp.value.blockhash))
522+
return blockhash_resp.value.blockhash
522523

523524
def _process_blockhash_resp(self, blockhash_resp: GetLatestBlockhashResp, used_immediately: bool) -> Blockhash:
524525
recent_blockhash = self.parse_recent_blockhash(blockhash_resp)

src/solana/transaction.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Library to package an atomic sequence of instructions to a transaction."""
22
from __future__ import annotations
33

4-
from dataclasses import dataclass
5-
from typing import Any, List, NamedTuple, NewType, Optional, Sequence, Tuple, Union
4+
from typing import Any, List, NamedTuple, Optional, Sequence, Tuple, Union
65

7-
from solders.hash import Hash
6+
from solders.hash import Hash as Blockhash
87
from solders.instruction import AccountMeta, Instruction
98
from solders.keypair import Keypair
109
from solders.message import Message
@@ -15,14 +14,8 @@
1514
from solders.transaction import Transaction as SoldersTx
1615
from solders.transaction import TransactionError
1716

18-
from solana.blockhash import Blockhash
19-
20-
TransactionSignature = NewType("TransactionSignature", str)
21-
"""Type for TransactionSignature."""
2217
PACKET_DATA_SIZE = 1280 - 40 - 8
2318
"""Constant for maximum over-the-wire size of a Transaction."""
24-
SIG_LENGTH = 64
25-
"""Constant for standard length of a signature."""
2619

2720

2821
class NonceInformation(NamedTuple):
@@ -34,14 +27,6 @@ class NonceInformation(NamedTuple):
3427
"""AdvanceNonceAccount Instruction."""
3528

3629

37-
@dataclass
38-
class SigPubkeyPair:
39-
"""Pair of signature and corresponding public key."""
40-
41-
pubkey: Pubkey
42-
signature: Optional[bytes] = None
43-
44-
4530
def _build_solders_tx(
4631
recent_blockhash: Optional[Blockhash] = None,
4732
nonce_info: Optional[NonceInformation] = None,
@@ -52,17 +37,15 @@ def _build_solders_tx(
5237
underlying_instructions = (
5338
core_instructions if nonce_info is None else [nonce_info.nonce_instruction, *core_instructions]
5439
)
55-
underlying_blockhash_str: Optional[str]
40+
underlying_blockhash: Optional[Blockhash]
5641
if nonce_info is not None:
57-
underlying_blockhash_str = nonce_info.nonce
42+
underlying_blockhash = nonce_info.nonce
5843
elif recent_blockhash is not None:
59-
underlying_blockhash_str = recent_blockhash
44+
underlying_blockhash = recent_blockhash
6045
else:
61-
underlying_blockhash_str = None
46+
underlying_blockhash = None
6247
underlying_fee_payer = None if fee_payer is None else fee_payer
63-
underlying_blockhash = (
64-
Hash.default() if underlying_blockhash_str is None else Hash.from_string(underlying_blockhash_str)
65-
)
48+
underlying_blockhash = Blockhash.default() if underlying_blockhash is None else underlying_blockhash
6649
msg = SoldersMessage.new_with_blockhash(underlying_instructions, underlying_fee_payer, underlying_blockhash)
6750
return SoldersTx.new_unsigned(msg)
6851

@@ -144,7 +127,7 @@ def __eq__(self, other: Any) -> bool:
144127
@property
145128
def recent_blockhash(self) -> Optional[Blockhash]:
146129
"""Optional[Blockhash]: The blockhash assigned to this transaction."""
147-
return Blockhash(str(self._solders.message.recent_blockhash))
130+
return self._solders.message.recent_blockhash
148131

149132
@recent_blockhash.setter
150133
def recent_blockhash(self, blockhash: Optional[Blockhash]) -> None: # noqa: D102
@@ -288,15 +271,14 @@ def serialize(self, verify_signatures: bool = True) -> bytes:
288271
289272
Example:
290273
>>> from solders.keypair import Keypair
291-
>>> from solana.blockhash import Blockhash
292274
>>> from solders.pubkey import Pubkey
293275
>>> from solders.hash import Hash
294276
>>> from solders.system_program import transfer, TransferParams
295277
>>> leading_zeros = [0] * 31
296278
>>> seed = bytes(leading_zeros + [1])
297279
>>> sender, receiver = Keypair.from_seed(seed), Pubkey(leading_zeros + [2])
298280
>>> transfer_tx = Transaction().add(transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=receiver, lamports=1000)))
299-
>>> transfer_tx.recent_blockhash = Blockhash(str(Hash(leading_zeros + [3])))
281+
>>> transfer_tx.recent_blockhash = Hash(leading_zeros + [3])
300282
>>> transfer_tx.sign(sender)
301283
>>> transfer_tx.serialize().hex()
302284
'019d53be8af3a7c30f86c1092d2c3ea61d270c0cfa275a23ba504674c8fbbb724827b23b42dc8e08019e23120f1b6f40f9799355ce54185b4415be37ca2cee6e0e010001034cb5abf6ad79fbf5abbccafcc269d85cd2651ed4b885b5869f241aedf0a5ba2900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000301020200010c02000000e803000000000000'
@@ -355,7 +337,7 @@ def populate(cls, message: Message, signatures: List[Signature]) -> Transaction:
355337
>>> from solders.message import Message
356338
>>> from solders.signature import Signature
357339
>>> msg = Message.from_bytes(raw_message)
358-
>>> signatures = [Signature(bytes([1] * SIG_LENGTH)), Signature(bytes([2] * SIG_LENGTH))]
340+
>>> signatures = [Signature(bytes([1] * Signature.LENGTH)), Signature(bytes([2] * Signature.LENGTH))]
359341
>>> type(Transaction.populate(msg, signatures))
360342
<class 'solana.transaction.Transaction'>
361343

src/spl/token/async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from typing import List, Optional, Union, cast
66

7-
from solana.blockhash import Blockhash
87
from solana.rpc.async_api import AsyncClient
98
from solana.rpc.commitment import Commitment
109
from solana.rpc.types import TxOpts
10+
from solders.hash import Hash as Blockhash
1111
from solders.keypair import Keypair
1212
from solders.pubkey import Pubkey
1313
from solders.rpc.responses import (

src/spl/token/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from typing import List, Optional, Union, cast
66

7-
from solana.blockhash import Blockhash
87
from solana.rpc.api import Client
98
from solana.rpc.commitment import Commitment
109
from solana.rpc.types import TxOpts
10+
from solders.hash import Hash as Blockhash
1111
from solders.keypair import Keypair
1212
from solders.pubkey import Pubkey
1313
from solders.rpc.responses import (

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from typing import NamedTuple
55

66
import pytest
7-
from solana.blockhash import Blockhash
87
from solana.rpc.api import Client
98
from solana.rpc.async_api import AsyncClient
109
from solana.rpc.commitment import Processed
10+
from solders.hash import Hash as Blockhash
1111
from solders.keypair import Keypair
1212
from solders.pubkey import Pubkey
1313

@@ -36,7 +36,7 @@ def event_loop():
3636
@pytest.fixture(scope="session")
3737
def stubbed_blockhash() -> Blockhash:
3838
"""Arbitrary block hash."""
39-
return Blockhash("EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k")
39+
return Blockhash.from_string("EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k")
4040

4141

4242
@pytest.fixture(scope="session")

tests/integration/test_async_http_client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pytest
55
import solders.system_program as sp
6-
from solana.blockhash import Blockhash
76
from solana.rpc.async_api import AsyncClient
87
from solana.rpc.commitment import Confirmed, Finalized, Processed
98
from solana.rpc.core import RPCException, TransactionExpiredBlockheightExceededError
@@ -270,7 +269,7 @@ async def test_send_raw_transaction_and_get_balance(
270269
recent_blockhash = resp.value.blockhash
271270
assert recent_blockhash is not None
272271
# Create transfer tx transfer lamports from stubbed sender to async_stubbed_receiver
273-
transfer_tx = Transaction(recent_blockhash=Blockhash(str(recent_blockhash))).add(
272+
transfer_tx = Transaction(recent_blockhash=recent_blockhash).add(
274273
sp.transfer(
275274
sp.TransferParams(
276275
from_pubkey=async_stubbed_sender.pubkey(), to_pubkey=async_stubbed_receiver, lamports=1000
@@ -305,7 +304,7 @@ async def test_send_raw_transaction_and_get_balance_using_latest_blockheight(
305304
assert recent_blockhash is not None
306305
last_valid_block_height = resp.value.last_valid_block_height
307306
# Create transfer tx transfer lamports from stubbed sender to async_stubbed_receiver
308-
transfer_tx = Transaction(recent_blockhash=Blockhash(str(recent_blockhash))).add(
307+
transfer_tx = Transaction(recent_blockhash=recent_blockhash).add(
309308
sp.transfer(
310309
sp.TransferParams(
311310
from_pubkey=async_stubbed_sender.pubkey(), to_pubkey=async_stubbed_receiver, lamports=1000
@@ -340,7 +339,7 @@ async def test_confirm_expired_transaction(stubbed_sender, stubbed_receiver, tes
340339
assert recent_blockhash is not None
341340
last_valid_block_height = resp.value.last_valid_block_height - 330
342341
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
343-
transfer_tx = Transaction(recent_blockhash=Blockhash(str(recent_blockhash))).add(
342+
transfer_tx = Transaction(recent_blockhash=recent_blockhash).add(
344343
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
345344
)
346345
# Sign transaction
@@ -367,7 +366,7 @@ async def test_get_fee_for_transaction_message(stubbed_sender, stubbed_receiver,
367366
recent_blockhash = resp.value.blockhash
368367
assert recent_blockhash is not None
369368
# Create transfer tx transfer lamports from stubbed sender to stubbed_receiver
370-
transfer_tx = Transaction(recent_blockhash=Blockhash(str(recent_blockhash))).add(
369+
transfer_tx = Transaction(recent_blockhash=recent_blockhash).add(
371370
sp.transfer(sp.TransferParams(from_pubkey=stubbed_sender.pubkey(), to_pubkey=stubbed_receiver, lamports=1000))
372371
)
373372
# Get fee for transaction message

0 commit comments

Comments
 (0)