-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbridge.go
More file actions
311 lines (226 loc) · 11.2 KB
/
Copy pathbridge.go
File metadata and controls
311 lines (226 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Copyright © 2023 Hugobyte AI Labs <hello@hugobyte.com>
*/
package bridge
import (
"fmt"
"strconv"
"strings"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"
"github.com/hugobyte/dive/commands/chain/types"
"github.com/hugobyte/dive/common"
"github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings"
"github.com/spf13/cobra"
)
const bridgeMainFunction = "run_btp_setup"
const runbridgeicon2icon = "start_btp_for_already_running_icon_nodes"
const runbridgeicon2ethhardhat = "start_btp_icon_to_eth_for_already_running_nodes"
var (
chainA string
chainB string
serviceA string
serviceB string
)
var runChain = map[string]func(diveContext *common.DiveContext) *common.DiveserviceResponse{
"icon": func(diveContext *common.DiveContext) *common.DiveserviceResponse {
nodeResponse := types.RunIconNode(diveContext)
params := types.GetDecentralizeParms(nodeResponse.ServiceName, nodeResponse.PrivateEndpoint, nodeResponse.KeystorePath, nodeResponse.KeyPassword, nodeResponse.NetworkId)
diveContext.SetSpinnerMessage("Starting Decentralisation")
types.Decentralisation(diveContext, params)
return nodeResponse
},
"eth": func(diveContext *common.DiveContext) *common.DiveserviceResponse {
return types.RunEthNode(diveContext)
},
"hardhat": func(diveContext *common.DiveContext) *common.DiveserviceResponse {
return types.RunHardhatNode(diveContext)
},
}
type Chains struct {
chainA string
chainB string
chainAServiceName string
chainBServiceName string
bridge string
}
func initChains(chainA, chainB, serviceA, serviceB string, bridge bool) *Chains {
return &Chains{
chainA: strings.ToLower(chainA),
chainB: strings.ToLower(chainB),
chainAServiceName: serviceA,
chainBServiceName: serviceB,
bridge: strconv.FormatBool(bridge),
}
}
func (c *Chains) areChainsIcon() bool {
return (c.chainA == "icon" && c.chainB == "icon")
}
func NewBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
var bridgeCmd = &cobra.Command{
Use: "bridge",
Short: "Command for cross chain communication between two different chains",
Long: `To connect two different chains using any of the supported cross chain communication protocols.
This will create an relay to connect two different chains and pass any messages between them.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
bridgeCmd.AddCommand(btpBridgeCmd(diveContext))
return bridgeCmd
}
func btpBridgeCmd(diveContext *common.DiveContext) *cobra.Command {
var btpbridgeCmd = &cobra.Command{
Use: "btp",
Short: "Starts BTP Bridge between ChainA and ChainB",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
common.ValidateCmdArgs(args, cmd.UsageString())
diveContext.InitKurtosisContext()
enclaveCtx, err := diveContext.GetEnclaveContext()
if err != nil {
diveContext.Error(err.Error())
}
bridge, _ := cmd.Flags().GetBool("bmvbridge") // To Specify Which Type of BMV to be used in btp setup(if true BMV bridge is used else BMV-BTP Block is used)
chains := initChains(chainA, chainB, serviceA, serviceB, bridge)
diveContext.StartSpinner(fmt.Sprintf(" Starting BTP Bridge for %s,%s", chains.chainA, chains.chainB))
if chains.areChainsIcon() {
if chains.chainAServiceName != "" && chains.chainBServiceName != "" {
srcChainServiceResponse, dstChainServiceResponse, err := chains.getServicesResponse()
if err != nil {
diveContext.FatalError("Failed To read ServiceFile", err.Error())
}
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2icon, chains.chainA, chains.chainB, chains.chainAServiceName, chains.chainBServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
} else {
params := chains.getParams()
runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}
} else {
if chains.chainAServiceName != "" && chains.chainBServiceName != "" {
srcChainServiceResponse, dstChainServiceResponse, err := chains.getServicesResponse()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
if chains.chainB == "icon" {
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainB, chains.chainA, chains.chainBServiceName, chains.chainAServiceName, bridge, dstChainServiceResponse, srcChainServiceResponse)
} else {
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainA, chains.chainB, chains.chainAServiceName, chains.chainBServiceName, bridge, srcChainServiceResponse, dstChainServiceResponse)
}
} else if (chains.chainAServiceName == "" && chains.chainBServiceName != "") || (chains.chainAServiceName != "" && chains.chainBServiceName == "") {
var chainAServiceResponse string
var chainBServiceResponse string
var chainAServiceName string
var chainBServiceName string
serviceConfig, err := common.ReadServiceJsonFile()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
if chains.chainAServiceName == "" {
chainBserviceresponse, OK := serviceConfig[chains.chainBServiceName]
if !OK {
diveContext.FatalError("failed to get service data", fmt.Sprint("service name not found:", chains.chainBServiceName))
}
chainBServiceName = chainBserviceresponse.ServiceName
chainBServiceResponse, err = chainBserviceresponse.EncodeToString()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
response := runChain[chains.chainA](diveContext)
chainAServiceName = response.ServiceName
chainAServiceResponse, err = response.EncodeToString()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
} else if chains.chainBServiceName == "" {
chainAserviceresponse, OK := serviceConfig[chains.chainAServiceName]
if !OK {
diveContext.FatalError("failed to get service data", fmt.Sprint("service name not found:", chains.chainAServiceName))
}
chainAServiceName = chainAserviceresponse.ServiceName
chainAServiceResponse, err = chainAserviceresponse.EncodeToString()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
response := runChain[chains.chainB](diveContext)
chainBServiceName = response.ServiceName
chainBServiceResponse, err = response.EncodeToString()
if err != nil {
diveContext.FatalError("failed to get service data", err.Error())
}
}
if chains.chainB == "icon" {
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainB, chains.chainA, chainBServiceName, chainAServiceName, bridge, chainBServiceResponse, chainAServiceResponse)
} else {
runBtpSetupForAlreadyRunningNodes(diveContext, enclaveCtx, runbridgeicon2ethhardhat, chains.chainA, chains.chainB, chainAServiceName, chainBServiceName, bridge, chainAServiceResponse, chainBServiceResponse)
}
} else {
params := chains.getParams()
runBtpSetupByRunningNodes(diveContext, enclaveCtx, params)
}
}
diveContext.StopSpinner(fmt.Sprintf("BTP Bridge Setup Completed between %s and %s. Please find service details in current working directory(dive.json)", chainA, chainB))
},
}
btpbridgeCmd.Flags().StringVar(&chainA, "chainA", "", "Mention Name of Supported Chain")
btpbridgeCmd.Flags().StringVar(&chainB, "chainB", "", "Mention Name of Supported Chain")
btpbridgeCmd.Flags().BoolP("bmvbridge", "b", false, "To Specify Which Type of BMV to be used in btp setup(if true BMV bridge is used else BMV-BTP Block is used)")
btpbridgeCmd.Flags().StringVar(&serviceA, "chainAServiceName", "", "Service Name of Chain A from services.json")
btpbridgeCmd.Flags().StringVar(&serviceB, "chainBServiceName", "", "Service Name of Chain B from services.json")
btpbridgeCmd.MarkFlagRequired("chainA")
btpbridgeCmd.MarkFlagRequired("chainB")
return btpbridgeCmd
}
func runBtpSetupByRunningNodes(diveContext *common.DiveContext, enclaveCtx *enclaves.EnclaveContext, params string) {
diveContext.SetSpinnerMessage(" Executing BTP Starlark Package")
data, _, err := enclaveCtx.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveBridgeScript, bridgeMainFunction, params, common.DiveDryRun, common.DiveDefaultParallelism, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{})
if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())
}
diveContext.CheckInstructionSkipped(skippedInstructions, "Bridge Already Running")
common.WriteToFile(response)
}
func runBtpSetupForAlreadyRunningNodes(diveContext *common.DiveContext, enclaveCtx *enclaves.EnclaveContext, mainFunctionName string, srcChain string, dstChain string, srcChainServiceName string, dstChainServiceName string, bridge bool, srcChainServiceResponse string, dstChainServiceResponse string) {
configData := fmt.Sprintf(`{"links": {"src":"%s","dst":"%s"},"chains" : { "%s" : %s,"%s" : %s},"contracts" : {"%s" : {},"%s" : {}},"bridge" : "%s"}`, srcChain, dstChain, srcChainServiceName, srcChainServiceResponse, dstChainServiceName, dstChainServiceResponse, srcChainServiceName, dstChainServiceName, strconv.FormatBool(bridge))
params := fmt.Sprintf(`{"src_chain":"%s", "dst_chain":"%s", "config_data":%s, "src_service_name":"%s", "dst_service_name":"%s"}`, srcChain, dstChain, configData, srcChainServiceName, dstChainServiceName)
data, _, err := enclaveCtx.RunStarlarkRemotePackage(diveContext.Ctx, common.DiveRemotePackagePath, common.DiveBridgeScript, mainFunctionName, params, common.DiveDryRun, common.DiveDefaultParallelism, []kurtosis_core_rpc_api_bindings.KurtosisFeatureFlag{})
if err != nil {
diveContext.FatalError("Starlark Run Failed", err.Error())
}
response, services, skippedInstructions, err := diveContext.GetSerializedData(data)
if err != nil {
diveContext.StopServices(services)
diveContext.FatalError("Starlark Run Failed", err.Error())
}
diveContext.CheckInstructionSkipped(skippedInstructions, "Bridge Already Running")
common.WriteToFile(response)
}
func (chains *Chains) getParams() string {
return fmt.Sprintf(`{"args":{"links": {"src": "%s", "dst": "%s"},"bridge":"%s"}}`, chains.chainA, chains.chainB, chains.bridge)
}
func (chains *Chains) getServicesResponse() (string, string, error) {
serviceConfig, err := common.ReadServiceJsonFile()
if err != nil {
return "", "", err
}
chainAServiceResponse, OK := serviceConfig[chains.chainAServiceName]
if !OK {
return "", "", fmt.Errorf("service name not found")
}
chainBServiceResponse, OK := serviceConfig[chains.chainBServiceName]
if !OK {
return "", "", fmt.Errorf("service name not found")
}
srcChainServiceResponse, err := chainAServiceResponse.EncodeToString()
if err != nil {
return "", "", err
}
dstChainServiceResponse, err := chainBServiceResponse.EncodeToString()
if err != nil {
return "", "", err
}
return srcChainServiceResponse, dstChainServiceResponse, nil
}