|
|
@ -151,6 +151,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er |
|
|
|
localTd := hc.GetTd(hc.currentHeaderHash, hc.currentHeader.Number.Uint64()) |
|
|
|
localTd := hc.GetTd(hc.currentHeaderHash, hc.currentHeader.Number.Uint64()) |
|
|
|
externTd := new(big.Int).Add(header.Difficulty, ptd) |
|
|
|
externTd := new(big.Int).Add(header.Difficulty, ptd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Irrelevant of the canonical status, write the td and header to the database
|
|
|
|
|
|
|
|
if err := hc.WriteTd(hash, number, externTd); err != nil { |
|
|
|
|
|
|
|
glog.Fatalf("failed to write header total difficulty: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := WriteHeader(hc.chainDb, header); err != nil { |
|
|
|
|
|
|
|
glog.Fatalf("failed to write header contents: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If the total difficulty is higher than our known, add it to the canonical chain
|
|
|
|
// If the total difficulty is higher than our known, add it to the canonical chain
|
|
|
|
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
|
|
|
// Second clause in the if statement reduces the vulnerability to selfish mining.
|
|
|
|
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
|
|
|
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
|
|
|
@ -176,6 +184,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er |
|
|
|
headNumber = headHeader.Number.Uint64() - 1 |
|
|
|
headNumber = headHeader.Number.Uint64() - 1 |
|
|
|
headHeader = hc.GetHeader(headHash, headNumber) |
|
|
|
headHeader = hc.GetHeader(headHash, headNumber) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Extend the canonical chain with the new header
|
|
|
|
// Extend the canonical chain with the new header
|
|
|
|
if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil { |
|
|
|
if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil { |
|
|
|
glog.Fatalf("failed to insert header number: %v", err) |
|
|
|
glog.Fatalf("failed to insert header number: %v", err) |
|
|
@ -183,19 +192,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er |
|
|
|
if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil { |
|
|
|
if err := WriteHeadHeaderHash(hc.chainDb, hash); err != nil { |
|
|
|
glog.Fatalf("failed to insert head header hash: %v", err) |
|
|
|
glog.Fatalf("failed to insert head header hash: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header) |
|
|
|
hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header) |
|
|
|
|
|
|
|
|
|
|
|
status = CanonStatTy |
|
|
|
status = CanonStatTy |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
status = SideStatTy |
|
|
|
status = SideStatTy |
|
|
|
} |
|
|
|
} |
|
|
|
// Irrelevant of the canonical status, write the header itself to the database
|
|
|
|
|
|
|
|
if err := hc.WriteTd(hash, number, externTd); err != nil { |
|
|
|
|
|
|
|
glog.Fatalf("failed to write header total difficulty: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := WriteHeader(hc.chainDb, header); err != nil { |
|
|
|
|
|
|
|
glog.Fatalf("failed to write header contents: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
hc.headerCache.Add(hash, header) |
|
|
|
hc.headerCache.Add(hash, header) |
|
|
|
hc.numberCache.Add(hash, number) |
|
|
|
hc.numberCache.Add(hash, number) |
|
|
|
|
|
|
|
|
|
|
|