ipld-eth-server/pkg/eth/cid_retriever_test.go

171 lines
6.0 KiB
Go
Raw Normal View History

2019-08-28 18:41:49 +00:00
// VulcanizeDB
// Copyright © 2019 Vulcanize
// 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/>.
package eth_test
2019-08-28 18:41:49 +00:00
import (
"github.com/cerc-io/ipld-eth-server/v4/pkg/eth"
"github.com/cerc-io/ipld-eth-server/v4/pkg/eth/test_helpers"
"github.com/cerc-io/ipld-eth-server/v4/pkg/shared"
2022-03-11 04:32:22 +00:00
"github.com/ethereum/go-ethereum/core/types"
2021-08-12 06:23:41 +00:00
"github.com/ethereum/go-ethereum/params"
2022-03-11 04:32:22 +00:00
"github.com/ethereum/go-ethereum/statediff/indexer/interfaces"
"github.com/ethereum/go-ethereum/trie"
2022-03-11 04:32:22 +00:00
"github.com/jmoiron/sqlx"
2019-08-28 18:41:49 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Retriever", func() {
var (
2022-03-11 04:32:22 +00:00
db *sqlx.DB
diffIndexer interfaces.StateDiffIndexer
2021-08-12 06:23:41 +00:00
retriever *eth.CIDRetriever
)
2019-08-28 18:41:49 +00:00
BeforeEach(func() {
2022-03-17 10:48:18 +00:00
db = shared.SetupDB()
diffIndexer = shared.SetupTestStateDiffIndexer(ctx, params.TestChainConfig, test_helpers.Genesis.Hash())
2020-08-31 15:47:06 +00:00
retriever = eth.NewCIDRetriever(db)
2019-08-28 18:41:49 +00:00
})
AfterEach(func() {
2022-03-17 10:48:18 +00:00
shared.TearDownDB(db)
2019-08-28 18:41:49 +00:00
})
Describe("Retrieve", func() {
BeforeEach(func() {
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty())
Expect(err).ToNot(HaveOccurred())
for _, node := range test_helpers.MockStateNodes {
2022-03-11 04:32:22 +00:00
err = diffIndexer.PushStateNode(tx, node, test_helpers.MockBlock.Hash().String())
2021-08-12 06:23:41 +00:00
Expect(err).ToNot(HaveOccurred())
}
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
})
2019-08-28 18:41:49 +00:00
})
Describe("RetrieveFirstBlockNumber", func() {
2020-08-05 03:34:49 +00:00
It("Throws an error if there are no blocks in the database", func() {
_, err := retriever.RetrieveFirstBlockNumber()
Expect(err).To(HaveOccurred())
})
2019-08-28 18:41:49 +00:00
It("Gets the number of the first block that has data in the database", func() {
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
num, err := retriever.RetrieveFirstBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(1)))
2019-08-28 18:41:49 +00:00
})
It("Gets the number of the first block that has data in the database", func() {
payload := test_helpers.MockConvertedPayload
2020-08-05 03:34:49 +00:00
payload.Block = newMockBlock(1010101)
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(payload.Block, payload.Receipts, payload.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
num, err := retriever.RetrieveFirstBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(1010101)))
})
It("Gets the number of the first block that has data in the database", func() {
payload1 := test_helpers.MockConvertedPayload
2020-08-05 03:34:49 +00:00
payload1.Block = newMockBlock(1010101)
payload2 := payload1
2020-08-05 03:34:49 +00:00
payload2.Block = newMockBlock(5)
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(payload1.Block, payload1.Receipts, payload1.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
2021-08-12 06:23:41 +00:00
Expect(err).ToNot(HaveOccurred())
num, err := retriever.RetrieveFirstBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(5)))
})
2019-08-28 18:41:49 +00:00
})
2019-08-28 18:41:49 +00:00
Describe("RetrieveLastBlockNumber", func() {
2020-08-05 03:34:49 +00:00
It("Throws an error if there are no blocks in the database", func() {
_, err := retriever.RetrieveLastBlockNumber()
Expect(err).To(HaveOccurred())
})
2019-08-28 18:41:49 +00:00
It("Gets the number of the latest block that has data in the database", func() {
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
num, err := retriever.RetrieveLastBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(1)))
2019-08-28 18:41:49 +00:00
})
It("Gets the number of the latest block that has data in the database", func() {
payload := test_helpers.MockConvertedPayload
2020-08-05 03:34:49 +00:00
payload.Block = newMockBlock(1010101)
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(payload.Block, payload.Receipts, payload.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
num, err := retriever.RetrieveLastBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(1010101)))
})
It("Gets the number of the latest block that has data in the database", func() {
payload1 := test_helpers.MockConvertedPayload
2020-08-05 03:34:49 +00:00
payload1.Block = newMockBlock(1010101)
payload2 := payload1
2020-08-05 03:34:49 +00:00
payload2.Block = newMockBlock(5)
2021-08-12 06:23:41 +00:00
tx, err := diffIndexer.PushBlock(payload1.Block, payload1.Receipts, payload1.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
Expect(err).ToNot(HaveOccurred())
2021-08-12 06:23:41 +00:00
tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty())
Expect(err).ToNot(HaveOccurred())
2022-03-11 04:32:22 +00:00
err = tx.Submit(err)
2021-08-12 06:23:41 +00:00
Expect(err).ToNot(HaveOccurred())
num, err := retriever.RetrieveLastBlockNumber()
Expect(err).ToNot(HaveOccurred())
Expect(num).To(Equal(int64(1010101)))
})
})
2019-08-28 18:41:49 +00:00
})
2020-08-05 03:34:49 +00:00
func newMockBlock(blockNumber uint64) *types.Block {
header := test_helpers.MockHeader
2020-08-05 03:34:49 +00:00
header.Number.SetUint64(blockNumber)
return types.NewBlock(&test_helpers.MockHeader, test_helpers.MockTransactions, nil, test_helpers.MockReceipts, new(trie.Trie))
2020-08-05 03:34:49 +00:00
}