Updating header_sync_receipts to have FK reference to addresses

This commit is contained in:
Elizabeth Engelman 2019-08-05 09:04:29 -05:00
parent bcd6d14fcd
commit 4e40e892d2
5 changed files with 51 additions and 23 deletions

View File

@ -1,16 +1,17 @@
-- +goose Up -- +goose Up
CREATE TABLE header_sync_receipts( CREATE TABLE header_sync_receipts
id SERIAL PRIMARY KEY, (
transaction_id INTEGER NOT NULL REFERENCES header_sync_transactions(id) ON DELETE CASCADE, id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers(id) ON DELETE CASCADE, transaction_id INTEGER NOT NULL REFERENCES header_sync_transactions (id) ON DELETE CASCADE,
contract_address VARCHAR(42), header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
cumulative_gas_used NUMERIC, contract_address_id INTEGER NOT NULL REFERENCES addresses (id) ON DELETE CASCADE,
gas_used NUMERIC, cumulative_gas_used NUMERIC,
state_root VARCHAR(66), gas_used NUMERIC,
status INTEGER, state_root VARCHAR(66),
tx_hash VARCHAR(66), status INTEGER,
rlp BYTEA, tx_hash VARCHAR(66),
UNIQUE(header_id, transaction_id) rlp BYTEA,
UNIQUE (header_id, transaction_id)
); );

View File

@ -284,7 +284,7 @@ CREATE TABLE public.header_sync_receipts (
id integer NOT NULL, id integer NOT NULL,
transaction_id integer NOT NULL, transaction_id integer NOT NULL,
header_id integer NOT NULL, header_id integer NOT NULL,
contract_address character varying(42), contract_address_id integer NOT NULL,
cumulative_gas_used numeric, cumulative_gas_used numeric,
gas_used numeric, gas_used numeric,
state_root character varying(66), state_root character varying(66),
@ -965,6 +965,14 @@ ALTER TABLE ONLY public.full_sync_transactions
ADD CONSTRAINT full_sync_transactions_block_id_fkey FOREIGN KEY (block_id) REFERENCES public.blocks(id) ON DELETE CASCADE; ADD CONSTRAINT full_sync_transactions_block_id_fkey FOREIGN KEY (block_id) REFERENCES public.blocks(id) ON DELETE CASCADE;
--
-- Name: header_sync_receipts header_sync_receipts_contract_address_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.header_sync_receipts
ADD CONSTRAINT header_sync_receipts_contract_address_id_fkey FOREIGN KEY (contract_address_id) REFERENCES public.addresses(id) ON DELETE CASCADE;
-- --
-- Name: header_sync_receipts header_sync_receipts_header_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- Name: header_sync_receipts header_sync_receipts_header_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
-- --

View File

@ -64,6 +64,7 @@ var _ = Describe("address lookup", func() {
var addressCount int var addressCount int
addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`) addressErr := db.Get(&addressCount, `SELECT count(*) FROM public.addresses`)
Expect(addressErr).NotTo(HaveOccurred()) Expect(addressErr).NotTo(HaveOccurred())
Expect(addressCount).To(Equal(1))
}) })
It("gets upper-cased addresses", func() { It("gets upper-cased addresses", func() {

View File

@ -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) { func (repository HeaderRepository) CreateReceiptInTx(tx *sqlx.Tx, headerID, transactionID int64, receipt core.Receipt) (int64, error) {
var receiptId int64 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 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) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (header_id, transaction_id) DO UPDATE 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`, 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 { if err != nil {
log.Error("header_repository: error inserting receipt: ", err) log.Error("header_repository: error inserting receipt: ", err)
return receiptId, err return receiptId, err

View File

@ -217,25 +217,38 @@ var _ = Describe("Block header repository", func() {
} }
_, receiptErr := repo.CreateReceiptInTx(tx, headerID, txId, receipt) _, receiptErr := repo.CreateReceiptInTx(tx, headerID, txId, receipt)
Expect(receiptErr).ToNot(HaveOccurred())
commitErr := tx.Commit() commitErr := tx.Commit()
Expect(commitErr).ToNot(HaveOccurred()) Expect(commitErr).ToNot(HaveOccurred())
Expect(receiptErr).ToNot(HaveOccurred())
type idModel struct { type idModel struct {
TransactionId int64 `db:"transaction_id"` TransactionId int64 `db:"transaction_id"`
core.Receipt 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 var dbReceipt idModel
err = db.Get(&dbReceipt, getReceiptErr := db.Get(&dbReceipt,
`SELECT transaction_id, contract_address, cumulative_gas_used, gas_used, state_root, status, tx_hash, rlp `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) 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.TransactionId).To(Equal(txId))
Expect(dbReceipt.TxHash).To(Equal(txHash.Hex())) 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.CumulativeGasUsed).To(Equal(uint64(100)))
Expect(dbReceipt.GasUsed).To(Equal(uint64(10))) Expect(dbReceipt.GasUsed).To(Equal(uint64(10)))
Expect(dbReceipt.StateRoot).To(Equal(stateRoot.Hex())) Expect(dbReceipt.StateRoot).To(Equal(stateRoot.Hex()))
Expect(dbReceipt.Status).To(Equal(0))
Expect(dbReceipt.Rlp).To(Equal([]byte{1, 2, 3})) Expect(dbReceipt.Rlp).To(Equal([]byte{1, 2, 3}))
}) })
}) })