Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
enhancement: add predicate support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashad Alston authored and ra0x3 committed Nov 20, 2023
1 parent 61824b1 commit 6b0c530
Show file tree
Hide file tree
Showing 83 changed files with 3,899 additions and 1,408 deletions.
1,006 changes: 561 additions & 445 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fuel-tx = { version = "=0.35.4", default-features = false }
fuel-types = { version = "=0.35.4", default-features = false, features = ["serde"] }
fuel-vm = { version = "=0.35.4", default-features = false }
fuels = { version = "0.50", default-features = false }
fuels-code-gen = { version = "0.50", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions ci/Dockerfile.fuel-node
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update -y \
COPY --from=builder /build/target/release/fuel-node .
COPY --from=builder /build/target/release/fuel-node.d .
COPY --from=builder /build/packages/fuel-indexer-tests/test-chain-config.json .
COPY --from=builder /build/packages/fuel-indexer-tests/contracts/fuel-indexer-test/out/debug/fuel-indexer-test.bin .
COPY --from=builder /build/examples/greetings/contracts/greeting/out/debug/greeting.bin .
COPY --from=builder /build/packages/fuel-indexer-tests/sway/fuel-indexer-test/out/debug/fuel-indexer-test.bin .
COPY --from=builder /build/examples/hello-world/contracts/greeting/out/debug/greeting.bin .
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Blocks](./indexing-fuel-types/blocks.md)
- [Transactions](./indexing-fuel-types/transactions.md)
- [Receipts](./indexing-fuel-types/receipts.md)
- [Predicates](./indexing-fuel-types/predicates.md)
- [Indexing Custom Types](./indexing-custom-types/index.md)
- [Storing Records](./storing-records/index.md)
- [Querying](./querying/index.md)
Expand Down
29 changes: 20 additions & 9 deletions docs/src/indexing-custom-types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,25 @@ Next, we'll cover how to write the manifest file for your indexer.

Before writing any of the handler code for your indexer, we need to make sure that our indexer manifest contains the necessary information to allow for the compiler to parse our contract types.

Specifically, we should ensure that the `contract_abi` and `graphql_schema` fields point to the correct locations, respectively.
Specifically, we should ensure that the `contract_abi` and `schema` fields point to the correct locations, respectively.

```yaml
# A namespace is a logical grouping of declared names. Think of the namespace
# as an organization identifier
namespace: fuellabs

# The identifier field is used to identify the given index.
# Unique identifier for this indexer.
identifier: custom_types_example

# The abi option is used to provide a link to the Sway JSON ABI that is generated when you
# build your project.
abi: path/to/custom/type/example/contract-abi.json
# Indexer contract configuration.
contract: ~

# File paths to the contract JSON ABIs that are generated when you build your Sway contracts.
abis: ~

# Specifies which particular contracts you would like your indexer to subscribe to.
subscriptions: ~


# The particular start block after which you'd like your indexer to start indexing events.
start_block: ~
Expand All @@ -197,12 +203,11 @@ end_block: ~
# with the `--indexer_net_config` option.
fuel_client: ~

# The contract_id specifies which particular contract you would like your index to subscribe to.
# Specifies which particular contract(s) you would like your indexer to subscribe to.
contract_id: ~

# The graphql_schema field contains the file path that points to the GraphQL schema for the
# given index.
graphql_schema: path/to/custom/type/example/indexer.schema.graphql
# A file path that points to the GraphQL schema for the given indexer.
schema: path/to/custom/type/example/indexer.schema.graphql

# The module field contains a file path that points to code that will be run as an executor inside
# of the indexer.
Expand All @@ -213,6 +218,12 @@ module:
# The resumable field contains a boolean that specifies whether or not the indexer should, synchronise
# with the latest block if it has fallen out of sync.
resumable: true

# Indexer predicate configuration.
predicates:

# Template commitments (hashes) of the bytecode of predicates used by this indexer.
templates: ~
```

## 4. Handler Logic
Expand Down
1 change: 1 addition & 0 deletions docs/src/indexing-fuel-types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ This document provides information about Fuel-specific types and provides exampl
- [Blocks](./blocks.md)
- [Transactions](./transactions.md)
- [Receipts](./receipts.md)
- [Predicates](./predicates.md)
57 changes: 57 additions & 0 deletions docs/src/indexing-fuel-types/predicates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Predicates

## Definition

- Immutable and ephemeral.
- Predicates are one of the most important abstractions that Fuel makes availabe. A predicate effectively has two states: a _created_ state, and a _spent_ state. A predicate has a `created` state when the predicate is...created.

- A predicate becomes "created" when a given UTXO inclues certain parameters in its [witness data](./transactions.md).
- A predicate becomes "spent" when the aforementioned UTXO is used as an input into a subsequent [Transaction](./transactions.md), where this subsequent Transaction also includes certain parameters in its witness data.

