Skip to content

Commit f66678c

Browse files
committed
further cleanups
1 parent 328f470 commit f66678c

14 files changed

Lines changed: 331 additions & 303 deletions

File tree

golem-api-grpc/proto/golem/worker/public_oplog.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ message QueuedCardEventCard {
292292

293293
enum CardInstallFailure {
294294
CARD_INSTALL_FAILURE_UNSPECIFIED = 0;
295-
CARD_INSTALL_FAILURE_CARD_REVOKED = 1;
296295
CARD_INSTALL_FAILURE_NOT_FOUND = 2;
297296
CARD_INSTALL_FAILURE_RECIPIENT_MISMATCH = 3;
298297
CARD_INSTALL_FAILURE_NOT_PERMITTED = 4;

golem-api-grpc/proto/golem/worker/raw_oplog.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ message RawQueuedCardEventCard {
334334

335335
enum RawCardInstallFailure {
336336
RAW_CARD_INSTALL_FAILURE_UNSPECIFIED = 0;
337-
RAW_CARD_INSTALL_FAILURE_CARD_REVOKED = 1;
338337
RAW_CARD_INSTALL_FAILURE_NOT_FOUND = 2;
339338
RAW_CARD_INSTALL_FAILURE_RECIPIENT_MISMATCH = 3;
340339
RAW_CARD_INSTALL_FAILURE_NOT_PERMITTED = 4;

golem-common/src/base_model/card/algebra.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,7 @@ impl EffectiveSurface {
141141
&self,
142142
child_lower_positive: &[PermissionPattern],
143143
child_upper_positive: &[PermissionPattern],
144-
) -> Result<(), CardAlgebraError> {
145-
self.validates_derivation_with_witness(child_lower_positive, child_upper_positive)
146-
.map(|_| ())
147-
}
148-
149-
pub fn validates_derivation_with_witness(
150-
&self,
151-
child_lower_positive: &[PermissionPattern],
152-
child_upper_positive: &[PermissionPattern],
153144
) -> Result<Vec<CardId>, CardAlgebraError> {
154-
if self.source_card_ids.len() != self.lower.len()
155-
|| self.source_card_ids.len() != self.upper.len()
156-
{
157-
self.validates_derivation_legacy(child_lower_positive, child_upper_positive)?;
158-
return Ok(self.source_card_ids.clone());
159-
}
160-
161145
let mut witness = Vec::new();
162146

163147
for grant in child_lower_positive {
@@ -174,7 +158,7 @@ impl EffectiveSurface {
174158
grant: Box::new(grant.clone()),
175159
});
176160
};
177-
push_unique(&mut witness, self.source_card_ids[parent_index]);
161+
push_source_if_available(&mut witness, &self.source_card_ids, parent_index);
178162
}
179163

180164
for grant in child_upper_positive {
@@ -186,38 +170,14 @@ impl EffectiveSurface {
186170
});
187171
}
188172
if !surface.is_empty() {
189-
push_unique(&mut witness, self.source_card_ids[parent_index]);
173+
push_source_if_available(&mut witness, &self.source_card_ids, parent_index);
190174
}
191175
}
192176
}
193177

194178
Ok(witness)
195179
}
196180

