@proto-kit/protocol 0.1.1-develop.0

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 (333) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +45 -0
  3. package/dist/Constants.d.ts +4 -0
  4. package/dist/Constants.d.ts.map +1 -0
  5. package/dist/Constants.js +4 -0
  6. package/dist/Constants.js.map +1 -0
  7. package/dist/hooks/AccountStateHook.d.ts +52 -0
  8. package/dist/hooks/AccountStateHook.d.ts.map +1 -0
  9. package/dist/hooks/AccountStateHook.js +48 -0
  10. package/dist/hooks/AccountStateHook.js.map +1 -0
  11. package/dist/hooks/BlockHeightHook.d.ts +7 -0
  12. package/dist/hooks/BlockHeightHook.d.ts.map +1 -0
  13. package/dist/hooks/BlockHeightHook.js +16 -0
  14. package/dist/hooks/BlockHeightHook.js.map +1 -0
  15. package/dist/hooks/LastStateRootBlockHook.d.ts +8 -0
  16. package/dist/hooks/LastStateRootBlockHook.d.ts.map +1 -0
  17. package/dist/hooks/LastStateRootBlockHook.js +16 -0
  18. package/dist/hooks/LastStateRootBlockHook.js.map +1 -0
  19. package/dist/hooks/NoopBlockHook.d.ts +9 -0
  20. package/dist/hooks/NoopBlockHook.d.ts.map +1 -0
  21. package/dist/hooks/NoopBlockHook.js +10 -0
  22. package/dist/hooks/NoopBlockHook.js.map +1 -0
  23. package/dist/hooks/NoopSettlementHook.d.ts +6 -0
  24. package/dist/hooks/NoopSettlementHook.d.ts.map +1 -0
  25. package/dist/hooks/NoopSettlementHook.js +18 -0
  26. package/dist/hooks/NoopSettlementHook.js.map +1 -0
  27. package/dist/hooks/NoopTransactionHook.d.ts +6 -0
  28. package/dist/hooks/NoopTransactionHook.d.ts.map +1 -0
  29. package/dist/hooks/NoopTransactionHook.js +8 -0
  30. package/dist/hooks/NoopTransactionHook.js.map +1 -0
  31. package/dist/index.d.ts +58 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +58 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/model/MethodPublicOutput.d.ts +113 -0
  36. package/dist/model/MethodPublicOutput.d.ts.map +1 -0
  37. package/dist/model/MethodPublicOutput.js +15 -0
  38. package/dist/model/MethodPublicOutput.js.map +1 -0
  39. package/dist/model/Option.d.ts +122 -0
  40. package/dist/model/Option.d.ts.map +1 -0
  41. package/dist/model/Option.js +111 -0
  42. package/dist/model/Option.js.map +1 -0
  43. package/dist/model/Path.d.ts +31 -0
  44. package/dist/model/Path.d.ts.map +1 -0
  45. package/dist/model/Path.js +43 -0
  46. package/dist/model/Path.js.map +1 -0
  47. package/dist/model/RuntimeLike.d.ts +11 -0
  48. package/dist/model/RuntimeLike.d.ts.map +1 -0
  49. package/dist/model/RuntimeLike.js +2 -0
  50. package/dist/model/RuntimeLike.js.map +1 -0
  51. package/dist/model/StateTransition.d.ts +135 -0
  52. package/dist/model/StateTransition.d.ts.map +1 -0
  53. package/dist/model/StateTransition.js +69 -0
  54. package/dist/model/StateTransition.js.map +1 -0
  55. package/dist/model/StateTransitionProvableBatch.d.ts +198 -0
  56. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -0
  57. package/dist/model/StateTransitionProvableBatch.js +91 -0
  58. package/dist/model/StateTransitionProvableBatch.js.map +1 -0
  59. package/dist/model/network/NetworkState.d.ts +160 -0
  60. package/dist/model/network/NetworkState.d.ts.map +1 -0
  61. package/dist/model/network/NetworkState.js +32 -0
  62. package/dist/model/network/NetworkState.js.map +1 -0
  63. package/dist/model/transaction/RuntimeTransaction.d.ts +122 -0
  64. package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -0
  65. package/dist/model/transaction/RuntimeTransaction.js +87 -0
  66. package/dist/model/transaction/RuntimeTransaction.js.map +1 -0
  67. package/dist/model/transaction/SignedTransaction.d.ts +107 -0
  68. package/dist/model/transaction/SignedTransaction.d.ts.map +1 -0
  69. package/dist/model/transaction/SignedTransaction.js +34 -0
  70. package/dist/model/transaction/SignedTransaction.js.map +1 -0
  71. package/dist/model/transaction/ValueOption.d.ts +173 -0
  72. package/dist/model/transaction/ValueOption.d.ts.map +1 -0
  73. package/dist/model/transaction/ValueOption.js +25 -0
  74. package/dist/model/transaction/ValueOption.js.map +1 -0
  75. package/dist/protocol/Protocol.d.ts +43 -0
  76. package/dist/protocol/Protocol.d.ts.map +1 -0
  77. package/dist/protocol/Protocol.js +120 -0
  78. package/dist/protocol/Protocol.js.map +1 -0
  79. package/dist/protocol/ProtocolEnvironment.d.ts +9 -0
  80. package/dist/protocol/ProtocolEnvironment.d.ts.map +1 -0
  81. package/dist/protocol/ProtocolEnvironment.js +2 -0
  82. package/dist/protocol/ProtocolEnvironment.js.map +1 -0
  83. package/dist/protocol/ProtocolModule.d.ts +9 -0
  84. package/dist/protocol/ProtocolModule.d.ts.map +1 -0
  85. package/dist/protocol/ProtocolModule.js +13 -0
  86. package/dist/protocol/ProtocolModule.js.map +1 -0
  87. package/dist/protocol/ProvableBlockHook.d.ts +8 -0
  88. package/dist/protocol/ProvableBlockHook.d.ts.map +1 -0
  89. package/dist/protocol/ProvableBlockHook.js +5 -0
  90. package/dist/protocol/ProvableBlockHook.js.map +1 -0
  91. package/dist/protocol/ProvableTransactionHook.d.ts +7 -0
  92. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -0
  93. package/dist/protocol/ProvableTransactionHook.js +4 -0
  94. package/dist/protocol/ProvableTransactionHook.js.map +1 -0
  95. package/dist/protocol/TransitioningProtocolModule.d.ts +10 -0
  96. package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -0
  97. package/dist/protocol/TransitioningProtocolModule.js +9 -0
  98. package/dist/protocol/TransitioningProtocolModule.js.map +1 -0
  99. package/dist/prover/block/BlockProvable.d.ts +409 -0
  100. package/dist/prover/block/BlockProvable.d.ts.map +1 -0
  101. package/dist/prover/block/BlockProvable.js +48 -0
  102. package/dist/prover/block/BlockProvable.js.map +1 -0
  103. package/dist/prover/block/BlockProver.d.ts +94 -0
  104. package/dist/prover/block/BlockProver.d.ts.map +1 -0
  105. package/dist/prover/block/BlockProver.js +510 -0
  106. package/dist/prover/block/BlockProver.js.map +1 -0
  107. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts +64 -0
  108. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts.map +1 -0
  109. package/dist/prover/block/accummulators/BlockHashMerkleTree.js +17 -0
  110. package/dist/prover/block/accummulators/BlockHashMerkleTree.js.map +1 -0
  111. package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.d.ts +139 -0
  112. package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.d.ts.map +1 -0
  113. package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.js +21 -0
  114. package/dist/prover/block/accummulators/RuntimeVerificationKeyTree.js.map +1 -0
  115. package/dist/prover/block/services/RuntimeVerificationKeyRootService.d.ts +7 -0
  116. package/dist/prover/block/services/RuntimeVerificationKeyRootService.d.ts.map +1 -0
  117. package/dist/prover/block/services/RuntimeVerificationKeyRootService.js +23 -0
  118. package/dist/prover/block/services/RuntimeVerificationKeyRootService.js.map +1 -0
  119. package/dist/prover/statetransition/StateTransitionProvable.d.ts +172 -0
  120. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -0
  121. package/dist/prover/statetransition/StateTransitionProvable.js +16 -0
  122. package/dist/prover/statetransition/StateTransitionProvable.js.map +1 -0
  123. package/dist/prover/statetransition/StateTransitionProver.d.ts +48 -0
  124. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -0
  125. package/dist/prover/statetransition/StateTransitionProver.js +187 -0
  126. package/dist/prover/statetransition/StateTransitionProver.js.map +1 -0
  127. package/dist/settlement/ContractModule.d.ts +16 -0
  128. package/dist/settlement/ContractModule.d.ts.map +1 -0
  129. package/dist/settlement/ContractModule.js +12 -0
  130. package/dist/settlement/ContractModule.js.map +1 -0
  131. package/dist/settlement/SettlementContractModule.d.ts +53 -0
  132. package/dist/settlement/SettlementContractModule.d.ts.map +1 -0
  133. package/dist/settlement/SettlementContractModule.js +80 -0
  134. package/dist/settlement/SettlementContractModule.js.map +1 -0
  135. package/dist/settlement/contracts/BridgeContract.d.ts +97 -0
  136. package/dist/settlement/contracts/BridgeContract.d.ts.map +1 -0
  137. package/dist/settlement/contracts/BridgeContract.js +164 -0
  138. package/dist/settlement/contracts/BridgeContract.js.map +1 -0
  139. package/dist/settlement/contracts/BridgeContractProtocolModule.d.ts +14 -0
  140. package/dist/settlement/contracts/BridgeContractProtocolModule.d.ts.map +1 -0
  141. package/dist/settlement/contracts/BridgeContractProtocolModule.js +30 -0
  142. package/dist/settlement/contracts/BridgeContractProtocolModule.js.map +1 -0
  143. package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts +21 -0
  144. package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts.map +1 -0
  145. package/dist/settlement/contracts/DispatchContractProtocolModule.js +57 -0
  146. package/dist/settlement/contracts/DispatchContractProtocolModule.js.map +1 -0
  147. package/dist/settlement/contracts/DispatchSmartContract.d.ts +44 -0
  148. package/dist/settlement/contracts/DispatchSmartContract.d.ts.map +1 -0
  149. package/dist/settlement/contracts/DispatchSmartContract.js +230 -0
  150. package/dist/settlement/contracts/DispatchSmartContract.js.map +1 -0
  151. package/dist/settlement/contracts/SettlementContractProtocolModule.d.ts +21 -0
  152. package/dist/settlement/contracts/SettlementContractProtocolModule.d.ts.map +1 -0
  153. package/dist/settlement/contracts/SettlementContractProtocolModule.js +82 -0
  154. package/dist/settlement/contracts/SettlementContractProtocolModule.js.map +1 -0
  155. package/dist/settlement/contracts/SettlementSmartContract.d.ts +123 -0
  156. package/dist/settlement/contracts/SettlementSmartContract.d.ts.map +1 -0
  157. package/dist/settlement/contracts/SettlementSmartContract.js +309 -0
  158. package/dist/settlement/contracts/SettlementSmartContract.js.map +1 -0
  159. package/dist/settlement/contracts/TokenBridgeTree.d.ts +228 -0
  160. package/dist/settlement/contracts/TokenBridgeTree.d.ts.map +1 -0
  161. package/dist/settlement/contracts/TokenBridgeTree.js +60 -0
  162. package/dist/settlement/contracts/TokenBridgeTree.js.map +1 -0
  163. package/dist/settlement/contracts/authorizations/ContractAuthorization.d.ts +10 -0
  164. package/dist/settlement/contracts/authorizations/ContractAuthorization.d.ts.map +1 -0
  165. package/dist/settlement/contracts/authorizations/ContractAuthorization.js +2 -0
  166. package/dist/settlement/contracts/authorizations/ContractAuthorization.js.map +1 -0
  167. package/dist/settlement/contracts/authorizations/TokenBridgeDeploymentAuth.d.ts +85 -0
  168. package/dist/settlement/contracts/authorizations/TokenBridgeDeploymentAuth.d.ts.map +1 -0
  169. package/dist/settlement/contracts/authorizations/TokenBridgeDeploymentAuth.js +11 -0
  170. package/dist/settlement/contracts/authorizations/TokenBridgeDeploymentAuth.js.map +1 -0
  171. package/dist/settlement/contracts/authorizations/UpdateMessagesHashAuth.d.ts +79 -0
  172. package/dist/settlement/contracts/authorizations/UpdateMessagesHashAuth.d.ts.map +1 -0
  173. package/dist/settlement/contracts/authorizations/UpdateMessagesHashAuth.js +11 -0
  174. package/dist/settlement/contracts/authorizations/UpdateMessagesHashAuth.js.map +1 -0
  175. package/dist/settlement/messages/Deposit.d.ts +77 -0
  176. package/dist/settlement/messages/Deposit.d.ts.map +1 -0
  177. package/dist/settlement/messages/Deposit.js +8 -0
  178. package/dist/settlement/messages/Deposit.js.map +1 -0
  179. package/dist/settlement/messages/OutgoingMessageArgument.d.ts +197 -0
  180. package/dist/settlement/messages/OutgoingMessageArgument.d.ts.map +1 -0
  181. package/dist/settlement/messages/OutgoingMessageArgument.js +33 -0
  182. package/dist/settlement/messages/OutgoingMessageArgument.js.map +1 -0
  183. package/dist/settlement/messages/Withdrawal.d.ts +78 -0
  184. package/dist/settlement/messages/Withdrawal.d.ts.map +1 -0
  185. package/dist/settlement/messages/Withdrawal.js +16 -0
  186. package/dist/settlement/messages/Withdrawal.js.map +1 -0
  187. package/dist/settlement/modularity/ProvableSettlementHook.d.ts +26 -0
  188. package/dist/settlement/modularity/ProvableSettlementHook.d.ts.map +1 -0
  189. package/dist/settlement/modularity/ProvableSettlementHook.js +4 -0
  190. package/dist/settlement/modularity/ProvableSettlementHook.js.map +1 -0
  191. package/dist/settlement/modularity/types.d.ts +8 -0
  192. package/dist/settlement/modularity/types.d.ts.map +1 -0
  193. package/dist/settlement/modularity/types.js +2 -0
  194. package/dist/settlement/modularity/types.js.map +1 -0
  195. package/dist/settlement/modules/NetworkStateSettlementModule.d.ts +11 -0
  196. package/dist/settlement/modules/NetworkStateSettlementModule.d.ts.map +1 -0
  197. package/dist/settlement/modules/NetworkStateSettlementModule.js +15 -0
  198. package/dist/settlement/modules/NetworkStateSettlementModule.js.map +1 -0
  199. package/dist/state/State.d.ts +68 -0
  200. package/dist/state/State.d.ts.map +1 -0
  201. package/dist/state/State.js +130 -0
  202. package/dist/state/State.js.map +1 -0
  203. package/dist/state/StateMap.d.ts +37 -0
  204. package/dist/state/StateMap.d.ts.map +1 -0
  205. package/dist/state/StateMap.js +56 -0
  206. package/dist/state/StateMap.js.map +1 -0
  207. package/dist/state/StateService.d.ts +6 -0
  208. package/dist/state/StateService.d.ts.map +1 -0
  209. package/dist/state/StateService.js +2 -0
  210. package/dist/state/StateService.js.map +1 -0
  211. package/dist/state/StateServiceProvider.d.ts +8 -0
  212. package/dist/state/StateServiceProvider.d.ts.map +1 -0
  213. package/dist/state/StateServiceProvider.js +37 -0
  214. package/dist/state/StateServiceProvider.js.map +1 -0
  215. package/dist/state/assert/assert.d.ts +12 -0
  216. package/dist/state/assert/assert.d.ts.map +1 -0
  217. package/dist/state/assert/assert.js +31 -0
  218. package/dist/state/assert/assert.js.map +1 -0
  219. package/dist/state/context/RuntimeMethodExecutionContext.d.ts +193 -0
  220. package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -0
  221. package/dist/state/context/RuntimeMethodExecutionContext.js +136 -0
  222. package/dist/state/context/RuntimeMethodExecutionContext.js.map +1 -0
  223. package/dist/state/context/TransitionMethodExecutionContext.d.ts +23 -0
  224. package/dist/state/context/TransitionMethodExecutionContext.d.ts.map +1 -0
  225. package/dist/state/context/TransitionMethodExecutionContext.js +6 -0
  226. package/dist/state/context/TransitionMethodExecutionContext.js.map +1 -0
  227. package/dist/state/protocol/ProtocolState.d.ts +7 -0
  228. package/dist/state/protocol/ProtocolState.d.ts.map +1 -0
  229. package/dist/state/protocol/ProtocolState.js +40 -0
  230. package/dist/state/protocol/ProtocolState.js.map +1 -0
  231. package/dist/utils/MinaPrefixedProvableHashList.d.ts +24 -0
  232. package/dist/utils/MinaPrefixedProvableHashList.d.ts.map +1 -0
  233. package/dist/utils/MinaPrefixedProvableHashList.js +53 -0
  234. package/dist/utils/MinaPrefixedProvableHashList.js.map +1 -0
  235. package/dist/utils/PrefixedProvableHashList.d.ts +8 -0
  236. package/dist/utils/PrefixedProvableHashList.d.ts.map +1 -0
  237. package/dist/utils/PrefixedProvableHashList.js +13 -0
  238. package/dist/utils/PrefixedProvableHashList.js.map +1 -0
  239. package/dist/utils/ProvableHashList.d.ts +27 -0
  240. package/dist/utils/ProvableHashList.d.ts.map +1 -0
  241. package/dist/utils/ProvableHashList.js +44 -0
  242. package/dist/utils/ProvableHashList.js.map +1 -0
  243. package/dist/utils/ProvableReductionHashList.d.ts +14 -0
  244. package/dist/utils/ProvableReductionHashList.d.ts.map +1 -0
  245. package/dist/utils/ProvableReductionHashList.js +51 -0
  246. package/dist/utils/ProvableReductionHashList.js.map +1 -0
  247. package/dist/utils/StateTransitionReductionList.d.ts +11 -0
  248. package/dist/utils/StateTransitionReductionList.d.ts.map +1 -0
  249. package/dist/utils/StateTransitionReductionList.js +61 -0
  250. package/dist/utils/StateTransitionReductionList.js.map +1 -0
  251. package/dist/utils/utils.d.ts +11 -0
  252. package/dist/utils/utils.d.ts.map +1 -0
  253. package/dist/utils/utils.js +45 -0
  254. package/dist/utils/utils.js.map +1 -0
  255. package/jest.config.cjs +12 -0
  256. package/package.json +36 -0
  257. package/src/Constants.ts +3 -0
  258. package/src/hooks/AccountStateHook.ts +48 -0
  259. package/src/hooks/BlockHeightHook.ts +17 -0
  260. package/src/hooks/LastStateRootBlockHook.ts +26 -0
  261. package/src/hooks/NoopBlockHook.ts +21 -0
  262. package/src/hooks/NoopSettlementHook.ts +20 -0
  263. package/src/hooks/NoopTransactionHook.ts +10 -0
  264. package/src/index.ts +57 -0
  265. package/src/model/MethodPublicOutput.ts +14 -0
  266. package/src/model/Option.ts +172 -0
  267. package/src/model/Path.ts +50 -0
  268. package/src/model/RuntimeLike.ts +12 -0
  269. package/src/model/StateTransition.ts +88 -0
  270. package/src/model/StateTransitionProvableBatch.ts +145 -0
  271. package/src/model/Transaction.ts +31 -0
  272. package/src/model/network/NetworkState.ts +33 -0
  273. package/src/model/transaction/RuntimeTransaction.ts +105 -0
  274. package/src/model/transaction/SignedTransaction.ts +47 -0
  275. package/src/model/transaction/ValueOption.ts +28 -0
  276. package/src/protocol/Protocol.ts +226 -0
  277. package/src/protocol/ProtocolEnvironment.ts +10 -0
  278. package/src/protocol/ProtocolModule.ts +27 -0
  279. package/src/protocol/ProvableBlockHook.ts +19 -0
  280. package/src/protocol/ProvableTransactionHook.ts +13 -0
  281. package/src/protocol/TransitioningProtocolModule.ts +12 -0
  282. package/src/prover/block/BlockProvable.ts +99 -0
  283. package/src/prover/block/BlockProver.ts +990 -0
  284. package/src/prover/block/accummulators/BlockHashMerkleTree.ts +16 -0
  285. package/src/prover/block/accummulators/RuntimeVerificationKeyTree.ts +24 -0
  286. package/src/prover/block/services/RuntimeVerificationKeyRootService.ts +20 -0
  287. package/src/prover/statetransition/StateTransitionProvable.ts +41 -0
  288. package/src/prover/statetransition/StateTransitionProver.ts +377 -0
  289. package/src/settlement/ContractModule.ts +31 -0
  290. package/src/settlement/SettlementContractModule.ts +145 -0
  291. package/src/settlement/contracts/BridgeContract.ts +237 -0
  292. package/src/settlement/contracts/BridgeContractProtocolModule.ts +43 -0
  293. package/src/settlement/contracts/DispatchContractProtocolModule.ts +73 -0
  294. package/src/settlement/contracts/DispatchSmartContract.ts +326 -0
  295. package/src/settlement/contracts/SettlementContractProtocolModule.ts +108 -0
  296. package/src/settlement/contracts/SettlementSmartContract.ts +498 -0
  297. package/src/settlement/contracts/TokenBridgeTree.ts +73 -0
  298. package/src/settlement/contracts/authorizations/ContractAuthorization.ts +11 -0
  299. package/src/settlement/contracts/authorizations/TokenBridgeDeploymentAuth.ts +16 -0
  300. package/src/settlement/contracts/authorizations/UpdateMessagesHashAuth.ts +16 -0
  301. package/src/settlement/messages/Deposit.ts +7 -0
  302. package/src/settlement/messages/OutgoingMessageArgument.ts +42 -0
  303. package/src/settlement/messages/Withdrawal.ts +16 -0
  304. package/src/settlement/modularity/ProvableSettlementHook.ts +36 -0
  305. package/src/settlement/modularity/types.ts +22 -0
  306. package/src/settlement/modules/NetworkStateSettlementModule.ts +42 -0
  307. package/src/state/State.ts +175 -0
  308. package/src/state/StateMap.ts +73 -0
  309. package/src/state/StateService.ts +6 -0
  310. package/src/state/StateServiceProvider.ts +37 -0
  311. package/src/state/assert/assert.ts +35 -0
  312. package/src/state/context/RuntimeMethodExecutionContext.ts +174 -0
  313. package/src/state/context/TransitionMethodExecutionContext.ts +26 -0
  314. package/src/state/protocol/ProtocolState.ts +61 -0
  315. package/src/utils/MinaPrefixedProvableHashList.ts +73 -0
  316. package/src/utils/PrefixedProvableHashList.ts +21 -0
  317. package/src/utils/ProvableHashList.ts +50 -0
  318. package/src/utils/ProvableReductionHashList.ts +68 -0
  319. package/src/utils/StateTransitionReductionList.ts +89 -0
  320. package/src/utils/utils.ts +75 -0
  321. package/test/BlockProver.test.ts +212 -0
  322. package/test/Protocol.test.ts +37 -0
  323. package/test/State.test.ts +45 -0
  324. package/test/StateTransition.test.ts +174 -0
  325. package/test/TestingProtocol.ts +52 -0
  326. package/test/compiling/types.ts +28 -0
  327. package/test/model/Option.test.ts +72 -0
  328. package/test/modularity/types.ts +35 -0
  329. package/test/state/assert/assert.test.ts +56 -0
  330. package/test/tsconfig.json +7 -0
  331. package/test/utils/ProvableReductionHashList.test.ts +117 -0
  332. package/test/utils.test.ts +27 -0
  333. package/tsconfig.json +8 -0
