// Copyright 2020 Prysmatic Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";

package ethereum.eth.v1alpha1;

import "proto/eth/ext/options.proto";
import "proto/prysm/v1alpha1/attestation.proto";
import "proto/engine/v1/execution_engine.proto";

option csharp_namespace = "Ethereum.Eth.v1alpha1";
option go_package = "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1;eth";
option java_multiple_files = true;
option java_outer_classname = "BeaconBlockProto";
option java_package = "org.ethereum.eth.v1alpha1";
option php_namespace = "Ethereum\\Eth\\v1alpha1";

message GenericSignedBeaconBlock {
    oneof block {
        // Representing a signed, phase 0 beacon block.
        SignedBeaconBlock phase0 = 1;

        // Representing a signed, post-Altair fork beacon block.
        SignedBeaconBlockAltair altair = 2;

        // Representing a signed, post-Bellatrix fork beacon block.
        SignedBeaconBlockBellatrix bellatrix = 3;

        // Representing a signed, post-Bellatrix fork blinded beacon block.
        SignedBlindedBeaconBlockBellatrix blinded_bellatrix = 4;
    }
}

message GenericBeaconBlock {
    oneof block {
        // Representing a phase 0 beacon block.
        BeaconBlock phase0 = 1;

        // Representing a post-Altair fork beacon block.
        BeaconBlockAltair altair = 2;

        // Representing a post-Bellatrix fork beacon block.
        BeaconBlockBellatrix bellatrix = 3;

        // Representing a post-Bellatrix fork blinded beacon block.
        BlindedBeaconBlockBellatrix blinded_bellatrix = 4;
    }
}

// The Ethereum consensus beacon block. The message does not contain a validator signature.
message BeaconBlock {
    // Beacon chain slot that this block represents.
    uint64 slot = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Slot"];

    // Validator index of the validator that proposed the block header.
    uint64 proposer_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];

    // 32 byte root of the parent block.
    bytes parent_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte root of the resulting state after processing this block.
    bytes state_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];

    // The block body itself.
    BeaconBlockBody body = 5;
}

// The signed version of beacon block.
message SignedBeaconBlock {
    // The unsigned beacon block itself.
    BeaconBlock block = 1;

    // 96 byte BLS signature from the validator that produced this block.
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

// The unsigned version of a (HF1) beacon block. The message does not contain a validator signature.
message BeaconBlockAltair {
    // Beacon chain slot that this block represents.
    uint64 slot = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Slot"];

    // Validator index of the validator that proposed the block header.
    uint64 proposer_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];

    // 32 byte root of the parent block.
    bytes parent_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte root of the resulting state after processing this block.
    bytes state_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];

    // The  beacon block body.
    BeaconBlockBodyAltair body = 5;
}

