Skip to content

Commit

Permalink
Make mock attestation request at block 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Aug 20, 2024
1 parent 76d95eb commit 3ef7fe8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
17 changes: 12 additions & 5 deletions crates/threshold-signature-server/src/attestation/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
use axum::{body::Bytes, extract::State, http::StatusCode};
use entropy_shared::OcwMessageAttestationRequest;
use parity_scale_codec::Decode;
use sp_core::Pair;
use subxt::tx::PairSigner;
use x25519_dalek::StaticSecret;

Expand All @@ -35,14 +36,18 @@ pub async fn attest(
State(app_state): State<AppState>,
input: Bytes,
) -> Result<StatusCode, AttestationErr> {
let _attestaion_requests = OcwMessageAttestationRequest::decode(&mut input.as_ref())?;
// TODO check that attestation_requests.tss_account_ids contains our account_id
// which is signer.signer().public().0
let (signer, x25519_secret) = get_signer_and_x25519_secret(&app_state.kv_store).await?;
let attestaion_requests = OcwMessageAttestationRequest::decode(&mut input.as_ref())?;

// Check whether there is an attestion request for us
if !attestaion_requests.tss_account_ids.contains(&signer.signer().public().0) {
return Ok(StatusCode::OK);
}

let api = get_api(&app_state.configuration.endpoint).await?;
let rpc = get_rpc(&app_state.configuration.endpoint).await?;
let (signer, x25519_secret) = get_signer_and_x25519_secret(&app_state.kv_store).await?;

// Get the input nonce for this attestation
let nonce = {
let pending_attestation_query =
entropy::storage().attestation().pending_attestations(signer.account_id());
Expand All @@ -51,13 +56,15 @@ pub async fn attest(
.ok_or_else(|| AttestationErr::Unexpected)?
};

// We also need the current block number as input
let block_number =
rpc.chain_get_header(None).await?.ok_or_else(|| AttestationErr::BlockNumber)?.number;

let quote = create_quote(block_number, nonce, &signer, &x25519_secret).await?;

let attest_tx = entropy::tx().attestation().attest(quote.clone());
submit_transaction(&api, &rpc, &signer, &attest_tx, None).await?;
let res = submit_transaction(&api, &rpc, &signer, &attest_tx, None).await;
println!("Succesfully submitted tx {:?}", res);

Ok(StatusCode::OK)
}
Expand Down
17 changes: 9 additions & 8 deletions crates/threshold-signature-server/src/attestation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@ async fn test_attest() {
let api = get_api(&cxt.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.ws_url).await.unwrap();

let attestation_requests_query = entropy::storage().attestation().attestation_requests(1);
// Check that there is an attestation request at block 3 from the genesis config
let attestation_requests_query = entropy::storage().attestation().attestation_requests(3);
query_chain(&api, &rpc, attestation_requests_query, None).await.unwrap().unwrap();

// Get the nonce from the pending attestation from the genesis config
let nonce = {
let pending_attestation_query =
entropy::storage().attestation().pending_attestations(&TSS_ACCOUNTS[0]);
query_chain(&api, &rpc, pending_attestation_query, None).await.unwrap().unwrap()
};
assert_eq!(nonce, [0; 32]);

// Wait a few blocks for hopefully something to happen
let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
run_to_block(&rpc, block_number + 4).await;
run_to_block(&rpc, block_number + 10).await;

let nonce = {
let pending_attestation_query =
entropy::storage().attestation().pending_attestations(&TSS_ACCOUNTS[0]);
query_chain(&api, &rpc, pending_attestation_query, None).await.unwrap().unwrap()
};
assert_eq!(nonce, [0; 32]);
// There should be no more pending attestation as the attestation has been handled
let pending_attestation_query =
entropy::storage().attestation().pending_attestations(&TSS_ACCOUNTS[0]);
assert!(query_chain(&api, &rpc, pending_attestation_query, None).await.unwrap().is_none());
}
2 changes: 1 addition & 1 deletion node/cli/src/chain_spec/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn development_genesis_config(
)],
},
"attestation": AttestationConfig {
initial_attestation_requests: vec![(1, vec![crate::chain_spec::tss_account_id::ALICE.to_raw_vec()])],
initial_attestation_requests: vec![(3, vec![crate::chain_spec::tss_account_id::ALICE.to_raw_vec()])],
initial_pending_attestations: vec![(crate::chain_spec::tss_account_id::ALICE.clone(), [0; 32])],
},
})
Expand Down
2 changes: 1 addition & 1 deletion node/cli/src/chain_spec/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ pub fn integration_tests_genesis_config(
)],
},
"attestation": AttestationConfig {
initial_attestation_requests: vec![(1, vec![crate::chain_spec::tss_account_id::ALICE.to_raw_vec()])],
initial_attestation_requests: vec![(3, vec![crate::chain_spec::tss_account_id::ALICE.to_raw_vec()])],
initial_pending_attestations: vec![(crate::chain_spec::tss_account_id::ALICE.clone(), [0; 32])],
},
})
Expand Down
10 changes: 3 additions & 7 deletions pallets/propagation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ pub mod pallet {
) -> Result<(), http::Error> {
if let Some(attestations_to_request) =
pallet_attestation::Pallet::<T>::attestation_requests(
block_number.saturating_sub(1u32.into()),
// block_number.saturating_sub(1u32.into()),
block_number,
)
{
if attestations_to_request.is_empty() {
Expand All @@ -330,15 +331,10 @@ pub mod pallet {
.unwrap_or_else(|| b"http://localhost:3001/attest".to_vec());
let url = str::from_utf8(&from_local).unwrap_or("http://localhost:3001/attest");

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

let req_body = OcwMessageAttestationRequest {
// subtract 1 from blocknumber since the request is from the last block
// block_number: converted_block_number.saturating_sub(1),
tss_account_ids: attestations_to_request
.into_iter()
.map(|v| v.try_into().unwrap())
.filter_map(|v| v.try_into().ok())
.collect(),
};
log::debug!("propagation::post attestation: {:?}", &[req_body.encode()]);
Expand Down

0 comments on commit 3ef7fe8

Please sign in to comment.