@@ -5,14 +5,15 @@ import (
5
5
"bytes"
6
6
"io"
7
7
"log"
8
+ "math"
8
9
"os"
9
10
"sort"
10
11
11
12
"github.com/scroll-tech/go-ethereum/export-headers-toolkit/types"
12
13
)
13
14
14
- // maxVanityCount is the maximum number of unique vanities that can be represented with 6 bits in the bitmask
15
- const maxVanityCount = 1 << 6
15
+ // maxVanityCount is the maximum number of unique vanities that can be represented with a single byte.
16
+ const maxVanityCount = math . MaxUint8
16
17
17
18
type missingHeaderFileWriter struct {
18
19
file * os.File
@@ -94,21 +95,27 @@ func (m *missingHeaderWriter) writeVanities() {
94
95
95
96
func (m * missingHeaderWriter ) write (header * types.Header ) {
96
97
// 1. prepare the bitmask
97
- bits := newBitMask (m .sortedVanitiesMap [header .Vanity ()], int (header .Difficulty ), header .SealLen ())
98
+ bits := newBitMask (int (header .Difficulty ), header .SealLen ())
99
+ vanityIndex := m .sortedVanitiesMap [header .Vanity ()]
98
100
99
- // 2. write the header: bitmask and seal
101
+ if vanityIndex >= maxVanityCount {
102
+ log .Fatalf ("Vanity index %d exceeds maximum allowed %d" , vanityIndex , maxVanityCount - 1 )
103
+ }
104
+
105
+ // 2. write the header: bitmask, vanity index and seal
100
106
if _ , err := m .writer .Write (bits .Bytes ()); err != nil {
101
107
log .Fatalf ("Error writing bitmask: %v" , err )
102
108
}
103
-
109
+ if _ , err := m .writer .Write ([]byte {uint8 (vanityIndex )}); err != nil {
110
+ log .Fatalf ("Error writing vanity index: %v" , err )
111
+ }
104
112
if _ , err := m .writer .Write (header .Seal ()); err != nil {
105
113
log .Fatalf ("Error writing seal: %v" , err )
106
114
}
107
115
}
108
116
109
117
// bitMask is a bitmask that encodes the following information:
110
118
//
111
- // bit 0-5: index of the vanity in the sorted vanities list
112
119
// bit 6: 0 if difficulty is 2, 1 if difficulty is 1
113
120
// bit 7: 0 if seal length is 65, 1 if seal length is 85
114
121
type bitMask struct {
@@ -119,14 +126,9 @@ func newBitMaskFromByte(b uint8) bitMask {
119
126
return bitMask {b }
120
127
}
121
128
122
- func newBitMask (vanityIndex int , difficulty int , sealLen int ) bitMask {
129
+ func newBitMask (difficulty int , sealLen int ) bitMask {
123
130
b := uint8 (0 )
124
131
125
- if vanityIndex >= maxVanityCount {
126
- log .Fatalf ("Vanity index exceeds maximum: %d >= %d" , vanityIndex , maxVanityCount )
127
- }
128
- b |= uint8 (vanityIndex ) & 0b00111111
129
-
130
132
if difficulty == 1 {
131
133
b |= 1 << 6
132
134
} else if difficulty != 2 {
@@ -142,10 +144,6 @@ func newBitMask(vanityIndex int, difficulty int, sealLen int) bitMask {
142
144
return bitMask {b }
143
145
}
144
146
145
- func (b bitMask ) vanityIndex () int {
146
- return int (b .b & 0b00111111 )
147
- }
148
-
149
147
func (b bitMask ) difficulty () int {
150
148
val := (b .b >> 6 ) & 0x01
151
149
if val == 0 {
0 commit comments