Skip to content

Commit

Permalink
fix a few issues for the 2935 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Aug 30, 2024
1 parent 11b0170 commit e028192
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/blockchain/forks/prague.zig
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
const std = @import("std");
const Fork = @import("../fork.zig");
const lib = @import("../../lib.zig");
const Hash32 = lib.types.Hash32;
const StateDB = lib.state.StateDB;
const self = @This();
const Address = lib.types.Address;

const system_addr: Address = [_]u8{0xff} ** 19 ++ [_]u8{0xfe};
const history_size: u64 = 8192;
var state_db: ?*StateDB = null;

const vtable = Fork.VTable{
.update_parent_block_hash = update_parent_block_hash,
.get_parent_block_hash = get_parent_block_hash,
.deinit = deinit,
};

fn update_parent_block_hash(_: *Fork, num: u64, hash: Hash32) anyerror!void {
if (self.state_db) |state_db_ptr| {
const slot: u256 = @intCast(num % history_size);
try state_db_ptr.setStorage(self.system_addr, slot, hash);
}
const PragueFork = struct {
fork: Fork = Fork{
.vtable = &vtable,
},

return error.UninitializedStateDB;
}
state_db: *StateDB,
allocator: std.mem.Allocator,
};

fn get_parent_block_hash(_: *Fork, index: u64) !Hash32 {
if (self.state_db) |state_db_ptr| {
const slot: u256 = @intCast(index % history_size);
return state_db_ptr.getStorage(self.system_addr, slot);
}
fn update_parent_block_hash(self: *Fork, num: u64, hash: Hash32) anyerror!void {
const prague_fork: *PragueFork = @fieldParentPtr("fork", self);
const slot: u256 = @intCast(num % history_size);
try prague_fork.state_db.setStorage(system_addr, slot, hash);
}

return error.UninitializedStateDB;
fn get_parent_block_hash(self: *Fork, index: u64) !Hash32 {
const prague_fork: *PragueFork = @fieldParentPtr("fork", self);
const slot: u256 = @intCast(index % history_size);
return prague_fork.state_db.getStorage(system_addr, slot);
}

// This method takes a parent fork and activate all the
// Prague-specific methods, superseding the previous fork.
pub fn enablePrague(_: *StateDB, _: *Fork) Fork {
return Fork{
.vtable = &vtable,
};
pub fn enablePrague(state_db: *StateDB, _: ?*Fork, allocator: std.mem.Allocator) !*Fork {
var prague_fork = try allocator.create(PragueFork);
prague_fork.allocator = allocator;
prague_fork.state_db = state_db;
prague_fork.fork = Fork{ .vtable = &vtable };
return &prague_fork.fork;
}

fn deinit(_: *Fork) void {}

0 comments on commit e028192

Please sign in to comment.