/
types.go
161 lines (132 loc) · 3.88 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
// 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/types.go (2018/06/04).
// Modified and improved for the klaytn development.
package istanbul
import (
"fmt"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/ser/rlp"
"io"
"math/big"
)
// Proposal supports retrieving height and serialized block to be used during Istanbul consensus.
type Proposal interface {
// Number retrieves the sequence number of this proposal
Number() *big.Int
// Hash retrieves the hash of this proposal
Hash() common.Hash
EncodeRLP(w io.Writer) error
DecodeRLP(s *rlp.Stream) error
String() string
ParentHash() common.Hash
Header() *types.Header
WithSeal(header *types.Header) *types.Block
}
type Request struct {
Proposal Proposal
}
// View includes a round number and a sequence number.
// Sequence is the block number we'd like to commit.
// Each round has a number and is composed by 3 steps: preprepare, prepare and commit.
//
// If the given block is not accepted by validators, a round change will occur
// and the validators start a new round with round+1.
type View struct {
Round *big.Int
Sequence *big.Int
}
// EncodeRLP serializes a View into the Klaytn RLP format.
func (v *View) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{v.Round, v.Sequence})
}
func (v *View) DecodeRLP(s *rlp.Stream) error {
var view struct {
Round *big.Int
Sequence *big.Int
}
if err := s.Decode(&view); err != nil {
return err
}
v.Round, v.Sequence = view.Round, view.Sequence
return nil
}
func (v *View) String() string {
return fmt.Sprintf("{Round: %d, Sequence: %d}", v.Round.Uint64(), v.Sequence.Uint64())
}
// Cmp compares v and y and returns:
// -1 if v < y
// 0 if v == y
// +1 if v > y
func (v *View) Cmp(y *View) int {
sdiff := v.Sequence.Cmp(y.Sequence)
if sdiff != 0 {
return sdiff
}
rdiff := v.Round.Cmp(y.Round)
if rdiff != 0 {
return rdiff
}
return 0
}
type Preprepare struct {
View *View
Proposal Proposal
}
func (b *Preprepare) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{b.View, b.Proposal})
}
func (b *Preprepare) DecodeRLP(s *rlp.Stream) error {
var preprepare struct {
View *View
Proposal *types.Block
}
if err := s.Decode(&preprepare); err != nil {
return err
}
b.View, b.Proposal = preprepare.View, preprepare.Proposal
return nil
}
type Subject struct {
View *View
Digest common.Hash
PrevHash common.Hash
}
func (b *Subject) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{b.View, b.Digest, b.PrevHash})
}
func (b *Subject) DecodeRLP(s *rlp.Stream) error {
var subject struct {
View *View
Digest common.Hash
PrevHash common.Hash
}
if err := s.Decode(&subject); err != nil {
return err
}
b.View, b.Digest, b.PrevHash = subject.View, subject.Digest, subject.PrevHash
return nil
}
func (b *Subject) String() string {
return fmt.Sprintf("{View: %v, Digest: %v, ParentHash: %v}", b.View, b.Digest.String(), b.PrevHash.Hex())
}
type ConsensusMsg struct {
PrevHash common.Hash
Payload []byte
}