Skip to content

Commit

Permalink
Gj/don't reset offchain state (#968)
Browse files Browse the repository at this point in the history
## Describe your changes

Hot fix
  • Loading branch information
Gauthamastro authored Jul 2, 2024
2 parents 5f77a40 + 3491f6e commit de9c328
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
17 changes: 15 additions & 2 deletions pallets/ocex/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ impl<T: Config> AggregatorClient<T> {
/// Load signed summary and send it to the aggregator
/// # Parameters
/// * `snapshot_id`: Snapshot id for which signed summary should be loaded and sent
pub fn load_signed_summary_and_send(snapshot_id: u64) {
pub fn load_signed_summary_and_send(
current_set_id: u64,
snapshot_id: u64,
signer: &T::AuthorityId,
auth_index: u16,
) -> Result<(), &'static str> {
let mut key = LAST_PROCESSED_SNAPSHOT.to_vec();
key.append(&mut snapshot_id.encode());

Expand All @@ -56,7 +61,14 @@ impl<T: Config> AggregatorClient<T> {
<<T as Config>::AuthorityId as RuntimeAppPublic>::Signature,
u16,
)>() {
Ok(Some((summary, signature, index))) => {
Ok(Some((mut summary, mut signature, mut index))) => {
// Check if the validator set id is same as the current active, if not, update it and sign again
if summary.validator_set_id != current_set_id {
log::info!(target:"ocex","Signing with new validator set id's keys");
summary.validator_set_id = current_set_id;
signature = signer.sign(&summary.encode()).ok_or("Private key not found")?;
index = auth_index;
}
match serde_json::to_string(&ApprovedSnapshot {
summary: summary.encode(),
index: index.saturated_into(),
Expand All @@ -83,6 +95,7 @@ impl<T: Config> AggregatorClient<T> {
log::error!(target:"ocex","Error loading signed summary for: nonce {:?}, {:?}",snapshot_id,err);
},
}
Ok(())
}

/// Load user action batch from aggregator
Expand Down
4 changes: 2 additions & 2 deletions pallets/ocex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2250,8 +2250,8 @@ impl<T: Config + frame_system::offchain::SendTransactionTypes<Call<T>>> Pallet<T

//Check threshold

const MAJORITY: u8 = 67;
let p = Percent::from_percent(MAJORITY);
const THRESHOLD: u8 = 51;
let p = Percent::from_percent(THRESHOLD);
let threshold = p * authorities.len();

if threshold > signatures.len() {
Expand Down
22 changes: 18 additions & 4 deletions pallets/ocex/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ impl<T: Config> Pallet<T> {
return Err("Last processed snapshot root is not same as on-chain root");
}
info
} else if info.snapshot_id != 0 {
} else if info.snapshot_id != 0 && info.snapshot_id != next_nonce {
log::error!(target:"ocex","Unable to load last processed snapshot summary from on-chain: {:?}",info.snapshot_id);
store_trie_root(H256::zero());
return Err("Unable to load last processed snapshot summary from on-chain");
} else {
info
Expand All @@ -139,8 +138,22 @@ impl<T: Config> Pallet<T> {
// Check if we already processed this snapshot and updated our offchain state.
if last_processed_nonce == next_nonce {
log::debug!(target:"ocex","Submitting last processed snapshot: {:?}",next_nonce);
if !sp_io::offchain::is_validator() {
log::error!(target: "ocex","We have stale signed summary for {:?} but not a validator anymore", last_processed_nonce);
return Ok(true);
}
let current_set_id = Self::validator_set().set_id;
let key = available_keys.first().ok_or("No active keys found")?;
let auth_index = Self::calculate_signer_index(&authorities, key)
.ok_or("Unable to calculate signer index")?;

// resubmit the summary to aggregator
AggregatorClient::<T>::load_signed_summary_and_send(next_nonce);
AggregatorClient::<T>::load_signed_summary_and_send(
current_set_id,
next_nonce,
key,
auth_index as u16,
)?;
return Ok(true);
}
log::info!(target:"ocex","last_processed_nonce: {:?}, next_nonce: {:?}",last_processed_nonce, next_nonce);
Expand Down Expand Up @@ -657,7 +670,8 @@ impl<T: Config> Pallet<T> {
/// Reset the offchain state's LMP index and set the epoch
fn start_new_lmp_epoch(state: &mut OffchainState, epoch: u16) -> Result<(), &'static str> {
let mut config = if epoch > 1 {
get_lmp_config(state, epoch)?
get_lmp_config(state, epoch)
.unwrap_or_else(|_| orderbook_primitives::lmp::LMPConfig { epoch, index: 0 })
} else {
// To Handle the corner case of zero
orderbook_primitives::lmp::LMPConfig { epoch, index: 0 }
Expand Down
2 changes: 1 addition & 1 deletion runtimes/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 358,
spec_version: 368,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down
2 changes: 1 addition & 1 deletion runtimes/parachain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polkadex-parachain"),
impl_name: create_runtime_str!("polkadex-parachain"),
authoring_version: 1,
spec_version: 14,
spec_version: 16,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down

0 comments on commit de9c328

Please sign in to comment.