Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
afalaleev committed Jun 23, 2024
1 parent 4779528 commit 565577a
Show file tree
Hide file tree
Showing 52 changed files with 920 additions and 858 deletions.
49 changes: 0 additions & 49 deletions _old/_proxy/neon_rpc_api_model/neon_rpc_api_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,52 +81,3 @@ def eth_sendTransaction(self, tx: Dict[str, Any]) -> str:
tx = self.eth_signTransaction(tx)
return self.eth_sendRawTransaction(tx['raw'])


@staticmethod
def _mp_pool_tx(neon_tx_info: NeonTxInfo) -> Dict[str, Any]:
to_addr = NeonAddress.from_raw(neon_tx_info.to_addr)
if to_addr:
to_addr = to_addr.checksum_address

return {
'blockHash': '0x' + '0' * 64,
'blockNumber': None,
'transactionIndex': None,
'from': NeonAddress.from_raw(neon_tx_info.addr).checksum_address,
'gas': hex(neon_tx_info.gas_limit),
'gasPrice': hex(neon_tx_info.gas_price),
'hash': neon_tx_info.sig,
'input': neon_tx_info.calldata,
'nonce': hex(neon_tx_info.nonce),
'to': to_addr,
'value': hex(neon_tx_info.value),
'chainId': hex(neon_tx_info.chain_id) if neon_tx_info.has_chain_id else None
}

def _mp_pool_queue(self, tx_list: List[NeonTxInfo]) -> Dict[str, Any]:
sender_addr = ''
sender_pool: Dict[int, Any] = dict()
sender_pool_dict: Dict[str, Any] = dict()
for tx in tx_list:
if sender_addr != tx.addr and len(sender_addr):
sender_pool_dict[sender_addr] = sender_pool
sender_pool = dict()

sender_addr = tx.addr
sender_pool[tx.nonce] = self._mp_pool_tx(tx)

if sender_addr:
sender_pool_dict[sender_addr] = sender_pool

return sender_pool_dict

def txpool_content(self) -> Dict[str, Any]:
result_dict: Dict[str, Any] = dict()

req_id = get_req_id_from_log()
content = self._mempool_client.get_content(req_id)

result_dict['pending'] = self._mp_pool_queue(content.pending_list)
result_dict['queued'] = self._mp_pool_queue(content.queued_list)
return result_dict

6 changes: 5 additions & 1 deletion common/ethereum/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import eth_utils
from typing_extensions import Self

from ..utils.cached import cached_method
from ..utils.cached import cached_method, cached_property
from ..utils.format import hex_to_bytes, bytes_to_hex
from ..utils.pydantic import PlainValidator, PlainSerializer

Expand Down Expand Up @@ -127,6 +127,10 @@ class EthHash32(_BaseHash):
HashSize: ClassVar[int] = 32
ZeroHash: ClassVar[str] = "0x" + "00" * HashSize

@cached_property
def ident(self) -> str:
return self.to_bytes()[:4].hex()

def to_string(self, default: str | None = None) -> str | None:
return self._to_string() if self._data else default

Expand Down
46 changes: 3 additions & 43 deletions common/ethereum/transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Annotated, Final
from typing import Final

import eth_keys
import rlp
Expand All @@ -9,8 +9,7 @@

from .errors import EthError
from ..utils.cached import cached_property, cached_method
from ..utils.format import bytes_to_hex, hex_to_bytes
from ..utils.pydantic import PlainValidator, PlainSerializer
from ..utils.format import hex_to_bytes


class EthNoChainTx(rlp.Serializable):
Expand Down Expand Up @@ -66,14 +65,10 @@ def __init__(self, *args, **kwargs):

@classmethod
def from_raw(cls, s: bytes | bytearray | str) -> Self:
if isinstance(s, cls):
return s
elif isinstance(s, str):
if isinstance(s, str):
s = hex_to_bytes(s)
elif isinstance(s, bytearray):
s = bytes(s)
elif isinstance(s, dict):
s = cls.from_dict(s)

try:
return rlp.decode(s, cls)
Expand All @@ -92,38 +87,10 @@ def _copy_from_nochain_tx(cls, nochain_tx: EthNoChainTx) -> Self:
value_list += [0, 0, 0]
return cls(*value_list)

