コード例 #1
0
ファイル: opviewer.py プロジェクト: jwasinger/evmlab-1
def main(args):

    vm = None
    fname = args.file
    api = utils.getApi(args.web3)

    if not fname:
        if not args.hash:
            parser.error('hash is required to reproduce the tx if a trace file is not provided')

        #reproduce the tx
        if args.parity_evm:
            vm = VMUtils.ParityVM(args.parity_evm, not args.no_docker)
        else:
            vm = VMUtils.GethVM(args.geth_evm, not args.no_docker)

        artefacts, vm_args = reproduce.reproduceTx(args.hash, vm, api)
        saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)

        fname = os.path.join(saved_files['json-trace']['path'], saved_files['json-trace']['name'])


    ops = loadJsonDebugStackTrace(fname)
    if ops == None:
        # Usually, each line is a json-object
        try:
            ops = loadJsonObjects(fname)
        except Exception as e:
            print("Error loading json:")
            traceback.print_exc()
            print("Trying alternate loader")
            ops = loadWeirdJson(fname)


    contexts = None
    if args.json:
        if not args.hash:
            parser.error('hash is required if contract json is provided')

        contracts = []
        with open(args.json) as f:
            combined = json.load(f)

            sourceList = [os.path.join(args.source, s) for s in combined['sourceList']]

            for contract in combined['contracts'].keys():
                val = combined['contracts'][contract]
                c = Contract(sourceList, val)
                contracts.append(c)

        contexts = buildContexts(ops, api, contracts, args.hash)

    if vm:
        print("\nTo view the generated trace:\n")
        cmd = "python3 opviewer.py -f %s" % fname
        if args.json:
            cmd += " --web3 %s -s %s -j %s --hash %s" % (args.web3, args.source, args.json, args.hash)
        print(cmd)

    DebugViewer().setTrace(ops, contexts)
コード例 #2
0
    def reproduce_tx(txhash):
        # Verify input
        if not hash_regexp.match(txhash):
            return flask.render_template("index.html",
                                         message="Invalid tx hash")

        try:
            artefacts, vm_args = reproduce.reproduceTx(txhash, app.vm, app.api)
        except Exception as e:
            return flask.render_template("index.html", message=str(e))

        saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)

        #Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = g_gen
        command = app.vm.makeCommand(**vm_args)

        outp = "Transaction tracing seems to have been successfull. Use the following command to execute locally"
        (path, zipfilename) = zipFiles(saved_files, txhash[:8])

        return flask.render_template("index.html",
                                     files=saved_files,
                                     zipfile=zipfilename,
                                     message=outp,
                                     code=" \\\n\t".join(command))
コード例 #3
0
def main(args):

    if args.parity_evm:
        vm = VMUtils.ParityVM(args.parity_evm, not args.no_docker)
    else:
        vm = VMUtils.GethVM(args.geth_evm, not args.no_docker)

    parsed_web3 = urlparse(args.web3)
    if not parsed_web3.hostname:
        #User probably omitted 'http://'
        parsed_web3 = urlparse("http://%s" % args.web3)

    ssl = (parsed_web3.scheme == 'https')
    port = parsed_web3.port

    if not port:
        #Not explicitly defined
        if ssl:
            port = 443
        else:
            port = 80
    web3 = Web3(RPCProvider(host=parsed_web3.hostname, port=port, ssl=ssl))
    api = multiapi.MultiApi(web3=web3, etherchain=etherchain.EtherChainAPI())

    if args.test:
        artefacts = test(vm, api)
        import pprint
        pprint.PrettyPrinter().pprint(artefacts)

        sys.exit(0)

    if app and args.www:
        app.debug = args.debug
        app.api = api
        app.vm = vm
        app.run(host=args.www)
    elif args.hash:
        artefacts, vm_args = reproduce.reproduceTx(args.hash, vm, api)
        saved_files = saveFiles(artefacts)

        #Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = g_gen
        vm_args['dontExecuteButReturnCommand'] = True
        command = vm.execute(**vm_args)
        print("")
        print("Command to execute locally (geth):")
        print("")
        print("\t %s" % " ".join(command))
        print("")
        (zipfilepath, zipfilename) = zipFiles(saved_files, args.hash[:8])
        print("Zipped files into %s%s" % (zipfilepath, zipfilename))

    else:
        parser.print_usage()
