Skip to content

Commit

Permalink
execute block in engine api
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Jun 30, 2024
1 parent d08750d commit f6f8daf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
.hash = "1220c0cf921a5311489bdd6eaf2f2b82bc9245ba39c2da346039aa311e28db633828",
},
.httpz = .{
.url = "https://github.com/karlseguin/http.zig/archive/c8b04e3.tar.gz",
.hash = "1220f998896588d034c3027f2dae37b751b55cd0f12a6abaafc800b3f6954e93f97b",
.url = "https://github.com/karlseguin/http.zig/archive/c224ebb.tar.gz",
.hash = "12206297df84406cd4eed306acef30eb2a1a6a18b6771ebb4920391a3a7299b2e6a7",
},
.zigcli = .{
.url = "https://github.com/jiacai2050/zigcli/archive/54d095c.tar.gz",
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const ChainId = enum(u64) {

pub const ChainConfig = struct {
ChainName: []const u8,
chainId: u64 = @intFromEnum(ChainId.Mainnet),
chainId: ChainId = ChainId.Mainnet,
homesteadBlock: ?u64 = null,
daoForkBlock: ?u64 = null,
eip150Block: ?u64 = null,
Expand Down
14 changes: 5 additions & 9 deletions src/engine_api/execution_payload.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const types = @import("../types/types.zig");
const lib = @import("../lib.zig");
const blockchain = lib.blockchain;
const Blockchain = lib.blockchain.Blockchain;
const state = lib.state;
const Allocator = std.mem.Allocator;
const BlockHeader = types.BlockHeader;
Expand Down Expand Up @@ -65,15 +65,11 @@ pub const ExecutionPayload = struct {
}
};

pub fn newPayloadV2Handler(params: *ExecutionPayload) !void {
pub fn newPayloadV2Handler(blockchain: *Blockchain, params: *ExecutionPayload) !void {
const block = params.toBlock();
_ = block;
// TODO reconstruct the proof from the (currently undefined) execution witness
// and verify it. Then execute the block and return the result.
// bc.run_block(block, params.transactions);
// and verify it.

// But so far, just print the content of the payload
// std.log.info("newPayloadV2Handler: {any}", .{params});

// std.debug.print("block number={}\n", .{block.header.block_number});
// Then execute the block and return the result.
return blockchain.runBlock(block);
}
34 changes: 30 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ const Address = types.Address;
const VM = @import("blockchain/vm.zig").VM;
const StateDB = @import("state/state.zig").StateDB;
const Block = types.Block;
const BlockHeader = types.BlockHeader;
const Tx = types.Tx;
const TxSigner = @import("signer/signer.zig").TxSigner;
const Hash32 = types.Hash32;
const httpz = @import("httpz");
const engine_api = @import("engine_api/engine_api.zig");
const json = std.json;
const simargs = @import("simargs");
const version = @import("version.zig").version;
const Blockchain = lib.blockchain.Blockchain;

fn engineAPIHandler(req: *httpz.Request, res: *httpz.Response) !void {
fn engineAPIHandler(blockchain: *Blockchain, req: *httpz.Request, res: *httpz.Response) !void {
if (try req.json(engine_api.EngineAPIRequest)) |payload| {
if (std.mem.eql(u8, payload.method, "engine_newPayloadV2")) {
const execution_payload_json = payload.params[0];
var execution_payload = try execution_payload_json.to_execution_payload(res.arena);
defer execution_payload.deinit(res.arena);
try engine_api.execution_payload.newPayloadV2Handler(&execution_payload);
try engine_api.execution_payload.newPayloadV2Handler(blockchain, &execution_payload);
} else {
res.status = 500;
}
Expand Down Expand Up @@ -74,9 +77,32 @@ pub fn main() !void {
std.log.info("version: {s}", .{version});
try config.dump(allocator);

var engine_api_server = try httpz.Server().init(allocator, .{
var statedb = try StateDB.init(allocator, &[0]AccountState{});
defer statedb.deinit();
const parent_header = BlockHeader{
.parent_hash = [_]u8{0} ** 32,
.uncle_hash = types.empty_uncle_hash,
.fee_recipient = [_]u8{0} ** 20,
.state_root = [_]u8{0} ** 32,
.transactions_root = [_]u8{0} ** 32,
.receipts_root = [_]u8{0} ** 32,
.logs_bloom = [_]u8{0} ** 256,
.difficulty = 0,
.block_number = 0,
.gas_limit = 0,
.gas_used = 0,
.timestamp = 0,
.extra_data = &[_]u8{},
.prev_randao = [_]u8{0} ** 32,
.nonce = [_]u8{0} ** 8,
.base_fee_per_gas = 0,
.withdrawals_root = [_]u8{0} ** 32,
};
var blockchain = try Blockchain.init(allocator, config.chainId, &statedb, parent_header, [_]Hash32{[_]u8{0} ** 32} ** 256);

var engine_api_server = try httpz.ServerApp(*Blockchain).init(allocator, .{
.port = port,
});
}, &blockchain);
var router = engine_api_server.router();
router.post("/", engineAPIHandler);
std.log.info("Listening on {}", .{port});
Expand Down

0 comments on commit f6f8daf

Please sign in to comment.