Skip to content

Commit

Permalink
feat: ping metadata (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
karasakalmt authored Jan 12, 2024
1 parent 5b1c40c commit 5c5d333
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 29 deletions.
44 changes: 30 additions & 14 deletions config/networks/mainnet/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Mainnet config

# Extends the mainnet preset
PRESET_BASE: 'mainnet'
PRESET_BASE: "mainnet"

# Free-form short name of the network that this configuration applies to - known
# canonical network names include:
# * 'mainnet' - there can be only one
# * 'prater' - testnet
# Must match the regex: [a-z0-9\-]
CONFIG_NAME: 'mainnet'
CONFIG_NAME: "mainnet"

# Transition
# ---------------------------------------------------------------
Expand All @@ -18,8 +18,6 @@ TERMINAL_TOTAL_DIFFICULTY: 58750000000000000000000
TERMINAL_BLOCK_HASH: 0x0000000000000000000000000000000000000000000000000000000000000000
TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH: 18446744073709551615



# Genesis
# ---------------------------------------------------------------
# `2**14` (= 16,384)
Expand All @@ -31,7 +29,6 @@ GENESIS_FORK_VERSION: 0x00000000
# 604800 seconds (7 days)
GENESIS_DELAY: 604800


# Forking
# ---------------------------------------------------------------
# Some forks are disabled for now:
Expand All @@ -40,20 +37,17 @@ GENESIS_DELAY: 604800

# Altair
ALTAIR_FORK_VERSION: 0x01000000
ALTAIR_FORK_EPOCH: 74240 # Oct 27, 2021, 10:56:23am UTC
ALTAIR_FORK_EPOCH: 74240 # Oct 27, 2021, 10:56:23am UTC
# Bellatrix
BELLATRIX_FORK_VERSION: 0x02000000
BELLATRIX_FORK_EPOCH: 144896 # Sept 6, 2022, 11:34:47am UTC
BELLATRIX_FORK_EPOCH: 144896 # Sept 6, 2022, 11:34:47am UTC
# Capella
CAPELLA_FORK_VERSION: 0x03000000
CAPELLA_FORK_EPOCH: 194048 # April 12, 2023, 10:27:35pm UTC
CAPELLA_FORK_EPOCH: 194048 # April 12, 2023, 10:27:35pm UTC
# Deneb
DENEB_FORK_VERSION: 0x04000000
DENEB_FORK_EPOCH: 18446744073709551615




# Time parameters
# ---------------------------------------------------------------
# 12 seconds
Expand All @@ -67,7 +61,6 @@ SHARD_COMMITTEE_PERIOD: 256
# 2**11 (= 2,048) Eth1 blocks ~8 hours
ETH1_FOLLOW_DISTANCE: 2048


# Validator cycle
# ---------------------------------------------------------------
# 2**2 (= 4)
Expand All @@ -81,7 +74,6 @@ MIN_PER_EPOCH_CHURN_LIMIT: 4
# 2**16 (= 65,536)
CHURN_LIMIT_QUOTIENT: 65536


# Fork choice
# ---------------------------------------------------------------
# 40%
Expand All @@ -96,5 +88,29 @@ DEPOSIT_CONTRACT_ADDRESS: 0x00000000219ab540356cBB839Cbe05303d7705Fa

# Networking
# ---------------------------------------------------------------
# `10 * 2**20` (= 10485760, 10 MiB)
GOSSIP_MAX_SIZE: 10485760
# `2**10` (= 1024)
MAX_REQUEST_BLOCKS: 1024
MAX_REQUEST_BLOCKS: 1024
# `2**8` (= 256)
EPOCHS_PER_SUBNET_SUBSCRIPTION: 256
# `MIN_VALIDATOR_WITHDRAWABILITY_DELAY + CHURN_LIMIT_QUOTIENT // 2` (= 33024, ~5 months)
MIN_EPOCHS_FOR_BLOCK_REQUESTS: 33024
# `10 * 2**20` (=10485760, 10 MiB)
MAX_CHUNK_SIZE: 10485760
# 5s
TTFB_TIMEOUT: 5
# 10s
RESP_TIMEOUT: 10
ATTESTATION_PROPAGATION_SLOT_RANGE: 32
# 500ms
MAXIMUM_GOSSIP_CLOCK_DISPARITY: 500
MESSAGE_DOMAIN_INVALID_SNAPPY: 0x00000000
MESSAGE_DOMAIN_VALID_SNAPPY: 0x01000000
# 2 subnets per node
SUBNETS_PER_NODE: 2
# 2**8 (= 64)
ATTESTATION_SUBNET_COUNT: 64
ATTESTATION_SUBNET_EXTRA_BITS: 0
# ceillog2(ATTESTATION_SUBNET_COUNT) + ATTESTATION_SUBNET_EXTRA_BITS
ATTESTATION_SUBNET_PREFIX_BITS: 6
37 changes: 27 additions & 10 deletions config/networks/minimal/config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Minimal config

# Extends the minimal preset
PRESET_BASE: 'minimal'
PRESET_BASE: "minimal"

# Free-form short name of the network that this configuration applies to - known
# canonical network names include:
# * 'mainnet' - there can be only one
# * 'prater' - testnet
# Must match the regex: [a-z0-9\-]
CONFIG_NAME: 'minimal'
CONFIG_NAME: "minimal"

# Transition
# ---------------------------------------------------------------
Expand All @@ -18,8 +18,6 @@ TERMINAL_TOTAL_DIFFICULTY: 11579208923731619542357098500868790785326998466564056
TERMINAL_BLOCK_HASH: 0x0000000000000000000000000000000000000000000000000000000000000000
TERMINAL_BLOCK_HASH_ACTIVATION_EPOCH: 18446744073709551615



# Genesis
# ---------------------------------------------------------------
# [customized]
Expand All @@ -31,7 +29,6 @@ GENESIS_FORK_VERSION: 0x00000001
# [customized] Faster to spin up testnets, but does not give validator reasonable warning time for genesis
GENESIS_DELAY: 300


# Forking
# ---------------------------------------------------------------
# Values provided for illustrative purposes.
Expand All @@ -50,7 +47,6 @@ CAPELLA_FORK_EPOCH: 18446744073709551615
DENEB_FORK_VERSION: 0x04000001
DENEB_FORK_EPOCH: 18446744073709551615


# Time parameters
# ---------------------------------------------------------------
# [customized] Faster for testing purposes
Expand All @@ -64,7 +60,6 @@ SHARD_COMMITTEE_PERIOD: 64
# [customized] process deposits more quickly, but insecure
ETH1_FOLLOW_DISTANCE: 16


# Validator cycle
# ---------------------------------------------------------------
# 2**2 (= 4)
Expand All @@ -78,13 +73,11 @@ MIN_PER_EPOCH_CHURN_LIMIT: 4
# [customized] scale queue churn at much lower validator counts for testing
CHURN_LIMIT_QUOTIENT: 32


# Fork choice
# ---------------------------------------------------------------
# 40%
PROPOSER_SCORE_BOOST: 40


# Deposit contract
# ---------------------------------------------------------------
# Ethereum Goerli testnet
Expand All @@ -95,5 +88,29 @@ DEPOSIT_CONTRACT_ADDRESS: 0x1234567890123456789012345678901234567890

# Networking
# ---------------------------------------------------------------
# `10 * 2**20` (= 10485760, 10 MiB)
GOSSIP_MAX_SIZE: 10485760
# `2**10` (= 1024)
MAX_REQUEST_BLOCKS: 1024
MAX_REQUEST_BLOCKS: 1024
# `2**8` (= 256)
EPOCHS_PER_SUBNET_SUBSCRIPTION: 256
# [customized] `MIN_VALIDATOR_WITHDRAWABILITY_DELAY + CHURN_LIMIT_QUOTIENT // 2` (= 272)
MIN_EPOCHS_FOR_BLOCK_REQUESTS: 272
# `10 * 2**20` (=10485760, 10 MiB)
MAX_CHUNK_SIZE: 10485760
# 5s
TTFB_TIMEOUT: 5
# 10s
RESP_TIMEOUT: 10
ATTESTATION_PROPAGATION_SLOT_RANGE: 32
# 500ms
MAXIMUM_GOSSIP_CLOCK_DISPARITY: 500
MESSAGE_DOMAIN_INVALID_SNAPPY: 0x00000000
MESSAGE_DOMAIN_VALID_SNAPPY: 0x01000000
# 2 subnets per node
SUBNETS_PER_NODE: 2
# 2**8 (= 64)
ATTESTATION_SUBNET_COUNT: 64
ATTESTATION_SUBNET_EXTRA_BITS: 0
# ceillog2(ATTESTATION_SUBNET_COUNT) + ATTESTATION_SUBNET_EXTRA_BITS
ATTESTATION_SUBNET_PREFIX_BITS: 6
3 changes: 3 additions & 0 deletions lib/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@ defmodule Constants do

@spec proposer_score_boost() :: non_neg_integer()
def proposer_score_boost, do: 3

@spec sync_committee_subnet_count() :: non_neg_integer()
def sync_committee_subnet_count, do: 4
end
1 change: 1 addition & 0 deletions lib/lambda_ethereum_consensus/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule LambdaEthereumConsensus.Application do
{LambdaEthereumConsensus.Telemetry, []},
{LambdaEthereumConsensus.Store.Db, []},
{LambdaEthereumConsensus.Beacon.BeaconNode, [checkpoint_sync]},
{LambdaEthereumConsensus.P2P.Metadata, []},
{BeaconApi.Endpoint, []}
]

