migrate.move 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 wormhole::governance_message::{Self, DecreeReceipt};
  14. use pyth::state::{Self, State};
  15. use pyth::contract_upgrade::{Self};
  16. use pyth::governance_witness::{GovernanceWitness};
  17. struct MigrateComplete has drop, copy {
  18. package: ID
  19. }
  20. public fun migrate(
  21. pyth_state: &mut State,
  22. receipt: DecreeReceipt<GovernanceWitness>
  23. ) {
  24. // Perform standard migrate.
  25. handle_migrate(pyth_state, receipt);
  26. ////////////////////////////////////////////////////////////////////////
  27. //
  28. // NOTE: Put any one-off migration logic here.
  29. //
  30. // Most upgrades likely won't need to do anything, in which case the
  31. // rest of this function's body may be empty. Make sure to delete it
  32. // after the migration has gone through successfully.
  33. //
  34. // WARNING: The migration does *not* proceed atomically with the
  35. // upgrade (as they are done in separate transactions).
  36. // If the nature of this migration absolutely requires the migration to
  37. // happen before certain other functionality is available, then guard
  38. // that functionality with the `assert!` from above.
  39. //
  40. ////////////////////////////////////////////////////////////////////////
  41. ////////////////////////////////////////////////////////////////////////
  42. }
  43. fun handle_migrate(
  44. pyth_state: &mut State,
  45. receipt: DecreeReceipt<GovernanceWitness>
  46. ) {
  47. // See `version_control` module for hard-coded configuration.
  48. state::migrate_version(pyth_state);
  49. // This capability ensures that the current build version is used.
  50. let latest_only = state::assert_latest_only(pyth_state);
  51. // Check if build digest is the current one.
  52. let digest =
  53. contract_upgrade::take_digest(
  54. governance_message::payload(&receipt)
  55. );
  56. state::assert_authorized_digest(
  57. &latest_only,
  58. pyth_state,
  59. digest
  60. );
  61. governance_message::destroy(receipt);
  62. // Finally emit an event reflecting a successful migrate.
  63. let package = state::current_package(&latest_only, pyth_state);
  64. sui::event::emit(MigrateComplete { package });
  65. }
  66. #[test_only]
  67. public fun set_up_migrate(pyth_state: &mut State) {
  68. state::reverse_migrate__v__0_1_0(pyth_state);
  69. }
  70. }