forked from aergoio/aergo
/
litentcontainer.go
240 lines (204 loc) · 6.74 KB
/
litentcontainer.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package server
import (
"github.com/aergoio/aergo/internal/network"
"github.com/aergoio/aergo/p2p/p2putil"
"github.com/aergoio/aergo/p2p/transport"
"net"
"strconv"
"sync"
"time"
"github.com/aergoio/aergo-actor/actor"
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/config"
"github.com/aergoio/aergo/message"
"github.com/aergoio/aergo/p2p/p2pcommon"
"github.com/aergoio/aergo/p2p/p2pkey"
"github.com/aergoio/aergo/pkg/component"
"github.com/aergoio/aergo/types"
)
// P2P is actor component for p2p
type LiteContainerService struct {
*component.BaseComponent
dummySetting p2pcommon.LocalSettings
chainID *types.ChainID
meta p2pcommon.PeerMeta
nt p2pcommon.NetworkTransport
mutex sync.Mutex
}
func (lntc *LiteContainerService) LocalSettings() p2pcommon.LocalSettings {
return lntc.dummySetting
}
func (lntc *LiteContainerService) RoleManager() p2pcommon.PeerRoleManager {
panic("implement me")
}
var (
//_ ActorService = (*LiteContainerService)(nil)
)
// NewP2P create a new ActorService for p2p
func NewNTContainer(cfg *config.Config) *LiteContainerService {
lntc := &LiteContainerService{}
lntc.BaseComponent = component.NewBaseComponent(message.P2PSvc, lntc, log.NewLogger("p2p"))
lntc.init(cfg)
return lntc
}
func (lntc *LiteContainerService) SetHub(hub *component.ComponentHub) {
lntc.BaseComponent.SetHub(hub)
}
// BeforeStart starts p2p service.
func (lntc *LiteContainerService) BeforeStart() {}
func (lntc *LiteContainerService) AfterStart() {
lntc.mutex.Lock()
nt := lntc.nt
nt.Start()
lntc.mutex.Unlock()
}
// BeforeStop is called before actor hub stops. it finishes underlying peer manager
func (lntc *LiteContainerService) BeforeStop() {
lntc.mutex.Lock()
nt := lntc.nt
lntc.mutex.Unlock()
nt.Stop()
}
// Statistics show statistic information of p2p module. NOTE: It it not implemented yet
func (lntc *LiteContainerService) Statistics() *map[string]interface{} {
return nil
}
func (lntc *LiteContainerService) GetNetworkTransport() p2pcommon.NetworkTransport {
lntc.mutex.Lock()
defer lntc.mutex.Unlock()
return lntc.nt
}
func (lntc *LiteContainerService) GenesisChainID() *types.ChainID {
return lntc.chainID
}
func (lntc *LiteContainerService) init(cfg *config.Config) {
// load genesis file
// init from genesis file
// TODO code duplication. refactor to delete dupplicate with p2p.go
genesis, err := readGenesis(cfg.Polaris.GenesisFile)
if err != nil {
panic(err.Error())
}
chainIdBytes, err := genesis.ChainID()
if err != nil {
panic("genesis block is not set properly: " + err.Error())
}
chainID := types.NewChainID()
err = chainID.Read(chainIdBytes)
if err != nil {
panic("invalid chainid: " + err.Error())
}
lntc.chainID = chainID
lntc.meta = initMeta(p2pkey.NodeID(), cfg.P2P)
lntc.Logger.Info().Str("genesis", chainID.ToJSON()).Msg("genesis block loaded")
netTransport := transport.NewNetworkTransport(cfg.P2P, lntc.Logger, lntc)
lntc.mutex.Lock()
lntc.nt = netTransport
lntc.mutex.Unlock()
}
// Receive got actor message and then handle it.
func (lntc *LiteContainerService) Receive(context actor.Context) {
rawMsg := context.Message()
switch msg := rawMsg.(type) {
case time.Time:
lntc.Logger.Debug().Interface("time", msg.String()).Msg("why time is came?")
default:
lntc.Logger.Debug().Interface("type", msg).Msg("unexpected msg was sent")
// do nothing
}
}
// TODO need refactoring. this code is copied from subprotcoladdrs.go
func (lntc *LiteContainerService) checkAndAddPeerAddresses(peers []*types.PeerAddress) {
selfPeerID := p2pkey.NodeID()
peerMetas := make([]p2pcommon.PeerMeta, 0, len(peers))
for _, rPeerAddr := range peers {
rPeerID := types.PeerID(rPeerAddr.PeerID)
if selfPeerID == rPeerID {
continue
}
meta := p2pcommon.FromPeerAddress(rPeerAddr)
peerMetas = append(peerMetas, meta)
}
}
func (lntc *LiteContainerService) SelfMeta() p2pcommon.PeerMeta {
return lntc.meta
}
// TellRequest implement interface method of ActorService
func (lntc *LiteContainerService) TellRequest(actor string, msg interface{}) {
lntc.TellTo(actor, msg)
}
// SendRequest implement interface method of ActorService
func (lntc *LiteContainerService) SendRequest(actor string, msg interface{}) {
lntc.RequestTo(actor, msg)
}
// FutureRequest implement interface method of ActorService
func (lntc *LiteContainerService) FutureRequest(actor string, msg interface{}, timeout time.Duration) *actor.Future {
return lntc.RequestToFuture(actor, msg, timeout)
}
// FutureRequestDefaultTimeout implement interface method of ActorService
func (lntc *LiteContainerService) FutureRequestDefaultTimeout(actor string, msg interface{}) *actor.Future {
return lntc.RequestToFuture(actor, msg, p2pcommon.DefaultActorMsgTTL)
}
// CallRequest implement interface method of ActorService
func (lntc *LiteContainerService) CallRequest(actor string, msg interface{}, timeout time.Duration) (interface{}, error) {
future := lntc.RequestToFuture(actor, msg, timeout)
return future.Result()
}
// CallRequest implement interface method of ActorService
func (lntc *LiteContainerService) CallRequestDefaultTimeout(actor string, msg interface{}) (interface{}, error) {
future := lntc.RequestToFuture(actor, msg, p2pcommon.DefaultActorMsgTTL)
return future.Result()
}
func (lntc *LiteContainerService) SelfNodeID() types.PeerID {
return lntc.meta.ID
}
func (lntc *LiteContainerService) SelfRole() types.PeerRole {
// return dummy value
return types.PeerRole_Watcher
}
func (lntc *LiteContainerService) GetChainAccessor() types.ChainAccessor {
// return dummy value
return nil
}
func (lntc *LiteContainerService) CertificateManager() p2pcommon.CertificateManager {
// return dummy value
return nil
}
// it is copy of initMeta() in p2p package
func initMeta(peerID types.PeerID, conf *config.P2PConfig) p2pcommon.PeerMeta {
protocolAddr := conf.NetProtocolAddr
var ipAddress net.IP
var err error
var protocolPort int
if len(conf.NetProtocolAddr) != 0 {
ipAddress, err = network.GetSingleIPAddress(protocolAddr)
if err != nil {
panic("Invalid protocol address " + protocolAddr + " : " + err.Error())
}
if ipAddress.IsUnspecified() {
panic("NetProtocolAddr should be a specified IP address, not 0.0.0.0")
}
} else {
extIP, err := p2putil.ExternalIP()
if err != nil {
panic("error while finding IP address: " + err.Error())
}
ipAddress = extIP
protocolAddr = ipAddress.String()
}
protocolPort = conf.NetProtocolPort
if protocolPort <= 0 {
panic("invalid NetProtocolPort " + strconv.Itoa(conf.NetProtocolPort))
}
ma,err := types.ToMultiAddr(ipAddress.String(), uint32(protocolPort))
var meta p2pcommon.PeerMeta
meta.ID = peerID
meta.Addresses = []types.Multiaddr{ma}
meta.Hidden = !conf.NPExposeSelf
meta.Version = p2pkey.NodeVersion()
return meta
}