SNARK proofs are the backbone of the Mina blockchain and are used for verifying the validity of transactions, blocks and other SNARKs. We want to optimize the production of SNARKs so that the Mina blockchain can continue operating and expanding.
This is an overview of SNARK workflows. Click on the picture for a higher resolution:
Since blocks contain both transactions and SNARKs, each new block updates not only the staged ledger, but also the scan state (which contains SNARK proofs).
Via the GossipSub (P2P), a node receives a new block that contains transactions and SNARK proofs.
The work pool, which is a part of the modified SNARK pool and which contains the staged ledger and the scan state, is updated. The staged ledger includes the new blocks. The scan state is updated with the new jobs.
We want to avoid wasting time and resources in SNARK generation, specifically, we want to prevent Snarkers from working on the same pending snark job. For that purpose, we have introduced the notion of a commitment, in which SNARK workers commit to generating a proof for a pending SNARK job.
This is a message made by SNARK workers that informs other peers in the network that they are committing to generating a proof for a pending SNARK job, so that other SNARK workers do not perform the same task and can instead make commitments to other SNARK jobs.
Commitments are made through an extra P2P layer that was created for this purpose.
Commitments are sent across WebRTC, which enables direct communication between peers via the P2P network.
Valid commitments are added to the commitment pool.
For a commitment to be added here, it has to:
- Be made for a SNARK job that is still marked as not yet completed in the scan state
- Have no other prior commitment to that job. Alternatively, if there are other commitments, then only the one with the cheapest fee will be added.
The work pool, which is a part of the modified SNARK pool, is updated with a commitment (including its fee) for a specific pending SNARK job.
The commitments, once added to the commitment pool, are then broadcasted by the node other peers in the network through direct WebRTC P2P communication.
The Rust node receives a SNARK proof from an OCaml node (an OCaml SNARK worker).
The SNARK is verified.
If it is the lowest fee SNARK for a specific pending SNARK job, then it is added to the SNARK pool, from where block producers can take SNARKs and add them into blocks.
If it is the lowest fee SNARK for that job, then it is added to the SNARK pool
After this, the updated SNARK pool with the completed (but not yet included in a block) SNARK is broadcast across the PubSub P2P network via the topic mina/snark-work/1.0.0
(SNARK pool diff) and directly to other nodes via WebRTC.
Rust node sends SNARK via P2P.
SNARK is verified.
If it is the lowest fee, it will be added to the SNARK pool.
Once committed to a pending SNARK job, a SNARK worker will then produce a SNARK.
If a commitment is for a SNARK job that is marked as not yet completed in the scan state and there are no prior commitments to that job (Alternatively, if there are other commitments, then it is the commitment with the cheapest fee for the SNARK work), it is added to the SNARK pool.
From the SNARK pool, it can be committed to one of the following:
- An available job that hasn’t been completed or included in a block
- A job that has been already performed, but the new commitment has a lower fee
If the commitment is for the lowest fee available, then the SNARK worker begins working on the SNARK proof, which is performed in OCaml. After it is done, the generated SNARK is sent back to the SNARK worker (Rust).
A SNARK worker starts working on the committed job. The SNARK proof that is generated is then checked by a prover in OCaml, after which it is sent back to the SNARK worker.
The SNARK proof is then sent to the SNARK pool.
From here, it is broadcast to Rust nodes directly via WebRTC P2P, and to OCaml nodes indirectly via the mina/snark-work/1.0.0
(SNARK pool diff) topic of the PubSub P2P network.