Skip to content

Commit

Permalink
feat: ssz write schemas for all containers (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3r10 authored Jan 23, 2024
1 parent fba703f commit 363420f
Show file tree
Hide file tree
Showing 35 changed files with 421 additions and 64 deletions.
2 changes: 2 additions & 0 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ defmodule LambdaEthereumConsensus.SszEx do
defp get_fixed_size(:bool), do: 1
defp get_fixed_size({:int, size}), do: div(size, @bits_per_byte)
defp get_fixed_size({:bytes, size}), do: size
defp get_fixed_size({:vector, _, size}), do: size

defp get_fixed_size(module) when is_atom(module) do
schemas = module.schema()
Expand All @@ -576,6 +577,7 @@ defmodule LambdaEthereumConsensus.SszEx do
end

defp variable_size?({:list, _, _}), do: true
defp variable_size?({:vector, _, _}), do: false
defp variable_size?(:bool), do: false
defp variable_size?({:int, _}), do: false
defp variable_size?({:bytes, _}), do: false
Expand Down
12 changes: 11 additions & 1 deletion lib/types/beacon_chain/attestation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.Attestation do
Struct definition for `AttestationMainnet`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:aggregation_bits,
Expand All @@ -14,9 +15,18 @@ defmodule Types.Attestation do
defstruct fields

@type t :: %__MODULE__{
# max validators per committee is 2048
# MAX_VALIDATORS_PER_COMMITTEE
aggregation_bits: Types.bitlist(),
data: Types.AttestationData.t(),
signature: Types.bls_signature()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:aggregation_bits, {:bitlist, ChainSpec.get("MAX_VALIDATORS_PER_COMMITTEE")}},
{:data, Types.AttestationData},
{:signature, TypeAliases.bls_signature()}
]
end
end
6 changes: 3 additions & 3 deletions lib/types/beacon_chain/attestation_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ defmodule Types.AttestationData do
@impl LambdaEthereumConsensus.Container
def schema do
[
{:slot, {:int, 64}},
{:index, {:int, 64}},
{:beacon_block_root, {:bytes, 32}},
{:slot, TypeAliases.slot()},
{:index, TypeAliases.commitee_index()},
{:beacon_block_root, TypeAliases.root()},
{:source, Types.Checkpoint},
{:target, Types.Checkpoint}
]
Expand Down
9 changes: 9 additions & 0 deletions lib/types/beacon_chain/attester_slashing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.AttesterSlashing do
Struct definition for `AttesterSlashing`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:attestation_1,
Expand All @@ -16,4 +17,12 @@ defmodule Types.AttesterSlashing do
attestation_1: Types.IndexedAttestation.t(),
attestation_2: Types.IndexedAttestation.t()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:attestation_1, Types.IndexedAttestation},
{:attestation_2, Types.IndexedAttestation}
]
end
end
12 changes: 12 additions & 0 deletions lib/types/beacon_chain/beacon_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.BeaconBlock do
Struct definition for `BeaconBlock`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:slot,
Expand All @@ -22,4 +23,15 @@ defmodule Types.BeaconBlock do
state_root: Types.root(),
body: Types.BeaconBlockBody.t()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:slot, TypeAliases.slot()},
{:proposer_index, TypeAliases.validator_index()},
{:parent_root, TypeAliases.root()},
{:state_root, TypeAliases.root()},
{:body, Types.BeaconBlockBody}
]
end
end
28 changes: 28 additions & 0 deletions lib/types/beacon_chain/beacon_block_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.BeaconBlockBody do
Struct definition for `BeaconBlockBody`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:randao_reveal,
Expand All @@ -25,13 +26,40 @@ defmodule Types.BeaconBlockBody do
randao_reveal: Types.bls_signature(),
eth1_data: Types.Eth1Data.t(),
graffiti: Types.bytes32(),
# max MAX_PROPOSER_SLASHINGS
proposer_slashings: list(Types.ProposerSlashing.t()),
# max MAX_ATTESTER_SLASHINGS
attester_slashings: list(Types.AttesterSlashing.t()),
# max MAX_ATTESTATIONS
attestations: list(Types.Attestation.t()),
# max MAX_DEPOSITS
deposits: list(Types.Deposit.t()),
# max MAX_VOLUNTARY_EXITS
voluntary_exits: list(Types.VoluntaryExit.t()),
sync_aggregate: Types.SyncAggregate.t(),
execution_payload: Types.ExecutionPayload.t(),
# max MAX_BLS_TO_EXECUTION_CHANGES
bls_to_execution_changes: list(Types.BLSToExecutionChange.t())
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:randao_reveal, TypeAliases.bls_signature()},
{:eth1_data, Types.Eth1Data},
{:graffiti, TypeAliases.bytes32()},
{:proposer_slashings,
{:list, Types.ProposerSlashing, ChainSpec.get("MAX_PROPOSER_SLASHINGS")}},
{:attester_slashings,
{:list, Types.AttesterSlashing, ChainSpec.get("MAX_ATTESTER_SLASHINGS")}},
{:attestations, {:list, Types.Attestation, ChainSpec.get("MAX_ATTESTATIONS")}},
{:deposits, {:list, Types.Deposit, ChainSpec.get("MAX_DEPOSITS")}},
{:voluntary_exits,
{:list, Types.SignedVoluntaryExit, ChainSpec.get("MAX_VOLUNTARY_EXITS")}},
{:sync_aggregate, Types.SyncAggregate},
{:execution_payload, Types.ExecutionPayload},
{:bls_to_execution_changes,
{:list, Types.BLSToExecutionChange, ChainSpec.get("MAX_BLS_TO_EXECUTION_CHANGES")}}
]
end
end
12 changes: 12 additions & 0 deletions lib/types/beacon_chain/beacon_block_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.BeaconBlockHeader do
Struct definition for `BeaconBlockHeader`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:slot,
Expand All @@ -22,4 +23,15 @@ defmodule Types.BeaconBlockHeader do
state_root: Types.root(),
body_root: Types.root()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:slot, TypeAliases.slot()},
{:proposer_index, TypeAliases.validator_index()},
{:parent_root, TypeAliases.root()},
{:state_root, TypeAliases.root()},
{:body_root, TypeAliases.root()}
]
end
end
59 changes: 57 additions & 2 deletions lib/types/beacon_chain/beacon_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Types.BeaconState do
Struct definition for `BeaconState`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container
alias LambdaEthereumConsensus.Utils.BitVector

