Skip to content

Commit c3cf1d4

Browse files
committed
Many fixes to PoS
1 parent a384d48 commit c3cf1d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+724
-831
lines changed

docs/pos/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Auto-generated, apparently.
1+
Auto-generated, apparently.

docs/pos/architecture/bor/consensus.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ Once validators are active on Heimdall they get selected as producers through `b
1414

1515
A logically defined set of blocks for which a set of validators is chosen from among all the available validators. Heimdall provides span details through span-details APIs.
1616

17-
Each validator in span contains voting power. Based on their power, they get selected as block producers. Higher power, a higher probability of becoming block producers. Bor uses Tendermint's algorithm for the same.
17+
Each validator in span contains voting power. Based on their power, they get selected as block producers. Higher power, a higher probability of becoming block producers. Bor uses Tendermint's algorithm for the same.
1818

1919
## Sprint
2020

21-
A set of blocks within a span for which only a single block producer is chosen to produce blocks. The sprint size is a factor of span size.
21+
A set of blocks within a span for which only a single block producer is chosen to produce blocks. The sprint size is a factor of span size.
2222

2323
Apart from the current proposer, Bor also selects back-up producers.
2424

@@ -91,12 +91,12 @@ header.Extra = header.Vanity + header.ProducerBytes /* optional */ + header.Seal
9191

9292
Bor provides a mechanism where some specific events on the main Ethereum chain are relayed to Bor.
9393

94-
1. Any contract on Ethereum may call [syncState](https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol#L33) in `StateSender.sol`. This call emits `StateSynced` event: https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol#L38
94+
1. Any contract on Ethereum may call [syncState](https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol#L33) in `StateSender.sol`. This call emits `StateSynced` event: <https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol#L38>
9595

9696
```js
9797
event StateSynced(uint256 indexed id, address indexed contractAddress, bytes data)
9898
```
9999

100-
2. Heimdall listens to these events and calls `function proposeState(uint256 stateId)` in `StateReceiver.sol` - thus acting as a store for the pending state change ids. Note that the `proposeState` transaction will be processed even with a 0 gas fee as long as it is made by one of the validators in the current validator set: https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/StateReceiver.sol#L24
100+
2. Heimdall listens to these events and calls `function proposeState(uint256 stateId)` in `StateReceiver.sol` - thus acting as a store for the pending state change ids. Note that the `proposeState` transaction will be processed even with a 0 gas fee as long as it is made by one of the validators in the current validator set: <https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/StateReceiver.sol#L24>
101101

102-
3. At the start of every sprint, Bor pulls the details about the pending state changes using the states from Heimdall and commits them to the Bor state using a system call. See `commitState` here: https://github.com/maticnetwork/genesis-contracts/blob/f85d0409d2a99dff53617ad5429101d9937e3fc3/contracts/StateReceiver.sol#L41
102+
3. At the start of every sprint, Bor pulls the details about the pending state changes using the states from Heimdall and commits them to the Bor state using a system call. See `commitState` here: <https://github.com/maticnetwork/genesis-contracts/blob/f85d0409d2a99dff53617ad5429101d9937e3fc3/contracts/StateReceiver.sol#L41>

docs/pos/architecture/bor/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Bor is our Block producer layer, which, in sync with the Heimdall validator, selects the producers and verifiers for each span and sprint.
44

5-
The Bor node or the Block Producer implementation is basically the EVM-compatible blockchain operator. Currently, it is a Geth implementation with custom changes done to the consensus algorithm.
5+
The Bor node or the Block Producer implementation is basically the EVM-compatible blockchain operator. Currently, it is a Geth implementation with custom changes done to the consensus algorithm.
66

77
Polygon PoS uses dual-consensus architecture on the Polygon Network to optimise for speed and decentralisation.
88

@@ -26,5 +26,5 @@ The ratio of Stake/Staking power specifies the probability to be selected as a m
2626

2727
1. Validators are given slots proportionally according to their stake.
2828
2. Using historical Ethereum block data as seed, we shuffle this array.
29-
3. Now depending on Producer count*(maintained by validator's governance)*, validators are taken from the top.
29+
3. Now depending on Producer count*(maintained by validator's governance)*, validators are taken from the top.
3030
4. Using this validator set and Tendermint's proposer selection algorithm, we choose a producer for every sprint on Bor.

docs/pos/architecture/bor/snapshots.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
When setting up a new sentry, validator, or full node server, it is recommended that you use snapshots for faster syncing without having to sync over the network. Using snapshots will save you several days for both Heimdall and Bor. **Note: We no longer support bor archive snapshots due to unsustainable data growth.**
1+
When setting up a new sentry, validator, or full node server, it is recommended that you use snapshots for faster syncing without having to sync over the network. Using snapshots will save you several days for both Heimdall and Bor. **Note: We no longer support bor archive snapshots due to unsustainable data growth.**
22

3-
:::tip
3+
!!! tip
44

5-
For the latest snapshot, please visit [<ins>Polygon Chains Snapshots</ins>](https://snapshot.polygon.technology/).
6-
7-
:::
5+
For the latest snapshot, please visit [<ins>Polygon Chains Snapshots</ins>](https://snapshot.polygon.technology/).
86

97
## Client Snapshots
108

@@ -29,7 +27,7 @@ curl -L https://snapshot-download.polygon.technology/snapdown.sh | bash -s -- --
2927
- Consider using a Screen session to prevent accidental interruptions during the chaindata download and extraction process.
3028
- The raw bash script code is collapsed below for transparency:
3129

32-
<details>
30+
<details>
3331
<summary>View script here ↓</summary>
3432

3533
```bash
@@ -179,7 +177,7 @@ curl -L https://snapshot-download.polygon.technology/snapdown.sh | bash -s -- --
179177
aria2c -c -m 0 -x6 -s6 -i $client-$network-parts.txt --max-concurrent-downloads=1
180178
```
181179

182-
Once the extraction is complete, ensure that you update the datadir configuration of your client to point to the path where the extracted data is located. This ensures that the systemd services can correctly register the snapshot data when the client starts.
180+
Once the extraction is complete, ensure that you update the datadir configuration of your client to point to the path where the extracted data is located. This ensures that the systemd services can correctly register the snapshot data when the client starts.
183181
If you wish to preserve the default client configuration settings, you can use symbolic links (symlinks).
184182

185183
For example, let's say you have mounted your block device at `~/snapshots` and have downloaded and extracted the chaindata
@@ -212,7 +210,7 @@ sudo service bor start
212210
| approx. compressed total | 250 GB (bor) + 35 GB (heimdall) | 285 GB |
213211
| approx. data growth daily | 10 GB (bor) + .5 GB (heimdall) | 10.5 GB |
214212
| approx. total extracted size | 350 GB (bor) + 50 GB (heimdall) | 400 GB |
215-
| suggested disk size (2.5x buffer) | 400 GB * 2.5 (natural chain growth) | 1 TB |
213+
| suggested disk size (2.5x buffer) | 400 GB * 2.5 (natural chain growth) | 1 TB |
216214

217215
**Polygon Mainnet**
218216

@@ -230,7 +228,7 @@ sudo service bor start
230228
| approx. compressed total | 210 GB (erigon) + 35 GB (heimdall) | 245 GB |
231229
| approx. data growth daily | 4.5 GB (erigon) + .5 GB (heimdall) | 5 GB |
232230
| approx. total extracted size | 875 GB (erigon) + 50 GB (heimdall) | 925 GB |
233-
| suggested disk size (2.5x buffer) | 925 GB * 2.5 (natural chain growth) | 2.5 TB |
231+
| suggested disk size (2.5x buffer) | 925 GB * 2.5 (natural chain growth) | 2.5 TB |
234232

235233
**NOTE**
236234
PoS Network is deprecating Archive Node snapshots we request users to move to the Erigon Client and make use of Erigon Snapshots.

docs/pos/architecture/bor/state-sync.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
`State Sync` is the native mechanism to read Ethereum data from Matic EVM chain.
1+
`State Sync` is the native mechanism to read Ethereum data from Matic EVM chain.
22

3-
Validators on the Heimdall layer pickup the [StateSynced](https://github.com/maticnetwork/contracts/blob/a4c26d59ca6e842af2b8d2265be1da15189e29a4/contracts/root/stateSyncer/StateSender.sol#L24) event and pass it on to the Bor layer.
3+
Validators on the Heimdall layer pickup the [StateSynced](https://github.com/maticnetwork/contracts/blob/a4c26d59ca6e842af2b8d2265be1da15189e29a4/contracts/root/stateSyncer/StateSender.sol#L24) event and pass it on to the Bor layer.
44

55
The receiver contract inherits [IStateReceiver](https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/IStateReceiver.sol), and custom logic sits inside [onStateReceive](https://github.com/maticnetwork/genesis-contracts/blob/05556cfd91a6879a8190a6828428f50e4912ee1a/contracts/IStateReceiver.sol#L5) function.
66

7-
87
This is the flow required from dapps / users to work with state-sync:
98

109
1. Call the smart contract function present here: [https://github.com/maticnetwork/contracts/blob/19163ddecf91db17333859ae72dd73c91bee6191/contracts/root/stateSyncer/StateSender.sol#L33](https://github.com/maticnetwork/contracts/blob/19163ddecf91db17333859ae72dd73c91bee6191/contracts/root/stateSyncer/StateSender.sol#L33)
@@ -14,30 +13,29 @@ This is the flow required from dapps / users to work with state-sync:
1413
5. After every sprint on `bor`, the Bor node fetches the pending state-sync events from Heimdall via an API call.
1514
6. The receiver contract inherits `IStateReceiver` interface, and custom logic of decoding the data bytes and performing any action sits inside `onStateReceive` function: [https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/IStateReceiver.sol](https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/IStateReceiver.sol)
1615

17-
18-
# How does State Sync work?
16+
## How does State Sync work?
1917

2018
State management sends the state from the Ethereum chain to the Bor chain. It is called **state-sync**.
2119

22-
State transfer from Ethereum to Bor happens through system call. Suppose, a user deposits USDC to the deposit manager on Ethereum. Validators listen to those events, validate, and store them in Heimdall state. Bor gets the latest state-sync records and updates the Bor state (mints equal amount of USDC on Bor) using a system call.
20+
State transfer from Ethereum to Bor happens through system call. Suppose, a user deposits USDC to the deposit manager on Ethereum. Validators listen to those events, validate, and store them in Heimdall state. Bor gets the latest state-sync records and updates the Bor state (mints equal amount of USDC on Bor) using a system call.
2321

2422
## State sender
2523

2624
Source: [https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol](https://github.com/maticnetwork/contracts/blob/develop/contracts/root/stateSyncer/StateSender.sol)
2725

28-
To sync state, the contract calls following method **state sender contract** on Ethereum chain.
26+
To sync state, the contract calls following method **state sender contract** on Ethereum chain.
2927

3028
```jsx
3129
contract StateSender {
32-
/**
33-
* Emits `stateSynced` events to start sync process on Ethereum chain
34-
* @param receiver Target contract on Bor chain
35-
* @param data Data to send
36-
*/
37-
function syncState (
38-
address receiver,
39-
bytes calldata data
40-
) external;
30+
/**
31+
* Emits `stateSynced` events to start sync process on Ethereum chain
32+
* @param receiver Target contract on Bor chain
33+
* @param data Data to send
34+
*/
35+
function syncState (
36+
address receiver,
37+
bytes calldata data
38+
) external;
4139
}
4240
```
4341
@@ -51,9 +49,9 @@ contract StateSender {
5149
* @param data Data to send to Bor chain for Target contract address
5250
*/
5351
event StateSynced (
54-
uint256 indexed id,
55-
address indexed contractAddress,
56-
bytes data
52+
uint256 indexed id,
53+
address indexed contractAddress,
54+
bytes data
5755
);
5856
```
5957
@@ -92,4 +90,4 @@ Bor produces a new tx / receipt just for the client which includes all the logs
9290
keccak256("matic-bor-receipt-" + block number + block hash)
9391
```
9492
95-
This doesn't change any consensus logic, only client changes. `eth_getBlockByNumber`, `eth_getTransactionReceipt`, and `eth_getLogs` include state-sync logs with derived. Note that the bloom filter on the block doesn't include inclusion for state-sync logs. It also doesn't include derived tx in `transactionRoot` or `receiptRoot`.
93+
This doesn't change any consensus logic, only client changes. `eth_getBlockByNumber`, `eth_getTransactionReceipt`, and `eth_getLogs` include state-sync logs with derived. Note that the bloom filter on the block doesn't include inclusion for state-sync logs. It also doesn't include derived tx in `transactionRoot` or `receiptRoot`.

docs/pos/architecture/heimdall/checkpoint.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ Checkpoint structure on Heimdall state looks like following:
88

99
```go
1010
type CheckpointBlockHeader struct {
11-
// Proposer is selected based on stake
12-
Proposer types.HeimdallAddress `json:"proposer"`
11+
// Proposer is selected based on stake
12+
Proposer types.HeimdallAddress `json:"proposer"`
1313

14-
// StartBlock: The block number on Bor from which this checkpoint starts
15-
StartBlock uint64 `json:"startBlock"`
14+
// StartBlock: The block number on Bor from which this checkpoint starts
15+
StartBlock uint64 `json:"startBlock"`
1616

17-
// EndBlock: The block number on Bor from which this checkpoint ends
18-
EndBlock uint64 `json:"endBlock"`
17+
// EndBlock: The block number on Bor from which this checkpoint ends
18+
EndBlock uint64 `json:"endBlock"`
1919

20-
// RootHash is the Merkle root of all the leaves containing the block
21-
// headers starting from start to the end block
22-
RootHash types.HeimdallHash `json:"rootHash"`
20+
// RootHash is the Merkle root of all the leaves containing the block
21+
// headers starting from start to the end block
22+
RootHash types.HeimdallHash `json:"rootHash"`
2323

24-
// Account root hash for each validator
24+
// Account root hash for each validator
2525
// Hash of data that needs to be passed from Heimdall to Ethereum chain like withdraw topup etc.
26-
AccountRootHash types.HeimdallHash `json:"accountRootHash"`
26+
AccountRootHash types.HeimdallHash `json:"accountRootHash"`
2727

2828
// Timestamp when checkpoint was created on Heimdall
29-
TimeStamp uint64 `json:"timestamp"`
29+
TimeStamp uint64 `json:"timestamp"`
3030
}
3131
```
3232

@@ -61,10 +61,10 @@ Source: [https://github.com/maticnetwork/heimdall/blob/develop/checkpoint/types/
6161
```go
6262
// Golang representation of block data used in checkpoint
6363
blockData := crypto.Keccak256(appendBytes32(
64-
blockHeader.Number.Bytes(),
65-
new(big.Int).SetUint64(blockHeader.Time).Bytes(),
66-
blockHeader.TxHash.Bytes(),
67-
blockHeader.ReceiptHash.Bytes(),
64+
blockHeader.Number.Bytes(),
65+
new(big.Int).SetUint64(blockHeader.Time).Bytes(),
66+
blockHeader.TxHash.Bytes(),
67+
blockHeader.ReceiptHash.Bytes(),
6868
))
6969
7070
// array of block hashes of Bor blocks
@@ -105,21 +105,21 @@ Golang code for the account hash can be found here: [https://github.com/maticnet
105105
```go
106106
// DividendAccount contains Fee, Slashed amount
107107
type DividendAccount struct {
108-
ID DividendAccountID `json:"ID"`
109-
FeeAmount string `json:"feeAmount"` // string representation of big.Int
110-
SlashedAmount string `json:"slashedAmount"` // string representation of big.Int
108+
ID DividendAccountID `json:"ID"`
109+
FeeAmount string `json:"feeAmount"` // string representation of big.Int
110+
SlashedAmount string `json:"slashedAmount"` // string representation of big.Int
111111
}
112112
113113
// calculate hash for particular account
114114
func (da DividendAccount) CalculateHash() ([]byte, error) {
115-
fee, _ := big.NewInt(0).SetString(da.FeeAmount, 10)
116-
slashAmount, _ := big.NewInt(0).SetString(da.SlashedAmount, 10)
117-
divAccountHash := crypto.Keccak256(appendBytes32(
118-
new(big.Int).SetUint64(uint64(da.ID)).Bytes(),
119-
fee.Bytes(),
120-
slashAmount.Bytes(),
121-
))
122-
123-
return divAccountHash, nil
115+
fee, _ := big.NewInt(0).SetString(da.FeeAmount, 10)
116+
slashAmount, _ := big.NewInt(0).SetString(da.SlashedAmount, 10)
117+
divAccountHash := crypto.Keccak256(appendBytes32(
118+
new(big.Int).SetUint64(uint64(da.ID)).Bytes(),
119+
fee.Bytes(),
120+
slashAmount.Bytes(),
121+
))
122+
123+
return divAccountHash, nil
124124
}
125125
```

0 commit comments

Comments
 (0)