@layerzerolabs/protocol-stellar-v2 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (265) hide show
  1. package/.turbo/turbo-build.log +727 -0
  2. package/.turbo/turbo-lint.log +158 -0
  3. package/.turbo/turbo-test.log +796 -0
  4. package/Cargo.lock +2237 -0
  5. package/Cargo.toml +63 -0
  6. package/clippy.toml +7 -0
  7. package/contracts/common-macros/Cargo.toml +20 -0
  8. package/contracts/common-macros/src/error.rs +53 -0
  9. package/contracts/common-macros/src/event.rs +16 -0
  10. package/contracts/common-macros/src/lib.rs +255 -0
  11. package/contracts/common-macros/src/ownable.rs +63 -0
  12. package/contracts/common-macros/src/snapshots/common_macros__tests__tests__snapshot_generated_storage_code.snap +310 -0
  13. package/contracts/common-macros/src/storage.rs +439 -0
  14. package/contracts/common-macros/src/tests.rs +287 -0
  15. package/contracts/common-macros/src/ttl_configurable.rs +60 -0
  16. package/contracts/endpoint-v2/ARCHITECTURE.md +233 -0
  17. package/contracts/endpoint-v2/Cargo.toml +30 -0
  18. package/contracts/endpoint-v2/src/constants.rs +52 -0
  19. package/contracts/endpoint-v2/src/endpoint_v2.rs +305 -0
  20. package/contracts/endpoint-v2/src/errors.rs +29 -0
  21. package/contracts/endpoint-v2/src/events.rs +207 -0
  22. package/contracts/endpoint-v2/src/interfaces/layerzero_composer.rs +26 -0
  23. package/contracts/endpoint-v2/src/interfaces/layerzero_endpoint_v2.rs +170 -0
  24. package/contracts/endpoint-v2/src/interfaces/layerzero_receiver.rs +43 -0
  25. package/contracts/endpoint-v2/src/interfaces/message_lib.rs +62 -0
  26. package/contracts/endpoint-v2/src/interfaces/message_lib_manager.rs +220 -0
  27. package/contracts/endpoint-v2/src/interfaces/messaging_channel.rs +121 -0
  28. package/contracts/endpoint-v2/src/interfaces/messaging_composer.rs +63 -0
  29. package/contracts/endpoint-v2/src/interfaces/mod.rs +17 -0
  30. package/contracts/endpoint-v2/src/interfaces/send_lib.rs +70 -0
  31. package/contracts/endpoint-v2/src/lib.rs +22 -0
  32. package/contracts/endpoint-v2/src/message_lib_manager.rs +315 -0
  33. package/contracts/endpoint-v2/src/messaging_channel.rs +218 -0
  34. package/contracts/endpoint-v2/src/messaging_composer.rs +76 -0
  35. package/contracts/endpoint-v2/src/storage.rs +78 -0
  36. package/contracts/endpoint-v2/src/tests/endpoint_setup.rs +131 -0
  37. package/contracts/endpoint-v2/src/tests/endpoint_v2/clear.rs +237 -0
  38. package/contracts/endpoint-v2/src/tests/endpoint_v2/delegate.rs +42 -0
  39. package/contracts/endpoint-v2/src/tests/endpoint_v2/initializable.rs +76 -0
  40. package/contracts/endpoint-v2/src/tests/endpoint_v2/lz_receive_alert.rs +211 -0
  41. package/contracts/endpoint-v2/src/tests/endpoint_v2/mod.rs +18 -0
  42. package/contracts/endpoint-v2/src/tests/endpoint_v2/native_token.rs +10 -0
  43. package/contracts/endpoint-v2/src/tests/endpoint_v2/owner.rs +10 -0
  44. package/contracts/endpoint-v2/src/tests/endpoint_v2/pay_messaging_fees.rs +424 -0
  45. package/contracts/endpoint-v2/src/tests/endpoint_v2/quote.rs +144 -0
  46. package/contracts/endpoint-v2/src/tests/endpoint_v2/recover_token.rs +72 -0
  47. package/contracts/endpoint-v2/src/tests/endpoint_v2/require_oapp_auth.rs +29 -0
  48. package/contracts/endpoint-v2/src/tests/endpoint_v2/send.rs +513 -0
  49. package/contracts/endpoint-v2/src/tests/endpoint_v2/set_delegate.rs +43 -0
  50. package/contracts/endpoint-v2/src/tests/endpoint_v2/set_zro.rs +27 -0
  51. package/contracts/endpoint-v2/src/tests/endpoint_v2/transfer_ownership.rs +30 -0
  52. package/contracts/endpoint-v2/src/tests/endpoint_v2/ttl_config.rs +202 -0
  53. package/contracts/endpoint-v2/src/tests/endpoint_v2/verifiable.rs +59 -0
  54. package/contracts/endpoint-v2/src/tests/endpoint_v2/verify.rs +172 -0
  55. package/contracts/endpoint-v2/src/tests/endpoint_v2/zro.rs +23 -0
  56. package/contracts/endpoint-v2/src/tests/message_lib_manager/mod.rs +10 -0
  57. package/contracts/endpoint-v2/src/tests/message_lib_manager/register_library.rs +131 -0
  58. package/contracts/endpoint-v2/src/tests/message_lib_manager/require_registered.rs +35 -0
  59. package/contracts/endpoint-v2/src/tests/message_lib_manager/require_supported_eid.rs +28 -0
  60. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_config.rs +79 -0
  61. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_default_receive_lib_timeout.rs +246 -0
  62. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_default_receive_library.rs +285 -0
  63. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_default_send_library.rs +180 -0
  64. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_receive_library.rs +405 -0
  65. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_receive_library_timeout.rs +80 -0
  66. package/contracts/endpoint-v2/src/tests/message_lib_manager/set_send_library.rs +131 -0
  67. package/contracts/endpoint-v2/src/tests/messaging_channel/burn.rs +358 -0
  68. package/contracts/endpoint-v2/src/tests/messaging_channel/clear.rs +316 -0
  69. package/contracts/endpoint-v2/src/tests/messaging_channel/inbound_nonce.rs +288 -0
  70. package/contracts/endpoint-v2/src/tests/messaging_channel/inbound_payload_hash.rs +316 -0
  71. package/contracts/endpoint-v2/src/tests/messaging_channel/internal.rs +388 -0
  72. package/contracts/endpoint-v2/src/tests/messaging_channel/lazy_inbound_nonce.rs +307 -0
  73. package/contracts/endpoint-v2/src/tests/messaging_channel/mod.rs +10 -0
  74. package/contracts/endpoint-v2/src/tests/messaging_channel/next_guid.rs +239 -0
  75. package/contracts/endpoint-v2/src/tests/messaging_channel/nilify.rs +324 -0
  76. package/contracts/endpoint-v2/src/tests/messaging_channel/outbound_nonce.rs +242 -0
  77. package/contracts/endpoint-v2/src/tests/messaging_channel/skip.rs +232 -0
  78. package/contracts/endpoint-v2/src/tests/messaging_composer/clear_compose.rs +212 -0
  79. package/contracts/endpoint-v2/src/tests/messaging_composer/compose_queue.rs +213 -0
  80. package/contracts/endpoint-v2/src/tests/messaging_composer/lz_compose_alert.rs +269 -0
  81. package/contracts/endpoint-v2/src/tests/messaging_composer/mod.rs +4 -0
  82. package/contracts/endpoint-v2/src/tests/messaging_composer/send_compose.rs +173 -0
  83. package/contracts/endpoint-v2/src/tests/mock.rs +132 -0
  84. package/contracts/endpoint-v2/src/tests/mod.rs +12 -0
  85. package/contracts/endpoint-v2/src/tests/util/build_payload.rs +126 -0
  86. package/contracts/endpoint-v2/src/tests/util/compute_guid.rs +82 -0
  87. package/contracts/endpoint-v2/src/tests/util/keccak256.rs +115 -0
  88. package/contracts/endpoint-v2/src/tests/util/mod.rs +3 -0
  89. package/contracts/endpoint-v2/src/util.rs +52 -0
  90. package/contracts/message-libs/Cargo.toml +12 -0
  91. package/contracts/message-libs/block-message-lib/Cargo.toml +19 -0
  92. package/contracts/message-libs/block-message-lib/src/lib.rs +70 -0
  93. package/contracts/message-libs/lib.rs +2 -0
  94. package/contracts/message-libs/message-lib-common/Cargo.toml +24 -0
  95. package/contracts/message-libs/message-lib-common/src/errors.rs +20 -0
  96. package/contracts/message-libs/message-lib-common/src/interfaces/dvn.rs +55 -0
  97. package/contracts/message-libs/message-lib-common/src/interfaces/executor.rs +46 -0
  98. package/contracts/message-libs/message-lib-common/src/interfaces/mod.rs +7 -0
  99. package/contracts/message-libs/message-lib-common/src/interfaces/treasury.rs +17 -0
  100. package/contracts/message-libs/message-lib-common/src/lib.rs +14 -0
  101. package/contracts/message-libs/message-lib-common/src/packet_codec_v1.rs +99 -0
  102. package/contracts/message-libs/message-lib-common/src/testing_utils.rs +27 -0
  103. package/contracts/message-libs/message-lib-common/src/tests/mod.rs +2 -0
  104. package/contracts/message-libs/message-lib-common/src/tests/packet_codec_v1.rs +162 -0
  105. package/contracts/message-libs/message-lib-common/src/tests/worker_options.rs +319 -0
  106. package/contracts/message-libs/message-lib-common/src/worker_options.rs +190 -0
  107. package/contracts/message-libs/simple-message-lib/Cargo.toml +26 -0
  108. package/contracts/message-libs/simple-message-lib/src/errors.rs +11 -0
  109. package/contracts/message-libs/simple-message-lib/src/lib.rs +14 -0
  110. package/contracts/message-libs/simple-message-lib/src/simple_message_lib.rs +136 -0
  111. package/contracts/message-libs/simple-message-lib/src/storage.rs +27 -0
  112. package/contracts/message-libs/simple-message-lib/src/test.rs +280 -0
  113. package/contracts/message-libs/treasury/Cargo.toml +27 -0
  114. package/contracts/message-libs/treasury/src/errors.rs +10 -0
  115. package/contracts/message-libs/treasury/src/events.rs +28 -0
  116. package/contracts/message-libs/treasury/src/interfaces/mod.rs +3 -0
  117. package/contracts/message-libs/treasury/src/interfaces/zro_fee_lib.rs +20 -0
  118. package/contracts/message-libs/treasury/src/lib.rs +20 -0
  119. package/contracts/message-libs/treasury/src/storage.rs +18 -0
  120. package/contracts/message-libs/treasury/src/tests/mod.rs +2 -0
  121. package/contracts/message-libs/treasury/src/tests/setup.rs +112 -0
  122. package/contracts/message-libs/treasury/src/tests/treasury_tests.rs +562 -0
  123. package/contracts/message-libs/treasury/src/treasury.rs +140 -0
  124. package/contracts/message-libs/uln-302/Cargo.toml +28 -0
  125. package/contracts/message-libs/uln-302/src/config_validation.rs +173 -0
  126. package/contracts/message-libs/uln-302/src/errors.rs +29 -0
  127. package/contracts/message-libs/uln-302/src/events.rs +72 -0
  128. package/contracts/message-libs/uln-302/src/interfaces/mod.rs +5 -0
  129. package/contracts/message-libs/uln-302/src/interfaces/receive.rs +82 -0
  130. package/contracts/message-libs/uln-302/src/interfaces/send.rs +159 -0
  131. package/contracts/message-libs/uln-302/src/lib.rs +20 -0
  132. package/contracts/message-libs/uln-302/src/receive.rs +199 -0
  133. package/contracts/message-libs/uln-302/src/send.rs +349 -0
  134. package/contracts/message-libs/uln-302/src/storage.rs +47 -0
  135. package/contracts/message-libs/uln-302/src/tests/config/mod.rs +2 -0
  136. package/contracts/message-libs/uln-302/src/tests/config/oapp_uln_config.rs +291 -0
  137. package/contracts/message-libs/uln-302/src/tests/config/uln_config.rs +163 -0
  138. package/contracts/message-libs/uln-302/src/tests/mod.rs +7 -0
  139. package/contracts/message-libs/uln-302/src/tests/receive_uln302/commit_verification.rs +183 -0
  140. package/contracts/message-libs/uln-302/src/tests/receive_uln302/confirmations.rs +128 -0
  141. package/contracts/message-libs/uln-302/src/tests/receive_uln302/effective_receive_uln_config.rs +104 -0
  142. package/contracts/message-libs/uln-302/src/tests/receive_uln302/mod.rs +66 -0
  143. package/contracts/message-libs/uln-302/src/tests/receive_uln302/set_default_receive_uln_configs.rs +79 -0
  144. package/contracts/message-libs/uln-302/src/tests/receive_uln302/verifiable.rs +463 -0
  145. package/contracts/message-libs/uln-302/src/tests/receive_uln302/verify.rs +173 -0
  146. package/contracts/message-libs/uln-302/src/tests/send_uln302/effective_executor_config.rs +132 -0
  147. package/contracts/message-libs/uln-302/src/tests/send_uln302/effective_send_uln_config.rs +117 -0
  148. package/contracts/message-libs/uln-302/src/tests/send_uln302/mod.rs +6 -0
  149. package/contracts/message-libs/uln-302/src/tests/send_uln302/quote.rs +586 -0
  150. package/contracts/message-libs/uln-302/src/tests/send_uln302/send.rs +834 -0
  151. package/contracts/message-libs/uln-302/src/tests/send_uln302/set_default_executor_configs.rs +95 -0
  152. package/contracts/message-libs/uln-302/src/tests/send_uln302/set_default_send_uln_configs.rs +80 -0
  153. package/contracts/message-libs/uln-302/src/tests/setup.rs +268 -0
  154. package/contracts/message-libs/uln-302/src/tests/testing_utils.rs +47 -0
  155. package/contracts/message-libs/uln-302/src/tests/uln302/get_app_receive_uln_config.rs +51 -0
  156. package/contracts/message-libs/uln-302/src/tests/uln302/get_app_send_uln_config.rs +51 -0
  157. package/contracts/message-libs/uln-302/src/tests/uln302/get_oapp_executor_config.rs +48 -0
  158. package/contracts/message-libs/uln-302/src/tests/uln302/mod.rs +4 -0
  159. package/contracts/message-libs/uln-302/src/tests/uln302/set_config.rs +998 -0
  160. package/contracts/message-libs/uln-302/src/uln302.rs +117 -0
  161. package/contracts/oapp-macros/Cargo.toml +21 -0
  162. package/contracts/oapp-macros/src/lib.rs +408 -0
  163. package/contracts/oapp-macros/src/oapp_core.rs +49 -0
  164. package/contracts/oapp-macros/src/oapp_full.rs +15 -0
  165. package/contracts/oapp-macros/src/oapp_options_type3.rs +46 -0
  166. package/contracts/oapp-macros/src/oapp_receiver.rs +67 -0
  167. package/contracts/oapp-macros/src/oapp_sender.rs +23 -0
  168. package/contracts/oapp-macros/src/util.rs +103 -0
  169. package/contracts/oapp-macros/tests/test_macros.rs +522 -0
  170. package/contracts/oapps/Cargo.toml +12 -0
  171. package/contracts/oapps/counter/Cargo.toml +24 -0
  172. package/contracts/oapps/counter/integration_tests/mod.rs +3 -0
  173. package/contracts/oapps/counter/integration_tests/setup.rs +201 -0
  174. package/contracts/oapps/counter/integration_tests/test_with_sml.rs +166 -0
  175. package/contracts/oapps/counter/integration_tests/utils.rs +144 -0
  176. package/contracts/oapps/counter/src/codec.rs +63 -0
  177. package/contracts/oapps/counter/src/counter.rs +235 -0
  178. package/contracts/oapps/counter/src/errors.rs +9 -0
  179. package/contracts/oapps/counter/src/lib.rs +16 -0
  180. package/contracts/oapps/counter/src/options.rs +30 -0
  181. package/contracts/oapps/counter/src/storage.rs +33 -0
  182. package/contracts/oapps/counter/src/tests/mod.rs +37 -0
  183. package/contracts/oapps/counter/src/tests/test_codec.rs +64 -0
  184. package/contracts/oapps/counter/src/tests/test_counter.rs +390 -0
  185. package/contracts/oapps/counter/src/u256_ext.rs +21 -0
  186. package/contracts/oapps/lib.rs +2 -0
  187. package/contracts/oapps/oapp/Cargo.toml +21 -0
  188. package/contracts/oapps/oapp/src/errors.rs +9 -0
  189. package/contracts/oapps/oapp/src/lib.rs +10 -0
  190. package/contracts/oapps/oapp/src/oapp_core.rs +92 -0
  191. package/contracts/oapps/oapp/src/oapp_options_type3.rs +89 -0
  192. package/contracts/oapps/oapp/src/oapp_receiver.rs +72 -0
  193. package/contracts/oapps/oapp/src/oapp_sender.rs +66 -0
  194. package/contracts/oapps/oapp/src/tests/mod.rs +4 -0
  195. package/contracts/oapps/oapp/src/tests/test_oapp_core.rs +162 -0
  196. package/contracts/oapps/oapp/src/tests/test_oapp_options_type3.rs +180 -0
  197. package/contracts/oapps/oapp/src/tests/test_oapp_receiver.rs +157 -0
  198. package/contracts/oapps/oapp/src/tests/test_oapp_sender.rs +283 -0
  199. package/contracts/utils/Cargo.toml +21 -0
  200. package/contracts/utils/src/buffer_reader.rs +143 -0
  201. package/contracts/utils/src/buffer_writer.rs +117 -0
  202. package/contracts/utils/src/bytes_ext.rs +19 -0
  203. package/contracts/utils/src/errors.rs +30 -0
  204. package/contracts/utils/src/lib.rs +15 -0
  205. package/contracts/utils/src/option_ext.rs +38 -0
  206. package/contracts/utils/src/ownable.rs +88 -0
  207. package/contracts/utils/src/testing_utils.rs +100 -0
  208. package/contracts/utils/src/tests/buffer_reader.rs +1006 -0
  209. package/contracts/utils/src/tests/buffer_writer.rs +330 -0
  210. package/contracts/utils/src/tests/bytes_ext.rs +77 -0
  211. package/contracts/utils/src/tests/mod.rs +4 -0
  212. package/contracts/utils/src/tests/ownable.rs +149 -0
  213. package/contracts/utils/src/ttl.rs +164 -0
  214. package/contracts/workers/Cargo.toml +13 -0
  215. package/contracts/workers/executor/Cargo.toml +26 -0
  216. package/contracts/workers/executor/src/events.rs +22 -0
  217. package/contracts/workers/executor/src/executor.rs +347 -0
  218. package/contracts/workers/executor/src/interfaces/executor.rs +40 -0
  219. package/contracts/workers/executor/src/interfaces/mod.rs +5 -0
  220. package/contracts/workers/executor/src/interfaces/types.rs +51 -0
  221. package/contracts/workers/executor/src/lib.rs +10 -0
  222. package/contracts/workers/executor/src/storage.rs +23 -0
  223. package/contracts/workers/lib.rs +2 -0
  224. package/contracts/workers/worker-common/Cargo.toml +18 -0
  225. package/contracts/workers/worker-common/src/constants.rs +17 -0
  226. package/contracts/workers/worker-common/src/errors.rs +6 -0
  227. package/contracts/workers/worker-common/src/events.rs +34 -0
  228. package/contracts/workers/worker-common/src/interfaces/executor_fee_lib.rs +35 -0
  229. package/contracts/workers/worker-common/src/interfaces/mod.rs +7 -0
  230. package/contracts/workers/worker-common/src/interfaces/price_feed.rs +40 -0
  231. package/contracts/workers/worker-common/src/interfaces/worker.rs +60 -0
  232. package/contracts/workers/worker-common/src/lib.rs +19 -0
  233. package/contracts/workers/worker-common/src/storage.rs +32 -0
  234. package/contracts/workers/worker-common/src/worker_common.rs +166 -0
  235. package/package.json +25 -0
  236. package/rust-toolchain.toml +4 -0
  237. package/rustfmt.toml +17 -0
  238. package/sdk/.turbo/turbo-build.log +4 -0
  239. package/sdk/dist/generated/bml.d.ts +452 -0
  240. package/sdk/dist/generated/bml.js +72 -0
  241. package/sdk/dist/generated/counter.d.ts +824 -0
  242. package/sdk/dist/generated/counter.js +125 -0
  243. package/sdk/dist/generated/endpoint.d.ts +1676 -0
  244. package/sdk/dist/generated/endpoint.js +216 -0
  245. package/sdk/dist/generated/sml.d.ts +810 -0
  246. package/sdk/dist/generated/sml.js +132 -0
  247. package/sdk/dist/generated/uln302.d.ts +1227 -0
  248. package/sdk/dist/generated/uln302.js +185 -0
  249. package/sdk/dist/index.d.ts +5 -0
  250. package/sdk/dist/index.js +5 -0
  251. package/sdk/node_modules/.bin/tsc +21 -0
  252. package/sdk/node_modules/.bin/tsserver +21 -0
  253. package/sdk/node_modules/.bin/vitest +21 -0
  254. package/sdk/node_modules/.bin/zx +21 -0
  255. package/sdk/package.json +40 -0
  256. package/sdk/src/index.ts +5 -0
  257. package/sdk/test/index.test.ts +271 -0
  258. package/sdk/test/suites/constants.ts +13 -0
  259. package/sdk/test/suites/deploy.ts +277 -0
  260. package/sdk/test/suites/localnet.ts +42 -0
  261. package/sdk/test/suites/scan.ts +189 -0
  262. package/sdk/tsconfig.json +106 -0
  263. package/tools/ts-bindings-gen/Cargo.toml +14 -0
  264. package/tools/ts-bindings-gen/src/main.rs +147 -0
  265. package/turbo.json +12 -0
