migrate.move 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: Apache 2
  2. /// Note: this module is adapted from Wormhole's migrade.move module.
  3. ///
  4. /// This module implements a public method intended to be called after an
  5. /// upgrade has been commited. The purpose is to add one-off migration logic
  6. /// that would alter Pyth `State`.
  7. ///
  8. /// Included in migration is the ability to ensure that breaking changes for
  9. /// any of Pyth's methods by enforcing the current build version as
  10. /// their required minimum version.
  11. module pyth::migrate {
  12. use sui::object::{ID};
  13. use pyth::state::{Self, State};
  14. use pyth::contract_upgrade::{Self};
  15. use pyth::governance::{WormholeVAAVerificationReceipt};
  16. struct MigrateComplete has drop, copy {
  17. package: ID
  18. }
  19. public fun migrate(
  20. pyth_state: &mut State,
  21. receipt: WormholeVAAVerificationReceipt,
  22. ) {
  23. // Perform standard migrate.
  24. handle_migrate(pyth_state, receipt);
  25. ////////////////////////////////////////////////////////////////////////
  26. //
  27. // NOTE: Put any one-off migration logic here.
  28. //
  29. // Most upgrades likely won't need to do anything, in which case the
  30. // rest of this function's body may be empty. Make sure to delete it
  31. // after the migration has gone through successfully.
  32. //
  33. // WARNING: The migration does *not* proceed atomically with the
  34. // upgrade (as they are done in separate transactions).
  35. // If the nature of this migration absolutely requires the migration to
  36. // happen before certain other functionality is available, then guard
  37. // that functionality with the `assert!` from above.
  38. //
  39. ////////////////////////////////////////////////////////////////////////
  40. ////////////////////////////////////////////////////////////////////////
  41. }
  42. fun handle_migrate(
  43. pyth_state: &mut State,
  44. receipt: WormholeVAAVerificationReceipt,
  45. ) {
  46. // See `version_control` module for hard-coded configuration.
  47. state::migrate_version(pyth_state);
  48. // This capability ensures that the current build version is used.
  49. let latest_only = state::assert_latest_only(pyth_state);
  50. let digest = contract_upgrade::take_upgrade_digest(receipt);
  51. state::assert_authorized_digest(
  52. &latest_only,
  53. pyth_state,
  54. digest
  55. );
  56. // Finally emit an event reflecting a successful migrate.
  57. let package = state::current_package(&latest_only, pyth_state);
  58. sui::event::emit(MigrateComplete { package });
  59. }
  60. #[test_only]
  61. public fun set_up_migrate(pyth_state: &mut State) {
  62. state::reverse_migrate__v__0_1_0(pyth_state);
  63. }
  64. }