> For the complete documentation index, see [llms.txt](https://docs.flock.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flock.io/flock-products/fl-alliance/smart-contracts-deep-dive.md).

# Smart Contracts Deep-dive

This page gives detailed explanation of FL Alliance's smart contracts.

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2FCnuXpd82O6ifzSBw9Pp1%2FFL_Alliance_v3.png?alt=media&amp;token=9955b273-7ec2-45ff-aaed-b67f467e0066" alt=""><figcaption><p>FL Alliance Smart Contract Interactions.</p></figcaption></figure>

## Core Contracts

### `FlockTaskManager.sol`

**Role:** Factory and lifecycle controller for all `FlockTask` instances.

* **Task Creation**: Deploys new `FlockTask` contracts, passing in initial parameters like `_totalNumberOfRounds`, `_minStakeThreshold`, `_initialRewardPoolSize`, etc.
* **Task Management**: Maintains a list (via `EnumerableSet`) of active or unfinished `FlockTask` addresses.
* **Finish Task**: The owner can mark a task as `finished` if necessary (`finishTask(...)`), removing it from the active set.

### `FlockTask.sol`

**Role:** This contract implements a decentralized and scalable model for round-based federated learning (“FL tasks”).

* **Staking & Roles**: Participants stake tokens to become eligible to join rounds, where they are randomly assigned roles (proposers, voters).
* **Proposal & Voting Rounds**: Each round, selected proposers submit model updates, and voters commit/reveal votes that determine the most popular model via stake-weighted voting.
* **Rewards & Slashing**: Well-behaved participants (correct contributions/votes) receive rewards; dishonest behaviors can be “slashed,” losing a percentage of their stake.

## Libraries

### `LibABDKMathQuad.sol`

**Role**: Quadruple-precision (128-bit) math operations.

* Implements IEEE 754–style floating-point arithmetic for `bytes16` values.
* Provides precision far beyond typical fixed-point or 256-bit integer math.
* Useful for advanced computations like logarithms, exponentials, or rational approximations.

### `LibFlockTask.sol`

Role: Provides specialized numeric and utility functions for federated-learning tasks. Key Functions include:

* **`mulDiv`(uint256 x, uint256 y, uint256 z)**
  * Combines multiplication and division with quadruple precision by leveraging `LibABDKMathQuad`.
* `rand`()
  * Generates a pseudo-random number by hashing block and transaction properties.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flock.io/flock-products/fl-alliance/smart-contracts-deep-dive.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
