core: optimize blockchain init with existing DB
There is no need to generate genesis block (it's already in the DB) and we can add all hashes at once without iterating over them.
This commit is contained in:
parent
9281348846
commit
7812fb28ea
1 changed files with 7 additions and 12 deletions
|
@ -84,12 +84,6 @@ func NewBlockchain(ctx context.Context, s storage.Store, cfg config.ProtocolConf
|
|||
}
|
||||
|
||||
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.
|
||||
ver, err := storage.Version(bc.Store)
|
||||
if err != nil {
|
||||
|
@ -97,6 +91,11 @@ func (bc *Blockchain) init() error {
|
|||
if err = storage.PutVersion(bc.Store, version); err != nil {
|
||||
return err
|
||||
}
|
||||
genesisBlock, err := createGenesisBlock(bc.config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bc.headerList = NewHeaderHashList(genesisBlock.Hash())
|
||||
return bc.persistBlock(genesisBlock)
|
||||
}
|
||||
if ver != version {
|
||||
|
@ -119,12 +118,8 @@ func (bc *Blockchain) init() error {
|
|||
return err
|
||||
}
|
||||
|
||||
for _, hash := range hashes {
|
||||
if !genesisBlock.Hash().Equals(hash) {
|
||||
bc.headerList.Add(hash)
|
||||
bc.storedHeaderCount++
|
||||
}
|
||||
}
|
||||
bc.headerList = NewHeaderHashList(hashes...)
|
||||
bc.storedHeaderCount = uint32(len(hashes))
|
||||
|
||||
currHeaderHeight, currHeaderHash, err := storage.CurrentHeaderHeight(bc.Store)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue