Example #1
0
    def __init__(self):
        #Web3 instance connecting to node
        self.w3 = Web3(Web3.HTTPProvider(NODE_URL))
        
	#Cache with 3 categories fast, medium ,slow
        self.priceCache = priceCache = Cache(maxsize=3)
        
	#Caching Parameters
        self.cacheInterval = CACHE_INTERVAL
        self.blocksToCache = 150
        
	# Construct Cache with  with LRU Cache and 150 items
        self.block_hash_cache_middleware = construct_simple_cache_middleware(
            cache_class=partial(LRUCache, self.blocksToCache),
            rpc_whitelist='eth_getBlockByHash'
        )
    	#Adding caching to middle ware
        self.w3.middleware_stack.add(self.block_hash_cache_middleware)

        #Faucet Account initalization with priavte Key
        self.faucetAccount = Account.privateKeyToAccount(ETH_PRIVATE_KEY)
	
	#Transaction parameters of faucet
        self.txGas = 314150
        self.txGasPrice = 20000000000
        self.chainId = 3
        self.txData = '53656e742066726f6d20676173466175636574202a2e2a'
def test_simple_cache_middleware_pulls_from_cache():
    def cache_class():
        return {
            generate_cache_key(('fake_endpoint', [1])): 'value-a',
        }

    middleware = construct_simple_cache_middleware(
        cache_class=cache_class,
        rpc_whitelist={'fake_endpoint'},
    )(None, None)

    assert middleware('fake_endpoint', [1]) == 'value-a'
def test_simple_cache_middleware_does_not_endpoints_not_in_whitelist():
    def make_request(method, params):
        return {'result': str(uuid.uuid4())}

    middleware = construct_simple_cache_middleware(
        cache_class=dict,
        rpc_whitelist={'fake_endpoint'},
    )(make_request, None)

    response_a = middleware('not_whitelisted', [])
    assert 'result' in response_a

    response_b = middleware('not_whitelisted', [])
    assert 'result' in response_b

    assert not response_a['result'] == response_b['result']
def test_simple_cache_middleware_does_not_cache_bad_responses(response):
    def make_request(method, params):
        return assoc(response, 'id', str(uuid.uuid4()))

    middleware = construct_simple_cache_middleware(
        cache_class=dict,
        rpc_whitelist={'fake_endpoint'},
    )(make_request, None)

    response_a = middleware('fake_endpoint', [])
    assert 'id' in response_a

    response_b = middleware('fake_endpoint', [])
    assert 'id' in response_b

    assert not response_a['id'] == response_b['id']
def test_simple_cache_middleware_populates_cache():
    def make_request(method, params):
        return {
            'result': str(uuid.uuid4()),
        }

    middleware = construct_simple_cache_middleware(
        cache_class=dict,
        rpc_whitelist={'fake_endpoint'},
    )(make_request, None)

    response = middleware('fake_endpoint', [])
    assert 'result' in response

    assert middleware('fake_endpoint', []) == response
    assert not middleware('fake_endpoint', [1]) == response
Example #6
0
import functools

from cachetools import LRUCache
from web3.middleware.cache import construct_simple_cache_middleware

BLOCK_HASH_CACHE_RPC_WHITELIST = {"eth_getBlockByHash"}


block_hash_cache_middleware = construct_simple_cache_middleware(
    # default sample size of gas price strategies is 120
    cache_class=functools.partial(LRUCache, 150),
    rpc_whitelist=BLOCK_HASH_CACHE_RPC_WHITELIST,
)
Example #7
0
        def middleware(method, params):
            try:
                if web3.isConnected():
                    return make_request(method, params)
                else:
                    raise EthNodeCommunicationError('Web3 provider not connected')

            # the isConnected check doesn't currently catch JSON errors
            # see https://github.com/ethereum/web3.py/issues/866
            except JSONDecodeError:
                raise EthNodeCommunicationError('Web3 provider not connected')

        return middleware
    return connection_test_middleware


connection_test_middleware = make_connection_test_middleware()


BLOCK_HASH_CACHE_RPC_WHITELIST = {
    'eth_getBlockByHash',
}


block_hash_cache_middleware = construct_simple_cache_middleware(
    # default sample size of gas price strategies is 120
    cache_class=functools.partial(LRUCache, 150),
    rpc_whitelist=BLOCK_HASH_CACHE_RPC_WHITELIST,
)