Jarrod Watts Profile picture
Aug 11, 2023 28 tweets 9 min read Read on X
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

Dec 10, 2024
AI Agents have now reached over $10 billion in market cap.

So, are AI agents the next big thing, or just another way to pump tokens?

To find out, I spent the past few weeks learning about how AI agents work under the hood.

Here’s what I learned:
Initially, I was very sceptical about AI “agents”.

“Sure bro, an AI decided to launch a token and start shitposting about it on Twitter…”

I dismissed it as another short-term narrative built to pump tokens.

However, the tokens & tweets are a small part of AI agents.
Let's start with the tokens since that's why most of you are here.

AI Agent tokens typically fall into two categories:

· Governance tokens that let holders influence the project's direction (ai16z)

· "Attention" tokens - essentially ways to speculate on an agent's popularity
Read 16 tweets
Nov 22, 2024
Ethereum block production is extremely centralized.

95% of Ethereum's blocks are made by 2 entities, leading to the potential for transaction censorship.

EIP-7805 introduces FOCIL, a way to preserve Ethereum’s censorship resistance.

Here’s how it works: 🧵 Image
Justin Drake's Beam Chain proposal includes 9 major upgrades.

Upgrade #1 is to improve censorship resistance.

But why? Isn't Ethereum meant to be decentralized?

Let's quickly cover how the block-building process has evolved to where it is today. Image
By staking 32 ETH to run an Ethereum validator, occasionally, you get to propose the next block in the chain and get rewarded.

In theory, this means block creation would be decentralized amongst the validators.

So, how do 95% of Ethereum blocks get made by 2 entities...? Image
Read 13 tweets
Nov 19, 2024
Beam Chain was the biggest announcement at Devcon, introducing 9 major upgrades for Ethereum.

But most people still don’t understand them...

So, here are 9 tweets to explain the 9 upgrades: 🧵 Image
Beam Chain is Justin Drake's proposal to redesign Ethereum's consensus layer (CL).

Before we dive into the specific upgrades, I'll briefly overview what the CL actually is.

Each Ethereum node runs two components:
1/ Execution Client
2/ Consensus Client Image
The consensus client is what nodes use to “agree” on what the next block in the blockchain is.

You can stake 32 ETH to add a third component, a “validator”, to earn rewards for proposing new blocks via the proof-of-stake algorithm. Image
Read 17 tweets
Oct 22, 2024
Abstract just announced Panoramic Governance.

It is a proposed way to reward both the users and app developers bringing the most value to the chain.

Here's how it works (in simple terms) 🧵 Image
Panoramic Governance (PG) consists of two parts:

1. Activity Driven Loop
2. Emissions Driven Loop

(1) Incentivizes users for their active participation.
(2) Provides rewards for the best app builders.

(1/12)
Before we dive into these two parts, we need to first understand how L2s work today.

A critical component of each L2 is the sequencer.

The sequencer is responsible for both:
- Processing all L2 transactions.
- Posting transaction batches to Ethereum L1.

(2/12) Image
Read 13 tweets
Aug 16, 2024
Account abstraction is finally gaining the traction it deserves over the past few months.

So, why not build it natively into the protocol?

That's exactly what @AbstractChain is doing... and I spent the past week learning about it.

Here's exactly how it works (a thread): Image
As a quick recap, account abstraction enables the use of smart contracts as wallets (as opposed to EOAs).

It provides a standardized way for smart contracts to act as a wallet and handle transactions.

EOA: public/private key pair.
Smart Contract: any valid EVM code.

(2/24)
The way it works on Ethereum is by having two separate mempools (waiting areas) for transactions.

These are:
1. Regular transactions submitted via EOAs.
2. UserOperations (a new type of pseudo-transaction that smart contract wallets can handle).

(3/24) Image
Read 24 tweets
May 8, 2024
Vitalik just proposed EIP-7702.

It's one of the most impactful changes Ethereum is going to have... EVER.

So, here's everything you need to know about how it works and how we got here:
The new EIP-7702 proposal is surprisingly short! Leaving some people confused as to how this is going to work.

To understand 7702, there are first three other proposals that it mentions that we'll walk through:
1/ EIP-4337
2/ EIP-3074
3/ EIP-5003

(1/23)
Let's start with the goal of *all* of these proposals.

EOAs ("normal" accounts) on Ethereum suck. They're risky and have very limited capabilities.

Account abstraction allows you to use a smart contract as your account to add more features & safety to address this.

(2/23) Image
Read 26 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!

:(