@@ -129,6 +129,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
129129 localTd := hc .GetTd (hc .currentHeaderHash )
130130 externTd := new (big.Int ).Add (header .Difficulty , ptd )
131131
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+
132140 // If the total difficulty is higher than our known, add it to the canonical chain
133141 // Second clause in the if statement reduces the vulnerability to selfish mining.
134142 // 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
150158 headHeader = hc .GetHeader (headHash )
151159 headNumber = headHeader .Number .Uint64 ()
152160 }
161+
153162 // Extend the canonical chain with the new header
154163 if err := WriteCanonicalHash (hc .chainDb , hash , number ); err != nil {
155164 glog .Fatalf ("failed to insert header number: %v" , err )
156165 }
157166 if err := WriteHeadHeaderHash (hc .chainDb , hash ); err != nil {
158167 glog .Fatalf ("failed to insert head header hash: %v" , err )
159168 }
169+
160170 hc .currentHeaderHash , hc .currentHeader = hash , types .CopyHeader (header )
161171
162172 status = CanonStatTy
163173 } else {
164174 status = SideStatTy
165175 }
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- }
173176 hc .headerCache .Add (hash , header )
174177
175178 return
0 commit comments