Jarrod Watts Profile picture
Aug 11 28 tweets 9 min read Twitter logo Read on Twitter
We all know Layer 2s batch transactions together and send a single transaction back to Ethereum.

But what *actually* gets posted to L1?

I followed a transaction all the way from being submitted on Polygon zkEVM through to being part of a ZK proof on L1.

Here's what I learned:
The full flow of transactions can be broken down into 5 steps:

1/ Submitting
2/ Executing
3/ Batching
4/ Sequencing
5/ Aggregating

(1/25)
1/ Submitting

On the zkEVM, transactions go through almost instantly.

I built a demo application last week showcasing how quickly the state is updated from the user's POV on the Polygon zkEVM.

(2/25)
From the user's perspective, interacting with the zkEVM looks and feels exactly the same as Ethereum with cheaper fees and faster transaction speeds.

Users submit transactions via a JSON-RPC interface (typically through wallets like 🦊) where they go into a pending pool.

(3/25)
2/ Executing

After being submitted, the transactions are stored in a pending transactions pool, where they await the sequencer's selection for either execution or discard based on a few validity checks.

Once executed, the L2 state is updated almost instantly.

(4/25) Image
Under the hood, the sequencer is executing or discarding each transaction, and broadcasting this information to each zkEVM node.

For every transaction except bridging to L1, this is all the user needs to care about; the transaction is executed & the state is updated.

(5/25) Image
However, as we explored in this previous thread, bridging funds back to L1 requires those batches of transactions to be proved using ZK-proofs like a ZK SNARK on Ethereum.

This is where the next three steps (batching, sequencing, aggregating) come into play.

(6/25)
3/ Batching

To batch transactions together, they're concatenated into one bytes value.

On the L1 PolygonZkEVM smart contract, a "BatchData" struct is defined, containing a "transactions" field of type bytes.

(7/25) Image
The transactions are encoded using RLP (Recursive-Length Prefix) serialization and can be either EIP-155 (Simple replay attack protection) or pre-EIP-155 transactions.

The other properties contain the time the batch was created and a Merkle tree about L1-L2 transfers.

(8/25)
Each transaction goes through RLP serialization and is converted into bytes which are then concatenated together into the transactions field of the BatchData struct, along with the other metadata properties.

Multiple of these batches gets created for one transaction.

(9/25) Image
4/ Sequencing

Once these batches are created, they're ready to be sent to L1 for sequencing.

The PolygonZkEVM smart contract has a function called "sequenceBatches", which is called with the batches we just saw created as an argument.

(10/25)
Let's inspect a real transaction as an example.

In the screenshot below, we can see the sequencer calling the PolygonZkEVM smart contract on Ethereum L1, by providing the batches it prepared as an argument to the "sequenceBatches" function.

(11/25) Image
This particular transaction provided the function with 52 batches of transactions as we can see in the input data.

The max number of batches is actually defined in the contract, currently set at 1000 in the _MAX_VERIFY_BATCHES constant.

(12/25) Image
Each batch provided also contains the concatenated, RLP-serialized transactions in the transactions field of the BatchData struct.

The maximum it can take is also defined in the contract, currently set at 120000 in the _MAX_TRANSACTIONS_BYTE_LENGTH constant.

(13/25) Image
The sequenceBatches function iterates over each batch and ensures they are valid, before updating the virtual state on the L1 smart contract inside a mapping called "sequencedBatches".

(14/25) Image
The batches are now sequenced and the transactions are said to be in the "virtual state".

We'll explore what that means shortly, but here's a quick update to our diagram to see what the trusted sequencer is doing under the hood.

(15/25) Image
In the zkEVM, there are three (or four, if you include the pending state) states a transaction can be in:

1/ Trusted: State is updated on L2.
2/ Virtual: Batches are sequenced on L1.
3/ Consolidated: ZK Proof is posted on L1.

(16/25) Image
So far we've explored every step of the process up until the batches have been sequenced on L1 by the PolygonZkEVM smart contract. So we're currently in a virtual state.

But in order to bridge funds back to L1, a validity proof needs to be posted on L1 first.

(17/25)
5/ Aggregating

Finally, the trusted aggregator takes the sequenced batches and provides them to the zkProver to generate a ZK proof in the form of a ZK SNARK.

We explored this in another previous thread in more detail:

(18/25)
Using the FFLONK protocol, the specific trusted setup drawback that typically comes with SNARKs is avoided.

More details from Jordi are below:

(19/25)
The end result is the aggregator receives a ZK proof that is succinct enough that it can be stored on the Ethereum L1. A simplified diagram of this flow is below:

(20/25) Image
Once the aggregator has the proof, it calls another function on the PolygonZkEVM smart contract called "verifyBatchesTrustedAggregator", providing the proof it just received as an argument, among other parameters.

(21/25) Image
We can inspect another real transaction on Ethereum to see this in action.

Below, we can see the trusted aggregator calling the PolygonZkEVM Smart contract on Ethereum, providing the ZK proof as an argument to the "verifyBatchesTrustedAggregator" function:

(22/25) Image
Within this function, another smart contract, "rollupVerifier", has a function called "verifyProof" that gets called.