> TLDR: A predicate is a UTXO that includes extra information sent in the Transaction.
```rust,ignore
/// The indexer's public representation of a predicate.
///
/// This is a manual copy of `fuels::accounts::predicate::Predicate` due to the
/// fact that `fuels::accounts::predicate::Predicate` is not serializable.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Predicate {
/// Address of the predicate.
///
/// Using `Address` because `Bech32Address` is not serializable.
address: Address,
/// Bytecode of the predicate.
code: Vec<u8>,
/// Inputs injected into the predicate.
///
/// Using `Vec<u8> because `UnresolvedBytes` is not serializable.
data: Vec<u8>,
/// Chain ID of the predicate.
chain_id: u64,
}
```

## Usage

In order to get started indexing Predicates, users will need to:

1. Create a new Predicate project using [`forc`]
2. Build this predicate project in order to get the JSON ABI.
3. Add this predicate template hash to your indexer manifest.
4. Include the appropriate witness data in transactions you wish to flag to your indexer

```rust,ignore
extern crate alloc;
use fuel_indexer_utils::prelude::*;
#[indexer(manifest = "indexer.manifest.yaml")]
mod indexer_mod {
fn handle_spent_predicates(predicate: Predicate, configurables: MyPredicateConfigurables) {
unimplemented!()
}
}
```
3 changes: 3 additions & 0 deletions docs/src/indexing-fuel-types/receipts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct Burn {
```

```rust, ignore
extern crate alloc;
use fuel_indexer_utils::prelude::*;
mod indexer_mod {
fn handle_burn_receipt(block_data: BlockData) {
let height = block_data.header.height;
Expand Down
62 changes: 54 additions & 8 deletions docs/src/project-components/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ Below is a sample indexer manifest file
namespace: fuellabs
identifier: order_book_v1
fuel_client: beta-4.fuel.network:80
abi: path/to/my/contract-abi.json
contract_id: "fuels0x39150017c9e38e5e280432d546fae345d6ce6d8fe4710162c2e3a95a6faff051"
graphql_schema: path/to/my/schema.graphql
contract:
abi: path/to/my/contract-abi.json
subscriptions:
- fuels0x39150017c9e38e5e280432d546fae345d6ce6d8fe4710162c2e3a95a6faff051
schema: path/to/my/schema.graphql
start_block: 1564
end_block: 310000
module:
wasm: path/to/my/wasm_module.wasm
predicates:
abis: ~
templates: ~
```
## `namespace`
Expand All @@ -35,19 +40,26 @@ _Optional._

The `fuel_client` denotes the address (host, port combination) of the running Fuel client that you would like your indexer to index events from. In order to use this per-indexer `fuel_client` option, the indexer service at which your indexer is deployed will have to run with the `--indexer_net_config` option.

## `abi`
## `contract`

_Optional._

The `abi` option is used to provide a link to the Sway JSON application binary interface (ABI) that is generated when you build your Sway project. This generated ABI contains all types, type IDs, logged types, and message types used in your Sway contract.
Indexer contract settings.

## `contract_id`
### `abi`

_Optional._

The `contract_id` specifies the particular contract to which you would like an indexer to subscribe. Setting this field to an empty string will index events from any contract that is currently executing on the network. This field accepts either a single string, or a list of strings. The indexer will index events from all IDs if a list is passed.
The `abi` is used to provide a link to the contract JSON application binary interface (ABI) that is generated when you build your contract project.

> Important: Contract IDs are unique to the content of a contract. If you are subscribing to a certain contract and then the contract itself is changed or updated, you will need to change the `contract_id` field of the manifest to the new ID.
### `subscriptions`

_Optional._

The `contract_subscriptions` specifies the particular contract to which you would like an indexer to subscribe. Setting this field to an empty string will index events from any contract that is currently executing on the network. This field accepts either a single string, or a list of strings. The indexer will index events from all IDs if a list is passed.

> Important: Contract IDs are unique to the content of a contract. If you are subscribing to a certain contract and then the contract itself is changed or updated, you will need to change the `subscriptions` field of the manifest with the new contract ID.
>
> Note: This parameter supports both Bech32 contract IDs and non-Bech32 contract IDs

## `graphql_schema`
Expand Down Expand Up @@ -83,3 +95,37 @@ The `module` field contains a file path that points to code that will be run as
_Optional._

The `resumable` field contains a boolean value and specifies whether the indexer should synchronise with the latest block if it has fallen out of sync.

## `predicates`

_Optional._

Indexer predicate settings.

### `templates`

_Optional._

A list of predicate template configurations.

#### `name`

_Required._

The name of the predicate template.

> This name follow the name of the predicate listed in the predicate's `Forc.toml`

#### `id`

_Required._

The SHA-256 hash of the predicate bytecode.

> This can be found in your predicate's `[predicate-name]-bin-root` file after building your predicate with `forc build`

#### `abi`

_Required._

The `abi` is used to provide a link to the predicate JSON application binary interface (ABI) that is generated when you build your predicate project.
22 changes: 15 additions & 7 deletions examples/fuel-explorer/fuel-explorer/fuel_explorer.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace: fuellabs
# The identifier field is used to identify the given index.
identifier: explorer