197-
fn validates_derivation_legacy(
198-
&self,
199-
child_lower_positive: &[PermissionPattern],
200-
child_upper_positive: &[PermissionPattern],
201-
) -> Result<(), CardAlgebraError> {
202-
for grant in child_lower_positive {
203-
if !self.allows_lower(&grant.to_target())? {
204-
return Err(CardAlgebraError::DerivationNotSubsumed {
205-
grant: Box::new(grant.clone()),
206-
});
207-
}
208-
}
209-
210-
for grant in child_upper_positive {
211-
if !self.allows_upper(&grant.to_target())? {
212-
return Err(CardAlgebraError::DerivationNotSubsumed {
213-
grant: Box::new(grant.clone()),
214-
});
215-
}
216-
}
217-
218-
Ok(())
219-
}
220-
221181
fn allows_lower(&self, request: &PermissionTarget) -> Result<bool, CardAlgebraError> {
222182
for surface in &self.lower {
223183
if surface.allows(request)? {
@@ -239,6 +199,12 @@ impl EffectiveSurface {
239199
}
240200
}
241201

202+
fn push_source_if_available(values: &mut Vec<CardId>, source_card_ids: &[CardId], index: usize) {
203+
if let Some(card_id) = source_card_ids.get(index) {
204+
push_unique(values, *card_id);
205+
}
206+
}
207+
242208
fn push_unique(values: &mut Vec<CardId>, value: CardId) {
243209
if !values.contains(&value) {
244210
values.push(value);

golem-common/src/base_model/card/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn derivation_witness_excludes_unrelated_parent() {
327327
EffectiveSurface::from_cards(&[needed_parent, unrelated_parent], &recipient).unwrap();
328328

329329
let witness = surface
330-
.validates_derivation_with_witness(std::slice::from_ref(&read_tmp), &[])
330+
.validates_derivation(std::slice::from_ref(&read_tmp), &[])
331331
.unwrap();
332332

333333
assert_eq!(witness, vec![needed_parent_id]);
@@ -357,7 +357,7 @@ fn derivation_witness_includes_multiple_needed_parents() {
357357
EffectiveSurface::from_cards(&[filesystem_parent, secret_parent], &recipient).unwrap();
358358

359359
let witness = surface
360-
.validates_derivation_with_witness(&[read_tmp, reveal_secret], &[])
360+
.validates_derivation(&[read_tmp, reveal_secret], &[])
361361
.unwrap();
362362

363363
assert_eq!(witness, vec![filesystem_parent_id, secret_parent_id]);

golem-common/src/base_model/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,6 @@ impl From<QueuedCardEvent> for PublicQueuedCardEvent {
913913
#[cfg_attr(feature = "full", derive(desert_rust::BinaryCodec))]
914914
#[cfg_attr(feature = "full", desert(evolution()))]
915915
pub enum CardInstallFailure {
916-
CardRevoked,
917916
NotFound,
918917
RecipientMismatch,
919918
NotPermitted,

golem-common/src/model/oplog/protobuf.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ fn card_install_failure_from_proto(
162162
golem_api_grpc::proto::golem::worker::CardInstallFailure::Unspecified => {
163163
Err("Unspecified card install failure".to_string())
164164
}
165-
golem_api_grpc::proto::golem::worker::CardInstallFailure::CardRevoked => {
166-
Ok(CardInstallFailure::CardRevoked)
167-
}
168165
golem_api_grpc::proto::golem::worker::CardInstallFailure::NotFound => {
169166
Ok(CardInstallFailure::NotFound)
170167
}
@@ -181,9 +178,6 @@ fn card_install_failure_to_proto(
181178
value: CardInstallFailure,
182179
) -> golem_api_grpc::proto::golem::worker::CardInstallFailure {
183180
match value {
184-
CardInstallFailure::CardRevoked => {
185-
golem_api_grpc::proto::golem::worker::CardInstallFailure::CardRevoked
186-
}
187181
CardInstallFailure::NotFound => {
188182
golem_api_grpc::proto::golem::worker::CardInstallFailure::NotFound
189183
}
@@ -203,9 +197,6 @@ fn raw_card_install_failure_from_proto(
203197
golem_api_grpc::proto::golem::worker::RawCardInstallFailure::Unspecified => {
204198
Err("Unspecified raw card install failure".to_string())
205199
}
206-
golem_api_grpc::proto::golem::worker::RawCardInstallFailure::CardRevoked => {
207-
Ok(CardInstallFailure::CardRevoked)
208-
}
209200
golem_api_grpc::proto::golem::worker::RawCardInstallFailure::NotFound => {
210201
Ok(CardInstallFailure::NotFound)
211202
}
@@ -222,9 +213,6 @@ fn raw_card_install_failure_to_proto(
222213
value: CardInstallFailure,
223214
) -> golem_api_grpc::proto::golem::worker::RawCardInstallFailure {
224215
match value {
225-
CardInstallFailure::CardRevoked => {
226-
golem_api_grpc::proto::golem::worker::RawCardInstallFailure::CardRevoked
227-
}
228216
CardInstallFailure::NotFound => {
229217
golem_api_grpc::proto::golem::worker::RawCardInstallFailure::NotFound
230218
}

golem-registry-service/src/services/component/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ fn prepare_agent_initial_card_for_minting(
925925
},
926926
)?;
927927
effective_surface
928-
.validates_derivation_with_witness(&lower_positive, &upper_positive)
928+
.validates_derivation(&lower_positive, &upper_positive)
929929
.map_err(|error| ComponentError::InvalidAgentInitialPermissionCard {
930930
agent_type: agent_type_name.clone(),
931931
message: format!("card derivation is not allowed by the creator's cards: {error:?}"),

golem-registry-service/src/services/permission_share.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ fn validate_effective_surface_derivation(
595595
) -> Result<(), PermissionShareError> {
596596
effective_surface
597597
.validates_derivation(&parsed.lower_positive, &parsed.upper_positive)
598+
.map(|_| ())
598599
.map_err(derivation_error)
599600
}
600601

golem-worker-executor/src/durable_host/golem/v1x.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ fn card_install_failure_to_wit(
106106
failure: CardInstallFailure,
107107
) -> golem_api_1_x::host::CardInstallError {
108108
match failure {
109-
CardInstallFailure::CardRevoked => golem_api_1_x::host::CardInstallError::Revoked,
110109
CardInstallFailure::NotFound => golem_api_1_x::host::CardInstallError::NotFound,
111110
CardInstallFailure::RecipientMismatch | CardInstallFailure::NotPermitted => {
112111
golem_api_1_x::host::CardInstallError::NotPermitted
@@ -653,12 +652,11 @@ impl<Ctx: WorkerCtx> Host for DurableWorkerCtx<Ctx> {
653652
let card = ctx
654653
.state
655654
.card_service
656-
.get_cards(vec![card_id])
655+
.check_cards(vec![card_id])
657656
.await?
658-
.into_iter()
659-
.next();
660-
let result = if let Some(card) = card {
661-
ctx.apply_card_install(None, card).await?
657+
.remove(&card_id);
658+
let result = if let Some(crate::services::card::CardState::Live(card)) = card {
659+
ctx.apply_card_install(None, *card).await?
662660
} else {
663661
Err(CardInstallFailure::NotFound)
664662
};

golem-worker-executor/src/durable_host/mod.rs

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::model::{
5151
use crate::services::agent_types::AgentTypesService;
5252
use crate::services::agent_webhooks::AgentWebhooksService;
5353
use crate::services::blob_store::BlobStoreService;
54-
use crate::services::card::CardService;
54+
use crate::services::card::{CardService, CardState};
5555
use crate::services::component::ComponentService;
5656
use crate::services::environment_state::EnvironmentStateService;
5757
use crate::services::file_loader::{FileLoader, FileUseToken};
@@ -722,20 +722,40 @@ impl<Ctx: WorkerCtx> DurableWorkerCtx<Ctx> {
722722
card: StoredCard,
723723
) -> Result<Result<(), CardInstallFailure>, WorkerExecutorError> {
724724
let card_id = card.card_id();
725-
let revoked_or_missing = self
725+
let mut candidate_wallet_card_ids = self
726+
.state
727+
.agent_wallet_cards
728+
.keys()
729+
.copied()
730+
.collect::<Vec<_>>();
731+
if !candidate_wallet_card_ids.contains(&card_id) {
732+
candidate_wallet_card_ids.push(card_id);
733+
}
734+
self.state
735+
.card_service
736+
.set_card_interest(self.owned_agent_id.clone(), &candidate_wallet_card_ids)
737+
.await;
738+
739+
let card_state = self
726740
.state
727741
.card_service
728742
.check_cards(vec![card_id])
729743
.await?
730-
.contains(&card_id);
731-
let status = self.public_state.worker().get_last_known_status().await;
744+
.remove(&card_id);
732745

733-
if revoked_or_missing {
734-
let reason = if status.revoked_cards.contains(&card_id) {
735-
CardInstallFailure::CardRevoked
736-
} else {
737-
CardInstallFailure::NotFound
738-
};
746+
if !matches!(card_state, Some(CardState::Live(_))) {
747+
let wallet_card_ids = self
748+
.state
749+
.agent_wallet_cards
750+
.keys()
751+
.copied()
752+
.collect::<Vec<_>>();
753+
self.state
754+
.card_service
755+
.set_card_interest(self.owned_agent_id.clone(), &wallet_card_ids)
756+
.await;
757+
758+
let reason = CardInstallFailure::NotFound;
739759

740760
if let Some(queued_event_index) = queued_event_index {
741761
self.public_state
@@ -759,8 +779,9 @@ impl<Ctx: WorkerCtx> DurableWorkerCtx<Ctx> {
759779
.collect::<Vec<_>>();
760780
self.state
761781
.card_service
762-
.register_agent_cards(self.owned_agent_id.clone(), &wallet_card_ids)
782+
.set_card_interest(self.owned_agent_id.clone(), &wallet_card_ids)
763783
.await;
784+
764785
self.public_state
765786
.worker()
766787
.add_and_commit_oplog(OplogEntry::card_installed(queued_event_index, card))
@@ -784,7 +805,17 @@ impl<Ctx: WorkerCtx> DurableWorkerCtx<Ctx> {
784805
if is_live {
785806
self.state
786807
.card_service
787-
.remove_revoked_agent_cards(&self.owned_agent_id, &[card_id])
808+
.record_revoked_cards(&[card_id])
809+
.await;
810+
let wallet_card_ids = self
811+
.state
812+
.agent_wallet_cards
813+
.keys()
814+
.copied()
815+
.collect::<Vec<_>>();
816+
self.state
817+
.card_service
818+
.set_card_interest(self.owned_agent_id.clone(), &wallet_card_ids)
788819
.await;
789820

790821
self.public_state
@@ -2605,20 +2636,22 @@ impl<Ctx: WorkerCtx> DurableWorkerCtx<Ctx> {
26052636
.collect::<Vec<_>>();
26062637
self.state
26072638
.card_service
2608-
.register_agent_cards(self.owned_agent_id.clone(), &wallet_card_ids)
2639+
.set_card_interest(self.owned_agent_id.clone(), &wallet_card_ids)
26092640
.await;
26102641

26112642
if wallet_card_ids.is_empty() {
26122643
return Ok(());
26132644
}
26142645

2615-
let revoked_card_ids = self.state.card_service.check_cards(wallet_card_ids).await?;
2646+
let card_states = self.state.card_service.check_cards(wallet_card_ids).await?;
26162647

2617-
for card_id in revoked_card_ids {
2618-
self.public_state
2619-
.worker()
2620-
.queue_card_revocation(card_id)
2621-
.await;
2648+
for (card_id, state) in card_states {
2649+
if state == CardState::Revoked {
2650+
self.public_state
2651+
.worker()
2652+
.queue_card_revocation(card_id)
2653+
.await;
2654+
}
26222655
}
26232656

26242657
Ok(())
@@ -2722,6 +2755,16 @@ impl<Ctx: WorkerCtx> DurableWorkerCtx<Ctx> {
27222755
self.state.cached_agent_config_retry_policies = None;
27232756
self.state.agent_effective_surface = agent_effective_surface;
27242757
self.state.agent_wallet_cards = initial_wallet_cards;
2758+
let wallet_card_ids = self
2759+
.state
2760+
.agent_wallet_cards
2761+
.keys()
2762+
.copied()
2763+
.collect::<Vec<_>>();
2764+
self.state
2765+
.card_service
2766+
.set_card_interest(self.owned_agent_id.clone(), &wallet_card_ids)
2767+
.await;
27252768
};
27262769

27272770
self.state.component_metadata = new_metadata;

0 commit comments

Comments
 (0)