Merge pull request #707 from nspcc-dev/skip-outdated-headers-on-add
core: don't reverify stale headers in addHeader
This commit is contained in:
commit
032cb513a6
2 changed files with 15 additions and 2 deletions
|
@ -333,17 +333,30 @@ func (bc *Blockchain) AddBlock(block *block.Block) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddHeaders processes the given headers and add them to the
|
// AddHeaders processes the given headers and add them to the
|
||||||
// HeaderHashList.
|
// HeaderHashList. It expects headers to be sorted by index.
|
||||||
func (bc *Blockchain) AddHeaders(headers ...*block.Header) error {
|
func (bc *Blockchain) AddHeaders(headers ...*block.Header) error {
|
||||||
return bc.addHeaders(bc.config.VerifyBlocks, headers...)
|
return bc.addHeaders(bc.config.VerifyBlocks, headers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addHeaders is an internal implementation of AddHeaders (`verify` parameter
|
||||||
|
// tells it to verify or not verify given headers).
|
||||||
func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) (err error) {
|
func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) (err error) {
|
||||||
var (
|
var (
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
batch = bc.dao.store.Batch()
|
batch = bc.dao.store.Batch()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if len(headers) > 0 {
|
||||||
|
var i int
|
||||||
|
curHeight := bc.HeaderHeight()
|
||||||
|
for i = range headers {
|
||||||
|
if headers[i].Index > curHeight {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
headers = headers[i:]
|
||||||
|
}
|
||||||
|
|
||||||
if len(headers) == 0 {
|
if len(headers) == 0 {
|
||||||
return nil
|
return nil
|
||||||
} else if verify {
|
} else if verify {
|
||||||
|
|
|
@ -30,7 +30,7 @@ func TestAddHeaders(t *testing.T) {
|
||||||
assert.Equal(t, h3.Hash(), bc.CurrentHeaderHash())
|
assert.Equal(t, h3.Hash(), bc.CurrentHeaderHash())
|
||||||
|
|
||||||
// Add them again, they should not be added.
|
// Add them again, they should not be added.
|
||||||
require.Error(t, bc.AddHeaders(h3, h2, h1))
|
require.NoError(t, bc.AddHeaders(h3, h2, h1))
|
||||||
|
|
||||||
assert.Equal(t, h3.Index, bc.HeaderHeight())
|
assert.Equal(t, h3.Index, bc.HeaderHeight())
|
||||||
assert.Equal(t, uint32(0), bc.BlockHeight())
|
assert.Equal(t, uint32(0), bc.BlockHeight())
|
||||||
|
|
Loading…
Reference in a new issue