fields = [
:genesis_time,
Expand Down Expand Up @@ -46,32 +48,43 @@ defmodule Types.BeaconState do
fork: Types.Fork.t(),
# History
latest_block_header: Types.BeaconBlockHeader.t(),
# size SLOTS_PER_HISTORICAL_ROOT 8192
block_roots: list(Types.root()),
# size SLOTS_PER_HISTORICAL_ROOT 8192
state_roots: list(Types.root()),
# Frozen in Capella, replaced by historical_summaries
# size HISTORICAL_ROOTS_LIMIT 16777216
historical_roots: list(Types.root()),
# Eth1
eth1_data: Types.Eth1Data.t(),
# size EPOCHS_PER_ETH1_VOTING_PERIOD (64) * SLOTS_PER_EPOCH (32) = 2048
eth1_data_votes: list(Types.Eth1Data.t()),
eth1_deposit_index: Types.uint64(),
# Registry
# size VALIDATOR_REGISTRY_LIMIT 1099511627776
validators: Aja.Vector.t(Types.Validator.t()),
# size VALIDATOR_REGISTRY_LIMIT 1099511627776
balances: Aja.Vector.t(Types.gwei()),
# Randomness
# size EPOCHS_PER_HISTORICAL_VECTOR 65_536
randao_mixes: list(Types.bytes32()),
# Slashings
# Per-epoch sums of slashed effective balances
# size EPOCHS_PER_SLASHINGS_VECTOR 8192
slashings: list(Types.gwei()),
# Participation
# size VALIDATOR_REGISTRY_LIMIT 1099511627776
previous_epoch_participation: Aja.Vector.t(Types.participation_flags()),
# size VALIDATOR_REGISTRY_LIMIT 1099511627776
current_epoch_participation: Aja.Vector.t(Types.participation_flags()),
# Finality
# Bit set for every recent justified epoch
justification_bits: Types.bitvector(),
# Bit set for every recent justified epoch size 4
justification_bits: BitVector.t(),
previous_justified_checkpoint: Types.Checkpoint.t(),
current_justified_checkpoint: Types.Checkpoint.t(),
finalized_checkpoint: Types.Checkpoint.t(),
# Inactivity
# size VALIDATOR_REGISTRY_LIMIT 1099511627776
inactivity_scores: list(Types.uint64()),
# Sync
current_sync_committee: Types.SyncCommittee.t(),
Expand All @@ -86,6 +99,7 @@ defmodule Types.BeaconState do
next_withdrawal_validator_index: Types.withdrawal_index(),
# Deep history valid from Capella onwards
# [New in Capella]
# HISTORICAL_ROOTS_LIMIT
historical_summaries: list(Types.HistoricalSummary.t())
}

Expand Down Expand Up @@ -219,4 +233,45 @@ defmodule Types.BeaconState do
end
end)
end

