-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
142 lines (117 loc) · 3.07 KB
/
node.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
package kowalad
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/kowala-tech/kcoin/kcoin"
"github.com/kowala-tech/kcoin/kcoin/downloader"
"github.com/kowala-tech/kcoin/kcoinclient"
"github.com/kowala-tech/kcoin/log"
"github.com/kowala-tech/kcoin/node"
"github.com/kowala-tech/kcoin/p2p"
"github.com/kowala-tech/kcoin/p2p/nat"
)
const (
// config defaults
UseLightweightKDF = true
NoUSB = true
)
var (
// errors
ErrActiveNode = errors.New("node is already running")
ErrInactiveNode = errors.New("node is not running")
ErrLightRegistrationFailure = errors.New("failed to register the light protocol")
)
type Node interface {
Start(config *Config) error
Stop() error
Client() (*kcoinclient.Client, error)
}
// lightNode represents the kowala's blockchain gateway
type lightNode struct {
*node.Node
networkID uint64
log log.Logger
}
func NewLightNode() *lightNode {
return &lightNode{}
}
func (ln *lightNode) Start(config *Config) error {
if ln.isActive() {
return ErrActiveNode
}
kcoinNode, err := MakeNode(config)
if err != nil {
return err
}
ln.Node = kcoinNode
ln.networkID = config.NetworkID
return ln.Node.Start()
}
func (ln *lightNode) Stop() error {
if !ln.isActive() {
return ErrInactiveNode
}
return ln.Node.Stop()
}
func MakeNode(config *Config) (*node.Node, error) {
if config == nil {
config = NewConfig()
}
if err := os.MkdirAll(filepath.Join(config.DataDir), os.ModePerm); err != nil {
return nil, err
}
nodeConfig := getNodeConfig(config)
node, err := node.New(nodeConfig)
if err != nil {
return nil, err
}
if config.LightService.Enabled {
if err := registerLightKowalaService(node, config); err != nil {
return nil, err
}
}
return node, nil
}
func registerLightKowalaService(registry *node.Node, config *Config) error {
serviceConfig := &kcoin.DefaultConfig
serviceConfig.Genesis = mapNetworkIDToGenesis[config.NetworkID]
// @TODO (rgeraldes) - replace with light sync in the future
serviceConfig.SyncMode = downloader.FullSync
serviceConfig.NetworkId = config.NetworkID
if err := registry.Register(func(ctx *node.ServiceContext) (node.Service, error) {
return kcoin.New(ctx, serviceConfig)
}); err != nil {
return fmt.Errorf("%v: %v", ErrLightRegistrationFailure, err)
}
return nil
}
func (ln *lightNode) isActive() bool {
if ln.Node == nil || ln.Node.Server() == nil {
return false
}
return true
}
func (ln *lightNode) Client() (*kcoinclient.Client, error) {
rpcClient, err := ln.Node.Attach()
if err != nil {
return nil, err
}
return kcoinclient.NewClient(rpcClient), nil
}
func getNodeConfig(config *Config) *node.Config {
return &node.Config{
// @TODO (rgeraldes) uint64 > int
DataDir: filepath.Join(config.DataDir, strconv.Itoa(int(config.NetworkID))),
UseLightweightKDF: UseLightweightKDF,
NoUSB: NoUSB,
P2P: p2p.Config{
NAT: nat.Any(),
MaxPeers: config.Maxpeers,
MaxPendingPeers: config.MaxPendingPeers,
BootstrapNodes: getBootstrapNodesfromNetworkID(config.NetworkID),
},
}
}