Skip to content

Commit

Permalink
Change data bus parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hweawer committed Sep 16, 2024
1 parent 2d4a2f4 commit ef2d312
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
51 changes: 20 additions & 31 deletions src/transport/msg_providers/onchain_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
'type': 'event',
}

UNVET_V1_DATA_SCHEMA = '(uint256,uint256,bytes32,uint256,bytes,bytes32,bytes32,(bytes32))'
UNVET_V1_DATA_SCHEMA = '(uint256,bytes32,uint256,uint256,bytes,bytes,(bytes32,bytes32),(bytes32))'
PING_V1_DATA_SCHEMA = '(uint256,(bytes32))'
DEPOSIT_V1_DATA_SCHEMA = '(bytes32,uint256,uint256,bytes32,bytes,uint256,(bytes32))'
PAUSE_V2_DATA_SCHEMA = '(bytes32,uint256,uint256,bytes32,bytes,uint256,(bytes32))'
PAUSE_V3_DATA_SCHEMA = '(uint256,bytes,(bytes32))'
DEPOSIT_V1_DATA_SCHEMA = '(uint256,bytes32,bytes32,uint256,uint256,(bytes32,bytes32),(bytes32))'
PAUSE_V2_DATA_SCHEMA = '(uint256,bytes32,(bytes32,bytes32),uint256,(bytes32))'
PAUSE_V3_DATA_SCHEMA = '(uint256,bytes32,(bytes32,bytes32),(bytes32))'


def signature_to_r_vs(signature: bytes) -> tuple[VRS, VRS]:
Expand Down Expand Up @@ -71,15 +71,14 @@ def parse(self, log: LogReceipt) -> Optional[dict]:
return self._create_message(decoded_data, guardian)


# event MessageDepositV1(address indexed guardianAddress, (bytes32 depositRoot, uint256 nonce, uint256 blockNumber, bytes32 blockHash,
# bytes signature, uint256 stakingModuleId, (bytes32 version) app) data)",
# event MessageDepositV1(address indexed guardianAddress, (uint256 blockNumber, bytes32 blockHash, bytes32 depositRoot,
# uint256 stakingModuleId, uint256 nonce, (bytes32 r, bytes32 vs) signature, (bytes32 version) app) data),
class DepositParser(EventParser):
def __init__(self, w3: Web3):
super().__init__(w3, DEPOSIT_V1_DATA_SCHEMA)

def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
deposit_root, nonce, block_number, block_hash, signature, staking_module_id, app = parsed_data
r, _vs = signature_to_r_vs(signature)
block_number, block_hash, deposit_root, staking_module_id, nonce, (r, vs), app = parsed_data
return DepositMessage(
type=MessageType.DEPOSIT,
depositRoot=bytes_to_hex_string(deposit_root),
Expand All @@ -90,22 +89,19 @@ def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
stakingModuleId=staking_module_id,
signature={
'r': r,
'_vs': _vs,
'_vs': vs,
},
)


# event MessageUnvetV1(address indexed guardianAddress, (uint256 nonce, uint256 blockNumber, bytes32 blockHash, uint256 stakingModuleId,
# bytes signature, bytes32 operatorIds, bytes32 vettedKeysByOperator, (bytes32 version) app) data)",


# event MessageUnvetV1(address indexed guardianAddress, (uint256 blockNumber, bytes32 blockHash, uint256 stakingModuleId, uint256 nonce,
# bytes operatorIds, bytes vettedKeysByOperator, (bytes32 r, bytes32 vs) signature, (bytes32 version) app) data)
class UnvetParser(EventParser):
def __init__(self, w3: Web3):
super().__init__(w3, UNVET_V1_DATA_SCHEMA)

def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
nonce, block_number, block_hash, staking_module_id, signature, operator_ids, vetted_keys_by_operator, app = parsed_data
r, _vs = signature_to_r_vs(signature)
block_number, block_hash, staking_module_id, nonce, operator_ids, vetted_keys_by_operator, (r, vs), app = parsed_data
return UnvetMessage(
type=MessageType.UNVET,
nonce=nonce,
Expand All @@ -114,16 +110,14 @@ def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
stakingModuleId=staking_module_id,
signature={
'r': r,
'_vs': _vs,
'_vs': vs,
},
operatorIds=operator_ids,
vettedKeysByOperator=vetted_keys_by_operator,
)


