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

Schedule_at_job #270

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
84 changes: 51 additions & 33 deletions contracts/nois-gateway/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use cosmwasm_std::{
};
use cw_storage_plus::Bound;
use nois_protocol::{
check_order, check_version, InPacket, InPacketAck, OutPacket, OutPacketAck, StdAck,
BEACON_PRICE_PACKET_LIFETIME, IBC_APP_VERSION, WELCOME_PACKET_LIFETIME,
check_order, check_version, InPacket, InPacketAck, OutPacket, OutPacketAck, RequestType,
StdAck, BEACON_PRICE_PACKET_LIFETIME, IBC_APP_VERSION, WELCOME_PACKET_LIFETIME,
};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -344,10 +344,26 @@ pub fn ibc_packet_receive(
(|| {
let op: InPacket = from_binary(&packet.data)?;
match op {
InPacket::RequestBeacon { after, origin } => {
receive_request_beacon(deps, env, channel_id, relayer, after, origin)
}
InPacket::RequestBeacon { after, callback } => receive_job_request(
deps,
env,
channel_id,
relayer,
after,
callback,
RequestType::Randomness,
),
InPacket::RequestScheduleJob { after, callback } => receive_job_request(
deps,
env,
channel_id,
relayer,
after,
callback,
RequestType::AtJob,
),
InPacket::PullBeaconPrice {} => receive_pull_beacon_price(deps, env),

_ => Err(ContractError::UnsupportedPacketType),
}
})()
Expand All @@ -361,15 +377,16 @@ pub fn ibc_packet_receive(
})
}

fn receive_request_beacon(
fn receive_job_request(
mut deps: DepsMut,
env: Env,
channel_id: String,
relayer: Addr,
after: Timestamp,
origin: Binary,
callback: Binary,
request_type: RequestType,
) -> Result<IbcReceiveResponse, ContractError> {
validate_origin(&origin)?;
validate_origin(&callback)?;

let router = RequestRouter::new();
let RoutingReceipt {
Expand All @@ -382,15 +399,16 @@ fn receive_request_beacon(
&env,
channel_id.clone(),
after,
origin.clone(),
callback.clone(),
request_type,
)?;

// Store request
requests_log_add(
deps.storage,
&channel_id,
&RequestLogEntry {
origin,
origin: callback,
tx: (env.block.height, env.transaction.map(|ti| ti.index)),
source_id,
queued,
Expand Down Expand Up @@ -1045,7 +1063,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER2,
origin: origin(1),
callback: origin(1),
},
)
.unwrap();
Expand Down Expand Up @@ -1076,7 +1094,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER3,
origin: origin(i),
callback: origin(i),
},
)
.unwrap();
Expand Down Expand Up @@ -1106,7 +1124,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER4,
origin: origin(i),
callback: origin(i),
},
)
.unwrap();
Expand Down Expand Up @@ -1206,7 +1224,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER1,
origin: origin(1),
callback: origin(1),
},
)
.unwrap();
Expand Down Expand Up @@ -1240,7 +1258,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER1,
origin: origin(2),
callback: origin(2),
},
)
.unwrap();
Expand All @@ -1262,7 +1280,7 @@ mod tests {
"foo",
&InPacket::RequestBeacon {
after: AFTER2,
origin: origin(i),
callback: origin(i),
},
)
.unwrap();
Expand Down Expand Up @@ -1367,7 +1385,7 @@ mod tests {
CHANNEL,
&InPacket::RequestBeacon {
after: AFTER1,
origin: origin(1),
callback: origin(1),
},
)
.unwrap();
Expand All @@ -1386,8 +1404,8 @@ mod tests {
origin: origin(1),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string()),
tx: expected_tx_1
}]
}
Expand All @@ -1399,8 +1417,8 @@ mod tests {
origin: origin(1),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string()),
tx: expected_tx_1
}]
}
Expand All @@ -1411,7 +1429,7 @@ mod tests {
CHANNEL,
&InPacket::RequestBeacon {
after: AFTER2,
origin: origin(2),
callback: origin(2),
},
)
.unwrap();
Expand All @@ -1431,15 +1449,15 @@ mod tests {
origin: origin(1),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string()),
tx: expected_tx_1
}, RequestLogEntry {
origin: origin(2),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:820"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:820"
.to_string()),
tx: expected_tx_2
}]
}
Expand All @@ -1451,15 +1469,15 @@ mod tests {
origin: origin(2),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:820"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:820"
.to_string()),
tx: expected_tx_2
}, RequestLogEntry {
origin: origin(1),
queued: true,
source_id:
"drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string(),
Some("drand:dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493:810"
.to_string()),
tx: expected_tx_1
}]
}
Expand Down Expand Up @@ -1653,7 +1671,7 @@ mod tests {
CHANNEL_ID,
&InPacket::RequestBeacon {
after: AFTER1,
origin: origin(1),
callback: origin(1),
},
)
.unwrap();
Expand All @@ -1670,7 +1688,7 @@ mod tests {
CHANNEL_ID,
&InPacket::RequestBeacon {
after: AFTER1,
origin: origin(2),
callback: origin(2),
},
)
.unwrap();
Expand All @@ -1685,7 +1703,7 @@ mod tests {
CHANNEL_ID,
&InPacket::RequestBeacon {
after: AFTER2,
origin: origin(i),
callback: origin(i),
},
)
.unwrap();
Expand Down
62 changes: 57 additions & 5 deletions contracts/nois-gateway/src/request_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{
to_binary, Binary, CosmosMsg, DepsMut, Env, HexBinary, IbcMsg, StdError, StdResult, Timestamp,
};
use drand_common::{time_of_round, valid_round_after, DRAND_CHAIN_HASH};
use nois_protocol::{InPacketAck, OutPacket, StdAck, DELIVER_BEACON_PACKET_LIFETIME};
use nois_protocol::{InPacketAck, OutPacket, RequestType, StdAck, DELIVER_BEACON_PACKET_LIFETIME};

