|
| 1 | +--- |
| 2 | +id: prepare-sc-supernova |
| 3 | +title: Preparing SCs for Supernova |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +The MultiversX Supernova upgrade reduces block time from **6 seconds to 0.6 seconds**, enabling sub-second blocks. While this is a major improvement, it can impact existing smart contracts, especially those relying on assumptions about timestamp behavior. |
| 8 | + |
| 9 | +This guide explains how to prepare your contracts for Supernova safely. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +[comment]: # (mx-context-auto) |
| 14 | + |
| 15 | +## Understand What Changes — and What Doesn’t |
| 16 | + |
| 17 | +All existing timestamp APIs continue returning **seconds** unless you explicitly call the millisecond versions. Nothing changes silently in the VM, the framework, or deployed contracts. |
| 18 | + |
| 19 | +Obviously, contracts that have the 6 seconds between blocks hardcoded will have to change. But more importantly, block times expressed in seconds no longer uniquely identify a block, which leads to potential problems. |
| 20 | + |
| 21 | +We are going to go through the most important patterns to look out for. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +[comment]: # (mx-context-auto) |
| 26 | + |
| 27 | +## Potential problems to look out for |
| 28 | + |
| 29 | +[comment]: # (mx-context-auto) |
| 30 | + |
| 31 | +### Replace Hardcoded Block Timing |
| 32 | + |
| 33 | +If your contract uses hard-coded constants (like `6 seconds` or `6000 milliseconds`) to estimate the time between blocks, this logic will need to be changed. |
| 34 | + |
| 35 | +It might estimate durations from nonce deltas, or vice-versa, it might estimate number of blocks by timestamps. |
| 36 | + |
| 37 | +**Fix:** Use the API instead: `self.blockchain().get_block_round_time_millis()`. This returns `6000` (as `DurationMillis`) today and `600` after Supernova. |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +[comment]: # (mx-context-auto) |
| 42 | + |
| 43 | +### Avoid Timestamp-Based Monotonicity |
| 44 | + |
| 45 | +Logic such as: |
| 46 | + |
| 47 | +``` |
| 48 | +require!(ts_now > last_ts) |
| 49 | +``` |
| 50 | + |
| 51 | +may break because multiple blocks can share the same timestamp. |
| 52 | + |
| 53 | +**Fix:** Use **block nonces** for guaranteed monotonicity. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +[comment]: # (mx-context-auto) |
| 58 | + |
| 59 | +### Prevent Rate-Limit Bypasses |
| 60 | + |
| 61 | +If your contract allows one action “per block” but checks the difference in **seconds**, multiple blocks in the same second can bypass restrictions. |
| 62 | + |
| 63 | +**Fix:** Use block nonces or switch to millisecond timestamps. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +[comment]: # (mx-context-auto) |
| 68 | + |
| 69 | +### Revisit Expiration Logic |
| 70 | + |
| 71 | +Expiration logic written assuming a fixed block interval may accidentally allow: |
| 72 | + |
| 73 | +* extra blocks before expiration |
| 74 | +* longer-than-intended windows for execution |
| 75 | + |
| 76 | +If your expiration logic uses seconds, double-check assumptions. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +[comment]: # (mx-context-auto) |
| 81 | + |
| 82 | +### Stop Using Timestamps as Block Identifiers |
| 83 | + |
| 84 | +Before Supernova, a timestamp could uniquely identify a block. |
| 85 | +After Supernova, multiple blocks may share the same timestamp. |
| 86 | + |
| 87 | +If you use timestamps as map keys or identifiers: |
| 88 | + |
| 89 | +* collisions will occur |
| 90 | +* data may be overwritten or skipped |
| 91 | + |
| 92 | +**Fix:** Use block **nonces**. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +[comment]: # (mx-context-auto) |
| 97 | + |
| 98 | +### Review Reward and Accumulation Calculations |
| 99 | + |
| 100 | +Reward logic that uses: |
| 101 | + |
| 102 | +``` |
| 103 | +delta = ts_now - last_ts |
| 104 | +``` |
| 105 | + |
| 106 | +may behave unexpectedly: |
| 107 | + |
| 108 | +* delta may be zero over multiple blocks |
| 109 | +* rewards might accumulate slower or unevenly |
| 110 | +* divisions by delta may cause division-by-zero errors |
| 111 | + |
| 112 | +Consider switching to milliseconds for finer granularity. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +[comment]: # (mx-context-auto) |
| 117 | + |
| 118 | +## Migration Guidance |
| 119 | + |
| 120 | +Just as important as fixing potential issues with Supernova, it is essential not to introduce new bugs in the process. |
| 121 | + |
| 122 | +Make sure to take the following into consideration when migrating: |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +[comment]: # (mx-context-auto) |
| 127 | + |
| 128 | +### Seconds vs. Milliseconds |
| 129 | + |
| 130 | +Switching from second to millisecond timestamp can be error-prone. |
| 131 | + |
| 132 | +The most dangerous bug is accidentally mixing second and millisecond values, causing storage and logic corruption. |
| 133 | + |
| 134 | +To prevent this issue, use the [strongly typed timestamp and duration objects](time-types). |
| 135 | + |
| 136 | +Starting in **multiversx-sc v0.63.0**, all timestamp APIs have typed replacements: |
| 137 | + |
| 138 | +* `get_block_timestamp_seconds()` |
| 139 | +* `get_block_timestamp_millis()` |
| 140 | +* `get_prev_block_timestamp_seconds()` |
| 141 | +* `get_prev_block_timestamp_millis()` |
| 142 | +* `get_block_round_time_millis()` |
| 143 | +* `epoch_start_block_timestamp_millis()` |
| 144 | + |
| 145 | +And avoid using raw `u64` for time values. |
| 146 | + |
| 147 | +Make sure to convert form `u64` to type timestamps **before** doing any other refactoring. It is much safer this way. |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +[comment]: # (mx-context-auto) |
| 152 | + |
| 153 | +### Backwards compatibility |
| 154 | + |
| 155 | +When upgrading an existing contract from second to millisecond timestamps, it is essential to: |
| 156 | + |
| 157 | +* Ensure storage remains consistent |
| 158 | +* Update ESDT metadata carefully |
| 159 | +* Add compatibility logic if needed |
| 160 | + |
| 161 | +If you are unsure that this can be done safely, it might be safer to keep the contract running on second timestamps. |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +[comment]: # (mx-context-auto) |
| 166 | + |
| 167 | +## Summary Checklist |
| 168 | + |
| 169 | +To prepare for Supernova: |
| 170 | + |
| 171 | +* [ ] Upgrade to `multiversx-sc v0.63.0` |
| 172 | +* [ ] Use typed timestamp/duration APIs |
| 173 | +* [ ] Remove all hardcoded 6-second or 6000-millisecond assumptions |
| 174 | +* [ ] Avoid using timestamps as block identifiers |
| 175 | +* [ ] Review expiration, reward, and accumulation logic |
| 176 | +* [ ] Switch to millisecond timestamps where appropriate |
| 177 | +* [ ] Ensure storage/metadata compatibility |
| 178 | +* [ ] Update tests to use for millisecond block timestamps, where needed |
0 commit comments