-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WithdrawalRequests web3py extension
- Loading branch information
Showing
6 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import logging | ||
|
||
from web3 import Web3 | ||
from web3.module import Module | ||
|
||
from src.providers.execution.exceptions import InconsistentData | ||
from src.types import BlockStamp | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class WithdrawalRequests(Module): | ||
""" | ||
Web3py extension to work with EIP-7002 withdrawal requests. | ||
See https://eips.ethereum.org/EIPS/eip-7002 for details. | ||
""" | ||
|
||
w3: Web3 | ||
|
||
ADDRESS = Web3.to_checksum_address("0x0c15F14308530b7CDB8460094BbB9cC28b9AaaAA") | ||
|
||
QUEUE_HEAD_SLOT = 2 | ||
QUEUE_TAIL_SLOT = 3 | ||
|
||
def get_queue_len(self, blockstamp: BlockStamp): | ||
head = self.w3.eth.get_storage_at(self.ADDRESS, self.QUEUE_HEAD_SLOT, block_identifier=blockstamp.block_hash) | ||
tail = self.w3.eth.get_storage_at(self.ADDRESS, self.QUEUE_TAIL_SLOT, block_identifier=blockstamp.block_hash) | ||
if head > tail: | ||
raise InconsistentData("EIP-7002 queue's head is over the tail") | ||
return int.from_bytes(tail) - int.from_bytes(head) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from hexbytes import HexBytes | ||
|
||
from src.providers.execution.exceptions import InconsistentData | ||
from src.web3py.types import Web3 | ||
from tests.factory.blockstamp import ReferenceBlockStampFactory | ||
|
||
blockstamp = ReferenceBlockStampFactory.build() | ||
|
||
|
||
@pytest.mark.unit | ||
@pytest.mark.usefixtures("withdrawal_requests") | ||
def test_queue_len(web3: Web3): | ||
web3.eth.get_storage_at = Mock( | ||
side_effect=[ | ||
HexBytes(bytes.fromhex("")), | ||
HexBytes(bytes.fromhex("")), | ||
] | ||
) | ||
assert web3.withdrawal_requests.get_queue_len(blockstamp) == 0 | ||
|
||
web3.eth.get_storage_at = Mock( | ||
side_effect=[ | ||
HexBytes(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000")), | ||
HexBytes(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000")), | ||
] | ||
) | ||
assert web3.withdrawal_requests.get_queue_len(blockstamp) == 0 | ||
|
||
web3.eth.get_storage_at = Mock( | ||
side_effect=[ | ||
HexBytes(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000")), | ||
HexBytes(bytes.fromhex("00000000000000000000000000000000000000000000000000000000000001c7")), | ||
] | ||
) | ||
assert web3.withdrawal_requests.get_queue_len(blockstamp) == 455 | ||
|
||
web3.eth.get_storage_at = Mock( | ||
side_effect=[ | ||
HexBytes(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000020")), | ||
HexBytes(bytes.fromhex("00000000000000000000000000000000000000000000000000000000000001c7")), | ||
] | ||
) | ||
assert web3.withdrawal_requests.get_queue_len(blockstamp) == 423 | ||
|
||
web3.eth.get_storage_at = Mock( | ||
side_effect=[ | ||
HexBytes(bytes.fromhex("00000000000000000000000000000000000000000000000000000000000001c7")), | ||
HexBytes(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000020")), | ||
] | ||
) | ||
with pytest.raises(InconsistentData): | ||
web3.withdrawal_requests.get_queue_len(blockstamp) |