Skip to content

Commit

Permalink
Merge branch 'main' of github.com:subspace/subspace into test-3h-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
NingLin-P committed Jun 21, 2024
2 parents 43d12f8 + 5aa4d67 commit c6770e1
Show file tree
Hide file tree
Showing 103 changed files with 3,112 additions and 1,578 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/domain-genesis-storage-snapshot-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,31 @@ jobs:

- name: Generate testnet domain genesis storages
run: |
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --chain gemini-3h > domain-genesis-storage-gemini-3h
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --chain devnet > domain-genesis-storage-devnet
EVM_SPEC_VERSION=$(sed -nr 's/.*spec_version: ([0-9]+),/\1/p' domains/runtime/evm/src/lib.rs)
AUTO_ID_SPEC_VERSION=$(sed -nr 's/.*spec_version: ([0-9]+),/\1/p' domains/runtime/auto-id/src/lib.rs)
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --runtime-type evm --chain gemini-3h > evm-domain-genesis-storage-gemini-3h-v$EVM_SPEC_VERSION
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --runtime-type evm --chain devnet > evm-domain-genesis-storage-devnet-v$EVM_SPEC_VERSION
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --runtime-type auto-id --chain gemini-3h > auto-id-domain-genesis-storage-gemini-3h-v$AUTO_ID_SPEC_VERSION
docker run --rm -u root ${{ steps.build.outputs.digest }} domain build-genesis-storage --runtime-type auto-id --chain devnet > auto-id-domain-genesis-storage-devnet-v$AUTO_ID_SPEC_VERSION
echo "EVM_SPEC_VERSION=$EVM_SPEC_VERSION" >> $GITHUB_ENV
echo "AUTO_ID_SPEC_VERSION=$AUTO_ID_SPEC_VERSION" >> $GITHUB_ENV
- name: Upload domain genesis storages to artifacts
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.1.3
with:
name: domain-genesis-storage
path: |
domain-genesis-storage-gemini-3h
domain-genesis-storage-devnet
evm-domain-genesis-storage-gemini-3h-v${{ env.EVM_SPEC_VERSION }}
evm-domain-genesis-storage-devnet-v${{ env.EVM_SPEC_VERSION }}
auto-id-domain-genesis-storage-gemini-3h-v${{ env.AUTO_ID_SPEC_VERSION }}
auto-id-domain-genesis-storage-devnet-v${{ env.AUTO_ID_SPEC_VERSION }}
if-no-files-found: error

- name: Upload domain genesis storages to assets
uses: alexellis/upload-assets@13926a61cdb2cb35f5fdef1c06b8b591523236d3 # 0.4.1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
asset_paths: '["domain-genesis-storage-gemini-3h", "domain-genesis-storage-devnet"]'
asset_paths: '["evm-domain-genesis-storage-gemini-3h-v${{ env.EVM_SPEC_VERSION }}", "evm-domain-genesis-storage-devnet-v${{ env.EVM_SPEC_VERSION }}", "auto-id-domain-genesis-storage-gemini-3h-v${{ env.AUTO_ID_SPEC_VERSION }}", "auto-id-domain-genesis-storage-devnet-v${{ env.AUTO_ID_SPEC_VERSION }}"]'
# Only run for releases
if: github.event_name == 'push' && github.ref_type == 'tag'
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ jobs:
${{ runner.os }}-cargo-
- name: Install cargo-nextest
uses: taiki-e/cache-cargo-install-action@5b024fe3a0a2c7f2aaff0e47871acf0d14b07207 # v2.0.0
uses: taiki-e/install-action@07a34f8347b1eeb5f5469cdfa451b0a5db2ae4e8 # 2.38.4
with:
tool: cargo-nextest

Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/rustsec-audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ jobs:
- uses: rustsec/audit-check@dd51754d4e59da7395a4cd9b593f0ff2d61a9b95 #v1.4.1
with:
token: ${{ secrets.GITHUB_TOKEN }}
# TODO: Remove once Substrate upgrades libp2p and we no longer have rustls 0.20.9 in our dependencies
ignore: RUSTSEC-2024-0336
# TODO: Remove once Substrate upgrades libp2p and we no longer have rustls 0.20.9 and curve25519-dalek 3.2.0
# in our dependencies: https://github.com/paritytech/polkadot-sdk/pull/1631
ignore: RUSTSEC-2024-0336,RUSTSEC-2024-0344
54 changes: 24 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 63 additions & 9 deletions crates/pallet-domains/src/block_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ extern crate alloc;

