@@ -129,6 +129,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
129
129
localTd := hc .GetTd (hc .currentHeaderHash )
130
130
externTd := new (big.Int ).Add (header .Difficulty , ptd )
131
131
132
+ // Irrelevant of the canonical status, write the td and header to the database
133
+ if err := hc .WriteTd (hash , externTd ); err != nil {
134
+ glog .Fatalf ("failed to write header total difficulty: %v" , err )
135
+ }
136
+ if err := WriteHeader (hc .chainDb , header ); err != nil {
137
+ glog .Fatalf ("failed to write header contents: %v" , err )
138
+ }
139
+
132
140
// If the total difficulty is higher than our known, add it to the canonical chain
133
141
// Second clause in the if statement reduces the vulnerability to selfish mining.
134
142
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
@@ -150,26 +158,21 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
150
158
headHeader = hc .GetHeader (headHash )
151
159
headNumber = headHeader .Number .Uint64 ()
152
160
}
161
+
153
162
// Extend the canonical chain with the new header
154
163
if err := WriteCanonicalHash (hc .chainDb , hash , number ); err != nil {
155
164
glog .Fatalf ("failed to insert header number: %v" , err )
156
165
}
157
166
if err := WriteHeadHeaderHash (hc .chainDb , hash ); err != nil {
158
167
glog .Fatalf ("failed to insert head header hash: %v" , err )
159
168
}
169
+
160
170
hc .currentHeaderHash , hc .currentHeader = hash , types .CopyHeader (header )
161
171
162
172
status = CanonStatTy
163
173
} else {
164
174
status = SideStatTy
165
175
}
166
- // Irrelevant of the canonical status, write the header itself to the database
167
- if err := hc .WriteTd (hash , externTd ); err != nil {
168
- glog .Fatalf ("failed to write header total difficulty: %v" , err )
169
- }
170
- if err := WriteHeader (hc .chainDb , header ); err != nil {
171
- glog .Fatalf ("failed to write header contents: %v" , err )
172
- }
173
176
hc .headerCache .Add (hash , header )
174
177
175
178
return
0 commit comments