@impl LambdaEthereumConsensus.Container
def schema do
[
{:genesis_time, TypeAliases.uint64()},
{:genesis_validators_root, TypeAliases.root()},
{:slot, TypeAliases.slot()},
{:fork, Types.Fork},
{:latest_block_header, Types.BeaconBlockHeader},
{:block_roots, {:vector, TypeAliases.root(), ChainSpec.get("SLOTS_PER_HISTORICAL_ROOT")}},
{:state_roots, {:vector, TypeAliases.root(), ChainSpec.get("SLOTS_PER_HISTORICAL_ROOT")}},
{:historical_roots, {:list, TypeAliases.root(), ChainSpec.get("HISTORICAL_ROOTS_LIMIT")}},
{:eth1_data, Types.Eth1Data},
{:eth1_data_votes,
{:list, Types.Eth1Data,
ChainSpec.get("EPOCHS_PER_ETH1_VOTING_PERIOD") * ChainSpec.get("SLOTS_PER_EPOCH")}},
{:eth1_deposit_index, TypeAliases.uint64()},
{:validators, {:list, Types.Validator, ChainSpec.get("VALIDATOR_REGISTRY_LIMIT")}},
{:balances, {:list, TypeAliases.gwei(), ChainSpec.get("VALIDATOR_REGISTRY_LIMIT")}},
{:randao_mixes,
{:vector, TypeAliases.bytes32(), ChainSpec.get("EPOCHS_PER_HISTORICAL_VECTOR")}},
{:slashings, {:vector, TypeAliases.gwei(), ChainSpec.get("EPOCHS_PER_SLASHINGS_VECTOR")}},
{:previous_epoch_participation,
{:list, TypeAliases.participation_flags(), ChainSpec.get("VALIDATOR_REGISTRY_LIMIT")}},
{:current_epoch_participation,
{:list, TypeAliases.participation_flags(), ChainSpec.get("VALIDATOR_REGISTRY_LIMIT")}},
{:justification_bits, {:bitvector, ChainSpec.get("JUSTIFICATION_BITS_LENGTH")}},
{:previous_justified_checkpoint, Types.Checkpoint},
{:current_justified_checkpoint, Types.Checkpoint},
{:finalized_checkpoint, Types.Checkpoint},
{:inactivity_scores,
{:list, TypeAliases.uint64(), ChainSpec.get("VALIDATOR_REGISTRY_LIMIT")}},
{:current_sync_committee, Types.SyncCommittee},
{:next_sync_committee, Types.SyncCommittee},
{:latest_execution_payload_header, Types.ExecutionPayloadHeader},
{:next_withdrawal_index, TypeAliases.withdrawal_index()},
{:next_withdrawal_validator_index, TypeAliases.validator_index()},
{:historical_summaries,
{:list, Types.HistoricalSummary, ChainSpec.get("HISTORICAL_ROOTS_LIMIT")}}
]
end
end
10 changes: 10 additions & 0 deletions lib/types/beacon_chain/bls_to_execution_change.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.BLSToExecutionChange do
Struct definition for `BLSToExecutionChange`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:validator_index,
Expand All @@ -18,4 +19,13 @@ defmodule Types.BLSToExecutionChange do
from_bls_pubkey: Types.bls_pubkey(),
to_execution_address: Types.execution_address()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:validator_index, TypeAliases.validator_index()},
{:from_bls_pubkey, TypeAliases.bls_pubkey()},
{:to_execution_address, TypeAliases.execution_address()}
]
end
end
5 changes: 3 additions & 2 deletions lib/types/beacon_chain/checkpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ defmodule Types.Checkpoint do
root: Types.root()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:epoch, {:int, 64}},
{:root, {:bytes, 32}}
{:epoch, TypeAliases.epoch()},
{:root, TypeAliases.root()}
]
end
end
9 changes: 9 additions & 0 deletions lib/types/beacon_chain/deposit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.Deposit do
Struct definition for `Deposit`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:proof,
Expand Down Expand Up @@ -40,4 +41,12 @@ defmodule Types.Deposit do
slashed: false
}
end

@impl LambdaEthereumConsensus.Container
def schema do
[
{:proof, {:vector, TypeAliases.bytes32(), 33}},
{:data, Types.DepositData}
]
end
end
12 changes: 12 additions & 0 deletions lib/types/beacon_chain/deposit_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.DepositData do
Struct definition for `DepositData`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:pubkey,
Expand All @@ -18,6 +19,17 @@ defmodule Types.DepositData do
pubkey: Types.bls_pubkey(),
withdrawal_credentials: Types.bytes32(),
amount: Types.gwei(),
# Signing over DepositMessage
signature: Types.bls_signature()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:pubkey, TypeAliases.bls_pubkey()},
{:withdrawal_credentials, TypeAliases.bytes32()},
{:amount, TypeAliases.gwei()},
{:signature, TypeAliases.bls_signature()}
]
end
end
10 changes: 10 additions & 0 deletions lib/types/beacon_chain/deposit_message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Types.DepositMessage do
Struct definition for `DepositMessage`.
Related definitions in `native/ssz_nif/src/types/`.
"""
@behaviour LambdaEthereumConsensus.Container

fields = [
:pubkey,
Expand All @@ -18,4 +19,13 @@ defmodule Types.DepositMessage do
withdrawal_credentials: Types.bytes32(),
amount: Types.gwei()
}

@impl LambdaEthereumConsensus.Container
def schema do
[
{:pubkey, TypeAliases.bls_pubkey()},
{:withdrawal_credentials, TypeAliases.bytes32()},
{:amount, TypeAliases.gwei()}
]
end
end
Loading

0 comments on commit 363420f

Please sign in to comment.