Merge pull request #393 from nspcc-dev/assorted-db-handling-fixes-and-optimizations

Assorted db handling fixes and optimizations based on the testnet
connection experience.
This commit is contained in:
Roman Khimov 2019-09-11 22:31:19 +03:00 committed by GitHub
commit d0ffd165fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View file

@ -84,12 +84,6 @@ func NewBlockchain(ctx context.Context, s storage.Store, cfg config.ProtocolConf
} }
func (bc *Blockchain) init() error { func (bc *Blockchain) init() error {
genesisBlock, err := createGenesisBlock(bc.config)
if err != nil {
return err
}
bc.headerList = NewHeaderHashList(genesisBlock.Hash())
// If we could not find the version in the Store, we know that there is nothing stored. // If we could not find the version in the Store, we know that there is nothing stored.
ver, err := storage.Version(bc.Store) ver, err := storage.Version(bc.Store)
if err != nil { if err != nil {
@ -97,6 +91,11 @@ func (bc *Blockchain) init() error {
if err = storage.PutVersion(bc.Store, version); err != nil { if err = storage.PutVersion(bc.Store, version); err != nil {
return err return err
} }
genesisBlock, err := createGenesisBlock(bc.config)
if err != nil {
return err
}
bc.headerList = NewHeaderHashList(genesisBlock.Hash())
return bc.persistBlock(genesisBlock) return bc.persistBlock(genesisBlock)
} }
if ver != version { if ver != version {
@ -119,12 +118,8 @@ func (bc *Blockchain) init() error {
return err return err
} }
for _, hash := range hashes { bc.headerList = NewHeaderHashList(hashes...)
if !genesisBlock.Hash().Equals(hash) { bc.storedHeaderCount = uint32(len(hashes))
bc.headerList.Add(hash)
bc.storedHeaderCount++
}
}
currHeaderHeight, currHeaderHash, err := storage.CurrentHeaderHeight(bc.Store) currHeaderHeight, currHeaderHash, err := storage.CurrentHeaderHeight(bc.Store)
if err != nil { if err != nil {
@ -414,6 +409,9 @@ func (bc *Blockchain) persist(ctx context.Context) (err error) {
} }
bc.blockCache.Delete(hash) bc.blockCache.Delete(hash)
persisted++ persisted++
} else {
// no next block in the cache, no reason to continue looping
break
} }
} }
} }

View file

@ -74,7 +74,7 @@ func HeaderHashes(s Store) ([]util.Uint256, error) {
sort.Sort(uint32Slice(sortedKeys)) sort.Sort(uint32Slice(sortedKeys))
for _, key := range sortedKeys { for _, key := range sortedKeys {
hashes = append(hashes, hashMap[key]...) hashes = append(hashes[:key], hashMap[key]...)
} }
return hashes, nil return hashes, nil