So, how does the Relayer actually work?
The Relayer is constantly connected to two sources of information:
- Tron blockchain, through gRPC API of its node;
- Untron Core, using ZKsync Era's JSON-RPC API.
Both sources are used by the Relayer to fetch and compute over the data in the following way:
- The Relayer fetches the latest ZK proven Tron block ID and the corresponding action chain tip from the Core contract;
- The Relayer walks to deeper blocks to reconstruct the state which print is set up in the Core;
- After reconstructing the state, the Relayer fetches all actions and Tron blocks after the latest ZK proven ones and fetches new ones;
- Every Tron block, it executes a STF with new pending actions to update its local state copy. All locally executed blocks and actions are stored in the buffer;
- If the STF execution resulted in closed orders, the Relayer passes them to the Fulfiller thread;
- The Fulfiller thread calls
calculateFulfillerTotal
view function that returns the balance in USDT on ZKsync Era it must have, passing one order back in the buffer and re-calling the function until their balance is enough (if it wasn't enough since the first call); - With the number returned by
calculateFulfillerTotal
function, the Fulfiller thread callsfulfill
function with those orders to transfer the USDT to the order creators; - After the buffer has reached the size specified in the configuration, the Relayer generates a ZK proof for the STF with the buffer as the input;
- The Relayer sends the ZK proof to the Core contract on ZKsync Era by calling
closeOrders
function; - The Core contract verifies the ZK proof and the public inputs, and if everything is correct, it marks all orders in the public inputs as closed and the Relayer repeats the process from step 3.
Warning: Even though the situation of the Relayer's account not having enough USDT to fulfill orders is handled, the situation when the Relayer's ZKsync account doesn't have enough balance to pay for the gas of the
closeOrders
andfulfill
calls is not handled and will cause things to go wrong.