示例#1
0
def get_entry_exit_effect_slot(slot: SlotNumber, epoch_length: int,
                               entry_exit_delay: int) -> SlotNumber:
    """
    An entry or exit triggered in the slot given by the input takes effect at
    the slot given by the output.
    """
    # TODO: update to epoch version
    return SlotNumber((slot - slot % epoch_length) + epoch_length +
                      entry_exit_delay)
示例#2
0
 def _get_slot_by_root(db: BaseDB,
                       block_root: Hash32) -> SlotNumber:
     validate_word(block_root, title="block root")
     try:
         encoded_slot = db[SchemaV1.make_block_root_to_slot_lookup_key(block_root)]
     except KeyError:
         raise BlockNotFound("No block with root {0} found".format(
             encode_hex(block_root)))
     return SlotNumber(rlp.decode(encoded_slot, sedes=rlp.sedes.big_endian_int))
示例#3
0
 def create_pending_validator(
         cls, pubkey: BLSPubkey, withdrawal_credentials: Hash32,
         randao_commitment: Hash32,
         custody_commitment: Hash32) -> 'ValidatorRecord':
     """
     Return a new pending ``ValidatorRecord`` with the given fields.
     """
     return cls(
         pubkey=pubkey,
         withdrawal_credentials=withdrawal_credentials,
         randao_commitment=randao_commitment,
         randao_layers=0,
         activation_epoch=FAR_FUTURE_EPOCH,
         exit_epoch=FAR_FUTURE_EPOCH,
         withdrawal_epoch=FAR_FUTURE_EPOCH,
         penalized_epoch=FAR_FUTURE_EPOCH,
         exit_count=0,
         status_flags=0,
         custody_commitment=custody_commitment,
         latest_custody_reseed_slot=SlotNumber(0),
         penultimate_custody_reseed_slot=SlotNumber(0),
     )
示例#4
0
def get_shuffling(*, seed: Hash32, validators: Sequence['ValidatorRecord'],
                  slot: SlotNumber, epoch_length: int,
                  target_committee_size: int,
                  shard_count: int) -> Tuple[Iterable[ValidatorIndex], ...]:
    """
    Shuffle ``validators`` into crosslink committees seeded by ``seed`` and ``slot``.
    Return a list of ``EPOCH_LENGTH * committees_per_slot`` committees where each
    committee is itself a list of validator indices.

    If ``get_shuffling(seed, validators, slot)`` returns some value ``x``, it should return
    the same value ``x`` for the same seed and slot and possible future modifications of
    validators forever in phase 0, and until the ~1 year deletion delay in phase 2 and in the
    future.
    """
    # Normalizes slot to start of epoch boundary
    slot = SlotNumber(slot - slot % epoch_length)

    active_validator_indices = get_active_validator_indices(validators, slot)

    committees_per_slot = get_committee_count_per_slot(
        len(active_validator_indices),
        shard_count,
        epoch_length,
        target_committee_size,
    )

    # Shuffle
    seed = bitwise_xor(seed, Hash32(slot.to_bytes(32, byteorder="big")))
    shuffled_active_validator_indices = shuffle(active_validator_indices, seed)

    # Split the shuffled list into epoch_length * committees_per_slot pieces
    return tuple(
        split(
            shuffled_active_validator_indices,
            committees_per_slot * epoch_length,
        ))
示例#5
0
    async def _handle_get_beacon_blocks(self, peer: BCCPeer,
                                        msg: GetBeaconBlocksMessage) -> None:
        if not peer.is_operational:
            return

        request_id = msg["request_id"]
        max_blocks = msg["max_blocks"]
        block_slot_or_root = msg["block_slot_or_root"]

        try:
            if isinstance(block_slot_or_root, int):
                # TODO: pass accurate `block_class: Type[BaseBeaconBlock]` under
                # per BeaconStateMachine fork
                start_block = self.db.get_canonical_block_by_slot(
                    SlotNumber(block_slot_or_root),
                    BeaconBlock,
                )
            elif isinstance(block_slot_or_root, bytes):
                # TODO: pass accurate `block_class: Type[BaseBeaconBlock]` under
                # per BeaconStateMachine fork
                start_block = self.db.get_block_by_root(
                    Hash32(block_slot_or_root), BeaconBlock)
            else:
                raise TypeError(
                    f"Invariant: unexpected type for 'block_slot_or_root': "
                    f"{type(block_slot_or_root)}")
        except BlockNotFound:
            start_block = None

        if start_block is not None:
            self.logger.debug2(
                "%s requested %d blocks starting with %s",
                peer,
                max_blocks,
                start_block,
            )
            blocks = self._get_blocks(start_block, max_blocks)
        else:
            self.logger.debug2("%s requested unknown block %s",
                               block_slot_or_root)
            blocks = ()

        self.logger.debug2("Replying to %s with %d blocks", peer, len(blocks))
        peer.sub_proto.send_blocks(blocks, request_id)
示例#6
0
def generate_seed(state: 'BeaconState', slot: SlotNumber, epoch_length: int,
                  seed_lookahead: int, latest_index_roots_length: int,
                  latest_randao_mixes_length: int) -> Hash32:
    """
    Generate a seed for the given ``slot``.

    TODO: it's slot version, will be changed to epoch version.
    """
    randao_mix = get_randao_mix(
        state,
        SlotNumber(slot - seed_lookahead),
        latest_randao_mixes_length=latest_randao_mixes_length,
    )
    active_index_root = get_active_index_root(
        state,
        slot,
        epoch_length=epoch_length,
        latest_index_roots_length=latest_index_roots_length,
    )

    return hash_eth2(randao_mix + active_index_root)