# The abi option is used to provide a link to the Sway JSON ABI that is generated when you
# build your project.
abi: ~
# Indexer contract configuration.
contract:

# File paths to the contract JSON ABIs that are generated when you build your Sway contracts.
abi: ~

# Specifies which particular contracts you would like your indexer to subscribe to.
subscriptions: ~

# The particular start block after which you'd like your indexer to start indexing events.
start_block: ~
Expand All @@ -21,12 +26,9 @@ end_block: ~
# with the `--indexer_net_config` option.
fuel_client: ~

# The contract_id specifies which particular contract you would like your index to subscribe to.
contract_id: ~

# The graphql_schema field contains the file path that points to the GraphQL schema for the
# given index.
graphql_schema: examples/fuel-explorer/fuel-explorer/schema/fuel_explorer.schema.graphql
schema: examples/fuel-explorer/fuel-explorer/schema/fuel_explorer.schema.graphql

# The module field contains a file path that points to code that will be run as an executor inside
# of the indexer.
Expand All @@ -37,3 +39,9 @@ module:
# The resumable field contains a boolean that specifies whether or not the indexer should, synchronise
# with the latest block if it has fallen out of sync.
resumable: true

# Indexer predicate configuration.
predicates:

# Template commitments (hashes) of the bytecode of predicates used by this indexer.
templates: ~
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ namespace: fuellabs
# The identifier field is used to identify the given index.
identifier: greetings

# The abi option is used to provide a link to the Sway JSON ABI that is generated when you
# build your project.
abi: examples/greetings/contracts/greeting/out/debug/greeting-abi.json
# Indexer contract configuration.
contract:

# File paths to the contract JSON ABIs that are generated when you build your Sway contracts.
abi: examples/greetings/contracts/greeting/out/debug/greeting-abi.json

# Specifies which particular contracts you would like your indexer to subscribe to.
subscriptions:
- fuel1q6sj2srt0u40jdqg2lvvnspyuhse9rs2s2fv9nmv0hqjcrdc7sqsfpwv9x

# The particular start block after which you'd like your indexer to start indexing events.
start_block: ~
Expand All @@ -21,12 +27,9 @@ end_block: ~
# with the `--indexer_net_config` option.
fuel_client: ~

# The contract_id specifies which particular contract you would like your index to subscribe to.
contract_id: fuel1q6sj2srt0u40jdqg2lvvnspyuhse9rs2s2fv9nmv0hqjcrdc7sqsfpwv9x

# The graphql_schema field contains the file path that points to the GraphQL schema for the
# given index.
graphql_schema: examples/greetings/greetings-indexer/schema/greetings_indexer.schema.graphql
schema: examples/greetings/greetings-indexer/schema/greetings_indexer.schema.graphql

# The module field contains a file path that points to code that will be run as an executor inside
# of the indexer.
Expand All @@ -36,4 +39,10 @@ module:

# The resumable field contains a boolean that specifies whether or not the indexer should, synchronise
# with the latest block if it has fallen out of sync.
resumable: true
resumable: true

# Indexer predicate configuration.
predicates:

# Template commitments (hashes) of the bytecode of predicates used by this indexer.
templates: ~
24 changes: 16 additions & 8 deletions examples/hello-world/hello-world/hello_world.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace: fuellabs
# The identifier field is used to identify the given index.
identifier: hello_world

# The abi option is used to provide a link to the Sway JSON ABI that is generated when you
# build your project.
abi: ~
# Indexer contract configuration.
contract:

# File paths to the contract JSON ABIs that are generated when you build your Sway contracts.
abi: ~

# Specifies which particular contracts you would like your indexer to subscribe to.
subscriptions: ~

# The particular start block after which you'd like your indexer to start indexing events.
start_block: ~
Expand All @@ -21,12 +26,9 @@ end_block: ~
# with the `--indexer_net_config` option.
fuel_client: ~

# The contract_id specifies which particular contract you would like your index to subscribe to.
contract_id: ~

# The graphql_schema field contains the file path that points to the GraphQL schema for the
# given index.
graphql_schema: examples/hello-world/hello-world/schema/hello_world.schema.graphql
schema: examples/hello-world/hello-world/schema/hello_world.schema.graphql

# The module field contains a file path that points to code that will be run as an executor inside
# of the indexer.
Expand All @@ -36,4 +38,10 @@ module:

# The resumable field contains a boolean that specifies whether or not the indexer should, synchronise
# with the latest block if it has fallen out of sync.
resumable: true
resumable: true

# Indexer predicate configuration.
predicates:

# Template commitments (hashes) of the bytecode of predicates used by this indexer.
templates: ~
2 changes: 1 addition & 1 deletion packages/fuel-indexer-api-server/src/uses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ pub(crate) async fn verify_signature(
}
_ => {
error!("Unsupported authentication strategy.");
unimplemented!();
unimplemented!("Unsupported authentication strategy.");
}
}
}
Expand Down
Loading

0 comments on commit 6b0c530

Please sign in to comment.