-
Notifications
You must be signed in to change notification settings - Fork 147
/
msgCodec2.0.go
153 lines (133 loc) · 4.18 KB
/
msgCodec2.0.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
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package msgsend
import (
"encoding/json"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/mc"
"github.com/pkg/errors"
)
////////////////////////////////////////////////////////////////////////
// 重选询问请求消息
type lrInquiryReqCodecV2 struct {
}
func (*lrInquiryReqCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrInquiryReqCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ReelectInquiryReqMsg)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
msg.From.Set(from)
return msg, nil
}
////////////////////////////////////////////////////////////////////////
// 重选询问响应消息
type lrInquiryRspCodecV2 struct {
}
func (*lrInquiryRspCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrInquiryRspCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ReelectInquiryRspMsg)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
msg.From.Set(from)
return msg, nil
}
////////////////////////////////////////////////////////////////////////
// leader重选请求消息
type lrReqCodecV2 struct {
}
func (*lrReqCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrReqCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ReelectLeaderReqMsg)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
if msg.InquiryReq == nil {
return nil, errors.Errorf("'InquiryReq' of the msg is nil")
}
msg.InquiryReq.From.Set(from)
return msg, nil
}
////////////////////////////////////////////////////////////////////////
// leader重选投票消息
type lrVoteCodecV2 struct {
}
func (*lrVoteCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrVoteCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ConsensusVote)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
msg.From.Set(from)
return msg, nil
}
////////////////////////////////////////////////////////////////////////
// leader重选结果广播消息
type lrResultBCCodecV2 struct {
}
func (*lrResultBCCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrResultBCCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ReelectBroadcastMsg)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
msg.From.Set(from)
return msg, nil
}
////////////////////////////////////////////////////////////////////////
// leader重选结果广播响应消息
type lrResultBCRspCodecV2 struct {
}
func (*lrResultBCRspCodecV2) EncodeFn(msg interface{}) ([]byte, error) {
data, err := json.Marshal(msg)
if err != nil {
return nil, errors.Errorf("json.Marshal failed: %s", err)
}
return data, nil
}
func (*lrResultBCRspCodecV2) DecodeFn(data []byte, from common.Address) (interface{}, error) {
msg := new(mc.HD_V2_ReelectBroadcastRspMsg)
err := json.Unmarshal([]byte(data), msg)
if err != nil {
return nil, errors.Errorf("json.Unmarshal failed: %s", err)
}
msg.From.Set(from)
return msg, nil
}