From 4e40e892d217a250b7f245580bb1741eb2cadb04 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Mon, 5 Aug 2019 09:04:29 -0500 Subject: [PATCH] Updating header_sync_receipts to have FK reference to addresses --- ...0027_create_header_sync_receipts_table.sql | 25 ++++++++--------- db/schema.sql | 10 ++++++- .../repositories/address_repository_test.go | 1 + .../repositories/header_repository.go | 11 +++++--- .../repositories/header_repository_test.go | 27 ++++++++++++++----- 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/db/migrations/00027_create_header_sync_receipts_table.sql b/db/migrations/00027_create_header_sync_receipts_table.sql index ebd0a17b..842527f4 100644 --- a/db/migrations/00027_create_header_sync_receipts_table.sql +++ b/db/migrations/00027_create_header_sync_receipts_table.sql @@ -1,16 +1,17 @@ -- +goose Up -CREATE TABLE header_sync_receipts( - id SERIAL PRIMARY KEY, - transaction_id INTEGER NOT NULL REFERENCES header_sync_transactions(id) ON DELETE CASCADE, - header_id INTEGER NOT NULL REFERENCES headers(id) ON DELETE CASCADE, - contract_address VARCHAR(42), - cumulative_gas_used NUMERIC, - gas_used NUMERIC, - state_root VARCHAR(66), - status INTEGER, - tx_hash VARCHAR(66), - rlp BYTEA, - UNIQUE(header_id, transaction_id) +CREATE TABLE header_sync_receipts +( + id SERIAL PRIMARY KEY, + transaction_id INTEGER NOT NULL REFERENCES header_sync_transactions (id) ON DELETE CASCADE, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + contract_address_id INTEGER NOT NULL REFERENCES addresses (id) ON DELETE CASCADE, + cumulative_gas_used NUMERIC, + gas_used NUMERIC, + state_root VARCHAR(66), + status INTEGER, + tx_hash VARCHAR(66), + rlp BYTEA, + UNIQUE (header_id, transaction_id) ); diff --git a/db/schema.sql b/db/schema.sql index 45801bea..5470a107 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -284,7 +284,7 @@ CREATE TABLE public.header_sync_receipts ( id integer NOT NULL, transaction_id integer NOT NULL, header_id integer NOT NULL, - contract_address character varying(42), + contract_address_id integer NOT NULL, cumulative_gas_used numeric, gas_used numeric, 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; +-- +-- 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: - -- diff --git a/pkg/datastore/postgres/repositories/address_repository_test.go b/pkg/datastore/postgres/repositories/address_repository_test.go index 3935b9b5..1ac53672 100644 --- a/pkg/datastore/postgres/repositories/address_repository_test.go +++ b/pkg/datastore/postgres/repositories/address_repository_test.go @@ -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() { diff --git a/pkg/datastore/postgres/repositories/header_repository.go b/pkg/datastore/postgres/repositories/header_repository.go index 62ff7ecd..4429fe8d 100644 --- a/pkg/datastore/postgres/repositories/header_repository.go +++ b/pkg/datastore/postgres/repositories/header_repository.go @@ -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 diff --git a/pkg/datastore/postgres/repositories/header_repository_test.go b/pkg/datastore/postgres/repositories/header_repository_test.go index 68d20aa2..bf9ea80c 100644 --- a/pkg/datastore/postgres/repositories/header_repository_test.go +++ b/pkg/datastore/postgres/repositories/header_repository_test.go @@ -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})) }) })