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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth_test
|
2019-08-28 18:41:49 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-14 06:26:46 +00:00
|
|
|
"context"
|
|
|
|
|
2023-09-21 06:55:26 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/interfaces"
|
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"
|
2021-02-19 20:23:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2022-03-11 04:32:22 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-04-14 06:26:46 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2019-08-28 18:41:49 +00:00
|
|
|
. "github.com/onsi/gomega"
|
2023-04-14 06:26:46 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth/test_helpers"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/shared"
|
2019-08-28 18:41:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Retriever", func() {
|
2019-11-01 19:03:28 +00:00
|
|
|
var (
|
2022-03-11 04:32:22 +00:00
|
|
|
db *sqlx.DB
|
|
|
|
diffIndexer interfaces.StateDiffIndexer
|
2023-03-03 00:27:30 +00:00
|
|
|
retriever *eth.Retriever
|
2023-04-14 06:26:46 +00:00
|
|
|
ctx = context.Background()
|
2019-11-01 19:03:28 +00:00
|
|
|
)
|
2019-08-28 18:41:49 +00:00
|
|
|
BeforeEach(func() {
|
2022-03-17 10:48:18 +00:00
|
|
|
db = shared.SetupDB()
|
2022-03-21 06:11:34 +00:00
|
|
|
diffIndexer = shared.SetupTestStateDiffIndexer(ctx, params.TestChainConfig, test_helpers.Genesis.Hash())
|
2021-10-07 09:35:11 +00:00
|
|
|
|
2023-03-03 00:27:30 +00:00
|
|
|
retriever = eth.NewRetriever(db)
|
2019-08-28 18:41:49 +00:00
|
|
|
})
|
|
|
|
AfterEach(func() {
|
2022-03-17 10:48:18 +00:00
|
|
|
shared.TearDownDB(db)
|
2023-04-14 06:26:46 +00:00
|
|
|
db.Close()
|
2019-08-28 18:41:49 +00:00
|
|
|
})
|
2019-08-28 22:07:36 +00:00
|
|
|
|
2023-04-14 06:26:46 +00:00
|
|
|
It("Retrieve", func() {
|
|
|
|
tx, err := diffIndexer.PushBlock(test_helpers.MockBlock, test_helpers.MockReceipts, test_helpers.MockBlock.Difficulty())
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
2023-04-14 06:26:46 +00:00
|
|
|
for _, node := range test_helpers.MockStateNodes {
|
|
|
|
err = diffIndexer.PushStateNode(tx, node, test_helpers.MockBlock.Hash().String())
|
2021-08-12 06:23:41 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-04-14 06:26:46 +00:00
|
|
|
}
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2023-09-30 14:37:53 +00:00
|
|
|
err = tx.Submit()
|
2023-04-14 06:26:46 +00:00
|
|
|
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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2023-09-30 14:37:53 +00:00
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveFirstBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-09-15 11:43:12 +00:00
|
|
|
Expect(num).To(Equal(int64(1)))
|
2019-08-28 18:41:49 +00:00
|
|
|
})
|
2019-11-01 19:03:28 +00:00
|
|
|
|
|
|
|
It("Gets the number of the first block that has data in the database", func() {
|
2020-10-20 20:33:18 +00:00
|
|
|
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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2023-09-30 14:37:53 +00:00
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveFirstBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-11-01 19:03:28 +00:00
|
|
|
Expect(num).To(Equal(int64(1010101)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Gets the number of the first block that has data in the database", func() {
|
2020-10-20 20:33:18 +00:00
|
|
|
payload1 := test_helpers.MockConvertedPayload
|
2020-08-05 03:34:49 +00:00
|
|
|
payload1.Block = newMockBlock(1010101)
|
2019-11-01 19:03:28 +00:00
|
|
|
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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
|
|
|
tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty())
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
|
|
|
err = tx.Submit()
|
2021-08-12 06:23:41 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveFirstBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-11-01 19:03:28 +00:00
|
|
|
Expect(num).To(Equal(int64(5)))
|
|
|
|
})
|
2019-08-28 18:41:49 +00:00
|
|
|
})
|
2019-08-28 22:07:36 +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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveLastBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-09-15 11:43:12 +00:00
|
|
|
Expect(num).To(Equal(int64(1)))
|
2019-08-28 18:41:49 +00:00
|
|
|
})
|
2019-11-01 19:03:28 +00:00
|
|
|
|
|
|
|
It("Gets the number of the latest block that has data in the database", func() {
|
2020-10-20 20:33:18 +00:00
|
|
|
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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2023-09-30 14:37:53 +00:00
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveLastBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-11-01 19:03:28 +00:00
|
|
|
Expect(num).To(Equal(int64(1010101)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Gets the number of the latest block that has data in the database", func() {
|
2020-10-20 20:33:18 +00:00
|
|
|
payload1 := test_helpers.MockConvertedPayload
|
2020-08-05 03:34:49 +00:00
|
|
|
payload1.Block = newMockBlock(1010101)
|
2019-11-01 19:03:28 +00:00
|
|
|
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())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
|
|
|
err = tx.Submit()
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-12 06:23:41 +00:00
|
|
|
|
|
|
|
tx, err = diffIndexer.PushBlock(payload2.Block, payload2.Receipts, payload2.Block.Difficulty())
|
2020-01-16 20:48:38 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-09-30 14:37:53 +00:00
|
|
|
defer tx.RollbackOnFailure(err)
|
|
|
|
err = tx.Submit()
|
2021-08-12 06:23:41 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
num, err := retriever.RetrieveLastBlockNumber()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2019-11-01 19:03:28 +00:00
|
|
|
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 {
|
2020-10-20 20:33:18 +00:00
|
|
|
header := test_helpers.MockHeader
|
2020-08-05 03:34:49 +00:00
|
|
|
header.Number.SetUint64(blockNumber)
|
2023-04-14 06:26:46 +00:00
|
|
|
return types.NewBlock(&test_helpers.MockHeader, test_helpers.MockTransactions, nil, test_helpers.MockReceipts, trie.NewEmpty(nil))
|
2020-08-05 03:34:49 +00:00
|
|
|
}
|