use crate::{
drand_archive::{archive_lookup, archive_store},
Expand All @@ -24,7 +24,7 @@ const MAX_JOBS_PER_SUBMISSION_WITHOUT_VERIFICATION: u32 = 14;

pub struct RoutingReceipt {
pub queued: bool,
pub source_id: String,
pub source_id: Option<String>,
pub acknowledgement: StdAck,
pub msgs: Vec<CosmosMsg>,
}
Expand All @@ -47,10 +47,14 @@ impl RequestRouter {
env: &Env,
channel: String,
after: Timestamp,
origin: Binary,
callback: Binary,
request_type: RequestType,
) -> StdResult<RoutingReceipt> {
// Here we currently only have one backend
self.handle_drand(deps, env, channel, after, origin)
match request_type {
RequestType::Randomness => self.handle_drand(deps, env, channel, after, callback),
// For now handle_at_job is almost the same as drand because we'd still be relying on drand-bots trigger
RequestType::AtJob => self.handle_at_job(deps, env, channel, after, callback),
}
}

fn handle_drand(
Expand Down Expand Up @@ -95,6 +99,7 @@ impl RequestRouter {
source_id: source_id.clone(),
})
};
let source_id = Some(source_id);

Ok(RoutingReceipt {
queued,
Expand All @@ -104,6 +109,53 @@ impl RequestRouter {
})
}

fn handle_at_job(
&self,
deps: DepsMut,
env: &Env,
channel: String,
after: Timestamp,
origin: Binary,
) -> StdResult<RoutingReceipt> {
let (round, source_id) = commit_to_drand_round(after);

let existing_randomness = archive_lookup(deps.storage, round);

let job = Job {
source_id: source_id.clone(),
channel,
origin,
};

let mut msgs = Vec::<CosmosMsg>::new();

let queued = if let Some(randomness) = existing_randomness {
//If the drand round already exists we send it
increment_processed_drand_jobs(deps.storage, round)?;
let published = time_of_round(round);
let msg =
create_deliver_beacon_ibc_message(env.block.time, job, published, randomness)?;
msgs.push(msg.into());
false
} else {
unprocessed_drand_jobs_enqueue(deps.storage, round, &job)?;
true
};

let acknowledgement = if queued {
StdAck::success(InPacketAck::RequestQueued { source_id })
} else {
StdAck::success(InPacketAck::RequestProcessed { source_id })
};

Ok(RoutingReceipt {
queued,
source_id: None,
acknowledgement,
msgs,
})
}

pub fn new_drand(
&self,
deps: DepsMut,
Expand Down
2 changes: 1 addition & 1 deletion contracts/nois-gateway/src/state/requests_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct RequestLogEntry {
/// height and tx_index of the transaction in which this was added
pub tx: (u64, Option<u32>),
/// A RNG specific randomness source identifier, e.g. `drand:<network id>:<round>`
pub source_id: String,
pub source_id: Option<String>,
/// This is true if the request was queued, i.e. the randomness is not yet available.
/// It is false if the request is already available.
pub queued: bool,
Expand Down
Loading