quilt / simulation

A tool to simulate Ethereum 2.0 execution
12 stars 2 forks source link

Finish simulation, simulation server, and simulation client. #27

Closed gjtrowbridge closed 4 years ago

gjtrowbridge commented 4 years ago

Overview

As promised, the giant pull request that does ALL the things, most notably:

Context on the package as a whole is below, and a suggested plan for reviewing this code in "bite-size" pieces is in the section titled "Recommended Review Order"

Relevant Component Packages

simulation

The Simulation struct, found in the eth2/simulation package, contains the interface for simulating an Eth2-like system. Currently, it has the following methods described in the repo's README:

simulation_args

eth2/simulation_args contains all structs found in the external-facing interface of the Simulation.

As requested, these structs do not use any "internal" types found in the Simulation (eg. VariableList), and instead only use "standard" Rust types (eg. Vec). This has the advantage of allowing users of the Simulation to not need to "know" about the specifics of any internal types -- making the Simulation interface easier to use and understand.

One might expect to find these structs inside the simulation package (the same package where Simulation struct is defined). By putting them in a separate package, it means the external-facing interface of Simulation can be imported by other packages that only care about the Simulation interface, but don't necessarily want to have a dependency on the entire eth2/simulation codebase.

simulation_server

The simulation_server package defines an HTTP / JSON server that "wraps" the eth2/simulation package.

The purpose of this server is simply to allow the simulation package to be accessible via network requests. This way, a potential user of the Simulation doesn't need to necessarily import or run their own copy of the eth2/simulation package, they can instead simply send HTTP requests to an already-running simulation_server instance (whether running in a separate process on their machine or running on a separate machine online).

simulation_client

The simulation_client package defines a client with the same external-facing interface as the already-described eth2/simulation::Simulation struct.

The major difference with this package is that instead of calling the Simulation directly, the SimulationClient instead sends an HTTP request to a SimulationServer instance, and handles all encoding/decoding "under the hood" so calling the SimulationClient "feels" just like calling Simulation directly.

As requested, this allows users to access a running Simulation in their code (via under-the-hood network requests to a running SimulationServer) without necessarily needing to run a Simulation instance themselves.

Dependency Graph of All Component Packages

Quilt SImulation Dependencies

Notice that simulation_client ONLY depends on simulation_args (ie. the interface definitions), and does not need to import the server code or actual simulation code.

Design Decision Worth Discussing

I decided to have SimulationServer expose a "non-standard" HTTP/JSON api. The SimulationServer api currently only exposes POST endpoints that expect a JSON-serialized eth2/simulation_args argument struct as the data body:

Eg.

Advantages of this approach:

TL; DR: Makes the SimulationServer and SimulationClient methods easier to understand, create, and modify.

Recommended Review Order

(with links and descriptions)

I believe the easiest way to review this code in bite-size pieces is to follow the review order below: simply read the description of the file, open the link in a new tab, then review it. Once you've reviewed the file, you can close the tab and go to the next bullet point on the list.

I attempted to order everything so that there would be much less need to jump around within the "changed files" page. Once you get to a file, the description + context from reviewing the prior files should give most of the context necessary for reviewing that file.

Next Steps

gjtrowbridge commented 4 years ago

Thanks for the review, Sam! I appreciate you wading into this mammoth PR and taking a look.