# event MessagePingV1(address indexed guardianAddress, (uint256 blockNumber, (bytes32 version) app) data)",


class PingParser(EventParser):
def __init__(self, w3: Web3):
super().__init__(w3, PING_V1_DATA_SCHEMA)
Expand All @@ -137,46 +131,41 @@ def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
)


# event MessagePauseV2(address indexed guardianAddress, (bytes32 depositRoot, uint256 nonce, uint256 blockNumber, bytes32 blockHash,
# bytes signature, uint256 stakingModuleId, (bytes32 version) app) data)",


# event MessagePauseV2(address indexed guardianAddress, (uint256 blockNumber, bytes32 blockHash, (bytes32 r, bytes32 vs) signature,
# uint256 stakingModuleId, (bytes32 version) app) data)
class PauseV2Parser(EventParser):
def __init__(self, w3: Web3):
super().__init__(w3, PAUSE_V2_DATA_SCHEMA)

def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
deposit_root, nonce, block_number, block_hash, signature, staking_module_id, app = parsed_data
r, _vs = signature_to_r_vs(signature)
block_number, block_hash, (r, vs), staking_module_id, app = parsed_data
return PauseMessage(
type=MessageType.PAUSE,
blockNumber=block_number,
guardianAddress=guardian,
stakingModuleId=staking_module_id,
signature={
'r': r,
'_vs': _vs,
'_vs': vs,
},
)


# event MessagePauseV3(address indexed guardianAddress, (uint256 blockNumber, bytes signature, (bytes32 version) app) data)",


# event MessagePauseV3(address indexed guardianAddress, (uint256 blockNumber, bytes32 blockHash, (bytes32 r, bytes32 vs) signature,
# (bytes32 version) app) data)
class PauseV3Parser(EventParser):
def __init__(self, w3: Web3):
super().__init__(w3, PAUSE_V3_DATA_SCHEMA)

