This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
vent.go
202 lines (169 loc) · 6.33 KB
/
vent.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
package commands
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/hyperledger/burrow/config/source"
"github.com/hyperledger/burrow/execution/evm/abi"
"github.com/hyperledger/burrow/vent/config"
"github.com/hyperledger/burrow/vent/logger"
"github.com/hyperledger/burrow/vent/service"
"github.com/hyperledger/burrow/vent/sqldb"
"github.com/hyperledger/burrow/vent/sqlsol"
"github.com/hyperledger/burrow/vent/types"
cli "github.com/jawher/mow.cli"
)
// Vent consumes EVM events and commits to a DB
func Vent(output Output) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Command("start", "Start the Vent consumer service",
func(cmd *cli.Cmd) {
cfg := config.DefaultVentConfig()
dbOpts := sqlDBOpts(cmd, cfg)
grpcAddrOpt := cmd.StringOpt("grpc-addr", cfg.GRPCAddr, "Address to connect to the Hyperledger Burrow gRPC server")
httpAddrOpt := cmd.StringOpt("http-addr", cfg.HTTPAddr, "Address to bind the HTTP server")
logLevelOpt := cmd.StringOpt("log-level", cfg.LogLevel, "Logging level (error, warn, info, debug)")
abiFileOpt := cmd.StringsOpt("abi", cfg.AbiFileOrDirs, "EVM Contract ABI file or folder")
specFileOrDirOpt := cmd.StringsOpt("spec", cfg.SpecFileOrDirs, "SQLSol specification file or folder")
dbBlockOpt := cmd.BoolOpt("blocks", false, "Create block tables and persist related data")
dbTxOpt := cmd.BoolOpt("txs", false, "Create tx tables and persist related data")
announceEveryOpt := cmd.StringOpt("announce-every", "5s", "Announce vent status every period as a Go duration, e.g. 1ms, 3s, 1h")
cmd.Before = func() {
// Rather annoying boilerplate here... but there is no way to pass mow.cli a pointer for it to fill you value
cfg.DBAdapter = *dbOpts.adapter
cfg.DBURL = *dbOpts.url
cfg.DBSchema = *dbOpts.schema
cfg.GRPCAddr = *grpcAddrOpt
cfg.HTTPAddr = *httpAddrOpt
cfg.LogLevel = *logLevelOpt
cfg.AbiFileOrDirs = *abiFileOpt
cfg.SpecFileOrDirs = *specFileOrDirOpt
if *dbBlockOpt {
cfg.SpecOpt |= sqlsol.Block
}
if *dbTxOpt {
cfg.SpecOpt |= sqlsol.Tx
}
if *announceEveryOpt != "" {
var err error
cfg.AnnounceEvery, err = time.ParseDuration(*announceEveryOpt)
if err != nil {
output.Fatalf("could not parse announce-every duration %s: %v", *announceEveryOpt, err)
}
}
}
cmd.Spec = "--spec=<spec file or dir> --abi=<abi file or dir> [--db-adapter] [--db-url] [--db-schema] " +
"[--blocks] [--txs] [--grpc-addr] [--http-addr] [--log-level] [--announce-every=<duration>]"
cmd.Action = func() {
log := logger.NewLogger(cfg.LogLevel)
consumer := service.NewConsumer(cfg, log, make(chan types.EventData))
server := service.NewServer(cfg, log, consumer)
projection, err := sqlsol.SpecLoader(cfg.SpecFileOrDirs, cfg.SpecOpt)
if err != nil {
output.Fatalf("Spec loader error: %v", err)
}
abiSpec, err := abi.LoadPath(cfg.AbiFileOrDirs...)
if err != nil {
output.Fatalf("ABI loader error: %v", err)
}
var wg sync.WaitGroup
// setup channel for termination signals
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGTERM)
signal.Notify(ch, syscall.SIGINT)
// start the events consumer
wg.Add(1)
go func() {
if err := consumer.Run(projection, abiSpec, true); err != nil {
output.Fatalf("Consumer execution error: %v", err)
}
wg.Done()
}()
// start the http server
wg.Add(1)
go func() {
server.Run()
wg.Done()
}()
// wait for a termination signal from the OS and
// gracefully shutdown the events consumer and the http server
go func() {
<-ch
consumer.Shutdown()
server.Shutdown()
}()
// wait until the events consumer and the http server are done
wg.Wait()
}
})
cmd.Command("schema", "Print JSONSchema for spec file format to validate table specs",
func(cmd *cli.Cmd) {
cmd.Action = func() {
output.Printf(source.JSONString(types.EventSpecSchema()))
}
})
cmd.Command("restore", "Restore the mapped tables from the _vent_log table",
func(cmd *cli.Cmd) {
const timeLayout = "2006-01-02 15:04:05"
dbOpts := sqlDBOpts(cmd, config.DefaultVentConfig())
timeOpt := cmd.StringOpt("t time", "", fmt.Sprintf("restore time up to which all "+
"log entries will be applied to restore DB, in the format '%s'- restores all log entries if omitted",
timeLayout))
prefixOpt := cmd.StringOpt("p prefix", "", "")
cmd.Spec = "[--db-adapter] [--db-url] [--db-schema] [--time=<date/time to up to which to restore>] " +
"[--prefix=<destination table prefix>]"
var restoreTime time.Time
cmd.Before = func() {
if *timeOpt != "" {
var err error
restoreTime, err = time.Parse(timeLayout, *timeOpt)
if err != nil {
output.Fatalf("Could not parse restore time, should be in the format '%s': %v",
timeLayout, err)
}
}
}
cmd.Action = func() {
db, err := sqldb.NewSQLDB(types.SQLConnection{
DBAdapter: *dbOpts.adapter,
DBURL: *dbOpts.url,
DBSchema: *dbOpts.schema,
Log: logger.NewLogger("debug"),
})
if err != nil {
output.Fatalf("Could not connect to SQL DB: %v", err)
}
if restoreTime.IsZero() {
output.Logf("Restoring DB to state from log")
} else {
output.Logf("Restoring DB to state from log as of %v", restoreTime)
}
if *prefixOpt == "" {
output.Logf("Restoring DB in-place by overwriting any existing tables")
} else {
output.Logf("Restoring DB to destination tables with prefix '%s'", *prefixOpt)
}
err = db.RestoreDB(restoreTime, *prefixOpt)
if err != nil {
output.Fatalf("Error restoring DB: %v", err)
}
output.Logf("Successfully restored DB")
}
})
}
}
type dbOpts struct {
adapter *string
url *string
schema *string
}
func sqlDBOpts(cmd *cli.Cmd, cfg *config.VentConfig) dbOpts {
return dbOpts{
adapter: cmd.StringOpt("db-adapter", cfg.DBAdapter, "Database adapter, 'postgres' or 'sqlite' (if built with the sqlite tag) are supported"),
url: cmd.StringOpt("db-url", cfg.DBURL, "PostgreSQL database URL or SQLite db file path"),
schema: cmd.StringOpt("db-schema", cfg.DBSchema, "PostgreSQL database schema (empty for SQLite)"),
}
}