Skip to content

Commit

Permalink
Update header table constraint to block_number and node_id (#58)
Browse files Browse the repository at this point in the history
removing the block hash from the constraint
  • Loading branch information
Elizabeth authored Feb 27, 2020
1 parent dd7df7b commit 2c6d59a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion db/migrations/00003_create_headers_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE public.headers
eth_node_id INTEGER NOT NULL REFERENCES eth_nodes (id) ON DELETE CASCADE,
created TIMESTAMP NOT NULL DEFAULT NOW(),
updated TIMESTAMP NOT NULL DEFAULT NOW(),
UNIQUE (block_number, hash, eth_node_id)
UNIQUE (block_number, eth_node_id)
);

-- +goose StatementBegin
Expand Down
4 changes: 2 additions & 2 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ ALTER TABLE ONLY public.goose_db_version


--
-- Name: headers headers_block_number_hash_eth_node_id_key; Type: CONSTRAINT; Schema: public; Owner: -
-- Name: headers headers_block_number_eth_node_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.headers
ADD CONSTRAINT headers_block_number_hash_eth_node_id_key UNIQUE (block_number, hash, eth_node_id);
ADD CONSTRAINT headers_block_number_eth_node_id_key UNIQUE (block_number, eth_node_id);


--
Expand Down
26 changes: 22 additions & 4 deletions pkg/datastore/postgres/repositories/header_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
. "github.com/onsi/gomega"

"github.com/makerdao/vulcanizedb/pkg/core"
"github.com/makerdao/vulcanizedb/pkg/datastore/postgres"
"github.com/makerdao/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/makerdao/vulcanizedb/test_config"
)
Expand All @@ -37,7 +36,7 @@ var _ = Describe("Block header repository", func() {
rawHeader []byte
err error
timestamp string
db *postgres.DB
db = test_config.NewTestDB(test_config.NewTestNode())
repo repositories.HeaderRepository
header core.Header
)
Expand All @@ -46,8 +45,6 @@ var _ = Describe("Block header repository", func() {
rawHeader, err = json.Marshal(types.Header{})
Expect(err).NotTo(HaveOccurred())
timestamp = big.NewInt(123456789).String()

db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db)
repo = repositories.NewHeaderRepository(db)
header = core.Header{
Expand Down Expand Up @@ -129,6 +126,27 @@ var _ = Describe("Block header repository", func() {
Expect(dbHeader.Raw).To(MatchJSON(headerTwo.Raw))
})

It("does not duplicate headers with different hashes in concurrent inserts", func() {
_, err = repo.InternalInsertHeader(header)
Expect(err).NotTo(HaveOccurred())

headerTwo := core.Header{
BlockNumber: header.BlockNumber,
Hash: common.BytesToHash([]byte{5, 4, 3, 2, 1}).Hex(),
Raw: rawHeader,
Timestamp: timestamp,
}

_, err = repo.InternalInsertHeader(headerTwo)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(repositories.ErrValidHeaderExists))

var dbHeaders []core.Header
err = db.Select(&dbHeaders, `SELECT block_number, hash, raw FROM public.headers WHERE block_number = $1`, header.BlockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(len(dbHeaders)).To(Equal(1))
})

It("does not replace header if node id is different", func() {
_, err = repo.CreateOrUpdateHeader(header)
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 2c6d59a

Please sign in to comment.