@@ -0,0 +1,990 @@
1
+ import {
2
+ Bool,
3
+ DynamicProof,
4
+ Field,
5
+ Poseidon,
6
+ Proof,
7
+ Provable,
8
+ SelfProof,
9
+ VerificationKey,
10
+ ZkProgram,
11
+ } from "o1js";
12
+ import { container, inject, injectable, injectAll } from "tsyringe";
13
+ import {
14
+ AreProofsEnabled,
15
+ CompilableModule,
16
+ CompileArtifact,
17
+ CompileRegistry,
18
+ MAX_FIELD,
19
+ PlainZkProgram,
20
+ provableMethod,
21
+ WithZkProgrammable,
22
+ ZkProgrammable,
23
+ } from "@proto-kit/common";
24
+
25
+ import { DefaultProvableHashList } from "../../utils/ProvableHashList";
26
+ import { MethodPublicOutput } from "../../model/MethodPublicOutput";
27
+ import { ProtocolModule } from "../../protocol/ProtocolModule";
28
+ import {
29
+ StateTransitionProof,
30
+ StateTransitionProvable,
31
+ StateTransitionProverPublicInput,
32
+ StateTransitionProverPublicOutput,
33
+ } from "../statetransition/StateTransitionProvable";
34
+ import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
35
+ import {
36
+ ProvableStateTransition,
37
+ StateTransition,
38
+ } from "../../model/StateTransition";
39
+ import { ProvableTransactionHook } from "../../protocol/ProvableTransactionHook";
40
+ import { RuntimeMethodExecutionContext } from "../../state/context/RuntimeMethodExecutionContext";
41
+ import { ProvableBlockHook } from "../../protocol/ProvableBlockHook";
42
+ import { NetworkState } from "../../model/network/NetworkState";
43
+ import { SignedTransaction } from "../../model/transaction/SignedTransaction";
44
+ import {
45
+ MinaActions,
46
+ MinaActionsHashList,
47
+ } from "../../utils/MinaPrefixedProvableHashList";
48
+ import { StateTransitionReductionList } from "../../utils/StateTransitionReductionList";
49
+
50
+ import {
51
+ BlockProvable,
52
+ BlockProverExecutionData,
53
+ BlockProverProof,
54
+ BlockProverPublicInput,
55
+ BlockProverPublicOutput,
56
+ DynamicRuntimeProof,
57
+ } from "./BlockProvable";
58
+ import {
59
+ BlockHashMerkleTreeWitness,
60
+ BlockHashTreeEntry,
61
+ } from "./accummulators/BlockHashMerkleTree";
62
+ import {
63
+ MethodVKConfigData,
64
+ MinimalVKTreeService,
65
+ RuntimeVerificationKeyAttestation,
66
+ } from "./accummulators/RuntimeVerificationKeyTree";
67
+ import { RuntimeVerificationKeyRootService } from "./services/RuntimeVerificationKeyRootService";
68
+
69
+ const errors = {
70
+ stateProofNotStartingAtZero: () =>
71
+ "StateProof not starting ST-commitment at zero",
72
+
73
+ stateTransitionsHashNotEqual: () =>
74
+ "StateTransition list commitments are not equal",
75
+
76
+ propertyNotMatchingStep: (propertyName: string, step: string) =>
77
+ `${propertyName} not matching: ${step}`,
78
+
79
+ propertyNotMatching: (propertyName: string) => `${propertyName} not matching`,
80
+
81
+ stateRootNotMatching: (step: string) =>
82
+ errors.propertyNotMatchingStep("StateRoots", step),
83
+
84
+ transactionsHashNotMatching: (step: string) =>
85
+ errors.propertyNotMatchingStep("Transactions hash", step),
86
+
87
+ networkStateHashNotMatching: (step: string) =>
88
+ errors.propertyNotMatchingStep("Network state hash", step),
89
+
90
+ invalidZkProgramTreeRoot: () =>
91
+ "Root hash of the provided zkProgram config witness is invalid",
92
+
93
+ invalidZkProgramConfigMethodId: () =>
94
+ "Method id of the provided zkProgram config does not match the executed transaction method id",
95
+ };
96
+
97
+ // Should be equal to BlockProver.PublicInput
98
+ export interface BlockProverState {
99
+ /**
100
+ * The current state root of the block prover
101
+ */
102
+ stateRoot: Field;
103
+
104
+ /**
105
+ * The current commitment of the transaction-list which
106
+ * will at the end equal the bundle hash
107
+ */
108
+ transactionsHash: Field;
109
+
110
+ /**
111
+ * The network state which gives access to values such as blockHeight
112
+ * This value is the same for the whole batch (L2 block)
113
+ */
114
+ networkStateHash: Field;
115
+
116
+ /**
117
+ * The root of the merkle tree encoding all block hashes,
118
+ * see `BlockHashMerkleTree`
119
+ */
120
+ blockHashRoot: Field;
121
+
122
+ /**
123
+ * A variant of the transactionsHash that is never reset.
124
+ * Thought for usage in the sequence state mempool.
125
+ * In comparison, transactionsHash restarts at 0 for every new block
126
+ */
127
+ eternalTransactionsHash: Field;
128
+
129
+ incomingMessagesHash: Field;
130
+ }
131
+
132
+ export type BlockProof = Proof<BlockProverPublicInput, BlockProverPublicOutput>;
133
+ export type RuntimeProof = Proof<void, MethodPublicOutput>;
134
+
135
+ export class BlockProverProgrammable extends ZkProgrammable<
136
+ BlockProverPublicInput,
137
+ BlockProverPublicOutput
138
+ > {
139
+ public constructor(
140
+ private readonly prover: BlockProver,
141
+ public readonly stateTransitionProver: ZkProgrammable<
142
+ StateTransitionProverPublicInput,
143
+ StateTransitionProverPublicOutput
144
+ >,
145
+ public readonly runtime: ZkProgrammable<undefined, MethodPublicOutput>,
146
+ private readonly transactionHooks: ProvableTransactionHook<unknown>[],
147
+ private readonly blockHooks: ProvableBlockHook<unknown>[],
148
+ private readonly verificationKeyService: MinimalVKTreeService
149
+ ) {
150
+ super();
151
+ }
152
+
153
+ name = "BlockProver";
154
+
155
+ public get areProofsEnabled(): AreProofsEnabled | undefined {
156
+ return this.prover.areProofsEnabled;
157
+ }
158
+
159
+ /**
160
+ * Applies and checks the two proofs and applies the corresponding state
161
+ * changes to the given state
162
+ *
163
+ * @param state The from-state of the BlockProver
164
+ * @param stateTransitionProof
165
+ * @param runtimeProof
166
+ * @param executionData
167
+ * @param verificationKey
168
+ * @returns The new BlockProver-state to be used as public output
169
+ */
170
+ public async applyTransaction(
171
+ state: BlockProverState,
172
+ stateTransitionProof: Proof<
173
+ StateTransitionProverPublicInput,
174
+ StateTransitionProverPublicOutput
175
+ >,
176
+ runtimeProof: DynamicRuntimeProof,
177
+ executionData: BlockProverExecutionData,
178
+ verificationKey: VerificationKey
179
+ ): Promise<BlockProverState> {
180
+ const { transaction, networkState, signature } = executionData;
181
+
182
+ const { isMessage } = runtimeProof.publicOutput;
183
+
184
+ runtimeProof.verify(verificationKey);
185
+ stateTransitionProof.verify();
186
+
187
+ const stateTo = { ...state };
188
+
189
+ // Checks for the stateTransitionProof and appProof matching
190
+ stateTransitionProof.publicInput.stateTransitionsHash.assertEquals(
191
+ Field(0),
192
+ errors.stateProofNotStartingAtZero()
193
+ );
194
+ stateTransitionProof.publicInput.protocolTransitionsHash.assertEquals(
195
+ Field(0),
196
+ errors.stateProofNotStartingAtZero()
197
+ );
198
+
199
+ runtimeProof.publicOutput.stateTransitionsHash.assertEquals(
200
+ stateTransitionProof.publicOutput.stateTransitionsHash,
201
+ errors.stateTransitionsHashNotEqual()
202
+ );
203
+
204
+ // Assert from state roots
205
+ state.stateRoot.assertEquals(
206
+ stateTransitionProof.publicInput.stateRoot,
207
+ errors.propertyNotMatching("from state root")
208
+ );
209
+ state.stateRoot.assertEquals(
210
+ stateTransitionProof.publicInput.protocolStateRoot,
211
+ errors.propertyNotMatching("from protocol state root")
212
+ );
213
+
214
+ // Apply protocol state transitions
215
+ await this.assertProtocolTransitions(
216
+ stateTransitionProof,
217
+ executionData,
218
+ runtimeProof
219
+ );
220
+
221
+ // Apply state if status success
222
+ stateTo.stateRoot = Provable.if(
223
+ runtimeProof.publicOutput.status,
224
+ stateTransitionProof.publicOutput.stateRoot,
225
+ stateTransitionProof.publicOutput.protocolStateRoot
226
+ );
227
+
228
+ // Check transaction integrity against appProof
229
+ const blockTransactionHash = transaction.hash();
230
+
231
+ blockTransactionHash.assertEquals(
232
+ runtimeProof.publicOutput.transactionHash,
233
+ "Transactions provided in AppProof and BlockProof do not match"
234
+ );
235
+
236
+ // Check transaction signature
237
+ new SignedTransaction({
238
+ transaction,
239
+ signature,
240
+ })
241
+ .validateSignature()
242
+ .or(isMessage)
243
+ .assertTrue("Transaction signature not valid");
244
+
245
+ // Validate layout of transaction witness
246
+ transaction.assertTransactionType(isMessage);
247
+
248
+ // Check network state integrity against appProof
249
+ state.networkStateHash.assertEquals(
250
+ runtimeProof.publicOutput.networkStateHash,
251
+ "Network state does not match state used in AppProof"
252
+ );
253
+ state.networkStateHash.assertEquals(
254
+ networkState.hash(),
255
+ "Network state provided to BlockProver does not match the publicInput"
256
+ );
257
+
258
+ return stateTo;
259
+ }
260
+
261
+ // eslint-disable-next-line max-len
262
+ // TODO How does this interact with the RuntimeMethodExecutionContext when executing runtimemethods?
263
+
264
+ public async assertProtocolTransitions(
265
+ stateTransitionProof: Proof<
266
+ StateTransitionProverPublicInput,
267
+ StateTransitionProverPublicOutput
268
+ >,
269
+ executionData: BlockProverExecutionData,
270
+ runtimeProof: DynamicProof<void, MethodPublicOutput>
271
+ ) {
272
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
273
+ executionContext.clear();
274
+
275
+ // Setup context for potential calls to runtime methods.
276
+ // This way they can use this.transaction etc. while still having provable
277
+ // integrity between data
278
+ executionContext.setup({
279
+ // That is why we should probably hide it from the transaction context inputs
280
+ transaction: executionData.transaction,
281
+ networkState: executionData.networkState,
282
+ });
283
+ executionContext.beforeMethod("", "", []);
284
+
285
+ for (const module of this.transactionHooks) {
286
+ // eslint-disable-next-line no-await-in-loop
287
+ await module.onTransaction(executionData);
288
+ }
289
+
290
+ executionContext.afterMethod();
291
+
292
+ const { stateTransitions, status, statusMessage } =
293
+ executionContext.current().result;
294
+
295
+ status.assertTrue(statusMessage);
296
+
297
+ const transitions = stateTransitions.map((transition) =>
298
+ transition.toProvable()
299
+ );
300
+
301
+ const hashList = new StateTransitionReductionList(
302
+ ProvableStateTransition,
303
+ stateTransitionProof.publicInput.protocolTransitionsHash
304
+ );
305
+
306
+ transitions.forEach((transition) => {
307
+ hashList.push(transition);
308
+ });
309
+
310
+ stateTransitionProof.publicOutput.protocolTransitionsHash.assertEquals(
311
+ hashList.commitment,
312
+ "ProtocolTransitionsHash not matching the generated protocol transitions"
313
+ );
314
+ }
315
+
316
+ private async executeBlockHooks(
317
+ state: BlockProverState,
318
+ inputNetworkState: NetworkState,
319
+ type: "afterBlock" | "beforeBlock"
320
+ ): Promise<{
321
+ networkState: NetworkState;
322
+ stateTransitions: StateTransition<unknown>[];
323
+ }> {
324
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
325
+ executionContext.clear();
326
+ executionContext.beforeMethod("", "", []);
327
+
328
+ const resultingNetworkState = await this.blockHooks.reduce<
329
+ Promise<NetworkState>
330
+ >(async (networkStatePromise, blockHook) => {
331
+ const networkState = await networkStatePromise;
332
+ // Setup context for potential calls to runtime methods.
333
+ // With the special case that we set the new networkstate for every hook
334
+ // We also have to put in a dummy transaction for network.transaction
335
+ executionContext.setup({
336
+ transaction: RuntimeTransaction.dummyTransaction(),
337
+ networkState,
338
+ });
339
+
340
+ if (type === "beforeBlock") {
341
+ return await blockHook.beforeBlock(networkState, state);
342
+ }
343
+ if (type === "afterBlock") {
344
+ return await blockHook.afterBlock(networkState, state);
345
+ }
346
+ throw new Error("Unreachable");
347
+ }, Promise.resolve(inputNetworkState));
348
+
349
+ executionContext.afterMethod();
350
+
351
+ const { stateTransitions, status, statusMessage } =
352
+ executionContext.current().result;
353
+
354
+ status.assertTrue(`Block hook call failed: ${statusMessage ?? "-"}`);
355
+
356
+ return {
357
+ networkState: resultingNetworkState,
358
+ stateTransitions,
359
+ };
360
+ }
361
+
362
+ private addTransactionToBundle(
363
+ state: BlockProverState,
364
+ isMessage: Bool,
365
+ transaction: RuntimeTransaction
366
+ ): BlockProverState {
367
+ const stateTo = {
368
+ ...state,
369
+ };
370
+
371
+ const transactionHash = transaction.hash();
372
+
373
+ // Append tx to transaction list
374
+ const transactionList = new DefaultProvableHashList(
375
+ Field,
376
+ state.transactionsHash
377
+ );
378
+
379
+ transactionList.pushIf(transactionHash, isMessage.not());
380
+ stateTo.transactionsHash = transactionList.commitment;
381
+
382
+ // Append tx to eternal transaction list
383
+ // TODO Change that to the a sequence-state compatible transaction struct
384
+ const eternalTransactionList = new DefaultProvableHashList(
385
+ Field,
386
+ state.eternalTransactionsHash
387
+ );
388
+
389
+ eternalTransactionList.pushIf(transactionHash, isMessage.not());
390
+ stateTo.eternalTransactionsHash = eternalTransactionList.commitment;
391
+
392
+ // Append tx to incomingMessagesHash
393
+ const actionHash = MinaActions.actionHash(transaction.hashData());
394
+
395
+ const incomingMessagesList = new MinaActionsHashList(
396
+ state.incomingMessagesHash
397
+ );
398
+ incomingMessagesList.pushIf(actionHash, isMessage);
399
+
400
+ stateTo.incomingMessagesHash = incomingMessagesList.commitment;
401
+
402
+ return stateTo;
403
+ }
404
+
405
+ @provableMethod()
406
+ public async proveTransaction(
407
+ publicInput: BlockProverPublicInput,
408
+ stateProof: StateTransitionProof,
409
+ runtimeProof: DynamicRuntimeProof,
410
+ executionData: BlockProverExecutionData,
411
+ verificationKeyWitness: RuntimeVerificationKeyAttestation
412
+ ): Promise<BlockProverPublicOutput> {
413
+ const state: BlockProverState = {
414
+ ...publicInput,
415
+ };
416
+
417
+ state.networkStateHash.assertEquals(
418
+ executionData.networkState.hash(),
419
+ "ExecutionData Networkstate doesn't equal public input hash"
420
+ );
421
+
422
+ publicInput.blockNumber.assertEquals(
423
+ MAX_FIELD,
424
+ "blockNumber has to be MAX for transaction proofs"
425
+ );
426
+
427
+ // Verify the [methodId, vk] tuple against the baked-in vk tree root
428
+ const { verificationKey, witness: verificationKeyTreeWitness } =
429
+ verificationKeyWitness;
430
+
431
+ const root = Field(this.verificationKeyService.getRoot());
432
+ const calculatedRoot = verificationKeyTreeWitness.calculateRoot(
433
+ new MethodVKConfigData({
434
+ methodId: executionData.transaction.methodId,
435
+ vkHash: verificationKey.hash,
436
+ }).hash()
437
+ );
438
+ root.assertEquals(calculatedRoot, errors.invalidZkProgramTreeRoot());
439
+
440
+ const bundleInclusionState = this.addTransactionToBundle(
441
+ state,
442
+ runtimeProof.publicOutput.isMessage,
443
+ executionData.transaction
444
+ );
445
+
446
+ const stateTo = await this.applyTransaction(
447
+ bundleInclusionState,
448
+ stateProof,
449
+ runtimeProof,
450
+ executionData,
451
+ verificationKey
452
+ );
453
+
454
+ return new BlockProverPublicOutput({
455
+ ...stateTo,
456
+ blockNumber: publicInput.blockNumber,
457
+ closed: Bool(false),
458
+ });
459
+ }
460
+
461
+ private assertSTProofInput(
462
+ stateTransitionProof: StateTransitionProof,
463
+ stateRoot: Field
464
+ ) {
465
+ stateTransitionProof.publicInput.stateTransitionsHash.assertEquals(
466
+ Field(0),
467
+ errors.stateProofNotStartingAtZero()
468
+ );
469
+ stateTransitionProof.publicInput.protocolTransitionsHash.assertEquals(
470
+ Field(0),
471
+ errors.stateProofNotStartingAtZero()
472
+ );
473
+
474
+ // Assert from state roots
475
+ stateRoot.assertEquals(
476
+ stateTransitionProof.publicInput.stateRoot,
477
+ errors.propertyNotMatching("from state root")
478
+ );
479
+ }
480
+
481
+ @provableMethod()
482
+ public async proveBlock(
483
+ publicInput: BlockProverPublicInput,
484
+ networkState: NetworkState,
485
+ blockWitness: BlockHashMerkleTreeWitness,
486
+ // stateTransitionProof: StateTransitionProof,
487
+ transactionProof: BlockProverProof
488
+ ): Promise<BlockProverPublicOutput> {
489
+ const state: BlockProverState = {
490
+ ...publicInput,
491
+ };
492
+
493
+ // 1. Make assertions about the inputs
494
+ publicInput.transactionsHash.assertEquals(
495
+ Field(0),
496
+ "Transactionshash has to start at 0"
497
+ );
498
+ publicInput.networkStateHash.assertEquals(
499
+ networkState.hash(),
500
+ "Wrong NetworkState supplied"
501
+ );
502
+
503
+ transactionProof.publicInput.transactionsHash.assertEquals(
504
+ Field(0),
505
+ "TransactionProof transactionshash has to start at 0"
506
+ );
507
+ transactionProof.publicInput.blockHashRoot.assertEquals(
508
+ Field(0),
509
+ "TransactionProof cannot carry the blockHashRoot - publicInput"
510
+ );
511
+ transactionProof.publicOutput.blockHashRoot.assertEquals(
512
+ Field(0),
513
+ "TransactionProof cannot carry the blockHashRoot - publicOutput"
514
+ );
515
+ transactionProof.publicInput.networkStateHash.assertEquals(
516
+ transactionProof.publicOutput.networkStateHash,
517
+ "TransactionProof cannot alter the network state"
518
+ );
519
+ transactionProof.publicInput.eternalTransactionsHash.assertEquals(
520
+ state.eternalTransactionsHash,
521
+ "TransactionProof starting eternalTransactionHash not matching"
522
+ );
523
+ transactionProof.publicInput.incomingMessagesHash.assertEquals(
524
+ state.incomingMessagesHash,
525
+ "TransactionProof starting incomingMessagesHash not matching"
526
+ );
527
+
528
+ // TODO Reintroduce ST Proofs
529
+ // Verify ST Proof only if STs have been emitted,
530
+ // otherwise we can input a dummy proof
531
+ // const stsEmitted = stateTransitionProof.publicOutput.stateTransitionsHash
532
+ // .equals(0)
533
+ // .and(stateTransitionProof.publicOutput.protocolTransitionsHash.equals(0))
534
+ // .not();
535
+ // Provable.log("VerifyIf 1", stsEmitted);
536
+ // stateTransitionProof.verifyIf(Bool(false));
537
+ // stateTransitionProof.verifyIf(stsEmitted);
538
+
539
+ // Verify Transaction proof if it has at least 1 tx - i.e. the
540
+ // input and output doesn't match fully
541
+ // We have to compare the whole input and output because we can make no
542
+ // assumptions about the values, since it can be an arbitrary dummy-proof
543
+ const txProofOutput = transactionProof.publicOutput;
544
+ const isEmptyTransition = txProofOutput.equals(
545
+ transactionProof.publicInput,
546
+ txProofOutput.closed
547
+ );
548
+ Provable.log("VerifyIf 2", isEmptyTransition.not());
549
+ transactionProof.verifyIf(isEmptyTransition.not());
550
+
551
+ // 2. Execute beforeBlock hooks
552
+ const beforeBlockResult = await this.executeBlockHooks(
553
+ state,
554
+ networkState,
555
+ "beforeBlock"
556
+ );
557
+
558
+ // const beforeBlockHashList = new StateTransitionReductionList(
559
+ // ProvableStateTransition
560
+ // );
561
+ // beforeBlockResult.stateTransitions.forEach((st) => {
562
+ // beforeBlockHashList.push(st.toProvable());
563
+ // });
564
+
565
+ // We are reusing protocolSTs here as beforeBlock STs
566
+ // TODO Not possible atm bcs we can't have a seperation between protocol/runtime state roots,
567
+ // which we would for both before and after to be able to emit STs
568
+
569
+ // stateTransitionProof.publicInput.protocolTransitionsHash.assertEquals(
570
+ // beforeBlockHashList.commitment
571
+ // );
572
+ // state.stateRoot = stateTransitionProof.publicInput.protocolStateRoot;
573
+
574
+ // TODO Only for now
575
+ // beforeBlockHashList.commitment.assertEquals(
576
+ // Field(0),
577
+ // "beforeBlock() cannot emit state transitions yet"
578
+ // );
579
+
580
+ // 4. Apply TX-type BlockProof
581
+ transactionProof.publicInput.networkStateHash.assertEquals(
582
+ beforeBlockResult.networkState.hash(),
583
+ "TransactionProof networkstate hash not matching beforeBlock hook result"
584
+ );
585
+ transactionProof.publicInput.stateRoot.assertEquals(
586
+ state.stateRoot,
587
+ "TransactionProof input state root not matching blockprover state root"
588
+ );
589
+
590
+ state.stateRoot = transactionProof.publicOutput.stateRoot;
591
+ state.transactionsHash = transactionProof.publicOutput.transactionsHash;
592
+ state.eternalTransactionsHash =
593
+ transactionProof.publicOutput.eternalTransactionsHash;
594
+ state.incomingMessagesHash =
595
+ transactionProof.publicOutput.incomingMessagesHash;
596
+
597
+ // 5. Execute afterBlock hooks
598
+ // this.assertSTProofInput(stateTransitionProof, state.stateRoot);
599
+
600
+ const afterBlockResult = await this.executeBlockHooks(
601
+ state,
602
+ beforeBlockResult.networkState,
603
+ "afterBlock"
604
+ );
605
+
606
+ const afterBlockHashList = new StateTransitionReductionList(
607
+ ProvableStateTransition
608
+ );
609
+ afterBlockResult.stateTransitions.forEach((st) => {
610
+ afterBlockHashList.push(st.toProvable());
611
+ });
612
+
613
+ state.networkStateHash = afterBlockResult.networkState.hash();
614
+
615
+ // We are reusing runtime STs here as afterBlock STs
616
+ // stateTransitionProof.publicInput.protocolTransitionsHash.assertEquals(
617
+ // afterBlockHashList.commitment,
618
+ // "STProof from-ST-hash not matching generated ST-hash from afterBlock hooks"
619
+ // );
620
+ // state.stateRoot = Provable.if(
621
+ // stsEmitted,
622
+ // stateTransitionProof.publicOutput.stateRoot,
623
+ // state.stateRoot
624
+ // );
625
+
626
+ // 6. Close block
627
+
628
+ // Calculate the new block index
629
+ const blockIndex = blockWitness.calculateIndex();
630
+
631
+ blockIndex.assertEquals(publicInput.blockNumber);
632
+
633
+ blockWitness
634
+ .calculateRoot(Field(0))
635
+ .assertEquals(
636
+ publicInput.blockHashRoot,
637
+ "Supplied block hash witness not matching state root"
638
+ );
639
+
640
+ state.blockHashRoot = blockWitness.calculateRoot(
641
+ new BlockHashTreeEntry({
642
+ // Mirroring UnprovenBlock.hash()
643
+ blockHash: Poseidon.hash([blockIndex, state.transactionsHash]),
644
+ closed: Bool(true),
645
+ }).hash()
646
+ );
647
+
648
+ return new BlockProverPublicOutput({
649
+ ...state,
650
+ blockNumber: blockIndex.add(1),
651
+ closed: Bool(true),
652
+ });
653
+ }
654
+
655
+ @provableMethod()
656
+ public async merge(
657
+ publicInput: BlockProverPublicInput,
658
+ proof1: BlockProverProof,
659
+ proof2: BlockProverProof
660
+ ): Promise<BlockProverPublicOutput> {
661
+ proof1.verify();
662
+ proof2.verify();
663
+
664
+ // Check state
665
+ publicInput.stateRoot.assertEquals(
666
+ proof1.publicInput.stateRoot,
667
+ errors.stateRootNotMatching("publicInput.from -> proof1.from")
668
+ );
669
+ proof1.publicOutput.stateRoot.assertEquals(
670
+ proof2.publicInput.stateRoot,
671
+ errors.stateRootNotMatching("proof1.to -> proof2.from")
672
+ );
673
+
674
+ // Check transaction list hash.
675
+ // Only assert them if these are tx proofs, skip for closed proofs
676
+ publicInput.transactionsHash
677
+ .equals(proof1.publicInput.transactionsHash)
678
+ .or(proof1.publicOutput.closed)
679
+ .assertTrue(
680
+ errors.transactionsHashNotMatching("publicInput.from -> proof1.from")
681
+ );
682
+ proof1.publicOutput.transactionsHash
683
+ .equals(proof2.publicInput.transactionsHash)
684
+ .or(proof1.publicOutput.closed)
685
+ .assertTrue(
686
+ errors.transactionsHashNotMatching("proof1.to -> proof2.from")
687
+ );
688
+
689
+ // Check networkhash
690
+ publicInput.networkStateHash.assertEquals(
691
+ proof1.publicInput.networkStateHash,
692
+ errors.networkStateHashNotMatching("publicInput.from -> proof1.from")
693
+ );
694
+ proof1.publicOutput.networkStateHash.assertEquals(
695
+ proof2.publicInput.networkStateHash,
696
+ errors.networkStateHashNotMatching("proof1.to -> proof2.from")
697
+ );
698
+
699
+ // Check blockHashRoot
700
+ publicInput.blockHashRoot.assertEquals(
701
+ proof1.publicInput.blockHashRoot,
702
+ errors.transactionsHashNotMatching("publicInput.from -> proof1.from")
703
+ );
704
+ proof1.publicOutput.blockHashRoot.assertEquals(
705
+ proof2.publicInput.blockHashRoot,
706
+ errors.transactionsHashNotMatching("proof1.to -> proof2.from")
707
+ );
708
+
709
+ // Check eternalTransactionsHash
710
+ publicInput.eternalTransactionsHash.assertEquals(
711
+ proof1.publicInput.eternalTransactionsHash,
712
+ errors.transactionsHashNotMatching("publicInput.from -> proof1.from")
713
+ );
714
+ proof1.publicOutput.eternalTransactionsHash.assertEquals(
715
+ proof2.publicInput.eternalTransactionsHash,
716
+ errors.transactionsHashNotMatching("proof1.to -> proof2.from")
717
+ );
718
+
719
+ // Check incomingMessagesHash
720
+ publicInput.incomingMessagesHash.assertEquals(
721
+ proof1.publicInput.incomingMessagesHash,
722
+ errors.propertyNotMatchingStep(
723
+ "IncomingMessagesHash",
724
+ "publicInput.from -> proof1.from"
725
+ )
726
+ );
727
+ proof1.publicOutput.incomingMessagesHash.assertEquals(
728
+ proof2.publicInput.incomingMessagesHash,
729
+ errors.propertyNotMatchingStep(
730
+ "IncomingMessagesHash",
731
+ "proof1.to -> proof2.from"
732
+ )
733
+ );
734
+
735
+ // Assert closed indicator matches
736
+ // (i.e. we can only merge TX-Type and Block-Type with each other)
737
+ proof1.publicOutput.closed.assertEquals(
738
+ proof2.publicOutput.closed,
739
+ "Closed indicators not matching"
740
+ );
741
+
742
+ // Either
743
+ // blockNumbers are unset and proofs are unclosed or
744
+ // both blocks are closed, then they have to increment or
745
+ // one block is closed, then height has to be the same
746
+
747
+ // Imperative algo would look like
748
+ // if(proof1.height == MAX && proof2.height == MAX){
749
+ // assert !proof1.closed && !proof2.closed;
750
+ // }else if(proof1.closed && proof2.closed){
751
+ // assert proof1.height + 1 == proof2.height
752
+ // // next one is omitted for now
753
+ // }else if(proof1.closed || proof2.closed{
754
+ // assert proof1.height == proof2.height
755
+ // }
756
+
757
+ const proof1Closed = proof1.publicOutput.closed;
758
+ const proof2Closed = proof2.publicOutput.closed;
759
+
760
+ const blockNumberProgressionValid = publicInput.blockNumber
761
+ .equals(proof1.publicInput.blockNumber)
762
+ .and(
763
+ proof1.publicOutput.blockNumber.equals(proof2.publicInput.blockNumber)
764
+ );
765
+
766
+ // For tx proofs, we check that the progression starts and end with MAX
767
+ // in addition to that both proofs are non-closed
768
+ const isValidTransactionMerge = publicInput.blockNumber
769
+ .equals(MAX_FIELD)
770
+ .and(blockNumberProgressionValid)
771
+ .and(proof1Closed.or(proof2Closed).not());
772
+
773
+ const isValidClosedMerge = proof1Closed
774
+ .and(proof2Closed)
775
+ .and(blockNumberProgressionValid);
776
+
777
+ isValidTransactionMerge
778
+ .or(isValidClosedMerge)
779
+ .assertTrue("Invalid BlockProof merge");
780
+
781
+ return new BlockProverPublicOutput({
782
+ stateRoot: proof2.publicOutput.stateRoot,
783
+ transactionsHash: proof2.publicOutput.transactionsHash,
784
+ networkStateHash: proof2.publicOutput.networkStateHash,
785
+ blockHashRoot: proof2.publicOutput.blockHashRoot,
786
+ eternalTransactionsHash: proof2.publicOutput.eternalTransactionsHash,
787
+ incomingMessagesHash: proof2.publicOutput.incomingMessagesHash,
788
+ closed: isValidClosedMerge,
789
+ blockNumber: proof2.publicOutput.blockNumber,
790
+ });
791
+ }
792
+
793
+ /**
794
+ * Creates the BlockProver ZkProgram.
795
+ * Recursive linking of proofs is done via the previously
796
+ * injected StateTransitionProver and the required AppChainProof class
797
+ */
798
+ public zkProgramFactory(): PlainZkProgram<
799
+ BlockProverPublicInput,
800
+ BlockProverPublicOutput
801
+ >[] {
802
+ const { prover, stateTransitionProver } = this;
803
+ const StateTransitionProofClass = stateTransitionProver.zkProgram[0].Proof;
804
+ const proveTransaction = prover.proveTransaction.bind(prover);
805
+ const proveBlock = prover.proveBlock.bind(prover);
806
+ const merge = prover.merge.bind(prover);
807
+
808
+ const program = ZkProgram({
809
+ name: "BlockProver",
810
+ publicInput: BlockProverPublicInput,
811
+ publicOutput: BlockProverPublicOutput,
812
+
813
+ methods: {
814
+ proveTransaction: {
815
+ privateInputs: [
816
+ StateTransitionProofClass,
817
+ DynamicRuntimeProof,
818
+ BlockProverExecutionData,
819
+ RuntimeVerificationKeyAttestation,
820
+ ],
821
+
822
+ async method(
823
+ publicInput: BlockProverPublicInput,
824
+ stateProof: StateTransitionProof,
825
+ appProof: DynamicRuntimeProof,
826
+ executionData: BlockProverExecutionData,
827
+ verificationKeyAttestation: RuntimeVerificationKeyAttestation
828
+ ) {
829
+ return await proveTransaction(
830
+ publicInput,
831
+ stateProof,
832
+ appProof,
833
+ executionData,
834
+ verificationKeyAttestation
835
+ );
836
+ },
837
+ },
838
+
839
+ proveBlock: {
840
+ privateInputs: [
841
+ NetworkState,
842
+ BlockHashMerkleTreeWitness,
843
+ // StateTransitionProofClass,
844
+ SelfProof<BlockProverPublicInput, BlockProverPublicOutput>,
845
+ ],
846
+ async method(
847
+ publicInput: BlockProverPublicInput,
848
+ networkState: NetworkState,
849
+ blockWitness: BlockHashMerkleTreeWitness,
850
+ // stateTransitionProof: StateTransitionProof,
851
+ transactionProof: BlockProverProof
852
+ ) {
853
+ return await proveBlock(
854
+ publicInput,
855
+ networkState,
856
+ blockWitness,
857
+ // stateTransitionProof,
858
+ transactionProof
859
+ );
860
+ },
861
+ },
862
+
863
+ merge: {
864
+ privateInputs: [
865
+ SelfProof<BlockProverPublicInput, BlockProverPublicOutput>,
866
+ SelfProof<BlockProverPublicInput, BlockProverPublicOutput>,
867
+ ],
868
+
869
+ async method(
870
+ publicInput: BlockProverPublicInput,
871
+ proof1: BlockProverProof,
872
+ proof2: BlockProverProof
873
+ ) {
874
+ return await merge(publicInput, proof1, proof2);
875
+ },
876
+ },
877
+ },
878
+ });
879
+
880
+ const methods = {
881
+ proveTransaction: program.proveTransaction,
882
+ proveBlock: program.proveBlock,
883
+ merge: program.merge,
884
+ };
885
+
886
+ const SelfProofClass = ZkProgram.Proof(program);
887
+
888
+ return [
889
+ {
890
+ name: program.name,
891
+ compile: program.compile.bind(program),
892
+ verify: program.verify.bind(program),
893
+ analyzeMethods: program.analyzeMethods.bind(program),
894
+ Proof: SelfProofClass,
895
+ methods,
896
+ },
897
+ ];
898
+ }
899
+ }
900
+
901
+ /**
902
+ * BlockProver class, which aggregates a AppChainProof and
903
+ * a StateTransitionProof into a single BlockProof, that can
904
+ * then be merged to be committed to the base-layer contract
905
+ */
906
+ @injectable()
907
+ export class BlockProver
908
+ extends ProtocolModule
909
+ implements BlockProvable, CompilableModule
910
+ {
911
+ public zkProgrammable: BlockProverProgrammable;
912
+
913
+ public constructor(
914
+ @inject("StateTransitionProver")
915
+ public readonly stateTransitionProver: WithZkProgrammable<
916
+ StateTransitionProverPublicInput,
917
+ StateTransitionProverPublicOutput
918
+ > &
919
+ StateTransitionProvable,
920
+ @inject("Runtime")
921
+ public readonly runtime: WithZkProgrammable<undefined, MethodPublicOutput> &
922
+ CompilableModule,
923
+ @injectAll("ProvableTransactionHook")
924
+ transactionHooks: ProvableTransactionHook<unknown>[],
925
+ @injectAll("ProvableBlockHook")
926
+ blockHooks: ProvableBlockHook<unknown>[],
927
+ verificationKeyService: RuntimeVerificationKeyRootService
928
+ ) {
929
+ super();
930
+ this.zkProgrammable = new BlockProverProgrammable(
931
+ this,
932
+ stateTransitionProver.zkProgrammable,
933
+ runtime.zkProgrammable,
934
+ transactionHooks,
935
+ blockHooks,
936
+ verificationKeyService
937
+ );
938
+ }
939
+
940
+ public async compile(
941
+ registry: CompileRegistry
942
+ ): Promise<Record<string, CompileArtifact> | undefined> {
943
+ await registry.forceProverExists(async () => {
944
+ await this.stateTransitionProver.compile(registry);
945
+ await this.runtime.compile(registry);
946
+ });
947
+
948
+ return await this.zkProgrammable.compile(registry);
949
+ }
950
+
951
+ public proveTransaction(
952
+ publicInput: BlockProverPublicInput,
953
+ stateProof: StateTransitionProof,
954
+ appProof: DynamicRuntimeProof,
955
+ executionData: BlockProverExecutionData,
956
+ verificationKeyAttestation: RuntimeVerificationKeyAttestation
957
+ ): Promise<BlockProverPublicOutput> {
958
+ return this.zkProgrammable.proveTransaction(
959
+ publicInput,
960
+ stateProof,
961
+ appProof,
962
+ executionData,
963
+ verificationKeyAttestation
964
+ );
965
+ }
966
+
967
+ public proveBlock(
968
+ publicInput: BlockProverPublicInput,
969
+ networkState: NetworkState,
970
+ blockWitness: BlockHashMerkleTreeWitness,
971
+ // stateTransitionProof: StateTransitionProof,
972
+ transactionProof: BlockProverProof
973
+ ): Promise<BlockProverPublicOutput> {
974
+ return this.zkProgrammable.proveBlock(
975
+ publicInput,
976
+ networkState,
977
+ blockWitness,
978
+ // stateTransitionProof,
979
+ transactionProof
980
+ );
981
+ }
982
+
983
+ public merge(
984
+ publicInput: BlockProverPublicInput,
985
+ proof1: BlockProverProof,
986
+ proof2: BlockProverProof
987
+ ): Promise<BlockProverPublicOutput> {
988
+ return this.zkProgrammable.merge(publicInput, proof1, proof2);
989
+ }
990
+ }