コード例 #4
0
def test(vm, api):
    print("Doing tests")
    # Jumpdest-analysis attack
    tx = ""
    # Parity-wallet attack
    tx = "0x9dbf0326a03a2a3719c27be4fa69aacc9857fd231a8d9dcaede4bb083def75ec"
    # tenx token transfer (should include SLOADS)
    tx = "0xd6d519043d40691a36c9e718e47110309590e6f47084ac0ec00b53718e449fd3"

    return reproduce.reproduceTx(tx, vm, api)
コード例 #5
0
ファイル: opviewer.py プロジェクト: TokenAires/evmlab
    def reproduce(self, tx, vm):
        """

        :param tx:  # tx hash
        :param vm:  # see OpViewer.get_evm_handler(...)
        :return: self
        """
        artefacts, vm_args = reproduce.reproduceTx(tx, vm, self.api)
        logger.debug("done reproducing transaction trace...")
        ## TODO: fix artefacts
        ## TODO: get rid of saveFiles
        ## TODO: generally get rid of tempfile logic.
        # saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)
        # fname = os.path.join(saved_files['json-trace']['path'], saved_files['json-trace']['name'])

        return self.load_trace(path=artefacts['json-trace'])
コード例 #6
0
    def reproduce_tx(txhash):
        logger.debug("reproducing transaction %s" % txhash)

        # Verify input
        if not hash_regexp.match(txhash):
            logger.debug("rendering index(invalid tx hash)...")
            return flask.render_template("index.html",
                                         message="Invalid tx hash")

        try:
            artefacts, vm_args = reproduce.reproduceTx(txhash, app.vm, app.api)
            logger.debug("done reproducing transaction trace...")
        except Exception as e:
            logger.exception(
                "exception thrown while reproducing transaction...")
            return flask.render_template("index.html", message=str(e))

        logger.debug("saving artefacts to %s" % OUTPUT_DIR)
        saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)

        # Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = g_gen
        command = app.vm.makeCommand(**vm_args)
        logger.debug("vm command: %s" % command)

        logger.debug("creating zip archive for artefacts")
        prefix = txhash[:8]
        output_archive = os.path.join(OUTPUT_DIR, "%s.zip" % prefix)

        # create a list of files to pack with zipFiles
        input_files = [(os.path.join(v['path'], v['name']), v['name'])
                       for v in saved_files]

        create_zip_archive(input_files=input_files,
                           output_archive=output_archive)

        logger.debug("rendering reproduce_tx...")
        return flask.render_template(
            "index.html",
            files=saved_files,
            zipfile="%s.zip" % prefix,
            message=
            "Transaction tracing seems to have been successfull. Use the following command to execute locally",
            code=" \\\n\t".join(command))
