/
registry.go
294 lines (252 loc) · 7.56 KB
/
registry.go
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
package registry
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"time"
api "github.com/Bit-Nation/panthalassa/api"
dapp "github.com/Bit-Nation/panthalassa/dapp"
module "github.com/Bit-Nation/panthalassa/dapp/module"
ethAddrMod "github.com/Bit-Nation/panthalassa/dapp/module/ethAddress"
loggerMod "github.com/Bit-Nation/panthalassa/dapp/module/logger"
messageModule "github.com/Bit-Nation/panthalassa/dapp/module/message"
modalMod "github.com/Bit-Nation/panthalassa/dapp/module/modal"
randBytes "github.com/Bit-Nation/panthalassa/dapp/module/randBytes"
renderDApp "github.com/Bit-Nation/panthalassa/dapp/module/renderer/dapp"
renderMsg "github.com/Bit-Nation/panthalassa/dapp/module/renderer/message"
sendEthTxMod "github.com/Bit-Nation/panthalassa/dapp/module/sendEthTx"
uuidv4Mod "github.com/Bit-Nation/panthalassa/dapp/module/uuidv4"
db "github.com/Bit-Nation/panthalassa/db"
ethws "github.com/Bit-Nation/panthalassa/ethws"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
storm "github.com/asdine/storm"
log "github.com/ipfs/go-log"
host "github.com/libp2p/go-libp2p-host"
net "github.com/libp2p/go-libp2p-net"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
golog "github.com/op/go-logging"
ed25519 "golang.org/x/crypto/ed25519"
)
var logger = log.Logger("dapp - registry")
type fetchDAppChanStr struct {
signingKey ed25519.PublicKey
respChan chan *dapp.DApp
}
type addDevStreamChanStr struct {
signingKey ed25519.PublicKey
stream net.Stream
}
type fetchDAppStreamStr struct {
signingKey ed25519.PublicKey
respChan chan net.Stream
}
// keep track of all running DApps
type Registry struct {
host host.Host
closeChan chan *dapp.Data
conf Config
ethWS *ethws.EthereumWS
api *api.API
km *keyManager.KeyManager
dAppDB dapp.Storage
msgDB db.ChatStorage
db *storm.DB
addDAppChan chan *dapp.DApp
fetchDAppChan chan fetchDAppChanStr
addDevStreamChan chan addDevStreamChanStr
fetchDevStreamChan chan fetchDAppStreamStr
}
type Config struct {
EthWSEndpoint string
}
// create new dApp registry
func NewDAppRegistry(h host.Host, conf Config, api *api.API, km *keyManager.KeyManager, dAppDB dapp.Storage, msgDB db.ChatStorage) (*Registry, error) {
r := &Registry{
host: h,
closeChan: make(chan *dapp.Data),
conf: conf,
api: api,
km: km,
dAppDB: dAppDB,
msgDB: msgDB,
addDAppChan: make(chan *dapp.DApp),
fetchDAppChan: make(chan fetchDAppChanStr),
addDevStreamChan: make(chan addDevStreamChanStr),
fetchDevStreamChan: make(chan fetchDAppStreamStr),
ethWS: ethws.New(ethws.Config{
Retry: time.Second,
WSUrl: conf.EthWSEndpoint,
}),
}
// load all default DApps
dApps := []dapp.RawData{}
if err := json.Unmarshal([]byte(rawDApps), &dApps); err != nil {
return nil, err
}
for _, dAppData := range dApps {
dApp, err := dapp.ParseJsonToData(dAppData)
if err != nil {
return nil, err
}
if err := dAppDB.SaveDApp(dApp); err != nil {
return nil, err
}
}
// add worker to remove DApps
go func() {
dAppInstances := map[string]*dapp.DApp{}
streams := map[string]net.Stream{}
for {
select {
// remove DApp from state
case cc := <-r.closeChan:
delete(dAppInstances, hex.EncodeToString(cc.UsedSigningKey))
// @todo send signal to client that this app was shut down
// fetch dApp from state
case dAppFetch := <-r.fetchDAppChan:
dApp, exist := dAppInstances[hex.EncodeToString(dAppFetch.signingKey)]
if !exist {
dAppFetch.respChan <- nil
continue
}
dAppFetch.respChan <- dApp
// add dev stream to state
case addStream := <-r.addDevStreamChan:
streams[hex.EncodeToString(addStream.signingKey)] = addStream.stream
// fetch dev stream from state
case fetchDevStream := <-r.fetchDevStreamChan:
stream, exist := streams[hex.EncodeToString(fetchDevStream.signingKey)]
if !exist {
fetchDevStream.respChan <- nil
continue
}
fetchDevStream.respChan <- stream
case dApp := <-r.addDAppChan:
dAppInstances[dApp.ID()] = dApp
}
}
}()
return r, nil
}
// start a DApp
func (r *Registry) StartDApp(dAppSigningKey ed25519.PublicKey, timeOut time.Duration) error {
// fetch DApp
dApp, err := r.dAppDB.Get(dAppSigningKey)
if err != nil {
return err
}
if dApp == nil {
return fmt.Errorf("failed to fetch DApp for signing key: %x", dAppSigningKey)
}
// get logger
var l *golog.Logger
if l, err = golog.GetLogger("app name"); err != nil {
return err
}
vmModules := []module.Module{
uuidv4Mod.New(l),
modalMod.New(l, r.api, dApp.UsedSigningKey),
sendEthTxMod.New(r.api, l),
randBytes.New(l),
ethAddrMod.New(r.km),
renderMsg.New(l),
renderDApp.New(l),
messageModule.New(r.msgDB, dAppSigningKey, l),
}
// if there is a stream for this DApp
// we would like to mutate the logger
// to write to the stream we have for development
// this will write logs to the stream
fetchDAppChan := make(chan net.Stream)
r.fetchDevStreamChan <- fetchDAppStreamStr{
signingKey: dAppSigningKey,
respChan: fetchDAppChan,
}
stream := <-fetchDAppChan
if stream != nil {
// append log module
logger, err := loggerMod.New(stream)
l = logger.Logger
if err != nil {
return err
}
vmModules = append(vmModules, logger)
} else {
if err != nil {
return err
}
l.SetBackend(golog.AddModuleLevel(golog.NewLogBackend(ioutil.Discard, "", 0)))
}
app, err := dapp.New(l, dApp, vmModules, r.closeChan, timeOut, r.db)
if err != nil {
l.Error(err.Error())
return err
}
// add DApp to state
r.addDAppChan <- app
return nil
}
func (r *Registry) fetchDApp(signingKey ed25519.PublicKey) *dapp.DApp {
dAppRespChan := make(chan *dapp.DApp)
// fetch DApp
r.fetchDAppChan <- fetchDAppChanStr{
signingKey: signingKey,
respChan: dAppRespChan,
}
return <-dAppRespChan
}
// open DApp
func (r *Registry) OpenDApp(signingKey ed25519.PublicKey, context string) error {
dApp := r.fetchDApp(signingKey)
if dApp == nil {
return errors.New("it seems like that this app hasn't been started yet")
}
return dApp.OpenDApp(context)
}
func (r *Registry) RenderMessage(signingKey ed25519.PublicKey, payload string) (string, error) {
dApp := r.fetchDApp(signingKey)
if dApp == nil {
return "", errors.New("it seems like that this app hasn't been started yet")
}
return dApp.RenderMessage(payload)
}
// use this to connect to a development server
func (r *Registry) ConnectDevelopmentServer(addr ma.Multiaddr) error {
// address to peer info
pInfo, err := pstore.InfoFromP2pAddr(addr)
if err != nil {
return err
}
// connect to peer
if err := r.host.Connect(context.Background(), *pInfo); err != nil {
return err
}
// create stream to development peer
str, err := r.host.NewStream(context.Background(), pInfo.ID, "/dapp-development/0.0.0")
if err != nil {
return err
}
// handle stream
r.devStreamHandler(str)
return nil
}
// call a function in a DApp
func (r *Registry) CallFunction(signingKey ed25519.PublicKey, funcId uint, args string) error {
dApp := r.fetchDApp(signingKey)
if dApp == nil {
return errors.New("it seems like that this app hasn't been started yet")
}
return dApp.CallFunction(funcId, args)
}
func (r *Registry) ShutDown(signingKey ed25519.PublicKey) error {
dApp := r.fetchDApp(signingKey)
if dApp == nil {
return errors.New("it seems like that this app hasn't been started yet")
}
dApp.Close()
return nil
}