Skip to content

Commit 93ca9c4

Browse files
author
Dev Kalra
authored
migration logic (#687)
1 parent de0096e commit 93ca9c4

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

target_chains/cosmwasm/contracts/pyth/src/contract.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use {
2626
state::{
2727
config,
2828
config_read,
29+
deprecated_config,
30+
deprecated_config_read,
2931
price_feed_bucket,
3032
price_feed_read_bucket,
3133
ConfigInfo,
@@ -49,6 +51,7 @@ use {
4951
OverflowOperation,
5052
QueryRequest,
5153
Response,
54+
StdError,
5255
StdResult,
5356
WasmMsg,
5457
WasmQuery,
@@ -90,8 +93,30 @@ use {
9093
/// this function can safely be implemented as:
9194
/// `Ok(Response::default())`
9295
#[cfg_attr(not(feature = "library"), entry_point)]
93-
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
94-
Ok(Response::default())
96+
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
97+
let depreceated_cfg_result = deprecated_config_read(deps.storage).load();
98+
match depreceated_cfg_result {
99+
Ok(depreceated_cfg) => {
100+
let cfg = ConfigInfo {
101+
wormhole_contract: depreceated_cfg.wormhole_contract,
102+
data_sources: depreceated_cfg.data_sources,
103+
governance_source: depreceated_cfg.governance_source,
104+
governance_source_index: depreceated_cfg.governance_source_index,
105+
governance_sequence_number: depreceated_cfg.governance_sequence_number,
106+
chain_id: depreceated_cfg.chain_id,
107+
valid_time_period: depreceated_cfg.valid_time_period,
108+
fee: depreceated_cfg.fee,
109+
};
110+
111+
config(deps.storage).save(&cfg)?;
112+
deprecated_config(deps.storage).remove();
113+
114+
Ok(Response::default())
115+
}
116+
Err(_) => Err(StdError::GenericErr {
117+
msg: String::from("Error reading config"),
118+
}),
119+
}
95120
}
96121

97122
#[cfg_attr(not(feature = "library"), entry_point)]

target_chains/cosmwasm/contracts/pyth/src/state.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use {
2727
},
2828
};
2929

30-
pub static CONFIG_KEY: &[u8] = b"config";
30+
pub static DEPRECATED_CONFIG_KEY: &[u8] = b"config";
31+
pub static CONFIG_KEY: &[u8] = b"config_v1";
3132
pub static PRICE_FEED_KEY: &[u8] = b"price_feed";
3233

3334
/// 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> {
7879
pub fn price_feed_read_bucket(storage: &dyn Storage) -> ReadonlyBucket<PriceFeed> {
7980
bucket_read(storage, PRICE_FEED_KEY)
8081
}
82+
83+
84+
// this code is only added to facilititate migration
85+
// once migrated this code can be removed
86+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
87+
pub struct DepreceatedConfigInfo {
88+
pub owner: Addr,
89+
pub wormhole_contract: Addr,
90+
pub data_sources: HashSet<PythDataSource>,
91+
pub governance_source: PythDataSource,
92+
// Index for preventing replay attacks on governance data source transfers.
93+
// This index increases every time the governance data source is changed, which prevents old
94+
// transfer request VAAs from being replayed.
95+
pub governance_source_index: u32,
96+
// The wormhole sequence number for governance messages. This value is increased every time the
97+
// a governance instruction is executed.
98+
//
99+
// This field differs from the one above in that it is generated by wormhole and applicable to all
100+
// governance messages, whereas the one above is generated by Pyth and only applicable to governance
101+
// source transfers.
102+
pub governance_sequence_number: u64,
103+
// Warning: This id needs to agree with the wormhole chain id.
104+
// We should read this directly from wormhole, but their contract doesn't expose it.
105+
pub chain_id: u16,
106+
pub valid_time_period: Duration,
107+
108+
// The fee to pay, denominated in fee_denom (typically, the chain's native token)
109+
pub fee: Coin,
110+
}
111+
112+
pub fn deprecated_config(storage: &mut dyn Storage) -> Singleton<DepreceatedConfigInfo> {
113+
singleton(storage, DEPRECATED_CONFIG_KEY)
114+
}
115+
116+
pub fn deprecated_config_read(storage: &dyn Storage) -> ReadonlySingleton<DepreceatedConfigInfo> {
117+
singleton_read(storage, DEPRECATED_CONFIG_KEY)
118+
}

0 commit comments

Comments
 (0)