/
istanbul.go
121 lines (104 loc) · 3.9 KB
/
istanbul.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
// 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 core/types/istanbul.go (2018/06/04).
// Modified and improved for the klaytn development.
package types
import (
"errors"
"io"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/rlp"
)
var (
// IstanbulDigest represents a hash of "Istanbul practical byzantine fault tolerance"
// to identify whether the block is from Istanbul consensus engine
IstanbulDigest = common.HexToHash("0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365")
IstanbulExtraVanity = 32 // Fixed number of extra-data bytes reserved for validator vanity
IstanbulExtraSeal = 65 // Fixed number of extra-data bytes reserved for validator seal
// ErrInvalidIstanbulHeaderExtra is returned if the length of extra-data is less than 32 bytes
ErrInvalidIstanbulHeaderExtra = errors.New("invalid istanbul header extra-data")
)
type IstanbulExtra struct {
Validators []common.Address
Seal []byte
CommittedSeal [][]byte
}
// EncodeRLP serializes the istanbul fields into the Klaytn RLP format.
func (ist *IstanbulExtra) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, []interface{}{
ist.Validators,
ist.Seal,
ist.CommittedSeal,
})
}
// DecodeRLP implements rlp.Decoder, and load the istanbul fields from a RLP stream.
func (ist *IstanbulExtra) DecodeRLP(s *rlp.Stream) error {
var istanbulExtra struct {
Validators []common.Address
Seal []byte
CommittedSeal [][]byte
}
if err := s.Decode(&istanbulExtra); err != nil {
return err
}
ist.Validators, ist.Seal, ist.CommittedSeal = istanbulExtra.Validators, istanbulExtra.Seal, istanbulExtra.CommittedSeal
return nil
}
// ExtractIstanbulExtra extracts all values of the IstanbulExtra from the header. It returns an
// error if the length of the given extra-data is less than 32 bytes or the extra-data can not
// be decoded.
func ExtractIstanbulExtra(h *Header) (*IstanbulExtra, error) {
if len(h.Extra) < IstanbulExtraVanity {
return nil, ErrInvalidIstanbulHeaderExtra
}
var istanbulExtra *IstanbulExtra
err := rlp.DecodeBytes(h.Extra[IstanbulExtraVanity:], &istanbulExtra)
if err != nil {
return nil, err
}
return istanbulExtra, nil
}
// IstanbulFilteredHeader returns a filtered header which some information (like seal, committed seals)
// are clean to fulfill the Istanbul hash rules. It returns nil if the extra-data cannot be
// decoded/encoded by rlp.
func IstanbulFilteredHeader(h *Header, keepSeal bool) *Header {
newHeader := CopyHeader(h)
istanbulExtra, err := ExtractIstanbulExtra(newHeader)
if err != nil {
return nil
}
if !keepSeal {
istanbulExtra.Seal = []byte{}
}
istanbulExtra.CommittedSeal = [][]byte{}
payload, err := rlp.EncodeToBytes(&istanbulExtra)
if err != nil {
return nil
}
newHeader = SetRoundToHeader(newHeader, 0)
newHeader.Extra = append(newHeader.Extra[:IstanbulExtraVanity], payload...)
return newHeader
}
func SetRoundToHeader(h *Header, r int64) *Header {
h.Extra[IstanbulExtraVanity-1] = byte(r)
return h
}
func SetRoundToBlock(block *Block, r int64) *Block {
header := SetRoundToHeader(block.Header(), r)
return block.WithSeal(header)
}