forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
consensus_message.go
108 lines (93 loc) · 2.82 KB
/
consensus_message.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package dbft
import (
"errors"
"io"
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/common/log"
)
type ConsensusMessage interface {
Serialization(sink *common.ZeroCopySink)
Deserialization(source *common.ZeroCopySource) error
Type() ConsensusMessageType
ViewNumber() byte
ConsensusMessageData() *ConsensusMessageData
}
type ConsensusMessageData struct {
Type ConsensusMessageType
ViewNumber byte
}
func DeserializeMessage(data []byte) (ConsensusMessage, error) {
if len(data) == 0 {
return nil, io.ErrUnexpectedEOF
}
msgType := ConsensusMessageType(data[0])
source := common.NewZeroCopySource(data)
switch msgType {
case PrepareRequestMsg:
prMsg := &PrepareRequest{}
err := prMsg.Deserialization(source)
if err != nil {
log.Error("[DeserializeMessage] PrepareRequestMsg Deserialize Error: ", err.Error())
return nil, err
}
return prMsg, nil
case PrepareResponseMsg:
presMsg := &PrepareResponse{}
err := presMsg.Deserialization(source)
if err != nil {
log.Error("[DeserializeMessage] PrepareResponseMsg Deserialize Error: ", err.Error())
return nil, err
}
return presMsg, nil
case ChangeViewMsg:
cv := &ChangeView{}
err := cv.Deserialization(source)
if err != nil {
log.Error("[DeserializeMessage] ChangeViewMsg Deserialize Error: ", err.Error())
return nil, err
}
return cv, nil
case BlockSignaturesMsg:
blockSigs := &BlockSignatures{}
err := blockSigs.Deserialization(source)
if err != nil {
log.Error("[DeserializeMessage] BlockSignaturesMsg Deserialize Error: ", err.Error())
return nil, err
}
return blockSigs, nil
}
return nil, errors.New("The message is invalid.")
}
func (cd *ConsensusMessageData) Serialization(sink *common.ZeroCopySink) {
sink.WriteByte(byte(cd.Type))
sink.WriteByte(byte(cd.ViewNumber))
}
//read data to reader
func (cd *ConsensusMessageData) Deserialization(source *common.ZeroCopySource) error {
var eof bool
var temp byte
temp, eof = source.NextByte()
cd.Type = ConsensusMessageType(temp)
cd.ViewNumber, eof = source.NextByte()
if eof {
return io.ErrUnexpectedEOF
}
return nil
}