Skip to content
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,12 @@ endif()

# Add all local subdirecetories
add_subdirectory(libzeth)
add_subdirectory(libtool)

# If zeth is being used as a dependency, skip the tools build
if ("${IS_ZETH_PARENT}")
add_subdirectory(prover_server)
add_subdirectory(verifier)
add_subdirectory(zeth_tool)
Comment thread
AntoineRondelet marked this conversation as resolved.
# For now the MPC for Groth16 only is tailored to the alt_bn128 pairing group
if((${ZETH_SNARK} STREQUAL "GROTH16") AND (${MPC}))
add_subdirectory(mpc_tools)
Expand Down
1 change: 0 additions & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ grpc: $(PROTOBUF_OUTPUT)
syntax: ${PROTOBUF_OUTPUT}
flake8 `git ls-files '**.py'`
mypy -p zeth
mypy zeth/cli/zeth zeth/helper/zeth_helper
mypy -p tests
mypy -p test_commands
mypy -p test_contracts
Expand Down
12 changes: 6 additions & 6 deletions client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
"web3==4.8.2",
"websockets==6.0",
],
scripts=[
"test_commands/test_ether_mixing.py",
"test_commands/test_erc_token_mixing.py",
"zeth/helper/zeth_helper",
"zeth/cli/zeth",
]
entry_points={
'console_scripts': [
'zeth-helper=zeth.helper.zeth_helper:zeth_helper',
'zeth=zeth.cli.zeth:zeth',
],
}
)
3 changes: 2 additions & 1 deletion client/zeth/cli/zeth → client/zeth/cli/zeth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
from zeth.cli.zeth_ls_commits import ls_commits
from click import group, command, option, pass_context, ClickException, Context
from click_default_group import DefaultGroup # type: ignore
from typing import Optional, Any
from typing import Optional


