Skip to content

Commit

Permalink
add client to verifier balance event
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Mar 20, 2024
1 parent dc8d927 commit aa2745a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
5 changes: 4 additions & 1 deletion actors/verifreg/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub fn verifier_balance(
rt: &impl Runtime,
verifier: ActorID,
new_balance: &DataCap,
client: Option<ActorID>,
) -> Result<(), ActorError> {
let event = build_verifier_balance_event(verifier, new_balance)?;
let event = build_verifier_balance_event(verifier, new_balance, client)?;
rt.emit_event(&event)
}

Expand Down Expand Up @@ -63,11 +64,13 @@ pub fn claim_removed(rt: &impl Runtime, id: ClaimID, claim: &Claim) -> Result<()
pub fn build_verifier_balance_event(
verifier: ActorID,
balance: &DataCap,
client: Option<ActorID>,
) -> Result<ActorEvent, ActorError> {
EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier)
.field("balance", &BigIntSer(balance))
.field_indexed("client", &client)
.build()
}

Expand Down
11 changes: 8 additions & 3 deletions actors/verifreg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Actor {
.context("failed to add verifier")
})?;

emit::verifier_balance(rt, verifier, &params.allowance)
emit::verifier_balance(rt, verifier, &params.allowance, None)
}

pub fn remove_verifier(
Expand All @@ -143,7 +143,7 @@ impl Actor {
rt.transaction(|st: &mut State, rt| {
rt.validate_immediate_caller_is(std::iter::once(&st.root_key))?;
st.remove_verifier(rt.store(), &verifier_addr).context("failed to remove verifier")?;
emit::verifier_balance(rt, verifier, &DataCap::zero())
emit::verifier_balance(rt, verifier, &DataCap::zero(), None)
})?;
Ok(())
}
Expand Down Expand Up @@ -203,7 +203,12 @@ impl Actor {
st.put_verifier(rt.store(), &verifier_addr, &new_verifier_cap)
.context("failed to update verifier allowance")?;

emit::verifier_balance(rt, verifier_addr.id().unwrap(), &new_verifier_cap)
emit::verifier_balance(
rt,
verifier_addr.id().unwrap(),
&new_verifier_cap,
Some(client.id().unwrap()),
)
})?;

// Credit client token allowance.
Expand Down
22 changes: 14 additions & 8 deletions actors/verifreg/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ impl Harness {
ExitCode::OK,
None,
);

let client: Option<ActorID> = None;
rt.expect_emitted_event(
EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier_resolved.id().unwrap())
.field("balance", &BigIntSer(allowance))
.field_indexed("client", &client)
.build()?,
);

Expand All @@ -162,12 +163,14 @@ impl Harness {

pub fn remove_verifier(&self, rt: &MockRuntime, verifier: &Address) -> Result<(), ActorError> {
rt.expect_validate_caller_addr(vec![self.root]);
let client: Option<ActorID> = None;

rt.expect_emitted_event(
EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier.id().unwrap())
.field("balance", &BigIntSer(&DataCap::zero()))
.field_indexed("client", &client)
.build()?,
);

Expand Down Expand Up @@ -233,13 +236,16 @@ impl Harness {

let params = AddVerifiedClientParams { address: *client, allowance: allowance.clone() };

rt.expect_emitted_event(
EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier.id().unwrap())
.field("balance", &BigIntSer(&(verifier_balance - allowance)))
.build()?,
);
if client_resolved.id().is_ok() {
rt.expect_emitted_event(
EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier.id().unwrap())
.field("balance", &BigIntSer(&(verifier_balance - allowance)))
.field_indexed("client", &client_resolved.id().unwrap())
.build()?,
);
}

let ret = rt.call::<VerifregActor>(
Method::AddVerifiedClient as MethodNum,
Expand Down
1 change: 1 addition & 0 deletions actors/verifreg/tests/verifreg_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ mod clients {
.typ("verifier-balance")
.field_indexed("verifier", &VERIFIER.id().unwrap())
.field("balance", &BigIntSer(&(allowance_verifier - allowance_client)))
.field_indexed("client", &CLIENT.id().unwrap())
.build()
.unwrap(),
);
Expand Down
6 changes: 5 additions & 1 deletion integration_tests/src/tests/verifreg_remove_datacap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ pub fn remove_datacap_simple_successful_path_test(v: &dyn VM) {
subinvocs: None,
..Default::default()
}]),
events: vec![verifier_balance_event(verifier1.id().unwrap(), verifier_datacap)],
events: vec![verifier_balance_event(
verifier1.id().unwrap(),
verifier_datacap,
Some(verified_client.id().unwrap()),
)],
..Default::default()
}
.matches(v.take_invocations().last().unwrap());
Expand Down
15 changes: 12 additions & 3 deletions integration_tests/src/util/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,18 @@ pub fn submit_invalid_post(
);
}

pub fn verifier_balance_event(verifier: ActorID, data_cap: DataCap) -> EmittedEvent {
pub fn verifier_balance_event(
verifier: ActorID,
data_cap: DataCap,
client: Option<ActorID>,
) -> EmittedEvent {
EmittedEvent {
emitter: VERIFIED_REGISTRY_ACTOR_ID,
event: EventBuilder::new()
.typ("verifier-balance")
.field_indexed("verifier", &verifier)
.field("balance", &BigIntSer(&data_cap))
.field_indexed("client", &client)
.build()
.unwrap(),
}
Expand Down Expand Up @@ -803,7 +808,7 @@ pub fn verifreg_add_verifier(v: &dyn VM, verifier: &Address, data_cap: StoragePo
DATACAP_TOKEN_ACTOR_ADDR,
*verifier,
)]),
events: vec![verifier_balance_event(verifier.id().unwrap(), data_cap)],
events: vec![verifier_balance_event(verifier.id().unwrap(), data_cap, None)],
..Default::default()
}]),
..Default::default()
Expand Down Expand Up @@ -863,7 +868,11 @@ pub fn verifreg_add_client(
)]),
..Default::default()
}]),
events: vec![verifier_balance_event(verifier.id().unwrap(), updated_verifier_balance)],
events: vec![verifier_balance_event(
verifier.id().unwrap(),
updated_verifier_balance,
Some(client.id().unwrap()),
)],
..Default::default()
}
.matches(v.take_invocations().last().unwrap());
Expand Down

0 comments on commit aa2745a

Please sign in to comment.