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(zink-codegen): introduce derive macro for events #298

Merged
merged 24 commits into from
Dec 12, 2024

Conversation

malik672
Copy link
Contributor

@malik672 malik672 commented Dec 6, 2024

Resolves #255

  • generate ABIs for events and gathered events as enums
  • introduce util function for converting type to string

0x30bE4D758d86cfb1Ae74Ae698f2CF4BA7dC8d693

@malik672
Copy link
Contributor Author

malik672 commented Dec 6, 2024

@clearloop seems like log does not exist

Copy link
Member

@clearloop clearloop left a comment

Choose a reason for hiding this comment

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

Sorry for the late! was busy these days, almost finished! your new tests are totally correct!

at the current stage, you now need to handle the log function in the visitor, see https://github.com/zink-lang/zink/blob/main/codegen/src/visitor/log.rs#L62

If you need more details or have specific questions, free feel to pin me in the telegram group! In case this issue is a bit harder than the previous, just updated the bounty to large level!

zink/codegen/src/event.rs Outdated Show resolved Hide resolved
@malik672
Copy link
Contributor Author

malik672 commented Dec 10, 2024

Sorry for the late! was busy these days, almost finished! your new tests are totally correct!

at the current stage, you now need to handle the log function in the visitor, see https://github.com/zink-lang/zink/blob/main/codegen/src/visitor/log.rs#L62

If you need more details or have specific questions, free feel to pin me in the telegram group! In case this issue is a bit harder than the previous, just updated the bounty to large level!

@malik672
Copy link
Contributor Author

    pub fn log(&mut self, count: usize) -> Result<()> {
        let topics = Vec::<Vec<u8>>::default();
 
        let (offset, size) = self.data()?;
        let size = size as usize;
        let data = self.env.data.load(offset, size)?;

        tracing::debug!("log1 name: {:?}", data);

        for topic in topics {
            self.masm.push(&topic)?;
        }

    
        // 1. write data to memory
        let MemoryInfo { offset, size } = self.masm.memory_write_bytes(&data)?;

        // 3. prepare the offset and size of the data.
        self.masm.push(&size.to_ls_bytes())?;
        self.masm.push(&offset)?;

        // 4. run log for the data
        match count {
            0 => self.masm._log0(),
            1 => self.masm._log1(),
            2 => self.masm._log2(),
            3 => self.masm._log3(),
            4 => self.masm._log4(),
            _ => unreachable!("invalid topics"),
        }?;

        Ok(())
    }``` @clearloop 

@malik672
Copy link
Contributor Author

    pub fn log(&mut self, count: usize) -> Result<()> {
        let topics = Vec::<Vec<u8>>::default();
 
        let (offset, size) = self.data()?;
        let size = size as usize;
        let data = self.env.data.load(offset, size)?;

        tracing::debug!("log1 name: {:?}", data);

        for topic in topics {
            self.masm.push(&topic)?;
        }

    
        // 1. write data to memory
        let MemoryInfo { offset, size } = self.masm.memory_write_bytes(&data)?;

        // 3. prepare the offset and size of the data.
        self.masm.push(&size.to_ls_bytes())?;
        self.masm.push(&offset)?;

        // 4. run log for the data
        match count {
            0 => self.masm._log0(),
            1 => self.masm._log1(),
            2 => self.masm._log2(),
            3 => self.masm._log3(),
            4 => self.masm._log4(),
            _ => unreachable!("invalid topics"),
        }?;

        Ok(())
    }``` @clearloop 

@clearloop
Copy link
Member

clearloop commented Dec 10, 2024

    pub fn log(&mut self, count: usize) -> Result<()> {
        let data = self.env.data.load(offset, size)?;

        // 1. write data to memory
        let MemoryInfo { offset, size } = self.masm.memory_write_bytes(&data)?;

        // 3. prepare the offset and size of the data.
        self.masm.push(&size.to_ls_bytes())?;
        self.masm.push(&offset)?;

        // 4. run log for the data
        match count {
            0 => self.masm._log0(),
            1 => self.masm._log1(),
            2 => self.masm._log2(),
            3 => self.masm._log3(),
            4 => self.masm._log4(),
            _ => unreachable!("invalid topics"),
        }?;

        Ok(())

like this

@malik672
Copy link
Contributor Author

let (offset, size) = self.data()?;

quite similar to what i did first, made the tests fail

@malik672
Copy link
Contributor Author

malik672 commented Dec 10, 2024

    pub fn log(&mut self, count: usize) -> Result<()> {
        let data = self.env.data.load(offset, size)?;

        // 1. write data to memory
        let MemoryInfo { offset, size } = self.masm.memory_write_bytes(&data)?;

        // 3. prepare the offset and size of the data.
        self.masm.push(&size.to_ls_bytes())?;
        self.masm.push(&offset)?;

        // 4. run log for the data
        match count {
            0 => self.masm._log0(),
            1 => self.masm._log1(),
            2 => self.masm._log2(),
            3 => self.masm._log3(),
            4 => self.masm._log4(),
            _ => unreachable!("invalid topics"),
        }?;

        Ok(())

like this

it sorts normally:
--- STDERR: zink::log log1 ---
thread 'log1' panicked at tests/log.rs:25:5:
assertion left == right failed
left: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 111, 110, 103]
right: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 105, 110, 103]

@clearloop
Copy link
Member

clearloop commented Dec 10, 2024

assertion left == right failed left: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 111, 110, 103] right: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 105, 110, 103]

looks closed! now you can decode this output to see what exactly the log is!

let left = String::from_utf8_lossy(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 111, 110, 103]);
let right = String::from_utf8_lossy(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 105, 110, 103])

@clearloop
Copy link
Member

see my fix here: malik672/zink@sync...zink-lang:zink:cl/sync

@malik672 malik672 requested a review from clearloop December 11, 2024 10:14
Copy link
Member

@clearloop clearloop left a comment

Choose a reason for hiding this comment

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

Everything works now!

Now we need to wrap these log functions into our proc-macro

for the event abi part, let's handle it in a separated PR (#299), since it modifies logic in compiler

zink/codegen/Cargo.toml Outdated Show resolved Hide resolved
zink/codegen/src/event.rs Outdated Show resolved Hide resolved
examples/log.rs Outdated Show resolved Hide resolved
examples/log.rs Outdated Show resolved Hide resolved
examples/log.rs Outdated Show resolved Hide resolved
examples/log.rs Outdated Show resolved Hide resolved
examples/log.rs Outdated Show resolved Hide resolved
@clearloop
Copy link
Member

need to generate sugar functions in our proc-macro, the implementation till 583dbe2 makes sure our deep logic are working now, so the last thing is wrap them in proc-macro

@malik672
Copy link
Contributor Author

@clearloop might need your help here

Copy link
Member

@clearloop clearloop left a comment

Choose a reason for hiding this comment

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

Instead of quote!, construct the functions with the ImplItemType could make things more dynamic

zink/codegen/src/event.rs Outdated Show resolved Hide resolved
Copy link
Member

@clearloop clearloop left a comment

Choose a reason for hiding this comment

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

Thanks for contributing on this issue! however, I can not send you the full bounty because I solved the issue instead finally XD, it's unfair to other contributors who solve tricky issues independently, hope you can understand!

Apart from that, thank you again for spending time on digging the feature! see the transaction 🎁 : https://polygonscan.com/tx/0x1b588cab3bdb014fe247acf4c06f825515af6b8ee4c1df1c505de220f7af39b1

@clearloop clearloop merged commit 453604b into zink-lang:main Dec 12, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the declaration of events
2 participants