Skip to content

Commit

Permalink
add rotate keyshare trigger to chain
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 21, 2024
1 parent 160f8a8 commit 9e4da4c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ pub fn new_full_base(
b"reshare_validators",
&format!("{}/validator/reshare", endpoint).into_bytes(),
);
offchain_db.local_storage_set(
sp_core::offchain::StorageKind::PERSISTENT,
b"rotate_keyshares",
&format!("{}/validator/rotate_keyshares", endpoint).into_bytes(),
);
offchain_db.local_storage_set(
sp_core::offchain::StorageKind::PERSISTENT,
b"attest",
Expand Down
47 changes: 47 additions & 0 deletions pallets/propagation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub mod pallet {

/// Attestations request message passed
AttestationRequestMessagePassed(OcwMessageAttestationRequest),

/// Key Rotate Message passed to validators
/// parameters. [BlockNumberFor<T>]
KeyRotatesMessagePassed(BlockNumberFor<T>),
}

#[pallet::call]
Expand Down Expand Up @@ -319,6 +323,49 @@ pub mod pallet {
Ok(())
}

/// Submits a request to rotate parent network key the threshold servers.
pub fn post_rotate_keyshare(block_number: BlockNumberFor<T>) -> Result<(), http::Error> {
let rotate_keyshares = pallet_staking_extension::Pallet::<T>::rotate_keyshares();
if !rotate_keyshares {
return Ok(());
}

let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
let kind = sp_core::offchain::StorageKind::PERSISTENT;
let from_local = sp_io::offchain::local_storage_get(kind, b"rotate_keyshares")
.unwrap_or_else(|| b"http://localhost:3001/validator/rotate_keyshares".to_vec());
let url = str::from_utf8(&from_local)
.unwrap_or("http://localhost:3001/validator/rotate_keyshares");

log::warn!("propagation::post rotate keyshare");

let converted_block_number: u32 =
BlockNumberFor::<T>::try_into(block_number).unwrap_or_default();

// We construct the request
// important: the header->Content-Type must be added and match that of the receiving
// party!!
let pending = http::Request::post(url, vec![converted_block_number.encode()])
.deadline(deadline)
.send()
.map_err(|_| http::Error::IoError)?;

// We await response, same as in fn get()
let response =
pending.try_wait(deadline).map_err(|_| http::Error::DeadlineReached)??;

// check response code
if response.code != 200 {
log::warn!("Unexpected status code: {}", response.code);
return Err(http::Error::Unknown);
}
let _res_body = response.body().collect::<Vec<u8>>();

Self::deposit_event(Event::KeyRotatesMessagePassed(block_number));

Ok(())
}

/// Submits a request for a TDX attestation.
pub fn post_attestation_request(
block_number: BlockNumberFor<T>,
Expand Down
11 changes: 11 additions & 0 deletions pallets/propagation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ fn knows_how_to_mock_several_http_calls() {
body: [32, 1, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0].to_vec(),
..Default::default()
});
state.expect_request(testing::PendingRequest {
method: "POST".into(),
uri: "http://localhost:3001/validator/rotate_keyshares".into(),
sent: true,
response: Some([].to_vec()),
body: [10, 0, 0, 0].to_vec(),
..Default::default()
});
});

t.execute_with(|| {
Expand Down Expand Up @@ -137,6 +145,9 @@ fn knows_how_to_mock_several_http_calls() {
});
// now triggers
Propagation::post_reshare(7).unwrap();

pallet_staking_extension::RotateKeyshares::<Test>::put(true);
Propagation::post_rotate_keyshare(10).unwrap();
})
}

Expand Down
6 changes: 6 additions & 0 deletions pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ pub mod pallet {
#[pallet::getter(fn jump_start_progress)]
pub type JumpStartProgress<T: Config> = StorageValue<_, JumpStartDetails<T>, ValueQuery>;

/// Tell Signers to rotate keyshare
#[pallet::storage]
#[pallet::getter(fn rotate_keyshares)]
pub type RotateKeyshares<T: Config> = StorageValue<_, bool, ValueQuery>;

/// A type used to simplify the genesis configuration definition.
pub type ThresholdServersConfig<T> = (
<T as pallet_session::Config>::ValidatorId,
Expand Down Expand Up @@ -505,6 +510,7 @@ pub mod pallet {
let current_signer_length = signers_info.next_signers.len();
if signers_info.confirmations.len() == (current_signer_length - 1) {
Signers::<T>::put(signers_info.next_signers.clone());
RotateKeyshares::<T>::put(true);
Self::deposit_event(Event::SignersRotation(signers_info.next_signers));
Ok(Pays::No.into())
} else {
Expand Down

0 comments on commit 9e4da4c

Please sign in to comment.