Skip to main content

Overview

Epoch represents a consensus layer epoch in Ethereum’s proof-of-stake system. Each epoch contains 32 slots (6.4 minutes) and serves as the fundamental period for validator duties, finality, and checkpoint organization. Type: bigint & { readonly [brand]: "Epoch" }

Key Concepts

  • Duration: 32 slots = 6.4 minutes per epoch
  • Finality: Two consecutive epochs must pass for finality
  • Validator committees: Reshuffled every epoch
  • Rewards: Calculated and distributed per epoch

Methods

Epoch.from(value)

Create Epoch from number, bigint, or string.
import { Epoch } from '@tevm/primitives';

const epoch1 = Epoch.from(100000n);
const epoch2 = Epoch.from(100000);
const epoch3 = Epoch.from("0x186a0");

Epoch.toNumber(epoch)

Convert Epoch to number. Throws if exceeds MAX_SAFE_INTEGER.
const epoch = Epoch.from(100000n);
const num = Epoch.toNumber(epoch); // 100000

Epoch.toBigInt(epoch)

Convert Epoch to bigint.
const epoch = Epoch.from(100000);
const big = Epoch.toBigInt(epoch); // 100000n

Epoch.equals(a, b)

Check if two epochs are equal.
const a = Epoch.from(100000n);
const b = Epoch.from(100000n);
Epoch.equals(a, b); // true

Epoch.toSlot(epoch)

Convert epoch to the first slot of that epoch (epoch * 32).
const epoch = Epoch.from(3n);
const slot = Epoch.toSlot(epoch); // Slot 96

Usage Examples

Calculate epoch timing

import { Epoch } from '@tevm/primitives';

const genesisTime = 1606824023; // Beacon chain genesis
const epoch = Epoch.from(100000n);

// Calculate timestamp for epoch start
const epochTime = genesisTime + Number(epoch) * 32 * 12;
console.log(`Epoch ${epoch} started at: ${new Date(epochTime * 1000)}`);

Work with finality

import { Epoch } from '@tevm/primitives';

const justifiedEpoch = Epoch.from(100n);
const finalizedEpoch = Epoch.from(98n);

// Check if justified epoch can finalize
const canFinalize = justifiedEpoch - finalizedEpoch >= 2n;
console.log(`Can finalize: ${canFinalize}`);

Convert between slots and epochs

import { Epoch, Slot } from '@tevm/primitives';

const epoch = Epoch.from(5n);
const firstSlot = Epoch.toSlot(epoch); // 160
const lastSlot = Epoch.toSlot(Epoch.from(epoch + 1n)) - 1n; // 191

console.log(`Epoch ${epoch} spans slots ${firstSlot} to ${lastSlot}`);

References