forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
758 lines (606 loc) · 18.1 KB
/
main.go
File metadata and controls
758 lines (606 loc) · 18.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
package main
import (
"bytes"
"encoding/xml"
"errors"
"flag"
"fmt"
"github.com/FactomProject/FactomCode/notaryapi"
"github.com/conformal/btcrpcclient"
"github.com/conformal/btcutil"
"github.com/FactomProject/dynrsrc"
"github.com/FactomProject/gobundle"
"github.com/FactomProject/gocoding"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"sync"
"os"
"time"
"log"
"encoding/binary"
"encoding/csv"
"github.com/FactomProject/FactomCode/database"
"github.com/FactomProject/FactomCode/database/ldb"
"code.google.com/p/gcfg"
)
var (
wclient *btcrpcclient.Client //rpc client for btcwallet rpc server
dclient *btcrpcclient.Client //rpc client for btcd rpc server
currentAddr btcutil.Address
tickers [2]*time.Ticker
db database.Db // database
chainIDMap map[string]*notaryapi.EChain // ChainIDMap with chainID string([32]byte) as key
//chainNameMap map[string]*notaryapi.Chain // ChainNameMap with chain name string as key
dchain *notaryapi.DChain //Directory Block Chain
cchain *notaryapi.CChain //Entry Credit Chain
eCreditMap map[string]int // eCreditMap with public key string([32]byte) as key
prePaidEntryMap map[string]byte // Paid but unrevealed entries string(Pubkey + Etnry Hash + Nounce) as key
// dbBatches []*notaryapi.FBBatch
dbBatches *DBBatches
dbBatch *notaryapi.DBBatch
)
var (
logLevel = "DEBUG"
portNumber int = 8083
sendToBTCinSeconds = 600
directoryBlockInSeconds = 60
applicationName = "factom/restapi"
dataStorePath = "/tmp/store/seed/"
ldbpath = "/tmp/ldb9"
//BTC:
// addrStr = "movaFTARmsaTMk3j71MpX8HtMURpsKhdra"
walletPassphrase = "lindasilva"
certHomePath = "btcwallet"
rpcClientHost = "localhost:18332" //btcwallet rpcserver address
rpcClientEndpoint = "ws"
rpcClientUser = "testuser"
rpcClientPass = "notarychain"
btcTransFee float64 = 0.0001
certHomePathBtcd = "btcd"
rpcBtcdHost = "localhost:18334" //btcd rpcserver address
)
type DBBatches struct {
batches []*notaryapi.DBBatch
batchMutex sync.Mutex
}
func loadConfigurations(){
cfg := struct {
App struct{
PortNumber int
ApplicationName string
LdbPath string
DataStorePath string
DirectoryBlockInSeconds int
}
Btc struct{
BTCPubAddr string
SendToBTCinSeconds int
WalletPassphrase string
CertHomePath string
RpcClientHost string
RpcClientEndpoint string
RpcClientUser string
RpcClientPass string
BtcTransFee float64
}
Log struct{
LogLevel string
}
}{}
wd, err := os.Getwd()
if err != nil{
log.Println(err)
}
err = gcfg.ReadFileInto(&cfg, wd+"/restapi.conf")
if err != nil{
log.Println(err)
log.Println("Server starting with default settings...")
} else {
//setting the variables by the valued form the config file
logLevel = cfg.Log.LogLevel
applicationName = cfg.App.ApplicationName
portNumber = cfg.App.PortNumber
dataStorePath = cfg.App.DataStorePath
ldbpath = cfg.App.LdbPath
directoryBlockInSeconds = cfg.App.DirectoryBlockInSeconds
// addrStr = cfg.Btc.BTCPubAddr
sendToBTCinSeconds = cfg.Btc.SendToBTCinSeconds
walletPassphrase = cfg.Btc.WalletPassphrase
certHomePath = cfg.Btc.CertHomePath
rpcClientHost = cfg.Btc.RpcClientHost
rpcClientEndpoint = cfg.Btc.RpcClientEndpoint
rpcClientUser = cfg.Btc.RpcClientUser
rpcClientPass = cfg.Btc.RpcClientPass
btcTransFee = cfg.Btc.BtcTransFee
}
}
func watchError(err error) {
panic(err)
}
func readError(err error) {
fmt.Println("error: ", err)
}
func initWithBinary(chain *notaryapi.EChain) {
matches, err := filepath.Glob(dataStorePath + chain.ChainID.String() + "/store.*.block") // need to get it from a property file??
if err != nil {
panic(err)
}
chain.Blocks = make([]*notaryapi.EBlock, len(matches))
num := 0
for _, match := range matches {
data, err := ioutil.ReadFile(match)
if err != nil {
panic(err)
}
block := new(notaryapi.EBlock)
err = block.UnmarshalBinary(data)
if err != nil {
panic(err)
}
block.Chain = chain
block.IsSealed = true
chain.Blocks[num] = block
num++
}
//Create an empty block and append to the chain
if len(chain.Blocks) == 0{
chain.NextBlockID = 0
newblock, _ := notaryapi.CreateBlock(chain, nil, 10)
chain.Blocks = append(chain.Blocks, newblock)
} else{
chain.NextBlockID = uint64(len(chain.Blocks))
newblock,_ := notaryapi.CreateBlock(chain, chain.Blocks[len(chain.Blocks)-1], 10)
chain.Blocks = append(chain.Blocks, newblock)
}
//Get the unprocessed entries in db for the past # of mins for the open block
binaryTimestamp := make([]byte, 8)
binary.BigEndian.PutUint64(binaryTimestamp, uint64(0))
if chain.Blocks[chain.NextBlockID].IsSealed == true {
panic ("chain.Blocks[chain.NextBlockID].IsSealed for chain:" + chain.ChainID.String())
}
chain.Blocks[chain.NextBlockID].EBEntries, _ = db.FetchEBEntriesFromQueue(&chain.ChainID.Bytes, &binaryTimestamp)
}
func initDB() {
//init db
var err error
db, err = ldb.OpenLevelDB(ldbpath, false)
if err != nil{
log.Println("err opening db: %v", err)
}
if db == nil{
log.Println("Creating new db ...")
db, err = ldb.OpenLevelDB(ldbpath, true)
if err!=nil{
panic(err)
}
}
log.Println("Database started from: " + ldbpath)
}
func init() {
loadConfigurations()
gobundle.Setup.Application.Name = applicationName
gobundle.Init()
initDB()
initChains()
dynrsrc.Start(watchError, readError)
notaryapi.StartDynamic(gobundle.DataFile("html.gwp"), readError)
for _, chain := range chainIDMap {
initWithBinary(chain)
fmt.Println("Loaded", len(chain.Blocks)-1, "blocks for chain: " + chain.ChainID.String())
for i := 0; i < len(chain.Blocks); i = i + 1 {
if uint64(i) != chain.Blocks[i].Header.BlockID {
panic(errors.New("BlockID does not equal index"))
}
}
}
// init Directory Block Chain
initDChain()
fmt.Println("Loaded", len(dchain.Blocks)-1, "Directory blocks for chain: "+ notaryapi.EncodeBinary(dchain.ChainID))
// init dbBatches, dbBatch
dbBatches = &DBBatches {
batches: make([]*notaryapi.DBBatch, 0, 100),
}
dbBatch := ¬aryapi.DBBatch {
DBlocks: make([]*notaryapi.DBlock, 0, 10),
}
dbBatches.batches = append(dbBatches.batches, dbBatch)
// create EBlocks and FBlock every 60 seconds
tickers[0] = time.NewTicker(time.Second * time.Duration(directoryBlockInSeconds))
// write 10 FBlock in a batch to BTC every 10 minutes
tickers[1] = time.NewTicker(time.Second * time.Duration(sendToBTCinSeconds))
go func() {
for _ = range tickers[0].C {
fmt.Println("in tickers[0]: newEntryBlock & newFactomBlock")
for _, chain := range chainIDMap {
eblock := newEntryBlock(chain)
if eblock != nil{
dchain.AddDBEntry(eblock)
}
save(chain)
}
dbBlock := newDirectoryBlock(dchain)
if dbBlock != nil {
// mark the start block of a DBBatch
fmt.Println("in tickers[0]: len(dbBatch.DBlocks)=", len(dbBatch.DBlocks))
if len(dbBatch.DBlocks) == 0 {
dbBlock.Header.BatchFlag = byte(1)
}
dbBatch.DBlocks = append(dbBatch.DBlocks, dbBlock)
fmt.Println("in tickers[0]: ADDED FBBLOCK: len(dbBatch.DBlocks)=", len(dbBatch.DBlocks))
}
saveDChain(dchain)
}
}()
go func() {
for _ = range tickers[1].C {
fmt.Println("in tickers[1]: new FBBatch. len(dbBatch.DBlocks)=", len(dbBatch.DBlocks))
// skip empty dbBatch.
if len(dbBatch.DBlocks) > 0 {
doneBatch := dbBatch
dbBatch = ¬aryapi.DBBatch {
DBlocks: make([]*notaryapi.DBlock, 0, 10),
}
dbBatches.batchMutex.Lock()
dbBatches.batches = append(dbBatches.batches, doneBatch)
dbBatches.batchMutex.Unlock()
fmt.Printf("in tickers[1]: doneBatch=%#v\n", doneBatch)
// go routine here?
saveDBBatchMerkleRoottoBTC(doneBatch)
}
}
}()
}
func main() {
//addrStr := "muhXX7mXoMZUBvGLCgfjuoY2n2mziYETYC"
//addrStr := "movaFTARmsaTMk3j71MpX8HtMURpsKhdra"
err := initRPCClient()
if err != nil {
log.Fatalf("cannot init rpc client: %s", err)
}
defer shutdown()
if err := initWallet(); err != nil {
log.Fatalf("cannot init wallet: %s", err)
}
//doEntries()
flag.Parse()
defer func() {
tickers[0].Stop()
tickers[1].Stop()
dynrsrc.Stop()
db.Close()
}()
http.HandleFunc("/", serveRESTfulHTTP)
err = http.ListenAndServe(":"+strconv.Itoa(portNumber), nil)
if err != nil {
panic(err)
}
}
func fileNotExists(name string) (bool) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return true
}
return err != nil
}
func save(chain *notaryapi.EChain) {
if len(chain.Blocks)==0{
log.Println("no blocks to save for chain: " + chain.ChainID.String())
return
}
bcp := make([]*notaryapi.EBlock, len(chain.Blocks))
chain.BlockMutex.Lock()
copy(bcp, chain.Blocks)
chain.BlockMutex.Unlock()
for i, block := range bcp {
//the open block is not saved
if block.IsSealed == false {
continue
}
data, err := block.MarshalBinary()
if err != nil {
panic(err)
}
strChainID := chain.ChainID.String()
if fileNotExists (dataStorePath + strChainID){
err:= os.MkdirAll(dataStorePath + strChainID, 0777)
if err==nil{
log.Println("Created directory " + dataStorePath + strChainID)
} else{
log.Println(err)
}
}
err = ioutil.WriteFile(fmt.Sprintf(dataStorePath + strChainID + "/store.%09d.block", i), data, 0777)
if err != nil {
panic(err)
}
}
}
func serveRESTfulHTTP(w http.ResponseWriter, r *http.Request) {
var resource interface{}
var err *notaryapi.Error
var buf bytes.Buffer
path, method, accept, form, err := parse(r)
defer func() {
switch accept {
case "text":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
case "json":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
case "xml":
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
case "html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
if err != nil {
var r *notaryapi.Error
buf.Reset()
r = notaryapi.Marshal(err, accept, &buf, false)
if r != nil {
err = r
}
w.WriteHeader(err.HTTPCode)
}
//buf.WriteTo(w)
if resource != nil {
//Send back entry hash
w.Write(resource.([]byte))
}else{
w.Write([]byte("\n\n"))
}
}()
switch method {
case "GET":
//resource, err = find(path)
case "POST":
if len(path) != 1 {
err = notaryapi.CreateError(notaryapi.ErrorBadMethod, `POST can only be used in the root context: /v1`)
return
}
datatype := form.Get("datatype")
switch datatype {
case "chain":
resource, err = postChain("/"+strings.Join(path, "/"), form)
default:
resource, err = postEntry("/"+strings.Join(path, "/"), form)
}
default:
err = notaryapi.CreateError(notaryapi.ErrorBadMethod, fmt.Sprintf(`The HTTP %s method is not supported`, method))
return
}
if err != nil {
resource = err
}
alt := false
for _, s := range form["byref"] {
b, err := strconv.ParseBool(s)
if err == nil {
alt = b
break
}
}
err = notaryapi.Marshal(resource, accept, &buf, alt)
}
func postEntry(context string, form url.Values) (interface{}, *notaryapi.Error) {
newEntry := new(notaryapi.Entry)
format, data := form.Get("format"), form.Get("entry")
switch format {
case "", "json":
reader := gocoding.ReadString(data)
err := notaryapi.UnmarshalJSON(reader, newEntry)
if err != nil {
return nil, notaryapi.CreateError(notaryapi.ErrorJSONUnmarshal, err.Error())
}
case "xml":
err := xml.Unmarshal([]byte(data), newEntry)
if err != nil {
return nil, notaryapi.CreateError(notaryapi.ErrorXMLUnmarshal, err.Error())
}
case "binary":
binaryEntry,_ := notaryapi.DecodeBinary(&data)
fmt.Println("data:%v", data)
err := newEntry.UnmarshalBinary(binaryEntry)
if err != nil {
return nil, notaryapi.CreateError(notaryapi.ErrorXMLUnmarshal, err.Error())
}
default:
return nil, notaryapi.CreateError(notaryapi.ErrorUnsupportedUnmarshal, fmt.Sprintf(`The format "%s" is not supported`, format))
}
return processNewEntry(newEntry)
}
func processNewEntry(newEntry *notaryapi.Entry) ([]byte, *notaryapi.Error) {
if newEntry == nil {
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, `Entity to be POSTed is nil`)
}
chain := chainIDMap[newEntry.ChainID.String()]
if chain == nil{
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, `This chain is not supported`) //ErrorInternal?
}
// store the new entry in db
entryBinary, _ := newEntry.MarshalBinary()
fmt.Println("entryBinary:%v", notaryapi.EncodeBinary(&entryBinary))
hash, _ := notaryapi.CreateHash(newEntry)
db.InsertEntryAndQueue( hash, &entryBinary, newEntry, &chain.ChainID.Bytes)
chain.BlockMutex.Lock()
err := chain.Blocks[len(chain.Blocks)-1].AddEBEntry(newEntry)
chain.BlockMutex.Unlock()
if err != nil {
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, fmt.Sprintf(`Error while adding Entity to Block: %s`, err.Error()))
}
return hash.Bytes, nil
}
func postChain(context string, form url.Values) (interface{}, *notaryapi.Error) {
newChain := new(notaryapi.EChain)
format, data := form.Get("format"), form.Get("chain")
switch format {
case "binary":
binaryChain,_ := notaryapi.DecodeBinary(&data)
err := newChain.UnmarshalBinary(binaryChain)
newChain.GenerateIDFromName()
if err != nil {
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, err.Error())
}
default:
return nil, notaryapi.CreateError(notaryapi.ErrorUnsupportedUnmarshal, fmt.Sprintf(`The format "%s" is not supported`, format))
}
if newChain == nil {
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, `Chain is nil`)
}
chain := chainIDMap[newChain.ChainID.String()]
if chain != nil{
return nil, notaryapi.CreateError(notaryapi.ErrorInternal, `This chain is already existing`) //ErrorInternal?
}
// Store the new chain in db
db.InsertChain(newChain)
// Chain initialization
initWithBinary(newChain)
fmt.Println("Loaded", len(newChain.Blocks)-1, "blocks for chain: " + newChain.ChainID.String())
// Add the new chain in the chainIDMap
chainIDMap[newChain.ChainID.String()] = newChain
ExportDataFromDbToFile()
return newChain.ChainID.Bytes, nil
}
/*
func createNewChain(chainName string) (chain *notaryapi.Chain){
chain = new (notaryapi.Chain)
chain.Name = chainName
chain.GenerateIDFromName(chainName)
db.InsertChain(chain)
chainIDMap[chain.ChainID.String()] = chain
return chain
}
*/
func saveDChain(chain *notaryapi.DChain) {
if len(chain.Blocks)==0{
//log.Println("no blocks to save for chain: " + string (*chain.ChainID))
return
}
bcp := make([]*notaryapi.DBlock, len(chain.Blocks))
chain.BlockMutex.Lock()
copy(bcp, chain.Blocks)
chain.BlockMutex.Unlock()
for i, block := range bcp {
//the open block is not saved
if block.IsSealed == false {
continue
}
data, err := block.MarshalBinary()
if err != nil {
panic(err)
}
strChainID := notaryapi.EncodeBinary(chain.ChainID)
if fileNotExists (dataStorePath + strChainID){
err:= os.MkdirAll(dataStorePath + strChainID, 0777)
if err==nil{
log.Println("Created directory " + dataStorePath + strChainID)
} else{
log.Println(err)
}
}
err = ioutil.WriteFile(fmt.Sprintf(dataStorePath + strChainID + "/store.%09d.block", i), data, 0777)
if err != nil {
panic(err)
}
}
}
func initDChain() {
dchain = new (notaryapi.DChain)
barray := (make([]byte, 32))
dchain.ChainID = &barray
matches, err := filepath.Glob(dataStorePath + notaryapi.EncodeBinary(dchain.ChainID) + "/store.*.block") // need to get it from a property file??
if err != nil {
panic(err)
}
dchain.Blocks = make([]*notaryapi.DBlock, len(matches))
num := 0
for _, match := range matches {
data, err := ioutil.ReadFile(match)
if err != nil {
panic(err)
}
block := new(notaryapi.DBlock)
err = block.UnmarshalBinary(data)
if err != nil {
panic(err)
}
block.Chain = dchain
block.IsSealed = true
dchain.Blocks[num] = block
num++
}
//Create an empty block and append to the chain
if len(dchain.Blocks) == 0{
dchain.NextBlockID = 0
newblock, _ := notaryapi.CreateFBlock(dchain, nil, 10)
dchain.Blocks = append(dchain.Blocks, newblock)
} else{
dchain.NextBlockID = uint64(len(dchain.Blocks))
newblock,_ := notaryapi.CreateFBlock(dchain, dchain.Blocks[len(dchain.Blocks)-1], 10)
dchain.Blocks = append(dchain.Blocks, newblock)
}
//Get the unprocessed entries in db for the past # of mins for the open block
binaryTimestamp := make([]byte, 8)
binary.BigEndian.PutUint64(binaryTimestamp, uint64(0))
if dchain.Blocks[dchain.NextBlockID].IsSealed == true {
panic ("dchain.Blocks[dchain.NextBlockID].IsSealed for chain:" + notaryapi.EncodeBinary(dchain.ChainID))
}
dchain.Blocks[dchain.NextBlockID].DBEntries, _ = db.FetchDBEntriesFromQueue(&binaryTimestamp)
}
func ExportDbToFile(dbHash *notaryapi.Hash) {
if fileNotExists( dataStorePath+"csv/") {
os.MkdirAll(dataStorePath+"csv/" , 0755)
}
//write the records to a csv file:
file, err := os.Create(dataStorePath+"csv/" + dbHash.String() + ".csv")
if err != nil {panic(err)}
defer file.Close()
writer := csv.NewWriter(file)
ldbMap, err := db.FetchAllDBRecordsByDBHash(dbHash)
if err != nil{
log.Println(err)
return
}
for key, value := range ldbMap{
//csv header: key, value
writer.Write([]string {key, value})
}
writer.Flush()
}
func ExportDataFromDbToFile() {
if fileNotExists( dataStorePath+"csv/") {
os.MkdirAll(dataStorePath+"csv/" , 0755)
}
//write the records to a csv file:
file, err := os.Create(dataStorePath+"csv/" + "supportdata.csv")
if err != nil {panic(err)}
defer file.Close()
writer := csv.NewWriter(file)
ldbMap, err := db.FetchSupportDBRecords()
if err != nil{
log.Println(err)
return
}
for key, value := range ldbMap{
//csv header: key, value
writer.Write([]string {key, value})
}
writer.Flush()
}
func initChains() {
//initChainIDs()
chainIDMap = make(map[string]*notaryapi.EChain)
//chainNameMap = make(map[string]*notaryapi.Chain)
eCreditMap = make(map[string]int)
chains, err := db.FetchAllChainsByName(nil)
if err != nil{
panic (err)
}
for _, chain := range *chains {
var newChain = chain
chainIDMap[newChain.ChainID.String()] = &newChain
//chainIDMap[string(chain.ChainID.Bytes)] = &chain
}
}