Skip to main content
Version: 2.0.0-beta-4

Synchronizer

Used to retrieve and manage state information for a UniRep attester. Each instance is backed by an anondb instance.

import { Synchronizer } from '@unirep/core'

const state = new Synchronizer({
unirepAddress: '0xaabbccaabbccaabbccaabbccaabbccaabbccaaaa',
provider, // an ethers.js provider
})

// start the synchronizer deamon
await state.start()
await state.waitForSync()

// stop the synchronizer deamon
state.stop()

constructor

constructor(config: {
db?: DB
attesterId?: bigint | bigint[]
provider: ethers.providers.Provider
unirepAddress: string
})

db

The database object.

synchronizer.db: DB

provider

The provider which is connected in the synchronizer.

synchronizer.provider: ethers.providers.Provider

unirepContract

The UniRep smart contract object which is connected in the synchronizer.

synchronizer.unirepContract: ethers.Contract

settings

The settings of the UniRep smart contract.

synchronizer.settings: any

attesterId

The default attester ID that is set when construction. If there is a list of attester IDs, then the first one will be the default attester ID. If no attester ID is given during construction, all attesters will be synchronized. And the default attesterId would be BigInt(0).

caution

Should check which default attester Id is carefully while synchronizing more than one attester. The default attester ID could be changed through setAttesterId.

synchronizer.attesterId: bigint

setAttesterId

Change default attesterId to another attester ID. It will fail if an attesterId is not synchronized when construction.

synchronizer.setAttesterId(attesterId: string | bigint): void

start

Start the synchronizer daemon.

synchronizer.start(): Promise<void>

stop

Stop the synchronizer daemon.

synchronizer.stop(): void

poll

Manually poll for new events. Returns a boolean indicating whether the synchronizer has synced to the head of the blockchain.

synchronizer.poll(): Promise<{ complete: boolean }>

pollRate

How frequently the synchronizer will poll the blockchain for new events (specified in milliseconds). Default: 5000

synchronizer.pollRate

blockRate

How many blocks the synchronizer will query on each poll. Default: 100000

synchronizer.blockRate

waitForSync

Wait for the synchronizer to sync up to a certain block. By default this will wait until the current latest known block (according to the provider).

synchronizer.waitForSync(blockNumber?: number): Promise<void>

calcCurrentEpoch

Calculate the current epoch determining the amount of time since the attester registration timestamp. This operation is synchronous and does not involve any database operations.

synchronizer.calcCurrentEpoch(attesterId?: bigint | string): number

calcEpochRemainingTime

Calculate the amount of time remaining in the current epoch. This operation is synchronous and does not involve any database operations.

synchronizer.calcEpochRemainingTime(attesterId?: bigint | string): number

readCurrentEpoch

Get the latest processed epoch from the database.

caution

This value may mismatch the onchain value depending on synchronization status.

synchronizer.readCurrentEpoch(attesterId?: bigint | string): Promise<{
number: number,
sealed: boolean
}>

loadCurrentEpoch

Load the current epoch number from the blockchain.

tip

Use this function in test environments where the blockchain timestamp may not match the real timestamp (e.g. due to snapshot/revert patterns).

synchronizer.loadCurrentEpoch(attesterId?: bigint | string): Promise<number>

epochTreeRoot

Get the epoch tree root for a certain epoch.

synchronizer.epochTreeRoot(
epoch: number,
attesterId?: bigint | string
): Promise<bigint>

epochTreeProof

Build a merkle inclusion proof for the tree from a certain epoch.

synchronizer.epochTreeProof(
epoch: number,
leafIndex: bigint,
attesterId?: bigint | string
): Promise<bigint[]>

nullifierExist

Determine if a nullifier exists. All nullifiers are stored in a single mapping and expected to be globally unique.

synchronizer.nullifierExist(nullifier: any): Promise<boolean>

genStateTree

Build the latest state tree for a certain epoch.

synchronizer.genStateTree(
epoch: number | bigint,
attesterId?: bigint | string
): Promise<IncrementalMerkleTree>

genEpochTree

Build the latest epoch tree for a certain epoch.

synchronizer.genEpochTree(
epoch: number | bigint,
attesterId?: bigint | string
): Promise<IncrementalMerkleTree>

genHistoryTree

Build the latest history tree for the current attester.

synchronizer.genHistoryTree(): Promise<IncrementalMerkleTree>

stateTreeRootExists

Determine if a state root exists in a certain epoch.

synchronizer.stateTreeRootExists(
root: bigint | string,
epoch: number,
attesterId?: bigint | string
): Promise<boolean>

epochTreeRootExists

Determine if an epoch tree root exists for a certain epoch.

synchronizer.epochTreeRootExists(
root: bigint | string,
epoch: number
): Promise<boolean>

numStateTreeLeaves

Get the number of state tree leaves in a certain epoch.

synchronizer.numStateTreeLeaves(
epoch: number,
attesterId?: bigint | string
): Promise<number>