Address PR comments

This commit is contained in:
Rob Mulholand 2019-03-27 12:26:04 -05:00
parent 54d46638a8
commit 81dfd12665
8 changed files with 69 additions and 74 deletions

View File

@ -32,12 +32,10 @@ func (syncer TransactionsSyncer) SyncTransactions(headerID int64, logs []types.L
if transactionErr != nil { if transactionErr != nil {
return transactionErr return transactionErr
} }
for _, transaction := range transactions { writeErr := syncer.Repository.CreateTransactions(headerID, transactions)
writeErr := syncer.Repository.CreateTransaction(headerID, transaction)
if writeErr != nil { if writeErr != nil {
return writeErr return writeErr
} }
}
return nil return nil
} }

View File

@ -11,11 +11,19 @@ import (
) )
var _ = Describe("Transaction syncer", func() { var _ = Describe("Transaction syncer", func() {
It("fetches transactions for logs", func() { var (
db := test_config.NewTestDB(test_config.NewTestNode()) blockChain *fakes.MockBlockChain
blockChain := fakes.NewMockBlockChain() syncer transactions.TransactionsSyncer
syncer := transactions.NewTransactionsSyncer(db, blockChain) )
BeforeEach(func() {
db := test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db)
blockChain = fakes.NewMockBlockChain()
syncer = transactions.NewTransactionsSyncer(db, blockChain)
})
It("fetches transactions for logs", func() {
err := syncer.SyncTransactions(0, []types.Log{}) err := syncer.SyncTransactions(0, []types.Log{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -23,10 +31,6 @@ var _ = Describe("Transaction syncer", func() {
}) })
It("only fetches transactions with unique hashes", func() { It("only fetches transactions with unique hashes", func() {
db := test_config.NewTestDB(test_config.NewTestNode())
blockChain := fakes.NewMockBlockChain()
syncer := transactions.NewTransactionsSyncer(db, blockChain)
err := syncer.SyncTransactions(0, []types.Log{{ err := syncer.SyncTransactions(0, []types.Log{{
TxHash: fakes.FakeHash, TxHash: fakes.FakeHash,
}, { }, {
@ -38,10 +42,7 @@ var _ = Describe("Transaction syncer", func() {
}) })
It("returns error if fetching transactions fails", func() { It("returns error if fetching transactions fails", func() {
db := test_config.NewTestDB(test_config.NewTestNode())
blockChain := fakes.NewMockBlockChain()
blockChain.GetTransactionsError = fakes.FakeError blockChain.GetTransactionsError = fakes.FakeError
syncer := transactions.NewTransactionsSyncer(db, blockChain)
err := syncer.SyncTransactions(0, []types.Log{}) err := syncer.SyncTransactions(0, []types.Log{})
@ -50,26 +51,20 @@ var _ = Describe("Transaction syncer", func() {
}) })
It("passes transactions to repository for persistence", func() { It("passes transactions to repository for persistence", func() {
db := test_config.NewTestDB(test_config.NewTestNode())
blockChain := fakes.NewMockBlockChain()
blockChain.Transactions = []core.TransactionModel{{}} blockChain.Transactions = []core.TransactionModel{{}}
syncer := transactions.NewTransactionsSyncer(db, blockChain)
mockHeaderRepository := fakes.NewMockHeaderRepository() mockHeaderRepository := fakes.NewMockHeaderRepository()
syncer.Repository = mockHeaderRepository syncer.Repository = mockHeaderRepository
err := syncer.SyncTransactions(0, []types.Log{}) err := syncer.SyncTransactions(0, []types.Log{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(mockHeaderRepository.CreateTransactionCalled).To(BeTrue()) Expect(mockHeaderRepository.CreateTransactionsCalled).To(BeTrue())
}) })
It("returns error if persisting transactions fails", func() { It("returns error if persisting transactions fails", func() {
db := test_config.NewTestDB(test_config.NewTestNode())
blockChain := fakes.NewMockBlockChain()
blockChain.Transactions = []core.TransactionModel{{}} blockChain.Transactions = []core.TransactionModel{{}}
syncer := transactions.NewTransactionsSyncer(db, blockChain)
mockHeaderRepository := fakes.NewMockHeaderRepository() mockHeaderRepository := fakes.NewMockHeaderRepository()
mockHeaderRepository.CreateTransactionError = fakes.FakeError mockHeaderRepository.CreateTransactionsError = fakes.FakeError
syncer.Repository = mockHeaderRepository syncer.Repository = mockHeaderRepository
err := syncer.SyncTransactions(0, []types.Log{}) err := syncer.SyncTransactions(0, []types.Log{})

View File

@ -2,18 +2,18 @@ package datastore
import "fmt" import "fmt"
var ErrBlockDoesNotExist = func(blockNumber int64) error { func ErrBlockDoesNotExist(blockNumber int64) error {
return fmt.Errorf("Block number %d does not exist", blockNumber) return fmt.Errorf("Block number %d does not exist", blockNumber)
} }
var ErrContractDoesNotExist = func(contractHash string) error { func ErrContractDoesNotExist(contractHash string) error {
return fmt.Errorf("Contract %v does not exist", contractHash) return fmt.Errorf("Contract %v does not exist", contractHash)
} }
var ErrFilterDoesNotExist = func(name string) error { func ErrFilterDoesNotExist(name string) error {
return fmt.Errorf("filter %s does not exist", name) return fmt.Errorf("filter %s does not exist", name)
} }
var ErrReceiptDoesNotExist = func(txHash string) error { func ErrReceiptDoesNotExist(txHash string) error {
return fmt.Errorf("Receipt for tx: %v does not exist", txHash) return fmt.Errorf("Receipt for tx: %v does not exist", txHash)
} }

View File

@ -49,15 +49,20 @@ func (repository HeaderRepository) CreateOrUpdateHeader(header core.Header) (int
return 0, ErrValidHeaderExists return 0, ErrValidHeaderExists
} }
func (repository HeaderRepository) CreateTransaction(headerID int64, transaction core.TransactionModel) error { func (repository HeaderRepository) CreateTransactions(headerID int64, transactions []core.TransactionModel) error {
for _, transaction := range transactions {
_, err := repository.database.Exec(`INSERT INTO public.light_sync_transactions _, err := repository.database.Exec(`INSERT INTO public.light_sync_transactions
(header_id, hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value") (header_id, hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value")
VALUES ($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9::NUMERIC, $10, $11::NUMERIC) VALUES ($1, $2, $3::NUMERIC, $4::NUMERIC, $5, $6::NUMERIC, $7, $8, $9::NUMERIC, $10, $11::NUMERIC)
ON CONFLICT DO NOTHING`, headerID, transaction.Hash, transaction.GasLimit, transaction.GasPrice, ON CONFLICT DO NOTHING`, headerID, transaction.Hash, transaction.GasLimit, transaction.GasPrice,
transaction.Data, transaction.Nonce, transaction.Raw, transaction.From, transaction.TxIndex, transaction.To, transaction.Data, transaction.Nonce, transaction.Raw, transaction.From, transaction.TxIndex, transaction.To,
transaction.Value) transaction.Value)
if err != nil {
return err return err
} }
}
return nil
}
func (repository HeaderRepository) GetHeader(blockNumber int64) (core.Header, error) { func (repository HeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {
var header core.Header var header core.Header

View File

@ -181,14 +181,21 @@ var _ = Describe("Block header repository", func() {
}) })
Describe("creating a transaction", func() { Describe("creating a transaction", func() {
It("adds a transaction", func() { var (
headerID, err := repo.CreateOrUpdateHeader(header) headerID int64
transactions []core.TransactionModel
)
BeforeEach(func() {
var err error
headerID, err = repo.CreateOrUpdateHeader(header)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
fromAddress := common.HexToAddress("0x1234") fromAddress := common.HexToAddress("0x1234")
toAddress := common.HexToAddress("0x5678") toAddress := common.HexToAddress("0x5678")
txHash := common.HexToHash("0x9876") txHash := common.HexToHash("0x9876")
txHashTwo := common.HexToHash("0x5432")
txIndex := big.NewInt(123) txIndex := big.NewInt(123)
transaction := core.TransactionModel{ transactions = []core.TransactionModel{{
Data: []byte{}, Data: []byte{},
From: fromAddress.Hex(), From: fromAddress.Hex(),
GasLimit: 0, GasLimit: 0,
@ -199,44 +206,34 @@ var _ = Describe("Block header repository", func() {
To: toAddress.Hex(), To: toAddress.Hex(),
TxIndex: txIndex.Int64(), TxIndex: txIndex.Int64(),
Value: "0", Value: "0",
} }, {
Data: []byte{},
insertErr := repo.CreateTransaction(headerID, transaction) From: fromAddress.Hex(),
GasLimit: 1,
GasPrice: 1,
Hash: txHashTwo.Hex(),
Nonce: 1,
Raw: []byte{},
To: toAddress.Hex(),
TxIndex: 1,
Value: "1",
}}
insertErr := repo.CreateTransactions(headerID, transactions)
Expect(insertErr).NotTo(HaveOccurred()) Expect(insertErr).NotTo(HaveOccurred())
var dbTransaction core.TransactionModel })
err = db.Get(&dbTransaction,
It("adds transactions", func() {
var dbTransactions []core.TransactionModel
err = db.Select(&dbTransactions,
`SELECT hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value" `SELECT hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value"
FROM public.light_sync_transactions WHERE header_id = $1`, headerID) FROM public.light_sync_transactions WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbTransaction).To(Equal(transaction)) Expect(dbTransactions).To(ConsistOf(transactions))
}) })
It("silently ignores duplicate inserts", func() { It("silently ignores duplicate inserts", func() {
headerID, err := repo.CreateOrUpdateHeader(header) insertTwoErr := repo.CreateTransactions(headerID, transactions)
Expect(err).NotTo(HaveOccurred())
fromAddress := common.HexToAddress("0x1234")
toAddress := common.HexToAddress("0x5678")
txHash := common.HexToHash("0x9876")
txIndex := big.NewInt(123)
transaction := core.TransactionModel{
Data: []byte{},
From: fromAddress.Hex(),
GasLimit: 0,
GasPrice: 0,
Hash: txHash.Hex(),
Nonce: 0,
Raw: []byte{},
Receipt: core.Receipt{},
To: toAddress.Hex(),
TxIndex: txIndex.Int64(),
Value: "0",
}
insertErr := repo.CreateTransaction(headerID, transaction)
Expect(insertErr).NotTo(HaveOccurred())
insertTwoErr := repo.CreateTransaction(headerID, transaction)
Expect(insertTwoErr).NotTo(HaveOccurred()) Expect(insertTwoErr).NotTo(HaveOccurred())
var dbTransactions []core.TransactionModel var dbTransactions []core.TransactionModel
@ -244,7 +241,7 @@ var _ = Describe("Block header repository", func() {
`SELECT hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value" `SELECT hash, gaslimit, gasprice, input_data, nonce, raw, tx_from, tx_index, tx_to, "value"
FROM public.light_sync_transactions WHERE header_id = $1`, headerID) FROM public.light_sync_transactions WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(dbTransactions)).To(Equal(1)) Expect(len(dbTransactions)).To(Equal(2))
}) })
}) })

View File

@ -41,7 +41,7 @@ type FilterRepository interface {
type HeaderRepository interface { type HeaderRepository interface {
CreateOrUpdateHeader(header core.Header) (int64, error) CreateOrUpdateHeader(header core.Header) (int64, error)
CreateTransaction(headerID int64, transaction core.TransactionModel) error CreateTransactions(headerID int64, transactions []core.TransactionModel) error
GetHeader(blockNumber int64) (core.Header, error) GetHeader(blockNumber int64) (core.Header, error)
MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) ([]int64, error) MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) ([]int64, error)
} }

View File

@ -27,8 +27,8 @@ type MockHeaderRepository struct {
createOrUpdateHeaderErr error createOrUpdateHeaderErr error
createOrUpdateHeaderPassedBlockNumbers []int64 createOrUpdateHeaderPassedBlockNumbers []int64
createOrUpdateHeaderReturnID int64 createOrUpdateHeaderReturnID int64
CreateTransactionCalled bool CreateTransactionsCalled bool
CreateTransactionError error CreateTransactionsError error
getHeaderError error getHeaderError error
getHeaderReturnBlockHash string getHeaderReturnBlockHash string
missingBlockNumbers []int64 missingBlockNumbers []int64
@ -58,9 +58,9 @@ func (repository *MockHeaderRepository) CreateOrUpdateHeader(header core.Header)
return repository.createOrUpdateHeaderReturnID, repository.createOrUpdateHeaderErr return repository.createOrUpdateHeaderReturnID, repository.createOrUpdateHeaderErr
} }
func (repository *MockHeaderRepository) CreateTransaction(headerID int64, transaction core.TransactionModel) error { func (repository *MockHeaderRepository) CreateTransactions(headerID int64, transactions []core.TransactionModel) error {
repository.CreateTransactionCalled = true repository.CreateTransactionsCalled = true
return repository.CreateTransactionError return repository.CreateTransactionsError
} }
func (repository *MockHeaderRepository) GetHeader(blockNumber int64) (core.Header, error) { func (repository *MockHeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {

View File

@ -25,8 +25,8 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"