-
Notifications
You must be signed in to change notification settings - Fork 199
/
commonMessenger.go
56 lines (44 loc) · 1.37 KB
/
commonMessenger.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
package broadcast
import (
"github.com/ElrondNetwork/elrond-go/consensus"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/logger"
"github.com/ElrondNetwork/elrond-go/crypto"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/sharding"
)
var log = logger.DefaultLogger()
type commonMessenger struct {
marshalizer marshal.Marshalizer
messenger consensus.P2PMessenger
privateKey crypto.PrivateKey
shardCoordinator sharding.Coordinator
singleSigner crypto.SingleSigner
}
// BroadcastConsensusMessage will send on consensus topic the consensus message
func (cm *commonMessenger) BroadcastConsensusMessage(message *consensus.Message) error {
signature, err := cm.signMessage(message)
if err != nil {
return err
}
message.Signature = signature
buff, err := cm.marshalizer.Marshal(message)
if err != nil {
return err
}
consensusTopic := core.ConsensusTopic +
cm.shardCoordinator.CommunicationIdentifier(cm.shardCoordinator.SelfId())
go cm.messenger.Broadcast(consensusTopic, buff)
return nil
}
func (cm *commonMessenger) signMessage(message *consensus.Message) ([]byte, error) {
buff, err := cm.marshalizer.Marshal(message)
if err != nil {
return nil, err
}
signature, err := cm.singleSigner.Sign(cm.privateKey, buff)
if err != nil {
return nil, err
}
return signature, nil
}