-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathnode.go
More file actions
87 lines (74 loc) · 2.39 KB
/
node.go
File metadata and controls
87 lines (74 loc) · 2.39 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
package cmd
import (
"path/filepath"
"github.com/deso-protocol/backend/config"
"github.com/deso-protocol/backend/routes"
coreCmd "github.com/deso-protocol/core/cmd"
"github.com/deso-protocol/core/lib"
"github.com/dgraph-io/badger/v4"
"github.com/golang/glog"
"github.com/kevinburke/twilio-go"
)
type Node struct {
APIServer *routes.APIServer
GlobalState *badger.DB
Config *config.Config
CoreNode *coreCmd.Node
}
func NewNode(config *config.Config, coreNode *coreCmd.Node) *Node {
result := Node{}
result.Config = config
result.CoreNode = coreNode
return &result
}
func (node *Node) Start() {
var err error
// For the global state, we use a local db unless a remote node is set in
// which case all global state set/fetch calls will proxy to the remote.
if node.Config.GlobalStateRemoteNode == "" {
globalStateDir := filepath.Join(lib.GetBadgerDbPath(node.CoreNode.Config.DataDirectory), "global_state")
globalStateOpts := badger.DefaultOptions(globalStateDir)
globalStateOpts.MemTableSize = 1024 << 20
globalStateOpts.ValueDir = lib.GetBadgerDbPath(globalStateDir)
glog.Infof("GlobalState BadgerDB Dir: %v", globalStateOpts.Dir)
glog.Infof("GlobalState BadgerDB ValueDir: %v", globalStateOpts.ValueDir)
node.GlobalState, err = badger.Open(globalStateOpts)
if err != nil {
glog.Fatal(err)
}
}
var twilioClient *twilio.Client
if node.Config.TwilioAccountSID != "" {
twilioClient = twilio.NewClient(node.Config.TwilioAccountSID, node.Config.TwilioAuthToken, nil)
}
if node.CoreNode.Config.HyperSync == true && node.Config.RunHotFeedRoutine == true {
if !lib.IsNodeArchival(node.CoreNode.Config.SyncType) {
node.Config.RunHotFeedRoutine = false
glog.Errorf(lib.CLog(lib.Red, "Hot feed is not compatible with non-archival mode. You need "+
"to set --archival-mode=true if you want to run hot feed with hypersync."))
}
}
node.APIServer, err = routes.NewAPIServer(
node.CoreNode.Server,
node.CoreNode.Server.GetMempool(),
node.CoreNode.Server.GetBlockchain(),
node.CoreNode.Server.GetBlockProducer(),
node.CoreNode.TXIndex,
node.CoreNode.Params,
node.Config,
node.CoreNode.Config.MinFeerate,
node.GlobalState,
twilioClient,
node.CoreNode.Config.BlockCypherAPIKey,
)
if err != nil {
glog.Fatal(err)
}
go node.APIServer.Start()
}
func (node *Node) Stop() {
node.APIServer.Stop()
if node.GlobalState != nil {
_ = node.GlobalState.Close()
}
}