Remove pubsub and replace w/ polling head of chain (#122)

* 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)
This commit is contained in:
Matt K
2018-01-05 11:55:00 -06:00
committed by GitHub
parent 095cb1e7b7
commit 6decf0b54b
38 changed files with 368 additions and 1034 deletions
+6 -29
View File
@@ -1,48 +1,25 @@
package history
import (
"log"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
type Window struct {
LowerBound int
UpperBound int
MaxBlockNumber int
}
func (window Window) Size() int {
return int(window.UpperBound - window.LowerBound)
}
func PopulateMissingBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int {
lastBlock := blockchain.LastBlock().Int64()
blockRange := repository.MissingBlockNumbers(startingBlockNumber, lastBlock-1)
updateBlockRange(blockchain, repository, blockRange)
log.SetPrefix("")
log.Printf("Backfilling %d blocks\n\n", len(blockRange))
RetrieveAndUpdateBlocks(blockchain, repository, blockRange)
return len(blockRange)
}
func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window {
maxBlockNumber := blockchain.LastBlock().Int64()
upperBound := maxBlockNumber - int64(1)
lowerBound := upperBound - int64(windowSize)
blockRange := MakeRange(lowerBound, upperBound)
updateBlockRange(blockchain, repository, blockRange)
return Window{int(lowerBound), int(upperBound), int(maxBlockNumber)}
}
func updateBlockRange(blockchain core.Blockchain, repository repositories.Repository, blockNumbers []int64) int {
func RetrieveAndUpdateBlocks(blockchain core.Blockchain, repository repositories.Repository, blockNumbers []int64) int {
for _, blockNumber := range blockNumbers {
block := blockchain.GetBlockByNumber(blockNumber)
repository.CreateOrUpdateBlock(block)
}
return len(blockNumbers)
}
func MakeRange(min, max int64) []int64 {
a := make([]int64, max-min)
for i := range a {
a[i] = min + int64(i)
}
return a
}
+8 -32
View File
@@ -66,30 +66,6 @@ var _ = Describe("Populating blocks", func() {
Expect(err).To(HaveOccurred())
})
It("updates the repository with a range of blocks w/in sliding window ", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 1},
{Number: 2},
{Number: 3},
{Number: 4},
{Number: 5},
})
repository := repositories.NewInMemory()
repository.CreateOrUpdateBlock(blockchain.GetBlockByNumber(5))
history.UpdateBlocksWindow(blockchain, repository, 2)
Expect(repository.BlockCount()).To(Equal(3))
Expect(repository.HandleBlockCallCount).To(Equal(3))
})
It("Generates a range of int64", func() {
numberOfBlocksCreated := history.MakeRange(0, 5)
expected := []int64{0, 1, 2, 3, 4}
Expect(numberOfBlocksCreated).To(Equal(expected))
})
It("returns the number of blocks created", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4},
@@ -105,19 +81,19 @@ var _ = Describe("Populating blocks", func() {
Expect(numberOfBlocksCreated).To(Equal(2))
})
It("returns the window size", func() {
window := history.Window{1, 3, 10}
Expect(window.Size()).To(Equal(2))
})
It("returns the number of largest block", func() {
It("updates the repository with a range of blocks w/in the range ", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 1},
{Number: 2},
{Number: 3},
{Number: 4},
{Number: 5},
})
maxBlockNumber := blockchain.LastBlock()
repository := repositories.NewInMemory()
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
history.RetrieveAndUpdateBlocks(blockchain, repository, history.MakeRange(2, 5))
Expect(repository.BlockCount()).To(Equal(3))
Expect(repository.CreateOrUpdateBlockCallCount).To(Equal(3))
})
})
+68
View File
@@ -0,0 +1,68 @@
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
}
+90
View File
@@ -0,0 +1,90 @@
package history_test
import (
"bytes"
"io/ioutil"
"log"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/fakes"
"github.com/8thlight/vulcanizedb/pkg/history"
"github.com/8thlight/vulcanizedb/pkg/repositories"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func init() {
log.SetOutput(ioutil.Discard)
}
var _ = Describe("Blocks validator", func() {
It("creates a ValidationWindow equal to (HEAD-windowSize, HEAD)", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 1},
{Number: 2},
{Number: 3},
{Number: 4},
{Number: 5},
})
validationWindow := history.MakeValidationWindow(blockchain, 2)
Expect(validationWindow.LowerBound).To(Equal(int64(3)))
Expect(validationWindow.UpperBound).To(Equal(int64(5)))
})
It("returns the window size", func() {
window := history.ValidationWindow{1, 3}
Expect(window.Size()).To(Equal(2))
})
It("calls create or update for all blocks within the window", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4},
{Number: 5},
{Number: 6},
{Number: 7},
})
repository := repositories.NewInMemory()
validator := history.NewBlockValidator(blockchain, repository, 2)
window := validator.ValidateBlocks()
Expect(window).To(Equal(history.ValidationWindow{5, 7}))
Expect(repository.BlockCount()).To(Equal(2))
Expect(repository.CreateOrUpdateBlockCallCount).To(Equal(2))
})
It("logs window message", func() {
expectedMessage := &bytes.Buffer{}
window := history.ValidationWindow{5, 7}
history.ParsedWindowTemplate.Execute(expectedMessage, history.ValidationWindow{5, 7})
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{})
repository := repositories.NewInMemory()
validator := history.NewBlockValidator(blockchain, repository, 2)
actualMessage := &bytes.Buffer{}
validator.Log(actualMessage, window)
Expect(actualMessage).To(Equal(expectedMessage))
})
It("generates a range of int64s", func() {
numberOfBlocksCreated := history.MakeRange(0, 5)
expected := []int64{0, 1, 2, 3, 4}
Expect(numberOfBlocksCreated).To(Equal(expected))
})
It("returns the number of largest block", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 1},
{Number: 2},
{Number: 3},
})
maxBlockNumber := blockchain.LastBlock()
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
})
})