-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
159 lines (143 loc) · 4.02 KB
/
main.go
File metadata and controls
159 lines (143 loc) · 4.02 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"proofofaccess/Rewards"
"proofofaccess/api"
"proofofaccess/connection"
"proofofaccess/database"
"proofofaccess/hive"
"proofofaccess/honeycomb"
"proofofaccess/ipfs"
"proofofaccess/localdata"
"proofofaccess/messaging"
"proofofaccess/peers"
"proofofaccess/validators"
"sync"
"syscall"
shell "github.com/ipfs/go-ipfs-api"
"github.com/sirupsen/logrus"
)
// Version information
var (
Version = "0.1.0"
BuildTime = "unknown"
CommitID = "unknown"
)
var (
version = flag.Bool("version", false, "Print version information")
nodeType = flag.Int("node", 1, "Node type 1 = validation 2 = access")
storageLimit = flag.Int("storageLimit", 1, "storageLimit in GB")
username = flag.String("username", "", "Username")
ipfsPort = flag.String("IPFS_PORT", "5001", "IPFS port number")
wsPort = flag.String("WS_PORT", "8000", "Websocket port number")
useWS = flag.Bool("useWS", false, "Use websocket")
getVids = flag.Bool("getVids", false, "Fetch 3Speak videos for rewarding")
runProofs = flag.Bool("runProofs", false, "Run proofs")
pinVideos = flag.Bool("pinVideos", false, "Pin videos")
getHiveRewards = flag.Bool("getHive", false, "Get Hive rewards")
useHoneycomb = flag.Bool("honeycomb", false, "Use honeycomb")
honeycombApi = flag.String("url", "", "Honeycomb API URL")
validatorsApi = flag.String("validators", "http://localhost:3001/services/VAL", "Validators URL")
threeSpeakNode = flag.Bool("threeSpeak", false, "3Speak node")
CID, Hash string
log = logrus.New()
newPins = false
)
var mu sync.Mutex
func main() {
flag.Parse()
// Handle version flag
if *version {
fmt.Printf("ProofOfAccess v%s\n", Version)
fmt.Printf("Build Time: %s\n", BuildTime)
fmt.Printf("Commit: %s\n", CommitID)
os.Exit(0)
}
log.SetLevel(logrus.WarnLevel)
ipfs.Shell = shell.NewShell("localhost:" + *ipfsPort)
ctx, cancel := context.WithCancel(context.Background())
setupCloseHandler(cancel)
initialize(ctx)
<-ctx.Done()
log.Info("Shutting down...")
if err := database.Close(); err != nil {
log.Error("Error closing the database: ", err)
}
}
func initialize(ctx context.Context) {
localdata.SetNodeName(*username)
localdata.NodeType = *nodeType
localdata.WsPort = *wsPort
database.Init(*nodeType)
ipfs.IpfsPeerID()
if *getHiveRewards {
fmt.Println("Getting Hive rewards")
localdata.HiveRewarded = hive.GetHiveSent()
fmt.Println("Done getting Hive rewards")
}
if *getVids {
fmt.Println("Getting 3Speak videos")
Rewards.ThreeSpeak()
fmt.Println("Done getting 3Speak videos")
if *pinVideos {
fmt.Println("Pinning and unpinning videos")
go Rewards.PinVideos(*storageLimit)
}
go ipfs.SaveRefs(localdata.ThreeSpeakVideos)
}
if *useHoneycomb {
var url = ""
if *honeycombApi == "" {
url = "https://spktest.dlux.io/list-contracts"
} else {
url = *honeycombApi
}
fmt.Println("Getting Honeycomb CIDs")
fmt.Println(url)
cids, err := honeycomb.GetCIDsFromAPI(url)
localdata.Lock.Lock()
localdata.HoneycombContractCIDs = cids
localdata.Lock.Unlock()
log.Error(err)
fmt.Println("Got Honeycomb CIDs")
// fmt.Println(cids)
go ipfs.SaveRefs(cids)
}
if *nodeType == 1 {
go messaging.PubsubHandler(ctx)
go connection.CheckSynced(ctx)
go Rewards.Update(ctx)
} else {
go peers.FetchPins()
}
if *nodeType == 2 {
validators.GetValidators(*validatorsApi)
if *useWS {
localdata.UseWS = *useWS
for _, name := range localdata.ValidatorNames {
go connection.StartWsClient(name)
}
} else {
go messaging.PubsubHandler(ctx)
go validators.ConnectToValidators(ctx, nodeType)
}
}
go api.StartAPI(ctx)
if *runProofs {
go Rewards.RunRewardProofs(ctx)
go Rewards.RewardPeers()
}
}
func setupCloseHandler(cancel context.CancelFunc) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-signalChan
log.Infof("Received signal: %v. Shutting down...", sig)
cancel()
}()
}