2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
package history
|
|
|
|
|
|
|
|
import (
|
2019-02-14 15:03:57 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-17 21:23:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlockValidator struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockchain core.BlockChain
|
2018-07-17 21:23:07 +00:00
|
|
|
blockRepository datastore.BlockRepository
|
|
|
|
windowSize int
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
func NewBlockValidator(blockchain core.BlockChain, blockRepository datastore.BlockRepository, windowSize int) *BlockValidator {
|
2018-07-17 21:23:07 +00:00
|
|
|
return &BlockValidator{
|
|
|
|
blockchain: blockchain,
|
|
|
|
blockRepository: blockRepository,
|
|
|
|
windowSize: windowSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:03:57 +00:00
|
|
|
func (bv BlockValidator) ValidateBlocks() (ValidationWindow, error) {
|
|
|
|
window, err := MakeValidationWindow(bv.blockchain, bv.windowSize)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("ValidateBlocks: error creating validation window: ", err)
|
|
|
|
return ValidationWindow{}, err
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
|
2019-02-14 15:03:57 +00:00
|
|
|
_, err = RetrieveAndUpdateBlocks(bv.blockchain, bv.blockRepository, blockNumbers)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("ValidateBlocks: error getting and updating blocks: ", err)
|
|
|
|
return ValidationWindow{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBlock, err := bv.blockchain.LastBlock()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("ValidateBlocks: error getting last block: ", err)
|
|
|
|
return ValidationWindow{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bv.blockRepository.SetBlocksStatus(lastBlock.Int64())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("ValidateBlocks: error setting block status: ", err)
|
|
|
|
return ValidationWindow{}, err
|
|
|
|
}
|
|
|
|
return window, nil
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|