# Epoch Transition

* Epoch transition happens when someone calls [`beginEpochTransition()`](https://github.com/Unirep/Unirep/blob/f3502e1a551f63ab44b73444b60ead8731d45167/packages/contracts/contracts/Unirep.sol#L468) and the current block is `epochLength` blocks more since last transitioned block.
* In `beginEpochTransition()`
  * An `EpochEnded` event is emitted and `currentEpoch` increases by 1.

![currentEpoch is increased by one.](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FzIqqJIq9onaKdXDdSBNM%2F%E6%88%AA%E5%9C%96%202022-07-22%20%E4%B8%8A%E5%8D%8811.58.55.png?alt=media\&token=ebe1ea5e-a2c3-487a-87e8-8d9fd0a3783d)

* After the `EpochEnded` event is emitted, all epoch keys attested during this epoch will have their [hash chain](https://unirep.gitbook.io/unirep/protocol/glossary/reputation) sealed
  * by ***sealed*** it means that the hash chain is hashed again with `1`, e.g., \
    `hash(1, originalHashChain)`
  * if an epoch key received no attestation, it's hash chain would be `hash(1, 0)`

![](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FwBy7ort7hQ4ExtDDi01e%2F%E6%88%AA%E5%9C%96%202022-07-22%20%E4%B8%8A%E5%8D%8811.59.45.png?alt=media\&token=23f67491-fade-468d-8a66-2132a782cda7)

* After hash chain of the epoch keys are sealed, these epoch keys and their hash chain will be inserted into the [epoch tree](https://unirep.gitbook.io/unirep/protocol/trees#epoch-tree) of this epoch
  * there's only one epoch tree for every epoch.

![](https://1939836936-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mb9Vp6gNlb57m_A4qKo%2Fuploads%2FaLGqVJPZXkZlnmemwGsU%2Fepoch%20tree.png?alt=media\&token=76cad797-a24c-4c08-be83-3790133f1cb1)

* There will be a new [global state tree](https://unirep.gitbook.io/unirep/protocol/trees#global-state-tree) for each epoch.
* And after epoch transition, user needs to perform [user state transition](https://unirep.gitbook.io/unirep/protocol/glossary/user-state-transition) to transition his user state into the latest epoch

{% hint style="info" %}
See also

* [Epoch](https://unirep.gitbook.io/unirep/protocol/glossary/epoch)
* [Reputation](https://unirep.gitbook.io/unirep/protocol/glossary/reputation)
* [Trees](https://unirep.gitbook.io/unirep/protocol/glossary/trees)
* [User State Transition](https://unirep.gitbook.io/unirep/protocol/glossary/user-state-transition)
  {% endhint %}