# pylint: disable=redefined-builtin
@command()
@pass_context
def help(ctx: Context) -> None:
Expand Down
8 changes: 4 additions & 4 deletions client/zeth/helper/eth_gen_network_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ def eth_gen_network_config(

\b
# Write default config for "ganache" to the default file
$ zeth_helper eth-gen-network-config ganache
$ zeth-helper eth-gen-network-config ganache

\b
# Write "geth" config with a custom endpoint to default file
$ zeth_helper eth-gen-network-config geth \\
$ zeth-helper eth-gen-network-config geth \\
--eth-rpc-endpoint http://localhost:8080

\b
# Write a custom https endpoint to file, specifying the certificate
$ zeth_helper eth-gen-network-config \\
$ zeth-helper eth-gen-network-config \\
my-network \\
--eth-rpc-endpoint https://rpc.my-network.io:8545 \\
--eth-rpc-certificate rpc.my-network.io.crt

\b
# Write default network and endpoint to file "default-network"
$ zeth_helper eth-gen-network-config --output-file default-network
$ zeth-helper eth-gen-network-config --output-file default-network
"""

if eth_rpc_endpoint is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from click_default_group import DefaultGroup # type: ignore


# pylint: disable=redefined-builtin
@command()
@pass_context
def help(ctx: Context) -> None:
Expand Down
10 changes: 10 additions & 0 deletions libtool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include_directories(.)

find_package(Boost REQUIRED COMPONENTS program_options)
include_directories(${Boost_INCLUDE_DIR})

file(GLOB_RECURSE LIBTOOL_SOURCE **.?pp **.tcc)

add_library(tool ${LIBTOOL_SOURCE})
target_include_directories(tool PUBLIC ..)
target_link_libraries(tool ${Boost_PROGRAM_OPTIONS_LIBRARY})
30 changes: 30 additions & 0 deletions libtool/command.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2015-2021 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_LIBTOOL_COMMAND_HPP__
#define __ZETH_LIBTOOL_COMMAND_HPP__

#include "libtool/subcommand.hpp"

namespace libtool
{

/// Represents a top-level command, implementing parsing of global options.
template<typename GlobalOptionsT> class command
{
public:
/// Set up global options which are valid for all subcommands.
virtual void initialize_global_options(
boost::program_options::options_description &global,
boost::program_options::options_description &all_options) = 0;

/// Parse the variables map to update the GlobalOptionsT object.
virtual void parse_global_options(
GlobalOptionsT &out_options,
const boost::program_options::variables_map &vm) = 0;
};

} // namespace libtool

#endif // __ZETH_LIBTOOL_COMMAND_HPP__
27 changes: 27 additions & 0 deletions libtool/run.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2015-2021 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_LIBTOOL_RUN_HPP__
#define __ZETH_LIBTOOL_RUN_HPP__

#include "libtool/command.hpp"

namespace libtool
{

/// Execute a command object, with some global options object (initialized to
/// default values), supporting the given set of subcommands.
template<typename GlobalOptionsT>
int run_command(
command<GlobalOptionsT> &command,
GlobalOptionsT &options,
const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands,
int argc,
char **argv);

} // namespace libtool

#include "libtool/run.tcc"

#endif // __ZETH_LIBTOOL_RUN_HPP__
152 changes: 152 additions & 0 deletions libtool/run.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (c) 2015-2021 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_LIBTOOL_RUN_TCC__
#define __ZETH_LIBTOOL_RUN_TCC__

#include "libtool/run.hpp"

namespace libtool
{

namespace internal
{

template<typename GlobalOptionsT>
void print_usage(
const char *const *argv,
const po::options_description &options,
const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands)
{
std::cout << "Usage:\n"
<< " " << argv[0] << " [OPTIONS] COMMAND [ARGS] ...\n\n"
<< options;

std::cout << "\nCommands:\n";

// list_commands(commands);
using entry_t = std::pair<std::string, subcommand<GlobalOptionsT> *>;

// +4 here to ensure minimal space between command name and description
const size_t cmd_name_padded =
4 + std::max_element(
Comment thread
AntoineRondelet marked this conversation as resolved.
subcommands.begin(),
subcommands.end(),
[](const entry_t &a, const entry_t &b) {
return a.first.size() < b.first.size();
})
->first.size();

for (const auto &cmd : subcommands) {
const size_t padding = cmd_name_padded - cmd.first.size();
std::cout << " " << cmd.first << std::string(padding, ' ')
<< cmd.second->description() << "\n";
}

std::cout << std::endl;
}

template<typename GlobalOptionsT>
int run_subcommand(
const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands,
const std::string &command_name,
const char *argv0,
const std::vector<std::string> &command_args,
const GlobalOptionsT &global_options)
{
const typename std::map<std::string, subcommand<GlobalOptionsT> *>::
const_iterator sub_it = subcommands.find(command_name);
if (sub_it == subcommands.end()) {
throw po::error("invalid command");
}

subcommand<GlobalOptionsT> *sub = sub_it->second;
return sub->execute(argv0, command_args, global_options);
}

} // namespace internal

template<typename GlobalOptionsT>
int run_command(
command<GlobalOptionsT> &command,
GlobalOptionsT &options,
const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands,
int argc,
char **argv)
{
po::options_description global("Global options");
po::options_description all("");

// Default --help option
global.add_options()("help,h", "Show this help message and exit");

// Global options
command.initialize_global_options(global, all);

// Add a single positional "command" option.
po::positional_options_description pos;
all.add_options()(
"command", po::value<std::string>(), "Command to execute")(
"subargs",
po::value<std::vector<std::string>>(),
"Arguments to command");
pos.add("command", 1).add("subargs", -1);

auto usage = [&argv, &global, &subcommands]() {
internal::print_usage<GlobalOptionsT>(argv, global, subcommands);
};

try {
po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(argc, argv)
.options(all)
.positional(pos)
.allow_unregistered()
.run();
po::store(parsed, vm);

const bool help_flag = (bool)vm.count("help");

// If no command was given, print the top-level usage message. If a
// help flag was specified, exit normally, otherwise print an error
// message and exit with error. (If a command was given, the help flag
// is passed to the subcommand).
if (!vm.count("command")) {
if (help_flag) {
usage();
return 0;
}
std::cerr << "error: no command specified\n";
usage();
return 1;
}

// Parse the global options
command.parse_global_options(options, vm);

// Execute the subcommand
const std::string subcommand(vm["command"].as<std::string>());
std::vector<std::string> subargs =
po::collect_unrecognized(parsed.options, po::include_positional);
subargs[0] = std::string(argv[0]) + " " + subargs[0];

// Add the --help flag back, if given (it was absorbed by the global
// parser above).
if (help_flag) {
subargs.push_back("--help");
}

return internal::run_subcommand(
subcommands, subcommand, argv[0], subargs, options);
} catch (po::error &error) {
std::cerr << " ERROR: " << error.what() << std::endl;
usage();
}

return 1;
}

} // namespace libtool

#endif // __ZETH_LIBTOOL_RUN_TCC__
62 changes: 62 additions & 0 deletions libtool/subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2015-2021 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_LIBTOOL_SUBCOMMAND_HPP__
#define __ZETH_LIBTOOL_SUBCOMMAND_HPP__

#include <boost/program_options.hpp>
#include <map>
#include <string>

namespace po = boost::program_options;

namespace libtool
{

/// Class representing a tool subcommand.
template<typename GlobalOptionsT> class subcommand
{
public:
subcommand(
const std::string &subcommand_name, const std::string &description);
virtual ~subcommand();
const std::string &description() const;

/// Common code to parse options and invoke the virtual execute entrypoint.
int execute(
const char *argv0,
const std::vector<std::string> command_args,
const GlobalOptionsT &global);

protected:
void usage(
const char *argv0,
const boost::program_options::options_description &options);

/// Instantiation can now set up the boost program_options structures.
virtual void initialize_suboptions(
boost::program_options::options_description &options,
boost::program_options::options_description &all_options,
boost::program_options::positional_options_description &pos) = 0;

/// Instantiation can record any command-specific information from the
/// parsed variables_map.
virtual void parse_suboptions(
const boost::program_options::variables_map &vm) = 0;

/// Any command-specific output for usage.
virtual void subcommand_usage(const char *argv0) = 0;

/// Execute the command using global options defined by the caller.
virtual int execute_subcommand(const GlobalOptionsT &global) = 0;

std::string subcommand_name;
std::string subcommand_description;
};

} // namespace libtool

#include "libtool/subcommand.tcc"

#endif // __ZETH_LIBTOOL_SUBCOMMAND_HPP__
Loading