Add light sync transactions table
- Foreign key to header instead of block - Can use same Transaction struct for both
This commit is contained in:
@@ -240,6 +240,9 @@ func TearDown(db *postgres.DB) {
|
||||
_, err = tx.Exec(`DELETE FROM full_sync_transactions`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec("DELETE FROM light_sync_transactions")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM receipts`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
|
||||
@@ -49,11 +49,21 @@ func (repository HeaderRepository) CreateOrUpdateHeader(header core.Header) (int
|
||||
return 0, ErrValidHeaderExists
|
||||
}
|
||||
|
||||
func (repository HeaderRepository) CreateTransaction(headerID int64, transaction core.Transaction) error {
|
||||
_, err := repository.database.Exec(`INSERT INTO public.light_sync_transactions
|
||||
(header_id, hash, tx_to, tx_from, tx_index) VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
headerID, transaction.Hash, transaction.To, transaction.From, transaction.TxIndex)
|
||||
return err
|
||||
}
|
||||
|
||||
func (repository HeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {
|
||||
var header core.Header
|
||||
err := repository.database.Get(&header, `SELECT id, block_number, hash, raw, block_timestamp FROM headers WHERE block_number = $1 AND eth_node_fingerprint = $2`,
|
||||
blockNumber, repository.database.Node.ID)
|
||||
log.Error("GetHeader: error getting headers: ", err)
|
||||
if err != nil {
|
||||
log.Error("GetHeader: error getting headers: ", err)
|
||||
}
|
||||
return header, err
|
||||
}
|
||||
|
||||
|
||||
@@ -180,6 +180,57 @@ var _ = Describe("Block header repository", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("creating a transaction", func() {
|
||||
It("adds a transaction", func() {
|
||||
headerID, err := repo.CreateOrUpdateHeader(header)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
fromAddress := common.HexToAddress("0x1234")
|
||||
toAddress := common.HexToAddress("0x5678")
|
||||
txHash := common.HexToHash("0x9876")
|
||||
txIndex := big.NewInt(123)
|
||||
transaction := core.Transaction{
|
||||
From: fromAddress.Hex(),
|
||||
Hash: txHash.Hex(),
|
||||
To: toAddress.Hex(),
|
||||
TxIndex: txIndex.Int64(),
|
||||
}
|
||||
|
||||
insertErr := repo.CreateTransaction(headerID, transaction)
|
||||
|
||||
Expect(insertErr).NotTo(HaveOccurred())
|
||||
var dbTransaction core.Transaction
|
||||
err = db.Get(&dbTransaction, `SELECT hash, tx_from, tx_to, tx_index FROM public.light_sync_transactions WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(dbTransaction).To(Equal(transaction))
|
||||
})
|
||||
|
||||
It("silently ignores duplicate inserts", func() {
|
||||
headerID, err := repo.CreateOrUpdateHeader(header)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
fromAddress := common.HexToAddress("0x1234")
|
||||
toAddress := common.HexToAddress("0x5678")
|
||||
txHash := common.HexToHash("0x9876")
|
||||
txIndex := big.NewInt(123)
|
||||
transaction := core.Transaction{
|
||||
From: fromAddress.Hex(),
|
||||
Hash: txHash.Hex(),
|
||||
To: toAddress.Hex(),
|
||||
TxIndex: txIndex.Int64(),
|
||||
}
|
||||
|
||||
insertErr := repo.CreateTransaction(headerID, transaction)
|
||||
Expect(insertErr).NotTo(HaveOccurred())
|
||||
|
||||
insertTwoErr := repo.CreateTransaction(headerID, transaction)
|
||||
Expect(insertTwoErr).NotTo(HaveOccurred())
|
||||
|
||||
var dbTransactions []core.Transaction
|
||||
err = db.Select(&dbTransactions, `SELECT hash, tx_from, tx_to, tx_index FROM public.light_sync_transactions WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(dbTransactions)).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Getting a header", func() {
|
||||
It("returns header if it exists", func() {
|
||||
_, err = repo.CreateOrUpdateHeader(header)
|
||||
|
||||
@@ -55,6 +55,7 @@ type FilterRepository interface {
|
||||
|
||||
type HeaderRepository interface {
|
||||
CreateOrUpdateHeader(header core.Header) (int64, error)
|
||||
CreateTransaction(headerID int64, transaction core.Transaction) error
|
||||
GetHeader(blockNumber int64) (core.Header, error)
|
||||
MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) ([]int64, error)
|
||||
}
|
||||
|
||||
@@ -56,6 +56,10 @@ func (repository *MockHeaderRepository) CreateOrUpdateHeader(header core.Header)
|
||||
return repository.createOrUpdateHeaderReturnID, repository.createOrUpdateHeaderErr
|
||||
}
|
||||
|
||||
func (repository *MockHeaderRepository) CreateTransaction(headerID int64, transaction core.Transaction) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (repository *MockHeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {
|
||||
repository.GetHeaderPassedBlockNumber = blockNumber
|
||||
return core.Header{BlockNumber: blockNumber, Hash: repository.getHeaderReturnBlockHash}, repository.getHeaderError
|
||||
|
||||
@@ -69,6 +69,9 @@ func TearDown(db *postgres.DB) {
|
||||
_, err = tx.Exec(`DELETE FROM full_sync_transactions`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec("DELETE FROM light_sync_transactions")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = tx.Exec(`DELETE FROM receipts`)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user