Skip to content

Commit b0335fa

Browse files
Ipsa11karanvir12pushkar1262
authored
Feat/offline election prediction tool (#1189)
* feat: initialize offline election prediction tool structure * chore:added prediction types * chore: add module for fetching and preprocessing election data * chore: add command-line interface for offline election prediction tool * chore: implement core prediction logic * chore: run election predictions and generate output files * test: add unit and integration tests for election prediction tool * chore:code restructured * chore:added readme * fix:fixed snapshot fetching logic * chore: Implemented suggested changes and improvements * fix(predict): custom file handling logic * chore:docker image updated * chore: apply minor codebase improvements and cleanup * chore:docker file updated * chore:rust version updated * chore:used mine_solution for prediction * fix:updated rust version * fix: predict.rs clippy workflow * chore: added generate_changelog.sh file * fix: readme and get_chain_properties * chore:docker file updated * fix(predict): used bags and voterlist for fetching staking data * fix: restored dockerfile and removed dockerignore * Changed permissions of generate_changelog.sh * feat: added cli options for do_reduce and block number * chore: updated README * fix(predict): fixed voter data fetch and ordering * chore: added number of nominators per validator and the individual nominators with their allocated stake * refactor: fixed custom data file reading and some refactoring * refactor: simplify backend initialization in Client * test(utils): add unit tests and misc cleanups * fix: fixed snapshot creation * fixed empty validator prediction * Added overrides option| removed custom-data option * Added support for PhragMMS * fix: address breaking changes from client.rs updates * improve logging hierarchy and used dynamic solver for kusama * implemented separate clients for monitor and predict | some refactoring --------- Co-authored-by: karanvir12 <[email protected]> Co-authored-by: pushkar1262 <[email protected]>
1 parent bf41a2d commit b0335fa

File tree

22 files changed

+2385
-68
lines changed

22 files changed

+2385
-68
lines changed

Cargo.lock

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pin-project-lite = "0.2"
3333
scale-value = "0.18"
3434
subxt = { version = "0.44.2", features = ["reconnecting-rpc-client"] }
3535
subxt-rpcs = "0.44.0"
36-
36+
ss58-registry = "1.9"
3737

3838
# polkadot-sdk
3939
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", features = [
@@ -61,10 +61,11 @@ once_cell = "1.21"
6161
anyhow = "1"
6262
assert_cmd = "2.1"
6363
regex = "1"
64+
tempfile = "3.8"
6465

6566
[features]
6667
integration-tests = []
6768

6869
[package.metadata.docs.rs]
6970
default-features = true
70-
rustdoc-args = ["--cfg", "docsrs"]
71+
rustdoc-args = ["--cfg", "docsrs"]

Dockerfile.README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
[GitHub](https://github.com/paritytech/polkadot-staking-miner)
44

5-
Formerly known as `staking-miner-v2` historical images versions are available in the [hub.docker.com](https://hub.docker.com/r/paritytech/staking-miner-v2)
5+
Formerly known as `staking-miner-v2` historical images versions are available in the [hub.docker.com](https://hub.docker.com/r/paritytech/staking-miner-v2)

README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ Here are some notable options you can use with the command:
185185
| `--min-signed-phase-blocks <number>` | Minimum number of blocks required in the signed phase before submitting a solution. | 10 |
186186
| `--balancing-iterations <number>` | Number of balancing iterations for the sequential phragmen algorithm. Higher values may produce better balanced solutions at the | 10 |
187187
| | cost of more computation time. | |
188+
| `--algorithm <algorithm>` | Election algorithm to use for mining solutions. Supported: `seq-phragmen`, `phragmms`. | `seq-phragmen` |
188189

189190
Refer to `--help` for the full list of options.
190191

@@ -239,6 +240,176 @@ The miner uses the on-chain nonce for a given user to submit solutions, which ca
239240
collisions if multiple miners are running with the same account. This can cause transaction failures
240241
and potentially result in lost rewards or other issues.
241242

243+
## Predict
244+
245+
The `predict` command allows you to predict validator election outcomes for Substrate-based chains
246+
without running a full node. It fetches the necessary staking data from the chain and runs the same
247+
Phragmén algorithm that the chain uses to determine validator sets.
248+
249+
### Basic Usage
250+
251+
```bash
252+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --do-reduce
253+
```
254+
255+
### Command Options
256+
257+
| Option | Description | Default Value |
258+
| :------------------------------ | :---------------------------------------------------------------------------------------------------------------------------- | :----------------- |
259+
| `--desired-validators <number>` | Desired number of validators for the prediction | Fetched from chain |
260+
| `--overrides <path>` | Path to election overrides JSON file (see format below) | None |
261+
| `--output-dir <path>` | Output directory for prediction results | `results` |
262+
| `--balancing-iterations <number>`| Number of balancing iterations for the sequential phragmen algorithm. Higher values may produce better balanced solutions at the cost of more computation time. | 10 |
263+
| `--do-reduce` | Reduce the solution to prevent further trimming. | `false` |
264+
| `--algorithm <algorithm>` | Election algorithm to use. Supported: `seq-phragmen`, `phragmms`. | `seq-phragmen` |
265+
| `--block-number <number>` | Block number at which to run the prediction. If not specified, uses the latest block. | Latest block |
266+
267+
### Examples
268+
269+
#### Basic Prediction
270+
271+
```bash
272+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --do-reduce
273+
```
274+
275+
#### With Desired Validators
276+
277+
```bash
278+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --desired-validators 50 --do-reduce
279+
```
280+
281+
#### Using Election Overrides File
282+
283+
```bash
284+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --overrides overrides.json
285+
```
286+
287+
#### Prediction at a Specific Block
288+
289+
```bash
290+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --block-number 13196110 --do-reduce
291+
```
292+
293+
#### Run Prediction with reduction
294+
295+
```bash
296+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --do-reduce
297+
```
298+
299+
#### Run Prediction with PhragMMS algorithm
300+
301+
```bash
302+
cargo run -- --uri wss://westend-asset-hub-rpc.polkadot.io predict --algorithm phragmms
303+
```
304+
305+
### Output Files
306+
307+
The tool generates the following JSON files in the specified output directory:
308+
309+
1. **`validators_prediction.json`**: Contains elected validators with their stake information
310+
2. **`nominators_prediction.json`**: Contains nominator allocations and validator support
311+
312+
#### Validators Prediction Format
313+
314+
```json
315+
{
316+
"metadata": {
317+
"timestamp": "1765799538",
318+
"desired_validators": 600,
319+
"round": 40,
320+
"block_number": 10803423,
321+
"solution_score": {
322+
"minimal_stake": 11797523289886283,
323+
"sum_stake": 8372189060111758480,
324+
"sum_stake_squared": 117584540059969491964159919300216042
325+
},
326+
"data_source": "snapshot"
327+
},
328+
"results": [
329+
{
330+
"account": "15roBmbe5NmRXb4imfmhKxSjH8k9J5xtHSrvYJKpmmCLoPqD",
331+
"total_stake": "2372626.3933261476 DOT",
332+
"self_stake": "0 DOT",
333+
"nominator_count": 2,
334+
"nominators": [
335+
{
336+
"address": "121GCLDNk9ErAkCovjjuF3npDB3veo3i3myY6a5v2yNEgrZw",
337+
"allocated_stake": "769476 DOT"
338+
},
339+
{
340+
"address": "14mtWxmkUHsWqJLxMiRR8qrHTHyck712E5yjWpnxPBEh8Acb",
341+
"allocated_stake": "135680 DOT"
342+
},
343+
]
344+
}
345+
]
346+
}
347+
```
348+
349+
#### Nominators Prediction Format
350+
351+
```json
352+
{
353+
"nominators": [
354+
{
355+
"address": "15VArSaLFf3r9MzyQjcNTexjPoRDJuVVkqUmqtuUuBcPCYrX",
356+
"stake": "447.2323363908 DOT",
357+
"active_validators": [
358+
{
359+
"validator": "15ZvLonEseaWZNy8LDkXXj3Y8bmAjxCjwvpy4pXWSL4nGSBs",
360+
"allocated_stake": "447.2323363908 DOT"
361+
}
362+
],
363+
"inactive_validators": [
364+
"1627VVB5gtHiseCV8ZdffF7P3bWrLMkU92Q6u3LsG8tGuB63"
365+
],
366+
"waiting_validators": [
367+
"13K6QTYBPMUFTbhZzqToKcfCiWbt4wDPHr3rUPyUessiPR61",
368+
"15rb4HVycC1KLHsdaSdV1x2TJAmUkD7PhubmhL3PnGv7RiGY"
369+
]
370+
}
371+
]
372+
}
373+
```
374+
375+
### Election Overrides File Format
376+
377+
When using `--overrides`, the file should have the following JSON structure:
378+
379+
```json
380+
{
381+
"candidates_include": ["15S7YtETM31QxYYqubAwRJKRSM4v4Ua6WGFYnx1VuFBnWqdG"],
382+
"candidates_exclude": [],
383+
"voters_include": [
384+
["15S7YtETM31QxYYqubAwRJKRSM4v4Ua6WGFYnx1VuFBnWqdG", 1000000, ["15S7YtETM31QxYYqubAwRJKRSM4v4Ua6WGFYnx1VuFBnWqdG"]]
385+
],
386+
"voters_exclude": []
387+
}
388+
```
389+
390+
**Note:** Override file paths can be nested (e.g., `data/elections/overrides.json`). The tool will
391+
automatically resolve relative paths from the current working directory.
392+
393+
### How Predict Command Works
394+
395+
1. **Data Source**: The tool first tries to fetch data from the chain's snapshot (if available),
396+
then falls back to the staking pallet.
397+
398+
2. **Overrides Application**: If `--overrides` is provided, the tool applies the specified modifications to the fetched candidates and voters:
399+
- (1) Add candidates that may not exist on-chain.
400+
- (2) Remove specific candidates from the election.
401+
- (3) Add or override voters with custom stake amounts.
402+
- (4) Remove specific voters from the election.
403+
404+
3. **Election Algorithm**: Runs the same Phragmén algorithm (`seq_phragmen`) used by Substrate chains
405+
to determine:
406+
- Which validators would be elected
407+
- Stake distribution among validators
408+
- Nominator allocations to validators
409+
410+
4. **Output Generation**: Creates detailed JSON files with predictions, including validator and
411+
nominator perspectives.
412+
242413
## Update metadata
243414

244415
The binary itself embeds [multi-block static metadata](./artifacts/multi_block.scale) to generate a

0 commit comments

Comments
 (0)