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/>.
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
package history_test
|
|
|
|
|
|
|
|
import (
|
2018-07-20 16:37:46 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2020-01-30 22:35:31 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/history"
|
2018-07-17 21:23:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Populating headers", func() {
|
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
var headerRepository *fakes.MockHeaderRepository
|
2018-07-17 21:23:07 +00:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
headerRepository = fakes.NewMockHeaderRepository()
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
It("returns number of headers added", func() {
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-08-14 21:59:41 +00:00
|
|
|
headersAdded, err := history.PopulateMissingHeaders(blockChain, headerRepository, 1)
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headersAdded).To(Equal(1))
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("adds missing headers to the db", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
headerRepository.SetMissingBlockNumbers([]int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-08-14 21:59:41 +00:00
|
|
|
_, err := history.PopulateMissingHeaders(blockChain, headerRepository, 1)
|
2018-07-17 21:23:07 +00:00
|
|
|
|
2018-07-24 16:38:49 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-07-20 16:37:46 +00:00
|
|
|
headerRepository.AssertCreateOrUpdateHeaderCallCountAndPassedBlockNumbers(1, []int64{2})
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|
2018-12-14 11:11:48 +00:00
|
|
|
|
|
|
|
It("returns early if the db is already synced up to the head of the chain", func() {
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetLastBlock(big.NewInt(2))
|
|
|
|
headersAdded, err := history.PopulateMissingHeaders(blockChain, headerRepository, 2)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(headersAdded).To(Equal(0))
|
|
|
|
})
|
2018-07-17 21:23:07 +00:00
|
|
|
})
|