@@ -17,44 +17,60 @@ To get the finalized block, you can use the following JSON-RPC call:
1717 "jsonrpc": "2.0"
1818}
1919```
20+ ## Using the Milestone API
21+
22+ Here's a simple code example to check if a transaction has reached finality
23+ using the milestone mechanism.
24+
25+ ``` ts
26+ async function milestones_checkFinality(client : any , txHash : string ): Promise <boolean > {
27+ const tx = await client .getTransaction ({ hash: ` 0x${txHash } ` })
28+ if (! tx || ! tx .blockNumber ) return false
29+ const latestBlock: Block = await client .getBlock ({ blockTag: ' finalized' })
30+
31+ console .log (` Latest finalized block: ${latestBlock .number } ` )
32+ console .log (` Your transaction block: ${tx .blockNumber } ` )
33+
34+ // Checking whether the finalized block number via milestones has reached the transaction block number.
35+ if (latestBlock .number !== null && latestBlock .number > tx .blockNumber ) {
36+ console .log (" Your transaction block has been confirmed after 16 blocks" );
37+ return true
38+ } else {
39+ return false
40+ }
41+ }
42+ ```
2043
21- ## Introduction to Bor and Heimdall in Polygon PoS
44+ ### Running the Code Locally
2245
23- ### What is Bor?
46+ - Step 1: Copy the code into a file named ` milestones.ts ` .
2447
25- Bor is the execution layer in the Polygon Proof-of-Stake (PoS) network. It is
26- responsible for:
48+ - Step 2: Install the required dependencies by running:
2749
28- - Aggregating transactions into blocks.
29- - Managing the execution of smart contracts.
30- - Maintaining the state of the Polygon network, such as account balances.
50+ ``` bash
51+ npm install
52+ ```
3153
32- ### What is Heimdall?
54+ - Step 3: Run the code using Node.js with the required command-line arguments:
3355
34- Heimdall acts as the proof of stake layer and uses Tendermint BFT consensus. It
35- decides which validators should be producing blocks in Bor in each span (based
36- on their stake). It also:
56+ ``` bash
57+ npx ts-node milestones.ts --txHash < transaction_hash > --function < function_name > --network < network_name >
58+ ```
3759
38- - Submits checkpoints to the Ethereum mainnet, securing the Polygon chain.
39- - Helps in arriving at finality of Bor blocks using Milestones.
40- - Plays a key role in state sync mechanism from L1 to Bor .
60+ Replace <transaction_hash> with the actual transaction hash, <function_name>
61+ with either pre_milestones or milestones, and <network_name> with either
62+ polygon or amoy .
4163
42- ### How Do They Work Together?
64+ - Step 4: Observe the output to determine if your transaction has been finalized
65+ based on the selected milestone mechanism and network.
4366
44- Heimdall determines the validators that should be part of the next span. Bor
45- fetches these details from Heimdall and the selected validators start producing
46- blocks based on the consensus rules. Heimdall also periodically aggregates Bor
47- blocks and submits checkpoints to L1 (Ethereum) to secure the network.
67+ ### Results
4868
49- Heimdall is also responsible for the finality of blocks produced by Bor which is
50- achieved through a mechanism called Milestones. And we will be diving deeper
51- into Milestones in this tutorial .
69+ The results should show whether the transaction has been finalized based on the
70+ selected milestone mechanism and network. Usually Milestones will take 3-5
71+ seconds to finalize the transaction .
5272
53- ![ Bor and Heimdall] ( ../../../img/pos/milestones_01.png )
5473
55- For more on Bor and Heimdall check out the official
56- [ documentation] ( https://docs.polygon.technology/pos/architecture/#architectural-overview )
57- for more details on Bor and Heimdall.
5874
5975## The Evolution of Finality: Before and After Milestones
6076
@@ -102,117 +118,5 @@ With the introduction of milestones:
102118![ Finality After Milestones] ( ../../../img/pos/milestones_03.png )
103119
104120_ Finality achieved after at least 12 blocks confirmation and 4 blocks of buffer,
105- as well as a consensus period among the validators (approx. 1-2 minute)_
106-
107- ## Using the Milestone API
108-
109- Here's a simple code example to check if a transaction has reached finality
110- using the milestone mechanism.
111-
112- ``` ts
113- // Import Relevant Libraries
114- import { createPublicClient , http , Hash } from ' viem'
115- import { polygon , polygonAmoy } from ' viem/chains'
116- import { program } from ' commander'
117- ```
118-
119- Here's the implementation of Checking Transaction Finality BEFORE Milestones Implementation:
120-
121- ``` ts
122- async function pre_milestones_checkFinality(client : any , txHash : string ): Promise <boolean > {
123- const tx = await client .getTransaction ({ hash: ` 0x${txHash } ` })
124- if (! tx || ! tx .blockNumber ) return false
125- const latestBlock: Block = await client .getBlock ({ blockTag: ' finalized' })
126-
127- console .log (` Latest finalized block: ${latestBlock .number } ` )
128- console .log (` Your transaction block: ${tx .blockNumber } ` )
129-
130- // Checking whether there has been 256 blocks since the transaction was included in a block
131- if (latestBlock .number !== null && latestBlock .number - tx .blockNumber >= 256 ) {
132- console .log (" Your transaction block has been confirmed after 256 blocks" );
133- return true
134- } else {
135- return false
136- }
137- }
138- ```
139-
140- Here's the implementation of Checking Transaction Finality AFTER Milestones Implementation:
141-
142- ``` ts
143- async function milestones_checkFinality(client : any , txHash : string ): Promise <boolean > {
144- const tx = await client .getTransaction ({ hash: ` 0x${txHash } ` })
145- if (! tx || ! tx .blockNumber ) return false
146- const latestBlock: Block = await client .getBlock ({ blockTag: ' finalized' })
147-
148- console .log (` Latest finalized block: ${latestBlock .number } ` )
149- console .log (` Your transaction block: ${tx .blockNumber } ` )
150-
151- // Checking whether the finalized block number via milestones has reached the transaction block number.
152- if (latestBlock .number !== null && latestBlock .number > tx .blockNumber ) {
153- console .log (" Your transaction block has been confirmed after 16 blocks" );
154- return true
155- } else {
156- return false
157- }
158- }
159- ```
160-
161- > Please note that this is just a demo purpose to show the previous
162- > implementations, since Milestones has already been implemented in the
163- > protocol, therefore, 16 blocks is the minimum time for finality, the
164- > ` pre_milestones_checkFinality ` function is not needed anymore in actual
165- > implementation. Just use the ` milestones_checkFinality ` function to check your
166- > transaction finality.
167-
168- ### Running the Code Locally
169-
170- - Step 1: Copy the code into a file named ` milestones.ts ` .
171-
172- - Step 2: Install the required dependencies by running:
173-
174- ``` bash
175- npm install
176- ```
177-
178- - Step 3: Run the code using Node.js with the required command-line arguments:
179-
180- ``` bash
181- npx ts-node milestones.ts --txHash < transaction_hash> --function < function_name> --network < network_name>
182- ```
183-
184- Replace <transaction_hash> with the actual transaction hash, <function_name>
185- with either pre_milestones or milestones, and <network_name> with either
186- polygon or amoy.
187-
188- - Step 4: Observe the output to determine if your transaction has been finalized
189- based on the selected milestone mechanism and network.
190-
191- ### Results
192-
193- The results should show whether the transaction has been finalized based on the
194- selected milestone mechanism and network. Usually Milestones will take 1-2
195- minutes to finalize the transaction. Result as follows:
196-
197- ![ milestones_result] ( ../../../img/pos/milestones_04.png )
198-
199- Here's a screenshot of the ` pre_milestones_checkFinality ` function, where it
200- shows that the new blocks are not yet 256:
201-
202- ![ pre_milestones_result] ( ../../../img/pos/milestones_05.png )
203-
204- Here's a screenshot of the ` pre_milestones_checkFinality ` function, where it
205- shows that the new blocks are 256:
206-
207- ![ pre_milestones_finalized] ( ../../../img/pos/milestones_06.png )
208-
209- ### Experimenting Further
210-
211- Modify the code to check different transactions and networks to see how finality
212- is achieved with milestones on various Polygon networks.
213-
214- ## Resources/References
121+ as well as a consensus period among the validators (approx. 3-5 seconds)_
215122
216- - [ Polygon PoS Documentation] ( https://docs.polygon.technology/pos/overview )
217- - [ Polygon PoS Faster Finality Announcement] ( https://polygon.technology/blog/faster-finality-with-the-aalborg-upgrade-for-polygon-proof-of-stake-network )
218- - [ PIP-11: Deterministic finality via Milestones] ( https://forum.polygon.technology/t/pip-11-deterministic-finality-via-milestones/11918 )
0 commit comments