コード例 #7
0
def main(args):

    if args.parity_evm:
        vm = VMUtils.ParityVM(args.parity_evm, not args.no_docker)
    else:
        vm = VMUtils.GethVM(args.geth_evm, not args.no_docker)

    api = utils.getApi(args.web3)

    if args.test:
        artefacts = test(vm, api)
        import pprint
        pprint.PrettyPrinter().pprint(artefacts)

        sys.exit(0)

    if app and args.www:
        if ':' in args.www:
            host, port = args.www.split(':')
        else:
            host = args.www
            port = 5000

        app.debug = args.debug
        app.api = api
        app.vm = vm
        app.run(host=host, port=port)
    elif args.hash:
        artefacts, vm_args = reproduce.reproduceTx(args.hash, vm, api)
        saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)

        #Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = "%s/%s" % (OUTPUT_DIR, g_gen)

        print("\nCommand to execute locally (geth):\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        print("\nWith memory:\n")
        vm_args['memory'] = True
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        vm_args.pop('json', None)
        vm_args.pop('memory', None)
        vm_args['statdump'] = "true"
        print("\nFor benchmarking:\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))

        print("\nFor opviewing:\n")
        print("python3 opviewer.py -f %s/%s" %
              (saved_files['json-trace']['path'],
               saved_files['json-trace']['name']))

        print("\nFor opviewing with sources:\n")
        print(
            "python3 opviewer.py -f %s/%s --web3 '%s' -s path_to_contract_dir -j path_to_solc_combined_json --hash %s"
            % (saved_files['json-trace']['path'],
               saved_files['json-trace']['name'], args.web3, args.hash))

        (zipfilepath, zipfilename) = zipFiles(saved_files, args.hash[:8])
        print("\nZipped files into %s%s" % (zipfilepath, zipfilename))

    else:
        parser.print_usage()
コード例 #8
0
def main():
    description = """
Tool to reproduce on-chain events locally. 
This can run either as a command-line tool, or as a webapp using a built-in flask interface.
    """
    examples = """
Examples

# Reproduce a tx with a local evm binary
python3 reproducer.py --no-docker -g ~/go/src/github.com/ethereum/go-ethereum/build/bin/evm --hash 0xd6d519043d40691a36c9e718e47110309590e6f47084ac0ec00b53718e449fd3 

# Reproduce a tx with a docker evm
python3 reproducer.py -g holiman/gethvm --hash 0xd6d519043d40691a36c9e718e47110309590e6f47084ac0ec00b53718e449fd3

# Start the reproducer webapp using the default geth docker image: 
python3 reproducer.py -w localhost

Unfinished: 

* This does not _quite_ work with parity, yet, because parity does not load the code in genesis for the 'to'
  -account, it expects the code to be given as an argument. 

    """
    parser = argparse.ArgumentParser(
        description=description,
        epilog=examples,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    evmchoice = parser.add_mutually_exclusive_group()
    evmchoice.add_argument('-g',
                           '--geth-evm',
                           type=str,
                           help="Geth EVM binary or docker image name",
                           default="holiman/gethvm")
    evmchoice.add_argument('-p',
                           '--parity-evm',
                           type=str,
                           default=None,
                           help="Parity EVM binary or docker image name")

    parser.add_argument(
        "--no-docker",
        action="store_true",
        help="Set to true if using a local binary instead of a docker image")

    web_or_direct = parser.add_mutually_exclusive_group()
    web_or_direct.add_argument('-x',
                               '--hash',
                               type=str,
                               help="Don't run webapp, just lookup hash")
    if app:
        web_or_direct.add_argument(
            '-w',
            '--www',
            type=str,
            help="Run webapp on given interface (interface:port)")
        parser.add_argument(
            '-d',
            '--debug',
            action="store_true",
            default=False,
            help=
            "Run flask in debug mode (WARNING: debug on in production is insecure)"
        )

    parser.add_argument('-t',
                        '--test',
                        action="store_true",
                        default=False,
                        help="Dont run webapp, only local tests")

    web3settings = parser.add_argument_group(
        'Web3',
        'Settings about where to fetch information from (default infura)')
    web3settings.add_argument(
        "--web3",
        type=str,
        default="https://mainnet.infura.io/",
        help=
        "Web3 API url to fetch info from (default 'https://mainnet.infura.io/'"
    )

    args = parser.parse_args()

    # end of arg handling

    if args.parity_evm:
        vm = VMUtils.ParityVM(args.parity_evm, not args.no_docker)
    else:
        vm = VMUtils.GethVM(args.geth_evm, not args.no_docker)

    api = utils.getApi(args.web3)

    if args.test:
        artefacts = test(vm, api)
        import pprint
        pprint.PrettyPrinter().pprint(artefacts)
        sys.exit(0)

    if app and args.www:
        if ':' in args.www:
            host, port = args.www.split(':')
            port = port
        else:
            host = args.www
            port = 5000

        app.debug = args.debug
        app.api = api
        app.vm = vm
        app.run(host=host, port=port)

    elif args.hash:
        artefacts, vm_args = reproduce.reproduceTx(args.hash, vm, api)
        saved_files = utils.saveFiles(OUTPUT_DIR, artefacts)

        # Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = "%s/%s" % (OUTPUT_DIR, g_gen)

        print("\nCommand to execute locally (geth):\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        print("\nWith memory:\n")
        vm_args['memory'] = True
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        vm_args.pop('json', None)
        vm_args.pop('memory', None)
        vm_args['statdump'] = "true"
        print("\nFor benchmarking:\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))

        print("\nFor opviewing:\n")
        print("python3 opviewer.py -f %s/%s" %
              (saved_files['json-trace']['path'],
               saved_files['json-trace']['name']))

        print("\nFor opviewing with sources:\n")
        print(
            "python3 opviewer.py -f %s/%s --web3 '%s' -s path_to_contract_dir -j path_to_solc_combined_json --hash %s"
            % (saved_files['json-trace']['path'],
               saved_files['json-trace']['name'], args.web3, args.hash))

        logger.debug("creating zip archive for artefacts")
        prefix = args.hash[:8]
        output_archive = os.path.join(OUTPUT_DIR, "%s.zip" % prefix)
        # create a list of files to pack with zipFiles
        input_files = [(os.path.join(saved_files[v]['path'],
                                     saved_files[v]['name']),
                        saved_files[v]['name']) for v in saved_files]
        create_zip_archive(input_files=input_files,
                           output_archive=output_archive)

        print("\nZipped files into %s" % output_archive)

    else:
        parser.print_usage()
コード例 #9
0
ファイル: reproducer.py プロジェクト: ottodevs/evmlab
def main(args):

    if args.parity_evm:
        vm = VMUtils.ParityVM(args.parity_evm, not args.no_docker)
    else:
        vm = VMUtils.GethVM(args.geth_evm, not args.no_docker)

    parsed_web3 = urlparse(args.web3)
    if not parsed_web3.hostname:
        #User probably omitted 'http://'
        parsed_web3 = urlparse("http://%s" % args.web3)

    ssl = (parsed_web3.scheme == 'https')
    port = parsed_web3.port

    if not port:
        #Not explicitly defined
        if ssl:
            port = 443
        else:
            port = 80
    web3 = Web3(RPCProvider(host = parsed_web3.hostname,port= port ,ssl= ssl)) 
    api = multiapi.MultiApi(web3 = web3, etherchain = etherchain.EtherChainAPI())



    if args.test:
        artefacts = test(vm, api)
        import pprint
        pprint.PrettyPrinter().pprint(artefacts)

        sys.exit(0)

    if app and args.www:
        app.debug = args.debug
        app.api = api
        app.vm = vm
        app.run(host=args.www)
    elif args.hash:
        artefacts, vm_args = reproduce.reproduceTx(args.hash, vm, api)
        saved_files = saveFiles(artefacts)

        #Some tricks to get the right command for local replay
        p_gen = saved_files['parity genesis']['name']
        g_gen = saved_files['geth genesis']['name']
        vm_args['genesis'] = "%s/%s" % (OUTPUT_DIR, g_gen)

        print("\nCommand to execute locally (geth):\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        print("\nWith memory:\n")
        vm_args['memory'] = True
        print("%s" % " ".join(vm.makeCommand(**vm_args)))
        vm_args.pop('json', None)
        vm_args.pop('memory', None)
        vm_args['statdump'] = "true"
        print("\nFor benchmarking:\n")
        print("%s" % " ".join(vm.makeCommand(**vm_args)))

        print("\nFor opviewing:\n")
        print("python3 opviewer.py -f %s/%s" % (saved_files['json-trace']['path'],saved_files['json-trace']['name']))

        (zipfilepath, zipfilename) = zipFiles(saved_files, args.hash[:8])
        print("\nZipped files into %s%s" % (zipfilepath, zipfilename))



    else:
        parser.print_usage()