Skip to content

Commit dbc5dab

Browse files
docs: Align BuiltinHintProcessor README with current cairo-vm API (#2274)
* docs: Align BuiltinHintProcessor README with current cairo-vm API * Update CHANGELOG.md * remove changelog --------- Co-authored-by: Gabriel Bosio <[email protected]>
1 parent a0ed06d commit dbc5dab

File tree

1 file changed

+38
-17
lines changed
  • docs/hint_processor/builtin_hint_processor

1 file changed

+38
-17
lines changed

docs/hint_processor/builtin_hint_processor/README.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ For this step, you will have to code your hint implementation as a Rust function
2727
The hint implementation must also follow a specific structure in terms of variable input and output:
2828
```rust
2929
fn hint_func(
30-
vm: &mut VM,
30+
vm: &mut VirtualMachine,
3131
exec_scopes: &mut ExecutionScopes,
3232
ids_data: &HashMap<String, HintReference>,
3333
ap_tracking: &ApTracking,
34-
constants: &HashMap<String, BigInt>,
35-
) -> Result<(), VirtualMachineError> {
34+
constants: &HashMap<String, Felt252>,
35+
) -> Result<(), HintError> {
3636
// Your implementation
3737
}
3838

@@ -43,12 +43,12 @@ For example, this function implements the hint "print(ids.a)":
4343

4444
```rust
4545
fn print_a_hint(
46-
vm: &mut VM,
46+
vm: &mut VirtualMachine,
4747
exec_scopes: &mut ExecutionScopes,
4848
ids_data: &HashMap<String, HintReference>,
4949
ap_tracking: &ApTracking,
50-
constants: &HashMap<String, BigInt>,
51-
) -> Result<(), VirtualMachineError> {
50+
constants: &HashMap<String, Felt252>,
51+
) -> Result<(), HintError> {
5252
let a = get_integer_from_var_name("a", vm, ids_data, ap_tracking)?;
5353
println!("{}", a);
5454
Ok(())
@@ -61,22 +61,43 @@ let hint = HintFunc(Box::new(print_a_hint));
6161
Import the BuiltinHintProcessor from cairo-vm, instantiate it using the `new_empty()` method and the add your custom hint implementation using the method `add_hint`
6262
```rust
6363
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor
64+
use std::rc::Rc;
6465

6566
let mut hint_processor = BuiltinHintProcessor::new_empty();
66-
hint_processor.add_hint(String::from("print(ids.a)"), hint);
67+
hint_processor.add_hint(String::from("print(ids.a)"), Rc::new(hint));
68+
```
69+
You can also create a dictionary of `Rc<HintFunc>` and use the method `new()` to create a `BuiltinHintProcessor` with a preset dictionary of functions instead of using `add_hint()` for each custom hint. Note that `new()` also requires a `RunResources` instance:
70+
```rust
71+
use cairo_vm::vm::runners::cairo_runner::RunResources;
72+
use std::collections::HashMap;
73+
use std::rc::Rc;
74+
75+
let mut map: HashMap<String, Rc<HintFunc>> = HashMap::new();
76+
map.insert(String::from("print(ids.a)"), Rc::new(hint));
77+
let hint_processor = BuiltinHintProcessor::new(map, RunResources::default());
6778
```
68-
You can also create a dictionary of HintFunc and use the method `new()` to create a BuiltinHintProcessor with a preset dictionary of functions instead of using `add_hint()` for each custom hint.
6979

7080
#### Step 4: Run your cairo program using BuiltinHintProcessor extended with your hint
7181
Import the function cairo_run from cairo-vm, and run your compiled program
7282

7383
```rust
74-
use cairo_vm::cairo_run::cairo_run;
84+
use cairo_vm::cairo_run::{cairo_run, CairoRunConfig};
85+
use cairo_vm::types::layout_name::LayoutName;
86+
use std::fs::File;
87+
use std::io::{BufReader, Read};
88+
use std::path::Path;
89+
90+
let file = File::open(Path::new("custom_hint.json")).expect("Couldn't load file");
91+
let mut reader = BufReader::new(file);
92+
let mut buffer = Vec::<u8>::new();
93+
reader.read_to_end(&mut buffer).expect("Couldn't read file");
94+
7595
cairo_run(
76-
Path::new("custom_hint.json"),
77-
"main",
78-
false,
79-
false,
96+
&buffer,
97+
&CairoRunConfig {
98+
layout: LayoutName::all_cairo,
99+
..Default::default()
100+
},
80101
&mut hint_processor,
81102
)
82103
.expect("Couldn't run program");
@@ -125,11 +146,11 @@ There are also some helpers that dont depend on the hint processor used that can
125146
You can also find plenty of example implementations in the [builtin hint processor folder](../../../vm/src/hint_processor/builtin_hint_processor).
126147

127148
### Error Handling
128-
This API uses VirtualMachineError as error return type for hint functions, while its not possible to add error types to VirtualMachineError, you can use VirtualMachineError::CustomHint which receives a string and prints an error message with the format: "Hint Error: [your message]".
129-
For example, if we want our hint to return an error if ids.a is less than 0 we could write:
149+
This API uses `HintError` as error return type for hint functions. To return a custom error, use `HintError::CustomHint`, which receives a string and prints an error message with the format: "Hint Error: [your message]".
150+
For example, if we want our hint to return an error if `ids.a` is less than 0 we could write:
130151

131152
```rust
132-
if (get_integer_from_var_name("a", vm, ids_data, ap_tracking)? < 0){
133-
return Err(VirtualMachineError::CustomHint(String::from("a < 0")))
153+
if get_integer_from_var_name("a", vm, ids_data, ap_tracking)? < 0 {
154+
return Err(HintError::CustomHint("a < 0".into()));
134155
}
135156
```

0 commit comments

Comments
 (0)