-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from gballet/fork-manager-and-2935
create fork manager + implement 2935 with it
- Loading branch information
Showing
10 changed files
with
172 additions
and
27 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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const lib = @import("../lib.zig"); | ||
const Hash32 = lib.types.Hash32; | ||
const Fork = @This(); | ||
pub const frontier = @import("./forks/frontier.zig"); | ||
pub const prague = @import("./forks/prague.zig"); | ||
|
||
vtable: *const VTable, | ||
|
||
pub const VTable = struct { | ||
update_parent_block_hash: *const fn (self: *Fork, num: u64, hash: Hash32) anyerror!void, | ||
get_parent_block_hash: *const fn (self: *Fork, index: u64) anyerror!Hash32, | ||
deinit: *const fn (self: *Fork) void, | ||
}; | ||
|
||
// Used to update the parent hash at the end of a block execution | ||
pub fn update_parent_block_hash(self: *Fork, num: u64, hash: Hash32) !void { | ||
return self.vtable.update_parent_block_hash(self, num, hash); | ||
} | ||
|
||
// Used to get the block hash of a parent when implementing the | ||
// BLOCKHASH instruction. | ||
pub fn get_parent_block_hash(self: *Fork, index: u64) !Hash32 { | ||
return self.vtable.get_parent_block_hash(self, index); | ||
} | ||
|
||
pub fn deinit(self: *Fork) void { | ||
self.vtable.deinit(self); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const std = @import("std"); | ||
const Fork = @import("../fork.zig"); | ||
const lib = @import("../../lib.zig"); | ||
const Hash32 = lib.types.Hash32; | ||
|
||
const base_fork_vtable = Fork.VTable{ | ||
.update_parent_block_hash = update_parent_block_hash, | ||
.get_parent_block_hash = get_parent_block_hash, | ||
.deinit = deinit, | ||
}; | ||
|
||
const FrontierFork = struct { | ||
const Self = @This(); | ||
|
||
fork: Fork = .{ | ||
.vtable = &base_fork_vtable, | ||
}, | ||
|
||
allocator: std.mem.Allocator, | ||
next_block_hash_index: u64 = 0, // index of the next block hash to be written | ||
block_hashes: [256]Hash32 = [_]Hash32{[_]u8{0} ** 32} ** 256, | ||
|
||
fn init(self: *Self) void { | ||
self.next_block_hash_index = 0; | ||
self.fork.vtable = &base_fork_vtable; | ||
} | ||
}; | ||
|
||
fn update_parent_block_hash(self: *Fork, block_num: u64, hash: Hash32) anyerror!void { | ||
var base_fork: *FrontierFork = @fieldParentPtr("fork", self); | ||
if (block_num != base_fork.next_block_hash_index) { | ||
return error.NonSequentialParentUpdate; | ||
} | ||
|
||
base_fork.block_hashes[base_fork.next_block_hash_index % base_fork.block_hashes.len] = hash; | ||
base_fork.next_block_hash_index += 1; | ||
} | ||
|
||
fn get_parent_block_hash(self: *Fork, block_num: u64) !Hash32 { | ||
const base_fork: *FrontierFork = @fieldParentPtr("fork", self); | ||
if (block_num > base_fork.next_block_hash_index or block_num + base_fork.block_hashes.len < base_fork.next_block_hash_index) { | ||
return std.mem.zeroes(Hash32); | ||
} | ||
|
||
return base_fork.block_hashes[block_num % base_fork.block_hashes.len]; | ||
} | ||
|
||
pub fn newFrontierFork(allocator: std.mem.Allocator) !*Fork { | ||
var base_fork = try allocator.create(FrontierFork); | ||
base_fork.init(); | ||
base_fork.allocator = allocator; | ||
return &base_fork.fork; | ||
} | ||
|
||
fn deinit(self: *Fork) void { | ||
var base_fork: *FrontierFork = @fieldParentPtr("fork", self); | ||
base_fork.allocator.destroy(base_fork); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 Address = lib.types.Address; | ||
|
||
const system_addr: Address = [_]u8{0xff} ** 19 ++ [_]u8{0xfe}; | ||
const history_size: u64 = 8192; | ||
|
||
const vtable = Fork.VTable{ | ||
.update_parent_block_hash = update_parent_block_hash, | ||
.get_parent_block_hash = get_parent_block_hash, | ||
.deinit = deinit, | ||
}; | ||
|
||
const PragueFork = struct { | ||
fork: Fork = Fork{ | ||
.vtable = &vtable, | ||
}, | ||
|
||
state_db: *StateDB, | ||
allocator: std.mem.Allocator, | ||
}; | ||
|
||
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); | ||
} | ||
|
||
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(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(self: *Fork) void { | ||
const prague_fork: *PragueFork = @fieldParentPtr("fork", self); | ||
prague_fork.allocator.destroy(prague_fork); | ||
} | ||
|
||
// a helper function to deploy the 2935 contract on a testnet. | ||
pub fn deployContract(self: *Fork) !void { | ||
const prague_fork: *PragueFork = @fieldParentPtr("fork", self); | ||
try prague_fork.state_db.db.put(system_addr, try lib.state.AccountState.init(prague_fork.allocator, system_addr, 1, 0, &[0]u8{})); | ||
} |
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
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
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
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
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
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