Add ability to populate missing blocks

* The command populates up to the highest known block number
 * The anticipated use case is that the listener will be running
   in parallel to the populateBlocks command
    * This will mean that the listener is responsible for picking up
      new blocks, and the populateBlocks command is reposible for
      historical blocks
 * Reformat SQL statements
This commit is contained in:
Eric Meyer
2017-11-08 14:52:38 -06:00
parent cdc861e5f2
commit 4c84173bc0
15 changed files with 315 additions and 19 deletions
+13
View File
@@ -0,0 +1,13 @@
package history_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestHistory(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "History Suite")
}
+15
View File
@@ -0,0 +1,15 @@
package history
import (
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
func PopulateBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int {
blockNumbers := repository.MissingBlockNumbers(startingBlockNumber, repository.MaxBlockNumber())
for _, blockNumber := range blockNumbers {
block := blockchain.GetBlockByNumber(blockNumber)
repository.CreateBlock(block)
}
return len(blockNumbers)
}
+69
View File
@@ -0,0 +1,69 @@
package history_test
import (
"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"
)
var _ = Describe("Populating blocks", func() {
It("fills in the only missing block", func() {
blocks := []core.Block{{Number: 1, Hash: "x012343"}}
blockchain := fakes.NewBlockchainWithBlocks(blocks)
repository := repositories.NewInMemory()
repository.CreateBlock(core.Block{Number: 2})
history.PopulateBlocks(blockchain, repository, 1)
block := repository.FindBlockByNumber(1)
Expect(block).NotTo(BeNil())
Expect(block.Hash).To(Equal("x012343"))
})
It("fills in two missing blocks", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4},
{Number: 5},
{Number: 8},
{Number: 10},
{Number: 13},
})
repository := repositories.NewInMemory()
repository.CreateBlock(core.Block{Number: 1})
repository.CreateBlock(core.Block{Number: 2})
repository.CreateBlock(core.Block{Number: 3})
repository.CreateBlock(core.Block{Number: 6})
repository.CreateBlock(core.Block{Number: 7})
repository.CreateBlock(core.Block{Number: 9})
repository.CreateBlock(core.Block{Number: 11})
repository.CreateBlock(core.Block{Number: 12})
history.PopulateBlocks(blockchain, repository, 5)
Expect(repository.BlockCount()).To(Equal(11))
Expect(repository.FindBlockByNumber(4)).To(BeNil())
Expect(repository.FindBlockByNumber(5)).NotTo(BeNil())
Expect(repository.FindBlockByNumber(8)).NotTo(BeNil())
Expect(repository.FindBlockByNumber(10)).NotTo(BeNil())
Expect(repository.FindBlockByNumber(13)).To(BeNil())
})
It("returns the number of blocks created", func() {
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
{Number: 4},
{Number: 5},
})
repository := repositories.NewInMemory()
repository.CreateBlock(core.Block{Number: 3})
repository.CreateBlock(core.Block{Number: 6})
numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, 3)
Expect(numberOfBlocksCreated).To(Equal(2))
})
})