Skip to content

Commit

Permalink
Replace mutate with get + set
Browse files Browse the repository at this point in the history
I'm legitimately unsure why mutate doesn't work. Reading the impls, it should...
  • Loading branch information
kayabaNerve committed Oct 11, 2023
1 parent ed90d17 commit 2401266
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
23 changes: 13 additions & 10 deletions substrate/in-instructions/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub mod pallet {
}
}

fn key_for_network<T: Config>(network: NetworkId) -> Result<(Session, Option<Public>, Option<Public>), InvalidTransaction> {
fn keys_for_network<T: Config>(
network: NetworkId,
) -> Result<(Session, Option<Public>, Option<Public>), InvalidTransaction> {
let session = ValidatorSets::<T>::session(network);
let mut set = ValidatorSet { session, network };
let latest = ValidatorSets::<T>::keys(set).map(|keys| keys.0);
Expand Down Expand Up @@ -146,25 +148,26 @@ pub mod pallet {
Call::__Ignore(_, _) => unreachable!(),
};

let network = batch.batch.network;
let (current_session, prior, current) = key_for_network::<T>(network)?;

// verify the batch size
// TODO: Merge this encode with the one done by batch_message
if batch.batch.encode().len() > MAX_BATCH_SIZE {
Err(InvalidTransaction::ExhaustsResources)?;
}

// verify the signature
let network = batch.batch.network;
let (current_session, prior, current) = keys_for_network::<T>(network)?;
let batch_message = batch_message(&batch.batch);
// Check the prior key first since only a single `Batch` (the last one) will be when prior is
// Some yet prior wasn't the signing key
let valid_by_prior = if let Some(key) = prior {
key.verify(&batch_message, &batch.signature)
} else { false };
let valid = valid_by_prior || (if let Some(key) = current {
key.verify(&batch_message, &batch.signature)
} else { false });
let valid_by_prior =
if let Some(key) = prior { key.verify(&batch_message, &batch.signature) } else { false };
let valid = valid_by_prior ||
(if let Some(key) = current {
key.verify(&batch_message, &batch.signature)
} else {
false
});
if !valid {
Err(InvalidTransaction::BadProof)?;
}
Expand Down
9 changes: 5 additions & 4 deletions substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ pub mod pallet {
fn new_set(network: NetworkId) {
// Update CurrentSession
let session = if network != NetworkId::Serai {
CurrentSession::<T>::mutate(network, |session| {
Some(session.map(|session| Session(session.0 + 1)).unwrap_or(Session(0)))
})
.unwrap()
let new_session = CurrentSession::<T>::get(network)
.map(|session| Session(session.0 + 1))
.unwrap_or(Session(0));
CurrentSession::<T>::set(network, Some(new_session));
new_session
} else {
Self::session(network)
};
Expand Down

0 comments on commit 2401266

Please sign in to comment.