2019-04-26 15:42:40 +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/>.
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
package transactions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transactions"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2019-03-27 04:05:30 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Transaction syncer", func() {
|
2019-03-27 17:26:04 +00:00
|
|
|
var (
|
|
|
|
blockChain *fakes.MockBlockChain
|
|
|
|
syncer transactions.TransactionsSyncer
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-03-27 04:05:30 +00:00
|
|
|
db := test_config.NewTestDB(test_config.NewTestNode())
|
2019-03-27 17:26:04 +00:00
|
|
|
test_config.CleanTestDB(db)
|
|
|
|
blockChain = fakes.NewMockBlockChain()
|
|
|
|
syncer = transactions.NewTransactionsSyncer(db, blockChain)
|
|
|
|
})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
2019-03-27 17:26:04 +00:00
|
|
|
It("fetches transactions for logs", func() {
|
2019-04-17 21:10:58 +00:00
|
|
|
err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(blockChain.GetTransactionsCalled).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
2019-04-17 21:10:58 +00:00
|
|
|
It("does not fetch transactions if no logs", func() {
|
|
|
|
err := syncer.SyncTransactions(0, []types.Log{})
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(blockChain.GetTransactionsCalled).To(BeFalse())
|
|
|
|
})
|
|
|
|
|
2019-03-27 04:05:30 +00:00
|
|
|
It("only fetches transactions with unique hashes", func() {
|
|
|
|
err := syncer.SyncTransactions(0, []types.Log{{
|
|
|
|
TxHash: fakes.FakeHash,
|
|
|
|
}, {
|
|
|
|
TxHash: fakes.FakeHash,
|
|
|
|
}})
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(blockChain.GetTransactionsPassedHashes)).To(Equal(1))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if fetching transactions fails", func() {
|
|
|
|
blockChain.GetTransactionsError = fakes.FakeError
|
|
|
|
|
2019-04-17 21:10:58 +00:00
|
|
|
err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("passes transactions to repository for persistence", func() {
|
|
|
|
blockChain.Transactions = []core.TransactionModel{{}}
|
|
|
|
mockHeaderRepository := fakes.NewMockHeaderRepository()
|
|
|
|
syncer.Repository = mockHeaderRepository
|
|
|
|
|
2019-04-17 21:10:58 +00:00
|
|
|
err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-03-27 17:26:04 +00:00
|
|
|
Expect(mockHeaderRepository.CreateTransactionsCalled).To(BeTrue())
|
2019-03-27 04:05:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if persisting transactions fails", func() {
|
|
|
|
blockChain.Transactions = []core.TransactionModel{{}}
|
|
|
|
mockHeaderRepository := fakes.NewMockHeaderRepository()
|
2019-03-27 17:26:04 +00:00
|
|
|
mockHeaderRepository.CreateTransactionsError = fakes.FakeError
|
2019-03-27 04:05:30 +00:00
|
|
|
syncer.Repository = mockHeaderRepository
|
|
|
|
|
2019-04-17 21:10:58 +00:00
|
|
|
err := syncer.SyncTransactions(0, []types.Log{{TxHash: fakes.FakeHash}})
|
2019-03-27 04:05:30 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|