diff --git a/pallets/weaver/Cargo.toml b/pallets/weaver/Cargo.toml index 6c2ebbd..7a0bd2b 100644 --- a/pallets/weaver/Cargo.toml +++ b/pallets/weaver/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-weaver" -version = "0.0.17" +version = "0.0.18" description = "Weaving a secure cryptographic tapestry across different external chains." license.workspace = true authors.workspace = true diff --git a/pallets/weaver/src/benchmarking.rs b/pallets/weaver/src/benchmarking.rs index 106d7dd..37e01bb 100644 --- a/pallets/weaver/src/benchmarking.rs +++ b/pallets/weaver/src/benchmarking.rs @@ -35,6 +35,9 @@ fn prepare_pallet( T::NetworkDataHandler::register(network_id, network_data) .expect("network should be valid for insertion"); + let current_block = T::BlockNumberProvider::current_block_number(); + Pallet::::on_initialize(current_block); + (network_id, authority, 0) } diff --git a/pallets/weaver/src/lib.rs b/pallets/weaver/src/lib.rs index ddc9b2f..4b87b1e 100644 --- a/pallets/weaver/src/lib.rs +++ b/pallets/weaver/src/lib.rs @@ -330,22 +330,13 @@ pub mod pallet { let external_block = block_attestation.external_block; - let current_authorities_count = Authorities::::decode_len(). - unwrap_or_default(); - NetworkAttestations::::try_mutate( &network_id, |attestations| -> DispatchResult { - if attestations.is_empty() { - attestations.bounded_resize(current_authorities_count, None); - } + let index = authority_index as usize; - ensure!( - (authority_index as usize) < attestations.len(), - Error::::TooManyAttestations, - ); - - attestations[authority_index as usize] = Some(external_block); + ensure!(index < attestations.len(), Error::::TooManyAttestations); + attestations[index] = Some(external_block); Ok(()) })?; @@ -532,9 +523,11 @@ pub mod pallet { weight.saturating_accrue(T::DbWeight::get().reads(1)); let attestation_len = NetworkAttestations::::decode_len(&network_id); - if attestation_len.unwrap_or(0) > len { - weight.saturating_accrue(T::DbWeight::get().writes(1)); - NetworkAttestations::::remove(&network_id); + if attestation_len.unwrap_or(0) != len { + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + NetworkAttestations::::mutate(&network_id, |attestations| { + attestations.bounded_resize(len, None); + }); return weight; } @@ -675,6 +668,13 @@ impl Pallet { let attestation_block = attestation.block(); let network_id = attestation.network_id(); + let current_attestation_len = NetworkAttestations::::decode_len(&network_id) + .unwrap_or(0); + + if authority_index as usize >= current_attestation_len { + return Err(InvalidTransaction::BadSigner.into()); + } + if DisabledAuthorities::::get().contains(authority_index) { return Err(InvalidTransaction::BadSigner); } diff --git a/pallets/weaver/src/tests.rs b/pallets/weaver/src/tests.rs index 4fc1700..82181c9 100644 --- a/pallets/weaver/src/tests.rs +++ b/pallets/weaver/src/tests.rs @@ -76,6 +76,7 @@ fn should_successfully_compute_median_and_start_weaving_round() { let networks = vec![(network_id, network_data, Default::default())]; new_test_ext(authorities, networks).execute_with(|| { + GhostWeaver::on_initialize(1); System::set_block_number(2); let context = AttestationContext::default() @@ -120,6 +121,7 @@ fn should_apply_cooldown_delay_on_consensus_failure() { let networks = vec![(network_id, network_data, Default::default())]; new_test_ext(authorities, networks).execute_with(|| { + GhostWeaver::on_initialize(9); System::set_block_number(10); let context = AttestationContext::default() @@ -168,6 +170,7 @@ fn validate_unsigned_should_prevent_mempool_dos_via_tags() { let networks = vec![(network_id, network_data, Default::default())]; new_test_ext(authorities, networks).execute_with(|| { + GhostWeaver::on_initialize(1); System::set_block_number(2); let att = AttestationContext::default() @@ -240,6 +243,9 @@ fn offchain_worker_should_fetch_block_and_submit_unsigned_tx() { ext.register_extension(TransactionPoolExt::new(pool)); ext.execute_with(|| { + GhostWeaver::on_initialize(1); + GhostWeaver::on_initialize(2); + let network_id_encoded = evm_network_id.encode(); let mut endpoint_key = b"ghost-weaver::".to_vec(); @@ -406,10 +412,11 @@ fn offchain_worker_should_fetch_hash_and_submit_unsigned_tx() { ext.register_extension(TransactionPoolExt::new(pool)); ext.execute_with(|| { + GhostWeaver::on_initialize(1); + GhostWeaver::on_initialize(2); + let tapestry_draft = TapestryDraftBuilder::::default(); - let evm_tapestry_draft = tapestry_draft.with_median_external(25639556).build(); - let utxo_tapestry_draft = tapestry_draft.with_median_external(960129).build(); CurrentWeavingSession::::insert(&evm_network_id, evm_session); @@ -635,6 +642,8 @@ fn should_successfully_pull_evm_thread_and_mint_currency() { ]; new_test_ext(authorities, networks).execute_with(|| { + GhostWeaver::on_initialize(1); + // https://sepolia.etherscan.io/address/0x7fec11e652f4d54e8cf11c4b52c360380332f2bd#readContract // getRoot: session 0, atBlock 11716130 let session = 0; @@ -928,6 +937,7 @@ fn on_initialize_should_finalize_session_when_timeout_is_reached() { new_test_ext(authorities, networks).execute_with(|| { let current_substrate_block = 10; + GhostWeaver::on_initialize(current_substrate_block); System::set_block_number(current_substrate_block + 1); let tapestry_draft = TapestryDraftBuilder::default()