Add support for --no-cache

This commit is contained in:
Luke Van Seters 2021-08-06 16:15:16 -04:00
parent 68f5144959
commit 388f780f68
3 changed files with 41 additions and 10 deletions

View File

@ -12,7 +12,13 @@ cache_directory = "./cache"
## Creates a block object, either from the cache or from the chain itself ## Creates a block object, either from the cache or from the chain itself
## Note that you need to pass in the provider, not the web3 wrapped provider object! ## Note that you need to pass in the provider, not the web3 wrapped provider object!
## This is because only the provider allows you to make json rpc requests ## This is because only the provider allows you to make json rpc requests
def create_from_block_number(block_number: int, base_provider) -> Block: def create_from_block_number(
block_number: int, base_provider, should_cache: bool
) -> Block:
if not should_cache:
w3 = Web3(base_provider)
return fetch_block(w3, base_provider, block_number)
cache_path = _get_cache_path(block_number) cache_path = _get_cache_path(block_number)
if cache_path.is_file(): if cache_path.is_file():

View File

@ -28,29 +28,41 @@ def cli():
@cli.command() @cli.command()
@click.argument("block_number", type=int) @click.argument("block_number", type=int)
@click.argument("rpc") @click.argument("rpc")
def inspect_block(block_number: int, rpc: str): @click.option("--cache/--no-cache", default=True)
def inspect_block(block_number: int, rpc: str, cache: bool):
base_provider = Web3.HTTPProvider(rpc) base_provider = Web3.HTTPProvider(rpc)
_inspect_block(base_provider, block_number)
if not cache:
click.echo("Skipping cache")
_inspect_block(base_provider, block_number, should_cache=cache)
@cli.command() @cli.command()
@click.argument("after_block", type=int) @click.argument("after_block", type=int)
@click.argument("before_block", type=int) @click.argument("before_block", type=int)
@click.argument("rpc") @click.argument("rpc")
def inspect_many_blocks(after_block: int, before_block: int, rpc: str): @click.option("--cache/--no-cache", default=True)
def inspect_many_blocks(after_block: int, before_block: int, rpc: str, cache: bool):
base_provider = Web3.HTTPProvider(rpc) base_provider = Web3.HTTPProvider(rpc)
if not cache:
click.echo("Skipping cache")
for block_number in range(after_block + 1, before_block): for block_number in range(after_block + 1, before_block):
_inspect_block( _inspect_block(
base_provider, base_provider,
block_number, block_number,
should_print_stats=False, should_print_stats=False,
should_write_classified_traces=False, should_write_classified_traces=False,
should_cache=cache,
) )
def _inspect_block( def _inspect_block(
base_provider, base_provider,
block_number: int, block_number: int,
should_cache: bool,
should_print_stats: bool = True, should_print_stats: bool = True,
should_write_classified_traces: bool = True, should_write_classified_traces: bool = True,
should_write_swaps: bool = True, should_write_swaps: bool = True,
@ -63,7 +75,10 @@ def _inspect_block(
click.echo(block_message) click.echo(block_message)
click.echo(dashes) click.echo(dashes)
block_data = block.create_from_block_number(block_number, base_provider) block_data = block.create_from_block_number(
block_number, base_provider, should_cache
)
click.echo(f"Total traces: {len(block_data.traces)}") click.echo(f"Total traces: {len(block_data.traces)}")
total_transactions = len( total_transactions = len(

View File

@ -10,7 +10,12 @@ import click
@click.option( @click.option(
"-r", "--rpc", help="rpc endpoint, this needs to have parity style traces" "-r", "--rpc", help="rpc endpoint, this needs to have parity style traces"
) )
def inspect(block_number: str, rpc: str): @click.option(
"--cache/--no-cache",
help="whether to read / write to the cache",
default=True,
)
def inspect(block_number: str, rpc: str, cache: bool):
check_call( check_call(
[ [
"docker", "docker",
@ -22,17 +27,21 @@ def inspect(block_number: str, rpc: str):
"inspect-block", "inspect-block",
block_number, block_number,
rpc, rpc,
"--cache" if cache else "--no-cache",
] ]
) )
@click.command() @click.command()
@click.option("--after-block", type=str, help="look at blocks after this number") @click.argument("after_block", type=str)
@click.option("--before-block", type=str, help="look at blocks before this number") @click.argument("before_block", type=str)
@click.argument("rpc")
@click.option( @click.option(
"-r", "--rpc", help="rpc endpoint, this needs to have parity style traces" "--cache/--no-cache",
help="whether to read / write to the cache",
default=True,
) )
def inspect_many(after_block: str, before_block: str, rpc: str): def inspect_many(after_block: str, before_block: str, rpc: str, cache: bool):
check_call( check_call(
[ [
"docker", "docker",
@ -45,5 +54,6 @@ def inspect_many(after_block: str, before_block: str, rpc: str):
after_block, after_block,
before_block, before_block,
rpc, rpc,
"--cache" if cache else "--no-cache",
] ]
) )