-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
110 lines (94 loc) · 2.65 KB
/
main.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
// Copyright (c) 2018 CommerceBlock Team
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// Package main implements attestation and request services.
package main
import (
"context"
"flag"
"os"
"os/signal"
"strings"
"sync"
"mainstay/attestation"
"mainstay/config"
"mainstay/db"
"mainstay/log"
"mainstay/test"
)
var (
tx0 string
chaincode string
addrTopup string
isRegtest bool
mainConfig *config.Config
)
func parseFlags() {
flag.BoolVar(&isRegtest, "regtest", false, "Use regtest wallet configuration instead of user wallet")
flag.StringVar(&tx0, "tx", "", "Tx id for genesis attestation transaction")
flag.StringVar(&chaincode, "chaincode", "", "Chaincode for sig pubkey")
flag.StringVar(&addrTopup, "addrTopup", "", "Address for topup transaction")
flag.Parse()
}
func init() {
parseFlags()
if isRegtest {
test := test.NewTest(true, true)
mainConfig = test.Config
log.Infof("Running regtest mode with -tx=%s\n", mainConfig.InitTx())
} else {
var mainConfigErr error
mainConfig, mainConfigErr = config.NewConfig()
if mainConfigErr != nil {
log.Error(mainConfigErr)
}
// if either tx or script not set throw error
if tx0 == "" || chaincode == "" {
if mainConfig.InitTx() == "" || mainConfig.InitChaincode() == "" {
flag.PrintDefaults()
log.Error(`Need to provide all -tx, -script and -chaincode arguments.
To use test configuration set the -regtest flag.`)
}
} else {
mainConfig.SetInitTx(tx0)
chaincodeStr := strings.TrimSpace(chaincode) // trim whitespace
mainConfig.SetInitChaincode(chaincodeStr)
}
if addrTopup != "" {
mainConfig.SetTopupAddress(addrTopup)
}
mainConfig.SetRegtest(isRegtest)
}
}
func main() {
defer mainConfig.MainClient().Shutdown()
wg := &sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.Background())
dbInterface := db.NewDbMongo(ctx, mainConfig.DbConfig())
server := attestation.NewAttestServer(dbInterface)
signer := attestation.NewAttestSignerHttp(mainConfig.SignerConfig())
attestService := attestation.NewAttestService(ctx, wg, server, signer, mainConfig)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
wg.Add(1)
go func() {
defer cancel()
defer wg.Done()
select {
case sig := <-c:
log.Warnf("Got %s signal. Aborting...\n", sig)
case <-ctx.Done():
signal.Stop(c)
}
}()
wg.Add(1)
go attestService.Run()
// In regtest demo mode do block generation work
// Also auto commitment to ClientCommitment to
// allow easier testing without db intervention
if isRegtest {
wg.Add(1)
go test.DoRegtestWork(dbInterface, mainConfig, wg, ctx)
}
wg.Wait()
}