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

[Bug] Cannot get event using event handle if nested within resource #8981

Open
banool opened this issue Jul 6, 2023 · 4 comments
Open

[Bug] Cannot get event using event handle if nested within resource #8981

banool opened this issue Jul 6, 2023 · 4 comments
Labels
bug Something isn't working stale-exempt Prevents issues from being automatically marked and closed as stale

Comments

@banool
Copy link
Contributor

banool commented Jul 6, 2023

Summary

If you have an event handle and it isn't a top level field in a key struct in your Move module (aka a resource in storage) then you cannot read those events with this endpoint: https://fullnode.devnet.aptoslabs.com/v1/spec#/operations/get_events_by_event_handle.

Imagine you have this:

struct MyStruct has key {
    other: OtherStruct
}

struct OtherStruct has store {
    handle: EventHandle<Blah>
}

Neither of these work:

http://127.0.0.1:8080/v1/accounts/0x1/events/0x123::mymodule::OtherStruct/handle
http://127.0.0.1:8080/v1/accounts/0x1/events/0x123::mymodule::MyStruct/other.handle

Repro

Publish this Move module:

module addr::testing {
    use std::signer;
    use aptos_framework::event::{Self, EventHandle};
    use aptos_framework::account;

    struct MyEvent has store, drop {
        caller: address,
    }

    struct OtherStruct has store {
        handle: EventHandle<MyEvent>
    }

    struct MyStruct has key {
        other: OtherStruct
    }

    public entry fun testing(caller: &signer) acquires MyStruct {
        let caller_addr = signer::address_of(caller);
        if (!exists<MyStruct>(caller_addr)) {
            let my_struct = MyStruct {
                other: OtherStruct {
                    handle: account::new_event_handle<MyEvent>(caller),
                }
            };
            move_to(caller, my_struct);
        };

        let my_struct = borrow_global_mut<MyStruct>(caller_addr);
                
        event::emit_event(&mut my_struct.other.handle, MyEvent { caller: caller_addr });
    }
}
aptos move publish --named-addresses addr=`yq .profiles.default.account < .aptos/config.yaml` --assume-yes

Run the testing function:

aptos move run --function-id `yq .profiles.default.account < .aptos/config.yaml`::testing::testing --assume-yes

See that you can't read events:

$ curl http://127.0.0.1:8080/v1/accounts/0x`yq .profiles.default.account < .aptos/config.yaml`/events/0x`yq .profiles.default.account < .aptos/config.yaml`::testing::OtherStruct/handle
{"message":"Resource not found by Address(0x9712f422a9ed83bee4143d53e1a8598d089ca8af9c0d83248989308b80b7054e), Struct tag(0x9712f422a9ed83bee4143d53e1a8598d089ca8af9c0d83248989308b80b7054e::testing::OtherStruct) and Ledger version(397)","error_code":"resource_not_found","vm_error_code":null}
$ curl http://127.0.0.1:8080/v1/accounts/0x`yq .profiles.default.account < .aptos/config.yaml`/events/0x`yq .profiles.default.account < .aptos/config.yaml`::testing::MyStruct/other.handle
{"message":"failed to parse path `field_name`: failed to parse \"string(IdentifierWrapper)\": Invalid identifier 'other.handle'","error_code":"web_framework_error","vm_error_code":null}
@banool banool added the bug Something isn't working label Jul 6, 2023
@banool
Copy link
Contributor Author

banool commented Jul 6, 2023

Given this hasn't been a problem until now, we can probably leave this as something @lightmark will address with v2 events.

@alnoki
Copy link
Contributor

alnoki commented Jul 14, 2023

@banool @lightmark Can the solution to this issue also include lookup inside tables?

struct MyStruct has key {
    other: OtherStruct
}

struct OtherStruct has store {
    table_of_handles: Table<u64, EventHandle<Blah>>
}

Right now the only solution for this is to manually provide a view function that looks up the event handle in the table, then gets the GUID from the event handle (event::guid()), then returns the address/creation number from the GUID (guid::creator_address(), guild::creation_num()), such that the output of the view function can be fed into the event by creation number API (https://fullnode.devnet.aptoslabs.com/v1/spec#/operations/get_events_by_creation_number)

@lightmark
Copy link
Contributor

@alnoki yes, imagine that is similar to solidity. Event is associated with module

@davidiw
Copy link
Contributor

davidiw commented Jul 16, 2023

This was ... actually known since the beginning 😬 -- but we didn't really have any examples that needed it, so it got punted. I think we should mark this as not going to fix given the complexity in doing so and that we have a new event framework emerging shortly.

Open to hearing from others.

@sausagee sausagee added the stale-exempt Prevents issues from being automatically marked and closed as stale label Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale-exempt Prevents issues from being automatically marked and closed as stale
Projects
Status: 📋 Backlog
Development

No branches or pull requests

5 participants