Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cli options #24

Closed
wants to merge 5 commits into from
Closed
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: 3 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
const mod_httpz = httpz.module("httpz");
const zigcli_dep = b.dependency("zig-cli", .{ .target = target, .optimize = optimize });
const zigcli_mod = zigcli_dep.module("zig-cli");

const ethash = b.addStaticLibrary(.{
.name = "ethash",
Expand Down Expand Up @@ -106,6 +108,7 @@ pub fn build(b: *std.Build) void {
exe.linkLibrary(depSecp256k1.artifact("secp256k1"));
exe.addModule("zig-eth-secp256k1", mod_secp256k1);
exe.addModule("httpz", mod_httpz);
exe.addModule("zig-cli", zigcli_mod);

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
Expand Down
5 changes: 5 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
.url = "https://github.com/karlseguin/http.zig/archive/1a82beb0dfc22e6fc38e9918e323f8fbd3cb78a3.tar.gz",
.hash = "122020cbd399273b1220f11c4b2d734b86c94cc5f1c2d6d90fc766580bc0e25b0dfb",
},
.@"zig-cli" = .{
.url = "https://github.com/sam701/zig-cli/archive/ddf49596a9225c91e6cd9c28904aec598d5becf0.tar.gz",
.hash = "1220787d26a153a3755d55c8b65af53f7b1c4d79add4a47e2898dc776f3a16f1ce89",
},
},
.paths = .{""},
}
4 changes: 2 additions & 2 deletions src/blockchain/blockchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Keccak256 = std.crypto.hash.sha3.Keccak256;

pub const Blockchain = struct {
allocator: Allocator,
chain_id: config.ChainId,
chain_id: config.NetworkID,
state: *StateDB,
prev_block: BlockHeader,
last_256_blocks_hashes: [256]Hash32, // ordered in asc order
Expand All @@ -40,7 +40,7 @@ pub const Blockchain = struct {
// The caller **does not** transfer ownership of prev_block.
pub fn init(
allocator: Allocator,
chain_id: config.ChainId,
chain_id: config.NetworkID,
state: *StateDB,
prev_block: BlockHeader,
last_256_blocks_hashes: [256]Hash32,
Expand Down
7 changes: 6 additions & 1 deletion src/config/config.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
pub const ChainId = enum(u64) {
pub const NetworkID = enum(u64) {
SpecTest = 0,
Mainnet = 1,
Goerli = 5,
Holesky = 17000,
Kaustinen = 69420,
Sepolia = 11155111,
};

pub const Config = struct {
engine_port: u16 = 8551,
network_id: u64 = @intFromEnum(NetworkID.Mainnet),
};
43 changes: 36 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const types = @import("types/types.zig");
const crypto = @import("crypto/crypto.zig");
const ecdsa = crypto.ecdsa;
const config = @import("config/config.zig");
const Config = @import("config/config.zig").Config;
const AccountState = @import("state/state.zig").AccountState;
const Address = types.Address;
const VM = @import("blockchain/vm.zig").VM;
Expand All @@ -13,6 +13,7 @@ const TxSigner = @import("signer/signer.zig").TxSigner;
const httpz = @import("httpz");
const engine_api = @import("engine_api/engine_api.zig");
const json = std.json;
const cli = @import("zig-cli");

fn engineAPIHandler(req: *httpz.Request, res: *httpz.Response) !void {
if (try req.json(engine_api.EngineAPIRequest)) |payload| {
Expand All @@ -26,17 +27,45 @@ fn engineAPIHandler(req: *httpz.Request, res: *httpz.Response) !void {
}
}

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();

std.log.info("Welcome to phant! 🐘", .{});
var config = Config{};

var engine_port = cli.Option{
.long_name = "engine-port",
.help = "port of the execution engine",
.value_ref = cli.mkRef(&config.engine_port),
};

var network_id_opt = cli.Option{
.long_name = "network-id",
.help = "network id",
.value_ref = cli.mkRef(&config.network_id),
};

var app = &cli.App{
.command = cli.Command{
.name = "run",
.options = &.{ &network_id_opt, &engine_port },
.target = cli.CommandTarget{
.action = cli.CommandAction{ .exec = run_server },
},
},
};

fn run_server() !void {
var engine_api_server = try httpz.Server().init(allocator, .{
.port = 8551,
.port = config.engine_port,
});
var router = engine_api_server.router();
router.post("/", engineAPIHandler);
std.log.info("Listening on 8551", .{});
std.log.info("Listening on port {}", .{config.engine_port});
try engine_api_server.listen();
}

pub fn main() !void {
std.log.info("Welcome to phant! 🐘", .{});

return cli.run(app, allocator);
}
Loading