-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
print_latest_block_transactions
toy hint (#13)
* add print_latest_block_transactions toy hint * clean up * clean up * clean up * add Hint struct * clean up * fix start function * fix * add KakarotBuiltinHintProcessor * add default for KakarotBuiltinHintProcessor * Update crates/exex/src/hints.rs Co-authored-by: Clément Walter <[email protected]> * fix comments * fix conflicts --------- Co-authored-by: Clément Walter <[email protected]>
- Loading branch information
1 parent
bb7c013
commit ee5c038
Showing
6 changed files
with
294 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
func main() { | ||
%{ | ||
print_latest_block_transactions | ||
%} | ||
|
||
ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{ | ||
"attributes": [], | ||
"builtins": [], | ||
"compiler_version": "0.12.0", | ||
"data": [ | ||
"0x208b7fff7fff7ffe" | ||
], | ||
"debug_info": { | ||
"file_contents": {}, | ||
"instruction_locations": { | ||
"0": { | ||
"accessible_scopes": [ | ||
"__main__", | ||
"__main__.main" | ||
], | ||
"flow_tracking_data": { | ||
"ap_tracking": { | ||
"group": 0, | ||
"offset": 0 | ||
}, | ||
"reference_ids": {} | ||
}, | ||
"hints": [ | ||
{ | ||
"location": { | ||
"end_col": 7, | ||
"end_line": 4, | ||
"input_file": { | ||
"filename": "crates/exex/cairo-programs/transaction_hash.cairo" | ||
}, | ||
"start_col": 5, | ||
"start_line": 2 | ||
}, | ||
"n_prefix_newlines": 1 | ||
} | ||
], | ||
"inst": { | ||
"end_col": 8, | ||
"end_line": 6, | ||
"input_file": { | ||
"filename": "crates/exex/cairo-programs/transaction_hash.cairo" | ||
}, | ||
"start_col": 5, | ||
"start_line": 6 | ||
} | ||
} | ||
} | ||
}, | ||
"hints": { | ||
"0": [ | ||
{ | ||
"accessible_scopes": [ | ||
"__main__", | ||
"__main__.main" | ||
], | ||
"code": "print_latest_block_transactions", | ||
"flow_tracking_data": { | ||
"ap_tracking": { | ||
"group": 0, | ||
"offset": 0 | ||
}, | ||
"reference_ids": {} | ||
} | ||
} | ||
] | ||
}, | ||
"identifiers": { | ||
"__main__.main": { | ||
"decorators": [], | ||
"pc": 0, | ||
"type": "function" | ||
}, | ||
"__main__.main.Args": { | ||
"full_name": "__main__.main.Args", | ||
"members": {}, | ||
"size": 0, | ||
"type": "struct" | ||
}, | ||
"__main__.main.ImplicitArgs": { | ||
"full_name": "__main__.main.ImplicitArgs", | ||
"members": {}, | ||
"size": 0, | ||
"type": "struct" | ||
}, | ||
"__main__.main.Return": { | ||
"cairo_type": "()", | ||
"type": "type_definition" | ||
}, | ||
"__main__.main.SIZEOF_LOCALS": { | ||
"type": "const", | ||
"value": 0 | ||
} | ||
}, | ||
"main_scope": "__main__", | ||
"prime": "0x800000000000011000000000000000000000000000000000000000000000001", | ||
"reference_manager": { | ||
"references": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use crate::{db::Database, exex::DATABASE_PATH}; | ||
use cairo_vm::{ | ||
hint_processor::{ | ||
builtin_hint_processor::builtin_hint_processor_definition::{ | ||
BuiltinHintProcessor, HintFunc, | ||
}, | ||
hint_processor_definition::HintReference, | ||
}, | ||
serde::deserialize_program::ApTracking, | ||
types::exec_scope::ExecutionScopes, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
Felt252, | ||
}; | ||
use rusqlite::Connection; | ||
use std::{collections::HashMap, fmt, rc::Rc}; | ||
|
||
/// A wrapper around [`BuiltinHintProcessor`] to manage hint registration. | ||
pub struct KakarotHintProcessor { | ||
/// The underlying [`BuiltinHintProcessor`]. | ||
processor: BuiltinHintProcessor, | ||
} | ||
|
||
/// Implementation of `Debug` for `KakarotHintProcessor`. | ||
impl fmt::Debug for KakarotHintProcessor { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("KakarotHintProcessor") | ||
.field("extra_hints", &self.processor.extra_hints.keys()) | ||
.field("run_resources", &"...") | ||
.finish() | ||
} | ||
} | ||
|
||
impl Default for KakarotHintProcessor { | ||
fn default() -> Self { | ||
Self::new_empty().with_hint(print_tx_hint()) | ||
} | ||
} | ||
|
||
impl KakarotHintProcessor { | ||
/// Creates a new, empty [`KakarotHintProcessor`]. | ||
pub fn new_empty() -> Self { | ||
Self { processor: BuiltinHintProcessor::new_empty() } | ||
} | ||
|
||
/// Adds a hint to the [`KakarotHintProcessor`]. | ||
/// | ||
/// This method allows you to register a hint by providing a [`Hint`] instance. | ||
pub fn with_hint(mut self, hint: Hint) -> Self { | ||
self.processor.add_hint(hint.name.clone(), hint.func.clone()); | ||
self | ||
} | ||
|
||
/// Returns the underlying [`BuiltinHintProcessor`]. | ||
/// | ||
/// This allows the processor to be used elsewhere. | ||
pub fn build(self) -> BuiltinHintProcessor { | ||
self.processor | ||
} | ||
} | ||
|
||
/// A generic structure to encapsulate a hint with a closure that contains the specific logic. | ||
pub struct Hint { | ||
/// The name of the hint. | ||
name: String, | ||
/// The function containing the hint logic. | ||
func: Rc<HintFunc>, | ||
} | ||
|
||
impl fmt::Debug for Hint { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Hint").field("name", &self.name).field("func", &"...").finish() | ||
} | ||
} | ||
|
||
impl Hint { | ||
/// Creates a new [`Hint`] with the specified name and function logic. | ||
/// | ||
/// The logic is passed as a closure, which will be executed when the hint is triggered. | ||
pub fn new<F>(name: String, logic: F) -> Self | ||
where | ||
F: Fn( | ||
&mut VirtualMachine, | ||
&mut ExecutionScopes, | ||
&HashMap<String, HintReference>, | ||
&ApTracking, | ||
&HashMap<String, Felt252>, | ||
) -> Result<(), HintError> | ||
+ 'static | ||
+ Sync, | ||
{ | ||
Self { name, func: Rc::new(HintFunc(Box::new(logic))) } | ||
} | ||
} | ||
|
||
/// Public function to create the `print_latest_block_transactions` hint. | ||
/// | ||
/// This function returns a new `Hint` instance with the specified name and logic. | ||
pub fn print_tx_hint() -> Hint { | ||
Hint::new( | ||
String::from("print_latest_block_transactions"), | ||
|_vm: &mut VirtualMachine, | ||
_exec_scopes: &mut ExecutionScopes, | ||
_ids_data: &HashMap<String, HintReference>, | ||
_ap_tracking: &ApTracking, | ||
_constants: &HashMap<String, Felt252>| | ||
-> Result<(), HintError> { | ||
// Open the SQLite database connection. | ||
let connection = Connection::open(DATABASE_PATH) | ||
.map_err(|e| HintError::CustomHint(e.to_string().into_boxed_str()))?; | ||
|
||
// Initialize the database with the connection. | ||
let db = Database::new(connection) | ||
.map_err(|e| HintError::CustomHint(e.to_string().into_boxed_str()))?; | ||
|
||
// Retrieve the latest block from the database. | ||
let latest_block = db | ||
.latest_block() | ||
.map_err(|e| HintError::CustomHint(e.to_string().into_boxed_str()))?; | ||
|
||
// If a block was found, print each transaction hash. | ||
if let Some(block) = latest_block { | ||
for tx in &block.body { | ||
println!("Block: {}, transaction hash: {}", block.number, tx.hash()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod db; | ||
pub mod execution; | ||
pub mod exex; | ||
pub mod hints; |