-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix a few issues for the 2935 implementation
- Loading branch information
Showing
1 changed file
with
23 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |