Skip to content

Commit

Permalink
Fixed 98% of all signature verification failed
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-o committed Sep 12, 2024
1 parent 42c5564 commit 7696fd7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
4 changes: 3 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ config :lambda_ethereum_consensus, LambdaEthereumConsensus.PromExPlugin,
case Keyword.get(args, :log_file) do
nil ->
# Use custom formatter for prettier logs
config :logger, :default_formatter, format: {ConsoleLogger, :format}, metadata: [:slot, :root]
config :logger, :default_formatter,
format: {ConsoleLogger, :format},
metadata: [:slot, :root, :bits]

log_file ->
# Log to file
Expand Down
4 changes: 4 additions & 0 deletions lib/lambda_ethereum_consensus/logger/console_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ defmodule ConsoleLogger do
Utils.format_shorten_binary(root)
end

def format_metadata_value(:bits, slot) do
Utils.format_bitstring(slot)
end

def format_metadata_value(:slot, slot) do
Integer.to_string(slot)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/p2p/gossip/attestation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule LambdaEthereumConsensus.P2P.Gossip.Attestation do

with {:ok, uncompressed} <- :snappyer.decompress(message),
{:ok, attestation} <- Ssz.from_ssz(uncompressed, Types.Attestation) do
# TODO: validate before accepting
# TODO: (#1291) validate before accepting
Libp2pPort.validate_message(msg_id, :accept)

AttSubnetInfo.add_attestation!(subnet_id, attestation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,15 @@ defmodule LambdaEthereumConsensus.P2P.Gossip.OperationsCollector do

def handle_gossip_message(
<<_::binary-size(15)>> <> "sync_committee_contribution_and_proof" <> _,
_msg_id,
msg_id,
message
) do
with {:ok, uncompressed} <- :snappyer.decompress(message),
{:ok, %Types.SignedContributionAndProof{} = contribution_and_proof} <-
Ssz.from_ssz(uncompressed, Types.SignedContributionAndProof) do
# TODO: (#1291) validate before accepting
Libp2pPort.validate_message(msg_id, :accept)

handle_msg({:sync_committee_contribution, contribution_and_proof})
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda_ethereum_consensus/p2p/gossip/sync_committee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule LambdaEthereumConsensus.P2P.Gossip.SyncCommittee do

with {:ok, uncompressed} <- :snappyer.decompress(message),
{:ok, sync_committee_msg} <- Ssz.from_ssz(uncompressed, Types.SyncCommitteeMessage) do
# TODO: validate before accepting
# TODO: (#1291) validate before accepting
Libp2pPort.validate_message(msg_id, :accept)

SyncSubnetInfo.add_message!(subnet_id, sync_committee_msg)
Expand Down
12 changes: 12 additions & 0 deletions lib/lambda_ethereum_consensus/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ defmodule LambdaEthereumConsensus.Utils do
"0x#{String.slice(encoded, 0, 3)}..#{String.slice(encoded, -4, 4)}"
end

@doc """
Format a bitstring to a base 2 representation.
"""
@spec format_bitstring(bitstring) :: String.t()
def format_bitstring(bitstring) do
# This coudl also be done with Bitwise.to_integer/1 and Integer.to_string/2 but
# it would lack the padding.
bitstring
|> :binary.bin_to_list()
|> Enum.map_join(" ", fn int -> Integer.to_string(int, 2) |> String.pad_leading(8, "0") end)
end

def chunk_by_sizes(enum, sizes), do: chunk_by_sizes(enum, sizes, [], 0, [])

# No more elements, there may be a leftover chunk to add.
Expand Down
66 changes: 33 additions & 33 deletions lib/lambda_ethereum_consensus/validator/block_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ defmodule LambdaEthereumConsensus.Validator.BlockBuilder do
)
end)

Logger.info(
Logger.debug(
"[BlockBuilder] Contributions to aggregate: #{inspect(contributions, pretty: true)}",
slot: slot
)
Expand All @@ -231,43 +231,43 @@ defmodule LambdaEthereumConsensus.Validator.BlockBuilder do

sync_subcommittee_size = Misc.sync_subcommittee_size()

for %{message: %{contribution: contribution}} <- contributions, reduce: aggregate_data do
aggregate_data ->
right_size = sync_subcommittee_size * contribution.subcommittee_index
aggregate_data =
for %{message: %{contribution: contribution}} <- contributions, reduce: aggregate_data do
aggregate_data ->
left_size = sync_subcommittee_size * contribution.subcommittee_index

left_size =
sync_subcommittee_size *
(Constants.sync_committee_subnet_count() - 1 - contribution.subcommittee_index)
right_size =
sync_subcommittee_size *
(Constants.sync_committee_subnet_count() - 1 - contribution.subcommittee_index)

placed_bits =
<<0::size(right_size), contribution.aggregation_bits::bitstring, 0::size(left_size)>>
placed_bits =
<<0::size(right_size), contribution.aggregation_bits::bitstring, 0::size(left_size)>>

aggregated_bits = BitVector.bitwise_or(aggregate_data.aggregation_bits, placed_bits)
aggregated_bits = BitVector.bitwise_or(aggregate_data.aggregation_bits, placed_bits)

%{
aggregate_data
| aggregation_bits: aggregated_bits,
signatures: [
contribution.signature | aggregate_data.signatures
]
}
end
|> then(fn aggregate_data ->
Logger.info(
"[BlockBuilder] SyncAggregate to construct: #{inspect(aggregate_data, pretty: true)}",
slot: slot
)
%{
aggregate_data
| aggregation_bits: aggregated_bits,
signatures: [
contribution.signature | aggregate_data.signatures
]
}
end

%Types.SyncAggregate{
sync_committee_bits:
BitVector.new(aggregate_data.aggregation_bits, ChainSpec.get("SYNC_COMMITTEE_SIZE")),
sync_committee_signature:
case aggregate_data.signatures do
[] -> <<192, 0::760>>
signatures -> Bls.aggregate(signatures) |> then(fn {:ok, signature} -> signature end)
end
}
end)
Logger.debug(
"[BlockBuilder] SyncAggregate to construct: #{inspect(aggregate_data.signatures, pretty: true)}",
bits: aggregate_data.aggregation_bits,
slot: slot
)

%Types.SyncAggregate{
sync_committee_bits: aggregate_data.aggregation_bits,
sync_committee_signature:
case aggregate_data.signatures do
[] -> <<192, 0::760>>
signatures -> Bls.aggregate(signatures) |> then(fn {:ok, signature} -> signature end)
end
}
end

defp get_finalized_block_hash(state) do
Expand Down

0 comments on commit 7696fd7

Please sign in to comment.