Expand Down
10 changes: 5 additions & 5 deletions lib/lambda_ethereum_consensus/p2p/incoming_requests/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do
@moduledoc """
This module handles Req/Resp domain requests.
"""
require Logger

alias LambdaEthereumConsensus.Beacon.BeaconChain
alias LambdaEthereumConsensus.Store.BlockStore
alias LambdaEthereumConsensus.{Libp2pPort, P2P}
alias LambdaEthereumConsensus.Store.BlockStore

require Logger

Expand Down Expand Up @@ -86,10 +87,9 @@ defmodule LambdaEthereumConsensus.P2P.IncomingRequests.Handler do
end

defp handle_req("metadata/2/ssz_snappy", message_id, _message) do
# Values are hardcoded
with {:ok, payload} <-
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
|> Snappy.compress() do
with metadata <- P2P.Metadata.get_metadata(),
{:ok, metadata_ssz} <- Ssz.to_ssz(metadata),
{:ok, payload} <- Snappy.compress(metadata_ssz) do
Libp2pPort.send_response(message_id, <<0, 17>> <> payload)
end
end
Expand Down
100 changes: 100 additions & 0 deletions lib/lambda_ethereum_consensus/p2p/metadata.ex
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

0 comments on commit 5c5d333

Please sign in to comment.