forked from cerc-io/ipld-eth-server
Updating header_sync_receipts to have FK reference to addresses
This commit is contained in:
@@ -64,6 +64,7 @@ var _ = Describe("address lookup", func() {
|
||||
var addressCount int
|
||||
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
|
||||
Expect(addressErr).NotTo(HaveOccurred())
|
||||
Expect(addressCount).To(Equal(1))
|
||||
})
|
||||
|
||||
It("gets upper-cased addresses", func() {
|
||||
|
||||
@@ -87,13 +87,18 @@ func (repository HeaderRepository) CreateTransactionInTx(tx *sqlx.Tx, headerID i
|
||||
|
||||
func (repository HeaderRepository) CreateReceiptInTx(tx *sqlx.Tx, headerID, transactionID int64, receipt core.Receipt) (int64, error) {
|
||||
var receiptId int64
|
||||
addressId, getAddressErr := AddressRepository{}.GetOrCreateAddressInTransaction(tx, receipt.ContractAddress)
|
||||
if getAddressErr != nil {
|
||||
log.Error("createReceipt: Error getting address id: ", getAddressErr)
|
||||
return receiptId, getAddressErr
|
||||
}
|
||||
err := tx.QueryRowx(`INSERT INTO public.header_sync_receipts
|
||||
(header_id, transaction_id, contract_address, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp)
|
||||
(header_id, transaction_id, contract_address_id, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (header_id, transaction_id) DO UPDATE
|
||||
SET (contract_address, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp) = ($3, $4::NUMERIC, $5::NUMERIC, $6, $7, $8, $9)
|
||||
SET (contract_address_id, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp) = ($3, $4::NUMERIC, $5::NUMERIC, $6, $7, $8, $9)
|
||||
RETURNING id`,
|
||||
headerID, transactionID, receipt.ContractAddress, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, receipt.TxHash, receipt.Rlp).Scan(&receiptId)
|
||||
headerID, transactionID, addressId, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.StateRoot, receipt.Status, receipt.TxHash, receipt.Rlp).Scan(&receiptId)
|
||||
if err != nil {
|
||||
log.Error("header_repository: error inserting receipt: ", err)
|
||||
return receiptId, err
|
||||
|
||||
@@ -217,25 +217,38 @@ var _ = Describe("Block header repository", func() {
|
||||
}
|
||||
|
||||
_, receiptErr := repo.CreateReceiptInTx(tx, headerID, txId, receipt)
|
||||
Expect(receiptErr).ToNot(HaveOccurred())
|
||||
commitErr := tx.Commit()
|
||||
Expect(commitErr).ToNot(HaveOccurred())
|
||||
Expect(receiptErr).ToNot(HaveOccurred())
|
||||
|
||||
type idModel struct {
|
||||
TransactionId int64 `db:"transaction_id"`
|
||||
core.Receipt
|
||||
TransactionId int64 `db:"transaction_id"`
|
||||
ContractAddressId int64 `db:"contract_address_id"`
|
||||
CumulativeGasUsed uint64 `db:"cumulative_gas_used"`
|
||||
GasUsed uint64 `db:"gas_used"`
|
||||
StateRoot string `db:"state_root"`
|
||||
Status int
|
||||
TxHash string `db:"tx_hash"`
|
||||
Rlp []byte `db:"rlp"`
|
||||
}
|
||||
|
||||
var addressId int64
|
||||
getAddressErr := db.Get(&addressId, `SELECT id FROM addresses WHERE address = $1`, contractAddr.Hex())
|
||||
Expect(getAddressErr).NotTo(HaveOccurred())
|
||||
|
||||
var dbReceipt idModel
|
||||
err = db.Get(&dbReceipt,
|
||||
`SELECT transaction_id, contract_address, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp
|
||||
getReceiptErr := db.Get(&dbReceipt,
|
||||
`SELECT transaction_id, contract_address_id, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp
|
||||
FROM public.header_sync_receipts WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(getReceiptErr).NotTo(HaveOccurred())
|
||||
|
||||
Expect(dbReceipt.TransactionId).To(Equal(txId))
|
||||
Expect(dbReceipt.TxHash).To(Equal(txHash.Hex()))
|
||||
Expect(dbReceipt.ContractAddress).To(Equal(contractAddr.Hex()))
|
||||
Expect(dbReceipt.ContractAddressId).To(Equal(addressId))
|
||||
Expect(dbReceipt.CumulativeGasUsed).To(Equal(uint64(100)))
|
||||
Expect(dbReceipt.GasUsed).To(Equal(uint64(10)))
|
||||
Expect(dbReceipt.StateRoot).To(Equal(stateRoot.Hex()))
|
||||
Expect(dbReceipt.Status).To(Equal(0))
|
||||
Expect(dbReceipt.Rlp).To(Equal([]byte{1, 2, 3}))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user