downloader: minor changes in tests

This commit is contained in:
Martin Holst Swende 2020-05-13 10:09:21 +02:00
parent 5f5e753e1d
commit c2712e57cf
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -242,27 +242,32 @@ func (dl *downloadTester) GetTd(hash common.Hash, number uint64) *big.Int {
func (dl *downloadTester) InsertHeaderChain(headers []*types.Header, checkFreq int) (i int, err error) { func (dl *downloadTester) InsertHeaderChain(headers []*types.Header, checkFreq int) (i int, err error) {
dl.lock.Lock() dl.lock.Lock()
defer dl.lock.Unlock() defer dl.lock.Unlock()
// Do a quick check, as the blockchain.InsertHeaderChain doesn't insert anything in case of errors // Do a quick check, as the blockchain.InsertHeaderChain doesn't insert anything in case of errors
if _, ok := dl.ownHeaders[headers[0].ParentHash]; !ok { if _, ok := dl.ownHeaders[headers[0].ParentHash]; !ok {
return 0, errors.New("unknown parent") return 0, errors.New("InsertHeaderChain: unknown parent at first position")
} }
var hashes []common.Hash
for i := 1; i < len(headers); i++ { for i := 1; i < len(headers); i++ {
hash := headers[i-1].Hash()
if headers[i].ParentHash != headers[i-1].Hash() { if headers[i].ParentHash != headers[i-1].Hash() {
return i, errors.New("unknown parent") return i, fmt.Errorf("non-contiguous import at position %d", i)
} }
hashes = append(hashes, hash)
} }
hashes = append(hashes, headers[len(headers)-1].Hash())
// Do a full insert if pre-checks passed // Do a full insert if pre-checks passed
for i, header := range headers { for i, header := range headers {
if _, ok := dl.ownHeaders[header.Hash()]; ok { hash := hashes[i]
if _, ok := dl.ownHeaders[hash]; ok {
continue continue
} }
if _, ok := dl.ownHeaders[header.ParentHash]; !ok { if _, ok := dl.ownHeaders[header.ParentHash]; !ok {
return i, errors.New("unknown parent") // This _should_ be impossible, due to precheck and induction
return i, fmt.Errorf("InsertHeaderChain: unknown parent at position %d", i)
} }
dl.ownHashes = append(dl.ownHashes, header.Hash()) dl.ownHashes = append(dl.ownHashes, hash)
dl.ownHeaders[header.Hash()] = header dl.ownHeaders[hash] = header
dl.ownChainTd[header.Hash()] = new(big.Int).Add(dl.ownChainTd[header.ParentHash], header.Difficulty) dl.ownChainTd[hash] = new(big.Int).Add(dl.ownChainTd[header.ParentHash], header.Difficulty)
} }
return len(headers), nil return len(headers), nil
} }
@ -274,9 +279,9 @@ func (dl *downloadTester) InsertChain(blocks types.Blocks) (i int, err error) {
for i, block := range blocks { for i, block := range blocks {
if parent, ok := dl.ownBlocks[block.ParentHash()]; !ok { if parent, ok := dl.ownBlocks[block.ParentHash()]; !ok {
return i, errors.New("unknown parent") return i, fmt.Errorf("InsertChain: unknown parent at position %d / %d", i, len(blocks))
} else if _, err := dl.stateDb.Get(parent.Root().Bytes()); err != nil { } else if _, err := dl.stateDb.Get(parent.Root().Bytes()); err != nil {
return i, fmt.Errorf("unknown parent state %x: %v", parent.Root(), err) return i, fmt.Errorf("InsertChain: unknown parent state %x: %v", parent.Root(), err)
} }
if _, ok := dl.ownHeaders[block.Hash()]; !ok { if _, ok := dl.ownHeaders[block.Hash()]; !ok {
dl.ownHashes = append(dl.ownHashes, block.Hash()) dl.ownHashes = append(dl.ownHashes, block.Hash())
@ -301,7 +306,7 @@ func (dl *downloadTester) InsertReceiptChain(blocks types.Blocks, receipts []typ
} }
if _, ok := dl.ancientBlocks[blocks[i].ParentHash()]; !ok { if _, ok := dl.ancientBlocks[blocks[i].ParentHash()]; !ok {
if _, ok := dl.ownBlocks[blocks[i].ParentHash()]; !ok { if _, ok := dl.ownBlocks[blocks[i].ParentHash()]; !ok {
return i, errors.New("unknown parent") return i, errors.New("InsertReceiptChain: unknown parent")
} }
} }
if blocks[i].NumberU64() <= ancientLimit { if blocks[i].NumberU64() <= ancientLimit {