-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b1c40c
commit 5c5d333
Showing
6 changed files
with
166 additions
and
29 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
defmodule LambdaEthereumConsensus.P2P.Metadata do | ||
@moduledoc """ | ||
This module handles Metadata's genserver to fetch and edit. | ||
""" | ||
|
||
use GenServer | ||
|
||
alias LambdaEthereumConsensus.Utils.BitVector | ||
alias Types.Metadata | ||
|
||
########################## | ||
### Public API | ||
########################## | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@spec get_seq_number() :: Types.uint64() | ||
def get_seq_number do | ||
GenServer.call(__MODULE__, {:get_seq_number}) | ||
end | ||
|
||
@spec get_metadata() :: Metadata.t() | ||
def get_metadata do | ||
GenServer.call(__MODULE__, :get_metadata) | ||
end | ||
|
||
@spec set_attestation_subnet(integer(), boolean()) :: any() | ||
def set_attestation_subnet(i, set) do | ||
GenServer.call(__MODULE__, {:set_attestation_subnet, i, set}) | ||
end | ||
|
||
@spec set_sync_committee(integer(), boolean()) :: any() | ||
def set_sync_committee(i, set) do | ||
GenServer.call(__MODULE__, {:set_sync_committee, i, set}) | ||
end | ||
|
||
########################## | ||
### GenServer Callbacks | ||
########################## | ||
|
||
@impl true | ||
def init(_opts) do | ||
{:ok, | ||
%Metadata{ | ||
seq_number: 0, | ||
attnets: BitVector.new(0, ChainSpec.get("ATTESTATION_SUBNET_COUNT")), | ||
syncnets: BitVector.new(0, Constants.sync_committee_subnet_count()) | ||
}} | ||
end | ||
|
||
@impl true | ||
def handle_call({:get_seq_number}, _from, metadata) do | ||
seq_number = Map.fetch!(metadata, :seq_number) | ||
{:reply, seq_number, metadata} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_metadata, _from, metadata) do | ||
{:reply, metadata} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:set_attestation_subnet, i, set}, metadata) do | ||
attnets = set_or_clear(metadata.attnets, i, set) | ||
|
||
{:noreply, | ||
%{ | ||
metadata | ||
| attnets: attnets, | ||
seq_number: metadata.seq_number + 1 | ||
}} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:set_sync_committee, i, set}, metadata) do | ||
syncnets = set_or_clear(metadata.syncnets, i, set) | ||
|
||
{:noreply, | ||
%{ | ||
metadata | ||
| syncnets: syncnets, | ||
seq_number: metadata.seq_number + 1 | ||
}} | ||
end | ||
|
||
########################## | ||
### Private Functions | ||
########################## | ||
|
||
@spec set_or_clear(BitVector.t(), integer(), boolean()) :: BitVector.t() | ||
defp set_or_clear(bitvector, i, set) do | ||
if set do | ||
BitVector.set(bitvector, i) | ||
else | ||
BitVector.clear(bitvector, i) | ||
end | ||
end | ||
end |