@@ -0,0 +1,17 @@
1
+ mod layerzero_composer;
2
+ mod layerzero_endpoint_v2;
3
+ mod layerzero_receiver;
4
+ mod message_lib;
5
+ mod message_lib_manager;
6
+ mod messaging_channel;
7
+ mod messaging_composer;
8
+ mod send_lib;
9
+
10
+ pub use layerzero_composer::*;
11
+ pub use layerzero_endpoint_v2::*;
12
+ pub use layerzero_receiver::*;
13
+ pub use message_lib::*;
14
+ pub use message_lib_manager::*;
15
+ pub use messaging_channel::*;
16
+ pub use messaging_composer::*;
17
+ pub use send_lib::*;
@@ -0,0 +1,70 @@
1
+ use super::{IMessageLib, MessagingFee};
2
+ use soroban_sdk::{contractclient, contracttype, Address, Bytes, BytesN, Env, Vec};
3
+
4
+ /// Outbound packet containing all information for cross-chain transmission.
5
+ #[contracttype]
6
+ #[derive(Clone, Debug, Eq, PartialEq)]
7
+ pub struct OutboundPacket {
8
+ /// Outbound nonce for this pathway.
9
+ pub nonce: u64,
10
+ /// Source endpoint ID.
11
+ pub src_eid: u32,
12
+ /// Sender address on source chain.
13
+ pub sender: Address,
14
+ /// Destination endpoint ID.
15
+ pub dst_eid: u32,
16
+ /// Receiver address on destination chain (32 bytes).
17
+ pub receiver: BytesN<32>,
18
+ /// Globally unique identifier for this message.
19
+ pub guid: BytesN<32>,
20
+ /// The message payload.
21
+ pub message: Bytes,
22
+ }
23
+
24
+ /// A fee recipient with the amount to be paid.
25
+ #[contracttype]
26
+ #[derive(Clone, Debug, Eq, PartialEq)]
27
+ pub struct FeeRecipient {
28
+ /// Address to receive the fee.
29
+ pub address: Address,
30
+ /// Amount of fee to pay.
31
+ pub amount: i128,
32
+ }
33
+
34
+ /// Result of send operation containing fees and encoded packet.
35
+ #[contracttype]
36
+ #[derive(Clone, Debug, Eq, PartialEq)]
37
+ pub struct FeesAndPacket {
38
+ /// List of native token fee recipients (executor, DVNs, treasury).
39
+ pub native_fee_recipients: Vec<FeeRecipient>,
40
+ /// List of ZRO token fee recipients (treasury).
41
+ pub zro_fee_recipients: Vec<FeeRecipient>,
42
+ /// The encoded packet ready for transmission.
43
+ pub encoded_packet: Bytes,
44
+ }
45
+
46
+ /// Interface for send libraries that handle outbound message encoding and fee calculation.
47
+ #[contractclient(name = "SendLibClient")]
48
+ pub trait ISendLib: IMessageLib {
49
+ /// Quotes the fee for sending a packet without actually sending.
50
+ ///
51
+ /// # Arguments
52
+ /// * `packet` - The outbound packet containing message metadata and content
53
+ /// * `options` - Execution options (e.g., gas limit, airdrop amount)
54
+ /// * `pay_in_zro` - Whether to pay fees in ZRO token
55
+ ///
56
+ /// # Returns
57
+ /// `MessagingFee` containing estimated native and ZRO fees
58
+ fn quote(env: &Env, packet: &OutboundPacket, options: &Bytes, pay_in_zro: bool) -> MessagingFee;
59
+
60
+ /// Sends a packet through the message library.
61
+ ///
62
+ /// # Arguments
63
+ /// * `packet` - The outbound packet containing message metadata and content
64
+ /// * `options` - Execution options (e.g., gas limit, airdrop amount)
65
+ /// * `pay_in_zro` - Whether to pay fees in ZRO token
66
+ ///
67
+ /// # Returns
68
+ /// `FeesAndPacket` containing fee recipients and the encoded packet
69
+ fn send(env: &Env, packet: &OutboundPacket, options: &Bytes, pay_in_zro: bool) -> FeesAndPacket;
70
+ }
@@ -0,0 +1,22 @@
1
+ #![no_std]
2
+
3
+ mod interfaces;
4
+
5
+ pub use interfaces::*;
6
+ pub mod util;
7
+
8
+ cfg_if::cfg_if! {
9
+ // Include implementation when NOT in library mode, OR when testutils is enabled (for tests)
10
+ if #[cfg(any(not(feature = "library"), feature = "testutils"))] {
11
+ mod endpoint_v2;
12
+ mod errors;
13
+ mod events;
14
+ mod storage;
15
+ mod constants;
16
+
17
+ pub use endpoint_v2::{EndpointV2, EndpointV2Client};
18
+ }
19
+ }
20
+
21
+ #[cfg(test)]
22
+ mod tests;
@@ -0,0 +1,315 @@
1
+ #[contractimpl]
2
+ impl IMessageLibManager for EndpointV2 {
3
+ /// Registers a new message library with the endpoint.
4
+ #[only_owner]
5
+ fn register_library(env: &Env, new_lib: &Address) {
6
+ // Call library get type to make sure it's a valid library, will panic if not
7
+ let _ = MessageLibClient::new(env, new_lib).message_lib_type();
8
+
9
+ // Check if the library is already registered
10
+ assert_with_error!(&env, !Self::is_registered_library(env, new_lib), EndpointError::AlreadyRegistered);
11
+
12
+ // Register the library
13
+ let index = Self::registered_libraries_count(env);
14
+ EndpointStorage::set_library_to_index(env, new_lib, &index);
15
+ EndpointStorage::set_index_to_library(env, index, new_lib);
16
+ EndpointStorage::set_registered_library_count(env, &(index + 1)); // increment the registered library count
17
+
18
+ LibraryRegistered { new_lib: new_lib.clone() }.publish(env);
19
+ }
20
+
21
+ /// Sets the default send library for a destination endpoint.
22
+ #[only_owner]
23
+ fn set_default_send_library(env: &Env, dst_eid: u32, new_lib: &Address) {
24
+ Self::require_send_lib_for_eid(env, new_lib, dst_eid);
25
+
26
+ let old_lib = Self::default_send_library(env, dst_eid);
27
+ assert_with_error!(env, old_lib.as_ref() != Some(new_lib), EndpointError::SameValue);
28
+
29
+ EndpointStorage::set_default_send_library(env, dst_eid, new_lib);
30
+ DefaultSendLibrarySet { dst_eid, new_lib: new_lib.clone() }.publish(env);
31
+ }
32
+
33
+ /// Sets the default receive library for a source endpoint.
34
+ ///
35
+ /// If a grace period is provided and there was a previous library, the old library
36
+ /// remains valid until the grace period expires.
37
+ #[only_owner]
38
+ fn set_default_receive_library(env: &Env, src_eid: u32, new_lib: &Address, grace_period: u64) {
39
+ Self::require_receive_lib_for_eid(env, new_lib, src_eid);
40
+
41
+ let old_lib = Self::default_receive_library(env, src_eid);
42
+ assert_with_error!(env, old_lib.as_ref() != Some(new_lib), EndpointError::SameValue);
43
+
44
+ EndpointStorage::set_default_receive_library(env, src_eid, new_lib);
45
+ DefaultReceiveLibrarySet { src_eid, new_lib: new_lib.clone() }.publish(env);
46
+
47
+ // Set timeout based on grace period and old library
48
+ let timeout = if grace_period > 0 {
49
+ old_lib.map(|lib| {
50
+ let expiry = env.ledger().timestamp() + grace_period;
51
+ Timeout { lib, expiry }
52
+ })
53
+ } else {
54
+ None
55
+ };
56
+ EndpointStorage::set_or_remove_default_receive_library_timeout(env, src_eid, &timeout);
57
+ DefaultReceiveLibraryTimeoutSet { src_eid, timeout: timeout.clone() }.publish(env);
58
+ }
59
+
60
+ /// Sets or removes the default receive library timeout for a source endpoint.
61
+ ///
62
+ /// If a timeout is provided, it must be valid and not expired. If no timeout is provided,
63
+ /// the default receive library timeout is removed.
64
+ #[only_owner]
65
+ fn set_default_receive_lib_timeout(env: &Env, src_eid: u32, timeout: &Option<Timeout>) {
66
+ if let Some(t) = timeout {
67
+ Self::require_receive_lib_for_eid(env, &t.lib, src_eid);
68
+ assert_with_error!(env, !t.is_expired(env), EndpointError::InvalidExpiry);
69
+ }
70
+ EndpointStorage::set_or_remove_default_receive_library_timeout(env, src_eid, timeout);
71
+ DefaultReceiveLibraryTimeoutSet { src_eid, timeout: timeout.clone() }.publish(env);
72
+ }
73
+
74
+ // === OApp Control Functions ===
75
+
76
+ /// Sets or removes a custom send library for an OApp to a specific destination endpoint.
77
+ fn set_send_library(env: &Env, caller: &Address, sender: &Address, dst_eid: u32, new_lib: &Option<Address>) {
78
+ Self::require_oapp_auth(env, caller, sender);
79
+
80
+ let old_lib = EndpointStorage::send_library(env, sender, dst_eid);
81
+ assert_with_error!(env, &old_lib != new_lib, EndpointError::SameValue);
82
+
83
+ if let Some(lib) = new_lib {
84
+ Self::require_send_lib_for_eid(env, lib, dst_eid);
85
+ }
86
+ EndpointStorage::set_or_remove_send_library(env, sender, dst_eid, new_lib);
87
+ SendLibrarySet { sender: sender.clone(), dst_eid, new_lib: new_lib.clone() }.publish(env);
88
+ }
89
+
90
+ /// Sets or removes a custom receive library for an OApp to a specific source endpoint.
91
+ ///
92
+ /// If a grace period is provided and there was a previous library, the old library
93
+ /// remains valid until the grace period expires.
94
+ fn set_receive_library(
95
+ env: &Env,
96
+ caller: &Address,
97
+ receiver: &Address,
98
+ src_eid: u32,
99
+ new_lib: &Option<Address>,
100
+ grace_period: u64,
101
+ ) {
102
+ Self::require_oapp_auth(env, caller, receiver);
103
+
104
+ let old_lib = EndpointStorage::receive_library(env, receiver, src_eid);
105
+ assert_with_error!(env, &old_lib != new_lib, EndpointError::SameValue);
106
+
107
+ if let Some(lib) = new_lib {
108
+ Self::require_receive_lib_for_eid(env, lib, src_eid);
109
+ }
110
+ EndpointStorage::set_or_remove_receive_library(env, receiver, src_eid, new_lib);
111
+ ReceiveLibrarySet { receiver: receiver.clone(), src_eid, new_lib: new_lib.clone() }.publish(env);
112
+
113
+ // Set timeout based on grace period and library availability
114
+ let timeout = if grace_period > 0 {
115
+ // To simplify timeout logic, we only allow setting timeout when both old and new libraries are custom (non-default).
116
+ // This avoids complex interactions with default library timeout configurations.
117
+ //
118
+ // For other scenarios:
119
+ // (1) To fall back to default library: set new_lib to None with grace_period = 0
120
+ // (2) To change from default to custom library: set new_lib to custom with grace_period = 0,
121
+ // then use set_receive_library_timeout() to configure timeout separately if needed
122
+ assert_with_error!(env, old_lib.is_some() && new_lib.is_some(), EndpointError::OnlyNonDefaultLib);
123
+ Some(Timeout { lib: old_lib.unwrap(), expiry: env.ledger().timestamp() + grace_period })
124
+ } else {
125
+ None
126
+ };
127
+
128
+ EndpointStorage::set_or_remove_receive_library_timeout(env, receiver, src_eid, &timeout);
129
+ ReceiveLibraryTimeoutSet { receiver: receiver.clone(), eid: src_eid, timeout }.publish(env);
130
+ }
131
+
132
+ /// Sets or removes the custom receive library timeout for an OApp.
133
+ ///
134
+ /// Allows an OApp to extend or remove the validity period of a previously set library
135
+ /// after switching to a new one.
136
+ fn set_receive_library_timeout(
137
+ env: &Env,
138
+ caller: &Address,
139
+ receiver: &Address,
140
+ src_eid: u32,
141
+ timeout: &Option<Timeout>,
142
+ ) {
143
+ Self::require_oapp_auth(env, caller, receiver);
144
+
145
+ // Only if receive_library is set, we can set the timeout
146
+ let ResolvedLibrary { lib: _, is_default } = Self::get_receive_library(env, receiver, src_eid);
147
+ assert_with_error!(env, !is_default, EndpointError::OnlyNonDefaultLib);
148
+
149
+ if let Some(t) = timeout {
150
+ Self::require_receive_lib_for_eid(env, &t.lib, src_eid);
151
+ assert_with_error!(env, !t.is_expired(env), EndpointError::InvalidExpiry);
152
+ }
153
+ EndpointStorage::set_or_remove_receive_library_timeout(env, receiver, src_eid, timeout);
154
+ ReceiveLibraryTimeoutSet { receiver: receiver.clone(), eid: src_eid, timeout: timeout.clone() }.publish(env);
155
+ }
156
+
157
+ /// Sets the configuration for a message library.
158
+ ///
159
+ /// Requires the caller to be the OApp or its delegate and the library to be registered.
160
+ fn set_config(env: &Env, caller: &Address, oapp: &Address, lib: &Address, params: &Vec<SetConfigParam>) {
161
+ Self::require_oapp_auth(env, caller, oapp);
162
+ Self::require_registered(env, lib);
163
+
164
+ MessageLibClient::new(env, lib).set_config(oapp, params);
165
+ }
166
+
167
+ // === View Functions ===
168
+
169
+ /// Checks if a message library is registered.
170
+ fn is_registered_library(env: &Env, lib: &Address) -> bool {
171
+ EndpointStorage::has_library_to_index(env, lib)
172
+ }
173
+
174
+ /// Returns the index of a message library.
175
+ fn get_library_index(env: &Env, lib: &Address) -> Option<u32> {
176
+ EndpointStorage::library_to_index(env, lib)
177
+ }
178
+
179
+ /// Returns the number of registered message libraries.
180
+ fn registered_libraries_count(env: &Env) -> u32 {
181
+ EndpointStorage::registered_library_count(env)
182
+ }
183
+
184
+ /// Returns a list of registered message libraries within the specified range.
185
+ fn get_registered_libraries(env: &Env, start: u32, max_count: u32) -> Vec<Address> {
186
+ let count = EndpointStorage::registered_library_count(env);
187
+ if count == 0 || start >= count {
188
+ return vec![env];
189
+ }
190
+
191
+ let end = count.min(start + max_count);
192
+ let mut libraries = vec![env];
193
+ libraries.extend((start..end).map(|i| EndpointStorage::index_to_library(env, i).unwrap()));
194
+ libraries
195
+ }
196
+
197
+ /// Checks if an endpoint ID is supported.
198
+ /// Returns true only if both the default send/receive libraries are set for the given eid
199
+ fn is_supported_eid(env: &Env, eid: u32) -> bool {
200
+ EndpointStorage::has_default_send_library(env, eid) && EndpointStorage::has_default_receive_library(env, eid)
201
+ }
202
+
203
+ /// Returns the default send library for a destination endpoint.
204
+ fn default_send_library(env: &Env, dst_eid: u32) -> Option<Address> {
205
+ EndpointStorage::default_send_library(env, dst_eid)
206
+ }
207
+
208
+ /// Returns the default receive library for a source endpoint.
209
+ fn default_receive_library(env: &Env, src_eid: u32) -> Option<Address> {
210
+ EndpointStorage::default_receive_library(env, src_eid)
211
+ }
212
+
213
+ /// Returns the default receive library timeout for a source endpoint.
214
+ fn default_receive_library_timeout(env: &Env, src_eid: u32) -> Option<Timeout> {
215
+ EndpointStorage::default_receive_library_timeout(env, src_eid)
216
+ }
217
+
218
+ // === OApp View Functions ===
219
+
220
+ /// Returns the effective send library for an OApp and destination endpoint.
221
+ fn get_send_library(env: &Env, sender: &Address, dst_eid: u32) -> ResolvedLibrary {
222
+ EndpointStorage::send_library(env, sender, dst_eid)
223
+ .map(|lib| ResolvedLibrary { lib, is_default: false })
224
+ .unwrap_or_else(|| {
225
+ let default_lib = EndpointStorage::default_send_library(env, dst_eid)
226
+ .unwrap_or_panic(env, EndpointError::DefaultSendLibUnavailable);
227
+ ResolvedLibrary { lib: default_lib, is_default: true }
228
+ })
229
+ }
230
+
231
+ /// Returns the effective receive library for an OApp and source endpoint.
232
+ fn get_receive_library(env: &Env, receiver: &Address, src_eid: u32) -> ResolvedLibrary {
233
+ EndpointStorage::receive_library(env, receiver, src_eid)
234
+ .map(|lib| ResolvedLibrary { lib, is_default: false })
235
+ .unwrap_or_else(|| {
236
+ let default_lib = EndpointStorage::default_receive_library(env, src_eid)
237
+ .unwrap_or_panic(env, EndpointError::DefaultReceiveLibUnavailable);
238
+ ResolvedLibrary { lib: default_lib, is_default: true }
239
+ })
240
+ }
241
+
242
+ /// Returns the receive library timeout for an OApp and source endpoint.
243
+ fn receive_library_timeout(env: &Env, receiver: &Address, src_eid: u32) -> Option<Timeout> {
244
+ EndpointStorage::receive_library_timeout(env, receiver, src_eid)
245
+ }
246
+
247
+ /// Checks if a receive library is valid for an OApp and source endpoint.
248
+ fn is_valid_receive_library(env: &Env, receiver: &Address, src_eid: u32, actual_lib: &Address) -> bool {
249
+ // early return true if the lib is the currently configured one
250
+ let ResolvedLibrary { lib: expected_lib, is_default } = Self::get_receive_library(env, receiver, src_eid);
251
+ if actual_lib == &expected_lib {
252
+ return true;
253
+ }
254
+
255
+ // Check if the actual_lib matches a timeout library that hasn't expired
256
+ let timeout = if is_default {
257
+ Self::default_receive_library_timeout(env, src_eid)
258
+ } else {
259
+ Self::receive_library_timeout(env, receiver, src_eid)
260
+ };
261
+
262
+ timeout.is_some_and(|t| t.is_valid_for(env, actual_lib))
263
+ }
264
+
265
+ /// Returns the configuration for a message library for a specific endpoint ID and configuration type.
266
+ fn get_config(env: &Env, oapp: &Address, lib: &Address, eid: u32, config_type: u32) -> Bytes {
267
+ Self::require_registered(env, lib);
268
+ MessageLibClient::new(env, lib).get_config(&eid, oapp, &config_type)
269
+ }
270
+ }
271
+
272
+ // ============================================================================
273
+ // Internal Functions
274
+ // ============================================================================
275
+
276
+ impl EndpointV2 {
277
+ /// Requires a message library to be registered.
278
+ pub(crate) fn require_registered(env: &Env, lib: &Address) {
279
+ assert_with_error!(env, Self::is_registered_library(env, lib), EndpointError::OnlyRegisteredLib);
280
+ }
281
+
282
+ /// Requires an endpoint ID to be supported.
283
+ pub(crate) fn require_supported_eid(env: &Env, lib: &Address, eid: u32) {
284
+ let is_supported = MessageLibClient::new(env, lib).is_supported_eid(&eid);
285
+ assert_with_error!(env, is_supported, EndpointError::UnsupportedEid);
286
+ }
287
+
288
+ /// Requires a library to be a registered send library and supported for the given endpoint ID.
289
+ pub(crate) fn require_send_lib_for_eid(env: &Env, lib: &Address, eid: u32) {
290
+ Self::require_registered(env, lib);
291
+
292
+ let message_lib_type = MessageLibClient::new(env, lib).message_lib_type();
293
+ assert_with_error!(
294
+ env,
295
+ message_lib_type == MessageLibType::Send || message_lib_type == MessageLibType::SendAndReceive,
296
+ EndpointError::OnlySendLib
297
+ );
298
+
299
+ Self::require_supported_eid(env, lib, eid);
300
+ }
301
+
302
+ /// Requires a library to be a registered receive library and supported for the given endpoint ID.
303
+ pub(crate) fn require_receive_lib_for_eid(env: &Env, lib: &Address, eid: u32) {
304
+ Self::require_registered(env, lib);
305
+
306
+ let message_lib_type = MessageLibClient::new(env, lib).message_lib_type();
307
+ assert_with_error!(
308
+ env,
309
+ message_lib_type == MessageLibType::Receive || message_lib_type == MessageLibType::SendAndReceive,
310
+ EndpointError::OnlyReceiveLib
311
+ );
312
+
313
+ Self::require_supported_eid(env, lib, eid);
314
+ }
315
+ }
@@ -0,0 +1,218 @@
1
+ #[contractimpl]
2
+ impl IMessagingChannel for EndpointV2 {
3
+ /// Skips the next expected inbound nonce without verifying.
4
+ ///
5
+ /// The nonce to skip must be the next expected nonce.
6
+ fn skip(env: &Env, caller: &Address, receiver: &Address, src_eid: u32, sender: &BytesN<32>, nonce: u64) {
7
+ Self::require_oapp_auth(env, caller, receiver);
8
+
9
+ let next_nonce = Self::inbound_nonce(env, receiver, src_eid, sender) + 1;
10
+ assert_with_error!(env, nonce == next_nonce, EndpointError::InvalidNonce);
11
+ EndpointStorage::set_lazy_inbound_nonce(env, receiver, src_eid, sender, &nonce);
12
+
13
+ InboundNonceSkipped { src_eid, sender: sender.clone(), receiver: receiver.clone(), nonce }.publish(env);
14
+ }
15
+
16
+ /// Marks a packet as verified, but disallows execution until it is re-verified.
17
+ ///
18
+ /// Requires the payload hash not been executed.
19
+ fn nilify(
20
+ env: &Env,
21
+ caller: &Address,
22
+ receiver: &Address,
23
+ src_eid: u32,
24
+ sender: &BytesN<32>,
25
+ nonce: u64,
26
+ payload_hash: &Option<BytesN<32>>,
27
+ ) {
28
+ Self::require_oapp_auth(env, caller, receiver);
29
+
30
+ let cur_payload_hash = Self::inbound_payload_hash(env, receiver, src_eid, sender, nonce);
31
+ let lazy_nonce = Self::lazy_inbound_nonce(env, receiver, src_eid, sender);
32
+
33
+ assert_with_error!(env, payload_hash == &cur_payload_hash, EndpointError::PayloadHashNotFound);
34
+ assert_with_error!(env, nonce > lazy_nonce || cur_payload_hash.is_some(), EndpointError::InvalidNonce);
35
+ EndpointStorage::set_inbound_payload_hash(env, receiver, src_eid, sender, nonce, &Self::nil_payload_hash(env));
36
+
37
+ PacketNilified {
38
+ src_eid,
39
+ sender: sender.clone(),
40
+ receiver: receiver.clone(),
41
+ nonce,
42
+ payload_hash: payload_hash.clone(),
43
+ }
44
+ .publish(env);
45
+ }
46
+
47
+ /// Marks a nonce as unexecutable and un-verifiable. The nonce can never be re-verified or executed.
48
+ fn burn(
49
+ env: &Env,
50
+ caller: &Address,
51
+ receiver: &Address,
52
+ src_eid: u32,
53
+ sender: &BytesN<32>,
54
+ nonce: u64,
55
+ payload_hash: &BytesN<32>,
56
+ ) {
57
+ Self::require_oapp_auth(env, caller, receiver);
58
+
59
+ // Check if current payload hash matches the provided payload hash
60
+ let cur_payload_hash = Self::inbound_payload_hash(env, receiver, src_eid, sender, nonce);
61
+ assert_with_error!(env, cur_payload_hash.as_ref() == Some(payload_hash), EndpointError::PayloadHashNotFound);
62
+
63
+ // Check if nonce is greater than lazy nonce
64
+ let lazy_nonce = Self::lazy_inbound_nonce(env, receiver, src_eid, sender);
65
+ assert_with_error!(env, nonce <= lazy_nonce, EndpointError::InvalidNonce);
66
+
67
+ // Remove the payload hash from storage
68
+ EndpointStorage::remove_inbound_payload_hash(env, receiver, src_eid, sender, nonce);
69
+
70
+ PacketBurnt {
71
+ src_eid,
72
+ sender: sender.clone(),
73
+ receiver: receiver.clone(),
74
+ nonce,
75
+ payload_hash: payload_hash.clone(),
76
+ }
77
+ .publish(env);
78
+ }
79
+
80
+ // === View Functions ===
81
+
82
+ /// Generates the next GUID for an outbound packet.
83
+ fn next_guid(env: &Env, sender: &Address, dst_eid: u32, receiver: &BytesN<32>) -> BytesN<32> {
84
+ let next_nonce = Self::outbound_nonce(env, sender, dst_eid, receiver) + 1;
85
+ compute_guid(env, next_nonce, Self::eid(env), sender, dst_eid, receiver)
86
+ }
87
+
88
+ /// Returns the current outbound nonce for a specific destination.
89
+ fn outbound_nonce(env: &Env, sender: &Address, dst_eid: u32, receiver: &BytesN<32>) -> u64 {
90
+ EndpointStorage::outbound_nonce(env, sender, dst_eid, receiver)
91
+ }
92
+
93
+ /// Returns the max index of the longest gapless sequence of verified message nonces.
94
+ ///
95
+ /// The uninitialized value is 0. The first nonce is always 1.
96
+ /// It starts from the `lazy_inbound_nonce` (last checkpoint) and iteratively checks
97
+ /// if the next nonce has been verified.
98
+ ///
99
+ /// Note: OApp explicitly skipped nonces count as "verified" for these purposes.
100
+ ///
101
+ /// Examples: `[1,2,3,4,6,7] => 4`, `[1,2,6,8,10] => 2`, `[1,3,4,5,6] => 1`
102
+ fn inbound_nonce(env: &Env, receiver: &Address, src_eid: u32, sender: &BytesN<32>) -> u64 {
103
+ let mut nonce_cursor = Self::lazy_inbound_nonce(env, receiver, src_eid, sender);
104
+
105
+ // Find the effective inbound current nonce
106
+ while EndpointStorage::has_inbound_payload_hash(env, receiver, src_eid, sender, nonce_cursor + 1) {
107
+ nonce_cursor += 1;
108
+ }
109
+ nonce_cursor
110
+ }
111
+
112
+ /// Returns the lazy inbound nonce (last checkpoint) for a specific path.
113
+ fn lazy_inbound_nonce(env: &Env, receiver: &Address, src_eid: u32, sender: &BytesN<32>) -> u64 {
114
+ EndpointStorage::lazy_inbound_nonce(env, receiver, src_eid, sender)
115
+ }
116
+
117
+ /// Returns the payload hash for a specific inbound nonce.
118
+ fn inbound_payload_hash(
119
+ env: &Env,
120
+ receiver: &Address,
121
+ src_eid: u32,
122
+ sender: &BytesN<32>,
123
+ nonce: u64,
124
+ ) -> Option<BytesN<32>> {
125
+ EndpointStorage::inbound_payload_hash(env, receiver, src_eid, sender, nonce)
126
+ }
127
+ }
128
+
129
+ // ============================================================================
130
+ // Internal Functions
131
+ // ============================================================================
132
+
133
+ impl EndpointV2 {
134
+ /// Increments the outbound nonce and stores it for a specific path.
135
+ ///
136
+ /// # Returns
137
+ /// * `nonce` - The next nonce for the path
138
+ pub(crate) fn outbound(env: &Env, sender: &Address, dst_eid: u32, receiver: &BytesN<32>) -> u64 {
139
+ let nonce = Self::outbound_nonce(env, sender, dst_eid, receiver) + 1;
140
+ EndpointStorage::set_outbound_nonce(env, sender, dst_eid, receiver, &nonce);
141
+ nonce
142
+ }
143
+
144
+ /// Records an inbound message payload hash for a specific nonce on a specific path.
145
+ ///
146
+ /// Inbound won't update the nonce eagerly to allow unordered verification.
147
+ /// Instead, it will update the nonce lazily when the message is received.
148
+ /// Messages can only be cleared in order to preserve censorship-resistance.
149
+ ///
150
+ /// # Arguments
151
+ /// * `receiver` - The receiver OApp address
152
+ /// * `src_eid` - The source endpoint ID
153
+ /// * `sender` - The sender OApp address on the source chain
154
+ /// * `nonce` - The nonce of the message
155
+ /// * `payload_hash` - The payload hash of the message (must not be empty payload hash)
156
+ pub(crate) fn inbound(
157
+ env: &Env,
158
+ receiver: &Address,
159
+ src_eid: u32,
160
+ sender: &BytesN<32>,
161
+ nonce: u64,
162
+ payload_hash: &BytesN<32>,
163
+ ) {
164
+ assert_with_error!(env, payload_hash != &Self::empty_payload_hash(env), EndpointError::InvalidPayloadHash);
165
+ EndpointStorage::set_inbound_payload_hash(env, receiver, src_eid, sender, nonce, payload_hash);
166
+ }
167
+
168
+ /// Clears a stored message payload and increments the lazy inbound nonce.
169
+ ///
170
+ /// Calling this function will clear the stored message and increment the
171
+ /// `lazy_inbound_nonce` to the provided nonce.
172
+ ///
173
+ /// Note: This function does not change `inbound_nonce`, it only changes
174
+ /// the `lazy_inbound_nonce` up to the provided nonce.
175
+ ///
176
+ /// # Arguments
177
+ /// * `receiver` - The receiver OApp address
178
+ /// * `src_eid` - The source endpoint ID
179
+ /// * `sender` - The sender OApp address on the source chain
180
+ /// * `nonce` - The nonce of the message
181
+ /// * `payload` - The payload of the message
182
+ pub(crate) fn clear_payload(
183
+ env: &Env,
184
+ receiver: &Address,
185
+ src_eid: u32,
186
+ sender: &BytesN<32>,
187
+ nonce: u64,
188
+ payload: &Bytes,
189
+ ) {
190
+ let current_nonce = Self::lazy_inbound_nonce(env, receiver, src_eid, sender);
191
+
192
+ // Try to update the lazy inbound nonce until the target nonce
193
+ if nonce > current_nonce {
194
+ let has_payload = (current_nonce + 1..=nonce)
195
+ .all(|n| EndpointStorage::has_inbound_payload_hash(env, receiver, src_eid, sender, n));
196
+ assert_with_error!(env, has_payload, EndpointError::InvalidNonce);
197
+ EndpointStorage::set_lazy_inbound_nonce(env, receiver, src_eid, sender, &nonce);
198
+ }
199
+
200
+ // Check the hash of the payload to verify the executor has given the proper payload that has been verified
201
+ let actual_hash = keccak256(env, payload);
202
+ let expected_hash = Self::inbound_payload_hash(env, receiver, src_eid, sender, nonce);
203
+ assert_with_error!(env, Some(actual_hash) == expected_hash, EndpointError::PayloadHashNotFound);
204
+
205
+ // Remove it from storage
206
+ EndpointStorage::remove_inbound_payload_hash(env, receiver, src_eid, sender, nonce);
207
+ }
208
+
209
+ /// Represents an empty payload hash
210
+ pub(crate) fn empty_payload_hash(env: &Env) -> BytesN<32> {
211
+ BytesN::from_array(env, &EMPTY_PAYLOAD_HASH_BYTES)
212
+ }
213
+
214
+ /// Represents a nilified payload hash
215
+ pub(crate) fn nil_payload_hash(env: &Env) -> BytesN<32> {
216
+ BytesN::from_array(env, &NIL_PAYLOAD_HASH_BYTES)
217
+ }
218
+ }