// The signed version of a (HF1) beacon block.
message SignedBeaconBlockAltair {
    // The unsigned beacon block itself.
    BeaconBlockAltair block = 1;

    // 96 byte BLS signature from the validator that produced this block.
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

// The block body of an Ethereum consensus beacon block.
message BeaconBlockBody {
    // The validators RANDAO reveal 96 byte value.
    bytes randao_reveal = 1 [(ethereum.eth.ext.ssz_size) = "96"];

    // A reference to the Ethereum 1.x chain.
    Eth1Data eth1_data = 2;

    // 32 byte field of arbitrary data. This field may contain any data and
    // is not used for anything other than a fun message.
    bytes graffiti = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // Block operations
    // Refer to spec constants at https://github.com/ethereum/consensus-specs/blob/dev/specs/core/0_beacon-chain.md#max-operations-per-block

    // At most MAX_PROPOSER_SLASHINGS.
    repeated ProposerSlashing proposer_slashings = 4 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_ATTESTER_SLASHINGS.
    repeated AttesterSlashing attester_slashings = 5 [(ethereum.eth.ext.ssz_max) = "2"];

    // At most MAX_ATTESTATIONS.
    repeated Attestation attestations = 6 [(ethereum.eth.ext.ssz_max) = "128"];

    // At most MAX_DEPOSITS.
    repeated Deposit deposits = 7 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_VOLUNTARY_EXITS.
    repeated SignedVoluntaryExit voluntary_exits = 8 [(ethereum.eth.ext.ssz_max) = "16"];
}


// The block body of an (HF1) beacon block.
// The new addition for  is SyncAggregate for light client support.
message BeaconBlockBodyAltair {
    // The validators RANDAO reveal 96 byte value.
    bytes randao_reveal = 1 [(ethereum.eth.ext.ssz_size) = "96"];

    // A reference to the Ethereum 1.x chain.
    Eth1Data eth1_data = 2;

    // 32 byte field of arbitrary data. This field may contain any data and
    // is not used for anything other than a fun message.
    bytes graffiti = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // Block operations
    // Refer to spec constants at https://github.com/ethereum/consensus-specs/blob/dev/specs/core/0_beacon-chain.md#max-operations-per-block

    // At most MAX_PROPOSER_SLASHINGS.
    repeated ProposerSlashing proposer_slashings = 4 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_ATTESTER_SLASHINGS.
    repeated AttesterSlashing attester_slashings = 5 [(ethereum.eth.ext.ssz_max) = "2"];

    // At most MAX_ATTESTATIONS.
    repeated Attestation attestations = 6 [(ethereum.eth.ext.ssz_max) = "128"];

    // At most MAX_DEPOSITS.
    repeated Deposit deposits = 7 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_VOLUNTARY_EXITS.
    repeated SignedVoluntaryExit voluntary_exits = 8 [(ethereum.eth.ext.ssz_max) = "16"];

    // Sync aggregate object to track sync committee votes for light client support. [New in ]
    SyncAggregate sync_aggregate = 9;
}

// Proposer slashings are proofs that a slashable offense has been committed by
// proposing two conflicting blocks from the same validator.
message ProposerSlashing {
    // First conflicting signed block header.
    SignedBeaconBlockHeader header_1 = 2;

    // Second conflicting signed block header.
    SignedBeaconBlockHeader header_2 = 3;
}

// Attestor slashings are proofs that a slashable offense has been committed by
// attestating to two conflicting pieces of information by the same validator.
message AttesterSlashing {
    // First conflicting attestation.
    IndexedAttestation attestation_1 = 1;

    // Second conflicting attestation.
    IndexedAttestation attestation_2 = 2;
}

// Deposit into the Ethereum consensus from the Ethereum 1.x deposit contract.
message Deposit {
    // DepositData that is encoded into a deposit signature.
    message Data {
        // 48 byte BLS public key of the validator.
        bytes public_key = 1 [(ethereum.eth.ext.ssz_size) = "48", (ethereum.eth.ext.spec_name) = "pubkey"];

        // A 32 byte hash of the withdrawal address public key.
        bytes withdrawal_credentials = 2 [(ethereum.eth.ext.ssz_size) = "32"];

        // Deposit amount in gwei.
        uint64 amount = 3;

        // 96 byte signature from the validators public key.
        bytes signature = 4 [(ethereum.eth.ext.ssz_size) = "96"];
    }
    // 32 byte roots in the deposit tree branch.
    repeated bytes proof = 1 [(ethereum.eth.ext.ssz_size) = "33,32"];

    Data data = 2;
}

// A message that represents a validator signaling that they want to voluntarily
// withdraw from the active validator set. The message does not contain a
// validator signature.
message VoluntaryExit {
    // The epoch on when exit request becomes valid.
    uint64 epoch = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Epoch"];

    // Index of the exiting validator.
    uint64 validator_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];
}