use crate::{
BalanceOf, BlockTree, BlockTreeNodeFor, BlockTreeNodes, Config, ConsensusBlockHash,
DomainBlockNumberFor, DomainHashingFor, ExecutionInbox, ExecutionReceiptOf,
HeadReceiptExtended, HeadReceiptNumber, InboxedBundleAuthor, LatestConfirmedDomainBlock,
LatestSubmittedER, Pallet, ReceiptHashFor,
DomainBlockNumberFor, DomainHashingFor, DomainRuntimeUpgradeRecords, ExecutionInbox,
ExecutionReceiptOf, HeadReceiptExtended, HeadReceiptNumber, InboxedBundleAuthor,
LatestConfirmedDomainBlock, LatestSubmittedER, Pallet, ReceiptHashFor,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Decode, Encode};
use frame_support::{ensure, PalletError};
use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::TypeInfo;
use sp_core::Get;
use sp_domains::merkle_tree::MerkleTree;
use sp_domains::{
ChainId, ConfirmedDomainBlock, DomainId, DomainsTransfersTracker, ExecutionReceipt, OperatorId,
Transfers,
ChainId, ConfirmedDomainBlock, DomainId, DomainsTransfersTracker, ExecutionReceipt,
OnChainRewards, OperatorId, Transfers,
};
use sp_runtime::traits::{BlockNumberProvider, CheckedSub, One, Saturating, Zero};
use sp_std::cmp::Ordering;
Expand Down Expand Up @@ -46,6 +47,8 @@ pub enum Error {
DomainTransfersTracking,
InvalidDomainTransfers,
OverwritingER,
RuntimeNotFound,
LastBlockNotFound,
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -211,18 +214,29 @@ pub(crate) fn verify_execution_receipt<T: Config>(
return Err(Error::InvalidExecutionTrace);
}

let maybe_domain_runtime_upgraded_at = {
let runtime_id = Pallet::<T>::runtime_id(domain_id).ok_or(Error::RuntimeNotFound)?;
DomainRuntimeUpgradeRecords::<T>::get(runtime_id).remove(consensus_block_number)
};

// Check if the ER is derived from the correct consensus block in the current chain
let excepted_consensus_block_hash =
match ConsensusBlockHash::<T>::get(domain_id, consensus_block_number) {
Some(hash) => hash,
// The `initialize_block` of non-system pallets is skipped in the `validate_transaction`,
// thus the hash of best block, which is recorded in the this pallet's `on_initialize` hook,
// is unavailable at this point.
None => {
// The `initialize_block` of non-system pallets is skipped in the `validate_transaction`,
// thus the hash of best block, which is recorded in the this pallet's `on_initialize` hook,
// is unavailable at this point.
let parent_block_number =
frame_system::Pallet::<T>::current_block_number() - One::one();
if *consensus_block_number == parent_block_number {
frame_system::Pallet::<T>::parent_hash()

// The domain runtime upgrade is forced to happen even if there is no bundle, in this case,
// the `ConsensusBlockHash` will be empty so we need to get the consensus block hash from
// `DomainRuntimeUpgradeRecords`
} else if let Some(ref upgrade_entry) = maybe_domain_runtime_upgraded_at {
upgrade_entry.at_hash
} else {
return Err(Error::UnavailableConsensusBlockHash);
}
Expand All @@ -241,7 +255,7 @@ pub(crate) fn verify_execution_receipt<T: Config>(
let expected_extrinsics_roots: Vec<_> =
execution_inbox.iter().map(|b| b.extrinsics_root).collect();
ensure!(
!bundles_extrinsics_roots.is_empty()
(!bundles_extrinsics_roots.is_empty() || maybe_domain_runtime_upgraded_at.is_some())
&& bundles_extrinsics_roots == expected_extrinsics_roots,
Error::InvalidExtrinsicsRoots
);
Expand Down Expand Up @@ -377,6 +391,20 @@ pub(crate) fn process_execution_receipt<T: Config>(
update_domain_transfers::<T>(domain_id, &execution_receipt.transfers, block_fees)
.map_err(|_| Error::DomainTransfersTracking)?;

update_domain_runtime_upgrade_records::<T>(
domain_id,
execution_receipt.consensus_block_number,
)?;

// handle chain rewards from the domain
execution_receipt
.block_fees
.chain_rewards
.into_iter()
.for_each(|(chain_id, reward)| {
T::OnChainRewards::on_chain_rewards(chain_id, reward)
});

LatestConfirmedDomainBlock::<T>::insert(
domain_id,
ConfirmedDomainBlock {
Expand Down Expand Up @@ -473,6 +501,32 @@ fn update_domain_transfers<T: Config>(
Ok(())
}

// Update the domain runtime upgrade record at `consensus_number` if there is one
fn update_domain_runtime_upgrade_records<T: Config>(
domain_id: DomainId,
consensus_number: BlockNumberFor<T>,
) -> Result<(), Error> {
let runtime_id = Pallet::<T>::runtime_id(domain_id).ok_or(Error::RuntimeNotFound)?;
let mut domain_runtime_upgrade_records = DomainRuntimeUpgradeRecords::<T>::get(runtime_id);

if let Some(upgrade_entry) = domain_runtime_upgrade_records.get_mut(&consensus_number) {
// Decrease the `reference_count` by one and remove the whole entry if it drop to zero
if upgrade_entry.reference_count > One::one() {
upgrade_entry.reference_count =
upgrade_entry.reference_count.saturating_sub(One::one());
} else {
domain_runtime_upgrade_records.remove(&consensus_number);
}

if !domain_runtime_upgrade_records.is_empty() {
DomainRuntimeUpgradeRecords::<T>::set(runtime_id, domain_runtime_upgrade_records);
} else {
DomainRuntimeUpgradeRecords::<T>::remove(runtime_id);
}
}
Ok(())
}

fn add_new_receipt_to_block_tree<T: Config>(
domain_id: DomainId,
submitter: OperatorId,
Expand Down
Loading

0 comments on commit c6770e1

Please sign in to comment.