|
| 1 | +//! ## Overview |
| 2 | +//! Shadows the contract state written by the `3.13.0` release so [`crate::migrate`] |
| 3 | +//! can upgrade from it. See [`crate::v3_12_0_state`] for the rationale and guideline. |
| 4 | +//! |
| 5 | +//! `3.13.0` differs from the live layout only by the two fields this version adds: |
| 6 | +//! `Config::fail_attestation_submission_tera_gas` (and the three verifier gas knobs, |
| 7 | +//! all defaulted here) and the `MpcContract::pending_attestations` map. |
| 8 | +
|
| 9 | +use borsh::{BorshDeserialize, BorshSerialize}; |
| 10 | +use near_mpc_contract_interface::types::{Metrics, VerifyForeignTransactionRequest}; |
| 11 | +use near_sdk::{ |
| 12 | + AccountId, env, |
| 13 | + store::{Lazy, LookupMap}, |
| 14 | +}; |
| 15 | + |
| 16 | +use crate::{ |
| 17 | + SupportedForeignChainsByNode, |
| 18 | + foreign_chains_metadata::ForeignChainsMetadata, |
| 19 | + node_migrations::NodeMigrations, |
| 20 | + primitives::{ |
| 21 | + ckd::CKDRequest, |
| 22 | + domain::max_reconstruction_threshold, |
| 23 | + signature::{SignatureRequest, YieldIndex}, |
| 24 | + thresholds::ThresholdParameters, |
| 25 | + }, |
| 26 | + state::{ProtocolContractState, running::RunningContractState}, |
| 27 | + storage_keys::StorageKey, |
| 28 | + tee::{tee_state::TeeState, verifier_votes::TeeVerifierVotes}, |
| 29 | + update::ProposedUpdates, |
| 30 | +}; |
| 31 | + |
| 32 | +/// The `Config` layout written by the `3.13.0` contract, before |
| 33 | +/// `fail_attestation_submission_tera_gas` and the verifier gas knobs were added. |
| 34 | +#[derive(Debug, BorshSerialize, BorshDeserialize)] |
| 35 | +pub struct OldConfig { |
| 36 | + key_event_timeout_blocks: u64, |
| 37 | + tee_upgrade_deadline_duration_seconds: u64, |
| 38 | + contract_upgrade_deposit_tera_gas: u64, |
| 39 | + sign_call_gas_attachment_requirement_tera_gas: u64, |
| 40 | + ckd_call_gas_attachment_requirement_tera_gas: u64, |
| 41 | + return_signature_and_clean_state_on_success_call_tera_gas: u64, |
| 42 | + return_ck_and_clean_state_on_success_call_tera_gas: u64, |
| 43 | + fail_on_timeout_tera_gas: u64, |
| 44 | + clean_tee_status_tera_gas: u64, |
| 45 | + clean_invalid_attestations_tera_gas: u64, |
| 46 | + cleanup_orphaned_node_migrations_tera_gas: u64, |
| 47 | + remove_non_participant_update_votes_tera_gas: u64, |
| 48 | + clean_foreign_chain_data_tera_gas: u64, |
| 49 | + remove_non_participant_tee_verifier_votes_tera_gas: u64, |
| 50 | +} |
| 51 | + |
| 52 | +impl From<OldConfig> for crate::Config { |
| 53 | + fn from(old: OldConfig) -> Self { |
| 54 | + crate::Config { |
| 55 | + key_event_timeout_blocks: old.key_event_timeout_blocks, |
| 56 | + tee_upgrade_deadline_duration_seconds: old.tee_upgrade_deadline_duration_seconds, |
| 57 | + contract_upgrade_deposit_tera_gas: old.contract_upgrade_deposit_tera_gas, |
| 58 | + sign_call_gas_attachment_requirement_tera_gas: old |
| 59 | + .sign_call_gas_attachment_requirement_tera_gas, |
| 60 | + ckd_call_gas_attachment_requirement_tera_gas: old |
| 61 | + .ckd_call_gas_attachment_requirement_tera_gas, |
| 62 | + return_signature_and_clean_state_on_success_call_tera_gas: old |
| 63 | + .return_signature_and_clean_state_on_success_call_tera_gas, |
| 64 | + return_ck_and_clean_state_on_success_call_tera_gas: old |
| 65 | + .return_ck_and_clean_state_on_success_call_tera_gas, |
| 66 | + fail_on_timeout_tera_gas: old.fail_on_timeout_tera_gas, |
| 67 | + clean_tee_status_tera_gas: old.clean_tee_status_tera_gas, |
| 68 | + clean_invalid_attestations_tera_gas: old.clean_invalid_attestations_tera_gas, |
| 69 | + cleanup_orphaned_node_migrations_tera_gas: old |
| 70 | + .cleanup_orphaned_node_migrations_tera_gas, |
| 71 | + remove_non_participant_update_votes_tera_gas: old |
| 72 | + .remove_non_participant_update_votes_tera_gas, |
| 73 | + clean_foreign_chain_data_tera_gas: old.clean_foreign_chain_data_tera_gas, |
| 74 | + remove_non_participant_tee_verifier_votes_tera_gas: old |
| 75 | + .remove_non_participant_tee_verifier_votes_tera_gas, |
| 76 | + // New in this version: the attestation fail-call and verifier-call gas |
| 77 | + // knobs, added alongside the async attestation flow. |
| 78 | + ..crate::Config::default() |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Keep this module in sync with [`crate::MpcContract`]: it is the `3.13.0` layout, |
| 84 | +/// which differs only by the appended `pending_attestations` map. |
| 85 | +#[derive(Debug, BorshSerialize, BorshDeserialize)] |
| 86 | +pub struct MpcContract { |
| 87 | + protocol_state: ProtocolContractState, |
| 88 | + pending_signature_requests: LookupMap<SignatureRequest, Vec<YieldIndex>>, |
| 89 | + pending_ckd_requests: LookupMap<CKDRequest, Vec<YieldIndex>>, |
| 90 | + pending_verify_foreign_tx_requests: LookupMap<VerifyForeignTransactionRequest, Vec<YieldIndex>>, |
| 91 | + proposed_updates: ProposedUpdates, |
| 92 | + node_foreign_chain_support: SupportedForeignChainsByNode, |
| 93 | + config: OldConfig, |
| 94 | + tee_state: TeeState, |
| 95 | + accept_requests: bool, |
| 96 | + node_migrations: NodeMigrations, |
| 97 | + metrics: Metrics, |
| 98 | + foreign_chains: Lazy<ForeignChainsMetadata>, |
| 99 | + tee_verifier_account_id: Option<AccountId>, |
| 100 | + tee_verifier_votes: TeeVerifierVotes, |
| 101 | +} |
| 102 | + |
| 103 | +impl From<MpcContract> for crate::MpcContract { |
| 104 | + fn from(old: MpcContract) -> Self { |
| 105 | + if let ProtocolContractState::Running(running) = &old.protocol_state { |
| 106 | + validate_threshold_relation_on_migration(running); |
| 107 | + } |
| 108 | + |
| 109 | + crate::MpcContract { |
| 110 | + protocol_state: old.protocol_state, |
| 111 | + pending_signature_requests: old.pending_signature_requests, |
| 112 | + pending_ckd_requests: old.pending_ckd_requests, |
| 113 | + pending_verify_foreign_tx_requests: old.pending_verify_foreign_tx_requests, |
| 114 | + proposed_updates: old.proposed_updates, |
| 115 | + node_foreign_chain_support: old.node_foreign_chain_support, |
| 116 | + config: old.config.into(), |
| 117 | + tee_state: old.tee_state, |
| 118 | + accept_requests: old.accept_requests, |
| 119 | + node_migrations: old.node_migrations, |
| 120 | + metrics: old.metrics, |
| 121 | + foreign_chains: old.foreign_chains, |
| 122 | + tee_verifier_account_id: old.tee_verifier_account_id, |
| 123 | + tee_verifier_votes: old.tee_verifier_votes, |
| 124 | + pending_attestations: LookupMap::new(StorageKey::PendingAttestations), |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn validate_threshold_relation_on_migration(running: &RunningContractState) { |
| 130 | + let num_participants = running.parameters.participants().len() as u64; |
| 131 | + let max_reconstruction_threshold = max_reconstruction_threshold(running.domains.domains()); |
| 132 | + if let Err(err) = ThresholdParameters::validate_governance_against_reconstruction( |
| 133 | + num_participants, |
| 134 | + running.parameters.threshold(), |
| 135 | + max_reconstruction_threshold, |
| 136 | + ) { |
| 137 | + env::panic_str(&format!( |
| 138 | + "Migration aborted: existing state violates the GovernanceThreshold/ReconstructionThreshold relation ({err:?}). num_participants={}, governance_threshold={}, max_reconstruction_threshold={:?}. Correct it via vote_new_parameters before upgrading.", |
| 139 | + num_participants, |
| 140 | + running.parameters.threshold().value(), |
| 141 | + max_reconstruction_threshold.map(|t| t.inner()), |
| 142 | + )); |
| 143 | + } |
| 144 | +} |
0 commit comments