// The signed version of voluntary exit.
message SignedVoluntaryExit {
    // The unsigned voluntary exit itself.
    VoluntaryExit exit = 1;

    // Validator's 96 byte signature
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

// Eth1Data represents references to the Ethereum 1.x deposit contract.
message Eth1Data {
    // The 32 byte deposit tree root for the last deposit included in this
    // block.
    bytes deposit_root = 1 [(ethereum.eth.ext.ssz_size) = "32"];

    // The total number of deposits included in the beacon chain since genesis
    // including the deposits in this block.
    uint64 deposit_count = 2;

    // The 32 byte block hash of the Ethereum 1.x block considered for deposit
    // inclusion.
    bytes block_hash = 3 [(ethereum.eth.ext.ssz_size) = "32"];
}

// A beacon block header is essentially a beacon block with only a reference to
// the beacon body as a 32 byte merkle tree root. This type of message is more
// lightweight than a full beacon block. The message does not contain
// a validator signature.
message BeaconBlockHeader {
    // Beacon chain slot that this block represents.
    uint64 slot = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Slot"];

    // Validator index of the validator that proposed the block header.
    uint64 proposer_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];

    // 32 byte merkle tree root of the parent ssz encoded block.
    bytes parent_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte merkle tree root of the resulting ssz encoded state after processing this block.
    bytes state_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte merkle tree root of the ssz encoded block body.
    bytes body_root = 5 [(ethereum.eth.ext.ssz_size) = "32"];
}

message SignedBeaconBlockHeader {
    // The unsigned beacon block header itself.
    BeaconBlockHeader header = 1;

    // 96 byte BLS signature from the validator that produced this block header.
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

message IndexedAttestation {
    repeated uint64 attesting_indices = 1 [(ethereum.eth.ext.ssz_max) = "2048"];

    AttestationData data = 2;

    // 96 bytes aggregate signature.
    bytes signature = 3 [(ethereum.eth.ext.ssz_size) = "96"];
}

// The sync aggregate object for the beacon chain to track sync committee votes and to
// support light client infra.
message SyncAggregate {
    // Sync committee bits as Bitvector to track votes.
    bytes sync_committee_bits = 1 [(ethereum.eth.ext.ssz_size) = "sync_committee_bytes.size", (ethereum.eth.ext.cast_type) = "sync_committee_bits.type"];

    // BLS aggregated signature of the sync committee for the ones that voted.
    bytes sync_committee_signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

message SignedBeaconBlockBellatrix {
    // The unsigned beacon block itself.
    BeaconBlockBellatrix block = 1;

    // 96 byte BLS signature from the validator that produced this block.
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

message BeaconBlockBellatrix {
    // Beacon chain slot that this block represents.
    uint64 slot = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Slot"];

    // Validator index of the validator that proposed the block header.
    uint64 proposer_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];

    // 32 byte root of the parent block.
    bytes parent_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte root of the resulting state after processing this block.
    bytes state_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];

    // The beacon block body.
    BeaconBlockBodyBellatrix body = 5;
}

message BeaconBlockBodyBellatrix {
    // The validators RANDAO reveal 96 byte value.
    bytes randao_reveal = 1 [(ethereum.eth.ext.ssz_size) = "96"];

    // A reference to the Ethereum 1.x chain.
    Eth1Data eth1_data = 2;

    // 32 byte field of arbitrary data. This field may contain any data and
    // is not used for anything other than a fun message.
    bytes graffiti = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // Block operations
    // Refer to spec constants at https://github.com/ethereum/consensus-specs/blob/dev/specs/core/0_beacon-chain.md#max-operations-per-block

    // At most MAX_PROPOSER_SLASHINGS.
    repeated ProposerSlashing proposer_slashings = 4 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_ATTESTER_SLASHINGS.
    repeated AttesterSlashing attester_slashings = 5 [(ethereum.eth.ext.ssz_max) = "2"];

    // At most MAX_ATTESTATIONS.
    repeated Attestation attestations = 6 [(ethereum.eth.ext.ssz_max) = "128"];

    // At most MAX_DEPOSITS.
    repeated Deposit deposits = 7 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_VOLUNTARY_EXITS.
    repeated SignedVoluntaryExit voluntary_exits = 8 [(ethereum.eth.ext.ssz_max) = "16"];

    // Sync aggregate object for the beacon chain to track sync committee votes. New in Altair network upgrade.
    SyncAggregate sync_aggregate = 9;

    // Execution payload from the execution chain. New in Bellatrix network upgrade.
    ethereum.engine.v1.ExecutionPayload execution_payload = 10;
}

message SignedBlindedBeaconBlockBellatrix {
    // The unsigned blinded beacon block itself.
    BlindedBeaconBlockBellatrix block = 1;

    // 96 byte BLS signature from the validator that produced this blinded block.
    bytes signature = 2 [(ethereum.eth.ext.ssz_size) = "96"];
}

message BlindedBeaconBlockBellatrix {
    // Beacon chain slot that this blinded block represents.
    uint64 slot = 1 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.Slot"];

    // Validator index of the validator that proposed the block header.
    uint64 proposer_index = 2 [(ethereum.eth.ext.cast_type) = "github.com/prysmaticlabs/eth2-types.ValidatorIndex"];

    // 32 byte root of the parent block.
    bytes parent_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // 32 byte root of the resulting state after processing this blinded block.
    bytes state_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];

    // The blinded beacon block body.
    BlindedBeaconBlockBodyBellatrix body = 5;
}

