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

Create EVMC-compatible library #49

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- run:
name: Run unit tests
command: nix develop --command zig build test --summary all
# TODO: Test evmc compatibility of libethzvm.
compile_test_cases:
executor: nix
steps:
Expand Down Expand Up @@ -59,6 +60,8 @@ jobs:
root: ./
paths:
- ./benchmarks/*.benchmark
- store_artifacts:
path: zig-out/
benchmark_evm:
executor: base
steps:
Expand Down
13 changes: 13 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub fn build(b: *std.Build) void {
const evm_module = b.createModule(.{
.source_file = .{ .path = "src/evm/opcodes.zig" },
});
const evm_vm_module = b.createModule(.{
.source_file = .{ .path = "src/evm/vm.zig" },
});

const exe = b.addExecutable(.{
.name = "eth-zvm",
Expand All @@ -28,6 +31,16 @@ pub fn build(b: *std.Build) void {
mnemonic_exe.addIncludePath(.{ .path = evmc_include_path });
b.installArtifact(mnemonic_exe);

const eth_zvm_lib = b.addSharedLibrary(.{
.name = "ethzvm",
.root_source_file = .{ .path = "src/evmc/main.zig" },
.target = target,
.optimize = optimize,
});
eth_zvm_lib.addIncludePath(.{ .path = evmc_include_path });
eth_zvm_lib.addModule("evm", evm_vm_module);
b.installArtifact(eth_zvm_lib);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

Expand Down
74 changes: 74 additions & 0 deletions src/evmc/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const std = @import("std");
const evm = @import("evm");
const evmc = @cImport({
@cInclude("evmc/evmc.h");
});

fn execute(
_: ?*evmc.evmc_vm,
_: ?*const evmc.evmc_host_interface,
_: ?*evmc.evmc_host_context,
_: evmc.evmc_revision,
_: ?*const evmc.evmc_message,
bytecode_c: [*c]const u8,
bytecode_c_size: usize,
) callconv(.C) evmc.evmc_result {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var vm = evm.VM{};
vm.init(allocator);
defer vm.deinit();

const fail_result = evmc.evmc_result{
.status_code = evmc.EVMC_FAILURE,
.gas_left = 0,
.gas_refund = 0,
.output_data = null,
.output_size = 0,
.release = null,
.create_address = evmc.evmc_address{
.bytes = [20]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
},
.padding = [4]u8{ 0, 0, 0, 0 },
};

const bytecode = bytecode_c[0..bytecode_c_size];

vm.run(bytecode) catch return fail_result;

return evmc.evmc_result{
.status_code = evmc.EVMC_SUCCESS,
.gas_left = 0,
.gas_refund = 0,
.output_data = vm.returnValue.ptr,
.output_size = vm.returnValue.len,
.release = null,
.create_address = evmc.evmc_address{
.bytes = [20]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
},
.padding = [4]u8{ 0, 0, 0, 0 },
};
}

fn get_capabilities(_: ?*evmc.evmc_vm) callconv(.C) evmc.evmc_capabilities_flagset {
return evmc.EVMC_CAPABILITY_EVM1;
}

fn destroy(_: ?*evmc.evmc_vm) callconv(.C) void {
return;
}

export fn evmc_create() callconv(.C) *evmc.evmc_vm {
var vm: evmc.evmc_vm = evmc.evmc_vm{
.abi_version = evmc.EVMC_ABI_VERSION,
.name = "eth-zvm",
.version = "0.0.1",
.execute = &execute,
.get_capabilities = &get_capabilities,
.set_option = null,
.destroy = &destroy,
};
return &vm;
}