Skip to content

Commit

Permalink
update book to p2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte committed Nov 15, 2024
1 parent 5979801 commit efedb7e
Show file tree
Hide file tree
Showing 65 changed files with 2,725 additions and 749 deletions.
45 changes: 35 additions & 10 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
# Summary

- [Introduction](./introduction.md)
[Introduction](./introduction.md)
[Background](./background.md)

# Guides

- [Architecture](./architecture/index.md)
- [Polka Storage Provider Server](./architecture/polka-storage-provider-server.md)
- [Pallets](./architecture/pallets/index.md)
- [Market](./architecture/pallets/market.md)
- [Storage Provider](./architecture/pallets/storage-provider.md)
- [Proofs](./architecture/pallets/proofs.md)
- [Randomness](./architecture/pallets/randomness.md)
- [Getting Started](./getting-started/index.md)
- [Local Testnet - Polka Storage Parachain](./getting-started/local-testnet.md)
- [CAR server](./getting-started/car-server.md)
- [Real-world use case demo](./getting-started/demo.md)
- [Building](./getting-started/building/index.md)
- [From Source](./getting-started/building/source.md)
- [Using Docker](./getting-started/building/docker.md)
- [Local Testnet - Polka Storage Parachain](./getting-started/local-testnet/index.md)
- [Getting funds](./getting-started/local-testnet/getting-funds.md)
- [Launching the Storage Provider](./getting-started/storage-provider.md)
- [Proving a file](./getting-started/demo-file-prove.md)
- [Storing a file](./getting-started/demo-file-store.md)

# Tools

- [Polka Storage Provider CLI](./storage-provider-cli/index.md)
- [`storage`](./storage-provider-cli/storage.md)
- [`wallet`](./storage-provider-cli/wallet.md)
- [`server`](./storage-provider-cli/server.md)
- [`client`](./storage-provider-cli/client/index.md)
- [`wallet`](./storage-provider-cli/client/wallet.md)
- [`proofs`](./storage-provider-cli/client/proofs.md)
- [Storagext CLI](./storagext-cli/index.md)
- [`market`](./storagext-cli/market.md)
- [`storage-provider`](./storagext-cli/storage-provider.md)
- [`proofs`](./storagext-cli/proofs.md)
- [`randomness`](./storagext-cli/randomness.md)
- [`system`](./storagext-cli/system.md)
- [Pallets](./pallets/index.md)
- [Market](./pallets/market.md)
- [Storage Provider](./pallets/storage-provider.md)
- [Glossary](./glossary.md)
- [Mater CLI](./mater-cli/index.md)
- [Zombienet Configuration](./zombienet-config.md)

---

[Glossary](./glossary.md)
45 changes: 45 additions & 0 deletions src/architecture/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Architecture Overview

The Polka Storage parachain is, just like other parachains, composed of collators that receive extrinsics calls,
and through them perform state transitions.

## System Overview

![](../images/architecture/system_overview.png)

From left to right, we have validators (represented by a single node as only one validates blocks at a time),
collators and the storage providers.

The validators are handled by Polkadot itself — i.e. who gets to check the validity proofs is randomly selected by the network.

The collators execute our parachain runtime and process extrinsic calls from the storage providers — such as proof of storage submissions.

The storage providers are independent of the collators and are controlled by people like you, who provide storage to the system.
Storage management is left to the storage providers, being responsible to keep their physical system in good shape to serve clients.
We do provide an implementation of the storage provider, you can read more about it in the [Polka Storage Provider Server chapter](./polka-storage-provider-server.md).

## Pallets Overview

![](../images/architecture/pallets_overview.png)

We've been focusing on implementing the core functionality by developing the market, storage provider, proofs and randomness pallets.

The market pallet handles all things related to deal payments and slashing,
being informed by the storage provider when deals haven't been proven and applying slashing in those cases.
The storage provider handles the registering of storage providers and the proof submission,
the latter is checked inside the collator's WASM runtime, using the proofs pallet.
Finally, the proofs pallet makes use of randomness for challenges, ensuring the storage providers can't cheat the system.

For a deeper dive on the pallets, you can read the [Pallets chapter](./pallets/index.md).

## Resources on Parachains

