-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
159 lines (135 loc) · 3.34 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
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
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"strconv"
"time"
// "github.com/terra-money/mantle/indexers/account_txs"
"github.com/terra-money/mantle/indexers/blocks"
"github.com/terra-money/mantle/indexers/cw20"
"github.com/terra-money/mantle/indexers/tx_infos"
"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
_ "net/http/pprof"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/terra-money/mantle-sdk/app"
"github.com/terra-money/mantle-sdk/db/leveldb"
)
func main() {
// env variables
var dbDir = os.Getenv("DB_DIR")
var genesisPath = invariant(os.Getenv("GENESIS_PATH"), "genesis is not supplied")
var endpoint = invariant(os.Getenv("ENDPOINT"), "tendermint endpoint is not supplied")
var sentryDsn = os.Getenv("SENTRY_DSN")
var debugPort = func() uint64 {
debugPortString := os.Getenv("DEBUG_PORT")
debugPort, err := strconv.Atoi(debugPortString)
if err != nil {
return 0
}
return uint64(debugPort)
}()
var syncUntil = func() uint64 {
syncUntilString := os.Getenv("SYNC_UNTIL")
syncUntil, err := strconv.Atoi(syncUntilString)
if err != nil {
return 0
}
return uint64(syncUntil)
}()
var graphqlPort = func() int {
graphqlPortString := os.Getenv("PORT")
graphqlPort, err := strconv.Atoi(graphqlPortString)
if err != nil {
return 1337
}
return graphqlPort
}()
var lcdPort = func() int {
lcdPortSetting := os.Getenv("LCD_PORT")
lcdPort, err := strconv.Atoi(lcdPortSetting)
if err != nil {
return 1317
}
return lcdPort
}()
// init sentry
if sentryDsn != "" {
initSentry(sentryDsn)
defer sentry.Flush(time.Second * 2)
}
// init mantle
log.Printf(
"[mantle] initializing, dbDir=%s, genesisPath=%s, endpoint=%s, syncUntil=%d",
dbDir,
genesisPath,
endpoint,
syncUntil,
)
// Wasm keeper will read home variable from viper and create $home/data/wasm directory
viper.Set("home", dbDir)
db := leveldb.NewLevelDB(dbDir)
defer func() {
if closeErr := db.Close(); closeErr != nil {
fmt.Println(closeErr)
}
}()
genesis, genesisErr := tmtypes.GenesisDocFromFile(genesisPath)
if genesisErr != nil {
panic(genesisErr)
}
// check shasum
//
// file is definitely available once we come to this line,
// skip error check
jsonBlob, _ := ioutil.ReadFile(genesisPath)
shasum := sha1.New()
shasum.Write(jsonBlob)
sum := hex.EncodeToString(shasum.Sum(nil))
log.Printf("genesis shasum(sha1)=%v", sum)
mantle := app.NewMantle(
db,
genesis,
// account_txs.RegisterAccountTxs,
tx_infos.RegisterTxInfos,
blocks.RegisterBlocks,
cw20.RegisterCW20Transfers,
)
if debugPort != 0 {
go func() {
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%d", debugPort), nil))
}()
}
mantle.Server(graphqlPort)
mantle.LCDServer(lcdPort)
mantle.Sync(app.SyncConfiguration{
TendermintEndpoint: endpoint,
SyncUntil: syncUntil,
Reconnect: true,
OnWSError: func(err error) {
sentry.CaptureException(err)
sentry.Flush(time.Second)
},
})
}
func initSentry(sentryDsn string) {
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
Debug: true,
})
if err != nil {
log.Fatalf("tried to initialize sentry, but got error: %s", err)
}
}
func invariant(value string, errorMsg string) string {
if reflect.ValueOf(value).IsZero() {
panic(errorMsg)
}
return value
}