-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dojo-lang): add bytearray_hash
macro
#2946
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1930819
feat(namespace): add poseidon_hash_string macro and related tests
bengineer42 d26aceb
fix(world-storage): retrieve class hash using syscall for contract re…
bengineer42 46855d3
fix: avoid double declaration of plugin + renamings
glihm d32e89a
merge main
glihm 61a04a9
chore: bump scarb
glihm 144c714
tests: use new name in tests
glihm 9a80852
tests: regenerate test db
glihm 6f38ac2
tests: fix rename of macro
glihm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bengineer42 fyi the issue was that you were trying to add 2 times the
BuiltinDojoPlugin
. This was causing the weird scarb issue on events, which is not related at all. But this was the reason.