1
1
import { inject } from "tsyringe" ;
2
2
import {
3
3
ArtifactRecord ,
4
+ CompileRegistry ,
4
5
MandatoryProtocolModulesRecord ,
5
6
Protocol ,
6
7
RuntimeVerificationKeyRootService ,
7
8
SettlementSmartContractBase ,
8
9
} from "@proto-kit/protocol" ;
9
- import { log } from "@proto-kit/common" ;
10
+ import { CompileArtifact , log } from "@proto-kit/common" ;
10
11
11
- import { FlowCreator } from "../worker/flow/Flow" ;
12
+ import { Flow , FlowCreator } from "../worker/flow/Flow" ;
12
13
import { WorkerRegistrationFlow } from "../worker/worker/startup/WorkerRegistrationFlow" ;
13
- import { CircuitCompilerTask } from "../protocol/production/tasks/CircuitCompilerTask" ;
14
+ import {
15
+ CircuitCompilerTask ,
16
+ CompilerTaskParams ,
17
+ } from "../protocol/production/tasks/CircuitCompilerTask" ;
14
18
import { VerificationKeyService } from "../protocol/runtime/RuntimeVerificationKeyService" ;
15
19
16
20
import { SequencerModule , sequencerModule } from "./builder/SequencerModule" ;
@@ -23,47 +27,114 @@ export class SequencerStartupModule extends SequencerModule {
23
27
private readonly protocol : Protocol < MandatoryProtocolModulesRecord > ,
24
28
private readonly compileTask : CircuitCompilerTask ,
25
29
private readonly verificationKeyService : VerificationKeyService ,
26
- private readonly registrationFlow : WorkerRegistrationFlow
30
+ private readonly registrationFlow : WorkerRegistrationFlow ,
31
+ private readonly compileRegistry : CompileRegistry
27
32
) {
28
33
super ( ) ;
29
34
}
30
35
31
- public async start ( ) {
32
- const flow = this . flowCreator . createFlow ( "compile-circuits" , { } ) ;
36
+ private async pushCompileTask (
37
+ flow : Flow < { } > ,
38
+ payload : CompilerTaskParams
39
+ ) : Promise < ArtifactRecord > {
40
+ return await flow . withFlow < ArtifactRecord > ( async ( res , rej ) => {
41
+ await flow . pushTask ( this . compileTask , payload , async ( result ) => {
42
+ res ( result ) ;
43
+ } ) ;
44
+ } ) ;
45
+ }
33
46
34
- log . info ( "Compiling Protocol circuits, this can take a few minutes" ) ;
47
+ public async compileRuntime ( flow : Flow < { } > ) {
48
+ const artifacts = await this . pushCompileTask ( flow , {
49
+ existingArtifacts : { } ,
50
+ targets : [ "runtime" ] ,
51
+ runtimeVKRoot : undefined ,
52
+ } ) ;
53
+
54
+ // Init runtime VK tree
55
+ await this . verificationKeyService . initializeVKTree ( artifacts ) ;
56
+
57
+ const root = this . verificationKeyService . getRoot ( ) ;
58
+
59
+ this . protocol . dependencyContainer
60
+ . resolve ( RuntimeVerificationKeyRootService )
61
+ . setRoot ( root ) ;
62
+
63
+ this . compileRegistry . addArtifactsRaw ( artifacts ) ;
64
+
65
+ return root ;
66
+ }
67
+
68
+ private async compileProtocolAndBridge ( flow : Flow < { } > ) {
69
+ // Can happen in parallel
70
+ type ParallelResult = {
71
+ protocol ?: ArtifactRecord ;
72
+ bridge ?: ArtifactRecord ;
73
+ } ;
74
+ const result = await flow . withFlow < ArtifactRecord > ( async ( res , rej ) => {
75
+ const results : ParallelResult = { } ;
76
+
77
+ const resolveIfPossible = ( ) => {
78
+ const { bridge, protocol } = results ;
79
+ if ( bridge !== undefined && protocol !== undefined ) {
80
+ res ( { ...protocol , ...bridge } ) ;
81
+ }
82
+ } ;
35
83
36
- const artifacts = await flow . withFlow < ArtifactRecord > ( async ( res , rej ) => {
37
84
await flow . pushTask (
38
85
this . compileTask ,
39
- { existingArtifacts : { } , targets : [ "runtime" ] } ,
86
+ {
87
+ existingArtifacts : { } ,
88
+ targets : [ "protocol" ] ,
89
+ runtimeVKRoot : undefined ,
90
+ } ,
40
91
async ( result ) => {
41
- res ( result ) ;
92
+ results . protocol = result ;
93
+ resolveIfPossible ( ) ;
94
+ }
95
+ ) ;
96
+
97
+ await flow . pushTask (
98
+ this . compileTask ,
99
+ {
100
+ existingArtifacts : { } ,
101
+ targets : [ "bridge" ] ,
102
+ runtimeVKRoot : undefined ,
103
+ } ,
104
+ async ( result ) => {
105
+ results . bridge = result ;
106
+ resolveIfPossible ( ) ;
42
107
}
43
108
) ;
44
109
} ) ;
110
+ this . compileRegistry . addArtifactsRaw ( result ) ;
111
+ return result ;
112
+ }
45
113
46
- log . info ( "Protocol circuits compiled" ) ;
114
+ public async start ( ) {
115
+ const flow = this . flowCreator . createFlow ( "compile-circuits" , { } ) ;
47
116
48
- // Init runtime VK tree
49
- await this . verificationKeyService . initializeVKTree ( artifacts ) ;
117
+ log . info ( "Compiling Protocol circuits, this can take a few minutes" ) ;
50
118
51
- const root = this . verificationKeyService . getRoot ( ) ;
119
+ const root = await this . compileRuntime ( flow ) ;
52
120
53
- this . protocol . dependencyContainer
54
- . resolve ( RuntimeVerificationKeyRootService )
55
- . setRoot ( root ) ;
121
+ const protocolBridgeArtifacts = await this . compileProtocolAndBridge ( flow ) ;
122
+
123
+ log . info ( "Protocol circuits compiled" ) ;
56
124
57
125
// Init BridgeContract vk for settlement contract
58
- const bridgeVk = artifacts . BridgeContract ;
126
+ const bridgeVk = protocolBridgeArtifacts . BridgeContract ;
59
127
if ( bridgeVk !== undefined ) {
60
128
SettlementSmartContractBase . args . BridgeContractVerificationKey =
61
129
bridgeVk . verificationKey ;
62
130
}
63
131
132
+ // TODO Add vk record to start params
133
+
64
134
await this . registrationFlow . start ( {
65
135
runtimeVerificationKeyRoot : root ,
66
- bridgeContractVerificationKey : bridgeVk . verificationKey ,
136
+ bridgeContractVerificationKey : bridgeVk ?. verificationKey ,
137
+ compiledArtifacts : this . compileRegistry . getAllArtifacts ( ) ,
67
138
} ) ;
68
139
69
140
log . info ( "Protocol circuits compiled successfully, commencing startup" ) ;
0 commit comments