From 90d4299f0b364a92ba8666158d0ead07fec95b92 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Nov 2019 16:09:45 +0300 Subject: [PATCH 1/5] core: gofmt blockchain.go --- pkg/core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 59b82cca2..d33ee264a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -266,7 +266,7 @@ func (bc *Blockchain) AddHeaders(headers ...*Header) (err error) { } if oldlen != headerList.Len() { - updateHeaderHeightMetric(headerList.Len()-1) + updateHeaderHeightMetric(headerList.Len() - 1) if err = bc.store.PutBatch(batch); err != nil { return } From 9f7625d6992e56fdd76dc0c88797964988bc68d2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Nov 2019 16:10:37 +0300 Subject: [PATCH 2/5] core: don't do useless DB reads if nothing was persisted --- pkg/core/blockchain.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index d33ee264a..ac8ca3365 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -492,18 +492,18 @@ func (bc *Blockchain) persist() error { if err != nil { return err } - bHeight, err := storage.CurrentBlockHeight(bc.store) - if err != nil { - return err - } - oldHeight := atomic.SwapUint32(&bc.persistedHeight, bHeight) - diff := bHeight - oldHeight - - storedHeaderHeight, _, err := storage.CurrentHeaderHeight(bc.store) - if err != nil { - return err - } if persisted > 0 { + bHeight, err := storage.CurrentBlockHeight(bc.store) + if err != nil { + return err + } + oldHeight := atomic.SwapUint32(&bc.persistedHeight, bHeight) + diff := bHeight - oldHeight + + storedHeaderHeight, _, err := storage.CurrentHeaderHeight(bc.store) + if err != nil { + return err + } log.WithFields(log.Fields{ "persistedBlocks": diff, "persistedKeys": persisted, From c16c2bf1020b487ed270cf4a4602ee260c4fc14c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Nov 2019 17:56:06 +0300 Subject: [PATCH 3/5] core: store current header reference on clean init Missing it the following line could fail on subsequent restarts: currHeaderHeight, currHeaderHash, err := storage.CurrentHeaderHeight(bc.store) if the node was stopped before any headers had been received. --- pkg/core/blockchain.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index ac8ca3365..8f31adfb9 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -101,6 +101,10 @@ func (bc *Blockchain) init() error { return err } bc.headerList = NewHeaderHashList(genesisBlock.Hash()) + err = bc.store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndexToBytes(genesisBlock.Hash(), genesisBlock.Index)) + if err != nil { + return err + } return bc.storeBlock(genesisBlock) } if ver != version { From b5199625cd2962dcbb55a8634b7e2534b635dcf5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Nov 2019 17:58:19 +0300 Subject: [PATCH 4/5] core: init genesis block header if there are no other headers in DB Fixes crash when restarted after the DB initialization and no blocks written into the DB. --- pkg/core/blockchain.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 8f31adfb9..00e44b62e 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -135,6 +135,9 @@ func (bc *Blockchain) init() error { if err != nil { return err } + if bc.storedHeaderCount == 0 && currHeaderHeight == 0 { + bc.headerList.Add(currHeaderHash) + } // There is a high chance that the Node is stopped before the next // batch of 2000 headers was stored. Via the currentHeaders stored we can sync From c5673077480ec439fce2c014ac56f450c4a08f5c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 6 Nov 2019 18:00:14 +0300 Subject: [PATCH 5/5] core: fix potential issue with header list init If we're to receive some 500 headers (less than `headerBatchCount`) and quit before receiving more of them we end up with clean `bc.headerList` that will be inited going backwards to the `targetHash`, but code path doesn't add add the `targetHash` itself which it should do in this particular case, otherwise we end with no genesis block hash in the list. --- pkg/core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 00e44b62e..8b2ecfa75 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -153,6 +153,7 @@ func (bc *Blockchain) init() error { return err } targetHash = genesisBlock.Hash() + bc.headerList.Add(targetHash) } headers := make([]*Header, 0) @@ -164,7 +165,6 @@ func (bc *Blockchain) init() error { headers = append(headers, header) hash = header.PrevHash } - headerSliceReverse(headers) for _, h := range headers { if !h.Verify() {