@classmethod
def from_dict(cls, d: dict) -> Self:
return cls(
nonce=int(d.get("nonce", 0)),
gas_price=int(d.get("gasPrice", 0)),
gas_limit=int(d.get("gas", 0)),
to_address=bytes.fromhex(d.get("to", "")),
value=int(d.get("value", 0)),
call_data=bytes.fromhex(d.get("data", "")),
v=int(d.get("v", 0)),
r=int(d.get("r", 0)),
s=int(d.get("s", 0)),
)

def to_dict(self) -> dict:
return {
"nonce": int(self.nonce),
"gasPrice": int(self.gas_price),
"gas": int(self.gas_limit),
"to": self.to_address,
"value": int(self.value),
"data": bytes_to_hex(self.call_data),
}

@cached_method
def to_bytes(self) -> bytes:
return rlp.encode(self)

@cached_method
def to_string(self) -> str:
return bytes_to_hex(self.to_bytes(), prefix="0x")

@property
def has_chain_id(self) -> bool:
return self.chain_id is not None
Expand Down Expand Up @@ -216,10 +183,3 @@ def contract(self) -> bytes | None:

contract_addr = rlp.encode((self.from_address, self.nonce))
return keccak(contract_addr)[-20:]


EthTxField = Annotated[
EthTx,
PlainValidator(EthTx.from_raw),
PlainSerializer(lambda v: v.to_string(), return_type=str),
]
21 changes: 3 additions & 18 deletions common/http/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import hashlib
import inspect
import re
import time
Expand Down Expand Up @@ -67,23 +66,6 @@ def from_raw(

return self

@cached_property
def ctx_id(self) -> str:
if ctx_id := getattr(self, "_ctx_id", None):
return ctx_id

size = len(self.request.body)
raw_value = f"{self.ip_addr}:{size}:{self.start_time_nsec}"
ctx_id = hashlib.md5(bytes(raw_value, "utf-8")).hexdigest()[:8]
self.set_property_value("_ctx_id", ctx_id)
return ctx_id

@cached_property
def chain_id(self) -> int:
chain_id = getattr(self, "_chain_id", None)
assert chain_id is not None
return chain_id

@cached_property
def body(self) -> str:
value = self.request.body
Expand Down Expand Up @@ -121,6 +103,9 @@ def set_property_value(self, name: str, value) -> Self:
self._prop_name_set.add(name)
return self

def get_property_value(self, name: str, default):
return getattr(self, name, default)


@dataclass(frozen=True)
class HttpMethod:
Expand Down
12 changes: 6 additions & 6 deletions common/neon/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from pydantic.functional_validators import PlainValidator
from typing_extensions import Self

from ..ethereum.transaction import EthTx
from ..ethereum.hash import EthAddress
from .transaction_model import NeonTxModel
from ..utils.cached import cached_method, cached_property
from ..utils.format import bytes_to_hex, hex_to_bytes, hex_to_int

Expand Down Expand Up @@ -150,11 +150,11 @@ def private_key(self) -> eth_keys.keys.PrivateKey:
def sign_msg(self, data: bytes) -> eth_keys.keys.Signature:
return self.private_key.sign_msg(data)

def sign_transaction(self, tx: EthTx, chain_id: int) -> str:
tx = tx.to_dict()
tx["chainId"] = chain_id
signed_tx = eth_account.Account.sign_transaction(tx, self.private_key)
return signed_tx.raw_transaction.to_0x_hex()
def sign_tx(self, tx: NeonTxModel) -> bytes:
tx_dict = tx.to_eth_dict()
tx_dict["chainId"] = self._chain_id
signed_tx = eth_account.Account.sign_transaction(tx_dict, self.private_key)
return bytes(signed_tx.raw_transaction)

def __str__(self) -> str:
return self.to_string()
Expand Down
10 changes: 10 additions & 0 deletions common/neon/transaction_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def to_rlp_tx(self) -> bytes:

return tx.to_bytes()

def to_eth_dict(self) -> dict:
return dict(
nonce=self.nonce,
gasPrice=self.gas_price,
gas=self.gas_limit,
to=self.to_address.to_string(),
value=self.value,
data=self.call_data.to_string(),
)

@property
def has_chain_id(self) -> bool:
return self.chain_id is not None
Expand Down
2 changes: 1 addition & 1 deletion common/neon_rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async def get_neon_account(self, account: NeonAccount, block: NeonBlockHdrModel
acct_list = await self.get_neon_account_list([account], block)
return acct_list[0]

async def get_state_tx_cnt(self, account: NeonAccount, block: NeonBlockHdrModel | None) -> int:
async def get_state_tx_cnt(self, account: NeonAccount, block: NeonBlockHdrModel | None = None) -> int:
acct = await self.get_neon_account(account, block)
return acct.state_tx_cnt

Expand Down
16 changes: 8 additions & 8 deletions proxy/base/server.py → proxy/base/intl_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from common.utils.process_pool import ProcessPool


class BaseProxyComponent:
def __init__(self, server: BaseProxyServer):
class BaseIntlProxyComponent:
def __init__(self, server: BaseIntlProxyServer):
self._server = server

@cached_property
Expand All @@ -32,17 +32,17 @@ def _msg_filter(self) -> LogMsgFilter:
return self._server._msg_filter # noqa


class BaseProxyApi(BaseProxyComponent, AppDataApi):
def __init__(self, server: BaseProxyServer) -> None:
class BaseProxyApi(BaseIntlProxyComponent, AppDataApi):
def __init__(self, server: BaseIntlProxyServer) -> None:
AppDataApi.__init__(self)
BaseProxyComponent.__init__(self, server)
BaseIntlProxyComponent.__init__(self, server)


class BaseProxyServer(AppDataServer):
class BaseIntlProxyServer(AppDataServer):
class _ProcessPool(ProcessPool):
def __init__(self, server: BaseProxyServer) -> None:
def __init__(self, server: BaseIntlProxyServer) -> None:
super().__init__()
self._server: BaseProxyServer | None = server
self._server: BaseIntlProxyServer | None = server

def _on_process_start(self, idx: int) -> None:
self._server._on_process_start(idx)
Expand Down
5 changes: 3 additions & 2 deletions proxy/base/mp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def neon_tx_hash(self) -> EthTxHash:

@cached_property
def tx_id(self) -> str:
return self.neon_tx_hash.to_bytes()[:4].hex()
return self.neon_tx_hash.ident

@property
def sender(self) -> EthAddress:
Expand Down Expand Up @@ -113,7 +113,7 @@ def to_string(self) -> str:

@cached_property
def tx_id(self) -> str:
return self.neon_tx_hash.to_bytes()[:4].hex()
return self.neon_tx_hash.ident

@property
def process_time_nsec(self) -> int:
Expand Down Expand Up @@ -166,6 +166,7 @@ def is_empty(self) -> bool:

class MpRequest(BaseModel):
ctx_id: str
chain_id: int


class MpTxCntRequest(BaseModel):
Expand Down
4 changes: 2 additions & 2 deletions proxy/base/mp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ async def get_tx_by_sender_nonce(self, ctx_id: str, sender: NeonAccount, tx_nonc
resp = await self._get_tx_by_sender_nonce(req)
return resp.tx

async def get_content(self, ctx_id: str) -> MpTxPoolContentResp:
return await self._get_content(MpRequest(ctx_id=ctx_id))
async def get_content(self, ctx_id: str, chain_id: int) -> MpTxPoolContentResp:
return await self._get_content(MpRequest(ctx_id=ctx_id, chain_id=chain_id))

@AppDataClient.method(name="getGasPrice")
async def _get_gas_price(self) -> MpGasPriceModel: ...
Expand Down
18 changes: 9 additions & 9 deletions proxy/base/op_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from common.ethereum.bin_str import EthBinStrField
from common.ethereum.hash import EthAddressField, EthAddress
from common.ethereum.transaction import EthTxField
from common.neon.transaction_model import NeonTxModel
from common.solana.pubkey import SolPubKey, SolPubKeyField
from common.solana.transaction_model import SolTxModel
from common.utils.cached import cached_method
Expand Down Expand Up @@ -81,26 +81,26 @@ class OpTokenSolAddressModel(BaseModel):
token_sol_address: SolPubKeyField


class OpSignEthMessageRequest(BaseModel):
ctx_id: str
eth_address: EthAddressField
class OpSignEthMsgRequest(BaseModel):
req_id: dict
sender: EthAddressField
data: EthBinStrField


class OpSignEthMessageResp(BaseModel):
signed_message: str | None = None
class OpSignEthMsgResp(BaseModel):
signed_msg: EthBinStrField
error: str | None = None


class OpSignEthTxRequest(BaseModel):
ctx_id: str
tx: EthTxField
req_id: dict
neon_tx: NeonTxModel
eth_address: EthAddressField
chain_id: int


class OpSignEthTxResp(BaseModel):
signed_tx: str | None = None
signed_tx: EthBinStrField
error: str | None = None


Expand Down
Loading

0 comments on commit 565577a

Please sign in to comment.