-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dojo-lang): add
bytearray_hash
macro (#2946)
* feat(namespace): add poseidon_hash_string macro and related tests * fix(world-storage): retrieve class hash using syscall for contract resources and fmting * fix: avoid double declaration of plugin + renamings * chore: bump scarb * tests: use new name in tests * tests: regenerate test db * tests: fix rename of macro --------- Co-authored-by: glihm <[email protected]>
- Loading branch information
1 parent
94e6e16
commit 8c484fd
Showing
14 changed files
with
191 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
51 changes: 51 additions & 0 deletions
51
crates/dojo/core-cairo-test/src/tests/expanded/bytearray_hash.cairo
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,51 @@ | ||
use core::poseidon::poseidon_hash_span; | ||
|
||
#[test] | ||
fn test_bytearray_hash() { | ||
let bytes: ByteArray = "foo"; | ||
let hash = bytearray_hash!("foo"); | ||
let mut array = array![]; | ||
bytes.serialize(ref array); | ||
let computed = poseidon_hash_span(array.span()); | ||
assert_eq!(computed, hash); | ||
} | ||
|
||
#[test] | ||
fn test_bytearray_hash_empty() { | ||
let bytes: ByteArray = ""; | ||
let hash = bytearray_hash!(""); | ||
let mut array = array![]; | ||
bytes.serialize(ref array); | ||
let computed = poseidon_hash_span(array.span()); | ||
assert_eq!(computed, hash); | ||
} | ||
|
||
#[test] | ||
fn test_bytearray_hash_31() { | ||
let bytes: ByteArray = "0123456789012345678901234567890"; | ||
let hash = bytearray_hash!("0123456789012345678901234567890"); | ||
let mut array = array![]; | ||
bytes.serialize(ref array); | ||
let computed = poseidon_hash_span(array.span()); | ||
assert_eq!(computed, hash); | ||
} | ||
|
||
#[test] | ||
fn test_bytearray_hash_long() { | ||
let bytes: ByteArray = "0123456789012345678901234567890foo"; | ||
let hash = bytearray_hash!("0123456789012345678901234567890foo"); | ||
let mut array = array![]; | ||
bytes.serialize(ref array); | ||
let computed = poseidon_hash_span(array.span()); | ||
assert_eq!(computed, hash); | ||
} | ||
|
||
#[test] | ||
fn test_bytearray_hash_ne() { | ||
let bytes: ByteArray = "foo"; | ||
let hash = bytearray_hash!("bar"); | ||
let mut array = array![]; | ||
bytes.serialize(ref array); | ||
let computed = poseidon_hash_span(array.span()); | ||
assert_ne!(computed, hash); | ||
} |
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
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,63 @@ | ||
use cairo_lang_defs::patcher::PatchBuilder; | ||
use cairo_lang_defs::plugin::{ | ||
InlineMacroExprPlugin, InlinePluginResult, MacroPluginMetadata, NamedPlugin, PluginDiagnostic, | ||
PluginGeneratedFile, | ||
}; | ||
use cairo_lang_defs::plugin_utils::unsupported_bracket_diagnostic; | ||
use cairo_lang_diagnostics::Severity; | ||
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode}; | ||
use dojo_types::naming; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct BytearrayHashMacro; | ||
|
||
impl NamedPlugin for BytearrayHashMacro { | ||
const NAME: &'static str = "bytearray_hash"; | ||
} | ||
|
||
impl InlineMacroExprPlugin for BytearrayHashMacro { | ||
fn generate_code( | ||
&self, | ||
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup, | ||
syntax: &ast::ExprInlineMacro, | ||
_metadata: &MacroPluginMetadata<'_>, | ||
) -> InlinePluginResult { | ||
let ast::WrappedArgList::ParenthesizedArgList(arg_list) = syntax.arguments(db) else { | ||
return unsupported_bracket_diagnostic(db, syntax); | ||
}; | ||
|
||
let args = arg_list.arguments(db).elements(db); | ||
|
||
if args.len() != 1 { | ||
return InlinePluginResult { | ||
code: None, | ||
diagnostics: vec![PluginDiagnostic { | ||
stable_ptr: syntax.stable_ptr().untyped(), | ||
message: "Invalid arguments. Expected \"bytearray_hash!(\"long string\")\"" | ||
.to_string(), | ||
severity: Severity::Error, | ||
}], | ||
}; | ||
} | ||
|
||
let bytearray = &args[0].as_syntax_node().get_text(db).replace('\"', ""); | ||
|
||
let bytearray_hash = naming::compute_bytearray_hash(bytearray); | ||
|
||
let mut builder = PatchBuilder::new(db, syntax); | ||
builder.add_str(&format!("{:#64x}", bytearray_hash)); | ||
|
||
let (code, code_mappings) = builder.build(); | ||
|
||
InlinePluginResult { | ||
code: Some(PluginGeneratedFile { | ||
name: "bytearray_hash_macro".into(), | ||
content: code, | ||
code_mappings, | ||
diagnostics_note: None, | ||
aux_data: None, | ||
}), | ||
diagnostics: vec![], | ||
} | ||
} | ||
} |
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
Oops, something went wrong.