forked from cerc-io/ipld-eth-server
Canonical blocks (#108)
* Update Block w/ newest Block * Add cascading delete to blocks and transactions tables * Add handling for new conflicting blocks * Command line version of sliding window n behind HEAD
This commit is contained in:
@@ -5,11 +5,43 @@ import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
func PopulateBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int {
|
||||
blockNumbers := repository.MissingBlockNumbers(startingBlockNumber, repository.MaxBlockNumber())
|
||||
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 {
|
||||
blockRange := repository.MissingBlockNumbers(startingBlockNumber, repository.MaxBlockNumber())
|
||||
updateBlockRange(blockchain, repository, blockRange)
|
||||
return len(blockRange)
|
||||
}
|
||||
|
||||
func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window {
|
||||
maxBlockNumber := repository.MaxBlockNumber()
|
||||
upperBound := repository.MaxBlockNumber() - int64(2)
|
||||
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 {
|
||||
for _, blockNumber := range blockNumbers {
|
||||
block := blockchain.GetBlockByNumber(blockNumber)
|
||||
repository.CreateBlock(block)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -15,16 +15,16 @@ var _ = Describe("Populating blocks", func() {
|
||||
blocks := []core.Block{{Number: 1, Hash: "x012343"}}
|
||||
blockchain := fakes.NewBlockchainWithBlocks(blocks)
|
||||
repository := repositories.NewInMemory()
|
||||
repository.CreateBlock(core.Block{Number: 2})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 2})
|
||||
|
||||
history.PopulateBlocks(blockchain, repository, 1)
|
||||
history.PopulateMissingBlocks(blockchain, repository, 1)
|
||||
|
||||
block, err := repository.FindBlockByNumber(1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(block.Hash).To(Equal("x012343"))
|
||||
})
|
||||
|
||||
It("fills in two missing blocks", func() {
|
||||
It("fills in the three missing blocks (5,8,10)", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
@@ -33,16 +33,16 @@ var _ = Describe("Populating blocks", func() {
|
||||
{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})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 1})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 2})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 3})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 6})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 7})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 9})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 11})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 12})
|
||||
|
||||
history.PopulateBlocks(blockchain, repository, 5)
|
||||
history.PopulateMissingBlocks(blockchain, repository, 5)
|
||||
|
||||
Expect(repository.BlockCount()).To(Equal(11))
|
||||
_, err := repository.FindBlockByNumber(4)
|
||||
@@ -57,18 +57,47 @@ 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},
|
||||
{Number: 5},
|
||||
})
|
||||
repository := repositories.NewInMemory()
|
||||
repository.CreateBlock(core.Block{Number: 3})
|
||||
repository.CreateBlock(core.Block{Number: 6})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 3})
|
||||
repository.CreateOrUpdateBlock(core.Block{Number: 6})
|
||||
|
||||
numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, 3)
|
||||
numberOfBlocksCreated := history.PopulateMissingBlocks(blockchain, repository, 3)
|
||||
|
||||
Expect(numberOfBlocksCreated).To(Equal(2))
|
||||
})
|
||||
|
||||
It("returns the window size", func() {
|
||||
window := history.Window{1, 3, 10}
|
||||
Expect(window.Size()).To(Equal(2))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user