Based App Example
The example discussed in this page can be found at this repository:
It has been built to execute one task: fetch the most recent block and reach a majority vote on the slot number.
The simple example implemented here and used as reference in this page does not currently use the SDK as it breaks down the steps further, with verbose logging, for illustrative purposes, as this is the most important thing that BApp client developers need to familiarize with.
When launched, the application the first action it takes is to fetch on-chain data for the given Based Application, in order to calculate the Strategy weights.
Strategy Weightsβ
In Based Applications, the obligated token balance and delegated validator balance are used to attribute a weight to each Strategy, which is then used to vote on whatever task the client should be accomplishing.
For an overview of these steps as well as a thorough explanation on the calculations, please refer to this page in the Learn section.
Developers should, however, not worry too much about it, as all of this can be accomplished thanks to high-level SDK functions: getStrategyTokenWeights()
and calcArithmeticStrategyWeights()
(as well as its other variants).
The vote calculation follows these steps:
- Collect strategies opted-in to the bapp
- Collect total validator balance delegated to all opted-in strategy owners
- Collect total obligated token balances
- Get "significance" of tokens and validator balance from config
- Calculate risk-adjusted weights for each token, for each strategy
- Normalize the obtained weights
- Combine strategy-token weights into a final weight for each strategy
Here's an example of the result of these steps, referencing the output of the application:
π Normalized Final Weights
ββββββββββββ¬βββββββββββββ¬βββββββββββββββ¬βββββββββββββ
β Strategy β Raw Weight β Norm. Weight β Weight (%) β
ββββββββββββΌβββββββββββββΌβββββββββββββββΌβββββββββββββ€
β 4 β 1.28e-2 β 1.35e-1 β 13.54% β
β 5 β 8.18e-2 β 8.65e-1 β 86.46% β
ββββββββββββ΄βββββββββββββ΄βββββββββββββββ΄βββββββββββββ
BApp task executionβ
Once the application has finalized the Strategy Weights, it then starts the execution of a simple task (providing the next block slot number). The task is effectively executed, but the interaction between two independent strategy is simulated, for simplicity.
-
Strategies retrieve the slot number. In real world scenario, multiple instances of a client would do this independently, whereas the example only does this once.
-
The first Strategy attempts to complete the task, signing and broadcasting the voted slot number.
-
The vote is processed, but the first Strategy only has 13.54% of the total weight, the majority is not reached, so it is not enough to complete the task.
-
The second Strategy attempts to complete the task, signing and broadcasting the voted slot number.
-
The vote is processed, and the second Strategy has 86.46% of the total weight, reaching the majority threshold.
-
Since 100% of the total weight has now voted, the task is verified as complete. The system acknowledges that the task is fully verified.
You can visualize the task execution flow using the chart in the picture below
Below is the output of the example application pertaining to the steps just described:
π Simulate Blockchain Agreement Process for Slot 11059006
[π§strategy 4] π¦ Handling new block with slot 11059006.
[π§strategy 4] π€ Broadcasting vote
[π§strategy 4] π³οΈ Received vote from participant 4 with slot 11059006
[π§strategy 4] π Checking majority for slot 11059006
[π§strategy 4] π’ Total weight: 13.54%. Decomposition: 13.54% (from P4)
[π§strategy 4] β Majority not yet reached for slot: 11059006
[π§strategy 5] π³οΈ Received vote from participant 4 with slot 11059006
[π§strategy 5] π Checking majority for slot 11059006
[π§strategy 5] π’ Total weight: 13.54%. Decomposition: 13.54% (from P4)
[π§strategy 5] β Majority not yet reached for slot: 11059006
[π§strategy 5] π¦ Handling new block with slot 11059006.
[π§strategy 5] π€ Broadcasting vote
[π§strategy 4] π³οΈ Received vote from participant 5 with slot 11059006
[π§strategy 4] π Checking majority for slot 11059006
[π§strategy 4] π’ Total weight: 100.00%. Decomposition: 13.54% (from P4) + 86.46% (from P5)
[π§strategy 4] β
Majority found for slot: 11059006. Updating last decided slot.
[π§strategy 5] π³οΈ Received vote from participant 5 with slot 11059006
[π§strategy 5] π Checking majority for slot 11059006
[π§strategy 5] π’ Total weight: 100.00%. Decomposition: 13.54% (from P4) + 86.46% (from P5)
[π§strategy 5] β
Majority found for slot: 11059006. Updating last decided slot.