Skip to content

Commit 86ba30f

Browse files
authoredDec 22, 2023
Merge pull request #778 from multiversx/development
Development to main
2 parents 98a0f62 + e76bf62 commit 86ba30f

File tree

17 files changed

+585
-152
lines changed

17 files changed

+585
-152
lines changed
 

‎docs/developers/data/composite-values.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Composite Values
44
---
55
[comment]: # (mx-abstract)
66

7-
We often need to group simple values into more complex ones, without splitting them into [several arguments](/developers/data/multi-value).
7+
We often need to group simple values into more complex ones, without splitting them into [several arguments](/developers/data/multi-values).
88

99
Here too we opted for an encoding that is, above all, very easy to read.
1010

‎docs/developers/meta/sc-config.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ allocator = "leaking"
4040
stack-size = "3 pages"
4141
features = ["example_feature_1", "example_feature_2"]
4242
kill-legacy-callback = true
43+
44+
[contracts.main.profile]
45+
codegen-units = 1
46+
opt-level = "z"
47+
lto = true
48+
debug = false
49+
panic = "abort"
50+
overflow-checks = false
4351
```
4452

4553
The settings are as follows:
4654
- `panic-message`
4755
- “Panic with message” is a feature very useful for debugging. It displays messages from regular Rust panics in a contract, at the cost of ~1.5kB of additional contract size. It is disabled by default, we advise against using it in production.
48-
- _values_: `true` | `false
56+
- _values_: `true` | `false`
4957
- _default_: `false`
5058
- `ei`
5159
- Configures the post-processor that checks the environment interface (EI) used by the built smart contract.
@@ -82,7 +90,44 @@ The settings are as follows:
8290
- _default_: `[]`
8391
- `kill-legacy-callback`
8492
- The framework has no way of knowing whether or not a certain smart contract variant actually needs the async callback code or not, so in rare situations it is necessary to forcefully remove it.
85-
- _values_: `true` | `false
93+
- _values_: `true` | `false`
94+
- _default_: `false`
95+
- `codegen-units`
96+
- Controls the number of "code generation units" a crate will be split into. Splitting a crate into multiple code generation units can have a significant impact on the compile time and code optimization of the crate.
97+
- From our experience it is of no particular impact to smart contract compilation.
98+
- _default_: `1`
99+
- `opt-level`
100+
- Controls the level of optimization that Rust will apply to the code during compilation.
101+
- By default we run with `s` or `z`, since for smart contracts bytecode size optimization is of the essence.
102+
- We also run `wasm-opt` after this optimization phase, so this only refers to part of the optimization.
103+
- _values_:
104+
- `0`: no optimizations;
105+
- `1`: basic optimizations;
106+
- `2`: some optimizations;
107+
- `3`: all optimizations;
108+
- `s`: optimize for binary size;
109+
- `z`: optimize for binary size, but also turn off loop vectorization.
110+
- _default_: `z`
111+
- `lto`
112+
- Enables link-time optimization for the release profile.
113+
- _values_: `true` | `false`
114+
- _default_: `true`
115+
- `debug`
116+
- Controls the amount of debug information included in the compiled binary.
117+
- This setting is not normally used, since wasm-opt erases any debug information anyway. Use the `build-dbg` to get debug information for the compiled contracts.
118+
- _values_: `true` | `false`
119+
- _default_: `false`
120+
- `panic`
121+
- Controls how smart contracts handles panics, which are unexpected errors that occur at runtime.
122+
- Using `"unwind"` is not tested as it makes little sense in a smart contract.
123+
- _values_:
124+
- `"unwind"`: unwind the stack in case of a panic;
125+
- `"abort"`: terminate the execution in case of a panic.
126+
- _default_: `"abort"`
127+
- `overflow_checks`
128+
- Controls whether it performs runtime checks for integer overflow. When enabled, it will insert additional checks into the generated code to detect and prevent integer overflow errors.
129+
- Note that overflow checks are normally turned off in production, but are useful when testing. The overflow checks are enabled by default when testing smart contracts using the debugger.
130+
- _values_: `true` | `false`
86131
- _default_: `false`
87132

88133

@@ -272,7 +317,7 @@ An _external view contract_ has a behavior different from that of a regular cont
272317
- A list of endpoint names to be added directly to this contract.
273318
- It bypasses the label system.
274319
- Can be useful if for some reason labels are missing in code or deemed too cumbersome.
275-
- Use the public endpoin names, not the rust function names.
320+
- Use the public endpoint names, not the Rust function names.
276321
- _values_: a list of endpoint names, e.g. `add-labels = ["myEndpoint1", "myEndpoint1"]`
277322
- _default_: `[]`
278323
- `labels-for-contracts`

‎docs/developers/meta/sc-meta-cli.md

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Paramameters:
5858
- `--ignore`
5959
- Ignore all directories with these names.
6060
- _default_: `target`.
61-
- `--no-abi-git-version
61+
- `--no-abi-git-version`
6262
- Skips loading the Git version into the ABI
6363
- `--target-dir-meta`
6464
- For the meta crates, allows specifying the target directory where the Rust compiler will build the intermediary files. Sharing the same target directory can speed up building multiple contract crates at once.
@@ -114,6 +114,9 @@ Paramameters:
114114
- `--to`
115115
- Overrides the version to upgrade to.
116116
- _default_: the last released version.
117+
- `--no-check`
118+
- By default `upgrade` compile checks the project after each major version upgrade. This is to allow developers that upgrade multiple versions to address issues with the upgrade before too many such issues get to accumulate. This feature can be turned off by the `--no-check` flag.
119+
- _default_: project is compiled.
117120

