You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To illustrate how Komet can be used to test Soroban contracts, let's look at a simple example. We'll be working with a basic contract called adder, which features a single function that adds two numbers and returns their sum. In Komet, we write our tests as contracts that interact with the contract we want to test. For this example, we will create a test_adder contract to verify the behavior of the adder contract.
52
-
53
-
The project structure for this example looks like this:
54
-
55
-
```
56
-
.
57
-
├── contracts
58
-
│ ├── adder
59
-
│ │ ├── src
60
-
│ │ │ └── lib.rs
61
-
│ │ └── Cargo.toml
62
-
│ └── test_adder
63
-
│ ├── src
64
-
│ │ ├── lib.rs
65
-
│ │ └── komet.rs
66
-
│ ├── Cargo.toml
67
-
│ └── kasmer.json
68
-
├── Cargo.toml
69
-
└── README.md
70
-
```
71
-
72
-
#### The `adder` Contract
73
-
74
-
The adder contract is a simple, stateless contract with a single endpoint, add. This function takes two numbers as input and returns their sum. The result is returned as a u64 to avoid overflows. Since the contract doesn't maintain any internal state or use storage, its behavior is straightforward and purely based on the inputs provided.
75
-
76
-
```rust
77
-
#![no_std]
78
-
usesoroban_sdk::{contract, contractimpl, Env};
79
-
80
-
#[contract]
81
-
pubstructAdderContract;
82
-
83
-
#[contractimpl]
84
-
implAdderContract {
85
-
pubfnadd(_env:Env, first:u32, second:u32) ->u64 {
86
-
firstasu64+secondasu64
87
-
}
88
-
}
89
-
```
90
-
91
-
### 2.2 Writing The Test Contract
92
-
93
-
Test contracts typically have an init function for setting up the initial state, such as deploying contracts or preparing the blockchain environment. They also include functions with names starting with test_ to define properties and run test cases against the contract being tested. Test contracts have special abilities that normal contracts do not, provided by our framework through custom WebAssembly hooks. These hooks, declared as extern functions in komet.rs, enable advanced operations like contract creation and state manipulation.
94
-
95
-
#### Setting the Initial State: The init Function
96
-
97
-
In the context of testing the adder contract, the init function is specifically responsible for deploying the adder contract and saving its address within the test contract's storage.
We are using the `create_contract` function from `komet.rs` for deploying a contract with a specified address and a Wasm hash. The hash represents the Wasm code of the target contract (in this case, the `adder` contract).
116
-
117
-
The contracts passed to `init` are specified in the `kasmer.json` file, where we provide the relative path to each contract. In this example, there is only one contract—the `adder` contract—but more complex tests may require multiple contracts. Komet locates and compiles each contract, registers the Wasm module, and passes its hash to the `init` function.
118
-
119
-
```json
120
-
{
121
-
"contracts": [
122
-
"../adder"
123
-
]
124
-
}
125
-
```
126
-
127
-
Komet will locate the `adder` contract, compile it to Wasm, and register the resulting Wasm module automatically.
128
-
129
-
If the contract requires a custom build process or is already precompiled, you can provide the path to the compiled Wasm file instead:
In Komet, test cases are defined as contract endpoints with names starting with the test_ prefix. These endpoints specify properties of the contract being tested and return a boolean to indicate whether the test passed or failed.
143
-
144
-
For instance, in the test_adder contract, the test_add function verifies the adder contract's behavior by using its address—set up in the init function—to invoke the add method and check whether the adder contract correctly computes the sum of two numbers.
145
-
146
-
```rust
147
-
implTestAdderContract {
148
-
// Initialization code...
149
-
150
-
pubfntest_add(env:Env, x:u32, y:u32) ->bool {
151
-
// Retrieve the address of the `adder` contract from storage
This indicates that Komet discovered the `test_add` function and successfully executed the test using randomized inputs. By default, Komet runs each test 100 times. You can specify a different number of iterations using the `--max-examples` argument:
191
-
192
-
```bash
193
-
komet test --max-examples 500
194
-
```
195
-
196
-
This runs the test 500 times, allowing for more thorough fuzzing when needed.
197
-
198
-
3. Running Tests with Symbolic Execution (Proving)
199
-
200
-
To run tests with symbolic execution, which verifies the contract's behavior for all possible inputs, use the following command:
201
-
202
-
```bash
203
-
komet prove run
204
-
```
205
-
206
-
This method will run the proof for all test functions in the contract. It ensures that the property being tested holds true across all input combinations, providing thorough verification of the contract's correctness.
207
-
208
-
Additionally, you can explore more proving options by using the `--help` flag to see available commands and arguments:
COMMAND Proof command to run. One of (run, view, view-node, remove-node)
216
-
217
-
options:
218
-
-h, --help show this help message and exit
219
-
--always-allocate
220
-
--node NODE
221
-
--proof-dir PROOF_DIR
222
-
Output directory for proofs
223
-
--bug-report BUG_REPORT
224
-
Bug report directory for proofs
225
-
--extra-module EXTRA_MODULE
226
-
Extra module with user-defined lemmas to include for verification (which must import KASMER module).Format is <file>:<module name>.
227
-
--id ID Name of the testfunctionin the testing contract
228
-
--wasm WASM Use a specific contract wasm file instead
229
-
--directory DIRECTORY, -C DIRECTORY
230
-
The working directory for the command (defaults to the current working directory).
231
-
```
232
-
233
-
After running the proof with the `--proof-dir` option, you can use the `view` command to inspect the proof tree and examine the details of symbolic execution.
47
+
### [2.1. Komet Example: Testing the `adder` Contract](guides/komet-example/)
0 commit comments