2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// 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 HeaderValidator struct {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain core.BlockChain
|
2018-07-17 21:23:07 +00:00
|
|
|
headerRepository datastore.HeaderRepository
|
|
|
|
windowSize int
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:59:41 +00:00
|
|
|
func NewHeaderValidator(blockChain core.BlockChain, repository datastore.HeaderRepository, windowSize int) HeaderValidator {
|
2018-07-17 21:23:07 +00:00
|
|
|
return HeaderValidator{
|
|
|
|
blockChain: blockChain,
|
|
|
|
headerRepository: repository,
|
|
|
|
windowSize: windowSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 09:58:41 +00:00
|
|
|
func (validator HeaderValidator) ValidateHeaders() (ValidationWindow, error) {
|
2019-02-14 15:03:57 +00:00
|
|
|
window, err := MakeValidationWindow(validator.blockChain, validator.windowSize)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("ValidateHeaders: 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 = RetrieveAndUpdateHeaders(validator.blockChain, validator.headerRepository, blockNumbers)
|
2019-01-11 09:58:41 +00:00
|
|
|
if err != nil {
|
2019-02-14 15:03:57 +00:00
|
|
|
logrus.Error("ValidateHeaders: error getting/updating headers: ", err)
|
2019-01-11 09:58:41 +00:00
|
|
|
return ValidationWindow{}, err
|
|
|
|
}
|
|
|
|
return window, nil
|
2018-07-17 21:23:07 +00:00
|
|
|
}
|