示例#7
0
def get_epoch_start_slot(epoch: EpochNumber, epoch_length: int) -> SlotNumber:
    return SlotNumber(epoch * epoch_length)
示例#8
0
 TARGET_COMMITTEE_SIZE=2**7,  # (= 128) validators
 EJECTION_BALANCE=Ether(2**4),  # (= 16) ETH
 MAX_BALANCE_CHURN_QUOTIENT=2**5,  # (= 32)
 BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
 MAX_CASPER_VOTES=2**10,  # (= 1,024) votes
 LATEST_BLOCK_ROOTS_LENGTH=2**13,  # (= 8,192) block roots
 LATEST_RANDAO_MIXES_LENGTH=2**13,  # (= 8,192) randao mixes
 LATEST_PENALIZED_EXIT_LENGTH=2**13,  # (= 8,192) randao mixes
 # Deposit contract
 DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS,  # TBD
 DEPOSIT_CONTRACT_TREE_DEPTH=2**5,  # (= 32)
 MIN_DEPOSIT=Ether(2**0),  # (= 1) ETH
 MAX_DEPOSIT=Ether(2**5),  # (= 32) ETH
 # Initial values
 GENESIS_FORK_VERSION=0,
 GENESIS_SLOT=SlotNumber(0),
 GENESIS_START_SHARD=ShardNumber(0),
 BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
 # Time parameters
 SLOT_DURATION=Second(6),  # seconds
 MIN_ATTESTATION_INCLUSION_DELAY=2**2,  # (= 4) slots
 EPOCH_LENGTH=2**6,  # (= 64) slots
 SEED_LOOKAHEAD=2**6,  # (= 64) slots
 ENTRY_EXIT_DELAY=2**8,  # (= 256) slots
 ETH1_DATA_VOTING_PERIOD=2**10,  # (= 1,024) slots
 MIN_VALIDATOR_WITHDRAWAL_TIME=2**14,  # (= 16,384) slots
 # Reward and penalty quotients
 BASE_REWARD_QUOTIENT=2**10,  # (= 1,024)
 WHISTLEBLOWER_REWARD_QUOTIENT=2**9,  # (= 512)
 INCLUDER_REWARD_QUOTIENT=2**3,  # (= 8)
 INACTIVITY_PENALTY_QUOTIENT=2**24,  # (= 16,777,216)
示例#9
0
 EJECTION_BALANCE=Gwei(2**4 * GWEI_PER_ETH),  # (= 16,000,000,000) Gwei
 MAX_BALANCE_CHURN_QUOTIENT=2**5,  # (= 32)
 BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
 MAX_INDICES_PER_SLASHABLE_VOTE=2**12,  # (= 4,096) votes
 LATEST_BLOCK_ROOTS_LENGTH=2**13,  # (= 8,192) slots
 LATEST_INDEX_ROOTS_LENGTH=2**13,  # (= 8,192) epochs
 LATEST_RANDAO_MIXES_LENGTH=2**13,  # (= 8,192) epochs
 LATEST_PENALIZED_EXIT_LENGTH=2**13,  # (= 8,192) epochs
 # Deposit contract
 DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS,  # TBD
 DEPOSIT_CONTRACT_TREE_DEPTH=2**5,  # (= 32)
 MIN_DEPOSIT_AMOUNT=Gwei(2**0 * GWEI_PER_ETH),  # (= 1,000,000,000) Gwei
 MAX_DEPOSIT_AMOUNT=Gwei(2**5 * GWEI_PER_ETH),  # (= 32,000,000,00) Gwei
 # Initial values
 GENESIS_FORK_VERSION=0,
 GENESIS_SLOT=SlotNumber(0),
 GENESIS_EPOCH=slot_to_epoch(SlotNumber(0), 2**6),  # GENESIS_EPOCH=slot_to_epoch(GENESIS_SLOT)
 GENESIS_START_SHARD=ShardNumber(0),
 BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
 # Time parameters
 SLOT_DURATION=Second(6),  # seconds
 MIN_ATTESTATION_INCLUSION_DELAY=2**2,  # (= 4) slots
 EPOCH_LENGTH=2**6,  # (= 64) slots
 SEED_LOOKAHEAD=2**0,  # (= 1) epochs
 ENTRY_EXIT_DELAY=2**2,  # (= 4) epochs
 ETH1_DATA_VOTING_PERIOD=2**4,  # (= 16) epochs
 MIN_VALIDATOR_WITHDRAWAL_TIME=2**8,  # (= 256) epochs
 # Reward and penalty quotients
 BASE_REWARD_QUOTIENT=2**10,  # (= 1,024)
 WHISTLEBLOWER_REWARD_QUOTIENT=2**9,  # (= 512)
 INCLUDER_REWARD_QUOTIENT=2**3,  # (= 8)
示例#10
0
from eth2.beacon.typing import (
    BLSSignature,
    SlotNumber,
)

#
# shuffle function
#

# The size of 3 bytes in integer
# sample_range = 2 ** (3 * 8) = 2 ** 24 = 16777216
# sample_range = 16777216

# Entropy is consumed from the seed in 3-byte (24 bit) chunks.
RAND_BYTES = 3
# The highest possible result of the RNG.
RAND_MAX = 2**(RAND_BYTES * 8) - 1

EMPTY_SIGNATURE = BLSSignature(b'\x00' * 96)
GWEI_PER_ETH = 10**9
FAR_FUTURE_SLOT = SlotNumber(2**64 - 1)