forked from cerc-io/ipld-eth-server
6decf0b54b
* Rename geth package structs to not be prefaced with package name * No longer need to dump schema since Travis uses migrate * Rearrange history package * Removed double request for receipt from block rewards * Remove Listener + Observers and Replace w/ Polling Head * Potential Short term Issue w/ Infura (ignore these tests for now)
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package history
|
|
|
|
import (
|
|
"io"
|
|
"text/template"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
|
)
|
|
|
|
const WindowTemplate = `Validating Blocks
|
|
|{{.LowerBound}}|-- Validation Window --|{{.UpperBound}}| ({{.UpperBound}}:HEAD)
|
|
|
|
`
|
|
|
|
var ParsedWindowTemplate = *template.Must(template.New("window").Parse(WindowTemplate))
|
|
|
|
type BlockValidator struct {
|
|
blockchain core.Blockchain
|
|
repository repositories.Repository
|
|
windowSize int
|
|
parsedLoggingTemplate template.Template
|
|
}
|
|
|
|
func NewBlockValidator(blockchain core.Blockchain, repository repositories.Repository, windowSize int) *BlockValidator {
|
|
return &BlockValidator{
|
|
blockchain,
|
|
repository,
|
|
windowSize,
|
|
ParsedWindowTemplate,
|
|
}
|
|
}
|
|
|
|
func (bv BlockValidator) ValidateBlocks() ValidationWindow {
|
|
window := MakeValidationWindow(bv.blockchain, bv.windowSize)
|
|
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
|
|
RetrieveAndUpdateBlocks(bv.blockchain, bv.repository, blockNumbers)
|
|
lastBlock := bv.blockchain.LastBlock().Int64()
|
|
bv.repository.SetBlocksStatus(lastBlock)
|
|
return window
|
|
}
|
|
|
|
func (bv BlockValidator) Log(out io.Writer, window ValidationWindow) {
|
|
bv.parsedLoggingTemplate.Execute(out, window)
|
|
}
|
|
|
|
type ValidationWindow struct {
|
|
LowerBound int64
|
|
UpperBound int64
|
|
}
|
|
|
|
func (window ValidationWindow) Size() int {
|
|
return int(window.UpperBound - window.LowerBound)
|
|
}
|
|
|
|
func MakeValidationWindow(blockchain core.Blockchain, windowSize int) ValidationWindow {
|
|
upperBound := blockchain.LastBlock().Int64()
|
|
lowerBound := upperBound - int64(windowSize)
|
|
return ValidationWindow{lowerBound, upperBound}
|
|
}
|
|
|
|
func MakeRange(min, max int64) []int64 {
|
|
a := make([]int64, max-min)
|
|
for i := range a {
|
|
a[i] = min + int64(i)
|
|
}
|
|
return a
|
|
}
|