Skip to content

Commit

Permalink
Drop python-native serialization in favour of bindings to antelope-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
guilledk committed Feb 26, 2025
1 parent 20f2e1f commit 1f32c35
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 1,595 deletions.
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ authors = [{ name = "Guillermo Rodriguez", email = "[email protected]" }]
requires-python = ">=3.9"
readme = "README.md"
dependencies = [
"base58>=2.1.1,<3",
"cryptos>=2.0.9,<3",
"requests<2.32.0",
"ripemd-hash>=1.0.1,<2",
"httpx>=0.28.1,<0.29",
"msgspec>=0.19.0",
"pyo3-antelope-rs",
]

[dependency-groups]
Expand All @@ -38,6 +36,12 @@ default-groups = [
"snaps",
]

[tool.uv.sources.pyo3-antelope-rs]
url = "https://github.com/openrepublic/pyo3-antelope-rs/releases/download/v0.2.0/pyo3_antelope_rs-0.2a1-cp39-abi3-manylinux_2_34_x86_64.whl"
# git = "https://github.com/openrepublic/pyo3-antelope-rs.git"
# rev = "8a0724b0504acdaf2af67e5e97e90a9411da26fa"
# path = "../pyo3-antelope-rs"

[tool.hatch.build.targets.sdist]
include = ["src/leap"]

Expand Down
20 changes: 9 additions & 11 deletions src/leap/cleos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import json
import time
import base64
import logging
Expand All @@ -13,6 +14,7 @@

import httpx
import msgspec
import antelope_rs

from msgspec import Struct
from requests.adapters import HTTPAdapter
Expand All @@ -23,11 +25,8 @@
from leap.protocol import (
Asset,
GetTableRowsResponse,
ChainErrorResponse,
get_tapos_info,
create_and_sign_tx,
gen_key_pair,
get_pub_key
)


Expand Down Expand Up @@ -87,6 +86,7 @@ def __init__(
def load_abi(self, account: str, abi: dict):
'''Load abi dict into internal store
'''
antelope_rs.load_abi(account, json.dumps(abi))
self._loaded_abis[account] = abi

def load_abi_file(self, account: str, abi_path: str | Path):
Expand Down Expand Up @@ -290,9 +290,8 @@ def _create_signed_tx(
chain_info['last_irreversible_block_id'])

chain_id: str = chain_info['chain_id']
abis: dict[str, dict] = self._get_abis_for_actions(actions)

return create_and_sign_tx(chain_id, abis, actions, key, ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, **kwargs)
return create_and_sign_tx(chain_id, actions, key, ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, **kwargs)

async def _a_create_signed_tx(
self,
Expand All @@ -305,9 +304,8 @@ async def _a_create_signed_tx(
chain_info['last_irreversible_block_id'])

chain_id: str = chain_info['chain_id']
abis: dict[str, dict] = self._get_abis_for_actions(actions)

return create_and_sign_tx(chain_id, abis, actions, key, ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, **kwargs)
return create_and_sign_tx(chain_id, actions, key, ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, **kwargs)

def push_actions(
self,
Expand Down Expand Up @@ -533,7 +531,7 @@ def deploy_contract(
'name': 'setabi',
'data': [
account_name,
abi
json.dumps(abi).encode('utf-8')
],
'authorization': [{
'actor': account_name,
Expand Down Expand Up @@ -944,13 +942,13 @@ def disconnect_node(self, endpoint: str):
'/v1/net/disconnect',
json=endpoint)

def create_key_pair(self) -> tuple[str, str]:
def create_key_pair(self, key_type: int = 0) -> tuple[str, str]:
'''Generates a key pair.
:return: Private and public keys.
:rtype: tuple[str, str]
'''
priv, pub = gen_key_pair()
priv, pub = antelope_rs.gen_key_pair(key_type)
return priv, pub

def create_key_pairs(self, n: int) -> list[tuple[str, str]]:
Expand All @@ -976,7 +974,7 @@ def import_key(self, account: str, private_key: str) -> str:
:param private_key: Private key to import.
:type private_key: str
'''
public_key = get_pub_key(private_key)
public_key = antelope_rs.get_pub_key(private_key)
self.keys[account] = public_key
self.private_keys[account] = private_key
if public_key not in self._key_to_acc:
Expand Down
11 changes: 4 additions & 7 deletions src/leap/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

import json
import logging
import subprocess
Expand All @@ -9,13 +7,12 @@

import docker
import pytest
import antelope_rs

from docker.types import Mount

from leap.protocol import gen_key_pair

from .cleos import CLEOS
from .sugar import (
from leap.cleos import CLEOS
from leap.sugar import (
get_container,
get_free_port,
)
Expand Down Expand Up @@ -162,7 +159,7 @@ def bootstrap_test_nodeos(request, tmp_path_factory):
cmd += ['--http-validate-host', '0']

if randomize:
priv, pub = gen_key_pair()
priv, pub = antelope_rs.gen_key_pair(0)
else:
priv, pub = ('5Jr65kdYmn33C3UabzhmWDm2PuqbRfPuDStts3ZFNSBLM7TqaiL', 'EOS5GnobZ231eekYUJHGTcmy2qve1K23r5jSFQbMfwWTtPB7mFZ1L')

Expand Down
62 changes: 62 additions & 0 deletions src/leap/protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import time
import struct
from typing import TypeVar, Generic

import antelope_rs
from msgspec import Struct

Name = antelope_rs.Name
SymbolCode = antelope_rs.SymbolCode
Symbol = antelope_rs.Symbol
Asset = antelope_rs.Asset


def endian_reverse_u32(x: int) -> int:
# Ensure x fits into 32 bits, then convert to little-endian bytes
# and re-interpret in big-endian to reverse endianness.
return int.from_bytes((x & 0xFFFFFFFF).to_bytes(4, byteorder='little'), byteorder='big')

def get_tapos_info(block_id: str) -> tuple[int, int]:
block_id_bin = bytes.fromhex(block_id)

# Unpack the first 16 bytes as two 64-bit little-endian integers
hash0, hash1 = struct.unpack("<QQ", block_id_bin[:16])

ref_block_num = endian_reverse_u32(hash0) & 0xFFFF
ref_block_prefix = hash1 & 0xFFFFFFFF

return ref_block_num, ref_block_prefix


def create_and_sign_tx(
chain_id: str,
actions: list[dict],
key: str,
max_cpu_usage_ms=255,
max_net_usage_words=0,
ref_block_num: int = 0,
ref_block_prefix: int = 0
) -> dict:
return antelope_rs.create_and_sign_tx(
bytes.fromhex(chain_id),
actions,
key,
int(time.time() + 900),
max_cpu_usage_ms,
max_net_usage_words,
ref_block_num,
ref_block_prefix
)


T = TypeVar('T')
class GetTableRowsResponse(Struct, Generic[T]):
rows: list[T]
more: bool
ram_payer: list[str] | None = None
next_key: str | None = None

class ChainErrorResponse(Struct):
code: int
message: str
error: dict
2 changes: 0 additions & 2 deletions src/leap/protocol/__init__.py

This file was deleted.

Loading

0 comments on commit 1f32c35

Please sign in to comment.