Skip to content

Commit 6919f7f

Browse files
committed
feat: runner docs and examples
1 parent 4d38ae3 commit 6919f7f

File tree

12 files changed

+761
-23
lines changed

12 files changed

+761
-23
lines changed

pages/developers/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const meta: Meta = {
1313
},
1414
"blueprint-sdk": "Introduction",
1515
"blueprint-contexts": "Contexts",
16+
"blueprint-runner": "Blueprint Runner",
1617
"p2p-networking": "P2P Networking",
1718
"tangle-avs": "Build a Tangle Blueprint",
1819
"eigenlayer-avs": "Build an Eigenlayer AVS",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Meta } from "nextra";
2+
3+
const meta: Meta = {
4+
introduction: "Introduction",
5+
jobs: "Jobs",
6+
routers: "Routers",
7+
producers: "Producers",
8+
consumers: "Consumers",
9+
"background-services": "Background Services",
10+
building: "Building a Blueprint Runner",
11+
};
12+
13+
export default meta;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Background Services
3+
---
4+
5+
import GithubFileReaderDisplay from '/components/GithubFileReaderDisplay';
6+
7+
# Background Services
8+
9+
Background services are optional components in the [Blueprint Runner](/developers/blueprint-runner/introduction) architecture that run continuously to support the operation of your Actively Validated Service (AVS). This document explains how background services work, how to configure them, and best practices for implementation.
10+
11+
## What are Background Services?
12+
13+
Background services refer to any long-running processes that operate independently of job execution. They:
14+
15+
1. Run continuously in the background
16+
2. Can perform periodic or ongoing tasks
17+
3. Might maintain state or connections
18+
4. Support the overall operation of the Blueprint Runner
19+
20+
Unlike job handlers that execute in response to specific requests, background services operate autonomously to provide supporting functionality.
21+
22+
## Common Use Cases
23+
24+
Background services could be used for various purposes in Blueprints, with the following being only a few examples:
25+
26+
### Data Collection and Aggregation
27+
28+
Services that collect and aggregate data, such as an aggregator that submits aggregated signatures to a Blockchain.
29+
30+
### Monitoring and Health Checks
31+
32+
Services that monitor the state of something, such as a health checker that verifies that a Blueprint is running.
33+
34+
## Background Service Configuration
35+
36+
Background services are typically configured in the `main.rs` file of your Blueprint binary, when building the Blueprint Runner. The configuration involves:
37+
38+
### Basic Background Service Setup
39+
40+
The only requirement for a background service is that it implements the `BackgroundService` trait:
41+
42+
<GithubFileReaderDisplay
43+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-lib/src/lib.rs"
44+
fromLine={23}
45+
toLine={34}
46+
title="Defining a Background Service"
47+
/>
48+
49+
With a background service defined, it is passed into the Blueprint Runner's builder as producers and consumers are:
50+
51+
<GithubFileReaderDisplay
52+
url="https://github.com/tangle-network/blueprint/blob/7774ea42c4d3ec3ff7e170626de3ddce511d1f2f/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
53+
fromLine={36}
54+
toLine={67}
55+
title="Adding a Background Service to the Blueprint Runner"
56+
/>
57+
58+
For some background services, it may be necessary to add some sort of cleanup code that runs when the Blueprint Runner shuts down. This can be done in the `with_shutdown_handler` method seen in the above code. This specific example just prints a message, but it might end a background process gracefully.
59+
60+
## Integration with Other Components
61+
62+
Background services work closely with other Blueprint Runner components:
63+
64+
- **Routers**: Background services may provide support for [routers](/developers/blueprint-runner/routers), such as caching or state management
65+
- **Producers**: Background services can support [producers](/developers/blueprint-runner/producers) by maintaining connections or monitoring event sources
66+
- **Consumers**: Background services can assist [consumers](/developers/blueprint-runner/consumers) with resource management or periodic tasks
67+
68+
## Next Steps
69+
70+
Now that you understand background services, it might be helpful to take a look at:
71+
72+
- [Routers](/developers/blueprint-runner/routers) - How to direct job calls to appropriate handlers
73+
- [Producers](/developers/blueprint-runner/producers) - How to capture and process events
74+
- [Consumers](/developers/blueprint-runner/consumers) - How to handle job results
75+
- [Building a Blueprint Runner](/developers/blueprint-runner/building) - Step-by-step guide to building your own Blueprint Runner
76+
77+
## Conclusion
78+
79+
Background services are powerful components that enhance the capabilities of your Blueprint Runner by providing continuous support operations. By implementing well-designed background services, you can build more robust, efficient, and feature-rich Blueprints.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: Building a Blueprint Runner
3+
---
4+
5+
import GithubFileReaderDisplay from '/components/GithubFileReaderDisplay';
6+
7+
# Building a Blueprint Runner
8+
9+
This guide provides a step-by-step approach to building a Blueprint Runner, the primary component of a Blueprint.
10+
11+
## Prerequisites
12+
13+
Before you start building your Blueprint Runner, ensure you have:
14+
15+
1. [Rust](https://www.rust-lang.org/tools/install) installed
16+
2. [Tangle CLI](../cli/installation.mdx) installed
17+
3. A basic understanding of [Blueprints](../blueprints/introduction.mdx)
18+
4. A Blueprint created with the CLI as seen in [Step 1](#step-1-setting-up-the-project-structure) below
19+
20+
## Blueprint Runner Structure
21+
22+
A Blueprint Runner consists of:
23+
24+
1. **Entry Point**: The runner is the primary component in a blueprint's `main.rs`
25+
2. **[Jobs](/developers/blueprint-runner/jobs) Configuration**: Build the jobs that will be executed
26+
3. **[Router](/developers/blueprint-runner/routers) Configuration**: Setup for directing job calls to the jobs themselves for handling
27+
4. **[Producer](/developers/blueprint-runner/producers) Configuration**: Setup for the source of events (e.g. blockchain, external APIs)
28+
5. **[Consumer](/developers/blueprint-runner/consumers) Configuration**: Setup for the handling of results from jobs
29+
6. **[Background Service](/developers/blueprint-runner/background-services) Configuration**: Setup for supporting services (e.g. databases, servers, etc.)
30+
31+
## Step-by-Step Guide
32+
33+
### Step 1: Setting Up the Project Structure
34+
35+
If you haven't already created a Blueprint project, you can use the Tangle CLI (if you haven't installed it yet, see [here](../cli/installation.mdx)):
36+
37+
```bash
38+
cargo tangle blueprint create --name <BLUEPRINT_NAME>
39+
```
40+
41+
This creates a workspace with two main packages:
42+
43+
- **Library Package**: Contains your job definitions and core logic
44+
- **Binary Package**: Contains your Blueprint Runner implementation
45+
46+
### Step 2: Defining Your Jobs
47+
48+
Before building the runner, define the jobs that your Blueprint will execute. Jobs are defined in the library package:
49+
50+
<GithubFileReaderDisplay
51+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-lib/src/lib.rs"
52+
fromLine={7}
53+
toLine={21}
54+
title="Job Definition from our Incredible Squaring Example"
55+
/>
56+
57+
For more details on defining jobs, see the [Jobs documentation](/developers/blueprint-runner/jobs).
58+
59+
### Step 3: Creating a Producer and a Consumer
60+
61+
To build a Blueprint Runner, you need a producer to listen for events and a consumer to handle the results from jobs.
62+
63+
<GithubFileReaderDisplay
64+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
65+
fromLine={28}
66+
toLine={31}
67+
title="Creating a Producer and a Consumer"
68+
/>
69+
70+
### Step 4: Configuring the Router
71+
72+
The [router](/developers/blueprint-runner/routers) directs job calls to the appropriate handlers. In the example below, we configure the router with a single route for our defined job and specify that it is on Tangle with the `TangleLayer`:
73+
74+
<GithubFileReaderDisplay
75+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
76+
fromLine={33}
77+
toLine={48}
78+
title="Configuring the Router"
79+
/>
80+
81+
This configuration:
82+
1. Creates a new router
83+
2. Adds a route for the `square` job with ID `XSQUARE_JOB_ID`
84+
3. Applies the `TangleLayer` to add metadata to job results
85+
4. Adds a filter layer to only process jobs that match the service ID
86+
87+
For more details on routers, see the [Routers documentation](/developers/blueprint-runner/routers).
88+
89+
### Step 5: Defining a Background Service
90+
91+
Some blueprints may require one or more services to run in the background. Any number of background services can be set to run for a Blueprint Runner.
92+
93+
<GithubFileReaderDisplay
94+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-lib/src/lib.rs"
95+
fromLine={23}
96+
toLine={34}
97+
title="Defining a Background Service"
98+
/>
99+
100+
With a background service defined, it just needs to be added to the Blueprint Runner:
101+
102+
<GithubFileReaderDisplay
103+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
104+
fromLine={36}
105+
toLine={49}
106+
title="Adding a Background Service to the Blueprint Runner"
107+
/>
108+
109+
### Step 6: Configuring a Producer with the Blueprint Runner's Builder
110+
111+
[Producers](/developers/blueprint-runner/producers) listen for events and prepare them for processing. The following example uses a `TangleProducer` as seen in [Step 3](#step-3-creating-a-producer-and-a-consumer):
112+
113+
<GithubFileReaderDisplay
114+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
115+
fromLine={36}
116+
toLine={54}
117+
title="Adding a Producer to the Blueprint Runner"
118+
/>
119+
120+
This producer listens for finalized blocks on the Tangle network and converts them into job calls that can be processed by the router.
121+
122+
For more details on producers, see the [Producers documentation](/developers/blueprint-runner/producers).
123+
124+
### Step 7: Configuring a Consumer with the Blueprint Runner's Builder
125+
126+
[Consumers](/developers/blueprint-runner/consumers) handle the results of processed jobs. In the example above, we set up a Tangle consumer as seen in [Step 3](#step-3-creating-a-producer-and-a-consumer):
127+
128+
<GithubFileReaderDisplay
129+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
130+
fromLine={36}
131+
toLine={60}
132+
title="Adding a Consumer to the Blueprint Runner"
133+
/>
134+
135+
This consumer processes job results and can send transactions to the Tangle network based on those results.
136+
137+
For more details on consumers, see the [Consumers documentation](/developers/blueprint-runner/consumers).
138+
139+
### Step 8: Custom Shutdown Logic
140+
141+
Implement customized shutdown logic to handle cleanup and resource release:
142+
143+
<GithubFileReaderDisplay
144+
url="https://github.com/tangle-network/blueprint/blob/a9860d2e3a161af63a75d6d800f662d303e92e5b/examples/incredible-squaring/incredible-squaring-bin/src/main.rs"
145+
fromLine={36}
146+
toLine={65}
147+
title="Custom Shutdown Logic"
148+
/>
149+
150+
The above example simply prints a message when the Blueprint Runner is shutting down.
151+
152+
### Step 9: Running the Blueprint Runner
153+
154+
Finally, we run the Blueprint Runner:
155+
156+
```rust
157+
// Build and run the Blueprint Runner
158+
let result = BlueprintRunner::builder(tangle_config, env)
159+
// ... configuration ...
160+
.run()
161+
.await;
162+
```
163+
164+
The `run` method starts the Blueprint Runner and returns a result indicating whether it ran successfully or encountered an error.
165+
166+
With this, the Blueprint Runner, the centerpoint of your Blueprint is ready to be used!
167+
168+
## Next Steps
169+
170+
After building your Blueprint Runner, you might want to explore:
171+
172+
- [Advanced Router Features](/developers/blueprint-runner/routers#advanced-router-features)
173+
- [Producer Patterns](/developers/blueprint-runner/producers#producer-patterns)
174+
- [Consumer Patterns](/developers/blueprint-runner/consumers#consumer-patterns)
175+
176+
## Conclusion
177+
178+
Building a Blueprint Runner involves setting up various components that work together to execute your Tangle Blueprint. By following this guide and adhering to best practices, you can create a robust and efficient Blueprint Runner for your Actively Validated Service on the Tangle Network.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Consumers
3+
---
4+
5+
import GithubFileReaderDisplay from '/components/GithubFileReaderDisplay';
6+
7+
# Consumers
8+
9+
Consumers are a vital component of the [Blueprint Runner](/developers/blueprint-runner/introduction) architecture that handle the results of processed jobs. This document explains how consumers work, how to configure them, and best practices for implementation.
10+
11+
## What are Consumers?
12+
13+
In a Blueprint Runner, consumers are responsible for handling the results of job execution. They:
14+
15+
1. Receive results from jobs via the [router](/developers/blueprint-runner/routers)
16+
2. Process and transform these results as needed
17+
3. Perform actions based on the results, such as:
18+
- Sending transactions to the blockchain
19+
- Updating local state
20+
- Triggering other processes
21+
- Storing data for later use
22+
- Emitting events or notifications
23+
24+
Consumers act as the output handlers of your Blueprint Runner, ensuring that job results lead to appropriate actions.
25+
26+
## Consumer Configuration
27+
28+
Consumers are typically configured in the `main.rs` file of your Blueprint binary, when defining the Blueprint Runner. The configuration involves:
29+
30+
1. Creating a consumer
31+
2. Defining how it processes job results
32+
3. Connecting it to jobs via the [router](/developers/blueprint-runner/routers)
33+
34+
### Basic Consumer Setup
35+
36+
First, you define the consumer you want to use. For example, a `TangleConsumer` that listens for finalized blocks on Tangle. After which, you pass the consumer to the Blueprint Runner's builder.
37+
38+
```rust
39+
let env = BlueprintEnvironment::load()?;
40+
let sr25519_signer = env.keystore().first_local::<SpSr25519>()?;
41+
let sr25519_pair = env.keystore().get_secret::<SpSr25519>(&sr25519_signer)?;
42+
let sr25519_signer = TanglePairSigner::new(sr25519_pair.0);
43+
44+
let tangle_client = env.tangle_client().await?;
45+
let tangle_producer = TangleProducer::finalized_blocks(tangle_client.rpc_client.clone()).await?;
46+
let tangle_consumer = TangleConsumer::new(tangle_client.rpc_client.clone(), sr25519_signer);
47+
48+
BlueprintRunner::builder(tangle_config, env)
49+
.router(router) // Assuming your router is already defined
50+
.producer(tangle_producer)
51+
.consumer(tangle_consumer)
52+
.run()
53+
.await?;
54+
```
55+
56+
## Types of Consumers
57+
58+
Blueprint Runners can utilize various types of consumers depending on the requirements:
59+
60+
### Tangle Consumer
61+
62+
A Tangle Consumer is a consumer that handles transactions on the Tangle, submitting job results for a Blueprint.
63+
64+
<GithubFileReaderDisplay
65+
url="https://github.com/tangle-network/blueprint/blob/7774ea42c4d3ec3ff7e170626de3ddce511d1f2f/crates/tangle-extra/src/consumer/mod.rs"
66+
fromLine={39}
67+
toLine={51}
68+
title="Tangle Consumer Definition"
69+
/>
70+
71+
### EVM Consumer
72+
73+
An EVM Consumer is a consumer that handles transactions on the EVM, submitting job results for a Blueprint.
74+
75+
<GithubFileReaderDisplay
76+
url="https://github.com/tangle-network/blueprint/blob/7774ea42c4d3ec3ff7e170626de3ddce511d1f2f/crates/evm-extra/src/consumer/mod.rs"
77+
fromLine={44}
78+
toLine={54}
79+
title="EVM Consumer Definition"
80+
/>
81+
82+
## Integration with Other Components
83+
84+
Consumers work closely with other Blueprint Runner components:
85+
86+
- **Routers**: [Routers](/developers/blueprint-runner/routers) pass job results to consumers for handling
87+
- **Producers**: Results handled by consumers may generate new events for [producers](/developers/blueprint-runner/producers) to process
88+
89+
## Next Steps
90+
91+
Now that you understand consumers, learn about:
92+
93+
- [Routers](/developers/blueprint-runner/routers) - How to direct job calls to appropriate handlers
94+
- [Producers](/developers/blueprint-runner/producers) - How to capture and process events
95+
- [Building a Blueprint Runner](/developers/blueprint-runner/building) - Step-by-step guide to building your own Blueprint Runner
96+
97+
## Conclusion
98+
99+
Consumers are essential components that translate job results into meaningful actions in your Blueprint Runner. By implementing robust and efficient consumers, you can ensure that your Blueprint operates reliably and effectively.

0 commit comments

Comments
 (0)