118121
[comment]: # (mx-context-auto)
119122

@@ -211,7 +214,9 @@ Paramameter:
211214

212215
### Calling `test-gen`
213216

214-
<!-- TODO: expand section and move to a separate page -->
217+
The `test-gen` tool is used to [generate boilerplate](/developers/testing/scenario/running-scenarios#auto-generating-the-boilerplate) code when [integrating JSON scenario files in a contract's Rust test suite](/developers/testing/scenario/running-scenarios#integration-in-rust).
218+
219+
In short:
215220

216221
Contracts often have JSON scenario tests associated with them, which normally reside in the `scenarios` folder, under the contract crate root.
217222

@@ -221,53 +226,7 @@ These integration tests come in two flavors:
221226
- Rust tests, that exclusively use the Rust debugger infrastructure;
222227
- VM tests that use the Go infrastructure.
223228

224-
An example:
225-
226-
```rust title="adder/tests/adder_scenario_rs_test.rs"
227-
use multiversx_sc_scenario::*;
228-
229-
fn world() -> ScenarioWorld {
230-
let mut blockchain = ScenarioWorld::new();
231-
blockchain.set_current_dir_from_workspace("contracts/examples/adder");
232-
233-
blockchain.register_contract("file:output/adder.wasm", adder::ContractBuilder);
234-
blockchain
235-
}
236-
237-
#[test]
238-
fn adder_rs() {
239-
world().run("scenarios/adder.scen.json");
240-
}
241-
242-
#[test]
243-
fn interactor_trace_rs() {
244-
world().run("scenarios/interactor_trace.scen.json");
245-
}
246-
```
247-
248-
```rust title="adder/tests/adder_scenario_go_test.rs"
249-
use multiversx_sc_scenario::*;
250-
251-
fn world() -> ScenarioWorld {
252-
ScenarioWorld::vm_go()
253-
}
254-
255-
#[test]
256-
fn adder_go() {
257-
world().run("scenarios/adder.scen.json");
258-
}
259-
260-
#[test]
261-
fn interactor_trace_go() {
262-
world().run("scenarios/interactor_trace.scen.json");
263-
}
264-
```
265-
266-
The `world()` definition is expected form the developer, but the tests themselves are generated and updated automatically when calling `sc-meta test-gen`.
267-
268-
:::caution
269-
The tool does not work well with code that is commented-out. In order to temporarily disable a test, annotate it with `#[ignore]`.
270-
:::
229+
Read more about JSON scenarios in smart contract projects [here](/developers/testing/scenario/running-scenarios).
271230

272231
Paramameters:
273232
- `--path`

‎docs/developers/overview.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ the execution of smart contract, information about ESDT transfers or built-in fu
143143

144144
| Name | Description |
145145
|-------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
146-
| [ESDT Operations Events](/developers/log-events/esdt-events) | ESDT operations, which encompass token creation, transfers, and other critical actions, generate log events. These events record sender and receiver addresses, token amounts, and operation types. |
147-
| [Execution Events](/developers/log-events/execution-events) | Execution events are dedicated to recording the status of transaction execution. They indicate whether a transaction was successfully executed or encountered an error. |
148-
| [Smart Contract Call Events](/developers/log-events/contract-call-events) | Smart contract calls often emit log events to report their execution status and results. These events typically include information such as the caller's address, the called function, and any other relevant data. |
149-
| [Smart Contract Deploy Events](/developers/log-events/contract-deploy-events) | Smart contract deployment and upgrade events are used to record when a smart contract is initially deployed or when it undergoes an upgrade. |
150-
| [System Delegation Events](/developers/log-events/system-delegation-events) | System delegation events are generated in response to interactions with the system delegation contract. |
146+
| [ESDT Operations Events](/developers/event-logs/esdt-events) | ESDT operations, which encompass token creation, transfers, and other critical actions, generate log events. These events record sender and receiver addresses, token amounts, and operation types. |
147+
| [Execution Events](/developers/event-logs/execution-events) | Execution events are dedicated to recording the status of transaction execution. They indicate whether a transaction was successfully executed or encountered an error. |
148+
| [Smart Contract Call Events](/developers/event-logs/contract-call-events) | Smart contract calls often emit log events to report their execution status and results. These events typically include information such as the caller's address, the called function, and any other relevant data. |
149+
| [Smart Contract Deploy Events](/developers/event-logs/contract-deploy-events) | Smart contract deployment and upgrade events are used to record when a smart contract is initially deployed or when it undergoes an upgrade. |
150+
| [System Delegation Events](/developers/event-logs/system-delegation-events) | System delegation events are generated in response to interactions with the system delegation contract. |
151151

152152

153153

‎docs/developers/scenario-reference/overview.md

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: concept
3+
title: Concept
4+
---
5+
6+
7+
[comment]: # (mx-context-auto)
8+
9+
## What is a scenario?
10+
11+
Let's think for a moment how an interaction with a blockchain might look like.
12+
13+
The only way to change the blockchain state is by sending transctions. We might also need to query some contracts in between sending these transactions. We might also query the blockchain state (balances, for instance) directly. Let's call these actions "steps". In a simulated environment, we need at least an additional step: initializing the sandbox.
14+
15+
Several steps form a scenario.
16+
17+
A scenario is any interaction with a blockchain, composed of one or more steps. These interactions might be:
18+
- real, completed on a real blockchain, a history of sorts;
19+
- programmed to be executed in the future;
20+
- simulated, but using real blockchain data;
21+
- simulated, using absurd or unrealistic data.
22+
23+
So it doesn't really matter if these steps are real or imagined. All that matters is that they obey the rules of the blockchain.
24+
25+
Because of their generality, it is natural to think of all blockchain interactions and black-box tests as such scenarios.
26+
27+
28+
[comment]: # (mx-context-auto)
29+
30+
## Scenario formats
31+
32+
The concept of scenario is not tied to a specific technology, or language.
33+
34+
Historically, they started out as JSON tests. But writing a lot of JSON by hand is very inconvenient and unproductive, so we came up with a very similar syntax in Rust. When we created the interactor framework, we found that the scenario model fits naturally to real interactions too.
35+
36+
:::info important
37+
Nowadays, we think that the JSON scenario format is best used for interoperability and replays, not for writing tests.
38+
39+
There are several ways to generate a scenario JSON file automatically, and we encourage developers to move away from writing them by hand.
40+
:::
41+
42+
The greatest benefit of the JSON format is that it is language-agnostic, and so it can be used with any of our backends.
43+
44+
45+
[comment]: # (mx-context-auto)
46+
47+
## Scenarios as tests
48+
49+
Scenarios also have syntax for checking transaction outputs and the blockchain state. This means that each scenario is also a test. Any failing check causes the scenario test to fail.
50+
51+
:::important What kind of tests are they?
52+
They are always **black-box** tests. They model real blockchain interactions, so there is no way for them to peek inside contracts and access their private functions.
53+
:::
54+
55+
56+
[comment]: # (mx-context-auto)
57+
58+
## Typed vs. untyped scenarios
59+
60+
Transaction data on the blockchain is not really typed. Arguments, storage, logs, etc. - they are all untyped at blockchain level. The types are only added by developers when writing contracts, to avoid bugs and make them safer to use.
61+
62+
But this means that scenarios in their most general form are also untyped. This fits JSON well, which is also (mostly) untyped.
63+
64+
It clearly becomes tiring, as a developer, to only be able to work with untyped and un-annotated data. There are two ways to overcome this:
65+
- Using a typed version of the scenarios. This is what we do when working with scenarios in Rust.
66+
- Using a specialized language to make values easier to read by developers.
67+
68+
These are the scenario value expressions, and they help us better express numbers, addresses, or even more complex data. Note that this does not add any type checks, it is a purely cosmetic affair. The format is detailed [here](/developers/testing/scenario/values-simple) and [here](/developers/testing/scenario/values-complex).
69+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
id: generating-scenarios
3+
title: Generating scenarios
4+
---
5+
[comment]: # (mx-abstract)
6+
7+
There are currently several ways to generate scenarios.
8+
9+
The combination of generating and running scenarios is very powerful, since it means tests written originally for one system can be run of different systems too.
10+
11+
This diagram shows all the currently possible paths.
12+
13+
```mermaid
14+
graph TD
15+
interact ----> blockchain["⚙️ Blockchain"]
16+
interact[Interactor] -->|trace| json
17+
manual[Manually implemented] --> json["JSON Scenario
18+
<i>*.scen.json</i>"]
19+
bb[BlackBox Test] -->|trace| json
20+
wb[WhiteBox Test] -.->|trace| json
21+
json --> test-go["Generated
22+
<i>*_scenario_go_test.rs</i>"] --> vm-go["⚙️ Go VM"]
23+
json --> test-rs["Generated
24+
<i>*_scenario_rs_test.rs</i>"] --> vm-rust["⚙️ Rust VM (Debugger)"]
25+
bb --> vm-rust
26+
wb --> vm-rust
27+
```
28+
29+
More details about this coming soon.
30+

0 commit comments

Comments
 (0)