Reading:
* [Parachains' Protocol Overview](https://wiki.polkadot.network/docs/learn-parachains-protocol)
* [The Path of a Parachain Block](https://polkadot.com/blog/the-path-of-a-parachain-block)

Videos:
* [Introduction to Polkadot, Parachains, and Substrate](https://www.youtube.com/live/gT-9r1bcVHY?si=dmCJyWB5w2NY1bnu&t=1670)
* [The Path of a Parachain Block - Joe Petrowski](https://www.youtube.com/watch?v=vRsBlVELQEo)
* [The Path of a Parachain Block on Polkadot and Kusama Network](https://www.youtube.com/watch?v=m0vxqWwFfDs)

55 changes: 55 additions & 0 deletions src/architecture/pallets/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Polka Storage pallets

- [`storage-provider`](storage-provider.md) - A pallet that manages storage providers and their associated data.
- [`market`](market.md) - A pallet that handles the storage market operations.
- [`proofs`](proofs.md) - A pallet responsible for verifying [PoRep](../../glossary.md#porep) and [PoSt](../../glossary.md#post).
- [`randomness`](randomness.md) - A pallet providing randomness source for blocks, mainly used by Proofs.

## Overview

The Polka Storage parachain is all about making storage deals. Let us go over how a deal is done!

Before anything else, [Storage Providers](../../glossary.md#storage-provider) need to register themselves with the [Storage Provider Pallet](./storage-provider.md) — they can do so using the [`register_storage_provider`](./storage-provider.md#register_storage_provider) extrinsic.

<img src="../../images/storage-provider/register_storage_provider.svg" alt="Storage Provider registration">

Now that storage providers can be registered in the storage provider pallet, we need to add some balance to both the
[Storage User's](../../glossary.md#storage-user) and the Provider's accounts,
which is done using the Market's [`add_balance`](./market.md#add_balance) extrinsic.

<img src="../../images/market/add_balance.svg" alt="Adding balance to Market accounts">

Afterwards, storage users and providers negotiate data storage deals off-chain.
Once a deal between the two parties is reached, the client can sign the deal and send it to the storage provider for publishing
— the storage provider will then publish the signed deal using the [`publish_storage_deals`](market.md#publish_storage_deals) extrinsic.

After publishing, the funds allocated for the deal will be moved from `free` to `locked`, and they can no longer be withdrawn until the deal has ended.

<img src="../../images/market/publish_storage_deals.svg" alt="Publishing storage deals">

At this point, the remaining responsibility is shifted to the storage provider, which needs to activate the deal.
First, the storage provider needs to call `get_randomness` from the [Randomness Pallet](./randomness.md) in order to create a replica and
[pre-commit](./storage-provider.md#pre_commit_sectors) the deal's sectors.
The sealing and pre-committing takes some time, after that the storage provider needs to fetch yet another randomness seed to create a proof.
Subsequently, they [prove](./storage-provider.md#prove_commit_sectors) they stored the sectors by calling [`prove_commit_sectors`](./storage-provider.md#prove_commit_sectors) extrinsics.

Verification is done via the [Proofs Pallet](./proofs.md) and reported to the Market pallet to terminate the deal and apply penalties to the storage provider
(remove and burn its collateral — i.e. `locked` funds) if they fail to activate the deal on time and return the funds to the client.

<img src="../../images/storage-provider/sector_activation.svg" alt="Deal activation">

Suppose the deal has been completed successfully or is **Active**.
In that case, the storage provider is now required to periodically submit proofs that they're still storing the user's data
— the storage provider does this by calculating a proof and submitting it using [`submit_windowed_post`](./storage-provider.md#submit_windowed_post).

<img src="../../images/storage-provider/submit_windowed_post.svg" alt="Proving the data is still stored">

Finally, storage providers can then settle deal payments to receive their fair share for keeping the user's data safe — using the [`settle_deal_payments`](./market.md#settle_deal_payments) extrinsic.

<img src="../../images/market/settle_deal_payments.svg" alt="Settling deal payments">

Putting it all together, we get the following:

<img id="figure-overview" src="../../images/overview_flow.svg" alt="The described flow">


44 changes: 22 additions & 22 deletions src/pallets/market.md → src/architecture/pallets/market.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The purpose of the pallet is to manage storage deals between storage market part
Market Pallet is tightly coupled with [Storage Provider Pallet](./storage-provider.md) because it's a source of truth for deals.
Storage Provider Pallet cannot exist without deal information from Market Pallet.

## Extrinsics<a href="../glossary.md#extrinsics"><sup>\*</sup></a>
## Extrinsics<a href="../../glossary.md#extrinsics"><sup>\*</sup></a>

### `add_balance`

Expand All @@ -32,7 +32,7 @@ The reserved amount will be considered `free` until it is used in a deal when

#### <a id="add_balance.example" href="#add_balance.example" class="header">Example</a>

Using the `storagext-cli` to add 1000000000[^existential_deposit] [Plancks](../glossary.md#planck) to Alice's account with the following command[^add_balance]:
Using the `storagext-cli` to add 1_000_000_000[^existential_deposit] [Plancks](../../glossary.md#planck) to Alice's account with the following command[^add_balance]:

```bash
storagext-cli --sr25519-key "//Alice" market add-balance 1000000000
Expand All @@ -42,28 +42,28 @@ storagext-cli --sr25519-key "//Alice" market add-balance 1000000000
This value is the minimum amount due to Polkadot's existential deposit.
More information available in: <https://support.polkadot.network/support/solutions/articles/65000168651-what-is-the-existential-deposit->.

[^add_balance]: Read more about the `add-balance` command in [_Storagext CLI/Subcommand `market`/`add-balance`_](../storagext-cli/market.md#add-balance)
[^add_balance]: Read more about the `add-balance` command in [_Storagext CLI/Subcommand `market`/`add-balance`_](../../storagext-cli/market.md#add-balance).

### `withdraw_balance`

Withdraws funds from the Storage Market.

The funds will be withdrawn from the `free` balance, meaning that the `amount` must be
lesser than or equal to `free` and greater than 0 (\\({free} \ge {amount} \gt 0\\)).
less than or equal to `free` and greater than 0 (\\({free} \ge {amount} \gt 0\\)).

| Name | Description | Type |
| -------- | -------------------------- | ---------------- |
| `amount` | The amount to be withdrawn | Positive integer |

#### <a id="withdraw_balance.example" href="#withdraw_balance.example" class="header">Example</a>

Using the `storagext-cli` to withdraw 10000 [Plancks](../glossary.md#planck) from Alice's `free` balance using the following command[^withdraw_balance]:
Using the `storagext-cli` to withdraw 10000 [Plancks](../../glossary.md#planck) from Alice's `free` balance using the following command[^withdraw_balance]:

```bash
storagext-cli --sr25519-key "//Alice" market withdraw-balance 10000
```

[^withdraw_balance]: Read more about the `withdraw-balance` command in [_Storagext CLI/Subcommand `market`/`withdraw-balance`_](../storagext-cli/market.md#withdraw-balance)
[^withdraw_balance]: Read more about the `withdraw-balance` command in [_Storagext CLI/Subcommand `market`/`withdraw-balance`_](../../storagext-cli/market.md#withdraw-balance).

### `publish_storage_deals`

Expand All @@ -73,11 +73,11 @@ This extrinsic _must_ be called by a storage provider.

| Name | Description | Type |
| ------------------ | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `proposal` | Specific deal proposal, a JSON object | JSON object, specified in the [next section](#deal-proposal-components) |
| `proposal` | Specific deal proposal, a JSON object | JSON object, specified in the [deal proposal components section](#deal-proposal-components) |
| `client_signature` | Client signature of this specific deal proposal | [`MultiSignature`](https://paritytech.github.io/polkadot-sdk/master/sp_runtime/enum.MultiSignature.html), meaning a 64-byte array for Sr25519 and Ed25519 signatures and 65-byte array for ECDSA signatures |

The `client_signature`, as the name indicates, is generated by the client by signing the deal proposal with their private key —
the [`storagext-cli`](../storagext-cli/index.md) does this for the user automatically[^warn].
the [`storagext-cli`](../../storagext-cli/index.md) does this for the user automatically[^warn].
This signature ensures that the storage provider cannot forge a deal with an arbitrary client.
The type of signature is dependent on the key the signer has, currently supported key types are Sr25519, ECDSA and Ed25519.

Expand All @@ -94,8 +94,8 @@ _This step corresponds to the "sign & send proposal" step in the [deal overview]
| `label` | Arbitrary client chosen label | String, with a maximum length of 128 characters |
| `start_block` | Block number on which the deal should start | Positive integer |
| `end_block` | Block number on which the deal should end | Positive integer, `end_block > start_block` |
| `storage_price_per_block` | Price for the storage specified per block[^storage_price_per_block] | Positive integer, in [Plancks](../glossary.md#planck) |
| `provider_collateral` | Collateral which is slashed if the deal fails | Positive integer, in [Plancks](../glossary.md#planck) |
| `storage_price_per_block` | Price for the storage specified per block[^storage_price_per_block] | Positive integer, in [Plancks](../../glossary.md#planck) |
| `provider_collateral` | Collateral which is slashed if the deal fails | Positive integer, in [Plancks](../../glossary.md#planck) |
| `state` | Deal state. Can only be set to `Published` | String |

See the [original Filecoin specification](https://spec.filecoin.io/#section-systems.filecoin_markets.onchain_storage_market.storage_deal_flow) for details.
Expand Down Expand Up @@ -149,9 +149,9 @@ and is **not final**; it is a testing tool.

</div>

[^storage_price_per_block]: The formula to calculate the total price is as follows: \\[total\\\_price = (end\\\_block - start\\\_block) * storage\\_price\\_per\\_block\\]
[^publish_storage_deals]: Read more about the `publish-storage-deals` command in [_Storagext CLI/Subcommand `market`/`publish-storage-deals`_](../storagext-cli/market.md#publish-storage-deals)
[^warn]: Take into account that the CLI is currently for demo purposes, the authors are aware that the command isn't safe since it requires the private keys of both parties.
[^storage_price_per_block]: The formula to calculate the total price is as follows: \\\[total\\\_price = (end\\\_block - start\\\_block) \* storage\\\_price\\\_per\\\_block\\].
[^publish_storage_deals]: Read more about the `publish-storage-deals` command in [_Storagext CLI/Subcommand `market`/`publish-storage-deals`_](../../storagext-cli/market.md#publish-storage-deals).

### `settle_deal_payments`

Expand All @@ -174,7 +174,7 @@ Using the `storagext-cli` to settle deal payments for IDs 97, 1010, 1337 and 420
storagext-cli --sr25519-key "//Alice" market settle-deal-payments 97 1010 1337 42069
```

[^settle_deal_payments]: Read more about the `settle-deal-payments` command in [_Storagext CLI/Subcommand `market`/`settle-deal-payments`_](../storagext-cli/market.md#settle-deal-payments)
[^settle_deal_payments]: Read more about the `settle-deal-payments` command in [_Storagext CLI/Subcommand `market`/`settle-deal-payments`_](../../storagext-cli/market.md#settle-deal-payments)

## Events

Expand All @@ -197,9 +197,9 @@ The Market Pallet emits the following events:
- `DealsSettled` - Published after the `settle_deal_payments` extrinsic is called. Indicates which deals were successfully and unsuccessfully settled.
- `successful` - List of deal IDs that were settled
- `unsuccessful` - List of deal IDs with the corresponding errors
- `DealSlashed` - Is emitted when some deal expired
- `DealSlashed` - Is emitted when some deal expired.
- `deal_id` - Deal ID that was slashed
- `DealTerminated` - Is emitted it indicates that the deal was voluntarily or involuntarily terminated.
- `DealTerminated` - A deal was voluntarily or involuntarily terminated.
- `deal_id` - Terminated deal ID
- `client` - SS58 address of the storage client
- `provider` - SS58 address of the storage provider
Expand All @@ -210,7 +210,7 @@ The Market Pallet actions can fail with following errors:

- `InsufficientFreeFunds` - Market participants do not have enough free funds.
- `NoProposalsToBePublished` - `publish_storage_deals` was called with an empty list of `deals`.
- `ProposalsNotPublishedByStorageProvider` - Is returned when calling `publish_storage_deals` and the deals in a list are not published by the same storage provider.
- `ProposalsPublishedByIncorrectStorageProvider` - Is returned when calling `publish_storage_deals` and the deals in a list are not published by the same storage provider.
- `AllProposalsInvalid` - `publish_storage_deals` call was supplied with a list of `deals` which are all invalid.
- `DuplicateDeal` - There is more than one deal with this ID in the Sector.
- `DealNotFound` - Tried to activate a deal that is not in the system.
Expand All @@ -227,9 +227,9 @@ The Market Pallet actions can fail with following errors:

## Constants

| Name | Description | Value |
| ------------------ | ----------------------------------------------------------------------------- | ----------- |
| `MaxDeals` | How many deals can be published in a single batch of `publish_storage_deals`. | 128 |
| `MaxDealsPerBlock` | Maximum deals that can be scheduled to start at the same block. | 128 |
| `MinDealDuration` | Minimum time an activated deal should last. | 5 Minutes |
| `MaxDealDuration` | Maximum time an activated deal should last. | 180 Minutes |
| Name | Description | Value |
| ------------------ | ----------------------------------------------------------------------------- | ------------------------- |
| `MaxDeals` | How many deals can be published in a single batch of `publish_storage_deals`. | 128 |
| `MaxDealsPerBlock` | Maximum deals that can be scheduled to start at the same block. | 128 |
| `MinDealDuration` | Minimum time an activated deal should last. | 5 Minutes (50 Blocks) |
| `MaxDealDuration` | Maximum time an activated deal should last. | 180 Minutes (1800 Blocks) |
Loading

0 comments on commit efedb7e

Please sign in to comment.