-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·192 lines (176 loc) · 4.56 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/asdine/storm"
"github.com/baabeetaa/rpc"
"github.com/baabeetaa/rpc/transports/websocket"
"github.com/baabeetaa/rpc/types"
"github.com/cstockton/go-conv"
)
func main() {
if err := run(); err != nil {
log.Fatalln("Error:", err)
}
}
func run() (err error) {
// Process flags.
flagAddress := flag.String("rpc_endpoint", "ws://127.0.0.1:8090", "steemd RPC endpoint address")
flagReconnect := flag.Bool("reconnect", false, "enable auto-reconnect mode")
flag.Parse()
var (
url = *flagAddress
reconnect = *flagReconnect
)
// Start catching signals.
var interrupted bool
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
// Drop the error in case it is a request being interrupted.
defer func() {
if err == websocket.ErrClosing && interrupted {
err = nil
}
}()
// Start the connection monitor.
monitorChan := make(chan interface{}, 1)
if reconnect {
go func() {
for {
event, ok := <-monitorChan
if ok {
log.Println(event)
}
}
}()
}
// Instantiate the WebSocket transport.
log.Printf("---> Dial(\"%v\")\n", url)
t, err := websocket.NewTransport(url,
websocket.SetAutoReconnectEnabled(reconnect),
websocket.SetAutoReconnectMaxDelay(30*time.Second),
websocket.SetMonitor(monitorChan))
if err != nil {
return err
}
// Use the transport to get an RPC client.
client, err := rpc.NewClient(t)
if err != nil {
return err
}
defer func() {
if !interrupted {
client.Close()
}
}()
// Start processing signals.
go func() {
<-signalCh
fmt.Println()
log.Println("Signal received, exiting...")
signal.Stop(signalCh)
interrupted = true
client.Close()
}()
// Get config.
log.Println("---> GetConfig()")
config, err := client.Database.GetConfig()
if err != nil {
return err
}
//Open Storm
db, err := storm.Open("my.db", storm.Batch())
defer db.Close()
//start at block 1
StartBlock := uint32(1)
// Keep processing incoming blocks forever.
log.Printf("---> Entering the block processing loop (last block = %v)\n", StartBlock)
for {
// Get current properties.
props, err := client.Database.GetDynamicGlobalProperties()
if err != nil {
return err
}
lastBlock := props.LastIrreversibleBlockNum
// Process new blocks.
for StartBlock < lastBlock {
block, err := client.Database.GetBlock(StartBlock)
if err != nil {
return err
}
fmt.Println(conv.Uint(StartBlock))
// Process the transactions.
for _, tx := range block.Transactions {
for _, operation := range tx.Operations {
switch op := operation.Data().(type) {
//process votes
case *types.VoteOperation:
fmt.Printf("@%v voted for @%v/%v\n", op.Voter, op.Author, op.Permlink)
vote := op
err := db.Save(&vote)
if err != nil {
return err
}
//process account creations
case *types.AccountCreateOperation:
accountCreate := op
fmt.Printf("@%v created @v/%v/%v/%v/%v/%v/%v/%v\n", accountCreate.Creator, accountCreate.Active, accountCreate.Fee, accountCreate.JsonMetadata, accountCreate.MemoKey, accountCreate.NewAccountName, accountCreate.Owner, accountCreate.Posting)
err := db.Save(&accountCreate)
if err != nil {
return err
}
//process witness votes
case *types.AccountWitnessVoteOperation:
witnessVote := op
fmt.Printf(witnessVote.Account, witnessVote.Approve, witnessVote.Witness)
err := db.Save(&witnessVote)
if err != nil {
return err
}
//process account updates
case *types.AccountUpdateOperation:
accountUpdate := op
err := db.Save(&accountUpdate)
if err != nil {
return err
}
//process posts and comments
case *types.CommentOperation:
commentOperation := op
err := db.Save(&commentOperation)
if err != nil {
return err
}
//process follows
case *types.FollowOperation:
followOperation := op
err := db.Save(&followOperation)
if err != nil {
return err
}
case *types.CommentOptionsOperation:
commentOptions := op
err := db.Save(&commentOptions)
if err != nil {
return err
}
case *types.WithdrawVestingOperation:
withdrawVesting := op
err := db.Save(&withdrawVesting)
if err != nil {
return err
}
}
}
}
StartBlock++
}
// Sleep for STEEMIT_BLOCK_INTERVAL seconds before the next iteration.
time.Sleep(time.Duration(config.SteemitBlockInterval) * time.Second)
}
}