tangle-network / gadget

A framework for building modular AVS and Tangle Blueprints: https://docs.tangle.tools/developers/blueprints.
https://tangle.tools
Apache License 2.0
12 stars 3 forks source link

[TASK] Event listening impls and macro improvement #307

Closed drewstone closed 1 week ago

drewstone commented 3 weeks ago

Overview

Custom event listeners should be supported. Using a trait to unify different event listeners for insertion into the macro should be explored. EVM event listeners should be supported on Tangle for arbitrary hooks (not just jobs, possibly reports).

Event Pushing Trait

Should be generic for any blockchain (EVM/Substrate/weather API)

trait EventListner {
    type Event;
    push_event() -> Event;
}

Custom Subscription-like / Interval Listener

struct EventSecondListener { last_timestamp: Date }
impl EventListener for EverySecond {
    push_event() -> Event {
        // Has 1 second passed since the last event? Return something
    }
}

#[job(
    params(_)
    result(_)
    event_listener(EverySecondListener)
)]
pub get_oracle_price_usdt_eth() -> ... {
    // Call Coinbase API, get the USDT_ETH price
    // Submit onchain OR run MPC over it OR generate a zk-TLS proof.
}

/// Let's say I have service that I want running but it could be easy for someone to fake it (to not run it).
/// In order to ensure that they're running this service, I (Blueprint developer) want to randomly challenge
/// The operators to complete a task. In order to know if the operator has been randomly selected, they
/// must listen ever 1 minute and check if they're selected for the random challenge.
///
/// This is within the realm of online/uptime verification checks.
#[subscriptable(
    params(_)
    result(_)
    event_listener(EveryMinuteListener)
)]
pub listen_for_random_challenge() -> ... {
    // Random challenges rely on leader election process, i.e. "Who is the leader?"
    // This is often done using a verifiable random function (VRF) but other solutions should be explored

    // Use cases
    // - Check if an RPC provider can post some data about their recent requests fulfillments
    // - Webb `protocol-solidity` relayer to prove they've been relaying things
    // - Do a random FaaS (AI inference)
    // - Do a health check, have this operator submit a heartbeat.
}

Custom Report Event Listener

struct JobResultListener
impl EventListener for JobResultListener {
    push_event() -> Event {
        // Get the next job result event and return it
    }
}

#[report(
    params(_)
    result(_)
    event_handler(JobResultListener)
)]
pub report_invalid_job_result(result) -> ... {
    // Check job result and validate data
    // If it's incorrect decide on the return value of this function
    // Should I return a return a `Transactable(EVM/Substrate)`
}

Build a blockchain

#[subscriptable(
    params(_)
    result(_)
    event_listener(Every6SecondsListener)
)]
pub make_a_block() -> ... {
    // Check if it's my turn to make a block
}