Skip to content

Commit

Permalink
📝 Update docs structure, design
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Aug 20, 2024
1 parent 41066bd commit 18c3ed6
Show file tree
Hide file tree
Showing 26 changed files with 314 additions and 204 deletions.
2 changes: 1 addition & 1 deletion documentation/docs/fuzzing/fuzzing-examples.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Fuzzing Examples
# Trident Examples

### Hello World! Example
- [hello_world](https://github.com/Ackee-Blockchain/trident/tree/master/examples/fuzz-tests/hello_world)
Expand Down
22 changes: 0 additions & 22 deletions documentation/docs/fuzzing/fuzzing-introduction.md

This file was deleted.

1 change: 0 additions & 1 deletion documentation/docs/fuzzing/fuzzing-run-debug.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Run and Debug

## Run
Once you have finished the implementation of the Fuzz Test, you can run the Test as follows:

```bash
Expand Down
6 changes: 0 additions & 6 deletions documentation/docs/fuzzing/howto/fuzzing-howto-p0.md

This file was deleted.

21 changes: 0 additions & 21 deletions documentation/docs/fuzzing/howto/fuzzing-howto-p1.md

This file was deleted.

20 changes: 0 additions & 20 deletions documentation/docs/fuzzing/howto/fuzzing-howto-p2.md

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to use Arbitrary crate
# Structured Data

The [Arbitrary](https://docs.rs/arbitrary/latest/arbitrary/) crate in Rust is used for generating well-typed, structured instances of data from raw byte buffers, making it useful for fuzzing by producing random but structured data for tests.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# How to use Custom Data Types
# Custom Data Types

If you use Custom Types as Instruction data arguments, you may encounter a problem that the Custom Type does not implement

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Customize instructions generation
# Instruction sequences

It is possible to customize how the instructions are generated and which instructions will be executed at the beginning (`pre_ixs`), in the middle (`ixs`) and at the end (`post_ixs`) of each fuzz iteration. This can be useful for example if your program needs an initialization or you want to fuzz some specific program state.

- Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/test_fuzz.rs` and implement the corresponding optional method of the `FuzzDataBuilder<FuzzInstruction>` trait. For example, in order to always call the `initialize` instruction, modify the trait's implementation as follows:
!!! tip

- Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/test_fuzz.rs` and implement the corresponding optional method of the `FuzzDataBuilder<FuzzInstruction>` trait. For example, in order to always call the `initialize` instruction, modify the trait's implementation.

```rust
impl FuzzDataBuilder<FuzzInstruction> for MyFuzzData {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Define invariants checks
# Invariants checks

After each successful instruction execution, the `check()` method is called to check the account data invariants.

For each instruction, you can compare the account data **before** and **after** the instruction execution such as:
!!! tip

For each instruction, you can compare the account data **before** and **after** the instruction execution.

```rust
fn check(
Expand Down
31 changes: 31 additions & 0 deletions documentation/docs/fuzzing/writing-fuzz-test/accounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Accounts to reuse

{{ config.site_name }} helps you to generate only a limited amount of pseudo-random accounts and reuse them in the instructions.

??? note

**Details:**

Always generating only random accounts would **in most cases lead to a situation where the fuzzer would be stuck because the accounts would be almost every time rejected by your Anchor program**. Therefore it is necessary to specify, what accounts should be used and also limit the number of newly created accounts to reduce the space complexity.


!!! tip

Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/fuzz_instructions.rs` file and complete the pre-generated `FuzzAccounts` structure. It contains all accounts used in your program. You have to determine if the account is a:

- Signer
- PDA
- Token Account
- Program account

Then use the corresponding AccountsStorage.


```rust
pub struct FuzzAccounts {
signer: AccountsStorage<Keypair>,
some_pda: AccountsStorage<PdaStore>,
token_vault: AccountsStorage<TokenStore>,
mint: AccountsStorage<MintStore>,
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fuzz test-only
# Initialize Fuzz Test

If you are interested only in generating templates for fuzz tests run
For initialization of workspace for fuzz tests, call:
```bash
trident init fuzz
```
Expand All @@ -21,3 +21,10 @@ project-root
├── Trident.toml
└── ...
```

## Add new Fuzz Test

If you wish to generate template for a new fuzz test, call:
```bash
trident fuzz add
```
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Specify instruction accounts
# Instruction accounts

{{ config.site_name }} fuzzer generates random indexes of accounts to use in each instruction. Each created account is saved in the global `FuzzAccounts` structure which helps you to reuse already existing accounts across all instructions.
{{ config.site_name }} generates random indexes of accounts to use in each instruction. Each created account is saved in the global FuzzAccounts structure which helps you to reuse already existing accounts across all instructions.

You are required to define, how these accounts should be created and which accounts should be passed to an instruction. It is done using the `IxOps` trait and its method `get_accounts`.
!!! tip

You are required to define, how these accounts should be created and which accounts should be passed to an instruction. It is done using the `IxOps` trait and its method `get_accounts`.

- Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/fuzz_instructions.rs` file and complete the pre-generated `get_accounts` methods for each instruction.

- Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/fuzz_instructions.rs` file and complete the pre-generated `get_accounts` methods for each instruction such as:

```rust
fn get_accounts(
Expand Down Expand Up @@ -35,7 +38,10 @@ fn get_accounts(
Ok((signers, acc_meta))
}
```
Notice especially the helper method `fuzz_accounts.<account_name>.get_or_create_account` that is used to create a Keypair or retrieve the Public key of the already existing account.

!!! note

Notice especially the helper method `fuzz_accounts.<account_name>.get_or_create_account` that is used to create a Keypair or retrieve the Public key of the already existing account.

## Create an arbitrary account
The `AccountsStorage<T>` type provides an implementation of the `get_or_create_account` method that helps you create new or read already existing accounts. There are different implementations for different types of storage (`Keypair`, `TokenStore`, `MintStore`, `PdaStore`) to simplify the creation of new accounts.
Expand Down
22 changes: 22 additions & 0 deletions documentation/docs/fuzzing/writing-fuzz-test/instruction-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Instruction data

{{ config.site_name }} generates random instruction data for you.

!!! tip

Currently, it is however required, that you manually assign the random fuzzer data to the instruction data. It is done using the `IxOps` trait and its method `get_data`.

- Go to the `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/fuzz_instructions.rs` file and complete the pre-generated `get_data` methods for each instruction.

```rust
fn get_data(
&self,
_client: &mut impl FuzzClient,
_fuzz_accounts: &mut FuzzAccounts,
) -> Result<Self::IxData, FuzzingError> {
let data = fuzz_example1::instruction::Invest {
amount: self.data.amount,
};
Ok(data)
}
```
25 changes: 25 additions & 0 deletions documentation/docs/fuzzing/writing-fuzz-test/writing-fuzz-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Writing Fuzz Test

!!! tip

At the current development stage, there are some manual steps required to start the Fuzzing Session. In principle:

**Prerequisites:**

- Add dependencies specific to your program to `trident-tests/fuzz_tests/Cargo.toml` (such as anchor-spl etc.).
- Add necessary `use` statements into `trident-tests/fuzz_tests/<FUZZ_TEST_NAME>/accounts_snapshots.rs` to import missing types.

**Writing Fuzz Tests**

1. Specify pseudo-random accounts to re-use [Accounts to re-use](./accounts.md).
2. Specify instruction data [Instruction Data](./instruction-data.md).
3. Specify instruction accounts [Instruction Accounts](./instruction-accounts.md).

!!! note

For better fuzzing results and experience you can also manually adjust the following:

1. Define Invariants checks [Invariants Checks](../writing-fuzz-test-extra/invariants-checks.md).
2. Specify instruction sequences[Instruction sequences](../writing-fuzz-test-extra/instruction-sequences.md).
3. Specify custom data types[Custom Data types](../writing-fuzz-test-extra/custom-data-types.md).
4. Well structured data[Arbitrary](../writing-fuzz-test-extra/arbitrary.md).
3 changes: 3 additions & 0 deletions documentation/docs/getting-started/docker-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Docker Image

TBD
63 changes: 63 additions & 0 deletions documentation/docs/getting-started/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Installation

This guide will walk you through the Installation process of Trident.

!!! important

**Prerequisite**

It is expected that you have installed:

- Rust ([Install Rust](https://www.rust-lang.org/tools/install))
- Solana CLI ([Install Solana CLI](https://docs.solanalabs.com/cli/install))
- Anchor Framework ([Install Anchor](https://www.anchor-lang.com/docs/installation))

For supported versions check the [Supported Versions](#supported-versions)

### Install System Dependencies

Update your package list and install the required packages:

```bash
sudo apt-get update
sudo apt-get install -y \
curl \
git \
build-essential \
pkg-config \
libssl-dev \
npm \
vim \
nano \
wget \
binutils-dev \
libunwind-dev \
lldb
```

### Install Trident and Honggfuzz

Install them using the following commands:


```bash
cargo install trident-cli
cargo install honggfuzz
```

### Supported versions

| {{ config.site_name }} CLI | Anchor | Solana | Rust | Honggfuzz |
|--------------|---------|----------|-----------------------|-----------------------|
| `v0.7.0` | `>=0.29.*`<sup>1</sup> | `^1.17.4` | `nightly` | `0.5.56` |
| `v0.6.0` | `>=0.29.*`<sup>1</sup> | `^1.17` | `nightly` | `0.5.55` |
| `v0.5.0` | `~0.28.*` | `=1.16.6` | - | - |
| `v0.4.0` | `~0.27.*` | `>=1.15` | - | - |
| `v0.3.0` | `~0.25.*` | `>=1.10` | - | - |
| `v0.2.0` | `~0.24.*` | `>=1.9` | - | - |

1. To use Trident with Anchor 0.29.0, run the following commands from your project's root directory after Trident initialization:
```bash
cargo update [email protected] --precise 0.29.0
cargo update [email protected] --precise 0.29.0
```
Loading

0 comments on commit 18c3ed6

Please sign in to comment.