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: blueprint pre-registration #625

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ members = [
"blueprints/*",
"crates/*",
]
exclude = [
"blueprints/incredible-squaring-symbiotic",
"blueprints/examples",
]

[workspace.package]
authors = ["Tangle Network"]
Expand Down
25 changes: 19 additions & 6 deletions blueprints/incredible-squaring-eigenlayer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ sol!(
);

#[tokio::test(flavor = "multi_thread")]
#[allow(clippy::needless_return)]
async fn test_eigenlayer_incredible_squaring_blueprint() {
run_eigenlayer_incredible_squaring_test(false, 1).await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_eigenlayer_pre_register_incredible_squaring_blueprint() {
run_eigenlayer_incredible_squaring_test(true, 1).await;
}

async fn run_eigenlayer_incredible_squaring_test(pre_register: bool, expected_responses: usize) {
setup_log();

// Initialize test harness
Expand All @@ -48,7 +56,6 @@ async fn test_eigenlayer_incredible_squaring_blueprint() {
let task_manager_address = deploy_task_manager(&harness).await;

// Spawn Task Spawner and Task Response Listener
let num_successful_responses_required = 3;
let successful_responses = Arc::new(Mutex::new(0));
let successful_responses_clone = successful_responses.clone();
let response_listener_address =
Expand Down Expand Up @@ -91,14 +98,20 @@ async fn test_eigenlayer_incredible_squaring_blueprint() {
let x_square_eigen = XsquareEigenEventHandler::new(contract.clone(), eigen_client_context);

let mut test_env = EigenlayerBLSTestEnv::new(
EigenlayerBLSConfig::new(Default::default(), Default::default()),
EigenlayerBLSConfig::new(Default::default(), Default::default())
.with_pre_registration(pre_register),
env.clone(),
)
.unwrap();
test_env.add_job(initialize_task);
test_env.add_job(x_square_eigen);
test_env.add_background_service(aggregator_context);

if pre_register {
// Run the runner once to register, since pre-registration is enabled
test_env.run_runner().await.unwrap();
}

tokio::spawn(async move {
test_env.run_runner().await.unwrap();
});
Expand All @@ -110,7 +123,7 @@ async fn test_eigenlayer_incredible_squaring_blueprint() {
let timeout_duration = Duration::from_secs(300);
let result = wait_for_responses(
successful_responses.clone(),
num_successful_responses_required,
expected_responses,
timeout_duration,
)
.await;
Expand All @@ -123,13 +136,13 @@ async fn test_eigenlayer_incredible_squaring_blueprint() {

match result {
Ok(Ok(())) => {
info!("Test completed successfully with {num_successful_responses_required} tasks responded to.");
info!("Test completed successfully with {expected_responses} tasks responded to.");
}
_ => {
panic!(
"Test failed with {} successful responses out of {} required",
successful_responses_clone.lock().unwrap(),
num_successful_responses_required
expected_responses
);
}
}
Expand Down
1 change: 1 addition & 0 deletions blueprints/incredible-squaring/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
src = "contracts/src"
out = "contracts/out"
libs = ["dependencies"]
remappings = ["incredible-squaring/=contracts/src/"]
auto_detect_remappings = true
solc_version = "0.8.20"

Expand Down
1 change: 0 additions & 1 deletion blueprints/incredible-squaring/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
incredible-squaring/=contracts/src/
tnt-core/=dependencies/tnt-core-0.1.0/src/
51 changes: 50 additions & 1 deletion blueprints/incredible-squaring/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blueprint_sdk::testing::utils::harness::TestHarness;
use blueprint_sdk::testing::utils::runner::TestEnv;
use blueprint_sdk::testing::utils::tangle::{InputValue, OutputValue, TangleTestHarness};
use color_eyre::Result;
use std::time::Duration;

#[tokio::test]
async fn test_incredible_squaring() -> Result<()> {
Expand All @@ -27,7 +28,7 @@ async fn test_incredible_squaring() -> Result<()> {
.unwrap();

// Setup service
let (mut test_env, service_id) = harness.setup_services().await?;
let (mut test_env, service_id, _blueprint_id) = harness.setup_services(true).await?;
test_env.add_job(handler);

tokio::spawn(async move {
Expand All @@ -47,3 +48,51 @@ async fn test_incredible_squaring() -> Result<()> {
assert_eq!(results.service_id, service_id);
Ok(())
}

#[tokio::test]
async fn test_pre_registration_incredible_squaring() -> Result<()> {
setup_log();

// Initialize test harness (node, keys, deployment)
let temp_dir = tempfile::TempDir::new()?;
let harness = TangleTestHarness::setup(temp_dir).await?;
let env = harness.env().clone();

// Create blueprint-specific context
let blueprint_ctx = MyContext {
env: env.clone(),
call_id: None,
};

// Initialize event handler
let handler = XsquareEventHandler::new(&env.clone(), blueprint_ctx)
.await
.unwrap();

// Setup service, but we don't register yet
let (mut test_env, _, blueprint_id) = harness.setup_services(false).await?;
test_env.add_job(handler);

// Run once for pre-registration
test_env.run_runner().await.unwrap();
let service_id = harness.request_service(blueprint_id).await.unwrap();

tokio::spawn(async move {
// Run again to actually run the service, now that we have registered
test_env.run_runner().await.unwrap();
});

tokio::time::sleep(Duration::from_secs(2)).await;

// Execute job and verify result
let _results = harness
.execute_job(
service_id,
0,
vec![InputValue::Uint64(5)],
vec![OutputValue::Uint64(25)],
)
.await?;

Ok(())
}
6 changes: 6 additions & 0 deletions crates/runners/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub trait BlueprintConfig: Send + Sync + 'static {
async fn requires_registration(&self, _env: &GadgetConfiguration) -> Result<bool, RunnerError> {
Ok(true)
}
/// Controls whether the runner should exit after registration
///
/// Returns true if the runner should exit after registration, false if it should continue
fn should_pre_register(&self) -> bool {
true // By default, runners exit after registration
}
}

impl BlueprintConfig for () {}
4 changes: 4 additions & 0 deletions crates/runners/core/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ impl BlueprintRunner {
pub async fn run(&mut self) -> Result<(), Error> {
if self.config.requires_registration(&self.env).await? {
self.config.register(&self.env).await?;
if self.config.should_pre_register() {
// Return from pre-registration
return Ok(());
}
}

let mut background_receivers = Vec::new();
Expand Down
15 changes: 15 additions & 0 deletions crates/runners/eigenlayer/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@ use gadget_utils::evm::get_provider_http;
pub struct EigenlayerBLSConfig {
earnings_receiver_address: Address,
delegation_approver_address: Address,
pre_register: bool,
}

impl EigenlayerBLSConfig {
/// Creates a new [`EigenlayerBLSConfig`] with the given earnings receiver and delegation approver addresses.
///
/// By default, a Runner created with this config will exit after registration (Pre-Registration). To change
/// this, use [`EigenlayerBLSConfig::with_pre_registration`] passing false.
pub fn new(earnings_receiver_address: Address, delegation_approver_address: Address) -> Self {
Self {
earnings_receiver_address,
delegation_approver_address,
pre_register: true,
}
}

pub fn with_pre_registration(mut self, pre_register: bool) -> Self {
self.pre_register = pre_register;
self
}
}

#[async_trait::async_trait]
Expand All @@ -45,6 +56,10 @@ impl BlueprintConfig for EigenlayerBLSConfig {
async fn requires_registration(&self, env: &GadgetConfiguration) -> Result<bool, Error> {
requires_registration_bls_impl(env).await
}

fn should_pre_register(&self) -> bool {
self.pre_register
}
}

async fn requires_registration_bls_impl(env: &GadgetConfiguration) -> Result<bool, Error> {
Expand Down
23 changes: 21 additions & 2 deletions crates/runners/tangle/src/tangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,35 @@ impl Default for PriceTargets {
#[derive(Clone, Default)]
pub struct TangleConfig {
pub price_targets: PriceTargets,
pub pre_register: bool,
}

impl TangleConfig {
pub fn new(price_targets: PriceTargets) -> Self {
Self {
price_targets,
pre_register: true,
}
}

pub fn with_pre_register(mut self, pre_register: bool) -> Self {
self.pre_register = pre_register;
self
}
}

#[async_trait::async_trait]
impl BlueprintConfig for TangleConfig {
async fn register(&self, env: &GadgetConfiguration) -> Result<(), Error> {
register_impl(self.price_targets.clone(), vec![], env).await
}

async fn requires_registration(&self, env: &GadgetConfiguration) -> Result<bool, Error> {
requires_registration_impl(env).await
}

async fn register(&self, env: &GadgetConfiguration) -> Result<(), Error> {
register_impl(self.clone().price_targets, vec![], env).await
fn should_pre_register(&self) -> bool {
self.pre_register
}
}

Expand Down
Loading
Loading