Skip to content

Commit

Permalink
finish all events
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Oct 20, 2023
1 parent 5c66a17 commit b385eb0
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 54 deletions.
10 changes: 10 additions & 0 deletions actors/verifreg/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ pub fn claim(rt: &impl Runtime, id: ClaimID, claim: &Claim) -> Result<(), ActorE
rt.emit_event(&EventBuilder::new().event_type("claim").with_claim(id, claim).build()?)
}

/// Indicates an existing claim has been updated (e.g. with a longer term).
pub fn claim_updated(rt: &impl Runtime, id: ClaimID, claim: &Claim) -> Result<(), ActorError> {
rt.emit_event(&EventBuilder::new().event_type("claim-updated").with_claim(id, claim).build()?)
}

/// Indicates an expired claim has been removed.
pub fn claim_removed(rt: &impl Runtime, id: ClaimID, claim: &Claim) -> Result<(), ActorError> {
rt.emit_event(&EventBuilder::new().event_type("claim-removed").with_claim(id, claim).build()?)
}

// Private helpers //
trait WithAllocation {
fn with_allocation(self, id: AllocationID, alloc: &Allocation) -> EventBuilder;
Expand Down
23 changes: 17 additions & 6 deletions actors/verifreg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,12 @@ impl Actor {
}

let new_claim = Claim { term_max: term.term_max, ..*claim };
st_claims.put(term.provider, term.claim_id, new_claim).context_code(
st_claims.put(term.provider, term.claim_id, new_claim.clone()).context_code(
ExitCode::USR_ILLEGAL_STATE,
"HAMT put failure storing new claims",
)?;
batch_gen.add_success();
emit::claim_updated(rt, term.claim_id, &new_claim)?;
} else {
batch_gen.add_fail(ExitCode::USR_NOT_FOUND);
info!("no claim {} for provider {}", term.claim_id, term.provider);
Expand Down Expand Up @@ -606,10 +607,15 @@ impl Actor {
}

for id in to_remove {
claims.remove(params.provider, *id).context_code(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to remove claim {}", id),
)?;
let removed = claims
.remove(params.provider, *id)
.context_code(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to remove claim {}", id),
)?
.unwrap();

emit::claim_removed(rt, *id, &removed)?;
}

st.save_claims(&mut claims)?;
Expand Down Expand Up @@ -712,7 +718,12 @@ impl Actor {
emit::allocation(rt, *id, alloc)?;
}

st.put_claims(rt.store(), updated_claims)?;
st.put_claims(rt.store(), updated_claims.clone())?;

for (id, claim) in updated_claims {
emit::claim_updated(rt, id, &claim)?;
}

Ok(ids)
})?;

Expand Down
39 changes: 39 additions & 0 deletions actors/verifreg/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,22 @@ impl Harness {
rt: &MockRuntime,
provider: ActorID,
claim_ids: Vec<ClaimID>,
expect_removed: Vec<(ClaimID, Claim)>,
) -> Result<RemoveExpiredClaimsReturn, ActorError> {
rt.expect_validate_caller_any();

for (id, claim) in expect_removed {
rt.expect_emitted_event(
EventBuilder::new()
.event_type("claim-removed")
.field_indexed("id", &id)
.field_indexed("provider", &claim.provider)
.field_indexed("client", &claim.client)
.field_indexed("data-cid", &claim.data)
.build()?,
);
}

let params = RemoveExpiredClaimsParams { provider, claim_ids };
let ret = rt
.call::<VerifregActor>(
Expand Down Expand Up @@ -451,6 +464,19 @@ impl Harness {
);
}

for ext in allocs_req.extensions {
let claim = self.load_claim(rt, ext.provider, ext.claim).unwrap();
rt.expect_emitted_event(
EventBuilder::new()
.event_type("claim-updated")
.field_indexed("id", &ext.claim)
.field_indexed("provider", &claim.provider)
.field_indexed("client", &claim.client)
.field_indexed("data-cid", &claim.data)
.build()?,
);
}

rt.expect_validate_caller_addr(vec![DATACAP_TOKEN_ACTOR_ADDR]);
let ret = rt.call::<VerifregActor>(
Method::UniversalReceiverHook as MethodNum,
Expand Down Expand Up @@ -506,7 +532,20 @@ impl Harness {
&self,
rt: &MockRuntime,
params: &ExtendClaimTermsParams,
expected: Vec<(ClaimID, Claim)>,
) -> Result<ExtendClaimTermsReturn, ActorError> {
for (id, new_claim) in expected.iter() {
rt.expect_emitted_event(
EventBuilder::new()
.event_type("claim-updated")
.field_indexed("id", &id)
.field_indexed("provider", &new_claim.provider)
.field_indexed("client", &new_claim.client)
.field_indexed("data-cid", &new_claim.data)
.build()?,
);
}

rt.expect_validate_caller_any();
let ret = rt
.call::<VerifregActor>(
Expand Down
Loading

0 comments on commit b385eb0

Please sign in to comment.