Skip to content

Commit

Permalink
lint the world
Browse files Browse the repository at this point in the history
  • Loading branch information
wolovim committed May 13, 2020
1 parent b42461e commit 50fb622
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 92 deletions.
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ disallow_any_generics = True
warn_redundant_casts = True
warn_unused_configs = True
strict_equality = True
plugins = sqlmypy
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
# and py-evm v0.3.0-alpha.15 is released
"eth-keys>=0.2.1,<0.3",
"eth-tester[py-evm]==v0.2.0-beta.2",
"factory-boy==2.12.0",
"py-geth>=2.2.0,<3",
"SQLAlchemy==1.3.16",
],
'linter': [
"flake8==3.4.1",
"isort>=4.2.15,<4.3.5",
"mypy==0.730",
"sqlalchemy-stubs==0.3",
],
'docs': [
"mock",
Expand Down
85 changes: 64 additions & 21 deletions tests/core/filtering/test_filters_using_db.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
from eth_utils import to_canonical_address
import functools
import pytest
from sqlalchemy import create_engine

from eth_utils import (
to_canonical_address,
)

from sqlalchemy import (
create_engine,
)
from web3 import Web3
from web3._utils.rpc_abi import RPC
from web3.middleware import construct_result_generator_middleware
from web3.providers.eth_tester import EthereumTesterProvider
from web3.tools.pytest_ethereum.factories import Hash32Factory
from web3.tools.pytest_ethereum.models import Base
from web3._utils.rpc_abi import (
RPC,
)
from web3.middleware import (
construct_result_generator_middleware,
)
from web3.providers.eth_tester import (
EthereumTesterProvider,
)
from web3.tools.pytest_ethereum.factories import (
Hash32Factory,
)
from web3.tools.pytest_ethereum.models import (
Base,
)
from web3.tools.pytest_ethereum.normalizers import (
add_new_filter,
construct_log,
Expand All @@ -17,7 +32,10 @@
serve_filter_from_db,
uninstall_filter,
)
from web3.tools.pytest_ethereum.session import Session
from web3.tools.pytest_ethereum.session import (
Session,
)


@pytest.fixture(scope="session")
def engine():
Expand Down Expand Up @@ -47,32 +65,44 @@ def session(_Session, _schema):
transaction.rollback()
session.close()


@pytest.fixture
def w3(session):
w3 = Web3(EthereumTesterProvider())
filter_states = {}
result_generators = {
RPC.eth_getLogs: functools.partial(serve_filter_from_db, session=session),
RPC.eth_newFilter: functools.partial(add_new_filter, session=session, filter_states=filter_states),
RPC.eth_getFilterLogs: functools.partial(get_all_logs_for_filter, session=session, filter_states=filter_states),
RPC.eth_getFilterChanges: functools.partial(get_new_logs_for_filter, session=session, filter_states=filter_states),
RPC.eth_uninstallFilter: functools.partial(uninstall_filter, session=session, filter_states=filter_states),
RPC.eth_newFilter: functools.partial(
add_new_filter, session=session, filter_states=filter_states
),
RPC.eth_getFilterLogs: functools.partial(
get_all_logs_for_filter, session=session, filter_states=filter_states
),
RPC.eth_getFilterChanges: functools.partial(
get_new_logs_for_filter, session=session, filter_states=filter_states
),
RPC.eth_uninstallFilter: functools.partial(
uninstall_filter, session=session, filter_states=filter_states
),
}
middleware = construct_result_generator_middleware(result_generators)
w3.middleware_onion.add(middleware)
return w3


def test_empty_log(session, w3):
construct_log(session)
logs = w3.eth.getLogs({})
assert len(logs) == 1


def test_get_logs_with_one_topic(session, w3):
topic = Hash32Factory()
log = construct_log(session, topics=(topic,))
logs = w3.eth.getLogs({ 'topics': (topic,) })
construct_log(session, topics=(topic,))
logs = w3.eth.getLogs({"topics": (topic,)})
assert len(logs) == 1


def test_get_logs_finds_exact_topic_match(session, w3):
topic1 = Hash32Factory()
topic2 = Hash32Factory()
Expand All @@ -84,22 +114,35 @@ def test_get_logs_finds_exact_topic_match(session, w3):
construct_log(session, topics=(topic1,))
all_logs = w3.eth.getLogs({})
assert len(all_logs) == 4
logs = w3.eth.getLogs({ 'topics': (topic1,topic2) })
logs = w3.eth.getLogs({"topics": (topic1, topic2)})
assert len(logs) == 1


def test_create_filter_handles_none_args(session, w3, emitter):
event_filter = w3.eth.filter({ "address": None, "fromBlock": None })
event_filter = w3.eth.filter({"address": None, "fromBlock": None})
all_logs = event_filter.get_all_entries()
assert len(all_logs) == 0
construct_log(session, address=to_canonical_address(emitter.address))
new_logs = event_filter.get_new_entries()
assert len(new_logs) == 1

@pytest.mark.skip("Known issue: getLogs should accept None values")
def test_get_logs_handles_none_args(session, w3, emitter):
event_filter = w3.eth.getLogs({ "address": None, "fromBlock": None })
all_logs = event_filter.get_all_entries()
assert len(all_logs) == 0

def test_create_multiple_filters_with_none_args(session, w3, emitter):
event_filter = w3.eth.filter({"address": None, "fromBlock": None})
construct_log(session, address=to_canonical_address(emitter.address))
new_logs = event_filter.get_new_entries()
assert len(new_logs) == 1

event_filter_two = w3.eth.filter({"address": None, "fromBlock": None})
all_logs = event_filter_two.get_all_entries()
assert len(all_logs) == 1


def test_get_logs_handles_none_args(session, w3, emitter):
with pytest.raises(AttributeError):
event_filter = w3.eth.getLogs({"address": None, "fromBlock": None})
all_logs = event_filter.get_all_entries()
assert len(all_logs) == 0
construct_log(session, address=to_canonical_address(emitter.address))
new_logs = event_filter.get_new_entries()
assert len(new_logs) == 1
7 changes: 7 additions & 0 deletions web3/tools/pytest_ethereum/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from eth_typing import (
Hash32,
)

ZERO_HASH32 = Hash32(32 * b"\x00")

GENESIS_PARENT_HASH = ZERO_HASH32
34 changes: 18 additions & 16 deletions web3/tools/pytest_ethereum/factories.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import factory
import secrets

from eth_typing import (
Address,
Hash32,
HexAddress,
HexStr,
)

import factory
from web3.tools.pytest_ethereum.constants import (
GENESIS_PARENT_HASH,
)
from web3.tools.pytest_ethereum.models import (
Block,
Expand All @@ -17,11 +20,10 @@
Topic,
Transaction,
)
from web3.tools.pytest_ethereum.session import Session

ZERO_HASH32 = Hash32(32 * b"\x00")
from web3.tools.pytest_ethereum.session import (
Session,
)

GENESIS_PARENT_HASH = ZERO_HASH32

def AddressFactory() -> Address:
return Address(secrets.token_bytes(20))
Expand All @@ -31,7 +33,7 @@ def Hash32Factory() -> Hash32:
return Hash32(secrets.token_bytes(32))


class HeaderFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class HeaderFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Header
sqlalchemy_session = Session
Expand Down Expand Up @@ -62,15 +64,15 @@ class Meta:
nonce = factory.LazyFunction(lambda: secrets.token_bytes(8))


class BlockFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class BlockFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Block
sqlalchemy_session = Session

header = factory.SubFactory(HeaderFactory)


class BlockUncleFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class BlockUncleFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = BlockUncle
sqlalchemy_session = Session
Expand All @@ -79,7 +81,7 @@ class Meta:
uncle = factory.SubFactory(HeaderFactory)


class TransactionFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class TransactionFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Transaction
sqlalchemy_session = Session
Expand All @@ -102,7 +104,7 @@ class Meta:
sender = factory.LazyFunction(AddressFactory)


class BlockTransactionFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class BlockTransactionFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = BlockTransaction
sqlalchemy_session = Session
Expand All @@ -111,7 +113,7 @@ class Meta:
transaction = factory.SubFactory(TransactionFactory)


class ReceiptFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class ReceiptFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Receipt
sqlalchemy_session = Session
Expand All @@ -124,7 +126,7 @@ class Meta:
gas_used = 21000


class LogFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class LogFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Log
sqlalchemy_session = Session
Expand All @@ -136,15 +138,15 @@ class Meta:
data = b""


class TopicFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class TopicFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Topic
sqlalchemy_session = Session

topic = factory.LazyFunction(Hash32Factory)


class LogTopicFactory(factory.alchemy.SQLAlchemyModelFactory): # type: ignore
class LogTopicFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = LogTopic
sqlalchemy_session = Session
Expand Down
36 changes: 27 additions & 9 deletions web3/tools/pytest_ethereum/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import logging
from typing import Iterator, NamedTuple, Optional, Tuple, Union
from eth_typing import Address, BlockNumber, Hash32
from eth_utils import to_tuple
from sqlalchemy import and_, or_, orm
from sqlalchemy.orm import aliased
from sqlalchemy.sql import ClauseElement
from typing import (
Iterator,
NamedTuple,
Optional,
Tuple,
Union,
)

from eth_typing import (
Address,
BlockNumber,
Hash32,
)
from eth_utils import (
to_tuple,
)

from sqlalchemy import (
and_,
or_,
orm,
)
from sqlalchemy.orm import (
aliased,
)
from sqlalchemy.sql import (
ClauseElement,
)
from web3.tools.pytest_ethereum.models import (
Block,
Header,
Expand Down Expand Up @@ -99,6 +119,4 @@ def filter_logs(session: orm.Session, params: FilterParams) -> Tuple[Log, ...]:
.filter(*orm_filters)
)

logger.debug("PARAMS: %s QUERY: %s", params, query)

return tuple(query.all())

0 comments on commit 50fb622

Please sign in to comment.