2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
2019-05-17 04:15:54 +00:00
|
|
|
"math/big"
|
|
|
|
|
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/fakes"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/history"
|
2017-11-06 20:36:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Populating blocks", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
var blockRepository *fakes.MockBlockRepository
|
2018-02-12 16:54:05 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository = fakes.NewMockBlockRepository()
|
2018-02-12 16:54:05 +00:00
|
|
|
})
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
It("fills in the only missing block (BlockNumber 1)", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{2})
|
2018-02-12 16:54:05 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
blocksAdded, err := history.PopulateMissingBlocks(blockChain, blockRepository, 1)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
_, err = blockRepository.GetBlock(1)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2018-01-02 18:39:55 +00:00
|
|
|
Expect(blocksAdded).To(Equal(1))
|
2017-12-13 16:51:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
|
|
|
|
2018-01-02 18:39:55 +00:00
|
|
|
It("fills in the three missing blocks (Numbers: 5,8,10)", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(13))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{5, 8, 10})
|
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
blocksAdded, err := history.PopulateMissingBlocks(blockChain, blockRepository, 5)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-01-02 18:39:55 +00:00
|
|
|
Expect(blocksAdded).To(Equal(3))
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(3, []int64{5, 8, 10})
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns the number of blocks created", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(6))
|
|
|
|
blockRepository.SetMissingBlockNumbersReturnArray([]int64{4, 5})
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
numberOfBlocksCreated, err := history.PopulateMissingBlocks(blockChain, blockRepository, 3)
|
2017-11-06 20:36:12 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2017-11-06 20:36:12 +00:00
|
|
|
Expect(numberOfBlocksCreated).To(Equal(2))
|
|
|
|
})
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
It("updates the repository with a range of blocks w/in the range ", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
_, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, history.MakeRange(2, 5))
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository.AssertCreateOrUpdateBlocksCallCountAndBlockNumbersEquals(4, []int64{2, 3, 4, 5})
|
2017-12-20 20:06:22 +00:00
|
|
|
})
|
2018-01-05 17:55:00 +00:00
|
|
|
|
2018-03-27 21:06:12 +00:00
|
|
|
It("does not call repository create block when there is an error", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetGetBlockByNumberErr(fakes.FakeError)
|
2018-03-27 21:06:12 +00:00
|
|
|
blocks := history.MakeRange(1, 10)
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
_, err := history.RetrieveAndUpdateBlocks(blockChain, blockRepository, blocks)
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2019-02-14 15:04:12 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
blockRepository.AssertCreateOrUpdateBlockCallCountEquals(0)
|
2018-03-27 21:06:12 +00:00
|
|
|
})
|
2017-11-06 20:36:12 +00:00
|
|
|
})
|