|
| 1 | +--- |
| 2 | +title: "Connecting Steps" |
| 3 | +--- |
| 4 | + |
| 5 | +# Connecting Steps |
| 6 | + |
| 7 | +## Pre-requisite |
| 8 | +At this point, you'd have already followed the [Creating a Processor](./create-processor.mdx) and [Creating a Step](./steps.mdx) guides. |
| 9 | +Our next goal is to put those two pieces together and connect steps within the processor. |
| 10 | + |
| 11 | +## How to connect steps |
| 12 | + |
| 13 | +Now that you have created a step, you can connect it to other steps. |
| 14 | +To do so, we use a builder class called `ProcessorBuilder` to specifiy a sequence of steps that make up a processor. |
| 15 | + |
| 16 | +1. After you've instantiated your steps, you need to convert them into [`RunnableStep`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/sdk/src/traits/runnable_step.rs#L6). |
| 17 | +`RunnableStep` is a trait that wraps around a step. |
| 18 | +It provides the necessary input and output channels that feed into the step and allows the step to be spawned in a task. |
| 19 | +The SDK provides a helper function [`.into_runnable_step`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/sdk/src/traits/into_runnable_step.rs#L13) to convert a step into a `RunnableStep`. |
| 20 | +2. Setup your first step with [`ProcessorBuilder::new_with_inputless_first_step`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/sdk/src/builder/processor_builder.rs#L222). |
| 21 | +In almost all cases, the first step should be a `TransactionStreamStep`. {/* <!-- Add link --> */} |
| 22 | +3. Connect the previous step to the next step using [`.connect_to`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/sdk/src/builder/processor_builder.rs#L303). |
| 23 | + `connect_to` uses trait bounds to ensure at compile time that the output type of the previous step matches the input type of the next step. |
| 24 | + When calling `.connect_to`, a channel gets created with size `channel_size` and connects the previous and next steps. |
| 25 | + It also handles spawning the previous step in a task. |
| 26 | +4. Repeat step 3 for each step in your processor. |
| 27 | +5. To close off the `ProcessorBuilder`, use [`.end_and_return_output_receiver`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/sdk/src/builder/processor_builder.rs#L400). |
| 28 | +This return an [`InstrumentedAsyncReceiver`](https://github.com/aptos-labs/aptos-indexer-processor-sdk/blob/main/aptos-indexer-processors-sdk/instrumented-channel/src/lib.rs#L88) which you can use to process the output of the last step in the graph. |
| 29 | + |
| 30 | +Here's a simple example of connecting two steps: |
| 31 | +```rust |
| 32 | +let (processor_builder, buffer_receiver) = ProcessorBuilder::new_with_inputless_first_step( |
| 33 | + transaction_stream_step.into_runnable_step(), |
| 34 | + ) |
| 35 | + .connect_to(extractor_step.into_runnable_step(), 10) |
| 36 | + .end_and_return_output_receiver(10); |
| 37 | +``` |
| 38 | + |
| 39 | +Here's a [full example](https://github.com/aptos-labs/aptos-indexer-processor-example/blob/main/aptos-indexer-processor-example/src/processors/events/events_processor.rs#L75) from `aptos-indexer-processor-example`. |
| 40 | + |
| 41 | +{/* <!-- Add link to common SDK steps --> */} |
| 42 | + |
| 43 | +## Visualizing the processor |
| 44 | +As you connect steps, `ProcessorBuilder` in the background is constructing a graphical representation of the steps in your processor using [`petgraph`](https://docs.rs/petgraph/latest/petgraph/). |
| 45 | +You can see the visual representation of the graph by calling |
| 46 | +```rust |
| 47 | +let dot = processor_builder.graph.dot(); |
| 48 | +println!("{}", dot); |
| 49 | +``` |
| 50 | +This will output a graph in the [DOT language](https://graphviz.gitlab.io/_pages/doc/info/lang.html) that you can visualize using tools like [Graphviz](https://graphviz.org/). |
0 commit comments