Quellcode durchsuchen

migration logic (#687)

Dev Kalra vor 2 Jahren
Ursprung
Commit
93ca9c427a

+ 27 - 2
target_chains/cosmwasm/contracts/pyth/src/contract.rs

@@ -26,6 +26,8 @@ use {
         state::{
             config,
             config_read,
+            deprecated_config,
+            deprecated_config_read,
             price_feed_bucket,
             price_feed_read_bucket,
             ConfigInfo,
@@ -49,6 +51,7 @@ use {
         OverflowOperation,
         QueryRequest,
         Response,
+        StdError,
         StdResult,
         WasmMsg,
         WasmQuery,
@@ -90,8 +93,30 @@ use {
 /// this function can safely be implemented as:
 /// `Ok(Response::default())`
 #[cfg_attr(not(feature = "library"), entry_point)]
-pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
-    Ok(Response::default())
+pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
+    let depreceated_cfg_result = deprecated_config_read(deps.storage).load();
+    match depreceated_cfg_result {
+        Ok(depreceated_cfg) => {
+            let cfg = ConfigInfo {
+                wormhole_contract:          depreceated_cfg.wormhole_contract,
+                data_sources:               depreceated_cfg.data_sources,
+                governance_source:          depreceated_cfg.governance_source,
+                governance_source_index:    depreceated_cfg.governance_source_index,
+                governance_sequence_number: depreceated_cfg.governance_sequence_number,
+                chain_id:                   depreceated_cfg.chain_id,
+                valid_time_period:          depreceated_cfg.valid_time_period,
+                fee:                        depreceated_cfg.fee,
+            };
+
+            config(deps.storage).save(&cfg)?;
+            deprecated_config(deps.storage).remove();
+
+            Ok(Response::default())
+        }
+        Err(_) => Err(StdError::GenericErr {
+            msg: String::from("Error reading config"),
+        }),
+    }
 }
 
 #[cfg_attr(not(feature = "library"), entry_point)]

+ 39 - 1
target_chains/cosmwasm/contracts/pyth/src/state.rs

@@ -27,7 +27,8 @@ use {
     },
 };
 
-pub static CONFIG_KEY: &[u8] = b"config";
+pub static DEPRECATED_CONFIG_KEY: &[u8] = b"config";
+pub static CONFIG_KEY: &[u8] = b"config_v1";
 pub static PRICE_FEED_KEY: &[u8] = b"price_feed";
 
 /// A `PythDataSource` identifies a specific contract (given by its Wormhole `emitter`) on
@@ -78,3 +79,40 @@ pub fn price_feed_bucket(storage: &mut dyn Storage) -> Bucket<PriceFeed> {
 pub fn price_feed_read_bucket(storage: &dyn Storage) -> ReadonlyBucket<PriceFeed> {
     bucket_read(storage, PRICE_FEED_KEY)
 }
+
+
+// this code is only added to facilititate migration
+// once migrated this code can be removed
+#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
+pub struct DepreceatedConfigInfo {
+    pub owner:                      Addr,
+    pub wormhole_contract:          Addr,
+    pub data_sources:               HashSet<PythDataSource>,
+    pub governance_source:          PythDataSource,
+    // Index for preventing replay attacks on governance data source transfers.
+    // This index increases every time the governance data source is changed, which prevents old
+    // transfer request VAAs from being replayed.
+    pub governance_source_index:    u32,
+    // The wormhole sequence number for governance messages. This value is increased every time the
+    // a governance instruction is executed.
+    //
+    // This field differs from the one above in that it is generated by wormhole and applicable to all
+    // governance messages, whereas the one above is generated by Pyth and only applicable to governance
+    // source transfers.
+    pub governance_sequence_number: u64,
+    // Warning: This id needs to agree with the wormhole chain id.
+    // We should read this directly from wormhole, but their contract doesn't expose it.
+    pub chain_id:                   u16,
+    pub valid_time_period:          Duration,
+
+    // The fee to pay, denominated in fee_denom (typically, the chain's native token)
+    pub fee: Coin,
+}
+
+pub fn deprecated_config(storage: &mut dyn Storage) -> Singleton<DepreceatedConfigInfo> {
+    singleton(storage, DEPRECATED_CONFIG_KEY)
+}
+
+pub fn deprecated_config_read(storage: &dyn Storage) -> ReadonlySingleton<DepreceatedConfigInfo> {
+    singleton_read(storage, DEPRECATED_CONFIG_KEY)
+}