Skip to content
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: parse op_return #3

Merged
merged 7 commits into from
Dec 20, 2024
Merged

feat: parse op_return #3

merged 7 commits into from
Dec 20, 2024

Conversation

irisdv
Copy link
Collaborator

@irisdv irisdv commented Dec 6, 2024

Close #1

@irisdv irisdv requested a review from Th0rgal December 6, 2024 14:06
@irisdv irisdv added the 🔥 Ready for review This pull request needs a review label Dec 6, 2024
Copy link
Member

@Th0rgal Th0rgal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, special mention for the tests, it must have been painful. The code seems to be very similar to the Ord spec, which is not optimal for performances but is perfect for maintainability, so let's keep it that way unless we find out it really is too expensive. If that's the case a simple way to optimize would be to just panic instead of using flaws (and just not support transactions that contain one error). When you add the test for decode_integer(s), please add a comment with the gas result.

Note: Edicts (transfers) don't seem to be decoded correctly. We can use Hiro to write our own tests but I reported the issues on Ord github, see ordinals/ord#4122

src/parser.cairo Outdated
loop {
if current_index >= pubscript.len() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of checking if the index is > to the len, could we pop_front and match the first element? Because pubscript[current_index] is actually or shortcode for:

 if current_index >= pubscript.len() {
  panic
  else {
  return the value
  }

So we actually do the same check twice.

src/parser.cairo Outdated
// * 256 is << 8z
let size: u32 = ((pubscript[current_index]).into()
// so it doesn't exceed the max value
| ((pubscript[current_index + 1]).into() * 256)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the type of 256 here? I guess it should be u32 but It would be better to specify 256_u32 just to make sure we don't get something weird if the compiler updates the automatic type resolution

src/parser.cairo Outdated
let mut i = 0;

loop {
if i >= payload.len() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same advice, let's match on pop_front if possible

src/parser.cairo Outdated
if k >= 4 || j + k >= payload.len() {
break;
}
chunk.append(*payload[j + k]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like you need to backtrack here, so you can probably just pop_front 4 times to build a chunk

src/runestone/flag.cairo Outdated Show resolved Hide resolved
use alexandria_math::pow;
use alexandria_data_structures::byte_array_ext::ByteArrayIntoArrayU8;

pub fn decode_integers(payload: ByteArray) -> Result<Array<u128>, ByteArray> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function will be expensive. I see a few way to optimize it but if we really want to do it well it won't be trivial. Let's not focus on it at the moment, but please add some tests just so we can safely try faster implementations in the future.

@irisdv irisdv requested a review from Th0rgal December 10, 2024 15:24
Copy link
Member

@Th0rgal Th0rgal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small opti suggestions, you can merge it afterwards

if pubscript[0] == OP_RETURN && pubscript[1] == OP_13 {
// load payload
let payload = match load_payload(pubscript.clone()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to clone it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this error when I don't clone : Cannot desnap a non copyable type. note: Trait has no implementation in context: core::traits::Copy::<core::byte_array::ByteArray>.

src/parser.cairo Outdated
let size: u32 = opcode.into();
let mut i: usize = 0;
loop {
if i >= size {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand we only increment i by 1, so we can check i == size to break (it's much cheaper)

src/parser.cairo Outdated
};
let mut i: usize = 0;
loop {
if i >= size {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same trick here

src/parser.cairo Outdated

let mut i: usize = 0;
loop {
if i >= size {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here again

src/parser.cairo Outdated

let mut i: usize = 0;
loop {
if i >= size {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and finally here

@Th0rgal Th0rgal added ❌ Change request Change requested from reviewer and removed 🔥 Ready for review This pull request needs a review labels Dec 18, 2024
@irisdv irisdv requested a review from Th0rgal December 19, 2024 15:49
@irisdv irisdv added 🔥 Ready for review This pull request needs a review and removed ❌ Change request Change requested from reviewer labels Dec 19, 2024
Copy link
Member

@Th0rgal Th0rgal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Th0rgal Th0rgal merged commit 5399e01 into master Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔥 Ready for review This pull request needs a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parse OP_RETURN Runestone
2 participants