aboutsummaryrefslogtreecommitdiffstats
path: root/core/headerchain.go
diff options
context:
space:
mode:
authorBas van Kervel <bas@ethdev.com>2016-07-26 22:37:04 +0800
committerBas van Kervel <bas@ethdev.com>2016-08-16 21:21:22 +0800
commitbb8059f6aa86d1052d7c2dd75a6985982cb278f4 (patch)
tree44c57f98592518652f9791655be98c75fc198760 /core/headerchain.go
parent4c2cc32f2e279baa3059603b8c8a4329f31606f6 (diff)
downloadgo-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.gz
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.bz2
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.lz
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.xz
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.tar.zst
go-tangerine-bb8059f6aa86d1052d7c2dd75a6985982cb278f4.zip
core: ensure the canonical block is written before the canonical hash is set
Diffstat (limited to 'core/headerchain.go')
-rw-r--r--core/headerchain.go18
1 files changed, 11 insertions, 7 deletions
diff --git a/core/headerchain.go b/core/headerchain.go
index f856333a0..0f9dd7208 100644
--- a/core/headerchain.go
+++ b/core/headerchain.go
@@ -151,6 +151,14 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
localTd := hc.GetTd(hc.currentHeaderHash, hc.currentHeader.Number.Uint64())
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
// Second clause in the if statement reduces the vulnerability to selfish mining.
// 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
headHeader = hc.GetHeader(headHash, headNumber)
}
+
// Extend the canonical chain with the new header
if err := WriteCanonicalHash(hc.chainDb, hash, number); err != nil {
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 {
glog.Fatalf("failed to insert head header hash: %v", err)
}
+
hc.currentHeaderHash, hc.currentHeader = hash, types.CopyHeader(header)
status = CanonStatTy
} else {
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.numberCache.Add(hash, number)