Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for chain ID #1034

Merged
merged 7 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion aquarius/events/http_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def make_request(self, method, params):


def get_web3_connection_provider(network_url):

if network_url.startswith("http"):
provider = CustomHTTPProvider(network_url)
elif network_url.startswith("ws"):
Expand Down
15 changes: 15 additions & 0 deletions aquarius/events/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def get_address_file():
)


def get_config_chain_id():
config_rpc = os.getenv("NETWORK_URL")
provider = get_web3_connection_provider(config_rpc)
web3 = Web3(provider)

return web3.eth.chain_id


def get_metadata_start_block():
"""Returns the block number to use as start"""
block_number = int(os.getenv("METADATA_CONTRACT_BLOCK", 0))
Expand Down Expand Up @@ -273,6 +281,13 @@ def setup_web3(_logger=None):

web3.middleware_onion.inject(geth_poa_middleware, layer=0)

config_chain_id = get_config_chain_id()

if config_chain_id != web3.eth.chain_id:
raise Exception(
f"Mismatch of chain IDs between configuration and events RPC! Config chain ID: {config_chain_id} and events chain ID: {web3.eth.chain_id}"
)

return web3


Expand Down
11 changes: 9 additions & 2 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ def test_get_web3_connection_provider(monkeypatch):
with pytest.raises(AssertionError):
get_web3_connection_provider("not_a_network")
assert get_web3_connection_provider("kovan").endpoint_uri == "http://127.0.0.1:8545"
monkeypatch.setenv("NETWORK_URL", "wss://kovan")
assert get_web3_connection_provider("kovan").endpoint_uri == "wss://kovan"


def test_get_network_name(monkeypatch):
Expand All @@ -220,6 +218,15 @@ def test_setup_web3(monkeypatch):
assert setup_web3(logger)


def test_config_rpc(monkeypatch):
monkeypatch.setenv("NETWORK_URL", "https://rpc-mumbai.maticvigil.com/")

with pytest.raises(
Exception, match="Mismatch of chain IDs between configuration and events RPC!"
):
setup_web3(logger)


def test_setup_logging(monkeypatch):
with patch("logging.config.dictConfig") as mock:
mock.side_effect = Exception("Boom!")
Expand Down