2018-01-05 17:55:00 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/inmemory"
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2018-01-05 17:55:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
2018-02-08 16:12:08 +00:00
|
|
|
window := history.ValidationWindow{LowerBound: 1, UpperBound: 3}
|
2018-01-05 17:55:00 +00:00
|
|
|
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},
|
|
|
|
})
|
2018-02-12 16:54:05 +00:00
|
|
|
inMemoryDB := inmemory.NewInMemory()
|
|
|
|
blocksRepository := &inmemory.BlockRepository{InMemory: inMemoryDB}
|
2018-01-05 17:55:00 +00:00
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
validator := history.NewBlockValidator(blockchain, blocksRepository, 2)
|
2018-01-05 17:55:00 +00:00
|
|
|
window := validator.ValidateBlocks()
|
2018-02-08 16:12:08 +00:00
|
|
|
Expect(window).To(Equal(history.ValidationWindow{LowerBound: 5, UpperBound: 7}))
|
2018-02-12 16:54:05 +00:00
|
|
|
Expect(blocksRepository.BlockCount()).To(Equal(2))
|
|
|
|
Expect(blocksRepository.CreateOrUpdateBlockCallCount).To(Equal(2))
|
2018-01-05 17:55:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("logs window message", func() {
|
2018-02-12 16:54:05 +00:00
|
|
|
inMemoryDB := inmemory.NewInMemory()
|
|
|
|
blockRepository := &inmemory.BlockRepository{InMemory: inMemoryDB}
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
expectedMessage := &bytes.Buffer{}
|
2018-02-08 16:12:08 +00:00
|
|
|
window := history.ValidationWindow{LowerBound: 5, UpperBound: 7}
|
|
|
|
history.ParsedWindowTemplate.Execute(expectedMessage, history.ValidationWindow{LowerBound: 5, UpperBound: 7})
|
2018-01-05 17:55:00 +00:00
|
|
|
|
|
|
|
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{})
|
2018-02-12 16:54:05 +00:00
|
|
|
validator := history.NewBlockValidator(blockchain, blockRepository, 2)
|
2018-01-05 17:55:00 +00:00
|
|
|
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)))
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|