-
Notifications
You must be signed in to change notification settings - Fork 189
/
types.go
171 lines (147 loc) · 4.13 KB
/
types.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
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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 go-ethereum library 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from quorum/consensus/istanbul/core/types.go (2018/06/04).
// Modified and improved for the klaytn development.
package core
import (
"fmt"
"io"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/rlp"
)
type Engine interface {
Start() error
Stop() error
}
type State uint64
const (
StateAcceptRequest State = iota
StatePreprepared
StatePrepared
StateCommitted
)
func (s State) String() string {
if s == StateAcceptRequest {
return "Accept request"
} else if s == StatePreprepared {
return "Preprepared"
} else if s == StatePrepared {
return "Prepared"
} else if s == StateCommitted {
return "Committed"
} else {
return "Unknown"
}
}
// Cmp compares s and y and returns:
// -1 if s is the previous state of y
// 0 if s and y are the same state
// +1 if s is the next state of y
func (s State) Cmp(y State) int {
if uint64(s) < uint64(y) {
return -1
}
if uint64(s) > uint64(y) {
return 1
}
return 0
}
const (
msgPreprepare uint64 = iota
msgPrepare
msgCommit
msgRoundChange
msgAll
)
type message struct {
Hash common.Hash
Code uint64
Msg []byte
Address common.Address
Signature []byte
CommittedSeal []byte
}
// ==============================================
//
// define the functions that needs to be provided for rlp Encoder/Decoder.
// EncodeRLP serializes m into the Klaytn RLP format.
func (m *message) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{m.Hash, m.Code, m.Msg, m.Address, m.Signature, m.CommittedSeal})
}
// DecodeRLP implements rlp.Decoder, and load the consensus fields from a RLP stream.
func (m *message) DecodeRLP(s *rlp.Stream) error {
var msg struct {
Hash common.Hash
Code uint64
Msg []byte
Address common.Address
Signature []byte
CommittedSeal []byte
}
if err := s.Decode(&msg); err != nil {
return err
}
m.Hash, m.Code, m.Msg, m.Address, m.Signature, m.CommittedSeal = msg.Hash, msg.Code, msg.Msg, msg.Address, msg.Signature, msg.CommittedSeal
return nil
}
// ==============================================
//
// define the functions that needs to be provided for core.
func (m *message) FromPayload(b []byte, validateFn func([]byte, []byte) (common.Address, error)) error {
// Decode message
err := rlp.DecodeBytes(b, &m)
if err != nil {
return err
}
// Validate message (on a message without Signature)
if validateFn != nil {
var payload []byte
payload, err = m.PayloadNoSig()
if err != nil {
return err
}
_, err = validateFn(payload, m.Signature)
}
// Still return the message even the err is not nil
return err
}
func (m *message) Payload() ([]byte, error) {
return rlp.EncodeToBytes(m)
}
func (m *message) PayloadNoSig() ([]byte, error) {
return rlp.EncodeToBytes(&message{
Hash: m.Hash,
Code: m.Code,
Msg: m.Msg,
Address: m.Address,
Signature: []byte{},
CommittedSeal: m.CommittedSeal,
})
}
func (m *message) Decode(val interface{}) error {
return rlp.DecodeBytes(m.Msg, val)
}
func (m *message) String() string {
return fmt.Sprintf("{Code: %v, Address: %v}", m.Code, m.Address.String())
}
// ==============================================
//
// helper functions
func Encode(val interface{}) ([]byte, error) {
return rlp.EncodeToBytes(val)
}