It is provided with the proof and an inputSnark; which is a cryptographic representation of all the L2 transactions of a specific L2 State transition.

(23/25) Image
Finally, the state of the L1 smart contract is updated again.

If the proof is valid, various states are updated like the global exit root and the "batchNumToStateRoot" mapping containing the consolidated L2 state roots:

We've now reached the final consolidated state!

(24/25) Image
In this particular example, the posting and verification of this ZK proof cost around ~350K gas:

(25/25) Image
I've also posted the full blog post covering this in more detail, linked below:

https://t.co/eGsEb0hOpTblog.jarrodwatts.com/how-polygon-zk…
Image
That's everything I learned.

If you enjoyed this kind of technical content:
- Sharing the thread helps me out a lot with engagement
- Follow me for more threads!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Jarrod Watts

Jarrod Watts Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @jarrodWattsDev

Jul 17
I spent the last week building a dApp on every ZK-EVM.

I tested out Polygon, Linea, Scroll, Taiko, Era and more, documenting every step of the process.

Here's everything I learned:
ZK-EVMs are all aiming to achieve the same goal; to make Ethereum more scalable and grow the overall web3 space.

If you don't know what a ZK-EVM is, they batch transactions on "L2" (layer 2) together and post a "validity proof" of that batch back to Ethereum "L1".

(1/50)
In the past, these solutions, known as ZK rollups were not EVM (Ethereum Virtual Machine) compatible.

This usually meant they were only capable of performing a set list of operations like transfer, mint, or burn, and also required new tools like new wallets for users.

(2/50)
Read 52 tweets
Jun 29
Zero-knowledge proofs are powering the next wave of innovation in web3.

I spent the past week reading hundreds of resources about:
◆ How ZK proofs work
◆ ZK-SNARKs & ZK-STARKs
◆ zkEVMs and Ethereum's ZK rollups

Here's everything I learnt 🧵:
To start off, what is the goal of a ZK proof?

In one sentence: ZK proofs allow you to prove something without revealing the thing itself.

This has several real-world applications, such as verifying your age without providing your full license/passport information.

(1/39)
But how does this actually work?

To prove the validity of a statement without revealing the statement itself, there are two parties involved:

1. Prover: the person trying to prove something.
2. Verifier: the person trying to validate the claim is "real".

(2/39)
Read 42 tweets
Jun 22
Polygon has made a proposal to change its PoS chain to become a zkEVM validium.

◆ WTF is a validium?
◆ What's this mean for Polygon?
◆ How's this different from the zkEVM Rollup?

Let's explain it in simple terms 👇
Over the past few years, we've seen many different solutions come out to address the same problem: Ethereum's scalability.

We've seen sidechains, rollups, plasma chains, and more to enable users to more easily experience web3 by creating lower fees and higher throughput.

(1/17)
Polygon PoS (proof-of-stake) was one of the first of these solutions, and still, today is the most popular.

Top brands like Nike & Reddit have utilized Polygon PoS to power their web3 initiatives.

So, what's changing, and why?

(2/17)
Read 20 tweets
Jun 22
Is Fortnite doing NFTs? 🧐

Fortnite just dropped 2 skins, 7 items and an island called "Airphoria" in partnership with Nike and @dotSWOOSH.

How do NFTs fit into this though?

..🧵
To answer the original question: the skins and in-game items are not NFTs.

Fortnite already has an established in-game item shop and in-game currency, it would be a massive investment for them to rework this to include on-chain skins.

(1/9)
However, Epic Games has added the option to link your Epic account with your .swoosh account. Granting access to in-game and account-level information to .swoosh as a third party.

(although it's only for US users, so I can't see it)

(2/9)
Read 11 tweets
May 9
I just fixed web3 onboarding...

I created an app that has:
◆ No wallet requirement
◆ No transaction approvals
◆ No gas fees

Powered by the new EIP-4337 account abstraction.

Here's how:
I strongly believe that wallets, as we know them today, will be heavily abstracted away from users in the near future.

The barrier to entry when it comes to interacting with web3 apps is insanely high, involving KYC, purchasing & transferring of funds, gas fees, approvals, etc.
The way we resolve this as devs is by absorbing these complexities for our users under the hood. EIP-4337 account abstraction helps us do this.

What we can do:
1. Generate wallets for users
2. Cover the gas fees for transactions
3. "Hide" web3 technicalities from users
Read 11 tweets
Jul 1, 2022
Want to make a play-to-earn crypto game?

I built an open-source repo you can use, where you:

- Mint an NFT "Miner" Character 👷
- Buy a "Pickaxe" NFT from the shop ⛏️
- Start mining to earn ERC-20 "Gold Gem" tokens 💎

🧵👇 Mega thread of resources and links below!
Firstly, our NFT character

Our miners are ERC-1555 NFT tokens, meaning they're "semi-fungible".

This means that we can have multiple "copies" of the same NFT!

I used @thirdweb_'s Edition Drop contract to lazy-mint the character NFT and allow users to mint them for some MATIC! Image
@thirdweb_ I charged 0.0001 MATIC to mint this character.

I also restricted each wallet to only being able to mint 1 of these characters.

On a mainnet environment, I'd probably make these characters cost a bit more, and potentially limit the total supply to give more value to players. Image
Read 30 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(