-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocketOutportDriverNodePart.go
executable file
·177 lines (145 loc) · 6.23 KB
/
websocketOutportDriverNodePart.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
package websocketOutportDriver
import (
"fmt"
"github.com/Reshusk23/sr-chain-core-go/core"
"github.com/Reshusk23/sr-chain-core-go/core/atomic"
"github.com/Reshusk23/sr-chain-core-go/core/check"
"github.com/Reshusk23/sr-chain-core-go/data"
"github.com/Reshusk23/sr-chain-core-go/data/outport"
"github.com/Reshusk23/sr-chain-core-go/marshal"
outportSenderData "github.com/Reshusk23/sr-chain-core-go/websocketOutportDriver/data"
)
// WebsocketOutportDriverNodePartArgs holds the arguments needed for creating a new websocketOutportDriverNodePart
type WebsocketOutportDriverNodePartArgs struct {
Enabled bool
Marshaller marshal.Marshalizer
WebsocketSender WebSocketSenderHandler
WebSocketConfig outportSenderData.WebSocketConfig
Uint64ByteSliceConverter Uint64ByteSliceConverter
Log core.Logger
}
type websocketOutportDriverNodePart struct {
marshalizer marshal.Marshalizer
log core.Logger
uint64ByteSliceConverter Uint64ByteSliceConverter
webSocketSender WebSocketSenderHandler
isClosed atomic.Flag
}
// NewWebsocketOutportDriverNodePart will create a new instance of websocketOutportDriverNodePart
func NewWebsocketOutportDriverNodePart(args WebsocketOutportDriverNodePartArgs) (*websocketOutportDriverNodePart, error) {
if check.IfNil(args.Marshaller) {
return nil, outportSenderData.ErrNilMarshaller
}
if check.IfNil(args.WebsocketSender) {
return nil, outportSenderData.ErrNilWebSocketSender
}
if check.IfNil(args.Uint64ByteSliceConverter) {
return nil, outportSenderData.ErrNilUint64ByteSliceConverter
}
if check.IfNil(args.Log) {
return nil, outportSenderData.ErrNilLogger
}
isClosedFlag := atomic.Flag{}
isClosedFlag.SetValue(false)
return &websocketOutportDriverNodePart{
marshalizer: args.Marshaller,
webSocketSender: args.WebsocketSender,
uint64ByteSliceConverter: args.Uint64ByteSliceConverter,
log: args.Log,
isClosed: isClosedFlag,
}, nil
}
// SaveBlock will send the provided block saving arguments within the websocket
func (o *websocketOutportDriverNodePart) SaveBlock(args *outport.ArgsSaveBlockData) error {
argsSaveBlock := outportSenderData.ArgsSaveBlock{
HeaderType: core.GetHeaderType(args.Header),
ArgsSaveBlockData: PrepareArgsSaveBlock(*args),
}
return o.handleAction(argsSaveBlock, outportSenderData.OperationSaveBlock)
}
// RevertIndexedBlock will handle the action of reverting the indexed block
func (o *websocketOutportDriverNodePart) RevertIndexedBlock(header data.HeaderHandler, body data.BodyHandler) error {
args := outportSenderData.ArgsRevertIndexedBlock{
Header: header,
Body: body,
HeaderType: core.GetHeaderType(header),
}
return o.handleAction(args, outportSenderData.OperationRevertIndexedBlock)
}
// SaveRoundsInfo will handle the saving of rounds
func (o *websocketOutportDriverNodePart) SaveRoundsInfo(roundsInfos []*outport.RoundInfo) error {
args := outportSenderData.ArgsSaveRoundsInfo{
RoundsInfos: roundsInfos,
}
return o.handleAction(args, outportSenderData.OperationSaveRoundsInfo)
}
// SaveValidatorsPubKeys will handle the saving of the validators' public keys
func (o *websocketOutportDriverNodePart) SaveValidatorsPubKeys(validatorsPubKeys map[uint32][][]byte, epoch uint32) error {
args := outportSenderData.ArgsSaveValidatorsPubKeys{
ValidatorsPubKeys: validatorsPubKeys,
Epoch: epoch,
}
return o.handleAction(args, outportSenderData.OperationSaveValidatorsPubKeys)
}
// SaveValidatorsRating will handle the saving of the validators' rating
func (o *websocketOutportDriverNodePart) SaveValidatorsRating(indexID string, infoRating []*outport.ValidatorRatingInfo) error {
args := outportSenderData.ArgsSaveValidatorsRating{
IndexID: indexID,
InfoRating: infoRating,
}
return o.handleAction(args, outportSenderData.OperationSaveValidatorsRating)
}
// SaveAccounts will handle the accounts' saving
func (o *websocketOutportDriverNodePart) SaveAccounts(blockTimestamp uint64, acc map[string]*outport.AlteredAccount, shardID uint32) error {
args := outportSenderData.ArgsSaveAccounts{
BlockTimestamp: blockTimestamp,
Acc: acc,
ShardID: shardID,
}
return o.handleAction(args, outportSenderData.OperationSaveAccounts)
}
// FinalizedBlock will handle the finalized block
func (o *websocketOutportDriverNodePart) FinalizedBlock(headerHash []byte) error {
args := outportSenderData.ArgsFinalizedBlock{
HeaderHash: headerHash,
}
return o.handleAction(args, outportSenderData.OperationFinalizedBlock)
}
// Close will handle the closing of the outport driver web socket sender
func (o *websocketOutportDriverNodePart) Close() error {
o.isClosed.SetValue(true)
return o.webSocketSender.Close()
}
// IsInterfaceNil returns true if there is no value under the interface
func (o *websocketOutportDriverNodePart) IsInterfaceNil() bool {
return o == nil
}
func (o *websocketOutportDriverNodePart) handleAction(args interface{}, operation outportSenderData.OperationType) error {
if o.isClosed.IsSet() {
return outportSenderData.ErrWebSocketServerIsClosed
}
marshaledBlock, err := o.marshalizer.Marshal(args)
if err != nil {
o.log.Error("cannot marshal block", "operation", operation.String(), "error", err)
return fmt.Errorf("%w while marshaling block for operation %s", err, operation.String())
}
payload := o.preparePayload(operation, marshaledBlock)
err = o.webSocketSender.Send(outportSenderData.WsSendArgs{
Payload: payload,
})
if err != nil {
o.log.Error("cannot send on route", "operation", operation.String(), "error", err)
return fmt.Errorf("%w while sending data on route for operation %s", err, operation.String())
}
return nil
}
func (o *websocketOutportDriverNodePart) preparePayload(operation outportSenderData.OperationType, data []byte) []byte {
opBytes := o.uint64ByteSliceConverter.ToByteSlice(uint64(operation.Uint32()))
opBytes = opBytes[uint32NumBytes:]
messageLength := uint64(len(data))
messageLengthBytes := o.uint64ByteSliceConverter.ToByteSlice(messageLength)
messageLengthBytes = messageLengthBytes[uint32NumBytes:]
payload := append(opBytes, messageLengthBytes...)
payload = append(payload, data...)
return payload
}