@@ -3,8 +3,8 @@ package sync
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"math/rand"
7
- "strconv"
8
8
"time"
9
9
10
10
"github.com/ethereum/go-ethereum/event"
@@ -17,7 +17,6 @@ import (
17
17
"github.com/harmony-one/harmony/p2p/stream/common/requestmanager"
18
18
"github.com/harmony-one/harmony/p2p/stream/common/streammanager"
19
19
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
20
- "github.com/harmony-one/harmony/shard"
21
20
"github.com/hashicorp/go-version"
22
21
libp2p_host "github.com/libp2p/go-libp2p/core/host"
23
22
libp2p_network "github.com/libp2p/go-libp2p/core/network"
@@ -27,7 +26,8 @@ import (
27
26
28
27
const (
29
28
// serviceSpecifier is the specifier for the service.
30
- serviceSpecifier = "sync"
29
+ SyncServiceSpecifier = "sync"
30
+ EpochSyncServiceSpecifier = "epochsync"
31
31
)
32
32
33
33
var (
71
71
BeaconNode bool
72
72
Validator bool
73
73
Explorer bool
74
+ EpochChain bool
74
75
75
76
MaxAdvertiseWaitTime int
76
77
// stream manager config
@@ -106,6 +107,12 @@ func NewProtocol(config Config) *Protocol {
106
107
107
108
sp .rm = requestmanager .NewRequestManager (sp .sm )
108
109
110
+ // if it is not epoch chain, print the peer id and proto id
111
+ if ! config .EpochChain {
112
+ fmt .Println ("My peer id: " , config .Host .ID ().String ())
113
+ fmt .Println ("My proto id: " , sp .ProtoID ())
114
+ }
115
+
109
116
sp .logger = utils .Logger ().With ().Str ("Protocol" , string (sp .ProtoID ())).Logger ()
110
117
return sp
111
118
}
@@ -115,10 +122,7 @@ func (p *Protocol) Start() {
115
122
p .sm .Start ()
116
123
p .rm .Start ()
117
124
p .rl .Start ()
118
- // If it's not EpochChain, advertise
119
- if p .config .BeaconNode || p .chain .ShardID () != shard .BeaconChainShardID {
120
- go p .advertiseLoop ()
121
- }
125
+ go p .advertiseLoop ()
122
126
}
123
127
124
128
// Close close the protocol
@@ -130,19 +134,18 @@ func (p *Protocol) Close() {
130
134
close (p .closeC )
131
135
}
132
136
133
- // Specifier return the specifier for the protocol
134
- func (p * Protocol ) Specifier () string {
135
- return serviceSpecifier + "/" + strconv .Itoa (int (p .config .ShardID ))
136
- }
137
-
138
137
// ProtoID return the ProtoID of the sync protocol
139
138
func (p * Protocol ) ProtoID () sttypes.ProtoID {
140
139
return p .protoIDByVersion (MyVersion )
141
140
}
142
141
143
- // ShardProtoID returns the ProtoID of the sync protocol for shard nodes
144
- func (p * Protocol ) ShardProtoID () sttypes.ProtoID {
145
- return p .protoIDByVersionForShardNodes (MyVersion )
142
+ // ServiceID returns the service ID of the sync protocol
143
+ func (p * Protocol ) ServiceID () string {
144
+ serviceID := SyncServiceSpecifier
145
+ if p .config .EpochChain {
146
+ serviceID = EpochSyncServiceSpecifier
147
+ }
148
+ return serviceID
146
149
}
147
150
148
151
// Version returns the sync protocol version
@@ -155,6 +158,11 @@ func (p *Protocol) IsBeaconValidator() bool {
155
158
return p .config .BeaconNode && p .config .Validator
156
159
}
157
160
161
+ // IsEpochChain returns true if it is a epoch chain
162
+ func (p * Protocol ) IsEpochChain () bool {
163
+ return p .config .EpochChain
164
+ }
165
+
158
166
// IsValidator returns true if it is a validator node
159
167
func (p * Protocol ) IsValidator () bool {
160
168
return p .config .Validator
@@ -171,7 +179,7 @@ func (p *Protocol) Match(targetID protocol.ID) bool {
171
179
if err != nil {
172
180
return false
173
181
}
174
- if target .Service != serviceSpecifier {
182
+ if target .Service != p . ServiceID () {
175
183
return false
176
184
}
177
185
if target .NetworkType != p .config .Network {
@@ -347,11 +355,6 @@ func (p *Protocol) supportedProtoIDs() []sttypes.ProtoID {
347
355
pids := make ([]sttypes.ProtoID , 0 , len (vs ))
348
356
for _ , v := range vs {
349
357
pids = append (pids , p .protoIDByVersion (v ))
350
- // beacon node needs to inform shard nodes about it supports them as well for EpochChain
351
- // basically beacon node can accept connection from shard nodes to share last epoch blocks
352
- if p .IsBeaconValidator () {
353
- pids = append (pids , p .protoIDByVersionForShardNodes (v ))
354
- }
355
358
}
356
359
return pids
357
360
}
@@ -362,22 +365,10 @@ func (p *Protocol) supportedVersions() []*version.Version {
362
365
363
366
func (p * Protocol ) protoIDByVersion (v * version.Version ) sttypes.ProtoID {
364
367
spec := sttypes.ProtoSpec {
365
- Service : serviceSpecifier ,
366
- NetworkType : p .config .Network ,
367
- ShardID : p .config .ShardID ,
368
- Version : v ,
369
- IsBeaconValidator : (p .config .Validator || p .config .Explorer ) && p .config .BeaconNode ,
370
- }
371
- return spec .ToProtoID ()
372
- }
373
-
374
- func (p * Protocol ) protoIDByVersionForShardNodes (v * version.Version ) sttypes.ProtoID {
375
- spec := sttypes.ProtoSpec {
376
- Service : serviceSpecifier ,
377
- NetworkType : p .config .Network ,
378
- ShardID : p .config .ShardID ,
379
- Version : v ,
380
- IsBeaconValidator : false ,
368
+ Service : p .ServiceID (),
369
+ NetworkType : p .config .Network ,
370
+ ShardID : p .config .ShardID ,
371
+ Version : v ,
381
372
}
382
373
return spec .ToProtoID ()
383
374
}
0 commit comments