message BlindedBeaconBlockBodyBellatrix {
    // The validators RANDAO reveal 96 byte value.
    bytes randao_reveal = 1 [(ethereum.eth.ext.ssz_size) = "96"];

    // A reference to the Ethereum 1.x chain.
    Eth1Data eth1_data = 2;

    // 32 byte field of arbitrary data. This field may contain any data and
    // is not used for anything other than a fun message.
    bytes graffiti = 3 [(ethereum.eth.ext.ssz_size) = "32"];

    // At most MAX_PROPOSER_SLASHINGS.
    repeated ProposerSlashing proposer_slashings = 4 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_ATTESTER_SLASHINGS.
    repeated AttesterSlashing attester_slashings = 5 [(ethereum.eth.ext.ssz_max) = "2"];

    // At most MAX_ATTESTATIONS.
    repeated Attestation attestations = 6 [(ethereum.eth.ext.ssz_max) = "128"];

    // At most MAX_DEPOSITS.
    repeated Deposit deposits = 7 [(ethereum.eth.ext.ssz_max) = "16"];

    // At most MAX_VOLUNTARY_EXITS.
    repeated SignedVoluntaryExit voluntary_exits = 8 [(ethereum.eth.ext.ssz_max) = "16"];

    // Sync aggregate object for the beacon chain to track sync committee votes. New in Altair network upgrade.
    SyncAggregate sync_aggregate = 9;

    // Execution payload header from the execution chain. New in Bellatrix network upgrade to accommodate MEV interaction.
    ExecutionPayloadHeader execution_payload_header = 10;
}

message ExecutionPayloadHeader {
    bytes parent_hash = 1 [(ethereum.eth.ext.ssz_size) = "32"];
    bytes fee_recipient = 2 [(ethereum.eth.ext.ssz_size) = "20"];
    bytes state_root = 3 [(ethereum.eth.ext.ssz_size) = "32"];
    bytes receipt_root = 4 [(ethereum.eth.ext.ssz_size) = "32"];
    bytes logs_bloom = 5 [(ethereum.eth.ext.ssz_size) = "256"];
    bytes prev_randao = 6 [(ethereum.eth.ext.ssz_size) = "32"];
    uint64 block_number = 7;
    uint64 gas_limit = 8;
    uint64 gas_used = 9;
    uint64 timestamp = 10;
    bytes extra_data = 11 [(ethereum.eth.ext.ssz_max) = "32"];
    bytes base_fee_per_gas = 12 [(ethereum.eth.ext.ssz_size) = "32"];
    bytes block_hash = 13 [(ethereum.eth.ext.ssz_size) = "32"];
    bytes transactions_root = 14 [(ethereum.eth.ext.ssz_size) = "32"];
}
