Struct locutus_core::Executor
source · pub struct Executor { /* private fields */ }
Expand description
A WASM executor which will run any contracts, components, etc. registered.
This executor will monitor the store directories and databases to detect state changes. Consumers of the executor are required to poll for new changes in order to be notified of changes or can alternatively use the notification channel.
Implementations§
source§impl Executor
impl Executor
pub async fn new(
store: ContractStore,
contract_state: StateStore<SqlitePool>,
ctrl_handler: impl FnOnce()
) -> Result<Self, Box<dyn Error + Send + Sync + 'static>>
sourcepub fn register_contract_notifier(
&mut self,
key: ContractKey,
cli_id: ClientId,
notification_ch: UnboundedSender<HostResult>,
summary: StateSummary<'static>
) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
pub fn register_contract_notifier(
&mut self,
key: ContractKey,
cli_id: ClientId,
notification_ch: UnboundedSender<HostResult>,
summary: StateSummary<'static>
) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
Examples found in repository?
src/executor.rs (line 260)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
async fn contract_op(
&mut self,
req: ContractRequest<'_>,
id: ClientId,
updates: Option<UnboundedSender<Result<HostResponse, ClientError>>>,
) -> Response {
match req {
ContractRequest::Put {
contract,
state,
related_contracts,
} => {
// FIXME: in net node, we don't allow puts for existing contract states
// if it hits a node which already has it it will get rejected
// while we wait for confirmation for the state,
// we don't respond with the interim state
//
// if there is a conflict, resolve the conflict to see which
// is the outdated state:
// 1. through the arbitraur mechanism
// 2. a new func which compared two summaries and gives the most fresh
// you can request to several nodes and determine which node has a fresher ver
let key = contract.key();
let params = contract.params();
self.runtime
.contract_store
.store_contract(contract.clone())
.map_err(Into::into)
.map_err(Either::Right)?;
log::debug!("executing with params: {:?}", params);
let is_valid = self
.runtime
.validate_state(&key, ¶ms, &state, related_contracts)
.map_err(Into::into)
.map_err(Either::Right)?
== ValidateResult::Valid;
// FIXME: should deal with additional related contracts requested
let res = is_valid
.then(|| ContractResponse::PutResponse { key: key.clone() }.into())
.ok_or_else(|| {
Either::Left(
CoreContractError::Put {
key: key.clone(),
cause: "not valid".to_owned(),
}
.into(),
)
})?;
self.contract_state
.store(key.clone(), state.clone(), Some(params.clone()))
.await
.map_err(Into::into)
.map_err(Either::Right)?;
self.send_update_notification(&key, ¶ms, &state)
.await
.map_err(|_| {
Either::Left(
CoreContractError::Put {
key: key.clone(),
cause: "failed while sending notifications".to_owned(),
}
.into(),
)
})?;
Ok(res)
}
ContractRequest::Update { key, data } => {
let parameters = {
self.contract_state
.get_params(&key)
.await
.map_err(|err| Either::Right(err.into()))?
};
let new_state = {
let state = self
.contract_state
.get(&key)
.await
.map_err(Into::into)
.map_err(Either::Right)?
.clone();
let update_modification = self
.runtime
.update_state(&key, ¶meters, &state, &[data])
.map_err(|err| match err {
err if err.is_contract_exec_error() => Either::Left(
CoreContractError::Update {
key: key.clone(),
cause: format!("{err}"),
}
.into(),
),
other => Either::Right(other.into()),
})?;
if let Some(new_state) = update_modification.new_state {
let new_state = WrappedState::new(new_state.into_bytes());
self.contract_state
.store(key.clone(), new_state.clone(), None)
.await
.map_err(|err| Either::Right(err.into()))?;
new_state
} else {
todo!()
}
};
// in the network impl this would be sent over the network
let summary = self
.runtime
.summarize_state(&key, ¶meters, &new_state)
.map_err(Into::into)
.map_err(Either::Right)?;
self.send_update_notification(&key, ¶meters, &new_state)
.await?;
// TODO: in network mode, wait at least for one confirmation
// when a node receives a delta from updates, run the update themselves
// and send back confirmation
Ok(ContractResponse::UpdateResponse { key, summary }.into())
}
ContractRequest::Get {
key,
fetch_contract: contract,
} => self.perform_get(contract, key).await.map_err(Either::Left),
ContractRequest::Subscribe { key } => {
let updates =
updates.ok_or_else(|| Either::Right("missing update channel".into()))?;
self.register_contract_notifier(key.clone(), id, updates, [].as_ref().into())
.unwrap();
log::info!("getting contract: {}", key.encoded_contract_id());
// by default a subscribe op has an implicit get
self.perform_get(true, key).await.map_err(Either::Left)
// todo: in network mode, also send a subscribe to keep up to date
}
}
}
pub async fn preload(
&mut self,
cli_id: ClientId,
contract: ContractContainer,
state: WrappedState,
related_contracts: RelatedContracts<'static>
)
sourcepub async fn handle_request(
&mut self,
id: ClientId,
req: ClientRequest<'static>,
updates: Option<UnboundedSender<Result<HostResponse, ClientError>>>
) -> Result<HostResponse, Either<RequestError, Box<dyn Error + Send + Sync + 'static>>>
pub async fn handle_request(
&mut self,
id: ClientId,
req: ClientRequest<'static>,
updates: Option<UnboundedSender<Result<HostResponse, ClientError>>>
) -> Result<HostResponse, Either<RequestError, Box<dyn Error + Send + Sync + 'static>>>
Examples found in repository?
src/executor.rs (lines 91-100)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
pub async fn preload(
&mut self,
cli_id: ClientId,
contract: ContractContainer,
state: WrappedState,
related_contracts: RelatedContracts<'static>,
) {
if let Err(err) = self
.handle_request(
cli_id,
ContractRequest::Put {
contract,
state,
related_contracts,
}
.into(),
None,
)
.await
{
match err {
Either::Left(err) => log::error!("req error: {err}"),
Either::Right(err) => log::error!("other error: {err}"),
}
}
}
Auto Trait Implementations§
impl !RefUnwindSafe for Executor
impl Send for Executor
impl Sync for Executor
impl Unpin for Executor
impl !UnwindSafe for Executor
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
§impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
§fn deserialize(
&self,
deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>
fn deserialize(
&self,
deserializer: &mut D
) -> Result<With<T, W>, <D as Fallible>::Error>
Deserializes using the given deserializer
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Upcastable for Twhere
T: 'static + Any + Send + Sync,
impl<T> Upcastable for Twhere
T: 'static + Any + Send + Sync,
§fn upcast_any_ref(&self) -> &(dyn Any + 'static)
fn upcast_any_ref(&self) -> &(dyn Any + 'static)
upcast ref
§fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)
upcast mut ref