def _create_message(self, parsed_data: tuple, guardian: str) -> dict:
block_number, signature, app = parsed_data
r, _vs = signature_to_r_vs(signature)
block_number, block_hash, (r, vs), app = parsed_data
return PauseMessage(
type=MessageType.PAUSE,
blockNumber=block_number,
guardianAddress=guardian,
signature={
'r': r,
'_vs': _vs,
'_vs': vs,
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/transport/msg_types/pause.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import TypedDict

from eth_typing import Hash32
from schema import And, Schema
from transport.msg_types.base import ADDRESS_REGREX, Signature, SignatureSchema

Expand Down Expand Up @@ -37,9 +38,8 @@

class PauseMessage(TypedDict):
type: str
depositRoot: str
nonce: int
blockNumber: int
blockHash: Hash32
guardianAddress: str
signature: Signature
stakingModuleId: int
19 changes: 7 additions & 12 deletions tests/transport/onchain_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from transport.msg_types.pause import PauseMessage
from transport.msg_types.ping import PingMessage
from transport.msg_types.unvet import UnvetMessage
from utils.bytes import from_hex_string_to_bytes
from web3 import Web3


Expand All @@ -20,9 +19,7 @@ class OnchainTransportSender:
Is used in tests to create sequence of the events emitted from the DataBus contract
"""

_DEFAULT_SIGNATURE = from_hex_string_to_bytes(
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
)
_DEFAULT_SIGNATURE = ((0).to_bytes(32), (0).to_bytes(32))

def __init__(self, w3: Web3, data_bus_contract: DataBusContract):
self._w3 = w3
Expand All @@ -40,32 +37,30 @@ def send_deposit(self, deposit_mes: DepositMessage):
)
mes = self._w3.codec.encode(
types=[DEPOSIT_V1_DATA_SCHEMA],
args=[(deposit_root, nonce, block_number, block_hash, self._DEFAULT_SIGNATURE, staking_module_id, app)],
args=[(block_number, block_hash, deposit_root, staking_module_id, nonce, self._DEFAULT_SIGNATURE, app)],
)
tx = self._data_bus.functions.sendMessage(deposit_topic, mes)
return tx.transact()

def send_pause_v2(self, pause_mes: PauseMessage):
pause_topic = self._w3.keccak(text=OnchainTransportSinks.PAUSE_V2)
deposit_root, nonce, block_number, block_hash, staking_module_id, app = (
pause_mes['depositRoot'],
pause_mes['nonce'],
block_number, block_hash, staking_module_id, app = (
pause_mes['blockNumber'],
pause_mes['blockHash'],
pause_mes['stakingModuleId'],
((1).to_bytes(32),),
)
mes = self._w3.codec.encode(
types=[PAUSE_V2_DATA_SCHEMA],
args=[(deposit_root, nonce, block_number, block_hash, self._DEFAULT_SIGNATURE, staking_module_id, app)],
args=[(block_number, block_hash, self._DEFAULT_SIGNATURE, staking_module_id, app)],
)
tx = self._data_bus.functions.sendMessage(pause_topic, mes)
return tx.transact()

def send_pause_v3(self, pause_mes: PauseMessage):
pause_topic = self._w3.keccak(text=OnchainTransportSinks.PAUSE_V3)
block_number, version = pause_mes['blockNumber'], (1).to_bytes(32)
mes = self._w3.codec.encode(types=[PAUSE_V3_DATA_SCHEMA], args=[(block_number, self._DEFAULT_SIGNATURE, (version,))])
block_number, block_hash, version = pause_mes['blockNumber'], pause_mes['blockHash'], (1).to_bytes(32)
mes = self._w3.codec.encode(types=[PAUSE_V3_DATA_SCHEMA], args=[(block_number, block_hash, self._DEFAULT_SIGNATURE, (version,))])
tx = self._data_bus.functions.sendMessage(pause_topic, mes)
return tx.transact()

Expand All @@ -82,7 +77,7 @@ def send_unvet(self, unvet_mes: UnvetMessage):
)
mes = self._w3.codec.encode(
types=[UNVET_V1_DATA_SCHEMA],
args=[(nonce, block_number, block_hash, staking_module_id, self._DEFAULT_SIGNATURE, operator_ids, vetted_keys, (version,))],
args=[(block_number, block_hash, staking_module_id, nonce, operator_ids, vetted_keys, self._DEFAULT_SIGNATURE, (version,))],
)
tx = self._data_bus.functions.sendMessage(unvet_topic, mes)
return tx.transact()
Expand Down
13 changes: 10 additions & 3 deletions tests/transport/test_data_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def test_data_bus_mock_responses(web3_lido_unit):
assert len(messages) == len(receipts)


# event MessageDepositV1(address indexed guardianAddress, (uint256 blockNumber, bytes32 blockHash, bytes32 depositRoot,
# uint256 stakingModuleId, uint256 nonce, (bytes32 r, bytes32 vs) signature, (bytes32 version) app) data),

# event MessageDepositV1(address indexed guardianAddress, (bytes32 depositRoot, uint256 nonce, uint256 blockNumber, bytes32 blockHash,
# bytes signature, uint256 stakingModuleId, (bytes32 version) app) data)",


def mock_receipts(w3: Web3) -> list[EventData]:
return [
EventData(
Expand All @@ -127,12 +134,12 @@ def mock_receipts(w3: Web3) -> list[EventData]:
types=[DEPOSIT_V1_DATA_SCHEMA],
args=[
(
'0x0000000000000000000000000000000000000000000000000000000000000000',
40,
2,
'0x42eef33d13c4440627c3fab6e3abee85af796ae6f77dcade628b183640b519d0',
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
3,
40,
((0).to_bytes(32), (0).to_bytes(32)),
('0x0000000000000000000000000000000000000000000000000000000000000000',),
)
],
Expand Down

0 comments on commit ef2d312

Please sign in to comment.