Skip to content

Commit

Permalink
valset: keep validator status with operator info
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jan 27, 2022
1 parent d2e5dd1 commit 48be09b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn instantiate(
let info = OperatorInfo {
pubkey,
metadata: op.metadata,
active_validator: false,
};
operators().save(deps.storage, &oper, &info)?;
}
Expand Down Expand Up @@ -203,7 +204,11 @@ fn execute_register_validator_key(
let pubkey: Ed25519Pubkey = pubkey.try_into()?;
let moniker = metadata.moniker.clone();

let operator = OperatorInfo { pubkey, metadata };
let operator = OperatorInfo {
pubkey,
metadata,
active_validator: false,
};
match operators().may_load(deps.storage, &info.sender)? {
Some(_) => return Err(ContractError::OperatorRegistered {}),
None => operators().save(deps.storage, &info.sender, &operator)?,
Expand Down Expand Up @@ -450,6 +455,7 @@ fn list_validator_keys(
metadata: info.metadata,
pubkey: info.pubkey.into(),
jailed_until,
active_validator: info.active_validator,
})
})
.take(limit)
Expand Down Expand Up @@ -562,6 +568,19 @@ fn end_block(deps: DepsMut, env: Env) -> Result<Response, ContractError> {
// calculate and store new validator set
let (validators, auto_unjail) = calculate_validators(deps.as_ref(), &env)?;

// update operators list with info about whether or not they're active validators
let ops: Vec<_> = operators()
.keys(deps.storage, None, None, Order::Ascending)
.collect::<StdResult<_>>()?;
for op in ops {
let active = validators.iter().any(|val| val.operator == op);
operators().update::<_, StdError>(deps.storage, &op, |op| {
let mut op = op.ok_or(StdError::generic_err("operator doesn't exist"))?;
op.active_validator = active;
Ok(op)
})?;
}

// auto unjailing
for addr in &auto_unjail {
JAIL.remove(deps.storage, addr)
Expand Down
2 changes: 2 additions & 0 deletions contracts/tgrade-valset/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ pub struct OperatorResponse {
pub pubkey: Pubkey,
pub metadata: ValidatorMetadata,
pub jailed_until: Option<JailingPeriod>,
pub active_validator: bool,
}

impl OperatorResponse {
Expand All @@ -330,6 +331,7 @@ impl OperatorResponse {
pubkey: info.pubkey.into(),
metadata: info.metadata,
jailed_until: jailed_until.into(),
active_validator: info.active_validator,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/tgrade-valset/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub const JAIL: Map<&Addr, JailingPeriod> = Map::new("jail");
pub struct OperatorInfo {
pub pubkey: Ed25519Pubkey,
pub metadata: ValidatorMetadata,
/// Is this an active validator?
pub active_validator: bool,
}

/// This defines the stored and returned data for a slashing event.
Expand Down

0 comments on commit 48be09b

Please sign in to comment.